def test_process_event_mouse_button_up_inside(self, _init_pygame: None,
                                                  default_ui_manager: UIManager,
                                                  _display_surface_return_none: None):
        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.",
                          manager=default_ui_manager)

        # create a tool tip
        button.hover_time = 9999.0
        button.while_hovering(0.01, pygame.math.Vector2(150.0, 115.0))

        # process a mouse button down event
        processed_down_event = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
                                                                       {'button': 1,
                                                                        'pos': (50, 25)}))

        # process a mouse button up event
        processed_up_event = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONUP,
                                                                     {'button': 1,
                                                                      'pos': (50, 25)}))

        assert (
                    processed_down_event is True and processed_up_event is True and button.held is False and
                    button.pressed_event is True and default_ui_manager.focused_element is None)
    def test_process_event_double_click(self, _init_pygame: None, default_ui_manager: UIManager,
                                        _display_surface_return_none: None):
        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.",
                          manager=default_ui_manager,
                          allow_double_clicks=True)

        # process a mouse button down event
        consumed_event_1 = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
                                                                   {'button': pygame.BUTTON_LEFT,
                                                                    'pos': button.rect.center}))

        consumed_event_2 = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
                                                                   {'button': pygame.BUTTON_LEFT,
                                                                    'pos': button.rect.center}))

        confirm_double_click_event_fired = False
        for event in pygame.event.get():
            if (
                    event.type == pygame.USEREVENT and event.user_type == pygame_gui.UI_BUTTON_DOUBLE_CLICKED and
                    event.ui_element == button):
                confirm_double_click_event_fired = True

        assert consumed_event_1 and consumed_event_2
        assert confirm_double_click_event_fired
Beispiel #3
0
    def test_enable(self, _init_pygame: None, default_ui_manager: UIManager,
                    _display_surface_return_none: None):
        container = UIScrollingContainer(pygame.Rect(100, 100, 200, 200),
                                         manager=default_ui_manager)
        button_1 = 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.",
                            manager=default_ui_manager,
                            container=container)

        button_2 = UIButton(relative_rect=pygame.Rect(10, 50, 150, 30),
                            text="Test Button 2",
                            manager=default_ui_manager,
                            container=container)

        container.disable()
        container.enable()

        assert container.is_enabled is True
        assert button_1.is_enabled is True
        assert button_2.is_enabled is True

        # process a mouse button down event
        button_1.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': button_1.rect.center}))

        # process a mouse button up event
        button_1.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONUP, {'button': 1, 'pos': button_1.rect.center}))

        button_1.update(0.01)

        assert button_1.check_pressed() is True
Beispiel #4
0
    def test_check_pressed(self, _init_pygame: None, default_ui_manager: UIManager,
                           _display_surface_return_none: None):
        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.",
                          manager=default_ui_manager)

        # process a mouse button down event
        button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': (50, 25)}))

        # process a mouse button up event
        button.process_event(pygame.event.Event(pygame.MOUSEBUTTONUP, {'button': 1, 'pos': (50, 25)}))

        button.update(0.01)

        assert button.check_pressed() is True
    def test_old_events_warn(self, _init_pygame, _display_surface_return_none):
        manager = UIManager((800, 600))
        button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30),
                          text="Test Button",
                          manager=manager,
                          allow_double_clicks=True)

        button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
                                                                   {'button': pygame.BUTTON_LEFT,
                                                                    'pos': button.rect.center}))

        button.process_event(pygame.event.Event(pygame.MOUSEBUTTONUP,
                                                                   {'button': pygame.BUTTON_LEFT,
                                                                    'pos': button.rect.center}))

        with pytest.warns(DeprecationWarning, match="Pygame GUI event types can now"):
            for event in pygame.event.get():
                if (event.type == pygame.USEREVENT and
                        event.user_type == UI_BUTTON_PRESSED and event.ui_element == button):
                    assert isinstance(event.user_type, OldType)
Beispiel #6
0
    def test_disable(self, _init_pygame: None, default_ui_manager: UIManager,
                     _display_surface_return_none: None):
        panel = UIPanel(relative_rect=pygame.Rect(0, 0, 150, 400),
                        starting_layer_height=5,
                        manager=default_ui_manager)
        button_1 = 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.",
            manager=default_ui_manager,
            container=panel)

        button_2 = UIButton(relative_rect=pygame.Rect(10, 50, 150, 30),
                            text="Test Button 2",
                            manager=default_ui_manager,
                            container=panel)

        panel.disable()

        assert panel.is_enabled is False
        assert button_1.is_enabled is False
        assert button_2.is_enabled is False

        # process a mouse button down event
        button_1.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONDOWN, {
                'button': 1,
                'pos': button_1.rect.center
            }))

        # process a mouse button up event
        button_1.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONUP, {
                'button': 1,
                'pos': button_1.rect.center
            }))

        button_1.update(0.01)

        assert button_1.check_pressed() is False
Beispiel #7
0
    def test_process_event(self, _init_pygame, default_ui_manager,
                           _display_surface_return_none: None):
        window = UIWindow(pygame.Rect(0, 0, 200, 200),
                          window_display_title="Test Window",
                          manager=default_ui_manager)

        button_rect = pygame.Rect(0, 0, 150, 30)
        button_rect.topright = (-10, 10)
        button = UIButton(
            relative_rect=button_rect,
            text="Test Button",
            tool_tip_text=
            "This is a test of the button's tool tip functionality.",
            manager=default_ui_manager,
            container=window,
            object_id='#specific_id_test',
            anchors={
                'left': 'right',
                'right': 'right',
                'top': 'top',
                'bottom': 'top'
            })

        button.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONDOWN, {
                'button': pygame.BUTTON_LEFT,
                'pos': button.rect.center
            }))
        button.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONUP, {
                'button': pygame.BUTTON_LEFT,
                'pos': button.rect.center
            }))

        confirm_event_fired = False
        event_object_id = None
        for event in pygame.event.get():
            if (event.type == pygame.USEREVENT
                    and event.user_type == pygame_gui.UI_BUTTON_PRESSED
                    and event.ui_element == button):
                confirm_event_fired = True
                event_object_id = event.ui_object_id
        assert confirm_event_fired
        assert event_object_id == 'window.#specific_id_test'

        consumed_event = window.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONDOWN, {
                'button': pygame.BUTTON_RIGHT,
                'pos': window.rect.center
            }))
        assert consumed_event is True

        window.edge_hovering[0] = True
        consumed_event = window.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONDOWN, {
                'button': pygame.BUTTON_LEFT,
                'pos': window.rect.topleft
            }))
        assert consumed_event and window.resizing_mode_active

        consumed_event = window.process_event(
            pygame.event.Event(pygame.MOUSEBUTTONUP, {
                'button': pygame.BUTTON_LEFT,
                'pos': (500, 500)
            }))
        assert not (consumed_event or window.resizing_mode_active)

        consumed_event = window.process_event(
            pygame.event.Event(
                pygame.USEREVENT, {
                    'user_type': pygame_gui.UI_BUTTON_PRESSED,
                    'ui_element': window.close_window_button
                }))
        assert not (consumed_event or window.alive())