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

UITextEntryBox desyncs in an edge case #606

Open
hedesandxlii opened this issue Jun 20, 2024 · 3 comments
Open

UITextEntryBox desyncs in an edge case #606

hedesandxlii opened this issue Jun 20, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@hedesandxlii
Copy link

Describe the bug
Stumbled upon this while getting to know this lovely library (thank you for that!).

The behavior I observed was that sometimes when entering a newline and a character directly after it, the character would propagate to the widget's internal state, but not the widget. In the widget, selection is moved down a line instead.

To Reproduce
I've construct a small example of this behavior:

import pygame
import pygame_gui

SIZE = (400, 200)
EXAMPLE_TEXT = """
while True:
    print("hello")
"""


pygame.init()

window_surface = pygame.display.set_mode(SIZE)

manager = pygame_gui.UIManager(SIZE)

text_entry_box = pygame_gui.elements.UITextEntryBox(
    initial_text=EXAMPLE_TEXT,
    relative_rect=pygame.Rect(0, 0, *SIZE),
)

clock = pygame.time.Clock()
is_running = True

while is_running:
    dt = clock.tick(60) / 1000

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False

        if event.type == pygame_gui.UI_TEXT_ENTRY_CHANGED:
            print(text_entry_box.get_text())

        manager.process_events(event)

    manager.update(dt)
    manager.draw_ui(window_surface)
    pygame.display.update()

Steps to reproduce the behaviour:

  1. Put the cursor on the first line (the empty one before while True:)
  2. Press Enter
  3. Press a character, like j

Expected behaviour
I expect the typed character to be visible in the widget, so there is no desync between state and widget state.

Platform and software (please complete the following information):
OS: Linux, Ubuntu 22.04.4 LTS

pip freeze:

pygame-ce==2.5.0
pygame_gui==0.6.12
python-i18n==0.3.9
@hedesandxlii hedesandxlii added the bug Something isn't working label Jun 20, 2024
@MyreMylar
Copy link
Owner

I can confirm I can reproduce this, but it seems to happen only sporadically.

Might take me a while to pin down why this is happening.

@hedesandxlii
Copy link
Author

Ah, it's a pity it's reproducible ^^

I'd love to help out if I can get some pointers on where to poke around!

@rbaltrusch
Copy link
Contributor

The following code reproducibly produces the buggy behaviour with just a newline as initial text:

import pygame
import pygame_gui

pygame.init()
screen = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

manager = pygame_gui.UIManager((400, 200))
text_entry_box = pygame_gui.elements.UITextEntryBox(
    initial_text="\n",
    relative_rect=pygame.Rect(0, 0, 400, 200),
)
text_entry_box.focus()

# simulating keypress
manager.process_events(pygame.Event(pygame.KEYDOWN, {'key': pygame.K_RETURN, 'mod': 0}))
manager.process_events(pygame.Event(pygame.TEXTINPUT, {'text': "a"}))

is_running = True
while is_running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False
        manager.process_events(event)
    dt = clock.tick(60) / 1000
    manager.update(dt)
    manager.draw_ui(screen)
    pygame.display.update()

I have noticed the buggy behaviour seems to happen when inserting more than one blank line and jumping back to insert something to one of the blank lines in the middle.

Working on a fix right now but I don't have it fully worked out yet. I found out that UITextEntryBox.html_text attribute does hold the correct data, and that a call to the UITextEntryBox.rebuild() method resurfaces the lost characters. The behaviour seems to be a bug related to the interaction between TextBoxLayoutRow objects filled with just a newline and subsequent insertion of a text chunk, at which point we lose a character (or more?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants