the main idea is to add a temporary item to the leaders sub-lists which acts as the section header. when the list is printed, that item is popped (simultaneously removed and stored in a variable). the remaining items in the list are the leaders.
here is the code for sorting leaders by civilization (in def sortLeaders() in PlatyPediaMain.py):
Code:
if iType == 1:
lList0 = []
for iCivilization in xrange(gc.getNumCivilizationInfos()):
CivInfo = gc.getCivilizationInfo(iCivilization)
if CivInfo.isGraphicalOnly(): continue
if not CivInfo.isPlayable(): continue
lItem = []
## CA START
lItem.append(CivInfo)
## CA END
for item in lList:
if CivInfo.isLeaders(item[1]):
lItem.append(item)
lList0.append(lItem)
## CA START
lList0.sort(key=lambda x: x[0].getDescription() )
## CA END
return lList0
My additions are in between the ## CA START and ## CA END. In this case, I add the CivInfo to the top of lItem. Then before returning the whole list lList0, sort it by that same item we just added. This works the same for Traits.
then when we print the leaders lists (in both PlatyPediaMain.py and PlatyPediaLeader.py), we use this code:
Code:
if self.iSortLeaders == 1:
## CA MODIFIED START
CivInfo = list.pop(0)
sList = CivInfo.getShortDescription(0)
sButton = CivInfo.getButton()
## CA MODIFIED END
I modified this whole section. list.pop(0) removes the first item from list and returns its value. from the previous code, the first item in the list is the CivInfo, and the rest are leaders info (after we modified the code). here we save it in a variable with the same name, and we can use it for other purposes, such as retrieving the description and button.
here is how it looks when sorting by traits:
Code:
elif self.iSortLeaders == 2:
## CA MODIFIED START
sList = CyTranslator().getText(self.sTraitIcon, ()) + list.pop(0).getDescription()
#sList = CyTranslator().getText(self.top.sTraitIcon, ()) + gc.getTraitInfo(iList).getDescription()
## CA MODIFIED END
(I commented out the original line so you can compare). We just pop in the middle of the assignment since we just need to use it once (i.e. no button).
For Religion, it's a bit different due to an extra unidentified value. In this case, we just check for this exception, like so (in def sortLeaders() in PlatyPediaMain.py):
Code:
if iType == 4:
lList0 = []
for iReligion in xrange(-1, gc.getNumReligionInfos()):
lItem = []
## CA START
if iReligion == -1:
lItem.append( None )
else:
lItem.append( gc.getReligionInfo(iReligion) )
## CA END
for item in lList:
Info = gc.getLeaderHeadInfo(item[1])
if iReligion == Info.getFavoriteReligion():
lItem.append(item)
lList0.append(lItem)
## CA START
lList1 = sorted( lList0[1:], key=lambda x: x[0].getDescription() )
lList1.insert(0, lList0[0])
return lList1
## CA END
obviously if iReligion == -1 is the NONE criteria, so we append the python None. At the end, sort the rest of the list starting from the second item--hence lList0[1:]. Finally, we re-insert the NONE item anywhere. in this case, before all other lists to retain same functionality. (or if you want, after other lists if we consider it miscellaneous.)
so when we print the leaders sorted by religions, we use this code (again in both PlatyPediaMain.py and PlatyPediaLeader.py):
Code:
else:
## CA MODIFIED START
Religion = list.pop(0)
if Religion:
sList = Religion.getDescription()
sButton = Religion.getButton()
else:
sList = CyTranslator().getText("TXT_KEY_MAIN_MENU_NONE", ())
sButton = CyArtFileMgr().getInterfaceArtInfo("INTERFACE_BUTTONS_CANCEL").getPath()
## CA MODIFIED END
again, we pop the first item and store it. then we check if it's None or not, and act accordingly.
that's all we have to change. so each sort type is independent and can be sorted by XML order, alphabetical, or whatever. for instance, we didn't change sorting by civics, which works just like before, i.e. by civic categories as written in the XML, which i think is just fine. we can use the same methodology for other pages, not just leaders.
(P.S. Through this, I learned a lot about Python and how powerfully it handles arrays with compact code.)