On 2/21/08, jotham <[EMAIL PROTECTED]> wrote:
>
>  Hey guys.
>
>  I was just wondering what the logic was behind the way the event
>  dispatcher currently works.
>
>  In the code below I have to name the string value of my test event
>  something different from the name of the variable.  This appears to be
>  because as you register_event_type the EventDispatcher adds a new
>  attribute to the class in question with the same name as the event
>  string name.

It doesn't; the event names are kept in a separate list.

>  Right now the error you get is very ambiguous if you accidentally name
>  your event the same as an attribute the class already has.  Maybe that
>  could be modified or checked for or noted in the documentation? (If it
>  already is I apologise, I did not see it).

The EventDispatcher considers a method on itself named after the event
to be a handler for that event.  This useful to provide default
handlers, and also to allow easy adding of event handlers to a
subclass.  For example, you can subclass Window and write an
on_resize(self, width, height) method; this will override the default
handler.

EventDispatcher doesn't distinguish between methods and attributes
with the event name.  I suppose it could; but that would just
complicate an already confusing situation, I suspect (your TEST_EVENT
attribute would get lost when someone tries to handle that event in a
subclass).

>  class Test (event.EventDispatcher):
>     TEST_EVENT = 'TEST_EVENT_'

Incidentally, this was an old pattern used in earlier pre-alpha
versions of pyglet.  We ended up throwing it out because a string
constant (like 'on_resize') is just as easy to write/remember as a
named constant (like "ON_RESIZE") but cuts down on repetition and
errors.

Alex.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to