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

Fix cursor not "skipping" the space characters at the end of line properly. #636

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions pygame_gui/core/text/text_box_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,10 +1452,11 @@ def get_cursor_pos_move_up_one_row(self, last_cursor_horiz_index):
row_above = self.layout_rows[i - 1]
cursor_index -= row_above.letter_count
row_above_end = row_above.letter_count
if (row_above.row_text_ends_with_a_single_space() or
(len(row_above.items) > 0 and
isinstance(row_above.items[-1], LineBreakLayoutRect))):
row_above_end = row_above.letter_count - 1
space_count = row_above.row_text_count_final_whitespace()
if space_count:
row_above_end -= space_count
if len(row_above.items) > 0 and isinstance(row_above.items[-1], LineBreakLayoutRect):
row_above_end -= 1
cursor_index += min(last_cursor_horiz_index, row_above_end)
break
else:
Expand All @@ -1481,10 +1482,11 @@ def get_cursor_pos_move_down_one_row(self, last_cursor_horiz_index):
row_below = self.layout_rows[i + 1]
cursor_index += row.letter_count
row_below_end = row_below.letter_count
if (row_below.row_text_ends_with_a_single_space() or
(len(row_below.items) > 0 and
isinstance(row_below.items[-1], LineBreakLayoutRect))):
row_below_end = row_below.letter_count - 1
space_count = row_below.row_text_count_final_whitespace()
if space_count:
row_below_end -= space_count
if len(row_below.items) > 0 and isinstance(row_below.items[-1], LineBreakLayoutRect):
row_below_end -= 1
cursor_index += min(last_cursor_horiz_index, row_below_end)
break
else:
Expand Down
37 changes: 18 additions & 19 deletions pygame_gui/core/text/text_box_layout_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,32 @@ def find_cursor_pos_from_click_pos(self, click_pos: Tuple[int, int], num_rows: i
else:
cursor_draw_width += chunk.font.size(chunk.text)[0]
letter_acc += chunk.letter_count

if (not found_chunk and scrolled_click_pos[0] >= self.right) or (letter_acc == self.letter_count):
# if we have more than two rows check if we are on right of whole row and if row has space at the end.
# If so stick the edit cursor before the space because this is how it works.
if num_rows > 1 and self.row_text_ends_with_a_single_space():
letter_acc -= 1
space_count = self.row_text_count_final_whitespace()
if num_rows > 1 and self.row_text_count_final_whitespace():
letter_acc -= space_count
last_chunk = self.get_last_text_chunk()
if last_chunk is not None:
cursor_draw_width -= last_chunk.font.size(" ")[0]

cursor_draw_width -= last_chunk.font.size(" ")[0] * space_count
cursor_index = min(self.letter_count, max(0, letter_acc))
return cursor_index, cursor_draw_width

def row_text_count_final_whitespace(self):
count = 0
for item in reversed(self.items):
if isinstance(item, TextLineChunkFTFont):
cnt = 0
for l in reversed(item.text):
if l != " ":
return count + cnt
cnt += 1
count += cnt
return count

def get_last_text_chunk(self):
for item in reversed(self.items):
if isinstance(item, TextLineChunkFTFont):
Expand Down Expand Up @@ -535,21 +549,6 @@ def insert_linebreak_after_chunk(self, chunk_to_insert_after: Union[TextLineChun
empty_text_chunk = parser.create_styled_text_chunk('')
self.items.insert(chunk_insert_index + 1, empty_text_chunk)

@staticmethod
def string_ends_with_a_single_space(string_to_check: str) -> bool:
if string_to_check[-1] == " " and (
len(string_to_check) == 1 or string_to_check[-2] != " "
):
return True
return False

def row_text_ends_with_a_single_space(self):
for item in reversed(self.items):
if isinstance(item, TextLineChunkFTFont):
if len(item.text) > 0 and TextBoxLayoutRow.string_ends_with_a_single_space(item.text):
return True
return False

def get_last_text_or_line_break_chunk(self):
for item in reversed(self.items):
if isinstance(item, TextLineChunkFTFont) or isinstance(item, LineBreakLayoutRect):
Expand Down
Loading