Page 1 of 1

Function for focusing a GUI element

Posted: Mon Dec 12, 2016 1:52 am
by Mooncat
Request: is it possible to have a function to make player focus on a GUI element? E.g. LuaPlayer::focus_gui(guiElement), or LuaGuiElement::focus()

I have implemented the search bar in Creative Mode for searching event names. Basically it is a clone of the vanilla recipe search bar. But without the function to focus on GUI element, I can't fully replicate its functionality.
Now every time I click on the search button, I expect the search textfield is automatically focused (because I have cloned its appearance too perfectly 8-) ), but things go wrong when I try to type something. :|
Search bar

Re: Function for focusing a GUI element

Posted: Fri Apr 14, 2017 2:30 pm
by thenameipicked
+1. I was just about to write this exact suggestion.

Re: Function for focusing a GUI element

Posted: Tue May 30, 2017 9:33 am
by Coppermine
+1. This would be good for What Is It Used For, which also provides a search box.

Re: Function for focusing a GUI element

Posted: Mon Jun 05, 2017 1:26 pm
by quenenni
+1 for me too

For people using the mouse with their left-hand, it's very not nice to not have a focus possibility as I explained here: viewtopic.php?f=28&t=44378#p259711

Re: Function for focusing a GUI element

Posted: Thu Jun 29, 2017 8:56 am
by dgw
This would also be useful for folk's Shuttle Train Lite mod, with the text filter and quick go-to with dot enabled.

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 10:23 am
by luffe
+1, would be great quality of life change for couple mods like what's it really used for

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 2:47 pm
by eradicator
+1
More generally it would be nice if script.raise_event(defines.events.on_gui_click,{...}) actually behaved like a player triggered event. Currently it's weird somehow... (unless i'm doing it horribly wrong).

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 9:51 pm
by Rseding91
eradicator wrote:+1
More generally it would be nice if script.raise_event(defines.events.on_gui_click,{...}) actually behaved like a player triggered event. Currently it's weird somehow... (unless i'm doing it horribly wrong).
It's an identical call unless you setup the parameters wrong.

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 11:01 pm
by eradicator
Rseding91 wrote:
eradicator wrote:+1
More generally it would be nice if script.raise_event(defines.events.on_gui_click,{...}) actually behaved like a player triggered event. Currently it's weird somehow... (unless i'm doing it horribly wrong).
It's an identical call unless you setup the parameters wrong.
The first time i tried this method i was attempting to force-activate a "choose-elem-button" in a my mod. The result was that i could clear the button if it already had something chosen but i couldn't open the dialogue if the button was "empty". I don't have the code for that available right now, but here's something i tried just now for focusuing a text-box. Maybe you would be so kind to point me to what i'm doing wrong?

First create an empty text-box:

Code: Select all

/c game.player.gui.center.add{type='text-box',name='txt'}.style.minimal_width=100
Then attach an event handler to see if it worked?

Code: Select all

/c script.on_event(defines.events.on_gui_click,function(event) game.print(event.element.name..' '..event.player_index) end)
Then i attempt to focus the text box:

Code: Select all

/c script.raise_event(defines.events.on_gui_click,
{name=defines.events.on_gui_click,
tick=game.tick,
element=game.player.gui.center.txt,
player_index=game.player.index,
button=defines.mouse_button_type.left,
alt = false,
control = false,
shift = false,
})
The event handler fires (i.e. prints name+index), but the text-box is not focused (i.e. does not have a cursor) afterwards.

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 11:06 pm
by Rseding91
Events have nothing to do with focusing GUI elements. I'm not sure why you'd think that "gui clicked" event would also focus the GUI element?

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 11:08 pm
by eradicator
Rseding91 wrote:Events have nothing to do with focusing GUI elements. I'm not sure why you'd think that "gui clicked" event would also focus the GUI element?
Because there's no other way in the API i know of to emulate input, which i needed at the time. So i tried the only thing that seemed remotely like it might work :P?

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 11:43 pm
by Rseding91
There is no way to emulate player input in the API because that's not what the API is for. User input is user input and the API is completely independent.

I'm not sure on this original request. There are safety concerns such as making sure a mod doesn't try to focus a GUI element that isn't visible or focus one that shouldn't ever have focus.

Re: Function for focusing a GUI element

Posted: Tue Oct 17, 2017 11:52 pm
by eradicator
Rseding91 wrote:I'm not sure on this original request. There are safety concerns such as making sure a mod doesn't try to focus a GUI element that isn't visible or focus one that shouldn't ever have focus.
Visibility sounds easy to account for, unless you mean things like off-screen elements and not the .visibility property. As for "should never have focus" is that situationaly dependant or specific to certain types of element? I.e. if it's just "frames/flows should never have focus" then a focus method for only a limited subset of element-types would still be useful.

Re: Function for focusing a GUI element

Posted: Wed Oct 18, 2017 12:14 am
by dgw
Rseding91 wrote:I'm not sure on this original request. There are safety concerns such as making sure a mod doesn't try to focus a GUI element that isn't visible or focus one that shouldn't ever have focus.
Look at the HTMLElement.focus() method in JavaScript, for example: "The `HTMLElement.focus()` method sets focus on the specified element, if it can be focused." So, if you try to `focus()` an element that can't receive focus, it does nothing.

Focusing a hidden element can either show it (the parent frame/dialog) or behave as if the element cannot be focused. Either would be safe, as long as the behavior is consistent and documented.

Trying to focus an off-screen element is the stickiest case, but if an element is off screen it's probably either in a dialog or in a part of a (visible) frame that is too large for the Factorio window. Move the dialog (center it?), refuse to focus off-screen elements, whatever—again, as long as the behavior is consistent and documented.

I'm sure there are safety concerns I haven't addressed (my GUI programming experience is quite limited), but I doubt there are any insoluble issues.

Re: Function for focusing a GUI element

Posted: Mon Nov 06, 2017 10:56 pm
by Rseding91
Ok, I've added a focus() function to LuaGuiElement for 0.16.

Re: Function for focusing a GUI element

Posted: Sat Nov 11, 2017 6:12 pm
by npuldon
Thank you!

Re: Function for focusing a GUI element

Posted: Tue Nov 21, 2017 2:56 am
by Mooncat
Rseding91 wrote:Ok, I've added a focus() function to LuaGuiElement for 0.16.
Many thanks. :D