Skip to content

Commit

Permalink
Merge pull request #493 from LondonClass/LondonClass-patch-1
Browse files Browse the repository at this point in the history
Add on_pressed method
  • Loading branch information
MyreMylar authored Jan 28, 2024
2 parents c259e69 + 20fa263 commit c8d5ff7
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions pygame_gui/elements/ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,29 +315,9 @@ def process_event(self, event: pygame.event.Event) -> bool:
if self.is_enabled:
if (self.allow_double_clicks and self.last_click_button == event.button and
self.double_click_timer <= self.ui_manager.get_double_click_time()):
# old event to remove in 0.8.0
event_data = {'user_type': OldType(UI_BUTTON_DOUBLE_CLICKED),
'ui_element': self,
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data))

# new event
event_data = {'ui_element': self,
'ui_object_id': self.most_specific_combined_id,
'mouse_button': event.button}
pygame.event.post(pygame.event.Event(UI_BUTTON_DOUBLE_CLICKED, event_data))
self.on_double_clicked()
else:
# old event to remove in 0.8.0
event_data = {'user_type': OldType(UI_BUTTON_START_PRESS),
'ui_element': self,
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data))

# new event
event_data = {'ui_element': self,
'ui_object_id': self.most_specific_combined_id,
'mouse_button': event.button}
pygame.event.post(pygame.event.Event(UI_BUTTON_START_PRESS, event_data))
self.on_start_press(event.button)
self.double_click_timer = 0.0
self.last_click_button = event.button
self.held = True
Expand All @@ -355,18 +335,7 @@ def process_event(self, event: pygame.event.Event) -> bool:
self._set_inactive()
consumed_event = True
self.pressed_event = True

# old event
event_data = {'user_type': OldType(UI_BUTTON_PRESSED),
'ui_element': self,
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data))

# new event
event_data = {'ui_element': self,
'ui_object_id': self.most_specific_combined_id,
'mouse_button': event.button}
pygame.event.post(pygame.event.Event(UI_BUTTON_PRESSED, event_data))
self.on_pressed(event.button)

if self.is_enabled and self.held:
self.held = False
Expand All @@ -375,6 +344,46 @@ def process_event(self, event: pygame.event.Event) -> bool:

return consumed_event

def on_start_press(self, button: int):
# old event to remove in 0.8.0
event_data = {'user_type': OldType(UI_BUTTON_START_PRESS),
'ui_element': self,
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data))

# new event
event_data = {'ui_element': self,
'ui_object_id': self.most_specific_combined_id,
'mouse_button': button}
pygame.event.post(pygame.event.Event(UI_BUTTON_START_PRESS, event_data))

def on_pressed(self, button: int):
# old event
event_data = {'user_type': OldType(UI_BUTTON_PRESSED),
'ui_element': self,
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data))

# new event
event_data = {'ui_element': self,
'ui_object_id': self.most_specific_combined_id,
'mouse_button': button}
pygame.event.post(pygame.event.Event(UI_BUTTON_PRESSED, event_data))

def on_double_clicked(self):
# old event to remove in 0.8.0
event_data = {'user_type': OldType(UI_BUTTON_DOUBLE_CLICKED),
'ui_element': self,
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data))

# new event
event_data = {'ui_element': self,
'ui_object_id': self.most_specific_combined_id,
'mouse_button': event.button}
pygame.event.post(pygame.event.Event(UI_BUTTON_DOUBLE_CLICKED, event_data))


def check_pressed(self) -> bool:
"""
A direct way to check if this button has been pressed in the last update cycle.
Expand Down

0 comments on commit c8d5ff7

Please sign in to comment.