MyreMylar / pygame_gui

A GUI system for pygame.
MIT License
649 stars 78 forks source link

UITextEntryBox desyncs in an edge case #606

Open hedesandxlii opened 2 weeks ago

hedesandxlii commented 2 weeks ago

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
MyreMylar commented 2 weeks ago

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 commented 1 week ago

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!