def test_get_current_value(self, _init_pygame, default_ui_manager, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) assert scroll_bar.get_current_value() == 50
def test_rebuild(self, _init_pygame, default_ui_manager: IUIManagerInterface, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) scroll_bar.rebuild() assert scroll_bar.image is not None
def test_set_current_value_out_of_range(self, _init_pygame, default_ui_manager, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) with pytest.warns(UserWarning, match='value not in range'): scroll_bar.set_current_value(200)
def test_set_current_value_in_range(self, _init_pygame, default_ui_manager): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect( 100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) scroll_bar.set_current_value(75) assert scroll_bar.get_current_value() == 75
def test_check_has_moved_recently(self, _init_pygame, default_ui_manager, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) # move the scroll bar a bit scroll_bar.left_button.held = True scroll_bar.update(0.2) assert scroll_bar.has_moved_recently is True
def test_set_position(self, _init_pygame, default_ui_manager, _display_surface_return_none): slider = UIHorizontalSlider(relative_rect=pygame.Rect(300, 400, 150, 40), start_value=50, value_range=(0, 200), manager=default_ui_manager) slider.set_position((200, 200)) # try to click on the slider default_ui_manager.process_events(pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': (205, 205)})) # if we successfully clicked on the moved slider then this button should be True assert slider.left_button.held is True
def test_kill(self, _init_pygame, default_ui_manager): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect( 100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) # should kill everything scroll_bar.kill() assert scroll_bar.alive() is False and scroll_bar.sliding_button.alive( ) is False
def test_creation(self, _init_pygame, default_ui_manager): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect( 100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) assert scroll_bar.image is not None
def test_set_dimensions(self, _init_pygame, default_ui_manager, _display_surface_return_none): slider = UIHorizontalSlider(relative_rect=pygame.Rect(0, 0, 150, 40), start_value=50, value_range=(0, 200), manager=default_ui_manager) slider.set_dimensions((200, 60)) # try to click on the slider default_ui_manager.process_events( pygame.event.Event(pygame.MOUSEBUTTONDOWN, { 'button': 1, 'pos': (195, 50) })) # if we successfully clicked on the moved slider then this button should be True assert slider.right_button.held is True assert slider.right_button.rect.top == (slider.shadow_width + slider.border_width) assert slider.right_button.rect.bottom == 60 - (slider.shadow_width + slider.border_width) assert slider.right_button.rect.right == 200 - (slider.shadow_width + slider.border_width) slider.set_dimensions((100, 30)) # try to click on the slider default_ui_manager.process_events( pygame.event.Event(pygame.MOUSEBUTTONDOWN, { 'button': 1, 'pos': (95, 15) })) # if we successfully clicked on the moved slider then this button should be True assert slider.right_button.held is True assert slider.right_button.rect.top == (slider.shadow_width + slider.border_width) assert slider.right_button.rect.bottom == 30 - (slider.shadow_width + slider.border_width) assert slider.right_button.rect.right == 100 - (slider.shadow_width + slider.border_width) slider.set_dimensions((150, 45)) # try to click on the slider default_ui_manager.process_events( pygame.event.Event(pygame.MOUSEBUTTONDOWN, { 'button': 1, 'pos': (145, 22) })) # if we successfully clicked on the moved slider then this button should be True assert slider.right_button.held is True assert slider.right_button.rect.top == (slider.shadow_width + slider.border_width) assert slider.right_button.rect.bottom == 45 - (slider.shadow_width + slider.border_width) assert slider.right_button.rect.right == 150 - (slider.shadow_width + slider.border_width)
def test_set_relative_position(self, _init_pygame, default_ui_manager, _display_surface_return_none): test_container = UIContainer(relative_rect=pygame.Rect(100, 100, 300, 60), manager=default_ui_manager) slider = UIHorizontalSlider(relative_rect=pygame.Rect(300, 400, 150, 40), start_value=50, container=test_container, value_range=(0, 200), manager=default_ui_manager) slider.set_relative_position((150.0, 30.0)) # try to click on the slider default_ui_manager.process_events(pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': (260, 150)})) assert slider.rect.topleft == (250, 130) and slider.left_button.held is True
def test_hide(self, _init_pygame, default_ui_manager, _display_surface_return_none): slider = UIHorizontalSlider(relative_rect=pygame.Rect(0, 0, 150, 40), start_value=50, value_range=(0, 200), manager=default_ui_manager) assert slider.visible == 1 assert slider.sliding_button.visible == 1 assert slider.button_container.visible == 1 assert slider.left_button.visible == 1 assert slider.right_button.visible == 1 slider.hide() assert slider.visible == 0 assert slider.sliding_button.visible == 0 assert slider.button_container.visible == 0 assert slider.left_button.visible == 0 assert slider.right_button.visible == 0
def test_kill(self, _init_pygame, default_ui_manager: IUIManagerInterface, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) assert len(default_ui_manager.get_root_container().elements) == 2 assert len(default_ui_manager.get_sprite_group().sprites()) == 6 assert default_ui_manager.get_sprite_group().sprites() == [default_ui_manager.get_root_container(), scroll_bar, scroll_bar.button_container, scroll_bar.left_button, scroll_bar.right_button, scroll_bar.sliding_button] scroll_bar.kill() assert len(default_ui_manager.get_root_container().elements) == 0 assert len(default_ui_manager.get_sprite_group().sprites()) == 1 assert default_ui_manager.get_sprite_group().sprites() == [default_ui_manager.get_root_container()]
def test_rebuild_from_theme_data_non_default(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_horizontal_slider_non_default.json")) scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=manager) assert scroll_bar.image is not None
def test_rebuild_from_theme_data_bad_values(self, _init_pygame): manager = UIManager( (800, 600), os.path.join("tests", "data", "themes", "ui_horizontal_slider_bad_values.json")) scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect( 100, 100, 200, 30), start_value=51, value_range=(0, 100), manager=manager) assert scroll_bar.image is not None
def recreate_ui(self): self.ui_manager.set_window_resolution(self.options.resolution) self.ui_manager.clear_and_reset() self.background_surface = pygame.Surface(self.options.resolution) self.background_surface.fill(self.ui_manager.get_theme().get_colour(None, None, 'dark_bg')) self.test_button = UIButton(pygame.Rect((int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.90)), (100, 40)), '', self.ui_manager, tool_tip_text="<font face=fira_code color=normal_text size=2>" "<b><u>Test Tool Tip</u></b>" "<br><br>" "A little <i>test</i> of the " "<font color=#FFFFFF><b>tool tip</b></font>" " functionality." "<br><br>" "Unleash the Kraken!" "</font>", object_id='#hover_me_button') self.test_button_2 = UIButton(pygame.Rect((int(self.options.resolution[0] / 3), int(self.options.resolution[1] * 0.90)), (100, 40)), 'EVERYTHING', self.ui_manager, object_id='#everything_button') self.test_slider = UIHorizontalSlider(pygame.Rect((int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.70)), (240, 25)), 100.0, (0.0, 100.0), self.ui_manager, object_id='#cool_slider') self.test_text_entry = UITextEntryLine(pygame.Rect((int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.50)), (200, -1)), self.ui_manager, object_id='#main_text_entry') current_resolution_string = str(self.options.resolution[0]) + 'x' + str(self.options.resolution[1]) self.test_drop_down_menu = UIDropDownMenu(['640x480', '800x600', '1024x768'], current_resolution_string, pygame.Rect((int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.3)), (200, 25)), self.ui_manager)
def test_check_update_buttons(self, _init_pygame, default_ui_manager, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(100, 100, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) # scroll down a bit then up again to exercise update scroll_bar.get_current_value() # Clear has moved this turn scroll_bar.left_button.held = True scroll_bar.update(0.3) scroll_bar.left_button.held = False scroll_bar.right_button.held = True scroll_bar.update(0.3) assert scroll_bar.has_moved_recently is True
def add_slider(self, rect: pygame.Rect, label: str, start_value: float, value_range: Tuple[float, float]) -> None: """ Add a slider to the dictionary of sliders. Preconditions: - label not in self._sliders - value_range[1] > value_range[0] - value_range[0] <= start_value <= value_range[1] """ self._sliders[label] = UIHorizontalSlider(relative_rect=rect, start_value=start_value, value_range=value_range, manager=self._gui_manager)
def test_enable(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none: None): slider = UIHorizontalSlider(relative_rect=pygame.Rect(0, 0, 150, 40), start_value=50, value_range=(0, 200), manager=default_ui_manager) slider.disable() slider.enable() # process a mouse button down event slider.left_button.process_event( pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': slider.left_button.rect.center})) slider.update(0.1) # process a mouse button up event slider.left_button.process_event( pygame.event.Event(pygame.MOUSEBUTTONUP, {'button': 1, 'pos': slider.left_button.rect.center})) assert slider.get_current_value() != 50 and slider.is_enabled is True
def test_check_update_sliding_bar(self, _init_pygame, default_ui_manager, _display_surface_return_none): scroll_bar = UIHorizontalSlider(relative_rect=pygame.Rect(0, 0, 200, 30), start_value=50, value_range=(0, 100), manager=default_ui_manager) # scroll down a bit then up again to exercise update default_ui_manager.mouse_position = (100, 15) scroll_bar.sliding_button.held = True scroll_bar.update(0.3) assert scroll_bar.grabbed_slider is True scroll_bar.sliding_button.held = False scroll_bar.update(0.3) assert scroll_bar.grabbed_slider is False
def test_floats_in_floats_out(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none: None): slider = UIHorizontalSlider(relative_rect=pygame.Rect(0, 0, 150, 40), start_value=25.0, value_range=(0.0, 200.0), manager=default_ui_manager) assert isinstance(slider.get_current_value(), float) # process a mouse button down event slider.left_button.process_event( pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': slider.left_button.rect.center})) slider.update(0.1) # process a mouse button up event slider.left_button.process_event( pygame.event.Event(pygame.MOUSEBUTTONUP, {'button': 1, 'pos': slider.left_button.rect.center})) assert isinstance(slider.get_current_value(), float)
def test_show_hide_rendering(self, _init_pygame, default_ui_manager, _display_surface_return_none): resolution = (400, 400) empty_surface = pygame.Surface(resolution) empty_surface.fill(pygame.Color(0, 0, 0)) surface = empty_surface.copy() manager = UIManager(resolution) slider = UIHorizontalSlider(relative_rect=pygame.Rect(25, 25, 375, 150), start_value=50, value_range=(0, 200), manager=manager, visible=0) manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) slider.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) slider.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def __init__(self, rect, ui_manager): element_ids = ['everything_window'] super().__init__(rect, ui_manager, element_ids=element_ids) # create shadow shadow_padding = (2, 2) self.image = self.ui_manager.get_shadow(self.rect.size) self.image.fill(self.ui_manager.get_theme().get_colour(self.object_ids, self.element_ids, 'dark_bg'), pygame.Rect(shadow_padding, (self.rect.width - shadow_padding[0] * 2, self.rect.height - shadow_padding[1] * 2) )) self.get_container().relative_rect.width = self.rect.width - shadow_padding[0] * 2 self.get_container().relative_rect.height = self.rect.height - shadow_padding[1] * 2 self.get_container().relative_rect.x = self.get_container().relative_rect.x + shadow_padding[0] self.get_container().relative_rect.y = self.get_container().relative_rect.y + shadow_padding[1] self.get_container().update_containing_rect_position() self.close_window_button = UIButton(relative_rect=pygame.Rect((self.get_container().rect.width-20, 0), (20, 20)), text='╳', manager=ui_manager, container=self.get_container(), parent_element=self ) self.menu_bar = UIButton(relative_rect=pygame.Rect((0, 0), (self.get_container().rect.width-20, 20)), text='Everything Container', manager=ui_manager, container=self.get_container(), parent_element=self, object_id='#message_window_title_bar' ) self.menu_bar.set_hold_range((100, 100)) self.grabbed_window = False self.starting_grab_difference = (0, 0) self.test_slider = UIHorizontalSlider(pygame.Rect((int(self.rect.width / 2), int(self.rect.height * 0.70)), (240, 25)), 50.0, (0.0, 100.0), self.ui_manager, container=self.get_container(), parent_element=self) self.slider_label = UILabel(pygame.Rect((int(self.rect.width / 2) + 250, int(self.rect.height * 0.70)), (27, 25)), str(int(self.test_slider.get_current_value())), self.ui_manager, container=self.get_container(), parent_element=self) self.test_text_entry = UITextEntryLine(pygame.Rect((int(self.rect.width / 2), int(self.rect.height * 0.50)), (200, -1)), self.ui_manager, container=self.get_container(), parent_element=self) self.test_text_entry.set_forbidden_characters('numbers') current_resolution_string = 'Item 1' self.test_drop_down_menu = UIDropDownMenu(['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'], current_resolution_string, pygame.Rect((int(self.rect.width / 2), int(self.rect.height * 0.3)), (200, 25)), self.ui_manager, container=self.get_container(), parent_element=self) self.health_bar = UIScreenSpaceHealthBar(pygame.Rect((int(self.rect.width / 9), int(self.rect.height * 0.7)), (200, 20)), self.ui_manager, container=self.get_container(), parent_element=self) loaded_test_image = pygame.image.load('data/images/splat.png').convert_alpha() self.test_image = UIImage(pygame.Rect((int(self.rect.width / 9), int(self.rect.height * 0.3)), loaded_test_image.get_rect().size), loaded_test_image, self.ui_manager, container=self.get_container(), parent_element=self) self.is_selected = False
class EverythingWindow(UIWindow): def __init__(self, rect, ui_manager): element_ids = ['everything_window'] super().__init__(rect, ui_manager, element_ids=element_ids) # create shadow shadow_padding = (2, 2) self.image = self.ui_manager.get_shadow(self.rect.size) self.image.fill(self.ui_manager.get_theme().get_colour(self.object_ids, self.element_ids, 'dark_bg'), pygame.Rect(shadow_padding, (self.rect.width - shadow_padding[0] * 2, self.rect.height - shadow_padding[1] * 2) )) self.get_container().relative_rect.width = self.rect.width - shadow_padding[0] * 2 self.get_container().relative_rect.height = self.rect.height - shadow_padding[1] * 2 self.get_container().relative_rect.x = self.get_container().relative_rect.x + shadow_padding[0] self.get_container().relative_rect.y = self.get_container().relative_rect.y + shadow_padding[1] self.get_container().update_containing_rect_position() self.close_window_button = UIButton(relative_rect=pygame.Rect((self.get_container().rect.width-20, 0), (20, 20)), text='╳', manager=ui_manager, container=self.get_container(), parent_element=self ) self.menu_bar = UIButton(relative_rect=pygame.Rect((0, 0), (self.get_container().rect.width-20, 20)), text='Everything Container', manager=ui_manager, container=self.get_container(), parent_element=self, object_id='#message_window_title_bar' ) self.menu_bar.set_hold_range((100, 100)) self.grabbed_window = False self.starting_grab_difference = (0, 0) self.test_slider = UIHorizontalSlider(pygame.Rect((int(self.rect.width / 2), int(self.rect.height * 0.70)), (240, 25)), 50.0, (0.0, 100.0), self.ui_manager, container=self.get_container(), parent_element=self) self.slider_label = UILabel(pygame.Rect((int(self.rect.width / 2) + 250, int(self.rect.height * 0.70)), (27, 25)), str(int(self.test_slider.get_current_value())), self.ui_manager, container=self.get_container(), parent_element=self) self.test_text_entry = UITextEntryLine(pygame.Rect((int(self.rect.width / 2), int(self.rect.height * 0.50)), (200, -1)), self.ui_manager, container=self.get_container(), parent_element=self) self.test_text_entry.set_forbidden_characters('numbers') current_resolution_string = 'Item 1' self.test_drop_down_menu = UIDropDownMenu(['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'], current_resolution_string, pygame.Rect((int(self.rect.width / 2), int(self.rect.height * 0.3)), (200, 25)), self.ui_manager, container=self.get_container(), parent_element=self) self.health_bar = UIScreenSpaceHealthBar(pygame.Rect((int(self.rect.width / 9), int(self.rect.height * 0.7)), (200, 20)), self.ui_manager, container=self.get_container(), parent_element=self) loaded_test_image = pygame.image.load('data/images/splat.png').convert_alpha() self.test_image = UIImage(pygame.Rect((int(self.rect.width / 9), int(self.rect.height * 0.3)), loaded_test_image.get_rect().size), loaded_test_image, self.ui_manager, container=self.get_container(), parent_element=self) self.is_selected = False def select(self): self.is_selected = True def unselect(self): self.is_selected = False def process_event(self, event): processed_event = False if self.is_selected: if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: self.test_slider.set_current_value(50) if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: mouse_x, mouse_y = event.pos if self.rect.collidepoint(mouse_x, mouse_y): processed_event = True self.window_stack.move_window_to_front(self) return processed_event def update(self, time_delta): if self.alive(): if self.test_slider.has_moved_recently: self.slider_label.set_text(str(int(self.test_slider.get_current_value()))) if self.menu_bar.held: mouse_x, mouse_y = pygame.mouse.get_pos() if not self.grabbed_window: self.window_stack.move_window_to_front(self) self.grabbed_window = True self.starting_grab_difference = (mouse_x - self.rect.x, mouse_y - self.rect.y) current_grab_difference = (mouse_x - self.rect.x, mouse_y - self.rect.y) adjustment_required = (current_grab_difference[0] - self.starting_grab_difference[0], current_grab_difference[1] - self.starting_grab_difference[1]) self.rect.x += adjustment_required[0] self.rect.y += adjustment_required[1] self.get_container().relative_rect.x += adjustment_required[0] self.get_container().relative_rect.y += adjustment_required[1] self.get_container().update_containing_rect_position() else: self.grabbed_window = False if self.close_window_button.check_pressed(): self.kill() super().update(time_delta)
engineborder_north = smallfont.render('============================================================================' '============================================================================' '==========================', True, Render.DARKSHADE, Render.DARKSHADE) set_engineborder_north = engineborder_north.get_rect() set_engineborder_north.center = (0, 9) engineborder_south = smallfont.render('============================================================================' '============================================================================' '==========================', True, Render.DARKSHADE, Render.DARKSHADE) set_engineborder_south = engineborder_south.get_rect() set_engineborder_south.center = (0, 617) # Create the x and y horizontal float sliders playerxlocbar = UIHorizontalSlider(relative_rect=pygame.Rect(450, 205, 100, 20), start_value=80, value_range=(80, 400), visible=0, manager=mainframe) playerylocbar = UIHorizontalSlider(relative_rect=pygame.Rect(550, 205, 100, 20), start_value=215, value_range=(215, 425), visible=0, manager=mainframe) npcxlocbar = UIHorizontalSlider(relative_rect=pygame.Rect(450, 380, 100, 20), start_value=80, value_range=(80, 400), visible=0, manager=mainframe) npcylocbar = UIHorizontalSlider(relative_rect=pygame.Rect(550, 380, 100, 20), start_value=215, value_range=(215, 425), visible=0, manager=mainframe) # Menu variables for selecting the correct sprite images based on user choice