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

Improve the text box entry keyboard controls #528

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion pygame_gui/core/text/text_box_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self,
self._add_row_to_layout(current_row, True)

self.edit_buffer = 2
self.cursor_text_row = None
self.cursor_text_row: Optional[TextBoxLayoutRow] = None
self.last_horiz_cursor_row_pos = 0

self.selection_colour = pygame.Color(128, 128, 200, 255)
Expand Down Expand Up @@ -585,6 +585,29 @@ def vert_align_bottom_all_rows(self, y_padding):
row.vert_align_items_to_row()
new_y -= row.height

def set_cursor_to_end_of_current_row(self):
"""
Set the edit cursor position in the text layout to the end of the current row and returns the overall
position in the text

:return: the overall position of the cursor in the text layout, after setting it to the end of the current row
"""
if self.cursor_text_row is not None:
is_last_row = self.cursor_text_row == self.layout_rows[-1]
self.cursor_text_row.set_cursor_to_end(is_last_row)
return self.get_cursor_index()

def set_cursor_to_start_of_current_row(self):
"""
Set the edit cursor position in the text layout to the end of the current row and returns the overall
position in the text

:return: the overall position of the cursor in the text layout, after setting it to the end of the current row
"""
if self.cursor_text_row is not None:
self.cursor_text_row.set_cursor_to_start()
return self.get_cursor_index()

def set_cursor_position(self, cursor_pos):
"""
Set the edit cursor position in the text layout.
Expand Down
17 changes: 17 additions & 0 deletions pygame_gui/core/text/text_box_layout_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,23 @@

self._setup_offset_position_from_edit_cursor()

def set_cursor_to_end(self, is_last_row):
end_pos = self.letter_count
# we need to ignore the trailing space on line-wrapped rows,
# or we will spill over to the row below
if not is_last_row and len(self.items) > 0:
last_chunk = self.items[-1]
if (isinstance(last_chunk, TextLineChunkFTFont) and
(len(last_chunk.text) > 0 and last_chunk.text[-1] == " ")):
end_pos -= 1

Check warning on line 481 in pygame_gui/core/text/text_box_layout_row.py

View check run for this annotation

Codecov / codecov/patch

pygame_gui/core/text/text_box_layout_row.py#L481

Added line #L481 was not covered by tests
if isinstance(last_chunk, LineBreakLayoutRect):
end_pos -= 1
end_pos = max(0, end_pos)
self.set_cursor_position(end_pos)

def set_cursor_to_start(self):
self.set_cursor_position(0)

def get_cursor_index(self) -> int:
"""
Get the current character index of the cursor
Expand Down
22 changes: 15 additions & 7 deletions pygame_gui/core/ui_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,19 @@ def _update_absolute_rect_position_from_anchors(self, recalculate_margins=False)

self.rect.left = new_left
self.rect.top = new_top
new_height = new_bottom - new_top
new_width = new_right - new_left
if self.dynamic_height:
new_height = new_bottom - new_top
else:
new_height = max(0, new_bottom - new_top)

if self.dynamic_width:
new_width = new_right - new_left
else:
new_width = max(0, new_right - new_left)

new_width, new_height = self._get_clamped_to_minimum_dimensions((new_width, new_height))
if (new_height != self.relative_rect.height) or (new_width != self.relative_rect.width):
self._set_dimensions((new_width, new_height))
self.set_dimensions((new_width, new_height))

def _update_relative_rect_position_from_anchors(self, recalculate_margins=False):
"""
Expand Down Expand Up @@ -764,7 +772,7 @@ def set_minimum_dimensions(self, dimensions: Union[pygame.math.Vector2,
(self.rect.height < self.minimum_dimensions[1])):
new_width = max(self.minimum_dimensions[0], self.rect.width)
new_height = max(self.minimum_dimensions[1], self.rect.height)
self._set_dimensions((new_width, new_height))
self.set_dimensions((new_width, new_height))

def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
Tuple[int, int],
Expand Down Expand Up @@ -800,9 +808,9 @@ def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
self._set_dimensions(dimensions, clamp_to_container)

def _set_dimensions(self, dimensions: Union[pygame.math.Vector2,
Tuple[int, int],
Tuple[float, float]],
clamp_to_container: bool = False):
Tuple[int, int],
Tuple[float, float]],
clamp_to_container: bool = False):
"""
Method to directly set the dimensions of an element.
Dimensions must be positive values.
Expand Down
7 changes: 7 additions & 0 deletions pygame_gui/data/default_theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@
"rect_width": "170"
}
},
"file_dialog.button.tool_tip":
{
"misc":
{
"rect_width": "-1"
}
},
"vertical_scroll_bar":
{
"prototype": "#default_shape_style"
Expand Down
2 changes: 1 addition & 1 deletion pygame_gui/elements/ui_text_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def parse_html_into_style_data(self):
text_direction=self.parser.default_style['direction'])
self.text_box_layout.set_cursor_colour(self.text_cursor_colour)
self.parser.empty_layout_queue()
if not self.dynamic_height:
if self.dynamic_height:
self.text_box_layout.view_rect.height = self.text_box_layout.layout_rect.height

self._align_all_text_rows()
Expand Down
Loading
Loading