Skip to content

Commit f821575

Browse files
committed
Allow one directonal scrolling
Allows the users to choose which scrollbars can be created
1 parent a8401e3 commit f821575

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

pygame_gui/elements/ui_auto_scrolling_container.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def __init__(self,
4040
anchors: Optional[Dict[str, Union[str, UIElement]]] = None,
4141
visible: int = 1):
4242
super().__init__(relative_rect, manager,
43+
allow_scroll_x=allow_scroll_x,
44+
allow_scroll_y=allow_scroll_y,
4345
starting_height=starting_height,
4446
container=container,
4547
parent_element=parent_element,
@@ -52,26 +54,16 @@ def __init__(self,
5254
object_id=object_id,
5355
element_id="auto_scrolling_container")
5456

55-
self.allow_scroll_x = allow_scroll_x
56-
self.allow_scroll_y = allow_scroll_y
57-
58-
anchors = {"left": "left",
59-
"right": "left",
60-
"top": "top",
61-
"bottom": "top"}
62-
6357
resize_left: bool = True
6458
resize_right: bool = True
6559
resize_top: bool = True
6660
resize_bottom: bool = True
6761

6862
if not self.allow_scroll_x:
69-
anchors["right"] = "right"
7063
resize_left = False
7164
resize_right = False
7265

7366
if not self.allow_scroll_y:
74-
anchors["bottom"] = "bottom"
7567
resize_top = False
7668
resize_bottom = False
7769

@@ -89,9 +81,10 @@ def __init__(self,
8981
object_id=ObjectID(
9082
object_id="#scrollable_container",
9183
class_id=None),
92-
anchors=anchors)
93-
94-
self.scrollable_container_dimensions = scrollable_rect.size
84+
anchors={"left": "left",
85+
"right": "left",
86+
"top": "top",
87+
"bottom": "top"})
9588

9689
def update(self, time_delta: float):
9790
"""
@@ -103,9 +96,6 @@ def update(self, time_delta: float):
10396
"""
10497
super().update(time_delta)
10598

106-
new_dimensions = self.scrollable_container.rect.size
107-
108-
if self.scrollable_container_dimensions != new_dimensions:
109-
self.scrollable_container_dimensions = new_dimensions
99+
if self.scrollable_container.has_recently_updated_dimensions:
110100
self._calculate_scrolling_dimensions()
111101
self._sort_out_element_container_scroll_bars()

pygame_gui/elements/ui_scrolling_container.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class UIScrollingContainer(UIElement, IContainerLikeInterface):
3434
def __init__(self,
3535
relative_rect: pygame.Rect,
3636
manager: Optional[IUIManagerInterface] = None,
37+
allow_scroll_x: bool = True,
38+
allow_scroll_y: bool = True,
3739
*,
3840
starting_height: int = 1,
3941
container: Optional[IContainerLikeInterface] = None,
@@ -65,6 +67,9 @@ def __init__(self,
6567
self.vert_scroll_bar = None # type: Union[UIVerticalScrollBar, None]
6668
self.horiz_scroll_bar = None # type: Union[UIHorizontalScrollBar, None]
6769

70+
self.allow_scroll_x = allow_scroll_x
71+
self.allow_scroll_y = allow_scroll_y
72+
6873
self._set_image(self.ui_manager.get_universal_empty_surface())
6974

7075
# this contains the scroll bars and the 'view' container
@@ -362,11 +367,11 @@ def _check_scroll_bars_and_adjust(self):
362367
need_horiz_scroll_bar = False
363368
need_vert_scroll_bar = False
364369
if (self.scrolling_height > self._view_container.rect.height or
365-
self.scrollable_container.relative_rect.top != 0):
370+
self.scrollable_container.relative_rect.top != 0) and self.allow_scroll_y:
366371
need_vert_scroll_bar = True
367372
self.scroll_bar_width = 20
368373
if (self.scrolling_width > self._view_container.rect.width or
369-
self.scrollable_container.relative_rect.left != 0):
374+
self.scrollable_container.relative_rect.left != 0) and self.allow_scroll_x:
370375
need_horiz_scroll_bar = True
371376
self.scroll_bar_height = 20
372377
if need_vert_scroll_bar or need_horiz_scroll_bar:

0 commit comments

Comments
 (0)