def test_set_any_images_from_theme(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_button_with_images.json")) button = UIButton(relative_rect=pygame.Rect(100, 100, 150, 30), text="Test Button", tool_tip_text="This is a test of the button's tool tip functionality.", manager=manager) assert button.normal_image is not None and button.image is not None
def test_show_hide_rendering(self, _init_pygame, default_ui_manager, _display_surface_return_none): resolution = (600, 600) empty_surface = pygame.Surface(resolution) empty_surface.fill(pygame.Color(0, 0, 0)) surface = empty_surface.copy() manager = UIManager(resolution) message_window = UIMessageWindow( rect=pygame.Rect(100, 100, 250, 300), window_title="Test Message", html_message="This is a bold test of the " "message box functionality.", 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)) message_window.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) message_window.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def test_non_default_theme_build(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_tool_tip_non_default.json")) tool_tip = UITooltip(html_text="A tip about tools.", hover_distance=(0, 10), manager=manager) assert tool_tip.text_block.image is not None
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) text_box = UITextBox(html_text="some test text", relative_rect=pygame.Rect(100, 100, 400, 400), manager=manager, wrap_to_height=False, layer_starting_height=100, object_id="screen_message", visible=0) manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) text_box.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) text_box.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def test_default_selection_sets_correctly(self): """ Test that the default selection parameter ACTUALLY sets the values. """ resolution = (400, 400) manager = UIManager(resolution) lst = [f'item {i}' for i in range(20)] selection = 'item 10' # Single-selection list default. single_list = UISelectionList(relative_rect=pygame.Rect( 100, 100, 400, 400), item_list=lst, default_selection=selection, manager=manager) assert selection == single_list.get_single_selection() # Multi-selection list defaults. selection = ['item 3', 'item 10', 'item 15'] multi_list = UISelectionList(relative_rect=pygame.Rect( 100, 100, 400, 400), item_list=lst, default_selection=selection, allow_multi_select=True, manager=manager) assert selection == multi_list.get_multi_selection()
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_text_box_non_default.json")) manager.preload_fonts([{ "name": "fira_code", "size:": 14, "style": "bold" }, { "name": "fira_code", "size:": 14, "style": "italic" }]) text_box = UITextBox( html_text="<font color=#FF0000 face=fira_code>Some " "<font color=regular_text>text</font> " "in a <b>bold box</b> using " "colours and <i>styles</i>.</font>", relative_rect=pygame.Rect(100, 100, 200, 300), manager=manager) text_box.redraw_from_chunks() text_box.full_redraw() assert text_box.image is not None
def __init__(self): pygame.init() pygame.display.set_caption("Options UI") self.options = Options() if self.options.fullscreen: self.window_surface = pygame.display.set_mode(self.options.resolution, pygame.FULLSCREEN) else: self.window_surface = pygame.display.set_mode(self.options.resolution) self.background_surface = None self.ui_manager = UIManager(self.options.resolution, 'data/themes/theme_2.json') # , 'data/themes/theme_2.json' self.ui_manager.preload_fonts([{'name': 'fira_code', 'point_size': 10, 'style': 'bold'}, {'name': 'fira_code', 'point_size': 10, 'style': 'regular'}, {'name': 'fira_code', 'point_size': 10, 'style': 'italic'}, {'name': 'fira_code', 'point_size': 14, 'style': 'italic'}, {'name': 'fira_code', 'point_size': 14, 'style': 'bold'} ]) self.test_button = None self.test_button_2 = None self.test_slider = None self.test_text_entry = None self.test_drop_down_menu = None self.recreate_ui() self.clock = pygame.time.Clock() self.button_response_timer = pygame.time.Clock() self.running = True
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) healthy_sprite = UIWorldSpaceHealthBar.ExampleHealthSprite() health_bar = UIWorldSpaceHealthBar(relative_rect=pygame.Rect( 100, 100, 400, 400), sprite_to_monitor=healthy_sprite, 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)) health_bar.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) health_bar.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
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) label = UILabel(relative_rect=pygame.Rect(25, 25, 375, 150), text="Test Button", 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)) label.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) label.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def test_set_active_effect_with_word_split(self, _init_pygame: None, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_text_box_non_default.json")) manager.preload_fonts([{"name": "fira_code", "point_size": 10, "style": "regular"}, {'name': 'fira_code', 'point_size': 10, 'style': 'bold'}, {"name": "fira_code", "point_size": 10, "style": "italic"}, {"name": "fira_code", "point_size": 10, "style": "bold_italic"}]) htm_text_block_2 = UITextBox('<font face=fira_code size=2 color=#000000>' '<b>Hey, What the heck!</b>' '<br><br>' 'This is some <a href="test">text</a> in a different box,' ' hooray for variety - ' 'if you want then you should put a ring upon it. ' '<body bgcolor=#990000>What if we do a really long word?' '</body> ' '<b><i>derp FALALALALALALALXALALALXALALALALAAPaaaaarp' ' gosh</b></i></font>', pygame.Rect((0, 0), (250, 200)), manager=manager, object_id="#text_box_2") htm_text_block_2.set_active_effect('typing_appear') htm_text_block_2.active_text_effect.time_to_redraw = True htm_text_block_2.update(5.0) htm_text_block_2.update(5.0) assert type(htm_text_block_2.active_text_effect) == pygame_gui.elements.text.TypingAppearEffect
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) panel = UIPanel(pygame.Rect(25, 25, 375, 150), manager=manager, visible=0, starting_layer_height=1) button = UIButton(relative_rect=pygame.Rect(0, 0, 50, 50), text="", manager=manager, container=panel) manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) panel.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) panel.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def test_rebuild_from_theme_data_non_default(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_world_health_bar_non_default.json")) healthy_sprite = UIWorldSpaceHealthBar.ExampleHealthSprite() health_bar = UIWorldSpaceHealthBar(relative_rect=pygame.Rect(100, 100, 150, 30), sprite_to_monitor=healthy_sprite, manager=manager) assert health_bar.image is not None
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) scroll_bar = UIVerticalScrollBar(relative_rect=pygame.Rect(100, 100, 400, 400), visible_percentage=0.25, 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)) scroll_bar.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) scroll_bar.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def test_draw_ui(self, _init_pygame): """ Test that drawing the UI works. Note: the pygame comparison function here seems a little unreliable. Would not be surprised if it's behaviour changes. """ test_surface = pygame.display.set_mode((150, 30)) manager = UIManager((150, 30)) UIButton(relative_rect=pygame.Rect(0, 0, 150, 30), text="Test", manager=manager) # manager.update(0.01) manager.draw_ui(test_surface) comparison_surface = pygame.image.load(os.path.join('tests', 'comparison_images', 'test_draw_ui.png')).convert() test_pixel_array = pygame.PixelArray(test_surface) comparison_pixel_array = pygame.PixelArray(comparison_surface) result_pixel_array = test_pixel_array.compare(comparison_pixel_array) result_surface = result_pixel_array.make_surface() test_pixel_array.close() comparison_pixel_array.close() pixel_mismatch = False for x in range(0, 150): for y in range(0, 30): if result_pixel_array[x, y] != result_surface.map_rgb((255, 255, 255, 255)): pixel_mismatch = True break result_pixel_array.close() assert pixel_mismatch is False
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) container = UIScrollingContainer(relative_rect=pygame.Rect( 100, 100, 200, 100), manager=manager, visible=0) container.set_scrollable_area_dimensions((600, 600)) manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) container.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) container.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
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) text_entry = UITextEntryLine(relative_rect=pygame.Rect( 100, 100, 400, 400), 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)) text_entry.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) text_entry.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
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) loaded_image = pygame.image.load( os.path.join('tests', 'data', 'images', 'splat.png')) ui_image = UIImage(relative_rect=pygame.Rect(25, 25, 375, 150), image_surface=loaded_image, 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)) ui_image.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) ui_image.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
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) selection_list = UISelectionList( relative_rect=pygame.Rect(100, 100, 400, 400), item_list=[ 'item 1', 'item 2', 'item 3', 'item 4', 'item 5', 'item 6', 'item 7', 'item 8', 'item 9', 'item 10', 'item 11', 'item 12', 'item 13', 'item 14', 'item 15', 'item 16' ], manager=manager, allow_multi_select=True, visible=0) manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) selection_list.show() manager.update(0.01) manager.draw_ui(surface) assert not compare_surfaces(empty_surface, surface) surface.fill(pygame.Color(0, 0, 0)) selection_list.hide() manager.update(0.01) manager.draw_ui(surface) assert compare_surfaces(empty_surface, surface)
def test_rebuild_from_theme_data_bad_values(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_screen_health_bar_bad_values.json")) healthy_sprite = HealthySprite() health_bar = UIScreenSpaceHealthBar(relative_rect=pygame.Rect(100, 100, 150, 30), sprite_to_monitor=healthy_sprite, manager=manager) assert health_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_label_bad_values.json")) label = UILabel(relative_rect=pygame.Rect(100, 100, 10, 30), text="Test Label", manager=manager) assert label.image is not None
def test_bad_values_theme_build(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_tool_tip_bad_values.json")) tool_tip = UITooltip(html_text="A tip about tools.", hover_distance=(0, 10), manager=manager) assert tool_tip.text_block.image is not None
def test_rebuild_from_changed_theme_data_bad_values(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_button_bad_values.json")) 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=manager) assert button.image is not None
def test_bad_values_theme_build(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_message_window_bad_values.json")) message_window = UIMessageWindow(message_window_rect=pygame.Rect(100, 100, 200, 300), message_title="Test Message", html_message="This is a bold test of the message box functionality.", manager=manager) assert message_window.image is not None
def test_cropping_size_of_drop_down(self, _init_pygame, default_ui_manager): menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar'], starting_option='eggs', relative_rect=pygame.Rect(100, 100, 200, 30), manager=default_ui_manager) menu.current_state.should_transition = True menu.update(0.01) assert menu.current_state.options_selection_list.rect.height == 366 # uncropped menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar'], starting_option='eggs', relative_rect=pygame.Rect(100, 100, 200, 30), manager=default_ui_manager, expansion_height_limit=200) menu.current_state.should_transition = True menu.update(0.01) assert menu.current_state.options_selection_list.scroll_bar is not None assert menu.current_state.options_selection_list.rect.height == 200 # cropped to fixed height test_container = UIContainer(relative_rect=pygame.Rect(100, 100, 300, 100), manager=default_ui_manager) menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar'], starting_option='eggs', relative_rect=pygame.Rect(10, 10, 200, 30), manager=default_ui_manager, container=test_container) menu.current_state.should_transition = True menu.update(0.01) assert menu.current_state.options_selection_list.rect.height == 63 # cropped to container size by default manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_drop_down_menu_non_default.json")) test_container = UIContainer(relative_rect=pygame.Rect(100, 100, 300, 100), manager=manager) menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar', 'eggs', 'flour', 'sugar'], starting_option='eggs', relative_rect=pygame.Rect(10, 50, 200, 30), manager=manager, container=test_container) menu.current_state.should_transition = True menu.update(0.01) assert menu.current_state.options_selection_list.rect.height == 53 # cropped to container size by default
def test_hide_and_show_of_disabled_button(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600)) button = UIButton(relative_rect=pygame.Rect(100, 100, 100, 100), text="button test", manager=manager, starting_height=1) button.disable() button.hide() button.show() assert button.drawable_shape.active_state.state_id == 'disabled'
def test_rebuild_from_theme_data_bad_values(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_text_entry_line_bad_values.json")) text_entry = UITextEntryLine(relative_rect=pygame.Rect( 100, 100, 200, 30), manager=manager) assert text_entry.image is not None
def test_rebuild_from_theme_data_non_default(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_status_bar_no_default.json")) healthy_sprite = HealthySprite() health_bar = UIStatusBar(relative_rect=pygame.Rect(100, 100, 150, 30), sprite=healthy_sprite, manager=manager) assert health_bar.image is not None
def test_rebuild_shape_ellipse(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_button_non_default_2.json")) 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=manager) button.rebuild_shape() assert button.image is not None
def test_incremental_loading_nothing(self, _init_pygame, _display_surface_return_none): incremental_loader = IncrementalThreadedResourceLoader() UIManager((800, 600), resource_loader=incremental_loader) incremental_loader.start() finished = False while not finished: finished, progress = incremental_loader.update() assert finished
def test_rebuild_from_theme_data_bad_values(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_vertical_scroll_bar_bad_values.json")) scroll_bar = UIVerticalScrollBar(relative_rect=pygame.Rect(100, 100, 30, 150), visible_percentage=1.0, manager=manager) assert scroll_bar.image is not None