Hehe, I'll just say it won't be trivial and will be complicated by the . . . intriguing . . . coding of PLE.
The basic idea was to create a data structure to contain the visual style of each unit. The code already builds a data structure of some data to allow it to sort the units. This would be similar.
Then you compare the structures you are about to draw to those that you previously drew and update the graphical elements with only the differences. I highly recommend creating a simple class in a new PLE module (Contrib/PlotList.py? or add it to BUG/UnitUtil.py?) to hold the data instead of packing it into a list.
Code:
class PlotList:
def __init__(self, pUnit):
self.bSelected = ...
self.eUnit = pUnit.getUnitType()
self.iDamage = pUnit.getDamage()
self.iMovesLeft = pUnit.movesLeft()
self.iMoves = pUnit.getMoves()
self.bPromo = ...
self.bGG = ...
self.bUpgrade = ...
self.iMission = ...
You can pull all the code to assign values to those fields from PLE itself. The only tricky one is the mission tag. I don't remember if I have created the set of constants for them (orders and missions) yet or not. See BUG/OrderUtil.
Then the draw code will just compare the previous to current PlotList object field-by-field and only change the visual elements where the values are different. For example,
Code:
if prev.bSelected != new.bSelected:
screen.setState(szUnitIcon, new.bSelected)
You will need to handle the cases where a cell in the grid didn't have a unit and does now and vice versa. You could make this easier by creating functions to draw each individual element.
Code:
if prev is None:
if new is not None:
# draw all visual elements using new
screen.setImageButton(szUnitIcon, getUnitIcon(new.eUnitType))
...
else:
# no unit in both views, nothing to do
pass
elif new is None:
# hide all visual elements
screen.hide(szUnitIcon)
...
else:
# test each field in prev to new, drawing the differences
if prev.bSelected != new.bSelected:
screen.setState(szUnitIcon, new.bSelected)
...
Don't forget to remove the code that hides everything before drawing as the point is to only hide/show when necessary.
Hmm, now that I think about it, perhaps an easier pattern is this:
Code:
if new is None:
if prev is not None:
# hide all elements
else:
# change all different elements
if prev is None or prev.bSelected != new.bSelected:
screen.setState(szUnitIcon, new.bSelected)
...
if prev is None:
# show elements
screen.show(szUnitIcon)
Note that some elements are strictly handled by showing/hiding them (upgade and promo indicators), and some may be hidden sometimes (mission tag).
Oh, you also need to deal with the case of options changing. I recommend creating a reset() function that redraws the entire plot list ignoring the previous entries. You can call this function in an onChange handler for most of the PLE options.
Edit: Honestly, this is the type of comprehensive feature that would convince me to rewrite the whole plot list code to integrate PLE with the old style as far as drawing goes. I would keep both types of behavior and visual style, but merge the code that handles the redrawing so that both types are sped up by this change.
You would need a few major function breakdowns:
- Sorting units. PLE: filters and sorting. Standard: just sorting
- Placing units into cells. For PLE, apply view mode. Standard, only mode is multiline. This step creates the PlotList objects above.
- Draw. This code can be mostly uniform since the Standard vs. PLE style differences are applied when placing the original controls (health bar below).
I would also move all this code to a new module. That would be totally freaking awesome.
[*]