コード例 #1
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)
コード例 #2
0
    def test_kill(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)

        assert len(default_ui_manager.get_root_container().elements) == 3
        assert len(default_ui_manager.get_sprite_group().sprites()) == 7
        assert default_ui_manager.get_sprite_group().sprites() == [
            default_ui_manager.get_root_container(), text_box,
            text_box.scroll_bar, text_box.scroll_bar.button_container,
            text_box.scroll_bar.top_button, text_box.scroll_bar.bottom_button,
            text_box.scroll_bar.sliding_button
        ]
        text_box.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()
        ]
コード例 #3
0
class UITooltip(UIElement, IUITooltipInterface):
    """
    A tool tip is a floating block of text that gives additional information after a user hovers
    over an interactive part of a GUI for a short time. In Pygame GUI the tooltip's text is
    style-able with HTML.

    At the moment the tooltips are only available as an option on UIButton elements.

    Tooltips also don't allow a container as they are designed to overlap normal UI boundaries and
    be contained only within the 'root' window/container, which is synonymous with the pygame
    display surface.

    :param html_text: Text styled with HTML, to be displayed on the tooltip.
    :param hover_distance: Distance in pixels between the tooltip and the thing being hovered.
    :param manager: The UIManager that manages this element.
    :param parent_element: The element this element 'belongs to' in the theming hierarchy.
    :param object_id: A custom defined ID for fine tuning of theming.
    :param anchors: A dictionary describing what this element's relative_rect is relative to.

    """
    def __init__(self,
                 html_text: str,
                 hover_distance: Tuple[int, int],
                 manager: IUIManagerInterface,
                 parent_element: UIElement = None,
                 object_id: Union[str, None] = None,
                 anchors: Dict[str, str] = None):

        super().__init__(
            relative_rect=pygame.Rect((0, 0), (-1, -1)),
            manager=manager,
            container=None,
            starting_height=manager.get_sprite_group().get_top_layer() + 1,
            layer_thickness=1,
            anchors=anchors)

        self._create_valid_ids(container=None,
                               parent_element=parent_element,
                               object_id=object_id,
                               element_id='tool_tip')

        self.text_block = None
        self.rect_width = None
        self.hover_distance_from_target = hover_distance

        self.rebuild_from_changed_theme_data()

        self.text_block = UITextBox(html_text,
                                    pygame.Rect(0, 0, self.rect_width, -1),
                                    manager=self.ui_manager,
                                    layer_starting_height=self._layer,
                                    parent_element=self)

        self.set_dimensions(self.text_block.rect.size)

        self.set_image(self.ui_manager.get_universal_empty_surface())

    def rebuild(self):
        """
        Rebuild anything that might need rebuilding.

        """
        self.set_image(self.ui_manager.get_universal_empty_surface())

        if self.text_block is not None:
            self.text_block.set_dimensions((self.rect_width, -1))

            self.relative_rect.height = self.text_block.rect.height
            self.relative_rect.width = self.text_block.rect.width
            self.rect.width = self.text_block.rect.width
            self.rect.height = self.text_block.rect.height

    def kill(self):
        """
        Overrides the UIElement's default kill method to also kill the text block element that
        helps make up the complete tool tip.
        """
        self.text_block.kill()
        super().kill()

    def find_valid_position(self, position: pygame.math.Vector2) -> bool:
        """
        Finds a valid position for the tool tip inside the root container of the UI.

        The algorithm starts from the position of the target we are providing a tool tip for then it
        tries to fit the rectangle for the tool tip onto the screen by moving it above, below, to
        the left and to the right, until we find a position that fits the whole tooltip rectangle
        on the screen at once.

        If we fail to manage this then the method will return False. Otherwise it returns True and
        set the position of the tool tip to our valid position.

        :param position: A 2D vector representing the position of the target this tool tip is for.

        :return: returns True if we find a valid (visible) position and False if we do not.

        """

        window_rect = self.ui_manager.get_root_container().rect

        if window_rect.contains(
                pygame.Rect(int(position[0]), int(position[1]), 1, 1)):
            self.rect.left = int(position.x)
            self.rect.top = int(position.y +
                                self.hover_distance_from_target[1])

            if window_rect.contains(self.rect):
                self.relative_rect = self.rect.copy()
                self.text_block.set_position(self.rect.topleft)
                return True
            else:
                if self.rect.bottom > window_rect.bottom:
                    self.rect.bottom = int(position.y -
                                           self.hover_distance_from_target[1])
                if self.rect.right > window_rect.right:
                    self.rect.right = window_rect.right - self.hover_distance_from_target[
                        0]
                if self.rect.left < window_rect.left:
                    self.rect.left = window_rect.left + self.hover_distance_from_target[
                        0]

            if window_rect.contains(self.rect):
                self.relative_rect = self.rect.copy()
                self.text_block.set_position(self.rect.topleft)
                return True
            else:
                self.relative_rect = self.rect.copy()
                warnings.warn("Unable to fit tool tip on screen")
                return False
        else:
            self.relative_rect = self.rect.copy()
            warnings.warn("initial position for tool tip is off screen,"
                          " unable to find valid position")
            return False

    def rebuild_from_changed_theme_data(self):
        """
        Called by the UIManager to check the theming data and rebuild whatever needs rebuilding for
        this element when the theme data has changed.
        """
        super().rebuild_from_changed_theme_data()
        has_any_changed = False

        if self._check_misc_theme_data_changed(attribute_name='rect_width',
                                               default_value=170,
                                               casting_func=int):
            has_any_changed = True

        if has_any_changed:
            self.rebuild()

    def set_position(self, position: Union[pygame.math.Vector2,
                                           Tuple[int, int], Tuple[float,
                                                                  float]]):
        """
        Sets the absolute screen position of this tool tip, updating it's subordinate text box at
        the same time.

        :param position: The absolute screen position to set.

        """
        super().set_position(position)
        self.text_block.set_position(position)

    def set_relative_position(self, position: Union[pygame.math.Vector2,
                                                    Tuple[int, int],
                                                    Tuple[float, float]]):
        """
        Sets the relative screen position of this tool tip, updating it's subordinate text box at
        the same time.

        :param position: The relative screen position to set.

        """
        super().set_relative_position(position)
        self.text_block.set_relative_position(position)

    def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
                                               Tuple[int, int], Tuple[float,
                                                                      float]]):
        """
        Directly sets the dimensions of this tool tip. This will overwrite the normal theming.

        :param dimensions: The new dimensions to set

        """
        self.rect_width = dimensions[0]

        super().set_dimensions(dimensions)
        self.text_block.set_dimensions(dimensions)