Skip to content

Commit 6e3a983

Browse files
committed
Add possibility for element to span accross multiple columns
1 parent 76833f5 commit 6e3a983

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/pygamepopup/components/box_element.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ class BoxElement:
2020
In fact, it adds a margin to any border of an element to have a cleaner interface.
2121
2222
Keyword arguments:
23-
position (Position): the position of the box on the screen
24-
content (Optional[pygame.Surface]): a surface that will be wrapped by the box
23+
position (Position): the position of the box on the screen.
24+
content (Optional[pygame.Surface]): a surface that will be wrapped by the box.
2525
margin (Margin): a tuple containing the margins of the box, should be in the form
26-
"(top_margin, right_margin, bottom_margin, left_margin), defaults to (0, 0, 0, 0)"
26+
"(top_margin, right_margin, bottom_margin, left_margin), defaults to (0, 0, 0, 0)".
27+
column_span (int): the number of columns the element should span, defaults to 1.
2728
2829
Attributes:
29-
position (Position): the position of the box on the screen
30-
content (Optional[pygame.Surface]): the element wrapped in the box
30+
position (Position): the position of the box on the screen.
31+
content (Optional[pygame.Surface]): the element wrapped in the box.
3132
size (tuple[int, int]): the size of the content following the format "(width, height)"
32-
margin (dict[str, int]): a dict containing all the values for margins TOP, BOTTOM, LEFT and RIGHT
33+
margin (dict[str, int]): a dict containing all the values for margins TOP, BOTTOM, LEFT and RIGHT.
34+
column_span (int): the number of columns the element should span.
3335
"""
3436

3537
def __new__(cls, *args, **kwargs):
@@ -45,6 +47,7 @@ def __init__(
4547
position: Position,
4648
content: Optional[pygame.Surface],
4749
margin: Margin = (0, 0, 0, 0),
50+
column_span: int = 1,
4851
) -> None:
4952
self.position: Position = position
5053
self.content: pygame.Surface = content
@@ -57,6 +60,7 @@ def __init__(
5760
"LEFT": margin[3],
5861
"RIGHT": margin[1],
5962
}
63+
self.column_span = column_span
6064

6165
def get_width(self) -> int:
6266
"""

src/pygamepopup/components/info_box.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727

2828
class _Row:
2929
def __init__(self, elements: list[BoxElement], height: int = 0):
30-
self.elements = elements
30+
self.elements: list[BoxElement] = elements
3131
self.height = height
3232

33+
def compute_number_columns(self):
34+
return sum(element.column_span for element in self.elements)
35+
3336

3437
class InfoBox:
3538
"""
@@ -286,18 +289,18 @@ def determine_elements_position(self) -> None:
286289
mouse_pos = pygame.mouse.get_pos()
287290
# A row begins by a value identifying its height, followed by its elements
288291
for row in self.__elements:
289-
nb_elements = len(row.elements)
290-
i = 1
292+
number_columns = row.compute_number_columns()
293+
column = 1
291294
for element in row.elements:
292-
base_x = self.position.x + (self.__size[0] // (2 * nb_elements)) * i
295+
base_x = self.position.x + (self.__size[0] // (2 * number_columns)) * (column + element.column_span - 1)
293296
x_coordinate = base_x - element.get_width() // 2
294297
element.position = pygame.Vector2(
295298
x_coordinate,
296299
y_coordinate + element.get_margin_top(),
297300
)
298301
if isinstance(element, Button):
299302
element.set_hover(element.get_rect().collidepoint(mouse_pos))
300-
i += 2
303+
column += 2 * element.column_span
301304
y_coordinate += row.height
302305

303306
def display(self, screen: pygame.Surface) -> None:

src/pygamepopup/components/text_element.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TextElement(BoxElement):
2626
margin (Margin): a tuple containing the margins of the box,
2727
should be in the form "(top_margin, right_margin, bottom_margin, left_margin)", defaults to (0, 0, 0, 0).
2828
text_color (pygame.Color): the color of the rendered text, defaults to WHITE.
29+
column_span (int): the number of columns the element should span, defaults to 1.
2930
"""
3031

3132
def __init__(
@@ -35,14 +36,15 @@ def __init__(
3536
font: pygame.font.Font = None,
3637
margin: Margin = (0, 0, 0, 0),
3738
text_color: pygame.Color = WHITE,
39+
column_span: int = 1,
3840
) -> None:
3941
if not font:
4042
font = _default_fonts["text_element_content"]
4143
self._font = font
4244
self._text = text
4345
self._text_color = text_color
4446
rendered_text: pygame.Surface = font.render(text, True, text_color)
45-
super().__init__(position, rendered_text, margin)
47+
super().__init__(position, rendered_text, margin, column_span)
4648

4749
def _verify_rendered_text_size(
4850
self, rendered_text: pygame.Surface, text: str, container_width: int

0 commit comments

Comments
 (0)