Skip to content

Commit

Permalink
Fix TextInput selection by mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
anxuae committed Apr 7, 2020
1 parent 00485d7 commit 12f5c17
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
20 changes: 15 additions & 5 deletions pygameMenu/examples/scroll_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@
COLOR_BACKGROUND = (128, 230, 198)


def on_button_click(button_id):
def on_button_click(value=None, text=None):
"""
Button event on menus.
:param button_id: Button ID
"""
print('Hello from button {}'.format(button_id))
if not text:
print('Hello from {}'.format(value))
else:
print('Hello from {} with {}'.format(text, value))


def paint_background(surface):
Expand Down Expand Up @@ -98,10 +101,17 @@ def make_long_menu(surface):
)

_menu.add_button('Rows and Columns', _menu_sub)
label = 'Button n°{}'
label1 = 'Button n°{}'
label2 = 'Text n°{} : '
for i in range(1, 20):
txt = label.format(i)
_menu.add_button(txt, on_button_click, i)
if i % 2 == 0:
_menu.add_button(label1.format(i),
on_button_click,
'button n°{}'.format(i))
else:
_menu.add_text_input(label2.format(i),
onchange=on_button_click,
text='text n°{}'.format(i))
_menu.add_button('Exit', pygameMenu.events.EXIT)

label = 'Button n°{}'
Expand Down
4 changes: 3 additions & 1 deletion pygameMenu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ def _main(self, events=None):

# Check others
else:
for event in events:

for event in events: # type: _pygame.event.EventType

# noinspection PyUnresolvedReferences
if event.type == _pygame.locals.QUIT or (
Expand Down Expand Up @@ -1072,6 +1073,7 @@ def _main(self, events=None):
if event.button in (1, 2, 3) and \
self._actual._scroll.to_real_position(widget.get_rect()).collidepoint(*event.pos):
new_event = _pygame.event.Event(event.type, **event.dict)
new_event.dict['origin'] = self._actual._scroll.get_rect().topleft
new_event.pos = self._actual._scroll.to_world_position(event.pos)
widget.update((new_event,)) # This option can change the current menu to a submenu
break_mainloop = True # It is updated
Expand Down
7 changes: 6 additions & 1 deletion pygameMenu/widgets/textinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def __init__(self,
self._keyrepeat_initial_interval_ms = repeat_keys_initial_ms
self._keyrepeat_interval_ms = repeat_keys_interval_ms
self._last_key = 0 # type: int
self._absolute_origin = (0, 0) # To calculate mouse collide point

# Mouse handling
self._keyrepeat_mouse_ms = 0 # type: int
Expand Down Expand Up @@ -1586,10 +1587,12 @@ def update(self, events):
self._key_is_pressed = False

elif self.mouse_enabled and event.type == _pygame.MOUSEBUTTONUP:
self._absolute_origin = getattr(event, 'origin', self._absolute_origin)
self._selection_active = False
self._check_mouse_collide_input(event.pos)

elif self.mouse_enabled and event.type == _pygame.MOUSEBUTTONDOWN:
self._absolute_origin = getattr(event, 'origin', self._absolute_origin)
if self._selection_active:
self._unselect_text()
self._selection_active = True
Expand All @@ -1606,7 +1609,9 @@ def update(self, events):
if self._keyrepeat_mouse_ms > self._keyrepeat_mouse_interval_ms:
self._keyrepeat_mouse_ms = 0
if mouse_left:
self._check_mouse_collide_input(_pygame.mouse.get_pos())
pos = _pygame.mouse.get_pos()
self._check_mouse_collide_input((pos[0] - self._absolute_origin[0],
pos[1] - self._absolute_origin[1]))

# Update key counters:
for key in self._keyrepeat_counters:
Expand Down

0 comments on commit 12f5c17

Please sign in to comment.