示例#1
0
    def test_finalise_to_surf(self, _init_pygame,
                              default_ui_manager: UIManager):
        input_data = deque([
            SimpleTestLayoutRect(dimensions=(50, 20)),
            SimpleTestLayoutRect(dimensions=(30, 20)),
            SimpleTestLayoutRect(dimensions=(90, 20)),
            SimpleTestLayoutRect(dimensions=(175, 20)),
            SimpleTestLayoutRect(dimensions=(50, 20)),
            SimpleTestLayoutRect(dimensions=(30, 20)),
            SimpleTestLayoutRect(dimensions=(90, 20)),
            SimpleTestLayoutRect(dimensions=(175, 20))
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        layout_surface = pygame.Surface((200, 300),
                                        depth=32,
                                        flags=pygame.SRCALPHA)
        layout_surface.fill((0, 0, 0, 0))
        layout.finalise_to_surf(layout_surface)

        assert layout_surface.get_at((10, 10)) != pygame.Color(0, 0, 0, 0)
示例#2
0
    def test_insert_text(self, _init_pygame, default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=True,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 500, 300),
                               view_rect=pygame.Rect(0, 0, 500, 150),
                               line_spacing=1.0)

        layout.insert_text('nother insertion', 15)

        row = layout.layout_rows[0]
        chunk = row.items[1]

        assert chunk.text == 'this is another insertion'
示例#3
0
    def test_vert_align_bottom_all_rows(self, _init_pygame,
                                        default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 500, 300),
                               view_rect=pygame.Rect(0, 0, 500, 150),
                               line_spacing=1.0)

        row = layout.layout_rows[0]
        assert row.y == 0
        layout.vert_align_bottom_all_rows(y_padding=8)
        assert row.bottom == 292
示例#4
0
    def test_set_cursor_from_click_pos(self, _init_pygame,
                                       default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 100, 300),
                               view_rect=pygame.Rect(0, 0, 100, 150),
                               line_spacing=1.0)

        layout.set_cursor_from_click_pos((17, 24))
        assert layout.cursor_text_row is not None
        assert layout.cursor_text_row.cursor_index == 2
        assert layout.cursor_text_row.cursor_draw_width == 17
