airstruck / luigi

Lovely User Interfaces for Game Inventors
MIT License
113 stars 23 forks source link

Combobox widget #49

Closed linux-man closed 7 years ago

linux-man commented 7 years ago

What's the better way for emulate a combobox? The only thing that occurs to me is to use a menu->menu.items to select an option, and change menu text accordingly.

airstruck commented 7 years ago

I intended to add a dedicated dropdown widget at some point, but never got around to it. What you're describing should work, I think. You can put any kind of widget in a menu, not just a menu item, so you should be able to get a text box in there if you need a combobox and not just a dropdown. I don't know how well it's actually going to work; I haven't tried to do it yet. Let me know how it goes. ;)

linux-man commented 7 years ago

Next question: create a dynamic submenu.

    local f = layout:createWidget({type = 'menu.item', text = 'Options'})
    layout.rootMenu:addChild(f)

creates a menu item, but I can't figure how to create sub-items. I was thinking of populating an empty parent menu.

airstruck commented 7 years ago

Dynamic menu items would probably need a special API, there's some funky stuff going on behind the scenes there. For now you should be able to rebuild the menu each time it's opened something like this:

local Layout = require 'luigi.layout'

local menu = { isContextMenu = true }

menu[#menu + 1] = { text = 'thing one' }
menu[#menu + 1] = { text = 'thing two' }

yourMainLayout:createWidget { type = 'menu', menu }
menu.menuLayout:show() -- or :placeNear
linux-man commented 7 years ago

This code works.

layout.button:onPress(function (event)
    local menu = { isContextMenu = true }
    for n = 1, 4 do
        menu[#menu + 1] = {text = 'thing '..n}
    end
    layout:createWidget({type = 'menu', menu })
    menu.menuLayout:onPress(function (event) layout.button.text = event.target.text end)
    menu.menuLayout:placeNear(layout.button:getX(), layout.button:getY() + layout.button:getHeight())
    menu.menuLayout:show()  
end)
linux-man commented 7 years ago

I finally finished my dialog boxes. Can you take a look at https://github.com/linux-man/lovefs ? The only "bug" I found was that you have to click OVER the text on the dropdown menus, otherwise the event.target.text is empty.

airstruck commented 7 years ago

I think the "bug" is caused by the event target not being the menu item itself, but a child of it (some children of menu items are automatically created for icons, keyboard shortcut labels, maybe a spacer). I had planned to introduce some kind of "no-target" widget attribute to make this more intuitive, but for now you might have to walk up the target's ancestor tree until you find the widget you're looking for, something like this.

airstruck commented 7 years ago

Here's something else you might be interested in: https://love2d.org/forums/viewtopic.php?f=4&t=82442