Skip to content

Commit

Permalink
Fixes to horiz text alignment
Browse files Browse the repository at this point in the history
fixes #617
  • Loading branch information
MyreMylar committed Nov 3, 2024
1 parent cfe3699 commit 69093e5
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pygame_gui/core/drawable_shapes/drawable_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ def _evaluate_contents_for_containing_rect(self):
text_width = self.text_box_layout.layout_rect.width

horiz_padding = 0
if 'text_horiz_alignment_padding' in self.theming:
horiz_padding = self.theming['text_horiz_alignment_padding']
if 'text_horiz_alignment' in self.theming:
if self.theming['text_horiz_alignment'] in ['left', 'right']:
if 'text_horiz_alignment_padding' in self.theming:
horiz_padding = self.theming['text_horiz_alignment_padding']

Check warning on line 238 in pygame_gui/core/drawable_shapes/drawable_shape.py

View check run for this annotation

Codecov / codecov/patch

pygame_gui/core/drawable_shapes/drawable_shape.py#L237-L238

Added lines #L237 - L238 were not covered by tests

# As well as the text width we want to throw in the borders,
# shadows and any text padding
Expand All @@ -242,7 +244,7 @@ def _evaluate_contents_for_containing_rect(self):
(2 * self.border_width) +
self.rounded_corner_width_offsets[0] +
self.rounded_corner_width_offsets[1] +
(2 * horiz_padding))
horiz_padding)

self.text_view_rect.width = text_width
self.text_box_layout.view_rect.width = self.text_view_rect.width
Expand All @@ -252,8 +254,10 @@ def _evaluate_contents_for_containing_rect(self):
text_height = self.text_box_layout.layout_rect.height

vert_padding = 0
if 'text_vert_alignment_padding' in self.theming:
vert_padding = self.theming['text_vert_alignment_padding']
if 'text_horiz_alignment' in self.theming:
if self.theming['text_horiz_alignment'] in ['top', 'bottom']:
if 'text_vert_alignment_padding' in self.theming:
vert_padding = self.theming['text_vert_alignment_padding']

Check warning on line 260 in pygame_gui/core/drawable_shapes/drawable_shape.py

View check run for this annotation

Codecov / codecov/patch

pygame_gui/core/drawable_shapes/drawable_shape.py#L259-L260

Added lines #L259 - L260 were not covered by tests

# As well as the text height we want to throw in the borders,
# shadows and any text padding
Expand All @@ -262,7 +266,7 @@ def _evaluate_contents_for_containing_rect(self):
(2 * self.border_width) +
self.rounded_corner_height_offsets[0] +
self.rounded_corner_height_offsets[1] +
(2 * vert_padding))
vert_padding)
self.text_view_rect.height = text_height
self.text_box_layout.view_rect.height = self.text_view_rect.height
self.containing_rect.height = final_height
Expand Down Expand Up @@ -501,12 +505,16 @@ def build_text_layout(self):
# than the total text area.

horiz_padding = 0
if 'text_horiz_alignment_padding' in self.theming:
horiz_padding = self.theming['text_horiz_alignment_padding']
if 'text_horiz_alignment' in self.theming:
if self.theming['text_horiz_alignment'] in ['left', 'right']:
if 'text_horiz_alignment_padding' in self.theming:
horiz_padding = self.theming['text_horiz_alignment_padding']

vert_padding = 0
if 'text_vert_alignment_padding' in self.theming:
vert_padding = self.theming['text_vert_alignment_padding']
if 'text_vert_alignment' in self.theming:
if self.theming['text_vert_alignment'] in ['top', 'bottom']:
if 'text_vert_alignment_padding' in self.theming:
vert_padding = self.theming['text_vert_alignment_padding']

total_text_buffer = ((self.shadow_width * 2) +
(self.border_width * 2) +
Expand All @@ -519,19 +527,25 @@ def build_text_layout(self):
self.text_view_rect.width = -1
else:
self.text_view_rect.width = max(0, self.text_view_rect.width -
(total_text_buffer + (2 * horiz_padding)))
(total_text_buffer + horiz_padding))

if self.dynamic_height:
self.text_view_rect.height = -1
else:
self.text_view_rect.height = max(0, self.text_view_rect.height -
(total_text_buffer + (2 * vert_padding)))
(total_text_buffer + vert_padding))

text_actual_area_rect = self.text_view_rect.copy()
text_actual_area_rect.x = (self.shadow_width + self.border_width +
self.rounded_corner_width_offsets[0] + horiz_padding)
self.rounded_corner_width_offsets[0])
if 'text_horiz_alignment' in self.theming:
if self.theming['text_horiz_alignment'] in ['left']:
text_actual_area_rect.x += horiz_padding
text_actual_area_rect.y = (self.shadow_width + self.border_width +
self.rounded_corner_height_offsets[0] + vert_padding)
self.rounded_corner_height_offsets[0])
if 'text_vert_alignment' in self.theming:
if self.theming['text_vert_alignment'] in ['top']:
text_actual_area_rect.y += vert_padding

text_shadow_data = (0, 0, 0, pygame.Color('#10101070'), False)
if 'text_shadow' in self.theming:
Expand Down

0 comments on commit 69093e5

Please sign in to comment.