# Copyright (C) 2022 RozK # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from engine import ( EVENT_BUTTON_PRESS, EVENT_BUTTON_RELEASE, EVENT_MOTION, BUTTON_LEFT, BUTTON_WHEEL_UP, BUTTON_WHEEL_DOWN) class Mouse: __slots__ = 'buttons', 'wheel', 'wheel_min', 'position', 'drag' def __init__(self, events, wheel = 0, wheel_min = None): self.buttons = 0 self.wheel = wheel self.wheel_min = wheel_min self.position = None self.drag = (0, 0) events.register(EVENT_BUTTON_PRESS, self.button_press_handler) events.register(EVENT_BUTTON_RELEASE, self.button_release_handler) events.register(EVENT_MOTION, self.motion_handler) def button_press_handler(self, data): button = data.button.index self.buttons |= 1 << button if button == BUTTON_WHEEL_UP: self.wheel -= 1 if self.wheel_min is not None and self.wheel < self.wheel_min: self.wheel = self.wheel_min elif button == BUTTON_WHEEL_DOWN: self.wheel += 1 def button_release_handler(self, data): self.buttons &= ~(1 << data.button.index) def motion_handler(self, data): new_x = data.motion.x new_y = data.motion.y if self.position and (self.buttons & (1 << BUTTON_LEFT)): prev_x, prev_y = self.position prev_dx, prev_dy = self.drag self.drag = (prev_dx + (new_x - prev_x), prev_dy + (new_y - prev_y)) self.position = (new_x, new_y)