Page 1 of 1

LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sat Jul 13, 2019 7:01 pm
by raiguard
Hello. I am attempting to create an interface with a slider and number field, similar to many vanilla settings GUIs. In order to emulate exactly how input sanitation works in vanilla, I need to be able to tell when the user "defocuses" the textbox.

There is currently no way to read the focused state of a GUI element, so I would like to request that that capability be added. And in order to avoid the necessity of having an on_tick() event to keep track of the focused state, some sort of on_gui_focused_state_changed event would be nice.

Thanks in advance!

- raiguard
2019-07-13 13_00_44-Factorio 0.17.56.png
2019-07-13 13_00_44-Factorio 0.17.56.png (45.58 KiB) Viewed 1757 times

Re: LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sat Jul 13, 2019 8:02 pm
by Klonan
You can implement it using the on_gui_text_changed event,
I have done the same in a mod of my own:


Re: LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sat Jul 13, 2019 8:27 pm
by raiguard
Sorry, that's not what I meant. I have already done that as well. What I need this capability for is to emulate vanilla's sanity checking:



Vanilla doesn't actually edit the user's input while they're typing, but will correct it when they defocus the textfield. This is the behavior I am trying to emulate, which is impossible without what I requested.

The best that is possible now is to sanitize as the user types, but that can be very annoying since it won't let you completely replace the number very easily.

As an aside, it would be nice if pressing enter would defocus the textfield, like what happens with vanilla textfields.

Re: LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sun Jul 14, 2019 1:23 pm
by Therenas
+ 1 to this (and the unfocus on enter)

You can kind of catch the unfocus by listening to on_gui_click, and checking if the clicked element is not the one that should be reset, and run your code from there. It it not really reliable though, because not everything that defocuses fires on_gui_clicked.

Re: LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sun Jul 14, 2019 1:56 pm
by Rseding91
You're asking for the wrong thing. You should have just asked for support to do exactly what the base game is doing in that text field.

Re: LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sun Jul 14, 2019 3:19 pm
by Klonan
Rseding91 wrote: Sun Jul 14, 2019 1:56 pm You're asking for the wrong thing. You should have just asked for support to do exactly what the base game is doing in that text field.
Classic XY Problem
https://en.wikipedia.org/wiki/XY_problem

Re: LuaGuiElement:focused [R/W] / on_gui_focused_state_changed event

Posted: Sun Jul 14, 2019 4:59 pm
by raiguard
Rseding91 wrote: Sun Jul 14, 2019 1:56 pm You're asking for the wrong thing. You should have just asked for support to do exactly what the base game is doing in that text field.
Well I was trying to generalize it. I figured that something more general that could have lots of usecases would be more likely to make it in, than something very specific.