Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/dropdown.lua

raw
1 --[[ Copyright (c) 2013 Manuel "Roujin" Wolf
2 Copyright (c) 2026 "lewri"
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE. --]]
21
22 --! Dropdown "window" used for selection of one item from a list.
23 class "UIDropdown" (UIResizable)
24
25 ---@type UIDropdown
26 local UIDropdown = _G["UIDropdown"]
27
28 --! Constructor for the dropdown "window"
29 --!param ui (UI) The ui the window is created in
30 --!param parent_window (Window) The window that this dropdown will be attached to
31 --!param parent_button (Button) The button in the parent_window that this dropdown will be positioned under
32 --!param items (table) A list of items for the list to display, where each item is a table with at least
33 -- the field text, and optionally fields font and/or tooltip, which is a table containing text, x and y positions.
34 --!param callback (function) A function to be called when an item is selected. It is called with two parameters:
35 -- The parent window and the index of the selected item
36 --!param colour (table) Colour of the dropdown's list items, in the form of {red = ..., green = ..., blue = ...}.
37 -- Optional if parent_window is a UIResizable
38 --!param scrollbar_colour (table) Colour of the scrollbar's 'thumb', and
39 -- button hover in the form of {red = ..., green = ..., blue = ...}.
40 -- Optional if parent_window is a UIResizable
41 --!param disabled_colour (table) Colour of the disabled items in the form of {red = ..., green = ..., blue = ...}.
42 -- Optional.
43 function UIDropdown:UIDropdown(ui, parent_window, parent_button, items, callback, colour, scrollbar_colour, disabled_colour)
44 assert(colour or parent_window.colour,
45 "Dropdown has no colours available to it. Please specify one in the colour parameter.")
46 self.colour = colour or parent_window.colour
47 self.scrollbar_colour = scrollbar_colour
48 if not self.scrollbar_colour then
49 self.scrollbar_colour = {
50 red = math.floor(self.colour.red * 0.9),
51 green = math.floor(self.colour.green * 0.9),
52 blue = math.floor(self.colour.blue * 0.9)
53 }
54 end
55 self.disabled_colour = disabled_colour
56 self:UIResizable(ui, 1, 1, self.colour, true, true)
57
58 self.modal_class = "dropdown"
59 self.esc_closes = true
60 self.resizable = false
61 self.default_button_sound = "selectx.wav"
62
63 self.parent_window = parent_window
64 self.parent_button = parent_button
65 self.items = items
66 self.callback = callback
67 local scrollbar_threshold = 7
68 local show_scrollbar = #items > scrollbar_threshold
69 self.num_rows = math.min(scrollbar_threshold, #items)
70
71
72 local panel = parent_button.panel_for_sprite
73
74 local width = panel.w
75 local height = panel.h
76
77 self:setPosition(panel.x, panel.y + panel.h)
78
79 self.item_panels = {}
80 self.item_buttons = {}
81
82 local y = 0
83 for num = 1, self.num_rows do
84 self.item_panels[num] = self:addBevelPanel(1, y + 1, width, height,
85 self.colour, nil, nil, self.disabled_colour, self.scrollbar_colour)
86 :setLabel(nil, nil, "center")
87 :setTooltip(nil)
88 self.item_buttons[num] = self.item_panels[num]
89 :makeToggleButton(0, 0, width, height, nil,
90 --[[persistable:dropdown_callback]] function() self:selectItem(num) end)
91 y = y + height
92 end
93
94 if show_scrollbar then
95 local scrollbar_width = 20
96 local scrollbar_base = self:addBevelPanel(width, 1, scrollbar_width, self.num_rows * height, self.colour)
97 scrollbar_base.lowered = true
98 self.scrollbar = scrollbar_base:makeScrollbar(self.scrollbar_colour,
99 --[[persistable:dropdown_scrollbar_callback]] function() self:updateButtons() end,
100 1, math.max(#items, 1), self.num_rows)
101 width = width + scrollbar_width
102 end
103 -- Adjust size
104 self:overrideMinSize(width, height)
105 self:setSize(width, y)
106 self:updateButtons()
107 end
108
109 -- Updates buttons when scrolling.
110 function UIDropdown:updateButtons()
111 for num = 1, self.num_rows do
112 local panel = self.item_panels[num]
113 local button = self.item_buttons[num]
114 local item_index = self.scrollbar and num + self.scrollbar.value - 1 or num
115 local item = self.items[item_index]
116 if item then
117 panel.label_font = nil
118 panel:setLabel(item.text, item.font)
119 panel:setTooltip(item.tooltip and unpack(item.tooltip) or nil)
120 button:enable(not item.disabled)
121 else
122 panel:setLabel()
123 panel:setTooltip()
124 button:enable(false)
125 end
126 end
127 end
128
129 function UIDropdown:selectItem(number)
130 local item_index = self.scrollbar and number + self.scrollbar.value - 1 or number
131 local item = self.items[item_index]
132 UIResizable.close(self)
133 self.parent_button:setLabel(item.text, item.font):setToggleState(false)
134 if self.callback then
135 self.callback(self.parent_window, item_index)
136 end
137 end
138
139 function UIDropdown:beginDrag(x, y)
140 -- TODO: It may be undesirable anyway for dropdowns, but for dragging of sub-windows
141 -- offsets are wrongly calculated (results in jump when dragging)
142 -- Disable dragging
143 return false
144 end
145
146 --! Let dropdown close when clicked outside of
147 --!param button (button) mouseclick
148 --!param x (coord) x coordinate
149 --!param y (coord) y coordinate
150 --!return continue triggering parent function
151 function UIDropdown:onMouseDown(button, x, y)
152 if not self:hitTest(x, y) then
153 self.parent_button:toggle()
154 self:close()
155 return false
156 end
157 return UIResizable.onMouseDown(self, button, x, y)
158 end
159
160 --! Allow the dropdown to scroll through mousewheel input
161 --!param x (int) UNUSED, 1 on right scroll, -1 on left scroll
162 --!param y (int) 1 on up scroll, -1 on down scroll
163 function UIDropdown:onMouseWheel(x, y)
164 if y == 0 then return false end -- Do nothing on x only scroll
165 local bar = self.scrollbar
166
167 if not self:hitTest(self.cursor_x, self.cursor_y) or not (bar and bar.enabled) then
168 return false
169 end
170
171 local slider = bar.slider
172 local track_len = slider.max_y - slider.min_y
173 local steps = bar.max_value - bar.page_size
174
175 -- Identify the nearest normalised slider position to our y position
176 local slider_y = bar:getXorY() - slider.min_y
177 local nearest_slot = math.round((slider_y * steps) / track_len)
178
179 -- Next position
180 local next_slot = nearest_slot - y
181
182 -- Calculate the new slider y position
183 local offset = math.round((next_slot * track_len) / steps)
184 bar:setXorY(slider.min_y + offset)
185
186 return true
187 end
188
189 function UIDropdown:onMouseMove(x, y, dx, dy)
190 local panels = self.item_panels
191 local buttons = self.item_buttons
192 -- Items in the dropdown list overlap on the last pixel, causing two
193 -- panels to appear 'active', however the higher up panel is what is
194 -- actually selected. Separation by 1px causes gaps when UI scaled.
195 local hit = false
196 for row = 1, self.num_rows do
197 if buttons[row].enabled then
198 if self:hitTestPanel(x, y, panels[row]) and not hit then
199 panels[row]:setColour(self.scrollbar_colour, true)
200 hit = true
201 else
202 panels[row]:setColour(self.colour, true)
203 end
204 end
205 end
206 return UIResizable.onMouseMove(self, x, y, dx, dy)
207 end