def test_show_without_scrollbar(self, _init_pygame, default_ui_manager, _display_surface_return_none): x = 640 y = 360 width = 320 height = 100 text = "this is a test" rect = pygame.Rect((x, y), (width, height)) text_box = UITextBox(html_text=text, relative_rect=rect, manager=default_ui_manager, wrap_to_height=False, layer_starting_height=100, object_id="screen_message", visible=0) assert text_box.visible == 0 assert text_box.scroll_bar is None text_box.show() assert text_box.visible == 1 assert text_box.scroll_bar is None
def test_hide_with_scrollbar(self, _init_pygame, default_ui_manager, _display_surface_return_none): x = 640 y = 360 width = 100 height = 32 text = "this is a test, this is a test, this is a test, this is a test, this is a test, this is a test" rect = pygame.Rect((x, y), (width, height)) text_box = UITextBox(html_text=text, relative_rect=rect, manager=default_ui_manager, wrap_to_height=False, layer_starting_height=100, object_id="screen_message") assert text_box.visible == 1 assert text_box.scroll_bar.visible == 1 assert text_box.scroll_bar.button_container.visible == 1 assert text_box.scroll_bar.sliding_button.visible == 1 assert text_box.scroll_bar.top_button.visible == 1 assert text_box.scroll_bar.bottom_button.visible == 1 text_box.hide() assert text_box.visible == 0 assert text_box.scroll_bar.visible == 0 assert text_box.scroll_bar.button_container.visible == 0 assert text_box.scroll_bar.sliding_button.visible == 0 assert text_box.scroll_bar.top_button.visible == 0 assert text_box.scroll_bar.bottom_button.visible == 0
def test_redraw_from_text_block_no_scrollbar( self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.redraw_from_text_block() assert text_box.image is not None
def __init__(self, pos, manager, clear, parse, name): super().__init__(Rect(pos, (400, 300)), manager, name, resizable=True) self.textbox = UITextBox("", relative_rect=Rect(0, 0, 368, 200), manager=manager, container=self, anchors={ "left": "left", "right": "right", "top": "top", "bottom": "bottom" }) self.input = UITextEntryLine(relative_rect=Rect(0, -35, 368, 30), manager=manager, container=self, anchors={ "left": "left", "right": "right", "top": "bottom", "bottom": "bottom" }) self.text = '' self.manager = manager self.input.focus() self.clear = clear self.parse = parse
def prepare_chatbox(self): text = '' for type, auth, txt in self.app.messages: if type == 'regular': text += f"""<font color="{ColorHash(auth).hex}">{auth}</font>: {txt}<br/>""" elif type == 'event': text += f"""<font color="#FAA237">{txt}</font><br/>""" if self.chatbox is None: self.chatbox = UITextBox( text, pygame.Rect((560, 0), (300, SCREEN_HEIGHT - 60)), self.ui) else: self.chatbox.html_text = text self.chatbox.rebuild() if self.chatbox.scroll_bar: scroll_bar = self.chatbox.scroll_bar scroll_bar.scroll_wheel_down = False scroll_bar.scroll_position += (250 * 1) scroll_bar.scroll_position = min( scroll_bar.scroll_position, scroll_bar.bottom_limit - scroll_bar.sliding_button.rect.height) x_pos = scroll_bar.rect.x + scroll_bar.shadow_width + scroll_bar.border_width y_pos = scroll_bar.scroll_position + scroll_bar.rect.y + scroll_bar.shadow_width + \ scroll_bar.border_width + scroll_bar.button_height scroll_bar.sliding_button.set_position( pygame.math.Vector2(x_pos, y_pos)) scroll_bar.start_percentage = scroll_bar.scroll_position / \ scroll_bar.scrollable_height if not scroll_bar.has_moved_recently: scroll_bar.has_moved_recently = True
def process_event(self, event): super().process_event(event) if event.type == 769 and event.key == 13: self.text += self.input.get_text() + "<br>" self.input.kill() self.textbox.kill() self.textbox = UITextBox(self.parse(self.text), relative_rect=Rect(0, 0, 368, 200), manager=self.manager, container=self, anchors={ "left": "left", "right": "right", "top": "top", "bottom": "bottom" }) self.input = UITextEntryLine(relative_rect=Rect(0, -35, 368, 30), manager=self.manager, container=self, anchors={ "left": "left", "right": "right", "top": "bottom", "bottom": "bottom" }) self.input.focus()
def test_process_event_mouse_buttons_with_scrollbar( self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none: None): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(0, 0, 150, 100), manager=default_ui_manager) processed_down_event = text_box.process_event( pygame.event.Event(pygame.MOUSEBUTTONDOWN, { 'button': 1, 'pos': (30, 15) })) text_box.process_event( pygame.event.Event(pygame.MOUSEBUTTONUP, { 'button': 1, 'pos': (80, 15) })) assert processed_down_event is True
def test_set_dimensions(self, _init_pygame, default_ui_manager, _display_surface_return_none): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al' 'al alalalal lfed alal alal alal al' 'ala lalalal lasda lal a lalalal slapl' 'alalala lal la blop lal alal aferlal al', relative_rect=pygame.Rect(0, 0, 150, 100), manager=default_ui_manager) text_box.set_dimensions((200, 80)) # try to click on the slider default_ui_manager.process_events( pygame.event.Event(pygame.MOUSEBUTTONDOWN, { 'button': 1, 'pos': (195, 75) })) # if we successfully clicked on the moved slider then this button should be True assert text_box.scroll_bar.bottom_button.held is True
def test_disable(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none: None): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(0, 0, 150, 100), manager=default_ui_manager) text_box.disable() assert text_box.is_enabled is False assert text_box.scroll_bar.is_enabled is False # process a mouse button down event text_box.scroll_bar.bottom_button.process_event( pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': 1, 'pos': text_box.scroll_bar.bottom_button.rect.center})) text_box.scroll_bar.update(0.1) # process a mouse button up event text_box.scroll_bar.bottom_button.process_event( pygame.event.Event(pygame.MOUSEBUTTONUP, {'button': 1, 'pos': text_box.scroll_bar.bottom_button.rect.center})) assert text_box.scroll_bar.scroll_position == 0.0
def test_create_very_short_text_box(self, _init_pygame, default_ui_manager, _display_surface_return_none): x = 640 y = 360 width = 320 height = 10 # height will auto increase text = "this is a test" rect = pygame.Rect((x, y), (width, height)) text_box = UITextBox(html_text=text, relative_rect=rect, manager=default_ui_manager, wrap_to_height=True, layer_starting_height=100, object_id="screen_message") assert text_box.rect.height != 10 x = 640 y = 360 width = 320 height = 0 # height will auto increase text = "this is a test" rect = pygame.Rect((x, y), (width, height)) text_box = UITextBox(html_text=text, relative_rect=rect, manager=default_ui_manager, wrap_to_height=True, layer_starting_height=100, object_id="screen_message") assert text_box.rect.height != 0
class Cat(UIWindow): def __init__(self, pos, manager, clear, parse, name): super().__init__(Rect(pos, (400, 300)), manager, name, resizable=True) self.textbox = UITextBox("", relative_rect=Rect(0, 0, 368, 200), manager=manager, container=self, anchors={ "left": "left", "right": "right", "top": "top", "bottom": "bottom" }) self.input = UITextEntryLine(relative_rect=Rect(0, -35, 368, 30), manager=manager, container=self, anchors={ "left": "left", "right": "right", "top": "bottom", "bottom": "bottom" }) self.text = '' self.manager = manager self.input.focus() self.clear = clear self.parse = parse def process_event(self, event): super().process_event(event) if event.type == 769 and event.key == 13: self.text += self.input.get_text() + "<br>" self.input.kill() self.textbox.kill() self.textbox = UITextBox(self.parse(self.text), relative_rect=Rect(0, 0, 368, 200), manager=self.manager, container=self, anchors={ "left": "left", "right": "right", "top": "top", "bottom": "bottom" }) self.input = UITextEntryLine(relative_rect=Rect(0, -35, 368, 30), manager=self.manager, container=self, anchors={ "left": "left", "right": "right", "top": "bottom", "bottom": "bottom" }) self.input.focus() def kill(self): super().kill() self.clear.cats.remove(self)
def test_creation(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_TILT, effect_tag='test') assert isinstance(text_box.active_text_chunk_effects[0]['effect'], TiltEffect)
def test_creation_and_rebuild_with_scrollbar(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): default_ui_manager.preload_fonts([{'name': 'fira_code', 'html_size': 4.5, 'style': 'bold'}, {'name': 'fira_code', 'html_size': 4.5, 'style': 'regular'}, {'name': 'fira_code', 'html_size': 2, 'style': 'regular'}, {'name': 'fira_code', 'html_size': 2, 'style': 'italic'}, {'name': 'fira_code', 'html_size': 6, 'style': 'bold'}, {'name': 'fira_code', 'html_size': 6, 'style': 'regular'}, {'name': 'fira_code', 'html_size': 6, 'style': 'bold_italic'}, {'name': 'fira_code', 'html_size': 4, 'style': 'bold'}, {'name': 'fira_code', 'html_size': 4, 'style': 'italic'}, {'name': 'fira_code', 'html_size': 2, 'style': 'bold'}, {'name': 'fira_code', 'html_size': 2, 'style': 'bold_italic'}]) text_box = UITextBox(html_text='' '<font color=regular_text><font color=#E784A2 size=4.5><br><b><u>Lorem</u><br><br><br>' 'ipsum dolor sit amet</b></font>,' ' <b><a href="test">consectetur</a></b> adipiscing elit. in a flibb de dib do ' 'rub a la clob slip the perry tin fo glorp yip dorp' 'skorp si pork flum de dum be dung, slob be robble glurp destination flum kin slum. ' 'Ram slim gordo, fem tulip squirrel slippers save socks certainly.<br>' 'Vestibulum in <i>commodo me</i> tellus in nisi finibus a sodales.<br>Vestibulum' '<font size=2>hendrerit mi <i>sed nulla</i> scelerisque</font>, posuere ullamcorper ' 'sem pulvinar.' 'Nulla at pulvinar a odio, a dictum dolor.<br>Maecenas at <font size=6><b>tellus a' 'tortor. a<br>' 'In <i>bibendum</i> orci et velit</b> gravida lacinia.<br><br>' 'In hac a habitasse to platea dictumst.<br>' '<font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec porttitor.<br>Morbi ' 'accumsan, lectus at ' 'tincidunt to dictum, neque <font color=#879AF6>erat tristique erat</font>, ' 'sed a tempus for <b>nunc</b> dolor in nibh.<br>' 'Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, sit amet on pharetra a ante ' 'sollicitudin.</font></font>' '<br><br>' '<b>consectetur</b> adipiscing elit. in a<br>' 'Vestibulum in <i>commodo me</i> tellus in nisi finibus a sodales.<br>' 'Vestibulum <font size=2>hendrerit mi <i>sed nulla</i> scelerisque</font>, ' 'posuere ullamcorper sem pulvinar. ' 'Nulla at pulvinar a odio, a dictum dolor.<br>' 'Maecenas at <font size=6><b>tellus a tortor. a<br>' 'In <i>bibendum</i> orci et velit</b> gravida lacinia.<br><br>' 'In hac a habitasse to platea dictumst.<br>' '<font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec porttitor.<br>Morbi ' 'accumsan, lectus at' 'tincidunt to dictum, neque <font color=#879AF6>erat tristique erat</font>, ' 'sed a tempus for <b>nunc</b> dolor in nibh.<br>' 'Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, sit amet on pharetra a ante ' 'sollicitudin.</font></font>', relative_rect=pygame.Rect(100, 100, 200, 300), manager=default_ui_manager) text_box.rebuild() assert text_box.image is not None
def test_params(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_FADE_OUT, params={'time_per_alpha_change': 19.0}, effect_tag='test') assert isinstance(text_box.active_text_chunk_effects[0]['effect'], FadeOutEffect)
def test_set_relative_position_with_scrollbar( self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala alala alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.set_relative_position(pygame.Vector2(0.0, 0.0)) assert text_box.rect.topleft == (0, 0)
def test_params(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_TYPING_APPEAR, params={ 'time_per_letter': 3.0, 'time_per_letter_deviation': 1.0 }, effect_tag='test') assert isinstance(text_box.active_text_chunk_effects[0]['effect'], TypingAppearEffect)
def test_set_active_effect_invalid(self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) with pytest.warns(UserWarning, match="Unsupported effect name"): text_box.set_active_effect(pygame_gui.UI_BUTTON_PRESSED)
def test_full_redraw_with_scrollbar(self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.full_redraw() assert text_box.image is not None
def test_select_with_scrollbar(self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.select() assert text_box.scroll_bar.sliding_button.is_selected is True
def test_params(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_BOUNCE, params={ 'loop': False, 'bounce_max_height': 10, 'time_to_complete_bounce': 19.0 }, effect_tag='test') assert isinstance(text_box.active_text_chunk_effects[0]['effect'], BounceEffect)
def test_has_text_changed(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_TILT, effect_tag='test') effect: TiltEffect = text_box.active_text_chunk_effects[0]['effect'] assert not effect.has_text_changed() effect.update(time_delta=0.06) effect.update(time_delta=0.06) assert effect.has_text_changed()
def test_redraw_from_chunks_with_scrollbar(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.redraw_from_chunks() assert text_box.image is not None
def test_on_fresh_drawable_shape_ready(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala alala alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 100, 100), manager=default_ui_manager) text_box.on_fresh_drawable_shape_ready() assert text_box.background_surf is not None
def test_params(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_TILT, params={ 'loop': False, 'max_rotation': 360, 'time_to_complete_rotation': 9.0 }, effect_tag='test') assert isinstance(text_box.active_text_chunk_effects[0]['effect'], TiltEffect)
def test_params(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_EXPAND_CONTRACT, params={ 'loop': False, 'max_scale': 2.0, 'time_to_complete_expand_contract': 10.0 }, effect_tag='test') assert isinstance(text_box.active_text_chunk_effects[0]['effect'], ExpandContractEffect)
def test_set_active_effect_none(self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.set_active_effect(None) assert text_box.active_text_effect is None
def test_update_with_scrollbar(self, _init_pygame: None, default_ui_manager: UIManager): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) text_box.scroll_bar.has_moved_recently = True text_box.update(5.0) assert text_box.image is not None
def test_set_active_effect_invalid(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) with pytest.warns(UserWarning, match="Unsupported effect name"): text_box.set_active_effect("ghost_text")
def test_select_with_scrollbar(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): text_box = UITextBox(html_text='la la LA LA LAL LAL ALALA' 'LLALAALALA ALALA ALAL ALA' 'LAALA ALALA ALALA AAaal aa' 'ALALAa laalal alalal alala' 'alalalala alalalalalal alal' 'alalalala <a href=none>alala<a/> ' 'alalala ala' 'alalalalal lalal alalalal al', relative_rect=pygame.Rect(100, 100, 150, 100), manager=default_ui_manager) default_ui_manager.set_focus_set(text_box.get_focus_set()) assert text_box.scroll_bar.is_focused is True
def test_update(self, _init_pygame, default_ui_manager: UIManager): text_box = UITextBox('<effect id=test>Hello world</effect>', pygame.Rect((10, 10), (200, 100)), default_ui_manager) text_box.set_active_effect(TEXT_EFFECT_BOUNCE, effect_tag='test') effect = text_box.active_text_chunk_effects[0]['effect'] assert effect.bounce_height == 0 effect.update(time_delta=0.06) effect.update(time_delta=0.06) assert effect.bounce_height != 0