Clay
06-12-2005 22:43:54
I'm planning on reimplementing the Ogre event system and I would like some input on the plan. Right now there are multiple Director classes which must be subclassed to add callback functionality to ogre. These include the MouseMotionListener, MouseListener, KeyListener, and FrameListener (are there any I am missing here?).
My idea is to mark all of these director interfaces as depreciated and implement a more pythonic interface for all of this. To do this there will be a new class called "CallbackManager" (any suggestions for a better name?) which allows you to register any Python callable object (such as a member function of a class) to handle the event. For example, instead of inheriting from FrameListener, this would be the new code:
I also plan to add priority to each of these functions to allow you to determine which order things are called in:
This garuntees that firstFrameStarted is called before secondFrameStarted. You can leave off the priority and it is assigned a default priority of something to be determined.
Why the changes?
My idea is to mark all of these director interfaces as depreciated and implement a more pythonic interface for all of this. To do this there will be a new class called "CallbackManager" (any suggestions for a better name?) which allows you to register any Python callable object (such as a member function of a class) to handle the event. For example, instead of inheriting from FrameListener, this would be the new code:
def onFrameStarted(evt):
return True
class MyEventHandler:
def __init__(self):
cm = CallbackManager.getSingleton()
cm.addFrameStartedListener(onFrameStarted)
cm.addKeyUpListener(self.onKeyUp)
cm.addMouseMoveListener(self.onMouseMove)
def onKeyUp(self, evt):
pass
def onMouseMove(self, evt):
pass
I also plan to add priority to each of these functions to allow you to determine which order things are called in:
cm.addFrameStartedListener(self.firstFrameStarted, 1)
cm.addFrameStartedListener(self.secondFrameStarted, 2)
This garuntees that firstFrameStarted is called before secondFrameStarted. You can leave off the priority and it is assigned a default priority of something to be determined.
Why the changes?
- First of all this cleans up a lot of the code that uses PyOgre. Subclassing/redefining is not generally the "pythonic" way to do it. Allowing an arbitrary function to be used allows us to use any python callable which has the correct parameters/return values. This could be a class's member function or even just a stand-alone function.[/*:m]
- The Ogre API does not handle priority on events. This allows us to ensure listeners are called in the proper order when it is needed. This comes up often and the ogre developers have said "if this is the case, then create your own listeners and add priority that way", which is what we are doing...but in C++ code which leads us to... [/*:m]
- This method of handling frame events should be much faster than the current swig director system. This is because only registered events get callbacks and you don't have to deal with the wrapper-generated-overhead. Also since the priority is handled in C++ land (and only when you add/remove new event handlers...which is generally only done rarely) you will not experiance any slowdown associated with priority handling. Finally, since I will only be registering the class as a callback handler if events are actually registered for them, there will not be needless callbacks in C++ land either.[/*:m]
- This also allows us to fix some of the broken API with Ogre's event system, such as MouseClicked not working and removing once-and-forall the unneeded methods for some of the MouseMotionListener interface which does not get used.[/*:m][/list:u]
Overall I think this will make the library much more stable and useable. However I would like to remove the directors that this replaces altogether. This would be a huge interface breaking change, and would not be fully implemented until at least Dagon (1.1.0). That is, the class will be added, but the director classes FrameListener, KeyListener, etc. Would be marked as depreciated but not actually removed until Dagon's release.
If there are any questions, comments, or concerns about this I'm happy to discuss it now while I'm still in the planning phase.