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

Look into multiple dispatch helpers for cleaning up GUI event code #2313

Open
pushfoo opened this issue Jul 25, 2024 · 2 comments
Open

Look into multiple dispatch helpers for cleaning up GUI event code #2313

pushfoo opened this issue Jul 25, 2024 · 2 comments
Milestone

Comments

@pushfoo
Copy link
Member

pushfoo commented Jul 25, 2024

Enhancement request:

TL;DR: Consider one of the other dispatch tools to clean up GUI event code

What should be added/changed?

Consider one of the following to clean up our event code:

  1. functools.singledispatchmethod
  2. The 3rd party multipledispatch
  3. Beartype's plum

Why wait til 3.0+?

There might be unexpected problems which would delay 3.0.

The status of the following are unclear:

  1. Union types and arguments
  2. It looks like there are issues with inheritance as of a month ago

What would it help with?

Reduce verbosity in cases like this:

def on_event(self, event: UIEvent) -> Optional[bool]:
# If not active, check to activate, return
if not self._active and isinstance(event, UIMousePressEvent):
if self.rect.point_in_rect(event.pos):
self.activate()
return EVENT_UNHANDLED
# If active check to deactivate
if self._active and isinstance(event, UIMousePressEvent):
if self.rect.point_in_rect(event.pos):
x = int(event.x - self.left - self.LAYOUT_OFFSET)
y = int(event.y - self.bottom)
self.caret.on_mouse_press(x, y, event.button, event.modifiers)
else:
self.deactivate()
return EVENT_UNHANDLED
# If active pass all non press events to caret
if self._active:
# Act on events if active
if isinstance(event, UITextEvent):
self.caret.on_text(event.text)
self.trigger_full_render()
elif isinstance(event, UITextMotionEvent):
self.caret.on_text_motion(event.motion)
self.trigger_full_render()
elif isinstance(event, UITextMotionSelectEvent):
self.caret.on_text_motion_select(event.selection)
self.trigger_full_render()
if isinstance(event, UIMouseEvent) and self.rect.point_in_rect(event.pos):
x = int(event.x - self.left - self.LAYOUT_OFFSET)
y = int(event.y - self.bottom)
if isinstance(event, UIMouseDragEvent):
self.caret.on_mouse_drag(
x, y, event.dx, event.dy, event.buttons, event.modifiers
)
self.trigger_full_render()
elif isinstance(event, UIMouseScrollEvent):
self.caret.on_mouse_scroll(x, y, event.scroll_x, event.scroll_y)
self.trigger_full_render()
if super().on_event(event):
return EVENT_HANDLED
return EVENT_UNHANDLED

@pushfoo pushfoo added this to the Future milestone Jul 25, 2024
@cspotcode
Copy link
Collaborator

cspotcode commented Jul 25, 2024 via email

@eruvanos
Copy link
Member

Hi,

from my point of view, we do not have to enforce a decision
for such topic without experimenting and testing it in praxis.

You can just create a UIWidget, which passes the events to the singledispatch function.
This could even be an opt-in Mix-in.

Side note:

I personally try to stick to the current arcade "statement":

Arcade is an easy-to-learn Python library for creating 2D video games. It is ideal for beginning programmers, or programmers who want to create 2D games without learning a complex framework.

My conclusion is, it should be easy to understand for beginner, or have to be optional.
( I am aware, that the GUI comes with its own complexity, but it is the easiest solution I found so far 🙈)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants