Skip to content

Commit

Permalink
Add timestamp to events, and buttons to wheel events (#397)
Browse files Browse the repository at this point in the history
* add buttons to glfw wheel events (and make buttons and modifiers order consistent).

* Add buttons to wheel event for qt

* Add time_stamp to events (if they don't have it already)

* explain todo a bit better

---------

Co-authored-by: Korijn van Golen <[email protected]>
  • Loading branch information
almarklein and Korijn authored Oct 24, 2023
1 parent 47cdf89 commit 4026cd5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
14 changes: 9 additions & 5 deletions wgpu/gui/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def _get_event_wait_time(self):
target_time = self._last_event_time + 1.0 / rate
return max(0, target_time - now)

def _handle_event_rate_limited(self, ev, call_later_func, match_keys, accum_keys):
def _handle_event_rate_limited(
self, event, call_later_func, match_keys, accum_keys
):
"""Alternative `to handle_event()` for events that must be rate-limted.
If any of the `match_keys` keys of the new event differ from the currently
pending event, the old event is dispatched now. The `accum_keys` keys of
Expand All @@ -252,23 +254,25 @@ def _handle_event_rate_limited(self, ev, call_later_func, match_keys, accum_keys
Subclasses that use this method must use ``_handle_event_and_flush()``
where they would otherwise call ``handle_event()``, to preserve event order.
"""
event_type = ev["event_type"]
event_type = event["event_type"]
event.setdefault("time_stamp", time.perf_counter())
# We may need to emit the old event. Otherwise, we need to update the new one.
old = self._pending_events.get(event_type, None)
if old:
if any(ev[key] != old[key] for key in match_keys):
if any(event[key] != old[key] for key in match_keys):
self.handle_event(old)
else:
for key in accum_keys:
ev[key] = old[key] + ev[key]
event[key] = old[key] + event[key]
# Make sure that we have scheduled a moment to handle events
if not self._pending_events:
call_later_func(self._get_event_wait_time(), self._handle_pending_events)
# Store the event object
self._pending_events[event_type] = ev
self._pending_events[event_type] = event

def _handle_event_and_flush(self, event):
"""Call handle_event after flushing any pending (rate-limited) events."""
event.setdefault("time_stamp", time.perf_counter())
self._handle_pending_events()
self.handle_event(event)

Expand Down
21 changes: 15 additions & 6 deletions wgpu/gui/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def __init__(self, *, size=None, title=None, **kwargs):
glfw.set_window_iconify_callback(self._window, weakbind(self._on_iconify))

# User input
self._key_modifiers = set()
self._pointer_buttons = set()
self._key_modifiers = []
self._pointer_buttons = []
self._pointer_pos = 0, 0
self._double_click_state = {"clicks": 0}
glfw.set_mouse_button_callback(self._window, weakbind(self._on_mouse_button))
Expand Down Expand Up @@ -323,10 +323,14 @@ def _on_mouse_button(self, window, but, action, mods):

if action == glfw.PRESS:
event_type = "pointer_down"
self._pointer_buttons.add(button)
buttons = set(self._pointer_buttons)
buttons.add(button)
self._pointer_buttons = list(sorted(buttons))
elif action == glfw.RELEASE:
event_type = "pointer_up"
self._pointer_buttons.discard(button)
buttons = set(self._pointer_buttons)
buttons.discard(button)
self._pointer_buttons = list(sorted(buttons))
else:
return

Expand Down Expand Up @@ -426,6 +430,7 @@ def _on_scroll(self, window, dx, dy):
"dy": -100.0 * dy,
"x": self._pointer_pos[0],
"y": self._pointer_pos[1],
"buttons": list(self._pointer_buttons),
"modifiers": list(self._key_modifiers),
}
match_keys = {"modifiers"}
Expand All @@ -438,11 +443,15 @@ def _on_key(self, window, key, scancode, action, mods):
if action == glfw.PRESS:
event_type = "key_down"
if modifier:
self._key_modifiers.add(modifier)
modifiers = set(self._key_modifiers)
modifiers.add(modifier)
self._key_modifiers = list(sorted(modifiers))
elif action == glfw.RELEASE:
event_type = "key_up"
if modifier:
self._key_modifiers.discard(modifier)
modifiers = set(self._key_modifiers)
modifiers.discard(modifier)
self._key_modifiers = list(sorted(modifiers))
else: # glfw.REPEAT
return

Expand Down
10 changes: 8 additions & 2 deletions wgpu/gui/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ def _mouse_event(self, event_type, event, touches=True):
if touches:
ev.update(
{
"ntouches": 0, # TODO
"touches": {}, # TODO
"ntouches": 0,
"touches": {}, # TODO: Qt touch events
}
)

Expand Down Expand Up @@ -285,13 +285,19 @@ def wheelEvent(self, event): # noqa: N802
for mod in MODIFIERS_MAP.keys()
if mod & event.modifiers()
]
buttons = [
BUTTON_MAP[button]
for button in BUTTON_MAP.keys()
if button & event.buttons()
]

ev = {
"event_type": "wheel",
"dx": -event.angleDelta().x(),
"dy": -event.angleDelta().y(),
"x": event.position().x(),
"y": event.position().y(),
"buttons": buttons,
"modifiers": modifiers,
}
match_keys = {"modifiers"}
Expand Down

0 comments on commit 4026cd5

Please sign in to comment.