def on_ui_event(self, event: UIEvent): if event.type == UIClickable.CLICKED and event.get('ui_element').id == 'submit_button': # Trigger action if 'submit_button' was clicked self.submit() elif event.type == UIInputBox.ENTER and event.get('ui_element').id == 'username': # Trigger action if ENTER pressed in 'username'-UIInputBox self.submit()
def on_ui_event(self, event: UIEvent): super().on_ui_event(event) if self.focused: if event.type == TEXT_INPUT and event.get('text') == '\r': self.dispatch_event('on_enter') if self.mng: self.mng.dispatch_ui_event(UIEvent(UIInputBox.ENTER, ui_element=self)) return self.text_adapter.on_ui_event(event) if self.text_adapter.state_changed: self.text_adapter.reset_state_changed() self.render()
def on_ui_event(self, event: gui.UIEvent): # redefine on_ui_event to trigger a handler super().on_ui_event(event) if self.focused: if event.type == gui.TEXT_INPUT and event.get('text') == '\r': self._on_text_update() return
def test_click_on_element_makes_it_active(mock_mng, mock_button): mock_mng.add_ui_element(mock_button) mock_mng.dispatch_ui_event(UIEvent(MOUSE_PRESS, x=50, y=50, button=1, modifier=0)) assert mock_mng.focused_element is mock_button assert mock_button.on_focus_called
def on_mouse_release(self, x: float, y: float, button: int, modifiers: int): self.disptach_ui_event( UIEvent(MOUSE_RELEASE, x=x, y=y, button=button, modifiers=modifiers))
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float): self.disptach_ui_event(UIEvent( MOUSE_MOTION, x=x, y=y, dx=dx, dy=dy, ))
def on_text_motion_select(self, selection): """ Dispatches :py:meth:`arcade.View.on_text_motion_select()` as :py:class:`arcade.gui.UIElement` with type :py:attr:`arcade.gui.TEXT_MOTION_SELECT` """ self.dispatch_ui_event(UIEvent(TEXT_MOTION_SELECTION, selection=selection, ))
def on_text(self, text): """ Dispatches :py:meth:`arcade.View.on_text()` as :py:class:`arcade.gui.UIElement` with type :py:attr:`arcade.gui.TEXT_INPUT` """ self.dispatch_ui_event(UIEvent(TEXT_INPUT, text=text, ))
def test_click_beside_element_unfocuses(mock_mng, mock_button): mock_mng.add_ui_element(mock_button) mock_mng.focused_element = mock_button mock_mng.dispatch_ui_event(UIEvent(MOUSE_PRESS, x=100, y=100, button=1, modifier=0)) assert mock_mng.focused_element is None assert mock_button.on_unfocus_called
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int): self.disptach_ui_event( UIEvent( MOUSE_SCROLL, x=x, y=y, scroll_x=scroll_x, scroll_y=scroll_y, ))
def test_changes_cursor_on_text_motion(motion, expected_index, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_MOTION, motion=motion)) assert inputbox.cursor_index == expected_index
def test_changes_text_on_delete(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_MOTION, motion=MOTION_DELETE)) assert inputbox.text == 'Best ame Lib!' assert inputbox.cursor_index == 5
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float): """ Dispatches :py:meth:`arcade.View.on_mouse_motion()` as :py:class:`arcade.gui.UIElement` with type :py:attr:`arcade.gui.MOUSE_MOTION` """ self.dispatch_ui_event(UIEvent(MOUSE_MOTION, x=x, y=y, dx=dx, dy=dy, ))
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int): """ Dispatches :py:meth:`arcade.View.on_mouse_scroll()` as :py:class:`arcade.gui.UIElement` with type :py:attr:`arcade.gui.MOUSE_SCROLL` """ self.dispatch_ui_event(UIEvent(MOUSE_SCROLL, x=x, y=y, scroll_x=scroll_x, scroll_y=scroll_y, ))
def test_emits_event_on_enter(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='\r')) assert mock_mng.last_event.type == UIInputBox.ENTER assert mock_mng.last_event.get('ui_element') == inputbox
def test_ignores_newline(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='\r')) assert inputbox.text == 'Best Game Lib!' assert inputbox.cursor_index == 5
def test_changes_text_on_text_input(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='a')) assert inputbox.text == 'Best aGame Lib!' assert inputbox.cursor_index == 6
def test_cursor_can_not_be_negative(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 0 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_MOTION, motion=MOTION_LEFT)) assert inputbox.text == 'Best Game Lib!' assert inputbox.cursor_index == 0
def test_change_focus_to_different_element(mock_mng, mock_button: MockButton, mock_button2: MockButton): mock_button.center_x += 100 mock_mng.add_ui_element(mock_button) mock_mng.add_ui_element(mock_button2) mock_mng.focused_element = mock_button # WHEN mock_mng.dispatch_ui_event(UIEvent(MOUSE_PRESS, x=50, y=50, button=1, modifier=0)) # THEN assert mock_mng.focused_element is mock_button2 assert mock_button.on_unfocus_called assert mock_button2.on_focus_called
def test_invokes_decorator_on_return(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) invoked = False @inputbox.event('on_enter') def callback(): nonlocal invoked invoked = True inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='\r')) assert invoked is True