示例#5
0
    def test_add_chunks_to_hover_group(self, _init_pygame,
                                       default_ui_manager: UIManager):
        input_data = deque([
            TextLineChunkFTFont(text='hello',
                                font=pygame.freetype.Font(None, 20),
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            HyperlinkTextChunk(href='test',
                               text='a link',
                               font=pygame.freetype.Font(None, 20),
                               underlined=False,
                               colour=pygame.Color('#FFFFFF'),
                               bg_colour=pygame.Color('#FF0000'),
                               hover_colour=pygame.Color('#0000FF'),
                               active_colour=pygame.Color('#FFFF00'),
                               hover_underline=False)
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        links_found = []
        layout.add_chunks_to_hover_group(links_found)
        assert len(links_found) == 1
示例#6
0
    def test_delete_selected_text(self, _init_pygame,
                                  default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=True,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 100, 300),
                               view_rect=pygame.Rect(0, 0, 100, 150),
                               line_spacing=1.0)

        layout.set_text_selection(5, 17)

        assert len(layout.selected_rows) == 2
        assert len(layout.selected_chunks) == 4

        selected_text = ""
        for chunk in layout.selected_chunks:
            selected_text += chunk.text

        assert selected_text == ' this is a t'

        layout.delete_selected_text()

        assert len(layout.selected_rows) == 0
        assert len(layout.selected_chunks) == 0

        selected_text = ""
        for chunk in layout.selected_chunks:
            selected_text += chunk.text

        assert selected_text == ''

        remaining_text = ''
        for row in layout.layout_rows:
            for chunk in row.items:
                remaining_text += chunk.text

        assert remaining_text == 'helloest'
示例#7
0
    def test_creation_with_data(self, _init_pygame,
                                default_ui_manager: UIManager):
        input_data = deque([
            SimpleTestLayoutRect(dimensions=(50, 20)),
            SimpleTestLayoutRect(dimensions=(30, 20)),
            SimpleTestLayoutRect(dimensions=(90, 20)),
            SimpleTestLayoutRect(dimensions=(175, 20)),
            SimpleTestLayoutRect(dimensions=(50, 20)),
            SimpleTestLayoutRect(dimensions=(30, 20)),
            SimpleTestLayoutRect(dimensions=(90, 20)),
            SimpleTestLayoutRect(dimensions=(175, 20))
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        assert len(layout.layout_rows) > 0
示例#8
0
    def parse_html_into_style_data(self):
        """
        Parses HTML styled string text into a format more useful for styling pygame.freetype
        rendered text.
        """

        self.parser.feed(translate(self.html_text) + self.appended_text)

        self.text_box_layout = TextBoxLayout(
            self.parser.layout_rect_queue,
            pygame.Rect((0, 0),
                        (self.text_wrap_rect[2], self.text_wrap_rect[3])),
            pygame.Rect((0, 0),
                        (self.text_wrap_rect[2], self.text_wrap_rect[3])),
            line_spacing=1.25)
        self.parser.empty_layout_queue()
        if self.text_wrap_rect[3] == -1:
            self.text_box_layout.view_rect.height = self.text_box_layout.layout_rect.height

        self._align_all_text_rows()
        self.text_box_layout.finalise_to_new()
示例#9
0
    def test_backspace_at_cursor(self, _init_pygame,
                                 default_ui_manager: UIManager):
        the_font = pygame.freetype.Font(None, 20)
        input_data = deque([
            TextLineChunkFTFont(text='hello ',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text='this is a',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=True,
                                bg_colour=pygame.Color('#FF0000')),
            TextLineChunkFTFont(text=' test',
                                font=the_font,
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000')),
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 100, 300),
                               view_rect=pygame.Rect(0, 0, 100, 150),
                               line_spacing=1.0)

        layout.set_cursor_position(16)
        layout.backspace_at_cursor()
        layout.backspace_at_cursor()

        remaining_text = ''
        for row in layout.layout_rows:
            for chunk in row.items:
                remaining_text += chunk.text

        assert remaining_text == 'hello this is test'
示例#10
0
    def test_set_alpha(self, _init_pygame, default_ui_manager: UIManager):
        input_data = deque([
            SimpleTestLayoutRect(dimensions=(50, 20)),
            SimpleTestLayoutRect(dimensions=(30, 20)),
            SimpleTestLayoutRect(dimensions=(90, 20)),
            SimpleTestLayoutRect(dimensions=(175, 20)),
            SimpleTestLayoutRect(dimensions=(50, 20)),
            SimpleTestLayoutRect(dimensions=(30, 20)),
            SimpleTestLayoutRect(dimensions=(90, 20)),
            SimpleTestLayoutRect(dimensions=(175, 20))
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        layout_surface = layout.finalise_to_new()

        assert layout_surface.get_at((4, 4)) != pygame.Color(0, 0, 0, 0)
        layout.set_alpha(128)
        layout_surface = layout.finalised_surface
        assert layout_surface.get_at((4, 4)).a == 127
示例#11
0
    def test_update_text_with_new_text_end_pos(self, _init_pygame,
                                               default_ui_manager: UIManager):
        input_data = deque([
            TextLineChunkFTFont(text='hello',
                                font=pygame.freetype.Font(None, 20),
                                underlined=False,
                                colour=pygame.Color('#FFFFFF'),
                                using_default_text_colour=False,
                                bg_colour=pygame.Color('#FF0000'))
        ])

        layout = TextBoxLayout(input_data_queue=input_data,
                               layout_rect=pygame.Rect(0, 0, 200, 300),
                               view_rect=pygame.Rect(0, 0, 200, 150),
                               line_spacing=1.0)

        layout_surface = layout.finalise_to_new()
        layout.update_text_with_new_text_end_pos(
            0)  # this does nothing unless we pass in text
        assert layout_surface.get_at((10, 10)) == pygame.Color(0, 0, 0, 0)

        layout.update_text_with_new_text_end_pos(3)
        assert layout_surface.get_at((10, 10)) == pygame.Color(255, 0, 0, 255)
示例#12
0
 def test_creation(self, _init_pygame, default_ui_manager: UIManager):
     input_data = deque([])
     TextBoxLayout(input_data_queue=input_data,
                   layout_rect=pygame.Rect(0, 0, 200, 300),
                   view_rect=pygame.Rect(0, 0, 200, 150),
                   line_spacing=1.0)