Function for focusing a GUI element

Post Reply
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Function for focusing a GUI element

Post 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

thenameipicked
Burner Inserter
Burner Inserter
Posts: 13
Joined: Wed Mar 15, 2017 12:36 am
Contact:

Re: Function for focusing a GUI element

Post by thenameipicked »

+1. I was just about to write this exact suggestion.

Coppermine
Long Handed Inserter
Long Handed Inserter
Posts: 79
Joined: Sat May 06, 2017 11:25 am
Contact:

Re: Function for focusing a GUI element

Post by Coppermine »

+1. This would be good for What Is It Used For, which also provides a search box.

quenenni
Inserter
Inserter
Posts: 21
Joined: Fri Apr 14, 2017 2:16 am
Contact:

Re: Function for focusing a GUI element

Post 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

dgw
Fast Inserter
Fast Inserter
Posts: 197
Joined: Tue Apr 12, 2016 7:06 pm
Contact:

Re: Function for focusing a GUI element

Post 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.

luffe
Burner Inserter
Burner Inserter
Posts: 15
Joined: Fri Dec 18, 2015 5:29 pm
Contact:

Re: Function for focusing a GUI element

Post by luffe »

+1, would be great quality of life change for couple mods like what's it really used for

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Function for focusing a GUI element

Post 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).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13173
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Function for focusing a GUI element

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Function for focusing a GUI element

Post 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.
Last edited by eradicator on Tue Oct 17, 2017 11:06 pm, edited 2 times in total.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13173
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Function for focusing a GUI element

Post 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?
If you want to get ahold of me I'm almost always on Discord.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Function for focusing a GUI element

Post 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?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13173
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Function for focusing a GUI element

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Function for focusing a GUI element

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

dgw
Fast Inserter
Fast Inserter
Posts: 197
Joined: Tue Apr 12, 2016 7:06 pm
Contact:

Re: Function for focusing a GUI element

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13173
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Function for focusing a GUI element

Post by Rseding91 »

Ok, I've added a focus() function to LuaGuiElement for 0.16.
If you want to get ahold of me I'm almost always on Discord.

npuldon
Fast Inserter
Fast Inserter
Posts: 159
Joined: Thu Dec 08, 2016 8:36 am
Contact:

Re: Function for focusing a GUI element

Post by npuldon »

Thank you!

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Function for focusing a GUI element

Post by Mooncat »

Rseding91 wrote:Ok, I've added a focus() function to LuaGuiElement for 0.16.
Many thanks. :D

Post Reply

Return to “Implemented mod requests”