airstruck / luigi

Lovely User Interfaces for Game Inventors
MIT License
114 stars 21 forks source link

How to add stepper values dynamically from the lua code? #59

Open iddm opened 7 years ago

iddm commented 7 years ago

How to add stepper values dynamically from the lua code? I have looked over example code and googled anything related to this but with not success.

For example, I want to add those two values dynamically rather than specifying them immediately during layout declaration:

{ id = "current_mode", type = "stepper",
    { value = 1, text = "Free for all" },
    { value = 2, text = "Team match" },
},
airstruck commented 7 years ago

Those "step" widgets live in the stepper's items field. Something like this should work:

table.insert(yourStepper.items, { value = 1, text = "Free for all", type = "stepper.item" })

The type = "stepper.item" happens automatically when you define the "steps" during layout declaration. It's a look-and-feel thing, not really important.

iddm commented 7 years ago

@airstruck How should I write the stepper in the layout then?

            { id = "current_mode", type = "stepper" },

This does not work:

Error: luigi/widget/stepper.lua:71: attempt to index local 'item' (a nil value)
stack traceback:
    luigi/widget/stepper.lua:71: in function 'updateValue'
    luigi/widget/stepper.lua:105: in function 'decorate'
    luigi/attribute.lua:57: in function 'set'
    luigi/widget.lua:173: in function '__newindex'
    luigi/widget.lua:228: in function 'metaCall'
    luigi/widget.lua:232: in function 'metaCall'
    luigi/widget.lua:232: in function 'metaCall'
    luigi/widget.lua:232: in function 'Widget'
    luigi/layout.lua:57: in function 'constructor'
    luigi/base.lua:7: in function 'layout'
airstruck commented 7 years ago

Looks like stepper expects to have at least one item. Can probably work around it by adding a dummy item in layout. Fix should be easy, can bail out of the function where the error happened if there are no items.

iddm commented 7 years ago

@airstruck I have tried this exactly but this does not work normally. This dummy item is always shown and can not be rewritten. For example, If I add it the luigi's layout runs fine but then I must change the .items property instead of inserting values into it, but then the dummy value is always visibile in the stepper until I press stepper buttons. I have tried to find something like redraw in the Widget class but had no success. Could you try to implement such a stepper for me please?

airstruck commented 7 years ago

I'll take a look at it tomorrow. Meanwhile you could try adding something like if not item then return end right before the line where that error happens (luigi/widget/stepper.lua:71).