As you mentioned, there are two approaches you can take:
- use my custom event handler and merge your handlers ito it
- use your custom event handler and merge my alerts into it
I'll start with the first option, because as you'll eventually read, it's
much easier. The main thing you need to do is register your event manager's handler functions with my event manager. You would do this with calls like in the the example below. Note that although I think these code snippets are correct, I haven't actually run it and tested them, so there may be minor errors and typos.
Code:
def __init__(self, eventManager):
# initialize base class
self.parent = CvEventManager.CvEventManager
self.parent.__init__(self)
eventManager.addEventHandler("unitMove", self.onUnitMove)
eventManager.addEventHandler("unitCreated", self.onUnitCreated)
eventManager.addEventHandler("unitKilled", self.onUnitKilled)
# ... repeat for all event handlers ...
eventManager.addEventHandler("religionSpread", self.onReligionSpread)
The string names of the event handlers can be found in the original CvEventManager class where EventHandlerMap is initialized (around line 73-ish). As you can see from the above examples, most of the time the pattern is to remove "on" from the handler function, but that isn't always true, so make sure you check the names. It's particularly inconsistent when you're dealing with the onGame* and Save/Load types of events.
Then call your event manager's constructor from my CvCustomEventManager __init__ function. Let's assume for the sake of discussion that you rename your class to CvSpockoEventManager.
Code:
CvSpockoEventManager.CvSpockoEventManager(self)
The only obvious problem I see with this approach, based on a quick scan of your code, is the self.Events[7501] thing you're doing to install the popup handler. The easiest solution would probably be to just stick that line right in my __init__ function. So to sum up, you would wind up with a CvCustomEventManager constructor that looks something like this.
Code:
def __init__(self, *args, **kwargs):
super(CvCustomEventManager, self).__init__(*args, **kwargs)
# map the initial EventHandlerMap values into the new data structure
for eventType, eventHandler in self.EventHandlerMap.iteritems():
self.setEventHandler(eventType, eventHandler)
# --> INSERT EVENT HANDLER INITIALIZATION HERE <--
Civ4lerts.Civ4lerts(self)
CvSpockoEventManager.CvSpockoEventManager(self)
self.Events[7501] = ('FreeTechPopup', self.__eventFreeTechApply , self.__eventFreeTechBegin)
I think that's it. Done. It should work.
Alternatively, if you wanted to keep your event manager and call out to my handler functions, you could do that as well. I think that will require more code and/or will be more complicated, but I'm obviously biased. The main reason for the difficulty is that the deep class hierarchy of my alerts, while it makes it easy to add new alerts with a minimal amount of new code, it also makes it somewhat harder to quickly see what event handler functions are used by each alert. It also means that there are quite a few places where you would need to make changes to my code to let them interact with a different event manager.
For example, let's take the CityPendingGrowth alert to start with. That class implements an onCityPendingGrowth function, but that's not a "real" event handler. It's faked by the base class. If you look at the base class, you'll see that it implements an onCityDoTurn function. We'll need to plug that into your event manager. You would need to implement something like this.
Code:
def onCityDoTurn(self, argsList):
cityPendingGrowth.onCityDoTurn(argsList)
And at the beginning of your module in the same place where you initialize FreeTech and TechWindow, also create a CityPendingGrowth object.
Code:
cityPendingGrowth = Civ4lerts.CityPendingGrowth()
But notice that the CityPendingGrowth constructor expects a single argument -- the event manager. It will use that argument to register its event handlers. So that code needs to be removed in order to let it work with a more conventional event manager. You need to remove the eventManager argument from the AbstractAlert, AbstractCityPendingGrowth, and CityPendingGrowth classes. You also need to remove the addEventHandler call from the AbstractCityPendingGrowth class. You would wind up with an AbstractCityPendingGrowth constructor like this.
Code:
def __init__(self, *args, **kwargs):
super(AbstractCityPendingGrowth, self).__init__(*args, **kwargs)
I'm not going to continue this further, because I think I've made my case. Good, bad, for better or for worse, it's just not that easy to make my alerts work with a conventional event manager. I'd recommend the first approach if you want to merge the two.
Basically my event manager is fairly compatible with an old style event manager, but the old style event managers aren't very compatible with my event handlers.