Esempio n. 1
0
    def __init__(
            self,
            area_width: int,
            area_height: int,
            area_color: Optional[Union[ColorInputType, 'pygame_menu.BaseImage']] = None,
            extend_x: int = 0,
            extend_y: int = 0,
            menubar: Optional['pygame_menu.widgets.MenuBar'] = None,
            parent_scrollarea: Optional['ScrollArea'] = None,
            scrollarea_id: str = '',
            scrollbar_color: ColorInputType = (235, 235, 235),
            scrollbar_cursor: CursorInputType = None,
            scrollbar_slider_color: ColorInputType = (200, 200, 200),
            scrollbar_slider_hover_color: ColorInputType = (180, 180, 180),
            scrollbar_slider_pad: NumberType = 0,
            scrollbar_thick: int = 20,
            scrollbars: StringVector = DEFAULT_SCROLLBARS,
            shadow: bool = False,
            shadow_color: ColorInputType = (0, 0, 0),
            shadow_offset: int = 2,
            shadow_position: str = POSITION_SOUTHEAST,
            world: Optional['pygame.Surface'] = None
    ) -> None:
        super(ScrollArea, self).__init__(object_id=scrollarea_id)

        assert isinstance(area_height, int)
        assert isinstance(area_width, int)
        assert isinstance(extend_x, int)
        assert isinstance(extend_y, int)
        assert isinstance(scrollbar_slider_pad, NumberInstance)
        assert isinstance(scrollbar_thick, int)
        assert isinstance(shadow, bool)
        assert isinstance(shadow_offset, int)
        assert isinstance(world, (pygame.Surface, type(None)))

        if area_color is not None and not isinstance(area_color, pygame_menu.BaseImage):
            area_color = assert_color(area_color)

        scrollbar_color = assert_color(scrollbar_color)
        scrollbar_slider_color = assert_color(scrollbar_slider_color)
        shadow_color = assert_color(shadow_color)

        assert_position(shadow_position)

        assert area_width > 0 and area_height > 0, \
            'area size must be greater than zero'

        assert isinstance(scrollbars, (str, VectorInstance))
        unique_scrolls = []
        if isinstance(scrollbars, str):
            unique_scrolls.append(scrollbars)
        else:
            for s in scrollbars:
                if s not in unique_scrolls:
                    unique_scrolls.append(s)

        self._area_color = area_color
        self._bg_surface = None
        self._bg_surface = None
        self._decorator = Decorator(self)
        self._rect = pygame.Rect(0, 0, int(area_width), int(area_height))
        self._scrollbar_positions = tuple(unique_scrolls)  # Ensure unique
        self._scrollbar_thick = scrollbar_thick
        self._scrollbars = []
        self._translate = (0, 0)
        self._world = world

        self._extend_x = extend_x
        self._extend_y = extend_y
        self._menubar = menubar

        self.set_parent_scrollarea(parent_scrollarea)
        self._view_rect = self.get_view_rect()

        for pos in self._scrollbar_positions:
            assert_position(pos)

            if pos == POSITION_EAST or pos == POSITION_WEST:
                sbar = ScrollBar(
                    length=self._view_rect.height,
                    onchange=self._on_vertical_scroll,
                    orientation=ORIENTATION_VERTICAL,
                    page_ctrl_color=scrollbar_color,
                    page_ctrl_thick=scrollbar_thick,
                    slider_color=scrollbar_slider_color,
                    slider_hover_color=scrollbar_slider_hover_color,
                    slider_pad=scrollbar_slider_pad,
                    values_range=(0, max(1, self.get_hidden_height()))
                )
            else:
                sbar = ScrollBar(
                    length=self._view_rect.width,
                    onchange=self._on_horizontal_scroll,
                    page_ctrl_color=scrollbar_color,
                    page_ctrl_thick=scrollbar_thick,
                    slider_color=scrollbar_slider_color,
                    slider_hover_color=scrollbar_slider_hover_color,
                    slider_pad=scrollbar_slider_pad,
                    values_range=(0, max(1, self.get_hidden_width()))
                )
            sbar.set_shadow(
                enabled=shadow,
                color=shadow_color,
                position=shadow_position,
                offset=shadow_offset
            )
            sbar.set_controls(joystick=False)
            sbar.set_cursor(cursor=scrollbar_cursor)
            sbar.set_scrollarea(self)
            sbar.configured = True
            sbar.hide()

            self._scrollbars.append(sbar)

        self._apply_size_changes()

        # Menu reference
        self._menu = None
Esempio n. 2
0
    def create_rect(self, width: int, height: int) -> None:
        """
        Create rect object.

        :param width: Area width
        :param height: Area height
        """
        assert isinstance(width, int)
        assert isinstance(height, int)
        self._rect = pygame.Rect(0, 0, int(width), int(height))
        self._scrollbars = []
        self._view_rect = self.get_view_rect()

        # Unpack properties
        (scrollbar_color, scrollbar_thick, scrollbar_slider_color,
         scrollbar_slider_hover_color, scrollbar_slider_pad, scrollbar_cursor,
         shadow, shadow_color, shadow_position, shadow_offset,
         controls_joystick, controls_mouse, controls_touchscreen,
         controls_keyboard) = self._scrollbars_props

        for pos in self._scrollbar_positions:
            assert_position(pos)

            if pos == POSITION_EAST or pos == POSITION_WEST:
                sbar = ScrollBar(
                    length=self._view_rect.height,
                    onchange=self._on_vertical_scroll,
                    orientation=ORIENTATION_VERTICAL,
                    page_ctrl_color=scrollbar_color,
                    page_ctrl_thick=scrollbar_thick,
                    slider_color=scrollbar_slider_color,
                    slider_hover_color=scrollbar_slider_hover_color,
                    slider_pad=scrollbar_slider_pad,
                    values_range=(0, max(1, self.get_hidden_height())))

            else:
                sbar = ScrollBar(
                    length=self._view_rect.width,
                    onchange=self._on_horizontal_scroll,
                    page_ctrl_color=scrollbar_color,
                    page_ctrl_thick=scrollbar_thick,
                    slider_color=scrollbar_slider_color,
                    slider_hover_color=scrollbar_slider_hover_color,
                    slider_pad=scrollbar_slider_pad,
                    values_range=(0, max(1, self.get_hidden_width())))

            sbar.set_shadow(enabled=shadow,
                            color=shadow_color,
                            position=shadow_position,
                            offset=shadow_offset)
            sbar.set_controls(joystick=controls_joystick,
                              mouse=controls_mouse,
                              touchscreen=controls_touchscreen,
                              keyboard=controls_keyboard)
            sbar.set_cursor(cursor=scrollbar_cursor)
            sbar.set_scrollarea(self)
            sbar.configured = True
            sbar.hide()

            self._scrollbars.append(sbar)

        self._apply_size_changes()