| 1 |
--[[ Copyright (c) 2009 Peter "Corsix" Cawley |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
corsixth.require("persistance") |
| 22 |
|
| 23 |
--! Base class for user-interface dialogs. |
| 24 |
class "Window" |
| 25 |
|
| 26 |
---@type Window |
| 27 |
local Window = _G["Window"] |
| 28 |
|
| 29 |
-- NB: pressed mouse buttons are denoted with a "mouse_" prefix in buttons_down, |
| 30 |
-- i.e. mouse_left, mouse_middle, mouse_right |
| 31 |
Window.buttons_down = permanent"Window.buttons_down" {} |
| 32 |
|
| 33 |
function Window:Window() |
| 34 |
self.x = 0 |
| 35 |
self.y = 0 |
| 36 |
|
| 37 |
self.cursor_x = 0 |
| 38 |
self.cursor_y = 0 |
| 39 |
|
| 40 |
self.panels = { |
| 41 |
} |
| 42 |
self.buttons = { |
| 43 |
} |
| 44 |
self.tooltip_regions = { |
| 45 |
} |
| 46 |
self.scrollbars = { |
| 47 |
} |
| 48 |
self.textboxes = { -- list of textboxes in that window. NB: (Game)UI also uses this. |
| 49 |
} -- Take care not to handle things twice as UI is subclass of window! |
| 50 |
self.hotkeyboxes = { |
| 51 |
} |
| 52 |
self.key_handlers = {--[[a set]]} |
| 53 |
self.windows = false -- => {} when first window added |
| 54 |
self.active_button = false |
| 55 |
self.blinking_button = false |
| 56 |
self.blink_counter = 0 |
| 57 |
self.panel_sprites = false |
| 58 |
self.visible = true |
| 59 |
self.draggable = true |
| 60 |
self.apply_ui_scale = true |
| 61 |
end |
| 62 |
|
| 63 |
-- Most windows don't pause the game. Specific windows: fax, annual report, staff rise, and errors do pause and are set in the specific files |
| 64 |
function Window:mustPause() |
| 65 |
return false |
| 66 |
end |
| 67 |
|
| 68 |
function Window:setSize(width, height, apply_ui_scale) |
| 69 |
self.apply_ui_scale = apply_ui_scale ~= nil and apply_ui_scale or self.apply_ui_scale |
| 70 |
self.width = width |
| 71 |
self.height = height |
| 72 |
end |
| 73 |
|
| 74 |
-- Sets the window's onscreen position. Each of x and y can be: |
| 75 |
-- Integers >= 0 - Absolute pixel positions of top/left edge of window relative |
| 76 |
-- to top/left edge of screen |
| 77 |
-- Integers < 0 - Absolute pixel positions of right/bottom edge of window |
| 78 |
-- relative to right/bottom edge of screen. Use -0.1 to mean -0. |
| 79 |
-- Reals in [0, 1) - |
| 80 |
function Window:setPosition(x, y) |
| 81 |
-- Save values to recalculate x and y on screen resolution change |
| 82 |
self.x_original = x |
| 83 |
self.y_original = y |
| 84 |
-- Convert x and y to absolute pixel positions with regard to top/left |
| 85 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 86 |
local w, h = TheApp.config.width / s, TheApp.config.height / s |
| 87 |
if x < 0 then |
| 88 |
x = math.ceil(w - self.width + x) |
| 89 |
elseif x < 1 then |
| 90 |
x = math.floor((w - self.width) * x + 0.5) |
| 91 |
end |
| 92 |
if y < 0 then |
| 93 |
y = math.ceil(h - self.height + y) |
| 94 |
elseif y < 1 then |
| 95 |
y = math.floor((h - self.height) * y + 0.5) |
| 96 |
end |
| 97 |
self.x = x |
| 98 |
self.y = y |
| 99 |
end |
| 100 |
|
| 101 |
-- Sets the window's default onscreen position and onscreen position. |
| 102 |
-- The given x and y are interpreted as for setPosition(x, y), and are the |
| 103 |
-- default position for the window. If the user has previously repositioned a |
| 104 |
-- window of the same type, then setDefaultPosition() will set the window to |
| 105 |
-- that previous position, otherwise it sets it to the default position. |
| 106 |
function Window:setDefaultPosition(x, y) |
| 107 |
if self.ui then |
| 108 |
local config = self.ui.app.runtime_config.window_position |
| 109 |
if config then |
| 110 |
config = config[self:getSavedWindowPositionName()] |
| 111 |
if config then |
| 112 |
return self:setPosition(config.x, config.y) |
| 113 |
end |
| 114 |
end |
| 115 |
end |
| 116 |
return self:setPosition(x, y) |
| 117 |
end |
| 118 |
|
| 119 |
-- Called after the resolution of the game window changes |
| 120 |
function Window:onChangeResolution() |
| 121 |
if self.x_original and self.y_original then |
| 122 |
self:setPosition(self.x_original, self.y_original) |
| 123 |
end |
| 124 |
end |
| 125 |
|
| 126 |
-- Called before the window is closed |
| 127 |
function Window:close() |
| 128 |
if self.dragging then |
| 129 |
self.dragging = false |
| 130 |
self.ui.drag_mouse_move = nil |
| 131 |
end |
| 132 |
if self.parent then |
| 133 |
self.parent:removeWindow(self) |
| 134 |
end |
| 135 |
for key in pairs(self.key_handlers) do |
| 136 |
self.ui:removeKeyHandler(key, self) |
| 137 |
end |
| 138 |
for _, box in pairs(self.textboxes) do |
| 139 |
self.ui:unregisterTextBox(box) |
| 140 |
end |
| 141 |
for _, box in pairs(self.hotkeyboxes) do |
| 142 |
self.ui:unregisterHotkeyBox(box) |
| 143 |
end |
| 144 |
self.closed = true |
| 145 |
end |
| 146 |
|
| 147 |
function Window:addKeyHandler(key, handler, ...) |
| 148 |
self.ui:addKeyHandler(key, self, handler, ...) |
| 149 |
self.key_handlers[key] = true |
| 150 |
end |
| 151 |
|
| 152 |
--!param keys (string or table) The key or a list containing the key & its modifiers, |
| 153 |
-- previously passed to Window:addKeyHandler(keys). |
| 154 |
function Window:removeKeyHandler(keys) |
| 155 |
self.ui:removeKeyHandler(keys, self) |
| 156 |
end |
| 157 |
|
| 158 |
--! The basic component which makes up most `Window`s. |
| 159 |
--! The visual parts of most ingame dialogs are sprites from a sprite sheet. |
| 160 |
-- A `Panel` is an instance of a particular sprite, consisting of a sprite |
| 161 |
-- index and a position. It is advantageous to construct dialogs out of panels |
| 162 |
-- (using `Window:addPanel`) as the common operations on panels (like drawing |
| 163 |
-- them and hit-testing against them) are implemented in the `Window` class, |
| 164 |
-- thus reducing the amount of work that each individual dialog has to do. |
| 165 |
class "Panel" |
| 166 |
|
| 167 |
---@type Panel |
| 168 |
local Panel = _G["Panel"] |
| 169 |
|
| 170 |
-- !dummy |
| 171 |
function Panel:Panel() |
| 172 |
self.window = nil |
| 173 |
self.x = nil |
| 174 |
self.y = nil |
| 175 |
self.w = nil |
| 176 |
self.h = nil |
| 177 |
self.colour = nil |
| 178 |
self.custom_draw = nil |
| 179 |
self.visible = nil |
| 180 |
self.wrap_text = false |
| 181 |
end |
| 182 |
|
| 183 |
local panel_mt = permanent("Window.<panel_mt>", getmetatable(Panel())) |
| 184 |
|
| 185 |
function Panel:makeButton(...) |
| 186 |
return self.window:makeButtonOnPanel(self, ...) |
| 187 |
end |
| 188 |
|
| 189 |
function Panel:makeToggleButton(...) |
| 190 |
return self.window:makeButtonOnPanel(self, ...):makeToggle() |
| 191 |
end |
| 192 |
|
| 193 |
function Panel:makeRepeatButton(...) |
| 194 |
return self.window:makeButtonOnPanel(self, ...):makeRepeat() |
| 195 |
end |
| 196 |
|
| 197 |
function Panel:makeScrollbar(...) |
| 198 |
return self.window:makeScrollbarOnPanel(self, ...) |
| 199 |
end |
| 200 |
|
| 201 |
function Panel:makeTextbox(...) |
| 202 |
return self.window:makeTextboxOnPanel(self, ...) |
| 203 |
end |
| 204 |
|
| 205 |
function Panel:makeHotkeyBox(...) |
| 206 |
return self.window:makeHotkeyBoxOnPanel(self, ...) |
| 207 |
end |
| 208 |
|
| 209 |
local function sanitize(colour) |
| 210 |
if colour > 255 then |
| 211 |
colour = 255 |
| 212 |
elseif colour < 0 then |
| 213 |
colour = 0 |
| 214 |
end |
| 215 |
return colour |
| 216 |
end |
| 217 |
|
| 218 |
--[[ Set the colour of a panel |
| 219 |
! Note: This works only with ColourPanel and BevelPanel, not normal (sprite) panels. |
| 220 |
!param col (table) Colour given as a table with three fields red, green and blue, each an integer value in [0, 255]. |
| 221 |
!param preserve_disable (boolean) If true, do not adjust the panel's disabled colour. |
| 222 |
]] |
| 223 |
function Panel:setColour(col, preserve_disable) |
| 224 |
self.colour = self.colour and TheApp.video:mapRGB(col.red, col.green, col.blue) |
| 225 |
self.highlight_colour = self.highlight_colour and TheApp.video:mapRGB( |
| 226 |
sanitize(col.red + 60), |
| 227 |
sanitize(col.green + 60), |
| 228 |
sanitize(col.blue + 60)) |
| 229 |
self.shadow_colour = self.shadow_colour and TheApp.video:mapRGB( |
| 230 |
sanitize(col.red - 60), |
| 231 |
sanitize(col.green - 60), |
| 232 |
sanitize(col.blue - 60)) |
| 233 |
if preserve_disable then return self end |
| 234 |
self.disabled_colour = self.disabled_colour and TheApp.video:mapRGB( |
| 235 |
sanitize(math.floor((col.red + 100) / 2)), |
| 236 |
sanitize(math.floor((col.green + 100) / 2)), |
| 237 |
sanitize(math.floor((col.blue + 100) / 2))) |
| 238 |
return self |
| 239 |
end |
| 240 |
|
| 241 |
-- Specify a tooltip to be displayed when hovering this panel. |
| 242 |
-- x and y are optional position of bottom left of the tooltip. |
| 243 |
-- If not specified, will default to mouse position. |
| 244 |
function Panel:setTooltip(tooltip, x, y) |
| 245 |
self.tooltip = { |
| 246 |
text = tooltip, |
| 247 |
tooltip_x = x, |
| 248 |
tooltip_y = y, |
| 249 |
} |
| 250 |
return self |
| 251 |
end |
| 252 |
|
| 253 |
function Panel:setDynamicTooltip(callback, x, y) |
| 254 |
self.tooltip = { |
| 255 |
callback = callback, |
| 256 |
tooltip_x = x, |
| 257 |
tooltip_y = y, |
| 258 |
} |
| 259 |
return self |
| 260 |
end |
| 261 |
|
| 262 |
--! Specify a label to be drawn on top of the panel. |
| 263 |
-- Note: This works only with ColourPanel and BevelPanel, not normal (sprite) panels. |
| 264 |
--!param label (string) The text to be drawn on top of the label. |
| 265 |
--!param font (font) [optional] The font to use. Default is Font01V in QData. |
| 266 |
--!param align (string) [optional] Alignment for non-multiline labels (multiline is always left) |
| 267 |
--! can be either of "left", "center", "right" |
| 268 |
function Panel:setLabel(label, font, align) |
| 269 |
self.label = label or "" |
| 270 |
self.label_font = font or self.label_font or TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, {ttf_shadow = true, apply_ui_scale = true}) |
| 271 |
self.align = align or self.align |
| 272 |
return self |
| 273 |
end |
| 274 |
|
| 275 |
--! Specifies whether auto clip (clipping text at the end so it fits) is enabled for this panel |
| 276 |
--!param mode (boolean) true to activate, false to deactivate. |
| 277 |
function Panel:setAutoClip(mode) |
| 278 |
self.auto_clip = mode |
| 279 |
return self |
| 280 |
end |
| 281 |
|
| 282 |
--! Checks if a given line drawn with the panel's label font would be longer than the given limit |
| 283 |
--! and if so, shortens it enough to fit including "..." at the end |
| 284 |
--!param line (string) the line to modify |
| 285 |
--!param limit (int) the maximum length in pixels the line should have |
| 286 |
--!return the possibly modified line |
| 287 |
function Panel:clipLine(line, limit) |
| 288 |
local _, last_x = self.label_font:draw(nil, line, 0, 0) |
| 289 |
if last_x > limit then |
| 290 |
limit = limit - self.label_font:sizeOf("..._") |
| 291 |
while last_x > limit do |
| 292 |
line = line:sub(1, -2) |
| 293 |
_, last_x = self.label_font:draw(nil, line, 0, 0) |
| 294 |
end |
| 295 |
line = line .. "..." |
| 296 |
end |
| 297 |
return line |
| 298 |
end |
| 299 |
|
| 300 |
--! Draw function for the label on a panel |
| 301 |
--!param canvas The canvas to draw on (can be nil for test) |
| 302 |
--!param x x position to start drawing on |
| 303 |
--!param y y position to start drawing on |
| 304 |
--!param limit (nil or {int, int}) limit after which line and with character on that line to stop drawing |
| 305 |
--!return y and x end positions after drawing |
| 306 |
function Panel:drawLabel(canvas, x, y, limit) |
| 307 |
local text = self.label |
| 308 |
local center_y = false |
| 309 |
local wrapped = not self.auto_clip |
| 310 |
|
| 311 |
local multi_line = type(text) == "table" |
| 312 |
if not multi_line then |
| 313 |
text = {text} |
| 314 |
center_y = true |
| 315 |
wrapped = (self.wrap_text ~= nil) and self.wrap_text or false |
| 316 |
end |
| 317 |
|
| 318 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 319 |
local next_y = y + self.y * s + s |
| 320 |
local last_x = x + self.x * s + 2 * s |
| 321 |
for i, line in ipairs(text) do |
| 322 |
local old_y = next_y |
| 323 |
if limit and limit[1] == i then |
| 324 |
line = line:sub(1, limit[2]) |
| 325 |
end |
| 326 |
if self.auto_clip then |
| 327 |
line = self:clipLine(line, self.w * s - 4 * s) |
| 328 |
end |
| 329 |
|
| 330 |
if wrapped then |
| 331 |
next_y, last_x = self.label_font:drawWrapped(canvas, line, x + self.x * s + 2 * s, old_y, self.w * s - 4 * s, self.align) |
| 332 |
else |
| 333 |
next_y, last_x = self.label_font:draw(canvas, line, x + self.x * s + 2 * s, old_y, (self.w * s - 4 * s), (center_y and self.h * s or 0), self.align) |
| 334 |
end |
| 335 |
if not line:find("%S") then |
| 336 |
-- Special handling for empty lines or lines with only space |
| 337 |
next_y = self.label_font:draw(nil, "A", (x + self.x * s + 2 * s), old_y, (self.w * s - 4 * s), (center_y and self.h * s or 0), self.align) |
| 338 |
end |
| 339 |
if limit and limit[1] == i then |
| 340 |
break |
| 341 |
end |
| 342 |
end |
| 343 |
return next_y, last_x |
| 344 |
end |
| 345 |
|
| 346 |
--! Set the position of a panel. |
| 347 |
--!param x (int) New horizontal position of the panel. |
| 348 |
--!param y (int) New vertical position of the panel. |
| 349 |
function Panel:setPosition(x, y) |
| 350 |
self.x = x |
| 351 |
self.y = y |
| 352 |
end |
| 353 |
|
| 354 |
--! Set the size of a panel. |
| 355 |
--!param width (int) New width of the panel. |
| 356 |
--!param height (int) New height of the panel. |
| 357 |
function Panel:setSize(width, height) |
| 358 |
self.w = width |
| 359 |
self.h = height |
| 360 |
end |
| 361 |
|
| 362 |
--! Set the visibility of the panel. |
| 363 |
--!param visibility (bool) New visibility of the panel. |
| 364 |
function Panel:setVisible(visibility) |
| 365 |
self.visible = visibility |
| 366 |
return self |
| 367 |
end |
| 368 |
|
| 369 |
--! Set whether text wrapping is enabled for this panel. |
| 370 |
--!param enabled (bool) Whether to text wrapping enabled. |
| 371 |
function Panel:setTextWrap(enabled) |
| 372 |
self.wrap_text = enabled |
| 373 |
return self |
| 374 |
end |
| 375 |
|
| 376 |
--[[ Add a `Panel` to the window. |
| 377 |
! Panels form the basic building blocks of most windows. A panel is a small |
| 378 |
bitmap coupled with a position, and by combining several panels, a window can |
| 379 |
be made. By using panels to construct windows, all of the common tasks like |
| 380 |
drawing and hit-testing are provided for you by the base class methods, thus |
| 381 |
reducing the amount of code required elsewhere. |
| 382 |
!param sprite_index (integer) Index into the window's sprite table of the |
| 383 |
bitmap to be displayed. |
| 384 |
!param x (integer) The X pixel position to display the bitmap at. |
| 385 |
!param y (integer) The Y pixel position to display the bitmap at. |
| 386 |
!param w (integer, nil) If the panel is totally opaque, and the width of the |
| 387 |
panel (in pixels) is known, it should be specified here to speed up hit-tests. |
| 388 |
!param h (integer, nil) If the panel is totally opaque, and the height of the |
| 389 |
panel (in pixels) is known, it should be specified here to speed up hit-tests. |
| 390 |
!param apply_ui_scale (bool|nil) If true apply the ui_scale to the dimensions |
| 391 |
when doing the draw and hit test. If nil then apply_ui_scale if enabled for the |
| 392 |
parent window. |
| 393 |
!param draw_flags (integer) Draw flags to apply to the sprite |
| 394 |
]] |
| 395 |
function Window:addPanel(sprite_index, x, y, w, h, apply_ui_scale, draw_flags) |
| 396 |
local panel = setmetatable({ |
| 397 |
window = self, |
| 398 |
apply_ui_scale = apply_ui_scale ~= nil and apply_ui_scale or self.apply_ui_scale, |
| 399 |
draw_flags = draw_flags, |
| 400 |
x = x, |
| 401 |
y = y, |
| 402 |
w = w, |
| 403 |
h = h, |
| 404 |
sprite_index = sprite_index, |
| 405 |
visible = true, |
| 406 |
}, panel_mt) |
| 407 |
self.panels[#self.panels + 1] = panel |
| 408 |
return panel |
| 409 |
end |
| 410 |
|
| 411 |
function Window:removeAllPanels() |
| 412 |
self.panels = {} |
| 413 |
self.buttons = {} -- Buttons cannot live without a panel |
| 414 |
end |
| 415 |
|
| 416 |
local --[[persistable: window_panel_colour_draw]] function panel_colour_draw(panel, canvas, x, y) |
| 417 |
local s = panel.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 418 |
canvas:drawRect(panel.colour, x + panel.x * s, y + panel.y * s, panel.w * s, panel.h * s) |
| 419 |
if panel.label then |
| 420 |
panel:drawLabel(canvas, x, y) |
| 421 |
end |
| 422 |
end |
| 423 |
|
| 424 |
--[[ Add a solid-colour `Panel` to the window. |
| 425 |
! A solid-colour panel is like a normal panel, expect it displays a solid |
| 426 |
colour rather than a bitmap. |
| 427 |
!param x (integer) The X pixel position to start the panel at. |
| 428 |
!param y (integer) The Y pixel position to start the panel at. |
| 429 |
!param w (integer) The width of the panel, in pixels. |
| 430 |
!param h (integer) The height of the panel, in pixels. |
| 431 |
!param r (integer) Value in [0, 255] giving the red component of the colour. |
| 432 |
!param g (integer) Value in [0, 255] giving the green component of the colour. |
| 433 |
!param b (integer) Value in [0, 255] giving the blue component of the colour. |
| 434 |
!param apply_ui_scale (boolean) Whether to apply UI scale to position and size. |
| 435 |
Default is to match window. |
| 436 |
]] |
| 437 |
function Window:addColourPanel(x, y, w, h, r, g, b, apply_ui_scale) |
| 438 |
local panel = setmetatable({ |
| 439 |
window = self, |
| 440 |
apply_ui_scale = apply_ui_scale ~= nil and apply_ui_scale or self.apply_ui_scale, |
| 441 |
x = x, |
| 442 |
y = y, |
| 443 |
w = w, |
| 444 |
h = h, |
| 445 |
colour = TheApp.video:mapRGB(r, g, b), |
| 446 |
custom_draw = panel_colour_draw, |
| 447 |
visible = true, |
| 448 |
}, panel_mt) |
| 449 |
self.panels[#self.panels + 1] = panel |
| 450 |
return panel |
| 451 |
end |
| 452 |
|
| 453 |
local --[[persistable: window_panel_bevel_draw]] function panel_bevel_draw(panel, canvas, x, y) |
| 454 |
local s = panel.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 455 |
|
| 456 |
if panel.lowered then |
| 457 |
canvas:drawRect(panel.highlight_colour, x + panel.x * s, y + panel.y * s, panel.w * s, panel.h * s) |
| 458 |
canvas:drawRect(panel.shadow_colour, x + panel.x * s, y + panel.y * s, panel.w * s - s, panel.h * s - s) |
| 459 |
canvas:drawRect(panel.lowered_colour, x + panel.x * s + s, y + panel.y * s + s, panel.w * s - 2 * s, panel.h * s - 2 * s) |
| 460 |
else |
| 461 |
canvas:drawRect(panel.shadow_colour, x + panel.x * s + s, y + panel.y * s + s, panel.w * s - s, panel.h * s - s) |
| 462 |
canvas:drawRect(panel.highlight_colour, x + panel.x * s, y + panel.y * s, panel.w * s - s, panel.h * s - s) |
| 463 |
canvas:drawRect(panel.colour, x + panel.x * s + s, y + panel.y * s + s, panel.w * s - 2 * s, panel.h * s - 2 * s) |
| 464 |
end |
| 465 |
if panel.label then |
| 466 |
panel:drawLabel(canvas, x, y) |
| 467 |
end |
| 468 |
end |
| 469 |
|
| 470 |
--[[ Add a beveled `Panel` to the window. |
| 471 |
! A bevel panel is similar to a solid colour panel, except that it |
| 472 |
features a highlight and a shadow that makes it appear either lowered or raised. |
| 473 |
!param x (integer) The X pixel position to start the panel at. |
| 474 |
!param y (integer) The Y pixel position to start the panel at. |
| 475 |
!param w (integer) The width of the panel, in pixels. |
| 476 |
!param h (integer) The height of the panel, in pixels. |
| 477 |
!param colour (colour in form .red, .green and .blue) The colour for the panel. |
| 478 |
!param highlight_colour (colour in form .red, .green and .blue or nil) [optional] The colour for the highlight. |
| 479 |
!param shadow_colour (colour in form .red, .green and .blue or nil) [optional] The colour for the shadow. |
| 480 |
!param disabled_colour (colour in form .red, .green and .blue or nil) [optional] The colour for the disabled panel. |
| 481 |
!param lowered_colour (colour in form .red, .green and .blue or nil) [optional] The colour for the lowered (toggled) panel. |
| 482 |
]] |
| 483 |
function Window:addBevelPanel(x, y, w, h, colour, highlight_colour, shadow_colour, disabled_colour, lowered_colour, apply_ui_scale) |
| 484 |
highlight_colour = highlight_colour or { |
| 485 |
red = sanitize(colour.red + 60), |
| 486 |
green = sanitize(colour.green + 60), |
| 487 |
blue = sanitize(colour.blue + 60), |
| 488 |
} |
| 489 |
shadow_colour = shadow_colour or { |
| 490 |
red = sanitize(colour.red - 60), |
| 491 |
green = sanitize(colour.green - 60), |
| 492 |
blue = sanitize(colour.blue - 60), |
| 493 |
} |
| 494 |
disabled_colour = disabled_colour or { |
| 495 |
red = sanitize(math.floor((colour.red + 100) / 2)), |
| 496 |
green = sanitize(math.floor((colour.green + 100) / 2)), |
| 497 |
blue = sanitize(math.floor((colour.blue + 100) / 2)), |
| 498 |
} |
| 499 |
lowered_colour = lowered_colour or colour |
| 500 |
|
| 501 |
local panel = setmetatable({ |
| 502 |
window = self, |
| 503 |
apply_ui_scale = apply_ui_scale ~= nil and apply_ui_scale or self.apply_ui_scale, |
| 504 |
x = x, |
| 505 |
y = y, |
| 506 |
w = w, |
| 507 |
h = h, |
| 508 |
colour = TheApp.video:mapRGB(colour.red, colour.green, colour.blue), |
| 509 |
highlight_colour = TheApp.video:mapRGB(highlight_colour.red, highlight_colour.green, highlight_colour.blue), |
| 510 |
shadow_colour = TheApp.video:mapRGB(shadow_colour.red, shadow_colour.green, shadow_colour.blue), |
| 511 |
disabled_colour = TheApp.video:mapRGB(disabled_colour.red, disabled_colour.green, disabled_colour.blue), |
| 512 |
lowered_colour = TheApp.video:mapRGB(lowered_colour.red, lowered_colour.green, lowered_colour.blue), |
| 513 |
custom_draw = panel_bevel_draw, |
| 514 |
visible = true, |
| 515 |
lowered = false, |
| 516 |
}, panel_mt) |
| 517 |
self.panels[#self.panels + 1] = panel |
| 518 |
return panel |
| 519 |
end |
| 520 |
|
| 521 |
function Window:addWindow(window) |
| 522 |
if window.closed then |
| 523 |
return |
| 524 |
end |
| 525 |
if not self.windows then |
| 526 |
self.windows = {} |
| 527 |
end |
| 528 |
window.parent = self |
| 529 |
if window.on_top then |
| 530 |
-- As self.windows array is ordered from top to bottom and drawn by the end, a "On Top" window has be added at start |
| 531 |
table.insert(self.windows, 1, window) |
| 532 |
else |
| 533 |
-- Normal windows, are added in the first position after on_top windows |
| 534 |
local pos = false |
| 535 |
for i = 1, #self.windows do |
| 536 |
if not self.windows[i].on_top then |
| 537 |
pos = i |
| 538 |
break |
| 539 |
end |
| 540 |
end |
| 541 |
pos = pos or #self.windows + 1 |
| 542 |
table.insert(self.windows, pos, window) |
| 543 |
end |
| 544 |
end |
| 545 |
|
| 546 |
function Window:removeWindow(window) |
| 547 |
if self.windows then |
| 548 |
for n = 1, #self.windows do |
| 549 |
if self.windows[n] == window then |
| 550 |
if #self.windows == 1 then |
| 551 |
self.windows = false |
| 552 |
else |
| 553 |
table.remove(self.windows, n) |
| 554 |
end |
| 555 |
return true |
| 556 |
end |
| 557 |
end |
| 558 |
end |
| 559 |
return false |
| 560 |
end |
| 561 |
|
| 562 |
-- Searches (direct) child windows for window of the given class, and returns |
| 563 |
-- one (or nil if there weren't any at all). |
| 564 |
-- !param window_class (class) The class of window to search for. |
| 565 |
function Window:getWindow(window_class) |
| 566 |
if self.windows then |
| 567 |
for _, window in ipairs(self.windows) do |
| 568 |
if class.is(window, window_class) then |
| 569 |
return window |
| 570 |
end |
| 571 |
end |
| 572 |
end |
| 573 |
end |
| 574 |
|
| 575 |
-- Searches (direct) child windows for window of the given class, and returns |
| 576 |
-- a (potentially empty) list of matching windows. |
| 577 |
-- !param window_class (class) The class of window to search for. |
| 578 |
function Window:getWindows(window_class) |
| 579 |
local matching_windows = {} |
| 580 |
if self.windows then |
| 581 |
for _, window in ipairs(self.windows) do |
| 582 |
if class.is(window, window_class) then |
| 583 |
matching_windows[#matching_windows+1] = window |
| 584 |
end |
| 585 |
end |
| 586 |
end |
| 587 |
return matching_windows |
| 588 |
end |
| 589 |
|
| 590 |
--! A region of a `Panel` which causes some action when clicked. |
| 591 |
class "Button" |
| 592 |
|
| 593 |
---@type Button |
| 594 |
local Button = _G["Button"] |
| 595 |
|
| 596 |
--!dummy |
| 597 |
function Button:Button() |
| 598 |
self.ui = nil |
| 599 |
self.is_toggle = nil |
| 600 |
self.is_repeat = nil |
| 601 |
self.x = nil |
| 602 |
self.y = nil |
| 603 |
self.r = nil |
| 604 |
self.b = nil |
| 605 |
self.panel_for_sprite = nil |
| 606 |
self.sprite_index_normal = nil |
| 607 |
self.sprite_index_disabled = nil |
| 608 |
self.sprite_index_active = nil |
| 609 |
self.panel_lowered_normal = nil |
| 610 |
self.panel_lowered_active = nil |
| 611 |
self.on_click = nil |
| 612 |
self.on_click_self = nil |
| 613 |
self.on_rightclick = nil |
| 614 |
self.enabled = nil |
| 615 |
end |
| 616 |
|
| 617 |
local button_mt = permanent("Window.<button_mt>", getmetatable(Button())) |
| 618 |
|
| 619 |
function Button:setDisabledSprite(index) |
| 620 |
self.sprite_index_disabled = index |
| 621 |
return self |
| 622 |
end |
| 623 |
|
| 624 |
--! Enable or disable a button. |
| 625 |
--!param enable (boolean) Whether to enable (true) or disable (false) the button. |
| 626 |
function Button:enable(enable) |
| 627 |
if enable and not self.enabled then |
| 628 |
self.enabled = true |
| 629 |
self.panel_for_sprite.sprite_index = self.sprite_index_normal |
| 630 |
if self.panel_for_sprite.colour_backup then |
| 631 |
self.panel_for_sprite.colour = self.panel_for_sprite.colour_backup |
| 632 |
end |
| 633 |
end |
| 634 |
if not enable and self.enabled then |
| 635 |
self.enabled = false |
| 636 |
self.panel_for_sprite.sprite_index = self.sprite_index_disabled |
| 637 |
if self.panel_for_sprite.disabled_colour then |
| 638 |
self.panel_for_sprite.colour_backup = self.panel_for_sprite.colour |
| 639 |
self.panel_for_sprite.colour = self.panel_for_sprite.disabled_colour |
| 640 |
end |
| 641 |
end |
| 642 |
return self |
| 643 |
end |
| 644 |
|
| 645 |
function Button:makeToggle() |
| 646 |
self.is_toggle = true |
| 647 |
self.is_repeat = false |
| 648 |
self.toggled = false |
| 649 |
return self |
| 650 |
end |
| 651 |
|
| 652 |
function Button:makeRepeat() |
| 653 |
self.is_repeat = true |
| 654 |
self.is_toggle = false |
| 655 |
self.toggled = false |
| 656 |
return self |
| 657 |
end |
| 658 |
|
| 659 |
--! Flip the toggle state of the button (on -> off, or off -> on). |
| 660 |
function Button:toggle() |
| 661 |
self.sprite_index_normal, self.sprite_index_active = |
| 662 |
self.sprite_index_active, self.sprite_index_normal |
| 663 |
self.panel_lowered_active, self.panel_lowered_normal = |
| 664 |
self.panel_lowered_normal, self.panel_lowered_active |
| 665 |
self.panel_for_sprite.sprite_index = self.sprite_index_normal |
| 666 |
self.panel_for_sprite.lowered = self.panel_lowered_normal |
| 667 |
self.toggled = not self.toggled |
| 668 |
return self.toggled |
| 669 |
end |
| 670 |
|
| 671 |
--! Set the toggle state of the button to the provided state. |
| 672 |
--!param state (boolean) Desired state of the toggle button. |
| 673 |
function Button:setToggleState(state) |
| 674 |
if self.toggled ~= state then |
| 675 |
self:toggle() |
| 676 |
end |
| 677 |
return self |
| 678 |
end |
| 679 |
|
| 680 |
function Button:preservePanel() |
| 681 |
local window = self.panel_for_sprite.window |
| 682 |
self.panel_for_sprite = window:addPanel(0, self.x, self.y) |
| 683 |
self.sprite_index_normal = 0 |
| 684 |
return self |
| 685 |
end |
| 686 |
|
| 687 |
function Button:setSound(name) |
| 688 |
self.sound = name |
| 689 |
return self |
| 690 |
end |
| 691 |
|
| 692 |
local --[[persistable:window_drag_round]] function round(value, amount) |
| 693 |
return amount * math.floor(value / amount + 0.5) |
| 694 |
end |
| 695 |
|
| 696 |
-- Specify a tooltip to be displayed when hovering this button. |
| 697 |
-- x and y are optional position of bottom left of the tooltip. |
| 698 |
-- If not specified, will default to top center of button. |
| 699 |
function Button:setTooltip(tooltip, x, y) |
| 700 |
self.tooltip = { |
| 701 |
text = tooltip, |
| 702 |
tooltip_x = x or round((self.x + self.r) / 2, 1), |
| 703 |
tooltip_y = y or self.y, |
| 704 |
} |
| 705 |
return self |
| 706 |
end |
| 707 |
|
| 708 |
function Button:setDynamicTooltip(callback, x, y) |
| 709 |
self.tooltip = { |
| 710 |
callback = callback, |
| 711 |
tooltip_x = x or round((self.x + self.r) / 2, 1), |
| 712 |
tooltip_y = y or self.y, |
| 713 |
} |
| 714 |
return self |
| 715 |
end |
| 716 |
|
| 717 |
--! Called whenever a click on the button should be handled. This depends on the type of button. |
| 718 |
--! Normally this is called when a MouseUp occurs over the button (if the MouseDown occurred over |
| 719 |
--! this or another button). However for repeat buttons, it is called once on MouseDown and, after |
| 720 |
--! a short delay, repeatedly. |
| 721 |
--!param mouse_button (string) either "left" or "right" |
| 722 |
function Button:handleClick(mouse_button) |
| 723 |
local arg = nil |
| 724 |
if self.is_toggle then |
| 725 |
arg = self:toggle() |
| 726 |
end |
| 727 |
if self.sound then |
| 728 |
self.ui:playSound(self.sound) |
| 729 |
end |
| 730 |
local callback = mouse_button == "left" and self.on_click or self.on_rightclick |
| 731 |
if callback then |
| 732 |
callback(self.on_click_self, arg, self) |
| 733 |
else |
| 734 |
if mouse_button == "left" then |
| 735 |
print("Warning: No handler for button click") |
| 736 |
self.on_click = --[[persistable:button_on_click_handler_stub]] function() end |
| 737 |
end |
| 738 |
end |
| 739 |
end |
| 740 |
|
| 741 |
--! Set the position of a button. |
| 742 |
--!param x (int) New horizontal position of the button. |
| 743 |
--!param y (int) New vertical position of the button. |
| 744 |
function Button:setPosition(x, y) |
| 745 |
self.panel_for_sprite:setPosition(x, y) |
| 746 |
self.r = self.r - self.x + x |
| 747 |
self.b = self.b - self.y + y |
| 748 |
self.x = x |
| 749 |
self.y = y |
| 750 |
if self.tooltip then |
| 751 |
self.tooltip.tooltip_x = math.round((self.x + self.r) / 2, 1) |
| 752 |
self.tooltip.tooltip_y = self.y |
| 753 |
end |
| 754 |
end |
| 755 |
|
| 756 |
--! Set the size of a button. |
| 757 |
--!param width (int) New width of the button. |
| 758 |
--!param height (int) New height of the button. |
| 759 |
function Button:setSize(width, height) |
| 760 |
self.panel_for_sprite:setSize(width, height) |
| 761 |
self.r = self.x + width |
| 762 |
self.b = self.y + height |
| 763 |
if self.tooltip then |
| 764 |
self.tooltip.tooltip_x = math.round((self.x + self.r) / 2, 1) |
| 765 |
self.tooltip.tooltip_y = self.y |
| 766 |
end |
| 767 |
end |
| 768 |
|
| 769 |
--! Set the visibility of the button. |
| 770 |
--!param visibility (bool) New visibility of the button. |
| 771 |
function Button:setVisible(visibility) |
| 772 |
self.panel_for_sprite:setVisible(visibility) |
| 773 |
return self |
| 774 |
end |
| 775 |
|
| 776 |
--! Convenience function to allow setLabel to be called on a button, not only its panel. |
| 777 |
--! see Panel:setLabel |
| 778 |
function Button:setLabel(label, font, align) |
| 779 |
self.panel_for_sprite:setLabel(label, font, align) |
| 780 |
return self |
| 781 |
end |
| 782 |
|
| 783 |
--[[ Convert a static panel into a clickable button. |
| 784 |
!param panel (Panel) The panel to convert into a button. |
| 785 |
!param x (integer) The X coordinate of the clickable rectangle on the panel. |
| 786 |
!param y (integer) The Y coordinate of the clickable rectangle on the panel. |
| 787 |
!param w (integer) The width of the clickable rectangle on the panel. |
| 788 |
!param h (integer) The height of the clickable rectangle on the panel. |
| 789 |
!param sprite (integer) An index into the window's sprite sheet. The panel will |
| 790 |
display this sprite when the button is being pressed. |
| 791 |
!param on_click (function) The function to be run when the user left-clicks the |
| 792 |
button. Takes three arguments: `on_click_self`, the toggle state (nil for |
| 793 |
normal buttons, true/false for toggle buttons), the button itself. |
| 794 |
!param on_click_self (function, nil) The first value to pass to `on_click`. If |
| 795 |
nil or not given, then the window is passed as the first argument. |
| 796 |
!param on_rightclick (function, nil) The function to be called when the user |
| 797 |
right-clicks the button. |
| 798 |
]] |
| 799 |
function Window:makeButtonOnPanel(panel, x, y, w, h, sprite, on_click, on_click_self, on_rightclick) |
| 800 |
x = x + panel.x |
| 801 |
y = y + panel.y |
| 802 |
local button = setmetatable({ |
| 803 |
ui = self.ui, |
| 804 |
is_toggle = false, |
| 805 |
is_repeat = false, |
| 806 |
x = x, |
| 807 |
y = y, |
| 808 |
r = x + w, |
| 809 |
b = y + h, |
| 810 |
panel_for_sprite = panel, |
| 811 |
sprite_index_normal = panel.sprite_index, |
| 812 |
sprite_index_disabled = panel.sprite_index, |
| 813 |
sprite_index_active = sprite, |
| 814 |
on_click = on_click, |
| 815 |
on_click_self = on_click_self or self, |
| 816 |
on_rightclick = on_rightclick, |
| 817 |
enabled = true, |
| 818 |
panel_lowered_normal = false, |
| 819 |
panel_lowered_active = true, |
| 820 |
}, button_mt) |
| 821 |
if self.ui and on_click == self.close then |
| 822 |
button.sound = "no4.wav" |
| 823 |
elseif self.default_button_sound then |
| 824 |
button.sound = self.default_button_sound |
| 825 |
end |
| 826 |
self.buttons[#self.buttons + 1] = button |
| 827 |
return button |
| 828 |
end |
| 829 |
|
| 830 |
--! A window element used to scroll in lists |
| 831 |
class "Scrollbar" |
| 832 |
|
| 833 |
---@type Scrollbar |
| 834 |
local Scrollbar = _G["Scrollbar"] |
| 835 |
|
| 836 |
--!dummy |
| 837 |
function Scrollbar:Scrollbar() |
| 838 |
self.base = nil |
| 839 |
self.slider = nil |
| 840 |
self.min_value = nil |
| 841 |
self.max_value = nil |
| 842 |
self.value = nil |
| 843 |
self.page_size = nil |
| 844 |
self.direction = nil |
| 845 |
self.visible = nil |
| 846 |
end |
| 847 |
|
| 848 |
function Scrollbar:setRange(min_value, max_value, page_size, value) |
| 849 |
page_size = math.min(page_size, max_value - min_value + 1) -- page size must be number of elements at most |
| 850 |
value = math.min(value or min_value, math.max(min_value, max_value - page_size + 1)) |
| 851 |
|
| 852 |
self.min_value = min_value |
| 853 |
self.max_value = max_value |
| 854 |
self.page_size = page_size |
| 855 |
self.value = value |
| 856 |
|
| 857 |
local slider = self.slider |
| 858 |
slider.w = slider.max_w |
| 859 |
slider.h = slider.max_h |
| 860 |
slider.max_x = slider.min_x + slider.max_w - slider.w |
| 861 |
slider.max_y = slider.min_y + slider.max_h - slider.h |
| 862 |
|
| 863 |
if self.direction == "y" then |
| 864 |
slider.h = math.ceil((page_size / (max_value - min_value + 1)) * slider.max_h) |
| 865 |
slider.max_y = slider.min_y + slider.max_h - slider.h |
| 866 |
slider.y = math.floor((value - min_value) / (max_value - min_value - page_size + 2) * (slider.max_y - slider.min_y) + slider.min_y) |
| 867 |
else |
| 868 |
slider.w = math.ceil((page_size / (max_value - min_value + 1)) * slider.max_w) |
| 869 |
slider.max_x = slider.min_x + slider.max_w - slider.w |
| 870 |
slider.x = math.floor((value - min_value) / (max_value - min_value - page_size + 2) * (slider.max_x - slider.min_x) + slider.min_x) |
| 871 |
end |
| 872 |
|
| 873 |
return self |
| 874 |
end |
| 875 |
|
| 876 |
--! Get the pixel position of the slider in the axis which the slider can move |
| 877 |
function Scrollbar:getXorY() |
| 878 |
return self.slider[self.direction] |
| 879 |
end |
| 880 |
|
| 881 |
--! Set the pixel position of the slider in the axis which the slider can move |
| 882 |
function Scrollbar:setXorY(xy) |
| 883 |
local dir = self.direction |
| 884 |
local min, max |
| 885 |
if dir == "x" then |
| 886 |
min = self.slider.min_x |
| 887 |
max = self.slider.max_x |
| 888 |
else |
| 889 |
min = self.slider.min_y |
| 890 |
max = self.slider.max_y |
| 891 |
end |
| 892 |
if xy < min then |
| 893 |
xy = min |
| 894 |
end |
| 895 |
if xy > max then |
| 896 |
xy = max |
| 897 |
end |
| 898 |
self.slider[dir] = xy |
| 899 |
local old_value = self.value |
| 900 |
self.value = math.floor(((xy - min) / (max - min + 1)) * (self.max_value - self.min_value - self.page_size + 2)) + 1 |
| 901 |
if old_value ~= self.value then |
| 902 |
self.callback() |
| 903 |
end |
| 904 |
end |
| 905 |
|
| 906 |
local scrollbar_mt = permanent("Window.<scrollbar_mt>", getmetatable(Scrollbar())) |
| 907 |
|
| 908 |
--[[ Convert a static panel into a scrollbar. |
| 909 |
! Scrollbars consist of a base panel (the panel given as a parameter) |
| 910 |
and an additional slider panel (automatically created BevelPanel). |
| 911 |
!param panel (panel) The panel that will serve as the scrollbar base. |
| 912 |
!param slider_colour (colour in form .red, .green and .blue) The colour for the slider. |
| 913 |
!param callback (function) Function that is called whenever the slider position changes. |
| 914 |
!param min_value (integer) The minimum value the scrollbar can represent. |
| 915 |
!param max_value (integer) The maximum value the scrollbar can represent. |
| 916 |
!param page_size (integer) The amount of objects represented on one page. |
| 917 |
!param value (integer, nil) The current value, or min_value if not specified. |
| 918 |
]] |
| 919 |
function Window:makeScrollbarOnPanel(panel, slider_colour, callback, min_value, max_value, page_size, value) |
| 920 |
local slider = self:addBevelPanel(panel.x + 1, panel.y + 1, panel.w - 2, panel.h - 2, slider_colour) |
| 921 |
local scrollbar = setmetatable({ |
| 922 |
base = panel, |
| 923 |
slider = slider, |
| 924 |
direction = "y", |
| 925 |
callback = callback, |
| 926 |
visible = true, |
| 927 |
enabled = true, |
| 928 |
}, scrollbar_mt) |
| 929 |
slider.min_x = slider.x |
| 930 |
slider.min_y = slider.y |
| 931 |
slider.max_w = slider.w |
| 932 |
slider.max_h = slider.h |
| 933 |
scrollbar:setRange(min_value, max_value, page_size, value) |
| 934 |
self.scrollbars[#self.scrollbars + 1] = scrollbar |
| 935 |
|
| 936 |
return scrollbar |
| 937 |
end |
| 938 |
|
| 939 |
--! A window element used to enter text |
| 940 |
class "Textbox" |
| 941 |
|
| 942 |
---@type Textbox |
| 943 |
local Textbox = _G["Textbox"] |
| 944 |
|
| 945 |
--!dummy |
| 946 |
function Textbox:Textbox() |
| 947 |
self.panel = nil |
| 948 |
self.confirm_callback = nil |
| 949 |
self.abort_callback = nil |
| 950 |
self.button = nil |
| 951 |
self.text = nil |
| 952 |
self.allowed_input = nil |
| 953 |
self.char_limit = nil |
| 954 |
self.visible = nil |
| 955 |
self.enabled = nil |
| 956 |
self.active = nil |
| 957 |
self.cursor_counter = nil |
| 958 |
self.cursor_state = nil |
| 959 |
self.cursor_pos = nil |
| 960 |
end |
| 961 |
|
| 962 |
local textbox_mt = permanent("Window.<textbox_mt>", getmetatable(Textbox())) |
| 963 |
|
| 964 |
function Textbox:onTick() |
| 965 |
if self.active then |
| 966 |
self.cursor_counter = self.cursor_counter + 1 |
| 967 |
if self.cursor_counter >= 40 then |
| 968 |
self.cursor_state = true |
| 969 |
self.cursor_counter = self.cursor_counter - 40 |
| 970 |
elseif self.cursor_counter >= 20 then |
| 971 |
self.cursor_state = false |
| 972 |
end |
| 973 |
end |
| 974 |
end |
| 975 |
|
| 976 |
function Textbox:drawCursor(canvas, x, y) |
| 977 |
if self.cursor_state then |
| 978 |
local col = TheApp.video:mapRGB(255, 255, 255) |
| 979 |
local cursor_y, cursor_x = self.panel:drawLabel(nil, x, y, self.cursor_pos) |
| 980 |
local w, _ = self.panel.label_font:sizeOf("0") |
| 981 |
if self.panel.align == nil or self.panel.align == "center" then |
| 982 |
local _, text_x = self.panel:drawLabel(nil, x, y, {self.cursor_pos[1], #self.text}) |
| 983 |
cursor_x = text_x - (text_x - cursor_x) * 2 |
| 984 |
end |
| 985 |
cursor_y = cursor_y - 3 |
| 986 |
-- Add x separation, but only if there was actually some text in this line. |
| 987 |
if self.text[self.cursor_pos[1]] ~= "" then |
| 988 |
cursor_x = cursor_x + 1 |
| 989 |
end |
| 990 |
canvas:drawRect(col, cursor_x, cursor_y, w, 1) |
| 991 |
end |
| 992 |
end |
| 993 |
|
| 994 |
--! Set the box to not active and run confirm callback, if any |
| 995 |
function Textbox:confirm() |
| 996 |
self:setActive(false) |
| 997 |
if self.confirm_callback then |
| 998 |
self.confirm_callback() |
| 999 |
end |
| 1000 |
end |
| 1001 |
|
| 1002 |
--! Set the box to not active and run abort callback, if any |
| 1003 |
function Textbox:abort() |
| 1004 |
self:setActive(false) |
| 1005 |
if self.abort_callback then |
| 1006 |
self.abort_callback() |
| 1007 |
end |
| 1008 |
end |
| 1009 |
|
| 1010 |
--! Set the textbox active status to true or false, taking care of any |
| 1011 |
-- additional things that need to be done: deactivate any other textboxes, |
| 1012 |
-- handle blinking cursor, keyboard repeat on/off, set button state accordingly |
| 1013 |
--!param active (boolean) whether to activate (true) or deactivate (false) the box |
| 1014 |
function Textbox:setActive(active) |
| 1015 |
local ui = self.panel.window.ui |
| 1016 |
if active then |
| 1017 |
-- Unselect any other textbox |
| 1018 |
for _, textbox in ipairs(ui.textboxes) do |
| 1019 |
if textbox ~= self and textbox.active then |
| 1020 |
textbox:abort() |
| 1021 |
end |
| 1022 |
end |
| 1023 |
self.cursor_counter = 0 |
| 1024 |
self.cursor_state = true |
| 1025 |
self.cursor_pos[1] = type(self.text) == "table" and #self.text or 1 |
| 1026 |
self.cursor_pos[2] = type(self.text) == "table" and string.len(self.text[#self.text]) or string.len(self.text) |
| 1027 |
-- Update text |
| 1028 |
self.panel:setLabel(self.text) |
| 1029 |
else |
| 1030 |
self.cursor_state = false |
| 1031 |
end |
| 1032 |
|
| 1033 |
self.active = active |
| 1034 |
-- Update button if necessary |
| 1035 |
if self.button.toggled ~= active then |
| 1036 |
self.button:toggle() |
| 1037 |
end |
| 1038 |
end |
| 1039 |
|
| 1040 |
function Textbox:clicked() |
| 1041 |
local active = self.button.toggled |
| 1042 |
if active then |
| 1043 |
self:setActive(true) |
| 1044 |
else |
| 1045 |
if self.text == "" then |
| 1046 |
self:abort() |
| 1047 |
else |
| 1048 |
self:confirm() |
| 1049 |
end |
| 1050 |
end |
| 1051 |
end |
| 1052 |
|
| 1053 |
--! Handles special characters such as Enter. Normal text input is processed in the textInput function. |
| 1054 |
--! Note though that this function still returns true if it appears to be a characters being entered. |
| 1055 |
function Textbox:keyInput(char, rawchar) |
| 1056 |
if not self.active then |
| 1057 |
return false |
| 1058 |
end |
| 1059 |
local ui = self.panel.window.ui |
| 1060 |
local line = type(self.text) == "table" and self.text[self.cursor_pos[1]] or self.text |
| 1061 |
local pat = "%a%d_" -- which characters form "words" for ctrl+[left/right/backspace/delete] |
| 1062 |
local new_line |
| 1063 |
local handled = false |
| 1064 |
if not self.char_limit or string.len(line) < self.char_limit then |
| 1065 |
-- Upper- and lowercase letters |
| 1066 |
if self.allowed_input.alpha then |
| 1067 |
if #char == 1 and string.match(char, '%a') then |
| 1068 |
handled = true |
| 1069 |
end |
| 1070 |
end |
| 1071 |
-- Numbers |
| 1072 |
if not handled and self.allowed_input.numbers then |
| 1073 |
if #char == 1 and string.match(char, '%d') then |
| 1074 |
handled = true |
| 1075 |
end |
| 1076 |
end |
| 1077 |
-- Space, hyphen and plus sign |
| 1078 |
if not handled and self.allowed_input.misc then |
| 1079 |
if char == "space" or char == "-" or char == "+" then |
| 1080 |
handled = true |
| 1081 |
end |
| 1082 |
end |
| 1083 |
end |
| 1084 |
-- Backspace (delete last char, or last word if ctrl is pressed) |
| 1085 |
if not handled and char == "backspace" then |
| 1086 |
if self.cursor_pos[2] == 0 then |
| 1087 |
if type(self.text) == "table" and self.cursor_pos[1] > 1 then |
| 1088 |
table.remove(self.text, self.cursor_pos[1]) |
| 1089 |
self.cursor_pos[1] = self.cursor_pos[1] - 1 |
| 1090 |
self.cursor_pos[2] = string.len(self.text[self.cursor_pos[1]]) |
| 1091 |
new_line = self.text[self.cursor_pos[1]] .. line |
| 1092 |
end |
| 1093 |
else |
| 1094 |
local pos = self.cursor_pos[2] - 1 |
| 1095 |
if ui.app.key_modifiers.ctrl then |
| 1096 |
pos = string.find(string.sub(line, 1, self.cursor_pos[2]), |
| 1097 |
"[^" .. pat .. "][" .. pat .. "]+[^" .. pat .. "]*$") or 0 |
| 1098 |
end |
| 1099 |
new_line = line:sub(1, pos) .. line:sub(self.cursor_pos[2] + 1, -1) |
| 1100 |
self.cursor_pos[2] = pos |
| 1101 |
end |
| 1102 |
handled = true |
| 1103 |
end |
| 1104 |
-- Delete (delete next char, or next word if ctrl is pressed) |
| 1105 |
if not handled and char == "delete" then |
| 1106 |
if self.cursor_pos[2] == string.len(line) then |
| 1107 |
if type(self.text) == "table" and self.cursor_pos[1] < #self.text then |
| 1108 |
new_line = line .. self.text[self.cursor_pos[1] + 1] |
| 1109 |
table.remove(self.text, self.cursor_pos[1] + 1) |
| 1110 |
end |
| 1111 |
else |
| 1112 |
local pos = self.cursor_pos[2] + 2 |
| 1113 |
if ui.app.key_modifiers.ctrl then |
| 1114 |
pos = (string.find(line, "[^" .. pat .. "][" .. pat .. "]", |
| 1115 |
self.cursor_pos[2] + 1) or string.len(line)) + 1 |
| 1116 |
end |
| 1117 |
new_line = line:sub(1, self.cursor_pos[2]) .. line:sub(pos, -1) |
| 1118 |
end |
| 1119 |
handled = true |
| 1120 |
end |
| 1121 |
-- Enter (newline or confirm) |
| 1122 |
if not handled and (char == "return" or char == "enter") then |
| 1123 |
if type(self.text) == "table" then |
| 1124 |
local remainder = line:sub(self.cursor_pos[2] + 1, -1) |
| 1125 |
self.text[self.cursor_pos[1]] = line:sub(1, self.cursor_pos[2]) |
| 1126 |
table.insert(self.text, self.cursor_pos[1] + 1, remainder) |
| 1127 |
self.cursor_pos[1] = self.cursor_pos[1] + 1 |
| 1128 |
self.cursor_pos[2] = 0 |
| 1129 |
handled = true |
| 1130 |
else |
| 1131 |
self:confirm() |
| 1132 |
return true |
| 1133 |
end |
| 1134 |
end |
| 1135 |
-- Escape (abort) |
| 1136 |
if not handled and char == "escape" then |
| 1137 |
self:abort() |
| 1138 |
return true |
| 1139 |
end |
| 1140 |
-- Arrow keys |
| 1141 |
if not handled then |
| 1142 |
if char == "up" then -- up |
| 1143 |
if type(self.text) ~= "table" or self.cursor_pos[1] == 1 then |
| 1144 |
-- to beginning of line |
| 1145 |
self.cursor_pos[2] = 0 |
| 1146 |
else |
| 1147 |
-- one line up |
| 1148 |
self.cursor_pos[1] = self.cursor_pos[1] - 1 |
| 1149 |
self.cursor_pos[2] = math.min(self.cursor_pos[2], string.len(self.text[self.cursor_pos[1]])) |
| 1150 |
end |
| 1151 |
handled = true |
| 1152 |
elseif char == "down" then -- down |
| 1153 |
if type(self.text) ~= "table" or self.cursor_pos[1] == #self.text then |
| 1154 |
-- to end of line |
| 1155 |
self.cursor_pos[2] = string.len(line) |
| 1156 |
else |
| 1157 |
-- one line down |
| 1158 |
self.cursor_pos[1] = self.cursor_pos[1] + 1 |
| 1159 |
self.cursor_pos[2] = math.min(self.cursor_pos[2], string.len(self.text[self.cursor_pos[1]])) |
| 1160 |
end |
| 1161 |
handled = true |
| 1162 |
elseif char == "right" then -- right |
| 1163 |
if self.cursor_pos[2] == string.len(line) then |
| 1164 |
-- next line |
| 1165 |
if type(self.text) == "table" and self.cursor_pos[1] < #self.text then |
| 1166 |
self.cursor_pos[1] = self.cursor_pos[1] + 1 |
| 1167 |
self.cursor_pos[2] = 0 |
| 1168 |
end |
| 1169 |
else |
| 1170 |
if ui.app.key_modifiers.ctrl then |
| 1171 |
-- to the right until next word or end of line |
| 1172 |
self.cursor_pos[2] = string.find(line, "[^" .. pat .. "][" .. pat .. "]", |
| 1173 |
self.cursor_pos[2] + 1) or string.len(line) |
| 1174 |
else |
| 1175 |
-- one to the right |
| 1176 |
self.cursor_pos[2] = self.cursor_pos[2] + 1 |
| 1177 |
end |
| 1178 |
end |
| 1179 |
handled = true |
| 1180 |
elseif char == "left" then -- left |
| 1181 |
if self.cursor_pos[2] == 0 then |
| 1182 |
-- previous line |
| 1183 |
if type(self.text) == "table" and self.cursor_pos[1] > 1 then |
| 1184 |
self.cursor_pos[1] = self.cursor_pos[1] - 1 |
| 1185 |
self.cursor_pos[2] = string.len(self.text[self.cursor_pos[1]]) |
| 1186 |
end |
| 1187 |
else |
| 1188 |
if ui.app.key_modifiers.ctrl then |
| 1189 |
-- to the left until beginning of word or beginning of line |
| 1190 |
self.cursor_pos[2] = string.find(string.sub(line, 1, self.cursor_pos[2]), |
| 1191 |
"[^" .. pat .. "][" .. pat .. "]+[^" .. pat .. "]*$") or 0 |
| 1192 |
else |
| 1193 |
-- one to the left |
| 1194 |
self.cursor_pos[2] = self.cursor_pos[2] - 1 |
| 1195 |
end |
| 1196 |
end |
| 1197 |
handled = true |
| 1198 |
end |
| 1199 |
end |
| 1200 |
-- Tab (reserved) |
| 1201 |
if not handled and char == "tab" then |
| 1202 |
handled = true |
| 1203 |
end |
| 1204 |
-- Home (beginning of line) |
| 1205 |
if not handled and char == "home" then |
| 1206 |
self.cursor_pos[2] = 0 |
| 1207 |
handled = true |
| 1208 |
end |
| 1209 |
-- End (end of line) |
| 1210 |
if not handled and char == "end" then |
| 1211 |
self.cursor_pos[2] = string.len(line) |
| 1212 |
handled = true |
| 1213 |
end |
| 1214 |
if not self.char_limit or string.len(self.text) < self.char_limit then |
| 1215 |
-- Experimental "all" category |
| 1216 |
if not handled and self.allowed_input.all and |
| 1217 |
not (char == "shift" or char == "ctrl" or char == "alt") then -- F-Keys |
| 1218 |
handled = true |
| 1219 |
end |
| 1220 |
end |
| 1221 |
if new_line then |
| 1222 |
if type(self.text) == "table" then |
| 1223 |
self.text[self.cursor_pos[1]] = new_line |
| 1224 |
else |
| 1225 |
self.text = new_line |
| 1226 |
end |
| 1227 |
-- update label |
| 1228 |
self.panel:setLabel(self.text) |
| 1229 |
end |
| 1230 |
-- make cursor visible |
| 1231 |
self.cursor_counter = 0 |
| 1232 |
self.cursor_state = true |
| 1233 |
return handled |
| 1234 |
end |
| 1235 |
|
| 1236 |
--! Handles actual text input. |
| 1237 |
function Textbox:textInput(text) |
| 1238 |
if not self.active then |
| 1239 |
return false |
| 1240 |
end |
| 1241 |
|
| 1242 |
local line = type(self.text) == "table" and self.text[self.cursor_pos[1]] or self.text |
| 1243 |
local new_line = line:sub(1, self.cursor_pos[2]) .. text .. line:sub(self.cursor_pos[2] + 1, -1) |
| 1244 |
self.cursor_pos[2] = self.cursor_pos[2] + #text |
| 1245 |
|
| 1246 |
if type(self.text) == "table" then |
| 1247 |
self.text[self.cursor_pos[1]] = new_line |
| 1248 |
else |
| 1249 |
self.text = new_line |
| 1250 |
end |
| 1251 |
|
| 1252 |
-- update label |
| 1253 |
self.panel:setLabel(self.text) |
| 1254 |
end |
| 1255 |
|
| 1256 |
--[[ Limit input handled by textbox to specific classes of characters |
| 1257 |
!param types (string or table) One of, or an table of any number of input types |
| 1258 |
! valid input types are: |
| 1259 |
! "alpha": Letters (lower and uppercase) |
| 1260 |
! "numbers": 0-9 |
| 1261 |
! "misc": other characters, currently space and hyphen |
| 1262 |
! "all": experimental category that allows, theoretically, all input |
| 1263 |
]] |
| 1264 |
function Textbox:allowedInput(types) |
| 1265 |
if type(types) ~= "table" then types = {types} end |
| 1266 |
self.allowed_input = {} |
| 1267 |
for _, t in ipairs(types) do |
| 1268 |
self.allowed_input[t] = true |
| 1269 |
end |
| 1270 |
return self |
| 1271 |
end |
| 1272 |
|
| 1273 |
--[[ Limit input to a maximum of [limit] characters. |
| 1274 |
!param limit (integer or nil) Number of characters until the textbox will stop accepting input, or nil to deactivate limit. |
| 1275 |
]] |
| 1276 |
function Textbox:characterLimit(limit) |
| 1277 |
self.char_limit = limit |
| 1278 |
return self |
| 1279 |
end |
| 1280 |
|
| 1281 |
--[[ Set the text of the textbox to a given string or list of strings. |
| 1282 |
! Use empty string to make textbox a single line textbox (default). |
| 1283 |
! Use table with empty string {""} to make it a multiline textbox. |
| 1284 |
!param text (string or table) The string or list of strings the textbox should contain. |
| 1285 |
]] |
| 1286 |
function Textbox:setText(text) |
| 1287 |
self.text = text |
| 1288 |
self.panel:setLabel(self.text) |
| 1289 |
return self |
| 1290 |
end |
| 1291 |
|
| 1292 |
function Textbox:setPosition(x, y) |
| 1293 |
self.button:setPosition(x, y) |
| 1294 |
end |
| 1295 |
|
| 1296 |
function Textbox:setSize(width, height) |
| 1297 |
self.button:setSize(width, height) |
| 1298 |
end |
| 1299 |
|
| 1300 |
--[[ Convert a static panel into a textbox. |
| 1301 |
! Textboxes consist of the panel given as a parameter, which is made into a |
| 1302 |
ToggleButton automatically, and handle keyboard input while active. |
| 1303 |
!param panel (panel) The panel that will serve as the textbox base. |
| 1304 |
!param confirm_callback (function) The function to call when text is confirmed. |
| 1305 |
!param abort_callback (function) The function to call when entering is aborted. |
| 1306 |
]] |
| 1307 |
function Window:makeTextboxOnPanel(panel, confirm_callback, abort_callback) |
| 1308 |
local textbox = setmetatable({ |
| 1309 |
panel = panel, |
| 1310 |
confirm_callback = confirm_callback, |
| 1311 |
abort_callback = abort_callback, |
| 1312 |
button = nil, -- placeholder |
| 1313 |
text = "", |
| 1314 |
allowed_input = { |
| 1315 |
alpha = true, |
| 1316 |
numbers = true, |
| 1317 |
misc = true, |
| 1318 |
}, |
| 1319 |
char_limit = nil, |
| 1320 |
visible = true, |
| 1321 |
enabled = true, |
| 1322 |
active = false, |
| 1323 |
cursor_counter = 0, |
| 1324 |
cursor_state = false, |
| 1325 |
cursor_pos = {1, 1}, |
| 1326 |
}, textbox_mt) |
| 1327 |
|
| 1328 |
local button = panel:makeToggleButton(0, 0, panel.w, panel.h, nil, textbox.clicked, textbox) |
| 1329 |
textbox.button = button |
| 1330 |
|
| 1331 |
self.textboxes[#self.textboxes + 1] = textbox |
| 1332 |
self.ui:registerTextBox(textbox) |
| 1333 |
return textbox |
| 1334 |
end |
| 1335 |
|
| 1336 |
--! A window element used to accept hotkey configurations. |
| 1337 |
class "HotkeyBox" |
| 1338 |
|
| 1339 |
---@type HotkeyBox |
| 1340 |
local HotkeyBox = _G["HotkeyBox"] |
| 1341 |
|
| 1342 |
--!dummy |
| 1343 |
function HotkeyBox:HotkeyBox() |
| 1344 |
self.panel = nil |
| 1345 |
self.confirm_callback = nil |
| 1346 |
self.abort_callback = nil |
| 1347 |
self.button = nil |
| 1348 |
self.text = nil |
| 1349 |
self.allowed_input = nil |
| 1350 |
self.char_limit = nil |
| 1351 |
self.visible = nil |
| 1352 |
self.enabled = nil |
| 1353 |
self.active = nil |
| 1354 |
self.noted_keys = {} |
| 1355 |
end |
| 1356 |
|
| 1357 |
local hotkeybox_mt = permanent("Window.<hotkeybox_mt>", getmetatable(HotkeyBox())) |
| 1358 |
|
| 1359 |
--! Set the box to not active and run confirm callback, if any |
| 1360 |
function HotkeyBox:confirm() |
| 1361 |
self:setActive(false) |
| 1362 |
if self.confirm_callback then |
| 1363 |
self.confirm_callback() |
| 1364 |
end |
| 1365 |
self.noted_keys = {} |
| 1366 |
end |
| 1367 |
|
| 1368 |
--! Set the box to not active and run abort callback, if any |
| 1369 |
function HotkeyBox:abort() |
| 1370 |
self:setActive(false) |
| 1371 |
if self.abort_callback then |
| 1372 |
self.abort_callback() |
| 1373 |
end |
| 1374 |
self.noted_keys = {} |
| 1375 |
end |
| 1376 |
|
| 1377 |
--! Set the hotkeybox active status to true or false, taking care of any |
| 1378 |
-- additional things that need to be done: deactivate any other hotkeyboxes, |
| 1379 |
-- handle blinking cursor, keyboard repeat on/off, set button state accordingly |
| 1380 |
--!param active (boolean) whether to activate (true) or deactivate (false) the box |
| 1381 |
function HotkeyBox:setActive(active) |
| 1382 |
local ui = self.panel.window.ui |
| 1383 |
if active then |
| 1384 |
-- Unselect any other hotkeybox |
| 1385 |
for _, hotkeybox in ipairs(ui.hotkeyboxes) do |
| 1386 |
if hotkeybox ~= self and hotkeybox.active then |
| 1387 |
hotkeybox:abort() |
| 1388 |
end |
| 1389 |
end |
| 1390 |
|
| 1391 |
-- Update text |
| 1392 |
self.panel:setLabel(self.text) |
| 1393 |
end |
| 1394 |
|
| 1395 |
self.active = active |
| 1396 |
-- Update button if necessary |
| 1397 |
if self.button.toggled ~= active then |
| 1398 |
self.button:toggle() |
| 1399 |
end |
| 1400 |
end |
| 1401 |
|
| 1402 |
function HotkeyBox:clicked() |
| 1403 |
local active = self.button.toggled |
| 1404 |
if active then |
| 1405 |
self:setActive(true) |
| 1406 |
else |
| 1407 |
if self.text == "" then |
| 1408 |
self:abort() |
| 1409 |
else |
| 1410 |
self:confirm() |
| 1411 |
end |
| 1412 |
end |
| 1413 |
end |
| 1414 |
|
| 1415 |
function HotkeyBox:keyInput(char, rawchar, modifiers) |
| 1416 |
if not self.active then |
| 1417 |
return false |
| 1418 |
end |
| 1419 |
-- Just return true so any key's we press aren't passed to anything else. |
| 1420 |
return true |
| 1421 |
end |
| 1422 |
|
| 1423 |
--[[ Limit input handled by textbox to specific classes of characters |
| 1424 |
!param types (string or table) One of, or an table of any number of input types |
| 1425 |
! valid input types are: |
| 1426 |
! "alpha": Letters (lower and uppercase) |
| 1427 |
! "numbers": 0-9 |
| 1428 |
! "misc": other characters, currently space and hyphen |
| 1429 |
! "all": experimental category that allows, theoretically, all input |
| 1430 |
]] |
| 1431 |
function HotkeyBox:allowedInput(types) |
| 1432 |
if type(types) ~= "table" then types = {types} end |
| 1433 |
self.allowed_input = {} |
| 1434 |
for _, t in ipairs(types) do |
| 1435 |
self.allowed_input[t] = true |
| 1436 |
end |
| 1437 |
return self |
| 1438 |
end |
| 1439 |
|
| 1440 |
--[[ Set the text of the textbox to a given string or list of strings. |
| 1441 |
! Use empty string to make textbox a single line textbox (default). |
| 1442 |
! Use table with empty string {""} to make it a multiline textbox. |
| 1443 |
!param text (string or table) The string or list of strings the textbox should contain. |
| 1444 |
]] |
| 1445 |
function HotkeyBox:setText(text) |
| 1446 |
self.text = text |
| 1447 |
self.panel:setLabel(self.text) |
| 1448 |
return self |
| 1449 |
end |
| 1450 |
|
| 1451 |
function HotkeyBox:setPosition(x, y) |
| 1452 |
self.button:setPosition(x, y) |
| 1453 |
end |
| 1454 |
|
| 1455 |
function HotkeyBox:setSize(width, height) |
| 1456 |
self.button:setSize(width, height) |
| 1457 |
end |
| 1458 |
|
| 1459 |
--================================ |
| 1460 |
--================================ |
| 1461 |
--================================ |
| 1462 |
|
| 1463 |
--[[ Convert a static panel into a hotkeybox. |
| 1464 |
! hotkeyboxes consist of the panel given as a parameter, which is made into a |
| 1465 |
ToggleButton automatically, and handle keyboard input while active. |
| 1466 |
!param panel (panel) The panel that will serve as the hotkeybox base. |
| 1467 |
!param confirm_callback (function) The function to call when text is confirmed. |
| 1468 |
!param abort_callback (function) The function to call when entering is aborted. |
| 1469 |
]] |
| 1470 |
function Window:makeHotkeyBoxOnPanel(panel, confirm_callback, abort_callback) |
| 1471 |
local hotkeybox = setmetatable({ |
| 1472 |
panel = panel, |
| 1473 |
confirm_callback = confirm_callback, |
| 1474 |
abort_callback = abort_callback, |
| 1475 |
button = nil, -- placeholder |
| 1476 |
text = "", |
| 1477 |
allowed_input = { |
| 1478 |
alpha = true, |
| 1479 |
numbers = true, |
| 1480 |
misc = true, |
| 1481 |
}, |
| 1482 |
char_limit = nil, |
| 1483 |
visible = true, |
| 1484 |
enabled = true, |
| 1485 |
active = false, |
| 1486 |
noted_keys = {}, |
| 1487 |
}, hotkeybox_mt) |
| 1488 |
|
| 1489 |
local button = panel:makeToggleButton(0, 0, panel.w, panel.h, nil, hotkeybox.clicked, hotkeybox) |
| 1490 |
hotkeybox.button = button |
| 1491 |
|
| 1492 |
self.hotkeyboxes[#self.hotkeyboxes + 1] = hotkeybox |
| 1493 |
self.ui:registerHotkeyBox(hotkeybox) |
| 1494 |
return hotkeybox |
| 1495 |
end |
| 1496 |
|
| 1497 |
-- Return the X and Y coordinates of the window as drawn on |
| 1498 |
-- the screen after factoring in the ui_scale. |
| 1499 |
function Window:getRealXY() |
| 1500 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1501 |
return self.x * s, self.y * s |
| 1502 |
end |
| 1503 |
|
| 1504 |
function Window:draw(canvas, x, y) |
| 1505 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1506 |
x, y = x + self.x * s, y + self.y * s |
| 1507 |
if self.panels[1] then |
| 1508 |
local panel_sprites = self.panel_sprites |
| 1509 |
local panel_sprites_draw = panel_sprites and panel_sprites.draw |
| 1510 |
for _, panel in ipairs(self.panels) do |
| 1511 |
if panel.visible then |
| 1512 |
if panel.custom_draw then |
| 1513 |
panel:custom_draw(canvas, x, y) |
| 1514 |
else |
| 1515 |
panel_sprites_draw( |
| 1516 |
panel_sprites, |
| 1517 |
canvas, |
| 1518 |
panel.sprite_index, |
| 1519 |
x + panel.x * s, |
| 1520 |
y + panel.y * s, |
| 1521 |
{ flags = panel.draw_flags, scaleFactor = s }) |
| 1522 |
end |
| 1523 |
end |
| 1524 |
end |
| 1525 |
end |
| 1526 |
if not class.is(self, UI) then -- prevent UI (sub)class from handling the textboxes too |
| 1527 |
for _, box in ipairs(self.textboxes) do |
| 1528 |
box:drawCursor(canvas, x, y) |
| 1529 |
end |
| 1530 |
end |
| 1531 |
if not class.is(self, UI) then -- prevent UI (sub)class from handling the hotkeyboxes too |
| 1532 |
for _, _ in ipairs(self.hotkeyboxes) do |
| 1533 |
-- Empty right now. Don't know if I need it. -- LEIGET |
| 1534 |
end |
| 1535 |
end |
| 1536 |
if self.windows then |
| 1537 |
local windows = self.windows |
| 1538 |
for i = #windows, 1, -1 do |
| 1539 |
if windows[i].visible then |
| 1540 |
windows[i]:draw(canvas, x, y) |
| 1541 |
end |
| 1542 |
end |
| 1543 |
end |
| 1544 |
end |
| 1545 |
|
| 1546 |
function Window:onChangeLanguage() |
| 1547 |
if self.windows then |
| 1548 |
for _, window in ipairs(self.windows) do |
| 1549 |
window:onChangeLanguage() |
| 1550 |
end |
| 1551 |
end |
| 1552 |
end |
| 1553 |
|
| 1554 |
function Window:onCursorWorldPositionChange(x, y) |
| 1555 |
local repaint = false |
| 1556 |
if self.windows then |
| 1557 |
for _, window in ipairs(self.windows) do |
| 1558 |
if window:onCursorWorldPositionChange(x - window.x, y - window.y) then |
| 1559 |
repaint = true |
| 1560 |
end |
| 1561 |
end |
| 1562 |
end |
| 1563 |
return repaint |
| 1564 |
end |
| 1565 |
|
| 1566 |
function Window:hitTestPanel(x, y, panel) |
| 1567 |
local s = panel.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1568 |
local xpos, ypos = x - panel.x * s, y - panel.y * s |
| 1569 |
if panel.visible and xpos >= 0 and ypos >= 0 then |
| 1570 |
if panel.w and panel.h then |
| 1571 |
if xpos <= panel.w * s and ypos <= panel.h * s then |
| 1572 |
return true |
| 1573 |
end |
| 1574 |
else |
| 1575 |
if self.panel_sprites:hitTest(panel.sprite_index, math.floor(xpos / s), math.floor(ypos / s)) then |
| 1576 |
return true |
| 1577 |
end |
| 1578 |
end |
| 1579 |
end |
| 1580 |
return false |
| 1581 |
end |
| 1582 |
|
| 1583 |
--[[ Used to test if cursor is currently hovering over certain area in dialog. |
| 1584 |
!param active_hover (bool) Is cursor already hovers over it? |
| 1585 |
!param mouse_x (integer) Mouse X coordinate |
| 1586 |
!param mouse_y (integer) Mouse Y coordinate |
| 1587 |
!param min_x (integer) Minimum X area |
| 1588 |
!param max_x (integer) Maximum X area |
| 1589 |
!param min_y (integer) Minimum Y area |
| 1590 |
!param max_y (integer) Maximum Y area |
| 1591 |
]] |
| 1592 |
function Window:hoverTest(active_hover, mouse_x, mouse_y, min_x, max_x, min_y, max_y) |
| 1593 |
local is_currently_hovering = mouse_x > min_x and mouse_x < max_x and mouse_y > min_y and mouse_y < max_y |
| 1594 |
if is_currently_hovering then |
| 1595 |
if not active_hover then |
| 1596 |
self.ui:playSound("HLight5.wav") |
| 1597 |
end |
| 1598 |
return true |
| 1599 |
end |
| 1600 |
return false |
| 1601 |
end |
| 1602 |
|
| 1603 |
--[[ Used to test if the window has a (non-transparent) pixel at the given position. |
| 1604 |
!param x (integer) The X coordinate of the pixel to test, relative to the |
| 1605 |
top-left corner of the window. |
| 1606 |
!param y (integer) The Y coordinate of the pixel to test, relative to the |
| 1607 |
top-left corner of the window. |
| 1608 |
]] |
| 1609 |
function Window:hitTest(x, y) |
| 1610 |
if self.panels[1] then |
| 1611 |
for _, panel in ipairs(self.panels) do |
| 1612 |
if self:hitTestPanel(x, y, panel) then |
| 1613 |
return true |
| 1614 |
end |
| 1615 |
end |
| 1616 |
end |
| 1617 |
if self.windows then |
| 1618 |
for _, child in ipairs(self.windows) do |
| 1619 |
local s = child.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1620 |
if child:hitTest(x - child.x * s, y - child.y * s) then |
| 1621 |
return true |
| 1622 |
end |
| 1623 |
end |
| 1624 |
end |
| 1625 |
return false |
| 1626 |
end |
| 1627 |
|
| 1628 |
function Window:onMouseDown(button, x, y) |
| 1629 |
local repaint = false |
| 1630 |
if not self.visible then return false end |
| 1631 |
if self.windows then |
| 1632 |
for _, window in ipairs(self.windows) do |
| 1633 |
local ws = window.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1634 |
if window:onMouseDown(button, x - window.x * ws, y - window.y * ws) then |
| 1635 |
repaint = true |
| 1636 |
break |
| 1637 |
end |
| 1638 |
end |
| 1639 |
end |
| 1640 |
if not repaint and (button == "left" or button == "right") then |
| 1641 |
for _, btn in ipairs(self.buttons) do |
| 1642 |
local bs = btn.panel_for_sprite.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1643 |
if btn.enabled and |
| 1644 |
btn.x * bs <= x and x < btn.r * bs and btn.y * bs <= y and y < btn.b * bs and |
| 1645 |
(button == "left" or btn.on_rightclick ~= nil) then |
| 1646 |
btn.panel_for_sprite.sprite_index = btn.sprite_index_active |
| 1647 |
self.active_button = btn |
| 1648 |
btn.active = true |
| 1649 |
btn.panel_for_sprite.lowered = btn.panel_lowered_active |
| 1650 |
if btn.is_repeat then |
| 1651 |
-- execute callback once, then wait some ticks before repeatedly executing |
| 1652 |
btn:handleClick(button) |
| 1653 |
end |
| 1654 |
self.btn_repeat_delay = 10 |
| 1655 |
repaint = true |
| 1656 |
break |
| 1657 |
end |
| 1658 |
end |
| 1659 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1660 |
for _, bar in ipairs(self.scrollbars) do |
| 1661 |
if bar.enabled and self:hitTestPanel(x, y, bar.slider) then |
| 1662 |
self.active_scrollbar = bar |
| 1663 |
bar.active = true |
| 1664 |
bar.down_x = x / s - bar.slider.x |
| 1665 |
bar.down_y = y / s - bar.slider.y |
| 1666 |
repaint = true |
| 1667 |
break |
| 1668 |
end |
| 1669 |
end |
| 1670 |
end |
| 1671 |
if self:hitTest(x, y) then |
| 1672 |
if button == "left" and not repaint then |
| 1673 |
self:beginDrag(x, y) |
| 1674 |
end |
| 1675 |
repaint = true |
| 1676 |
end |
| 1677 |
|
| 1678 |
if repaint then |
| 1679 |
self:bringToTop() |
| 1680 |
end |
| 1681 |
return repaint |
| 1682 |
end |
| 1683 |
|
| 1684 |
--[[ Get the name of the saved window position group. |
| 1685 |
! When the user drags a window, the new position of the window is saved, and |
| 1686 |
then when any windows in the same group are opened in the future, the position |
| 1687 |
of the new window is set to the saved position. By default, each window class |
| 1688 |
is its own group, but by overriding this method, that can be changed. |
| 1689 |
]] |
| 1690 |
function Window:getSavedWindowPositionName() |
| 1691 |
return class.type(self) |
| 1692 |
end |
| 1693 |
|
| 1694 |
function Window:onMouseUp(button, x, y) |
| 1695 |
local repaint = false |
| 1696 |
|
| 1697 |
if self.dragging then |
| 1698 |
self.ui.drag_mouse_move = nil |
| 1699 |
self.dragging = false |
| 1700 |
local config = self.ui.app.runtime_config |
| 1701 |
if not config.window_position then |
| 1702 |
config.window_position = {} |
| 1703 |
end |
| 1704 |
config = config.window_position |
| 1705 |
local name = self:getSavedWindowPositionName() |
| 1706 |
if not config[name] then |
| 1707 |
config[name] = {} |
| 1708 |
end |
| 1709 |
config = config[name] |
| 1710 |
config.x = self.x_original |
| 1711 |
config.y = self.y_original |
| 1712 |
return false |
| 1713 |
end |
| 1714 |
|
| 1715 |
if self.windows then |
| 1716 |
for _, window in ipairs(self.windows) do |
| 1717 |
local s = window.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1718 |
if window:onMouseUp(button, x - window.x * s, y - window.y * s) then |
| 1719 |
repaint = true |
| 1720 |
break -- Click has been handled. No need to look any further. |
| 1721 |
end |
| 1722 |
end |
| 1723 |
end |
| 1724 |
|
| 1725 |
if button == "left" or button == "right" then |
| 1726 |
local btn = self.active_button |
| 1727 |
if btn then |
| 1728 |
local bs = btn.panel_for_sprite.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1729 |
btn.panel_for_sprite.sprite_index = btn.sprite_index_normal |
| 1730 |
btn.active = false |
| 1731 |
btn.panel_for_sprite.lowered = btn.panel_lowered_normal |
| 1732 |
self.active_button = false |
| 1733 |
self.btn_repeat_delay = nil |
| 1734 |
if btn.enabled and not btn.is_repeat and btn.x * bs <= x and x < btn.r * bs and btn.y * bs <= y and y < btn.b * bs then |
| 1735 |
btn:handleClick(button) |
| 1736 |
end |
| 1737 |
repaint = true |
| 1738 |
end |
| 1739 |
local bar = self.active_scrollbar |
| 1740 |
if bar then |
| 1741 |
self.active_scrollbar = nil |
| 1742 |
bar.active = false |
| 1743 |
bar.down_x = nil |
| 1744 |
bar.down_y = nil |
| 1745 |
end |
| 1746 |
end |
| 1747 |
|
| 1748 |
return repaint |
| 1749 |
end |
| 1750 |
|
| 1751 |
--! This function can be used to control mousewheel input (i.e. scrolling). |
| 1752 |
--! Override this function in derived classes, with what you'd like to happen |
| 1753 |
--! on this event. |
| 1754 |
--!param x (int) Mousewheel has moved on the horizontal axis (-1 is |
| 1755 |
-- leftward movement, +1 is rightward movement) |
| 1756 |
--!param y (int) Mousewheel has moved on the vertical axis (-1 is downward |
| 1757 |
-- movement, +1 is upward movement) |
| 1758 |
--!return repaint (boolean) if screen is to be redrawn. |
| 1759 |
function Window:onMouseWheel(x, y) |
| 1760 |
local repaint = false |
| 1761 |
if self.windows then |
| 1762 |
for _, window in ipairs(self.windows) do |
| 1763 |
if window:onMouseWheel(x, y) then |
| 1764 |
repaint = true |
| 1765 |
break -- Scroll has been handled. No need to look any further. |
| 1766 |
end |
| 1767 |
end |
| 1768 |
end |
| 1769 |
return repaint |
| 1770 |
end |
| 1771 |
|
| 1772 |
local --[[persistable:window_drag_position_representation]] function getNicestPositionRepresentation(pos, scale, size, dim_size) |
| 1773 |
if size == dim_size then |
| 1774 |
return 0.5 |
| 1775 |
end |
| 1776 |
|
| 1777 |
local left_rel = pos |
| 1778 |
local right_rel = pos + size * scale - dim_size |
| 1779 |
local rel = pos / (dim_size - size * scale) |
| 1780 |
if 0.15 < rel and rel < 0.85 then |
| 1781 |
return rel |
| 1782 |
end |
| 1783 |
if left_rel <= 0 then |
| 1784 |
return 0 |
| 1785 |
end |
| 1786 |
if right_rel >= 0 then |
| 1787 |
return -0.1 |
| 1788 |
end |
| 1789 |
if left_rel <= -right_rel then |
| 1790 |
return left_rel / scale |
| 1791 |
else |
| 1792 |
return right_rel / scale |
| 1793 |
end |
| 1794 |
end |
| 1795 |
|
| 1796 |
--[[ Initiate dragging of the window. |
| 1797 |
!param x The X position of the cursor in window coordinates. |
| 1798 |
!param y The Y position of the cursor in window coordinates. |
| 1799 |
]] |
| 1800 |
function Window:beginDrag(x, y) |
| 1801 |
if not self.width or not self.height or not self.ui or |
| 1802 |
self.ui.app.runtime_config.lock_windows or not self.draggable then |
| 1803 |
-- Need width, height and UI to do a drag |
| 1804 |
return false |
| 1805 |
end |
| 1806 |
|
| 1807 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1808 |
self.dragging = true |
| 1809 |
self.ui.drag_mouse_move = --[[persistable:window_drag_mouse_move]] function (sx, sy) |
| 1810 |
-- sx and sy are cursor screen co-ords. Convert to window's new abs co-ords |
| 1811 |
sx = sx - x |
| 1812 |
sy = sy - y |
| 1813 |
-- Calculate best positioning |
| 1814 |
local w, h = TheApp.config.width, TheApp.config.height |
| 1815 |
if TheApp.key_modifiers.ctrl then |
| 1816 |
local px = round(sx / (w - self.width * s), 0.1) |
| 1817 |
local py = round(sy / (h - self.height * s), 0.1) |
| 1818 |
if px >= 1 then |
| 1819 |
px = -0.1 |
| 1820 |
elseif px < 0 then |
| 1821 |
px = 0 |
| 1822 |
end |
| 1823 |
if py >= 1 then |
| 1824 |
py = -0.1 |
| 1825 |
elseif py < 0 then |
| 1826 |
py = 0 |
| 1827 |
end |
| 1828 |
self:setPosition(px, py) |
| 1829 |
else |
| 1830 |
local px = getNicestPositionRepresentation(sx, s, self.width, w) |
| 1831 |
local py = getNicestPositionRepresentation(sy, s, self.height, h) |
| 1832 |
self:setPosition(px, py) |
| 1833 |
end |
| 1834 |
end |
| 1835 |
return true |
| 1836 |
end |
| 1837 |
|
| 1838 |
--[[ Called when the user moves the mouse. |
| 1839 |
!param x (integer) The new X coordinate of the cursor, relative to the top-left |
| 1840 |
corner of the window. |
| 1841 |
!param y (integer) The new Y coordinate of the cursor, relative to the top-left |
| 1842 |
corner of the window. |
| 1843 |
!param dx (integer) The number of pixels which the cursor moved horizontally. |
| 1844 |
!param dy (integer) The number of pixels which the cursor moved vertically. |
| 1845 |
]] |
| 1846 |
function Window:onMouseMove(x, y, dx, dy) |
| 1847 |
local repaint = false |
| 1848 |
|
| 1849 |
self.cursor_x = x |
| 1850 |
self.cursor_y = y |
| 1851 |
|
| 1852 |
if self.windows then |
| 1853 |
for _, window in ipairs(self.windows) do |
| 1854 |
local s = window.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1855 |
if window:onMouseMove(x - window.x * s, y - window.y * s, dx, dy) then |
| 1856 |
repaint = true |
| 1857 |
end |
| 1858 |
end |
| 1859 |
end |
| 1860 |
|
| 1861 |
if self.active_button then |
| 1862 |
local btn = self.active_button |
| 1863 |
local index = btn.sprite_index_blink or btn.sprite_index_normal |
| 1864 |
local bs = btn.panel_for_sprite.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1865 |
if btn.x * bs <= x and x < btn.r * bs and btn.y * bs <= y and y < btn.b * bs then |
| 1866 |
index = btn.sprite_index_active |
| 1867 |
self.active_button.active = true |
| 1868 |
btn.panel_for_sprite.lowered = btn.panel_lowered_active |
| 1869 |
else |
| 1870 |
self.active_button.active = false |
| 1871 |
btn.panel_for_sprite.lowered = btn.panel_lowered_normal |
| 1872 |
for _, button in ipairs(self.buttons) do |
| 1873 |
bs = btn.panel_for_sprite.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1874 |
if button.enabled and button.x * bs <= x and x < button.r * bs and button.y * bs <= y and y < button.b * bs then |
| 1875 |
button.panel_for_sprite.sprite_index = button.sprite_index_active |
| 1876 |
button.active = true |
| 1877 |
button.panel_for_sprite.lowered = button.panel_lowered_active |
| 1878 |
self.active_button = button |
| 1879 |
repaint = true |
| 1880 |
break |
| 1881 |
end |
| 1882 |
end |
| 1883 |
end |
| 1884 |
if btn.panel_for_sprite.sprite_index ~= index then |
| 1885 |
btn.panel_for_sprite.sprite_index = index |
| 1886 |
repaint = true |
| 1887 |
end |
| 1888 |
end |
| 1889 |
|
| 1890 |
if self.active_scrollbar then |
| 1891 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 1892 |
local bar = self.active_scrollbar |
| 1893 |
if bar.direction == "x" then |
| 1894 |
bar:setXorY(math.floor(x / s - bar.down_x)) |
| 1895 |
elseif bar.direction == "y" then |
| 1896 |
bar:setXorY(math.floor(y / s - bar.down_y)) |
| 1897 |
end |
| 1898 |
end |
| 1899 |
|
| 1900 |
return repaint |
| 1901 |
end |
| 1902 |
|
| 1903 |
-- Called regularly at a rate independent of the game speed. |
| 1904 |
function Window:onTick() |
| 1905 |
if self.active_button then |
| 1906 |
local btn = self.active_button |
| 1907 |
local mouse_btn = self.buttons_down.mouse_left and "left" or self.buttons_down.mouse_right and "right" or nil |
| 1908 |
if mouse_btn then |
| 1909 |
if self.btn_repeat_delay > 0 then |
| 1910 |
self.btn_repeat_delay = self.btn_repeat_delay - 1 |
| 1911 |
else |
| 1912 |
if btn.active and btn.is_repeat then |
| 1913 |
self.btn_repeat_delay = 2 |
| 1914 |
btn:handleClick(mouse_btn) |
| 1915 |
end |
| 1916 |
end |
| 1917 |
end |
| 1918 |
end |
| 1919 |
if self.blinking_button then |
| 1920 |
self.blink_counter = self.blink_counter + 1 |
| 1921 |
if self.blink_counter == 20 then |
| 1922 |
self.blink_counter = 0 |
| 1923 |
local btn = self.buttons[self.blinking_button] |
| 1924 |
btn.sprite_index_blink = btn.sprite_index_blink == btn.sprite_index_active and btn.sprite_index_normal or btn.sprite_index_active |
| 1925 |
if btn.enabled and not btn.active then |
| 1926 |
btn.panel_for_sprite.sprite_index = btn.sprite_index_blink |
| 1927 |
end |
| 1928 |
end |
| 1929 |
end |
| 1930 |
if not class.is(self, UI) then -- prevent UI (sub)class from handling the textboxes too |
| 1931 |
for _, box in ipairs(self.textboxes) do |
| 1932 |
box:onTick() |
| 1933 |
end |
| 1934 |
end |
| 1935 |
if self.windows then |
| 1936 |
for _, window in ipairs(self.windows) do |
| 1937 |
window:onTick() |
| 1938 |
end |
| 1939 |
end |
| 1940 |
end |
| 1941 |
|
| 1942 |
-- Called regularly at the same rate that entities are ticked. |
| 1943 |
function Window:onWorldTick() |
| 1944 |
if self.windows then |
| 1945 |
for _, window in ipairs(self.windows) do |
| 1946 |
window:onWorldTick() |
| 1947 |
end |
| 1948 |
end |
| 1949 |
end |
| 1950 |
|
| 1951 |
-- Bring the window to the top of its parent |
| 1952 |
function Window:bringToTop() |
| 1953 |
if self.parent then |
| 1954 |
self.parent:sendToTop(self) |
| 1955 |
end |
| 1956 |
end |
| 1957 |
|
| 1958 |
-- Tell the window to bring the specified sub-window to its top |
| 1959 |
function Window:sendToTop(window) |
| 1960 |
local window_index |
| 1961 |
if self.windows then |
| 1962 |
for i = 1, #self.windows do -- Search specified window in windows list |
| 1963 |
if self.windows[i] == window then |
| 1964 |
window_index = i -- Keep window index |
| 1965 |
break |
| 1966 |
end |
| 1967 |
end |
| 1968 |
end |
| 1969 |
|
| 1970 |
if window_index ~= nil then |
| 1971 |
local insert_pos |
| 1972 |
if window.on_top then |
| 1973 |
insert_pos = 1 |
| 1974 |
else |
| 1975 |
-- First position after any on_top windows |
| 1976 |
for i = 1, #self.windows do |
| 1977 |
if not self.windows[i].on_top then |
| 1978 |
insert_pos = i |
| 1979 |
break |
| 1980 |
end |
| 1981 |
end |
| 1982 |
insert_pos = insert_pos or #self.windows + 1 |
| 1983 |
end |
| 1984 |
table.remove(self.windows, window_index) -- Remove the window from the list |
| 1985 |
table.insert(self.windows, insert_pos, window) -- And reinsert it at the before computed position |
| 1986 |
end |
| 1987 |
end |
| 1988 |
|
| 1989 |
-- Tell the window to bring the specified sub-window to its bottom |
| 1990 |
function Window:sendToBottom(window) |
| 1991 |
if self.windows then |
| 1992 |
for i = 1, #self.windows do -- Search specified window in windows list |
| 1993 |
if self.windows[i] == window then |
| 1994 |
table.remove(self.windows, i) -- Remove the window from the list |
| 1995 |
table.insert(self.windows, #self.windows + 1, window) -- And reinsert it at the end |
| 1996 |
break |
| 1997 |
end |
| 1998 |
end |
| 1999 |
end |
| 2000 |
end |
| 2001 |
|
| 2002 |
function Window:startButtonBlinking(button_index) |
| 2003 |
|
| 2004 |
self.blinking_button = button_index |
| 2005 |
self.blink_counter = 0 |
| 2006 |
local btn = self.buttons[button_index] |
| 2007 |
btn.sprite_index_blink = btn.sprite_index_normal |
| 2008 |
end |
| 2009 |
|
| 2010 |
function Window:stopButtonBlinking() |
| 2011 |
-- Does any button actually blink? |
| 2012 |
if self.blinking_button then |
| 2013 |
local btn = self.buttons[self.blinking_button] |
| 2014 |
btn.panel_for_sprite.sprite_index = btn.sprite_index_normal |
| 2015 |
btn.sprite_index_blink = nil |
| 2016 |
self.blinking_button = false |
| 2017 |
self.blink_counter = 0 |
| 2018 |
end |
| 2019 |
end |
| 2020 |
|
| 2021 |
--! Create a static (non-changeable) tooltip to be displayed in a certain region. |
| 2022 |
--! tooltip_x and tooltip_y are optional; if not specified, it will default to top center of region. |
| 2023 |
--!param text (string) The string to display. |
| 2024 |
--!param x (integer) The X coordinate relative to the top-left corner. |
| 2025 |
--!param y (integer) The Y coordinate relative to the top-left corner. |
| 2026 |
--!param r (integer) The right (X + width) coordinate relative to the top-left corner. |
| 2027 |
--!param b (integer) The bottom (Y + height) coordinate relative to the top-left corner. |
| 2028 |
--!param tooltip_x (integer) [optional] The X coordinate to display the tooltip at. |
| 2029 |
--!param tooltip_y (integer) [optional] The Y coordinate to display the tooltip at. |
| 2030 |
function Window:makeTooltip(text, x, y, r, b, tooltip_x, tooltip_y, apply_ui_scale) |
| 2031 |
local region = { |
| 2032 |
text = text, x = x, y = y, r = r, b = b, |
| 2033 |
tooltip_x = tooltip_x or round((x + r) / 2, 1), -- optional |
| 2034 |
tooltip_y = tooltip_y or y, -- optional |
| 2035 |
} |
| 2036 |
self.tooltip_regions[#self.tooltip_regions + 1] = region |
| 2037 |
return region |
| 2038 |
end |
| 2039 |
|
| 2040 |
--! Create a dynamic tooltip to be displayed in a certain region. |
| 2041 |
--! tooltip_x and tooltip_y are optional; if not specified, it will default to top center of region. |
| 2042 |
--!param callback (function) A function that returns the string to display or nil for no tooltip. |
| 2043 |
--!param x (integer) The X coordinate relative to the top-left corner. |
| 2044 |
--!param y (integer) The Y coordinate relative to the top-left corner. |
| 2045 |
--!param r (integer) The right (X + width) coordinate relative to the top-left corner. |
| 2046 |
--!param b (integer) The bottom (Y + height) coordinate relative to the top-left corner. |
| 2047 |
--!param tooltip_x (integer) [optional] The X coordinate to display the tooltip at. |
| 2048 |
--!param tooltip_y (integer) [optional] The Y coordinate to display the tooltip at. |
| 2049 |
function Window:makeDynamicTooltip(callback, x, y, r, b, tooltip_x, tooltip_y) |
| 2050 |
local region = { |
| 2051 |
callback = callback, x = x, y = y, r = r, b = b, |
| 2052 |
tooltip_x = tooltip_x or round((x + r) / 2, 1), -- optional |
| 2053 |
tooltip_y = tooltip_y or y, -- optional |
| 2054 |
} |
| 2055 |
self.tooltip_regions[#self.tooltip_regions + 1] = region |
| 2056 |
return region |
| 2057 |
end |
| 2058 |
|
| 2059 |
-- An 'element' can either be a panel, a button, or a tooltip region. |
| 2060 |
function Window:getTooltipForElement(elem, x, y) |
| 2061 |
local text |
| 2062 |
if elem.callback then |
| 2063 |
text = elem.callback(x, y) |
| 2064 |
else |
| 2065 |
text = elem.text |
| 2066 |
end |
| 2067 |
local apply_ui_scale = self.apply_ui_scale |
| 2068 |
if elem.apply_ui_scale ~= nil then |
| 2069 |
apply_ui_scale = elem.apply_ui_scale |
| 2070 |
elseif elem.panel_for_sprite and elem.panel_for_sprite.apply_ui_scale ~= nil then |
| 2071 |
apply_ui_scale = elem.panel_for_sprite.apply_ui_scale |
| 2072 |
end |
| 2073 |
local elem_scale = apply_ui_scale and TheApp.config.ui_scale or 1 |
| 2074 |
local xpos = elem.tooltip_x and elem.tooltip_x * elem_scale or nil |
| 2075 |
local ypos = elem.tooltip_y and elem.tooltip_y * elem_scale or nil |
| 2076 |
|
| 2077 |
local window_scale = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 2078 |
if xpos then xpos = xpos + self.x * window_scale end -- NB: can be nil, then it means position at mouse cursor |
| 2079 |
if ypos then ypos = ypos + self.y * window_scale end |
| 2080 |
if text then |
| 2081 |
return { text = text, x = xpos, y = ypos } |
| 2082 |
end |
| 2083 |
end |
| 2084 |
|
| 2085 |
--! Query the window for tooltip text to display for a particular position. |
| 2086 |
--! Tooltips are either associated with buttons, panels, or a region. |
| 2087 |
-- (see Button:setTooltip, Panel:setTooltip, Window:make[Dynamic]Tooltip) |
| 2088 |
--! Button tooltips take precedence over region tooltips, which again take precedence over panels. |
| 2089 |
-- Returns tooltip in form of { text = .. , x = .. , y = .. } or nil for no tooltip. |
| 2090 |
--!param x (integer) The X coordinate relative to the top-left corner. |
| 2091 |
--!param y (integer) The Y coordinate relative to the top-left corner. |
| 2092 |
function Window:getTooltipAt(x, y) |
| 2093 |
local s = self.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 2094 |
if x < 0 or y < 0 or (self.width and x >= self.width * s) or (self.height and y >= self.height * s) then |
| 2095 |
return |
| 2096 |
end |
| 2097 |
if self.windows then |
| 2098 |
for _, window in ipairs(self.windows) do |
| 2099 |
local ws = window.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 2100 |
if window:hitTest(x - window.x * ws, y - window.y * ws) then |
| 2101 |
return window:getTooltipAt(x - window.x * ws, y - window.y * ws) |
| 2102 |
end |
| 2103 |
end |
| 2104 |
end |
| 2105 |
for _, btn in ipairs(self.buttons) do |
| 2106 |
local bs = btn.panel_for_sprite.apply_ui_scale and TheApp.config.ui_scale or 1 |
| 2107 |
if btn.panel_for_sprite.visible ~= false and |
| 2108 |
btn.tooltip and |
| 2109 |
btn.x * bs <= x and x < btn.r * bs and btn.y * bs <= y and y < btn.b * bs then |
| 2110 |
return self:getTooltipForElement(btn.tooltip, x, y) |
| 2111 |
end |
| 2112 |
end |
| 2113 |
for _, region in ipairs(self.tooltip_regions) do |
| 2114 |
if region.enabled ~= false and region.x * s <= x and x < region.r * s and region.y * s <= y and y < region.b * s then |
| 2115 |
return self:getTooltipForElement(region, x, y) |
| 2116 |
end |
| 2117 |
end |
| 2118 |
for _, pnl in ipairs(self.panels) do |
| 2119 |
if pnl.tooltip and self:hitTestPanel(x, y, pnl) then |
| 2120 |
return self:getTooltipForElement(pnl.tooltip, x, y) |
| 2121 |
end |
| 2122 |
end |
| 2123 |
end |
| 2124 |
|
| 2125 |
function Panel:afterLoad(old, new) |
| 2126 |
if old < 160 then |
| 2127 |
self.lowered_colour = self.colour |
| 2128 |
end |
| 2129 |
if old < 236 then |
| 2130 |
self.apply_ui_scale = true |
| 2131 |
end |
| 2132 |
end |
| 2133 |
|
| 2134 |
--! Stub to be extended in subclasses, if needed. |
| 2135 |
function Window:afterLoad(old, new) |
| 2136 |
if old < 2 then |
| 2137 |
-- Scrollbars were added |
| 2138 |
self.scrollbars = {} |
| 2139 |
end |
| 2140 |
if old < 3 then |
| 2141 |
-- Textboxes were added |
| 2142 |
self.textboxes = {} |
| 2143 |
end |
| 2144 |
if old < 22 then |
| 2145 |
self.draggable = true |
| 2146 |
end |
| 2147 |
if old < 32 then |
| 2148 |
-- ui added to buttons |
| 2149 |
for _, btn in ipairs(self.buttons) do |
| 2150 |
btn.ui = self.ui |
| 2151 |
end |
| 2152 |
end |
| 2153 |
if old < 132 then |
| 2154 |
-- Hotkey boxes were added. |
| 2155 |
self.hotkeyboxes = {} |
| 2156 |
end |
| 2157 |
if old < 236 then |
| 2158 |
self.apply_ui_scale = true |
| 2159 |
end |
| 2160 |
|
| 2161 |
-- If a window or panel is asked to close during an afterLoad cycle we |
| 2162 |
-- can skip entries so use backwards iteration here instead |
| 2163 |
if self.windows then |
| 2164 |
for i = #self.windows, 1, -1 do |
| 2165 |
local window = self.windows[i] |
| 2166 |
if window then |
| 2167 |
window:afterLoad(old, new) |
| 2168 |
end |
| 2169 |
end |
| 2170 |
end |
| 2171 |
|
| 2172 |
if self.panels then |
| 2173 |
for i = #self.panels, 1, -1 do |
| 2174 |
local panel = self.panels[i] |
| 2175 |
if panel then |
| 2176 |
panel:afterLoad(old, new) |
| 2177 |
end |
| 2178 |
end |
| 2179 |
end |
| 2180 |
end |