Page 1 of 1

LuaGuiElement 'textfield' with value range

Posted: Mon Jul 09, 2018 5:08 pm
by eradicator
What?
The ability to specify a valid range of numerical values that may be entered into a textfield.

Whaat?
Mod settings can specify that a text field can only contain i.e. an integer beween 1 and 100, or a decimal between 0.1 and 0.5, and the text field will refuse any other input and show a helpful tooltip about the allowed value ranges. I'd like that ability for modded GUIs too. The LuaGuiElement could supply information about validity via LuaGuiElement.is_value_in_range [R] or similar.
valuerange.png
valuerange.png (88.46 KiB) Viewed 1659 times

Re: LuaGuiElement 'textfield' with value range

Posted: Mon Jul 09, 2018 9:04 pm
by Rseding91
FYI: you can already do this all script side. Watch for the text changed event and set the style of the text field if it's invalid + set the tooltip. It's exactly what the game does now for the mod setting text fields.

Re: LuaGuiElement 'textfield' with value range

Posted: Mon Jul 09, 2018 10:22 pm
by eradicator
Rseding91 wrote:FYI: you can already do this all script side. Watch for the text changed event and set the style of the text field if it's invalid + set the tooltip. It's exactly what the game does now for the mod setting text fields.
I know. But it would be a lot easier, especially for inexperienced modders, if there was a built-in way to do this. And as you already do it for mod-settings i was hoping it would be simple enough to expose the function to the API.

Re: LuaGuiElement 'textfield' with value range

Posted: Tue Jul 10, 2018 12:00 pm
by Rseding91
eradicator wrote:
Rseding91 wrote:FYI: you can already do this all script side. Watch for the text changed event and set the style of the text field if it's invalid + set the tooltip. It's exactly what the game does now for the mod setting text fields.
I know. But it would be a lot easier, especially for inexperienced modders, if there was a built-in way to do this. And as you already do it for mod-settings i was hoping it would be simple enough to expose the function to the API.
As with most things modding: it's not that simple.

It's not a simple function/property on the text box. The way it works in mod settings is the min/max is stored on the mod setting prototype which always exists in memory for the lifetime of the game and then the wrapper widget that shows the specific text-field mod setting listens to the text-changed event on the text widget and if it parses to a value below or above the prototype value then it changes the style and sets the tooltip.

That means in order to support it for custom-text-box the custom-text-box would have to store the min/max at all times in memory, save them to disk, listen to the change event and set the tooltip accordingly.

Re: LuaGuiElement 'textfield' with value range

Posted: Tue Jul 10, 2018 12:39 pm
by eradicator
Rseding91 wrote:That means in order to support it for custom-text-box the custom-text-box would have to store the min/max at all times in memory, save them to disk, listen to the change event and set the tooltip accordingly.
Are you implying that it would need to store/parse this for all text boxes regardless of if they use the feature? ... that would indeed be a no-go *sigh*.

If i have to implement that on the lua side i'd do it like this:

Code: Select all

local A = GUI.add{type='textbox'}
global.watchlist[A] = {start = range_start, end = range_end, is_in_range = false}
add_range_tooltip(A)

script.on_event(on_gui_text_changed,function(event)
  if watchlist[event.element] then
    if is_in_range(event.element) then
      event.element.style = default_style
      watchlist[event.element].is_in_range = true
    else
       event.element.style = out_of_range_style
       watchlist[event.element].is_in_range = false
    end
  end)

And when the player presses "ok" on that particular page, cross-reference all the elements with watchlist again. Hm, using the GuiElement as a table key makes it look easier than i initially thought. Still i can't shake the "reinvented the wheel" feeling.

Any further thoughts? (Or implementation tips.)

Re: LuaGuiElement 'textfield' with value range

Posted: Tue Jul 10, 2018 12:42 pm
by Rseding91
eradicator wrote:Are you implying that it would need to store/parse this for all text boxes regardless of if they use the feature? ...
Yes.