cschreib / lxgui

Portable, real time, modular and data-driven GUI C++ library.
https://cschreib.github.io/lxgui
MIT License
75 stars 20 forks source link

allow for focus to leave EditBox #64

Closed wgahnagl closed 3 years ago

wgahnagl commented 3 years ago

Hello again! So I've hit my first issue with the library! It looks like writing text in the textbox is working fine, but you're unable to leave it once you've clicked on it! I'm not sure if that can be solved by writing lua scripts to work around it, or if it needs to be fixed in the library itself, but either way I'll take a look :ok_hand:

It would probably be good to be able to set a global "textbox focus" type thing that locks out all other keypresses while you're interacting with a textbox, and unsets the lockout when you click anywhere else.

cschreib commented 3 years ago

Hello and thanks for your feedback! I think what you request is already possible :)

  1. Look at the edit box demo (bottom of the file). https://github.com/cschreib/lxgui/blob/8de440b6ecb5663e5ca7c2b3fd6f3baa8639780f/gui/bin/interface/scroll_test/templates.xml#L124-L126 You can hook to the OnEscapePressed event handler and call EditBox:clear_focus. This allows the focus to be cleared when pressing the escape key. You can also call clear_focus in other scripts (such as when pressing a "Submit" button associated to the edit box). Does that cover all your needs?
  2. The C++ API lets you query if there's something in the interface that currently has keyboard focus. Look at line 77 in the example for SFML. https://github.com/cschreib/lxgui/blob/8de440b6ecb5663e5ca7c2b3fd6f3baa8639780f/gui/examples/sfml/main.cpp#L73-L88 You should check for this before processing key events for your own game logic. At the moment there is no builtin logic to "unfocus" by clicking anywhere else, however. I'll have a think about this.

Note that is_keyboard_focus() is obviously only handling cases where the GUI swallows keyboard events. For mouse events, e.g., for cases where there's a GUI element under the mouse and you don't want click events to be registered in your game world, you should use https://github.com/cschreib/lxgui/blob/8de440b6ecb5663e5ca7c2b3fd6f3baa8639780f/gui/examples/sfml/main.cpp#L94-L98

cschreib commented 3 years ago

I've pushed changes on master so that clicking outside of an edit box removes its focus. Let me know if that does the trick for you.

wgahnagl commented 3 years ago

thank you so much! I'll take a look soon! :ok_hand: :fire:

cschreib commented 3 years ago

I haven't been able to reproduce the problem since this fix has been merged. Have you had a chance to look at it and confirm it on your end? Just so I can close this ticket.

cschreib commented 3 years ago

I'll take silence for approval :)