From 01641feacfc2f0a30c2c3ba45209abbaf62e9d37 Mon Sep 17 00:00:00 2001 From: LondonClass <43668206+LondonClass@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:27:25 +0800 Subject: [PATCH] rename to on_element_event rename to on_element_event for easy extension to other elements --- pygame_gui/elements/ui_button.py | 18 +++++++++--------- tests/test_elements/test_ui_button.py | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pygame_gui/elements/ui_button.py b/pygame_gui/elements/ui_button.py index a82a3c47..59068435 100644 --- a/pygame_gui/elements/ui_button.py +++ b/pygame_gui/elements/ui_button.py @@ -42,7 +42,7 @@ class UIButton(UIElement): unique event. :param visible: Whether the element is visible by default. Warning - container visibility may override this. - :param command: Functions to be called when a button event occurs. + :param command: Functions to be called when an element event occurs. :param text_kwargs: a dictionary of variable arguments to pass to the translated string useful when you have multiple translations that need variables inserted in the middle. @@ -329,9 +329,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()): - self.on_button_event(UI_BUTTON_DOUBLE_CLICKED, {'mouse_button':event.button}) + self.on_element_event(UI_BUTTON_DOUBLE_CLICKED, {'mouse_button':event.button}) else: - self.on_button_event(UI_BUTTON_START_PRESS, {'mouse_button':event.button}) + self.on_element_event(UI_BUTTON_START_PRESS, {'mouse_button':event.button}) self.double_click_timer = 0.0 self.last_click_button = event.button self.held = True @@ -349,7 +349,7 @@ def process_event(self, event: pygame.event.Event) -> bool: self._set_inactive() consumed_event = True self.pressed_event = True - self.on_button_event(UI_BUTTON_PRESSED, {'mouse_button':event.button}) + self.on_element_event(UI_BUTTON_PRESSED, {'mouse_button':event.button}) if self.is_enabled and self.held: self.held = False @@ -360,7 +360,7 @@ def process_event(self, event: pygame.event.Event) -> bool: def bind(self, event:int, function:Callable = None): """ - Bind a function to an event. + Bind a function to an element event. :param event: The event to bind. @@ -378,15 +378,15 @@ def bind(self, event:int, function:Callable = None): elif num_params == 0: self._handler[event] = lambda _:function() else: - raise ValueError("Button command function signatures can have 0 or 1 parameter. " + raise ValueError("Command function signatures can have 0 or 1 parameter. " "If one parameter is set it will contain data for the id of the mouse button used " "to trigger this click event.") else: - raise TypeError("Button command function must be callable") + raise TypeError("Command function must be callable") - def on_button_event(self, event:int, data:Dict[str, Any]=None): + def on_element_event(self, event:int, data:Dict[str, Any]=None): """ - Called when a button event occurs. Handles these events either by posting the event back + Called when an element event occurs. Handles these events either by posting the event back to the event queue, or by running a function supplied by the user. :param event: The event occurs. diff --git a/tests/test_elements/test_ui_button.py b/tests/test_elements/test_ui_button.py index d3e5cb0f..68fec1c3 100644 --- a/tests/test_elements/test_ui_button.py +++ b/tests/test_elements/test_ui_button.py @@ -481,7 +481,7 @@ def test_function(data): def test_command_bad_value(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): - with pytest.raises(TypeError, match="Button command function must be callable"): + with pytest.raises(TypeError, match="Command function must be callable"): button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30), text="Test Button", tool_tip_text="This is a test of the button's tool tip functionality.", @@ -510,7 +510,7 @@ def test_function(data): button.bind(pygame_gui.UI_BUTTON_PRESSED, None) assert pygame_gui.UI_BUTTON_PRESSED not in button._handler - with pytest.raises(TypeError, match="Button command function must be callable"): + with pytest.raises(TypeError, match="Command function must be callable"): button.bind(pygame_gui.UI_BUTTON_PRESSED, "non-callable") def function_with_3_params(x, y, z): @@ -519,7 +519,7 @@ def function_with_3_params(x, y, z): with pytest.raises(ValueError): button.bind(pygame_gui.UI_BUTTON_PRESSED, function_with_3_params) - def test_on_button_event(self, _init_pygame: None, default_ui_manager: UIManager, + def test_on_element_event(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): button_start_press = False @@ -546,14 +546,14 @@ def test_function2(data): assert pygame_gui.UI_BUTTON_DOUBLE_CLICKED not in button._handler assert not button_start_press - button.on_button_event(pygame_gui.UI_BUTTON_START_PRESS, {'mouse_button':1}) + button.on_element_event(pygame_gui.UI_BUTTON_START_PRESS, {'mouse_button':1}) assert button_start_press assert pressed_button == 0 - button.on_button_event(pygame_gui.UI_BUTTON_PRESSED, {'mouse_button':3}) + button.on_element_event(pygame_gui.UI_BUTTON_PRESSED, {'mouse_button':3}) assert pressed_button == 3 - button.on_button_event(pygame_gui.UI_BUTTON_DOUBLE_CLICKED, {'mouse_button':1}) + button.on_element_event(pygame_gui.UI_BUTTON_DOUBLE_CLICKED, {'mouse_button':1}) confirm_double_click_event_fired = False for event in pygame.event.get(): @@ -562,7 +562,7 @@ def test_function2(data): confirm_double_click_event_fired = True assert confirm_double_click_event_fired - def test_on_button_event_0_params(self, _init_pygame: None, default_ui_manager: UIManager, + def test_on_element_event_no_params(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): button_start_press = False @@ -579,7 +579,7 @@ def test_function(): command=command_dict) assert not button_start_press - button.on_button_event(pygame_gui.UI_BUTTON_START_PRESS, {'mouse_button':1}) + button.on_element_event(pygame_gui.UI_BUTTON_START_PRESS, {'mouse_button':1}) assert button_start_press