Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: event types are actual types #2759

Open
Starbuck5 opened this issue Mar 19, 2024 · 3 comments · May be fixed by #3121
Open

Idea: event types are actual types #2759

Starbuck5 opened this issue Mar 19, 2024 · 3 comments · May be fixed by #3121
Labels
event pygame.event New API This pull request may need extra debate as it adds a new class or function to pygame

Comments

@Starbuck5
Copy link
Member

This isn't fully developed, mostly putting this up so it doesn't get forgotten

Currently, event types don't have great autocomplete or type hinting correctness, because they are very generic. They are all instances of Event.

What if a pygame.KEYDOWN event, for example, was an instance of a KeydownEvent class, a subclass of Event? The KeydownEvent class could have its own docstring, type stub for properties, properties docstrings with descriptions, and it could all be type checked effectively.

In a game loop, one would do a bunch of isinstance() calls, rather than event.type checks. For example:

if isinstance(event, pygame.KeyDownEvent):
    if event.key == pygame.K_1:
        window.maximize()

This could be backwards compatible even, since each subclass could still export .type correctly to the old style constants (pygame.KEYDOWN)

  • How would this work elegantly with custom event types made by users?
  • Is this a pygame-ce 3 level thing?
@Starbuck5 Starbuck5 added New API This pull request may need extra debate as it adds a new class or function to pygame event pygame.event labels Mar 19, 2024
@robertpfeiffer
Copy link
Contributor

I looked into a similar thing: Have all key/event enums be python enums. Unfortunately, it's difficult to translate between python enums and C.

@gresm
Copy link
Contributor

gresm commented Apr 17, 2024

How would this work elegantly with custom event types made by users?

Sub-classing Event class could do the trick. The id could be automatically generated at class creation (this can be even implemented in python using __init__subclass__ and setting a class property).

The question, that I have, is how the library would behave when firing an event using event id? - Would it create normal Event class, or would it lookup the subclass and attempt creating its instance?

@gresm gresm mentioned this issue May 21, 2024
4 tasks
@gresm
Copy link
Contributor

gresm commented May 26, 2024

Is this a pygame-ce 3 level thing?

I think that I've implemented this without breaking compatibility with any code besides that which uses bad ways of checking type of instance, like: type(obj).__name__ == "Event" or obj.__class__ is Event.

@gresm gresm linked a pull request Sep 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event pygame.event New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants