Unfocus a textbox?

Place to get help with not working mods / modding interface.
Post Reply
boran_blok
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Fri Mar 01, 2019 7:56 am
Contact:

Unfocus a textbox?

Post by boran_blok »

Hi, I am modifying shuttle train lite a bit to fix some of the annoyances I have with it.

One of these is that when you press . to go to the top station or select a station from the list, the textbox remains focused, this prevents me from exiting the train when I arrive (by pressing enter) as the textbox still has focus.

Is there a way to unfocus this textbox so regular control inputs go back to the game, and not this textbox?

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

Re: Unfocus a textbox?

Post by eradicator »

Dirty hack: close the gui, then open it up again?

LuaPlayer.opened = nil
LuaPlayer.opened = whatever
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.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Unfocus a textbox?

Post by eduran »

Cycle the visibility or enabled property of the text box. You can do it on the same tick, so it won't actually be invisible or disabled.

Code: Select all

my_textbox.visible = false
my_textbox.visible = true

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

Re: Unfocus a textbox?

Post by eradicator »

If it's a modded gui simply focusing another element (like a frame) might work too. LuaGuiElement.focus(). But i have to agree that double-toggling the same element is a nicer hack.

Code: Select all

local function element_unfocus(element)
  local enabled = element.enabled
  element.enabled = not enabled
  element.enabled = enabled
  end
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.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Unfocus a textbox?

Post by eduran »

eradicator wrote:
Tue Jun 04, 2019 11:17 am
If it's a modded gui simply focusing another element (like a frame) might work too. LuaGuiElement.focus(). But i have to agree that double-toggling the same element is a nicer hack.
Afaik you can't focus elements that don't allow interaction. And elements allowing interaction will react to pressing enter. If you want to see messed up modded guis, click a button and see what happens when you press enter.

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

Re: Unfocus a textbox?

Post by eradicator »

eduran wrote:
Tue Jun 04, 2019 12:12 pm
eradicator wrote:
Tue Jun 04, 2019 11:17 am
If it's a modded gui simply focusing another element (like a frame) might work too. LuaGuiElement.focus(). But i have to agree that double-toggling the same element is a nicer hack.
Afaik you can't focus elements that don't allow interaction. And elements allowing interaction will react to pressing enter. If you want to see messed up modded guis, click a button and see what happens when you press enter.
I just tested, and focus()'ing a frame unfocuses the textbox as expected. And that the frame doesn't get focus itself is also expected, after all the question was about unfocussing stuff.

Code: Select all

/sudo clear() x = center.add{type='frame'} y = x.add{type='text-box'}
/sudo script.on_event(defines.events.on_gui_text_changed,function(e) x.focus() end)
I'm curious..*what* happens if you press enter? The event should be the same.
Last edited by eradicator on Tue Jun 04, 2019 1:13 pm, edited 1 time 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.

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

Re: Unfocus a textbox?

Post by eradicator »

eduran wrote:
Tue Jun 04, 2019 12:12 pm
Afaik you can't focus elements that don't allow interaction. And elements allowing interaction will react to pressing enter. If you want to see messed up modded guis, click a button and see what happens when you press enter.
My test button doesn't react to enter at all, nor can it be focus()'ed or otherwise interacted with non-mouse input. My testing on_gui_click handler is only raised for mouse clicks. What do you mean?

Edit:
Further testing...so if i press "Enter" (and not "Return" because that has a keybinding...) then the button toggles from pressed to depressed. That looks more like an engine bug. i'm not aware of eny event for this.
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.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Unfocus a textbox?

Post by eduran »

Sorry, I was not clear at all about what I meant. The problem is purely visual. You can get buttons to show their 'selected_graphical_set'. That's just a remnant from vanilla GUI elements and not meant to be a thing for modded UIs. Confusing when buttons suddenly look perma-clicked. If you create your own button styles and do not define a selected_graphical_set, buttons can suddenly default back to their original shape. So when someone pressed enter while one of the tabs in LTNT was focused, it would turn back into a square shaped button. Similar issue with the small triangle table header buttons.

At that point the buttons still function, but it does not look pretty. And I agree, it is probably a bug. I guess I should report it :D

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

Re: Unfocus a textbox?

Post by eradicator »

eduran wrote:
Tue Jun 04, 2019 4:30 pm
At that point the buttons still function, but it does not look pretty. And I agree, it is probably a bug. I guess I should report it :D
You found it, so you get the honor of reporting it. And the honor of being linked in the changelog ;).
(And yea...i doubt i have all styles defined for things that can't be legally pressed.)
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.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Unfocus a textbox?

Post by eduran »

viewtopic.php?f=7&t=71556

And good thing you pointed out that "Return" does not work when bound. I changed my keybinding for "Enter Vehicle" to something else and never noticed (and actually meant the "Return" key when I wrote "Enter"). Would have been confusing if I had reported it without that information.

Also: sorry for derailing this thread :oops:

boran_blok
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Fri Mar 01, 2019 7:56 am
Contact:

Re: Unfocus a textbox?

Post by boran_blok »

no problem, I will try your change and report back if it works.

After that I'll release my version, but it is not that useful anymore, since .17 temporary train stops made the whole shuttle thing kind of redundant imho. Still, I am stuck on .16 for at least another 200 hours I'd wager.

boran_blok
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Fri Mar 01, 2019 7:56 am
Contact:

Re: Unfocus a textbox?

Post by boran_blok »

Well,

enable/disable logic causes a hard crash in Factorio,

lua code:

Code: Select all

local function unfocusFilter(p, frame)
	if frame.one and frame.one["shuttle-lite-filter"] then
		local enabled = frame.one["shuttle-lite-filter"].enabled
		frame.one["shuttle-lite-filter"].enabled = not enabled
		frame.one["shuttle-lite-filter"].enabled = enabled
	end
end
(made it specific rather than generic, as I already had a sample of clearFilter that works)

crash:

Code: Select all

 115.715 Error CrashHandler.cpp:373: Exception Code: e06d7363, Address: 0x00007ffe6e3ea388
ModuleBase: 0x00007ff798750000, ImageSize: 013ee000, RelativeAddress: d5c9a388
 115.715 Error CrashHandler.cpp:393: Exception Context:
rax=000000000000001c, rbx=0000000000056247, rcx=0000000000000000,
rdx=0000000000000000, rsi=00007ff7999d0e98, rdi=0000000000056220,
rip=00007ffe6e3ea388, rsp=0000000850ef87a0, rbp=0000000850ef88e0,
 r8=0000000000000000,  r9=0000000000000000, r10=0000000000000000,
r11=0000000000000000, r12=0000000850efa500, r13=0000010b4bf8e6f0,
r14=0000000850ef8920, r15=0000000000000000
 115.715 Crashed in C:\WINDOWS\System32\KERNELBASE.dll (0x00007ffe6e3b0000 - 0x00007ffe6e623000)
Factorio crashed. Generating symbolized stacktrace, please wait ...
ERROR: LoadModule, GetLastError: 3765269347 (Address: 0000000000000000)
ERROR: SymGetSymFromAddr64, GetLastError: 126 (Address: 00007FF7993CFCAC)
ERROR: SymGetLineFromAddr64, GetLastError: 126 (Address: 00007FF7993CFCAC)
ERROR: SymGetModuleInfo64, GetLastError: 1114 (Address: 00007FF7993CFCAC)
00007FF7993CFCAC ((module-name not available)): (filename not available): (function-name not available)
Stack trace logging done
 116.046 Include minidump in crash report.
 116.046 Error CrashHandler.cpp:174: Map tick at moment of crash: 87821337
 116.046 Error Util.cpp:67: Unexpected error occurred. If you're running the latest version of the game you can help us solve the problem by posting the contents of the log file on the Factorio forums.
Please also include the save file(s), any mods you may be using, and any steps you know of to reproduce the crash.
 137.159 Uploading log file
 137.207 Error CrashHandler.cpp:227: Heap validation: success.
 137.209 Creating crash dump.
 137.480 CrashDump success
 137.484 Error CrashHandler.cpp:330: Unhandled exception type: .?AVbad_alloc@std@@
 137.485 Error CrashHandler.cpp:339: Unhandled exception: bad allocation
but this code works fine:

Code: Select all

local function unfocusFilter(p, frame)
	frame.focus()
end
since its a single line now I moved it to the function inline as well, with some comments.

Will do some testing and see how I can get it on the mod portal, someone else still on .16 might find it useful.

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

Re: Unfocus a textbox?

Post by eradicator »

eduran wrote:
Tue Jun 04, 2019 6:34 pm
Also: sorry for derailing this thread :oops:
Derailing is in the nature of any train related thread :p.
boran_blok wrote:
Wed Jun 05, 2019 7:00 am
enable/disable logic causes a hard crash in Factorio,
Lulz. It *does* work fine in 0.17. Not sure if wube does bugfix backports.
boran_blok wrote:
Wed Jun 05, 2019 7:00 am
Will do some testing and see how I can get it on the mod portal, someone else still on .16 might find it useful.
The portal doesn't care about which factorio version you upload for. I stil have an old mod for stable there too, which has now been replaced by vanilla functionality.
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.

boran_blok
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Fri Mar 01, 2019 7:56 am
Contact:

Re: Unfocus a textbox?

Post by boran_blok »

Well, I posted my version here

but I cannot figure out to make a decent version for the mod portal, I have to give it a new name, fine I thought, just add, e to the end and modify data.lua to refer to new resource locations. But in game the entire UI fails to load, I seem to be missing something there.

but now I am derailing my own thread.

boran_blok
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Fri Mar 01, 2019 7:56 am
Contact:

Re: Unfocus a textbox?

Post by boran_blok »

ok, back on topic again, it seems I placed the cart before the horse.

I was a tad too focused on my solution I didn't see other issues with my implementation.

Right now I have the issue with my changes that if I enter the train I do need to click elsewhere than the textbox if I want to leave the train without going to a destination, as the filter textbox intercepts the return key.

Is there any way to ignore the enter/return key on a gui element, so this still works for leaving a train?

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

Re: Unfocus a textbox?

Post by eradicator »

For upgrading the mod it should be sufficient to change the name of your new mod in info.json, and declare it incompatible to the old mod. No code changes should be needed if you want it to work with old savegames of the old mod.
boran_blok wrote:
Wed Jun 05, 2019 8:51 am
Is there any way to ignore the enter/return key on a gui element, so this still works for leaving a train?
If a textbox (or textfield) gui element has focus it'll steal all input. I'm not aware of any way around it.
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.

boran_blok
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Fri Mar 01, 2019 7:56 am
Contact:

Re: Unfocus a textbox?

Post by boran_blok »

that does not seem to be the case, I need to modify the mod folder as well to be the same name as the mod, if I dont do this factorio complains about the folder name within the mod zit. And then all references to that mod folder in data.lua have to be changed as well.

So far so good, but even when I do this nothing shows up in the UI.

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

Re: Unfocus a textbox?

Post by eradicator »

If you use absolue path then yes, the mod name has to be reflected. That would affect any prototype graphics. I forgot about that because i have the modname in a variable so it's only in one place for me :x.
The folder *inside* the zip could be named whatever i wanted the last time i checked. Only the zip itself had to obey the "mod-name_1.2.3" scheme (or the folder if you're running in un-zipped mode). As you're still on 0.16 that should still work.

If *nothing* shows up in the gui then it's highly likely that your info.json has an error (too many commas, not enough spaces, etc). In 0.17 factorio-current.log will mention this, not sure about 0.16. Any non-info.json errors should produce a proper error message.
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.

Post Reply

Return to “Modding help”