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

Try to fix text entry new line bug #646

Merged
merged 2 commits into from
Nov 3, 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
4 changes: 2 additions & 2 deletions pygame_gui/core/text/text_box_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,8 +1339,8 @@ def _find_row_from_text_box_index(self, text_box_index: int):
row_index = bisect_left(self.row_lengths, text_box_index)
if row_index >= len(self.layout_rows):
row_index = len(self.layout_rows) - 1
index_in_row = text_box_index - (self.row_lengths[row_index - 1]
if row_index != 0 else 0)
index_in_row = min(text_box_index - (self.row_lengths[row_index - 1]
if row_index != 0 else 0), self.row_lengths[row_index])
return self.layout_rows[row_index], index_in_row

return None, 0
Expand Down
63 changes: 57 additions & 6 deletions pygame_gui/core/text/text_box_layout_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

def __init__(self, row_start_x, row_start_y, row_index, line_spacing, layout):
super().__init__(row_start_x, row_start_y, 0, 0)
self.row_start_x = row_start_x
self.line_spacing = line_spacing
self.row_index = row_index
self.layout = layout
Expand Down Expand Up @@ -44,6 +45,9 @@
def __hash__(self):
return self.row_index

def __repr__(self):
return "[" + str([item for item in self.items]) + "]"

Check warning on line 49 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#L49

Added line #L49 was not covered by tests

def at_start(self):
"""
Returns true if this row has no items in it.
Expand Down Expand Up @@ -91,6 +95,46 @@

self.letter_count += item.letter_count

def insert_new_item_at_start(self, new_item: TextLayoutRect):
"""
Insert a new item to the row at the start

:param new_item: The new item to add to the text row
"""
# rejig existing items in row to accommodate the new one
new_rhs = self.row_start_x + new_item.width
for old_item in self.items:
old_item.left = new_rhs
new_rhs = old_item.right

Check warning on line 108 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#L107-L108

Added lines #L107 - L108 were not covered by tests
# add the new item at the beginning
self.items.insert(0, new_item)
self.width += new_item.width # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused

if new_item.row_chunk_height > self.text_chunk_height:
self.text_chunk_height = new_item.row_chunk_height
if not self.layout.dynamic_height:
self.height = min(self.layout.layout_rect.height, # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused
new_item.row_chunk_height)
self.line_spacing_height = min(self.layout.layout_rect.height, # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused
int(round(new_item.row_chunk_height * self.line_spacing)))
else:
self.height = int(item.row_chunk_height) # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused
self.line_spacing_height = int(round(item.row_chunk_height * self.line_spacing)) # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused

Check warning on line 122 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#L121-L122

Added lines #L121 - L122 were not covered by tests
self.cursor_rect = pygame.Rect(self.x, self.y, self.layout.edit_cursor_width, self.height - 2)

if isinstance(new_item, TextLineChunkFTFont):
if new_item.y_origin > self.y_origin:
self.y_origin = new_item.y_origin

for origin_item in self.items:
if isinstance(origin_item, TextLineChunkFTFont):
origin_item.origin_row_y_adjust = self.y_origin - origin_item.y_origin
origin_item.top = origin_item.pre_row_rect.top + origin_item.origin_row_y_adjust

self.fall_back_font = new_item.font

self.letter_count += new_item.letter_count

def rewind_row(self, layout_rect_queue):
"""
Use this to add items from the row back onto a layout queue, useful if we've added
Expand Down Expand Up @@ -503,21 +547,28 @@
:param parser: An optional HTML parser for text styling data
"""
letter_acc = 0
inserted_text = False
if len(self.items) > 0:
for chunk in self.items:
if isinstance(chunk, TextLineChunkFTFont):
if letter_row_index <= letter_acc + chunk.letter_count:
if letter_row_index <= letter_acc + chunk.letter_count:
if isinstance(chunk, TextLineChunkFTFont):
chunk_index = letter_row_index - letter_acc
chunk.insert_text(text, chunk_index)
inserted_text = True
break

letter_acc += chunk.letter_count
elif parser is not None:
text_chunk = parser.create_styled_text_chunk(text)
self.add_item(text_chunk)
else:
if parser is None and not len(self.items):
raise AttributeError("Trying to insert into empty text row with no Parser"
" for style data - fix this later?")
if not inserted_text and letter_row_index == 0:
# have not managed to insert text into existing text chunk, create a new one.
text_chunk = parser.create_styled_text_chunk(text)
self.insert_new_item_at_start(text_chunk)
inserted_text = True

if not inserted_text:
raise RuntimeError("Failed to insert text at index: %d", letter_row_index)

Check warning on line 571 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#L571

Added line #L571 was not covered by tests

def insert_linebreak_after_chunk(self, chunk_to_insert_after: Union[TextLineChunkFTFont, LineBreakLayoutRect], parser: HTMLParser):
if len(self.items) > 0:
Expand Down
3 changes: 3 additions & 0 deletions pygame_gui/core/text/text_line_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
def __repr__(self):
return "< '" + self.text + "' " + super().__repr__() + " >"

def __str__(self):
return "< '" + self.text + "' " + super().__str__() + " >"

Check warning on line 114 in pygame_gui/core/text/text_line_chunk.py

View check run for this annotation

Codecov / codecov/patch

pygame_gui/core/text/text_line_chunk.py#L114

Added line #L114 was not covered by tests

def _clamp_dimensions_to_maximums(self, text_width, text_height):
if self.max_dimensions is not None:
if not (self.max_dimensions[0] == -1 or self.max_dimensions[1] == -1):
Expand Down
Loading