-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdropdown.lua
More file actions
65 lines (53 loc) · 1.81 KB
/
dropdown.lua
File metadata and controls
65 lines (53 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local ennui = require("ennui")
local Dropdown = ennui.Widgets.Dropdown
local StackPanel = ennui.Widgets.Stackpanel
local Text = ennui.Widgets.Text
local Window = ennui.Widgets.Window
local host = ennui.Widgets.Host()
:setSize(love.graphics.getDimensions())
local window = Window("Dropdown Example")
:setSize(350, 300)
:setPosition(100, 100)
local panel = StackPanel()
:setSpacing(15)
:setPadding(10)
:setSize(ennui.Size.fill(), ennui.Size.fill())
local fruitLabel = Text("Select a fruit:"):setColor(1, 1, 1)
local fruitDropdown = Dropdown({
{ label = "Apple", value = "apple" },
{ label = "Banana", value = "banana" },
{ label = "Cherry", value = "cherry" },
{ label = "Dragon Fruit", value = "dragonfruit" },
{ label = "Elderberry", value = "elderberry" },
})
fruitDropdown:setSelectedIndex(1)
local countryLabel = Text("Select a country:"):setColor(1, 1, 1)
local countryDropdown = Dropdown()
:addItem("United States", "US")
:addItem("United Kingdom", "UK")
:addItem("Canada", "CA")
:addItem("Australia", "AU")
:addItem("Germany", "DE")
:addItem("Japan", "JP")
local selectionText = Text("Selected: none")
:setColor(0.7, 0.9, 1)
fruitDropdown:watch("selectedIndex", function(index)
local item = fruitDropdown:getSelectedItem()
if item then
selectionText:setText("Selected fruit: " .. item.label)
end
end)
countryDropdown:watch("selectedIndex", function(index)
local item = countryDropdown:getSelectedItem()
if item then
selectionText:setText("Selected country: " .. item.label .. " (" .. item.value .. ")")
end
end)
panel:addChild(fruitLabel)
panel:addChild(fruitDropdown)
panel:addChild(countryLabel)
panel:addChild(countryDropdown)
panel:addChild(selectionText)
window:setContent(panel)
host:addChild(window)
return host