From: mar77i Date: Thu, 18 Sep 2025 20:31:58 +0000 (+0200) Subject: finish mouse pad X-Git-Url: https://git.mar77i.info/?a=commitdiff_plain;h=816bb3af853fe75fe13e145f155df71eea285bb2;p=zenbook_gui finish mouse pad --- diff --git a/keyboard/keyboard.py b/keyboard/keyboard.py index 50c8ee1..ef4acdd 100644 --- a/keyboard/keyboard.py +++ b/keyboard/keyboard.py @@ -81,13 +81,11 @@ class Root(BaseRoot): self.keyboard.press(key) self.pushed.add(key) - def click_cb(self, button, *args): - if len(args) > 2: - raise TypeError(f"Too many arguments to click_callback: {args}") - elif len(args) == 2: - n, release = args + def click_cb(self, data, release=False): + if len(data) == 1: + button, n = *data, 1 else: - n, release = None, *args + button, n = data if release: if button in self.pushed: self.mouse.release(button) diff --git a/keyboard/layout.py b/keyboard/layout.py index bb9aa15..0035a6f 100644 --- a/keyboard/layout.py +++ b/keyboard/layout.py @@ -151,28 +151,6 @@ class LayoutWidget(Child): [self.keyboard_rects[not self.parent.keyboard_row]], self.keyboard_callback, ) - #self.keypad_rect = pygame.Rect( - # ( - # self.parent.rect.left + ( - # self.rect.left - self.parent.rect.left - # ) / 2 - self.rect.width / 6, - # self.parent.rect.top + 96, - # ), - # (self.rect.width / 3, self.rect.height / 2), - #) - #surf = pygame.Surface(self.keypad_rect.size) - #surf.fill("black") - #self.get_keyboard_keys(surf, self.keypad_rect.size, KEYPAD_KEYS) - #keypad_rect, keypad_dests = self.get_widget_row_rect_dests( - # len(parent.widget_row), KEYPAD_KEYS, self.keypad_rect - #) - #self.keypad = DraggableButton( - # self.parent, - # keypad_rect, - # surf, - # keypad_dests, - # self.keypad_callback, - #) def keyboard_callback(self): self.parent.keyboard_row = int( @@ -207,6 +185,6 @@ class LayoutModal(QuittableModal): self.deactivate, ) self.keyboard_row = 0 - self.widget_row = [CURSOR_KEYS, KEYPAD_KEYS, ClockWidget] + self.widget_row = [CURSOR_KEYS, MousePadWidget, ClockWidget] half_size = (size[0] / 2, size[1] / 2) LayoutWidget(self, pygame.Rect((half_size[0] / 2, half_size[1] / 2), half_size)) diff --git a/keyboard/menu.py b/keyboard/menu.py index c21861d..6c3b3e3 100644 --- a/keyboard/menu.py +++ b/keyboard/menu.py @@ -53,6 +53,7 @@ class MenuModal(QuittableModal): def fix_xkb_layout(self): run(["setxkbmap", "-synch"]) + self.deactivate() def restart(self): executable = Path(sys.executable).name diff --git a/keyboard/mousepad.py b/keyboard/mousepad.py index 050c3dc..1a7f64e 100644 --- a/keyboard/mousepad.py +++ b/keyboard/mousepad.py @@ -1,4 +1,5 @@ -from functools import partial +from dataclasses import dataclass +from math import inf import pygame from pynput.mouse import Button @@ -6,7 +7,7 @@ from pynput.mouse import Button from ui import Child from ui.multitouch import MultitouchHandler -from .touchbutton import TouchButton +from .key import KeyButton BUTTONS = ( @@ -15,6 +16,7 @@ BUTTONS = ( ("↓", (Button.middle,)), ("→", (Button.right,)), ) +SPEEDS = ((2400, 2400), (600, -600)) class MousePadWidget(Child): @@ -23,7 +25,7 @@ class MousePadWidget(Child): self.rect = rect button_size = (rect.width / len(BUTTONS), rect.height / 6) for i, (value, args) in enumerate(BUTTONS): - TouchButton( + KeyButton( parent, pygame.Rect( ( @@ -33,7 +35,8 @@ class MousePadWidget(Child): button_size, ), value, - partial(click_cb, *args) + click_cb, + data=args, ) MousePadArea( parent, @@ -42,6 +45,15 @@ class MousePadWidget(Child): ) +@dataclass +class Finger: + NO_COORD = (inf, inf) + + old: tuple[float, float] + new: tuple[float, float] = NO_COORD + lifted: bool = False + + class MousePadArea(Child): def __init__(self, parent, rect, move_cb): super().__init__(parent) @@ -49,6 +61,39 @@ class MousePadArea(Child): self.move_cb = move_cb self.fingers = {} + def update(self): + num_fingers = len(self.fingers) + if num_fingers == 0: + return + elif num_fingers == 1: + # calculate relative coordinate + finger = next(iter(self.fingers.values())) + if finger.new != Finger.NO_COORD: + self.move_cb( + ((n - o) * s for n, o, s in zip(finger.new, finger.old, SPEEDS[0])) + ) + finger.old = finger.new + elif num_fingers == 2: + avg = (0, 0) + num = 0 + for finger in self.fingers.values(): + if finger.new == Finger.NO_COORD: + continue + avg = ( + avg[0] + finger.new[0] - finger.old[0], + avg[1] + finger.new[1] - finger.old[1], + ) + num += 1 + if num: + self.move_cb((a / num * s for a, s in zip(avg, SPEEDS[1])), True) + for finger in self.fingers.values(): + if finger.new != Finger.NO_COORD: + finger.old = finger.new + + for key, finger in tuple(self.fingers.items()): + if finger.lifted: + self.fingers.pop(key) + def draw(self): pygame.draw.rect(self.surf, "yellow", self.rect, 1) @@ -57,27 +102,20 @@ class MousePadArea(Child): if self.rect.collidepoint( MultitouchHandler.map_coords(pos, self.surf.get_size()) ): - self.fingers[(event.touch_id, event.finger_id)] = [pos] + self.fingers[(event.touch_id, event.finger_id)] = Finger(pos) def handle_fingermotion(self, event): - key = (event.touch_id, event.finger_id) - if key not in self.fingers: + finger = self.fingers.get((event.touch_id, event.finger_id)) + if finger is None or finger.lifted: return pos = (event.x, event.y) - if self.rect.collidepoint( + if not self.rect.collidepoint( MultitouchHandler.map_coords(pos, self.surf.get_size()) ): - self.fingers[key].append(pos) - else: - self.fingers.pop(key) + pos = finger.old + finger.lifted = True + elif event.type == pygame.FINGERUP: + finger.lifted = True + finger.new = pos - def handle_fingerup(self, event): - key = (event.touch_id, event.finger_id) - if key not in self.fingers: - return - pos = (event.x, event.y) - if self.rect.collidepoint( - MultitouchHandler.map_coords(pos, self.surf.get_size()) - ): - pass - self.fingers.pop(key) + handle_fingerup = handle_fingermotion