Skip to content

Commit

Permalink
rename to on_element_event
Browse files Browse the repository at this point in the history
rename to on_element_event for easy extension to other elements
  • Loading branch information
LondonClass committed Feb 13, 2024
1 parent ee9a7e0 commit 01641fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions pygame_gui/elements/ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions tests/test_elements/test_ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand All @@ -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():
Expand All @@ -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

Expand All @@ -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


Expand Down

0 comments on commit 01641fe

Please sign in to comment.