Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to horiz text alignment #653

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
(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 @@
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 @@
(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 @@
# 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 @@
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
Loading