-
Notifications
You must be signed in to change notification settings - Fork 354
/
dropdown.lua
104 lines (88 loc) · 4.35 KB
/
dropdown.lua
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
--[[ Copyright (c) 2013 Manuel "Roujin" Wolf
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
--! Dropdown "window" used for selection of one item from a list.
class "UIDropdown" (UIResizable)
---@type UIDropdown
local UIDropdown = _G["UIDropdown"]
--! Constructor for the dropdown "window"
--!param ui (UI) The ui the window is created in
--!param parent_window (Window) The window that this dropdown will be attached to
--!param parent_button (Button) The button in the parent_window that this dropdown will be positioned under
--!param items (table) A list of items for the list to display, where each item is a table with at least
-- the field text, and optionally fields font and/or tooltip, which is a table containing text, x and y positions.
--!param callback (function) A function to be called when an item is selected. It is called with two parameters:
-- The parent window and the index of the selected item
--!param colour (table) A colour in the form of {red = ..., green = ..., blue = ...}. Optional if parent_window is a UIResizable
function UIDropdown:UIDropdown(ui, parent_window, parent_button, items, callback, colour)
local col = colour or parent_window.colour
self:UIResizable(ui, 1, 1, col, true, true)
self.modal_class = "dropdown"
self.esc_closes = true
self.resizable = false
self.default_button_sound = "selectx.wav"
self.parent_window = parent_window
self.parent_button = parent_button
self.items = items
self.callback = callback
local panel = parent_button.panel_for_sprite
local width = panel.w
local height = panel.h
self:setPosition(panel.x, panel.y + panel.h)
local y = 0
for i, item in ipairs(items) do
if item.tooltip and item.tooltip[1] then
self:addBevelPanel(1, y + 1, width - 2, height - 2, parent_window.colour):setLabel(item.text, item.font)
:makeToggleButton(-1, -1, width, height, nil, --[[persistable:dropdown_tooltip_callback]] function() self:selectItem(i) end)
:enable(not item.disabled)
:setTooltip(item.tooltip[1], item.tooltip[2] or math.floor(self.ui.app.config.width / 2 - 25),
math.floor(self.ui.app.config.height / 4 - 130 + item.tooltip[3]) or 0)
-- Magic numbers used to find a static position across different screen resolutions.
else
self:addBevelPanel(1, y + 1, width - 2, height - 2, parent_window.colour):setLabel(item.text, item.font)
:makeButton(-1, -1, width, height, nil, --[[persistable:dropdown_callback]] function() self:selectItem(i) end)
end
y = y + height
end
-- Adjust size
self:setSize(width, y)
end
function UIDropdown:selectItem(number)
UIResizable.close(self)
self.parent_button:setLabel(self.items[number].text, self.items[number].font):setToggleState(false)
if self.callback then
self.callback(self.parent_window, number)
end
end
function UIDropdown:beginDrag(x, y)
-- TODO: It may be undesirable anyway for dropdowns, but for dragging of sub-windows
-- offsets are wrongly calculated (results in jump when dragging)
-- Disable dragging
return false
end
--! Let dropdown close when clicked outside of
--!param button (button) mouseclick
--!param x (coord) x coordinate
--!param y (coord) y coordinate
--!return continue triggering parent function
function UIDropdown:onMouseDown(button, x, y)
if not self:hitTest(x, y) then
self.parent_button:toggle()
self:close()
return false
end
return UIResizable.onMouseDown(self, button, x, y)
end