Currently Online

Latest Posts

Topic: UI Spinbox: dynamic max value?

kaputtnik
Avatar
Topic Opener
Joined: 2013-02-18, 19:48
Posts: 2481
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: 2024-05-20, 16:52

I am trying to adjust the max value of a spinbox depending on the amount of a ware. But for some reason this won't do. Code snippet:

         {
            widget = "box",
            name = "ware_options",
            orientation = "horizontal",
            spacing     = 10,
            children = {
               {
                  widget = "spinbox",
                  name   = "nr_wares",
                  w      = 120,
                  unit_w = 40,
                  value  = 1,
                  min    = 1,
                  max    = 5,
                  label  = "Amount",
               },

But for some reason i can't change this value. Code snippet to change:

      local water = 12
      print(wl.ui.MapView():get_child("nr_wares"), wl.ui.MapView():get_child("nr_wares").max, water)
      wl.ui.MapView():get_child("nr_wares").max = water

The print statement says:

SpinBox: 0x5784544364f0 nil     12

So the Spinbox is found, but the max property is nil. The documentation shows also no other function to change this: https://www.widelands.org/documentation/autogen_wl_ui/index.html#spinbox

Any hints?


Fight simulator for Widelands:
https://wide-fighter.netlify.app/

Top Quote
Nordfriese
Avatar
Joined: 2017-01-17, 17:07
Posts: 2000
OS: Debian Testing
Version: Latest master
Ranking
One Elder of Players
Location: 0x55555d3a34c0
Posted at: 2024-05-20, 19:15

The documentation you linked doesn't mention any property named max, because it does not exist face-wink.png

Use the method set_interval to change the maximum, e.g. wl.ui.MapView():get_child("nr_wares"):set_interval(1, water).

Rationale for the choice of which properties are available and which are not is what is immediately available in the C++ interface of each component, and which would involve writing a dedicated new getter and/or setter function.


Top Quote
kaputtnik
Avatar
Topic Opener
Joined: 2013-02-18, 19:48
Posts: 2481
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: 2024-05-20, 21:26

Oh yes... i just misinterpreted the header set_interval an didn't read the explanation.

Many thanks!


Fight simulator for Widelands:
https://wide-fighter.netlify.app/

Top Quote
kaputtnik
Avatar
Topic Opener
Joined: 2013-02-18, 19:48
Posts: 2481
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: 2024-06-12, 18:13

I have now trouble to dynamically change the values of a spinbox when using values. Assigning a new table to values seem to have no effect:

Initializing:

         {
            widget = "spinbox",
            name   = "spinb_nr_wares",
            w      = 140,
            unit_w = 65,
            values  = {10,20,30,40},
            value  = 0,
            -- min    = 1,
            -- max    = 5,
            label  = "Amount",
            on_changed = [[ include("map:scripting/callbacks.lua") explain() ]],
         },

Try to change:

wl.ui.MapView():get_child("spinb_nr_wares").values = {2, 4, 6, 8}

print(wl.ui.MapView():get_child("spinb_nr_wares").values)        -- this prints "nil", i would expect it prints "table"

Did i overlooked something again?

If this is not possible with a spinbox, can i use a discrete_slider for this?


Fight simulator for Widelands:
https://wide-fighter.netlify.app/

Top Quote
Nordfriese
Avatar
Joined: 2017-01-17, 17:07
Posts: 2000
OS: Debian Testing
Version: Latest master
Ranking
One Elder of Players
Location: 0x55555d3a34c0
Posted at: 2024-06-12, 18:54

Well the documentation is a comprehensive list of all known properties and values face-smile.png

Value list spinboxes are somewhat fiddly. Internally, a value list with N entries is implemented as a regular spinbox in interval 0 to (N-1) with a remapping for value i to the i-th array item. Changing that after creation is difficult to say the least.

I would suggest that if you want to use a spinbox, you can imitate that behaviour in Lua using Replacements: Initially create the spinbox as a regular spinner with interval 1 to 1, and then update your values for a size-N array by setting the interval 1 .. N and calling add_replacement(i, names[i]) for all i from 1 to N to ensure the i-th value is displayed as names[i]. And when reading out the value, don't forget that the spinbox selection represents an index into an array defined by you.

Alternatively consider using a dropdown, this has one of the most flexible interfaces of all widgets.


        on_changed = [[ include("map:scripting/callbacks.lua") explain() ]],

Lua syntax alert: This needs a semicolon or newline between the two function calls.


Top Quote
kaputtnik
Avatar
Topic Opener
Joined: 2013-02-18, 19:48
Posts: 2481
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: 2024-06-13, 15:24

Nordfriese wrote:

Well the documentation is a comprehensive list of all known properties and values face-smile.png

The problem for a beginner is that some properties can be changed just by assigning a new value to the ui-element, and for some values one has to use the appropriate functions mentioned in the detailed description of the ui element to change a value. Probably the descriptions of ui elements below create_child() should get links to the detailed descriptions, e.g. spinbox in chapter create_child() should get a link to SpinBox.

Anyway, thanks for your answer again face-smile.png


Fight simulator for Widelands:
https://wide-fighter.netlify.app/

Top Quote