2022-06-14 16:05:49 +00:00
|
|
|
--
|
|
|
|
-- Generic Graphics Element
|
|
|
|
--
|
|
|
|
|
2023-09-30 15:46:47 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
local core = require("graphics.core")
|
|
|
|
|
2023-09-05 19:32:45 +00:00
|
|
|
local events = core.events
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
local element = {}
|
|
|
|
|
|
|
|
---@class graphics_args_generic
|
|
|
|
---@field window? table
|
|
|
|
---@field parent? graphics_element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-06 19:42:39 +00:00
|
|
|
---@field x? integer 1 if omitted
|
2022-07-28 15:17:34 +00:00
|
|
|
---@field y? integer next line if omitted
|
2022-06-06 19:42:39 +00:00
|
|
|
---@field width? integer parent width if omitted
|
|
|
|
---@field height? integer parent height if omitted
|
|
|
|
---@field gframe? graphics_frame frame instead of x/y/width/height
|
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2023-09-17 01:06:16 +00:00
|
|
|
---@field can_focus? boolean true if this element can be focused, false by default
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-09-21 19:53:51 +00:00
|
|
|
---@alias graphics_args graphics_args_generic
|
|
|
|
---|waiting_args
|
2023-07-29 04:25:20 +00:00
|
|
|
---|app_button_args
|
2023-07-01 23:40:33 +00:00
|
|
|
---|checkbox_args
|
2022-10-07 14:28:46 +00:00
|
|
|
---|hazard_button_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|multi_button_args
|
|
|
|
---|push_button_args
|
2023-09-25 02:27:39 +00:00
|
|
|
---|radio_2d_args
|
2022-12-13 20:18:29 +00:00
|
|
|
---|radio_button_args
|
2023-04-12 16:40:13 +00:00
|
|
|
---|sidebar_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|spinbox_args
|
|
|
|
---|switch_button_args
|
2023-05-10 15:46:06 +00:00
|
|
|
---|tabbar_args
|
2023-09-17 01:06:16 +00:00
|
|
|
---|number_field_args
|
2023-09-20 03:51:58 +00:00
|
|
|
---|text_field_args
|
2022-11-26 21:18:31 +00:00
|
|
|
---|alarm_indicator_light
|
2022-09-21 19:53:51 +00:00
|
|
|
---|core_map_args
|
|
|
|
---|data_indicator_args
|
|
|
|
---|hbar_args
|
|
|
|
---|icon_indicator_args
|
2023-04-07 02:10:33 +00:00
|
|
|
---|indicator_led_args
|
|
|
|
---|indicator_led_pair_args
|
2023-04-08 20:49:54 +00:00
|
|
|
---|indicator_led_rgb_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|indicator_light_args
|
2022-11-02 18:47:18 +00:00
|
|
|
---|power_indicator_args
|
2023-02-14 03:11:31 +00:00
|
|
|
---|rad_indicator_args
|
2024-01-14 19:20:59 +00:00
|
|
|
---|signal_bar_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|state_indicator_args
|
|
|
|
---|tristate_indicator_light_args
|
|
|
|
---|vbar_args
|
2024-04-13 18:47:20 +00:00
|
|
|
---|app_multipane_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|colormap_args
|
|
|
|
---|displaybox_args
|
|
|
|
---|div_args
|
2023-05-31 15:44:41 +00:00
|
|
|
---|listbox_args
|
2023-04-16 19:05:28 +00:00
|
|
|
---|multipane_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|pipenet_args
|
|
|
|
---|rectangle_args
|
|
|
|
---|textbox_args
|
|
|
|
---|tiling_args
|
|
|
|
|
2023-05-13 12:50:13 +00:00
|
|
|
---@class element_subscription
|
|
|
|
---@field ps psil ps used
|
|
|
|
---@field key string data key
|
|
|
|
---@field func function callback
|
|
|
|
|
2023-09-30 15:46:47 +00:00
|
|
|
-- more detailed assert message for element verification
|
|
|
|
---@param condition any assert condition
|
|
|
|
---@param msg string assert message
|
|
|
|
---@param callstack_offset? integer shift value to change targets of debug.getinfo()
|
|
|
|
function element.assert(condition, msg, callstack_offset)
|
|
|
|
callstack_offset = callstack_offset or 0
|
|
|
|
local caller = debug.getinfo(3 + callstack_offset)
|
|
|
|
assert(condition, util.c(caller.source, ":", caller.currentline, "{", debug.getinfo(2 + callstack_offset).name, "}: ", msg))
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- a base graphics element, should not be created on its own
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-09-21 19:53:51 +00:00
|
|
|
---@param args graphics_args arguments
|
2024-06-05 00:31:06 +00:00
|
|
|
---@param constraint? function apply a dimensional constraint based on proposed dimensions function(frame) -> width, height
|
2023-06-07 22:38:00 +00:00
|
|
|
---@param child_offset_x? integer mouse event offset x
|
|
|
|
---@param child_offset_y? integer mouse event offset y
|
2024-06-05 00:31:06 +00:00
|
|
|
function element.new(args, constraint, child_offset_x, child_offset_y)
|
2022-06-06 19:42:39 +00:00
|
|
|
local self = {
|
2023-06-01 17:00:45 +00:00
|
|
|
id = nil, ---@type element_id|nil
|
2023-09-17 01:06:16 +00:00
|
|
|
is_root = args.parent == nil,
|
2022-06-16 15:19:32 +00:00
|
|
|
elem_type = debug.getinfo(2).name,
|
2022-07-28 14:09:34 +00:00
|
|
|
define_completed = false,
|
2023-05-10 00:29:07 +00:00
|
|
|
p_window = nil, ---@type table
|
2023-09-30 15:46:47 +00:00
|
|
|
position = events.new_coord_2d(1, 1),
|
2023-05-10 00:29:07 +00:00
|
|
|
bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1 }, ---@class element_bounds
|
2023-10-21 17:58:42 +00:00
|
|
|
offset_x = 0,
|
|
|
|
offset_y = 0,
|
2023-07-19 15:27:33 +00:00
|
|
|
next_y = 1, -- next child y coordinate
|
|
|
|
next_id = 0, -- next child ID
|
2023-05-13 12:50:13 +00:00
|
|
|
subscriptions = {},
|
2023-09-05 19:32:45 +00:00
|
|
|
button_down = { events.new_coord_2d(-1, -1), events.new_coord_2d(-1, -1), events.new_coord_2d(-1, -1) },
|
2023-09-17 01:06:16 +00:00
|
|
|
focused = false,
|
2022-07-24 00:08:37 +00:00
|
|
|
mt = {}
|
2022-06-06 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
---@class graphics_base
|
2022-06-06 19:42:39 +00:00
|
|
|
local protected = {
|
2022-10-20 16:22:45 +00:00
|
|
|
enabled = true,
|
2023-05-30 23:51:10 +00:00
|
|
|
value = nil, ---@type any
|
|
|
|
window = nil, ---@type table
|
|
|
|
content_window = nil, ---@type table|nil
|
2023-10-15 21:02:48 +00:00
|
|
|
mouse_window_shift = { x = 0, y = 0 },
|
2023-05-07 01:27:36 +00:00
|
|
|
fg_bg = core.cpair(colors.white, colors.black),
|
2023-05-30 23:51:10 +00:00
|
|
|
frame = core.gframe(1, 1, 1, 1),
|
2023-09-17 01:06:16 +00:00
|
|
|
children = {},
|
|
|
|
child_id_map = {}
|
2022-06-06 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 00:08:37 +00:00
|
|
|
-- element as string
|
|
|
|
function self.mt.__tostring()
|
2023-09-30 15:46:47 +00:00
|
|
|
return util.c("graphics.element{", self.elem_type, "} @ ", self)
|
2022-07-24 00:08:37 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
---@class graphics_element
|
|
|
|
local public = {}
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
setmetatable(public, self.mt)
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-----------------------
|
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
-- use tab to jump to the next focusable field
|
|
|
|
---@param reverse boolean
|
|
|
|
local function _tab_focusable(reverse)
|
|
|
|
local first_f = nil ---@type graphics_element|nil
|
|
|
|
local prev_f = nil ---@type graphics_element|nil
|
|
|
|
local cur_f = nil ---@type graphics_element|nil
|
|
|
|
local done = false
|
|
|
|
|
|
|
|
---@param elem graphics_element
|
|
|
|
local function handle_element(elem)
|
|
|
|
if elem.is_visible() and elem.is_focusable() and elem.is_enabled() then
|
|
|
|
if first_f == nil then first_f = elem end
|
|
|
|
|
|
|
|
if cur_f == nil then
|
|
|
|
if elem.is_focused() then
|
|
|
|
cur_f = elem
|
|
|
|
if (not done) and (reverse and prev_f ~= nil) then
|
|
|
|
cur_f.unfocus()
|
|
|
|
prev_f.focus()
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if elem.is_focused() then
|
|
|
|
elem.unfocus()
|
|
|
|
elseif not (reverse or done) then
|
|
|
|
cur_f.unfocus()
|
|
|
|
elem.focus()
|
|
|
|
done = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
prev_f = elem
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param children table
|
|
|
|
local function traverse(children)
|
|
|
|
for i = 1, #children do
|
|
|
|
local child = children[i] ---@type graphics_base
|
|
|
|
handle_element(child.get())
|
|
|
|
if child.get().is_visible() then traverse(child.children) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
traverse(protected.children)
|
|
|
|
|
|
|
|
-- if no element was focused, wrap focus
|
|
|
|
if first_f ~= nil and not done then
|
|
|
|
if reverse then
|
|
|
|
if cur_f ~= nil then cur_f.unfocus() end
|
|
|
|
if prev_f ~= nil then prev_f.focus() end
|
|
|
|
else
|
|
|
|
if cur_f ~= nil then cur_f.unfocus() end
|
|
|
|
first_f.focus()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-------------------------
|
|
|
|
-- PROTECTED FUNCTIONS --
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
-- prepare the template
|
2023-06-07 22:38:00 +00:00
|
|
|
---@param offset_x integer x offset for mouse events
|
|
|
|
---@param offset_y integer y offset for mouse events
|
2022-07-28 15:17:34 +00:00
|
|
|
---@param next_y integer next line if no y was provided
|
2023-06-07 22:38:00 +00:00
|
|
|
function protected.prepare_template(offset_x, offset_y, next_y)
|
2024-05-04 17:50:53 +00:00
|
|
|
-- don't auto incrememnt y if inheriting height, that would cause an assertion
|
2024-06-12 23:59:19 +00:00
|
|
|
next_y = util.trinary(args.height == nil and constraint == nil, 1, next_y)
|
2024-05-04 17:50:53 +00:00
|
|
|
|
2023-10-21 17:58:42 +00:00
|
|
|
-- record offsets in case there is a reposition
|
|
|
|
self.offset_x = offset_x
|
|
|
|
self.offset_y = offset_y
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- get frame coordinates/size
|
|
|
|
if args.gframe ~= nil then
|
|
|
|
protected.frame.x = args.gframe.x
|
|
|
|
protected.frame.y = args.gframe.y
|
|
|
|
protected.frame.w = args.gframe.w
|
|
|
|
protected.frame.h = args.gframe.h
|
|
|
|
else
|
|
|
|
local w, h = self.p_window.getSize()
|
|
|
|
protected.frame.x = args.x or 1
|
2023-05-31 15:44:41 +00:00
|
|
|
protected.frame.y = args.y or next_y
|
2022-07-28 15:17:34 +00:00
|
|
|
protected.frame.w = args.width or w
|
|
|
|
protected.frame.h = args.height or h
|
|
|
|
end
|
2022-06-19 15:20:09 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- adjust window frame if applicable
|
|
|
|
local f = protected.frame
|
|
|
|
if args.parent ~= nil then
|
|
|
|
-- constrain to parent inner width/height
|
|
|
|
local w, h = self.p_window.getSize()
|
2023-05-31 00:43:33 +00:00
|
|
|
f.w = math.min(f.w, w - (f.x - 1))
|
|
|
|
f.h = math.min(f.h, h - (f.y - 1))
|
2024-06-05 00:31:06 +00:00
|
|
|
|
|
|
|
if type(constraint) == "function" then
|
|
|
|
-- constrain per provided constraint function (can only get smaller than available space)
|
2024-06-05 23:38:22 +00:00
|
|
|
w, h = constraint(f)
|
2024-06-05 00:31:06 +00:00
|
|
|
f.w = math.min(f.w, w)
|
|
|
|
f.h = math.min(f.h, h)
|
|
|
|
end
|
2022-07-28 15:17:34 +00:00
|
|
|
end
|
2022-06-25 18:27:15 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- check frame
|
2023-10-14 15:40:36 +00:00
|
|
|
element.assert(f.x >= 1, "frame x not >= 1", 3)
|
|
|
|
element.assert(f.y >= 1, "frame y not >= 1", 3)
|
|
|
|
element.assert(f.w >= 1, "frame width not >= 1", 3)
|
|
|
|
element.assert(f.h >= 1, "frame height not >= 1", 3)
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- create window
|
2023-05-25 21:40:16 +00:00
|
|
|
protected.window = window.create(self.p_window, f.x, f.y, f.w, f.h, args.hidden ~= true)
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- init colors
|
|
|
|
if args.fg_bg ~= nil then
|
2024-02-24 19:35:04 +00:00
|
|
|
protected.fg_bg = core.cpair(args.fg_bg.fgd, args.fg_bg.bkg)
|
2022-07-28 15:17:34 +00:00
|
|
|
end
|
2022-06-16 15:19:32 +00:00
|
|
|
|
2024-02-24 19:35:04 +00:00
|
|
|
if args.parent ~= nil then
|
|
|
|
local p_fg_bg = args.parent.get_fg_bg()
|
|
|
|
|
|
|
|
if args.fg_bg == nil then
|
|
|
|
protected.fg_bg = core.cpair(p_fg_bg.fgd, p_fg_bg.bkg)
|
|
|
|
else
|
|
|
|
if protected.fg_bg.fgd == colors._INHERIT then protected.fg_bg = core.cpair(p_fg_bg.fgd, protected.fg_bg.bkg) end
|
|
|
|
if protected.fg_bg.bkg == colors._INHERIT then protected.fg_bg = core.cpair(protected.fg_bg.fgd, p_fg_bg.bkg) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check colors
|
|
|
|
element.assert(protected.fg_bg.fgd ~= colors._INHERIT, "could not determine foreground color to inherit")
|
|
|
|
element.assert(protected.fg_bg.bkg ~= colors._INHERIT, "could not determine background color to inherit")
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- set colors
|
|
|
|
protected.window.setBackgroundColor(protected.fg_bg.bkg)
|
|
|
|
protected.window.setTextColor(protected.fg_bg.fgd)
|
|
|
|
protected.window.clear()
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- record position
|
|
|
|
self.position.x, self.position.y = protected.window.getPosition()
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-06-07 22:38:00 +00:00
|
|
|
-- shift per parent child offset
|
|
|
|
self.position.x = self.position.x + offset_x
|
|
|
|
self.position.y = self.position.y + offset_y
|
|
|
|
|
|
|
|
-- calculate mouse event bounds
|
2022-07-28 15:17:34 +00:00
|
|
|
self.bounds.x1 = self.position.x
|
|
|
|
self.bounds.x2 = self.position.x + f.w - 1
|
|
|
|
self.bounds.y1 = self.position.y
|
|
|
|
self.bounds.y2 = self.position.y + f.h - 1
|
2023-08-31 01:11:57 +00:00
|
|
|
|
|
|
|
-- alias functions
|
|
|
|
|
|
|
|
-- window set cursor position
|
|
|
|
---@param x integer
|
|
|
|
---@param y integer
|
|
|
|
function protected.w_set_cur(x, y) protected.window.setCursorPos(x, y) end
|
|
|
|
|
|
|
|
-- set background color
|
|
|
|
---@param c color
|
|
|
|
function protected.w_set_bkg(c) protected.window.setBackgroundColor(c) end
|
|
|
|
|
|
|
|
-- set foreground (text) color
|
|
|
|
---@param c color
|
|
|
|
function protected.w_set_fgd(c) protected.window.setTextColor(c) end
|
|
|
|
|
|
|
|
-- write text
|
|
|
|
---@param str string
|
|
|
|
function protected.w_write(str) protected.window.write(str) end
|
|
|
|
|
|
|
|
-- blit text
|
|
|
|
---@param str string
|
|
|
|
---@param fg string
|
|
|
|
---@param bg string
|
|
|
|
function protected.w_blit(str, fg, bg) protected.window.blit(str, fg, bg) end
|
2022-07-28 15:17:34 +00:00
|
|
|
end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-05-23 23:51:48 +00:00
|
|
|
-- check if a coordinate relative to the parent is within the bounds of this element
|
2023-05-18 14:58:42 +00:00
|
|
|
---@param x integer
|
|
|
|
---@param y integer
|
2023-05-23 23:51:48 +00:00
|
|
|
function protected.in_window_bounds(x, y)
|
2023-05-18 14:58:42 +00:00
|
|
|
local in_x = x >= self.bounds.x1 and x <= self.bounds.x2
|
|
|
|
local in_y = y >= self.bounds.y1 and y <= self.bounds.y2
|
|
|
|
return in_x and in_y
|
|
|
|
end
|
|
|
|
|
2023-05-23 23:51:48 +00:00
|
|
|
-- check if a coordinate relative to this window is within the bounds of this element
|
|
|
|
---@param x integer
|
|
|
|
---@param y integer
|
|
|
|
function protected.in_frame_bounds(x, y)
|
|
|
|
local in_x = x >= 1 and x <= protected.frame.w
|
|
|
|
local in_y = y >= 1 and y <= protected.frame.h
|
|
|
|
return in_x and in_y
|
|
|
|
end
|
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-- get public interface
|
|
|
|
---@nodiscard
|
|
|
|
---@return graphics_element element, element_id id
|
|
|
|
function protected.get() return public, self.id end
|
|
|
|
|
|
|
|
-- report completion of element instantiation and get the public interface
|
|
|
|
---@nodiscard
|
|
|
|
---@return graphics_element element, element_id id
|
|
|
|
function protected.complete()
|
|
|
|
if args.parent ~= nil then args.parent.__child_ready(self.id, public) end
|
|
|
|
return public, self.id
|
|
|
|
end
|
|
|
|
|
|
|
|
-- protected version of public is_focused()
|
|
|
|
---@nodiscard
|
|
|
|
---@return boolean is_focused
|
|
|
|
function protected.is_focused() return self.focused end
|
|
|
|
|
|
|
|
-- defocus this element
|
|
|
|
function protected.defocus() public.unfocus_all() end
|
|
|
|
|
2023-10-04 02:52:13 +00:00
|
|
|
-- focus this element and take away focus from all other elements
|
|
|
|
function protected.take_focus() args.parent.__focus_child(public) end
|
2023-09-17 01:06:16 +00:00
|
|
|
|
|
|
|
-- action handlers --
|
|
|
|
|
2023-05-05 17:15:17 +00:00
|
|
|
-- luacheck: push ignore
|
2023-04-20 01:26:54 +00:00
|
|
|
---@diagnostic disable: unused-local, unused-vararg
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
-- handle a child element having been added
|
2023-06-01 17:00:45 +00:00
|
|
|
---@param id element_id element identifier
|
2023-05-30 23:51:10 +00:00
|
|
|
---@param child graphics_element child element
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.on_added(id, child) end
|
2023-05-13 12:50:13 +00:00
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
-- handle a child element having been removed
|
2023-06-01 17:00:45 +00:00
|
|
|
---@param id element_id element identifier
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.on_removed(id) end
|
|
|
|
|
2023-09-23 18:31:37 +00:00
|
|
|
-- handle enabled
|
|
|
|
function protected.on_enabled() end
|
|
|
|
|
|
|
|
-- handle disabled
|
|
|
|
function protected.on_disabled() end
|
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-- handle this element having been focused
|
|
|
|
function protected.on_focused() end
|
|
|
|
|
|
|
|
-- handle this element having been unfocused
|
|
|
|
function protected.on_unfocused() end
|
|
|
|
|
2023-10-15 21:02:48 +00:00
|
|
|
-- handle this element having had a child focused
|
|
|
|
---@param child graphics_element
|
|
|
|
function protected.on_child_focused(child) end
|
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-- handle this element having been shown
|
|
|
|
function protected.on_shown() end
|
|
|
|
|
|
|
|
-- handle this element having been hidden
|
|
|
|
function protected.on_hidden() end
|
2023-05-13 12:50:13 +00:00
|
|
|
|
2023-04-09 01:33:54 +00:00
|
|
|
-- handle a mouse event
|
|
|
|
---@param event mouse_interaction mouse interaction event
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.handle_mouse(event) end
|
|
|
|
|
|
|
|
-- handle a keyboard event
|
|
|
|
---@param event key_interaction key interaction event
|
|
|
|
function protected.handle_key(event) end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-09-20 03:51:58 +00:00
|
|
|
-- handle a paste event
|
|
|
|
---@param text string pasted text
|
|
|
|
function protected.handle_paste(text) end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- handle data value changes
|
2022-10-20 16:22:45 +00:00
|
|
|
---@vararg any value(s)
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.on_update(...) end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-11-06 23:41:52 +00:00
|
|
|
-- callback on control press responses
|
|
|
|
---@param result any
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.response_callback(result) end
|
2022-11-06 23:41:52 +00:00
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- accessors and control --
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
-- get value
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.get_value() return protected.value end
|
2022-09-12 16:59:28 +00:00
|
|
|
|
|
|
|
-- set value
|
|
|
|
---@param value any value to set
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.set_value(value) end
|
2022-10-20 16:22:45 +00:00
|
|
|
|
2022-11-17 17:00:00 +00:00
|
|
|
-- set minimum input value
|
|
|
|
---@param min integer minimum allowed value
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.set_min(min) end
|
2022-11-17 17:00:00 +00:00
|
|
|
|
|
|
|
-- set maximum input value
|
|
|
|
---@param max integer maximum allowed value
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.set_max(max) end
|
2022-11-17 17:00:00 +00:00
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
-- custom recolor command, varies by element if implemented
|
|
|
|
---@vararg cpair|color color(s)
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.recolor(...) end
|
2022-09-12 16:59:28 +00:00
|
|
|
|
|
|
|
-- custom resize command, varies by element if implemented
|
|
|
|
---@vararg integer sizing
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.resize(...) end
|
2022-09-12 16:59:28 +00:00
|
|
|
|
2023-05-05 17:15:17 +00:00
|
|
|
-- luacheck: pop
|
2023-04-20 01:26:54 +00:00
|
|
|
---@diagnostic enable: unused-local, unused-vararg
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- re-draw this element
|
|
|
|
function protected.redraw() end
|
|
|
|
|
2022-09-10 19:14:48 +00:00
|
|
|
-- start animations
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.start_anim() end
|
2022-09-10 19:14:48 +00:00
|
|
|
|
|
|
|
-- stop animations
|
2023-09-17 01:06:16 +00:00
|
|
|
function protected.stop_anim() end
|
2023-05-30 23:51:10 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-----------
|
|
|
|
-- SETUP --
|
|
|
|
-----------
|
2022-07-24 00:08:37 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- get the parent window
|
|
|
|
self.p_window = args.window
|
|
|
|
if self.p_window == nil and args.parent ~= nil then
|
|
|
|
self.p_window = args.parent.window()
|
|
|
|
end
|
2022-07-28 14:09:34 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- check window
|
2023-09-30 15:46:47 +00:00
|
|
|
element.assert(self.p_window, "no parent window provided", 1)
|
2022-07-28 14:09:34 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- prepare the template
|
|
|
|
if args.parent == nil then
|
2023-06-01 17:00:45 +00:00
|
|
|
self.id = args.id or "__ROOT__"
|
2023-06-07 22:38:00 +00:00
|
|
|
protected.prepare_template(0, 0, 1)
|
2022-07-28 15:17:34 +00:00
|
|
|
else
|
2023-10-04 02:52:13 +00:00
|
|
|
self.id = args.parent.__add_child(args.id, protected)
|
2022-07-28 14:09:34 +00:00
|
|
|
end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
----------------------
|
2022-06-06 19:42:39 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
2022-07-28 15:17:34 +00:00
|
|
|
----------------------
|
2022-06-06 19:42:39 +00:00
|
|
|
|
|
|
|
-- get the window object
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2023-05-30 23:51:10 +00:00
|
|
|
function public.window() return protected.content_window or protected.window end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-05-13 12:50:13 +00:00
|
|
|
-- delete this element (hide and unsubscribe from PSIL)
|
|
|
|
function public.delete()
|
2023-05-31 00:43:33 +00:00
|
|
|
local fg_bg = protected.fg_bg
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
if args.parent ~= nil then
|
2023-05-31 00:43:33 +00:00
|
|
|
-- grab parent fg/bg so we can clear cleanly as a child element
|
|
|
|
fg_bg = args.parent.get_fg_bg()
|
2023-05-30 23:51:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- clear, hide, and stop animations
|
2023-05-31 00:43:33 +00:00
|
|
|
protected.window.setBackgroundColor(fg_bg.bkg)
|
|
|
|
protected.window.setTextColor(fg_bg.fgd)
|
2023-05-30 23:51:10 +00:00
|
|
|
protected.window.clear()
|
2023-05-13 12:50:13 +00:00
|
|
|
public.hide()
|
|
|
|
|
|
|
|
-- unsubscribe from PSIL
|
|
|
|
for i = 1, #self.subscriptions do
|
|
|
|
local s = self.subscriptions[i] ---@type element_subscription
|
|
|
|
s.ps.unsubscribe(s.key, s.func)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- delete all children
|
2023-05-30 23:51:10 +00:00
|
|
|
for k, v in pairs(protected.children) do
|
2023-09-17 01:06:16 +00:00
|
|
|
v.get().delete()
|
2023-05-30 23:51:10 +00:00
|
|
|
protected.children[k] = nil
|
2023-05-13 12:50:13 +00:00
|
|
|
end
|
2023-06-01 17:00:45 +00:00
|
|
|
|
2023-06-03 18:33:08 +00:00
|
|
|
if args.parent ~= nil then
|
|
|
|
-- remove self from parent
|
|
|
|
args.parent.__remove_child(self.id)
|
|
|
|
end
|
2023-05-13 12:50:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- ELEMENT TREE --
|
2022-07-28 15:17:34 +00:00
|
|
|
|
2022-07-24 00:08:37 +00:00
|
|
|
-- add a child element
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-07-28 14:09:34 +00:00
|
|
|
---@param key string|nil id
|
2023-05-30 23:51:10 +00:00
|
|
|
---@param child graphics_base
|
2022-07-28 15:17:34 +00:00
|
|
|
---@return integer|string key
|
2022-07-28 14:09:34 +00:00
|
|
|
function public.__add_child(key, child)
|
2023-06-07 22:38:00 +00:00
|
|
|
child.prepare_template(child_offset_x or 0, child_offset_y or 0, self.next_y)
|
2022-07-28 15:17:34 +00:00
|
|
|
|
|
|
|
self.next_y = child.frame.y + child.frame.h
|
|
|
|
|
2023-07-19 15:27:33 +00:00
|
|
|
local id = key ---@type string|integer|nil
|
|
|
|
if id == nil then
|
|
|
|
id = self.next_id
|
|
|
|
self.next_id = self.next_id + 1
|
2022-07-28 14:09:34 +00:00
|
|
|
end
|
2023-07-19 15:27:33 +00:00
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
table.insert(protected.children, child)
|
|
|
|
|
|
|
|
protected.child_id_map[id] = #protected.children
|
|
|
|
|
2023-07-19 15:27:33 +00:00
|
|
|
return id
|
2022-07-28 14:09:34 +00:00
|
|
|
end
|
2022-07-24 00:08:37 +00:00
|
|
|
|
2023-06-01 17:00:45 +00:00
|
|
|
-- remove a child element
|
2023-09-17 01:06:16 +00:00
|
|
|
---@param id element_id id
|
|
|
|
function public.__remove_child(id)
|
|
|
|
local index = protected.child_id_map[id]
|
|
|
|
if protected.children[index] ~= nil then
|
|
|
|
protected.on_removed(id)
|
|
|
|
protected.children[index] = nil
|
|
|
|
protected.child_id_map[id] = nil
|
2023-06-01 17:00:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
-- actions to take upon a child element becoming ready (initial draw/construction completed)
|
2023-06-01 17:00:45 +00:00
|
|
|
---@param key element_id id
|
2023-05-30 23:51:10 +00:00
|
|
|
---@param child graphics_element
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.__child_ready(key, child) protected.on_added(key, child) end
|
|
|
|
|
|
|
|
-- focus solely on this child
|
|
|
|
---@param child graphics_element
|
|
|
|
function public.__focus_child(child)
|
|
|
|
if self.is_root then
|
|
|
|
public.unfocus_all()
|
|
|
|
child.focus()
|
|
|
|
else args.parent.__focus_child(child) end
|
2023-05-30 23:51:10 +00:00
|
|
|
end
|
|
|
|
|
2023-10-15 21:02:48 +00:00
|
|
|
-- a child was focused, used to make sure it is actually visible to the user in the content frame
|
|
|
|
---@param child graphics_element
|
|
|
|
function public.__child_focused(child)
|
|
|
|
protected.on_child_focused(child)
|
|
|
|
if not self.is_root then args.parent.__child_focused(public) end
|
|
|
|
end
|
|
|
|
|
2022-07-24 00:08:37 +00:00
|
|
|
-- get a child element
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2023-05-13 12:50:13 +00:00
|
|
|
---@param id element_id
|
2022-07-24 00:08:37 +00:00
|
|
|
---@return graphics_element
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.get_child(id) return protected.children[protected.child_id_map[id]].get() end
|
2022-07-24 00:08:37 +00:00
|
|
|
|
2023-05-13 12:50:13 +00:00
|
|
|
-- remove a child element
|
|
|
|
---@param id element_id
|
|
|
|
function public.remove(id)
|
2023-09-17 01:06:16 +00:00
|
|
|
local index = protected.child_id_map[id]
|
|
|
|
if protected.children[index] ~= nil then
|
|
|
|
protected.children[index].get().delete()
|
2023-05-30 23:51:10 +00:00
|
|
|
protected.on_removed(id)
|
2023-09-17 01:06:16 +00:00
|
|
|
protected.children[index] = nil
|
|
|
|
protected.child_id_map[id] = nil
|
2023-05-13 12:50:13 +00:00
|
|
|
end
|
|
|
|
end
|
2022-07-24 00:08:37 +00:00
|
|
|
|
2023-09-24 00:22:02 +00:00
|
|
|
-- remove all child elements and reset next y
|
|
|
|
function public.remove_all()
|
|
|
|
for i = 1, #protected.children do
|
|
|
|
local child = protected.children[i].get() ---@type graphics_element
|
|
|
|
child.delete()
|
|
|
|
protected.on_removed(child.get_id())
|
|
|
|
end
|
|
|
|
|
|
|
|
self.next_y = 1
|
|
|
|
protected.children = {}
|
|
|
|
protected.child_id_map = {}
|
|
|
|
end
|
|
|
|
|
2022-07-28 14:15:12 +00:00
|
|
|
-- attempt to get a child element by ID (does not include this element itself)
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-07-28 14:09:34 +00:00
|
|
|
---@param id element_id
|
2022-07-28 14:15:12 +00:00
|
|
|
---@return graphics_element|nil element
|
2022-07-28 14:09:34 +00:00
|
|
|
function public.get_element_by_id(id)
|
2023-09-17 01:06:16 +00:00
|
|
|
local index = protected.child_id_map[id]
|
|
|
|
if protected.children[index] == nil then
|
2023-05-30 23:51:10 +00:00
|
|
|
for _, child in pairs(protected.children) do
|
2023-09-17 01:06:16 +00:00
|
|
|
local elem = child.get().get_element_by_id(id)
|
2022-07-28 14:15:12 +00:00
|
|
|
if elem ~= nil then return elem end
|
|
|
|
end
|
2023-09-17 01:06:16 +00:00
|
|
|
else return protected.children[index].get() end
|
2022-07-28 14:09:34 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- AUTO-PLACEMENT --
|
|
|
|
|
|
|
|
-- skip a line for automatically placed elements
|
2023-02-25 00:50:01 +00:00
|
|
|
function public.line_break()
|
|
|
|
self.next_y = self.next_y + 1
|
|
|
|
end
|
2022-07-28 15:17:34 +00:00
|
|
|
|
|
|
|
-- PROPERTIES --
|
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-- get element id
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2023-09-17 01:06:16 +00:00
|
|
|
---@return element_id
|
|
|
|
function public.get_id() return self.id end
|
2022-06-16 15:19:32 +00:00
|
|
|
|
2022-11-26 21:18:31 +00:00
|
|
|
-- get element x
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-11-26 21:18:31 +00:00
|
|
|
---@return integer x
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.get_x() return protected.frame.x end
|
2022-11-26 21:18:31 +00:00
|
|
|
|
|
|
|
-- get element y
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-11-26 21:18:31 +00:00
|
|
|
---@return integer y
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.get_y() return protected.frame.y end
|
2022-11-26 21:18:31 +00:00
|
|
|
|
2022-07-14 17:47:39 +00:00
|
|
|
-- get element width
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-09-12 17:53:39 +00:00
|
|
|
---@return integer width
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.get_width() return protected.frame.w end
|
2022-07-14 17:47:39 +00:00
|
|
|
|
|
|
|
-- get element height
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-09-12 17:53:39 +00:00
|
|
|
---@return integer height
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.get_height() return protected.frame.h end
|
|
|
|
|
|
|
|
-- get the foreground/background colors
|
|
|
|
---@nodiscard
|
|
|
|
---@return cpair fg_bg
|
|
|
|
function public.get_fg_bg() return protected.fg_bg end
|
2022-07-14 17:47:39 +00:00
|
|
|
|
2022-09-12 17:53:39 +00:00
|
|
|
-- get the element value
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-09-12 17:53:39 +00:00
|
|
|
---@return any value
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.get_value() return protected.get_value() end
|
2022-07-28 15:17:34 +00:00
|
|
|
|
2022-09-12 17:53:39 +00:00
|
|
|
-- set the element value
|
|
|
|
---@param value any new value
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.set_value(value) protected.set_value(value) end
|
2022-09-12 17:53:39 +00:00
|
|
|
|
2022-11-17 17:00:00 +00:00
|
|
|
-- set minimum input value
|
|
|
|
---@param min integer minimum allowed value
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.set_min(min) protected.set_min(min) end
|
2022-11-17 17:00:00 +00:00
|
|
|
|
|
|
|
-- set maximum input value
|
|
|
|
---@param max integer maximum allowed value
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.set_max(max) protected.set_max(max) end
|
|
|
|
|
|
|
|
-- check if this element is enabled
|
|
|
|
function public.is_enabled() return protected.enabled end
|
2022-11-17 17:00:00 +00:00
|
|
|
|
2022-10-20 16:22:45 +00:00
|
|
|
-- enable the element
|
|
|
|
function public.enable()
|
2023-09-17 01:06:16 +00:00
|
|
|
if not protected.enabled then
|
|
|
|
protected.enabled = true
|
2023-09-23 18:31:37 +00:00
|
|
|
protected.on_enabled()
|
2023-09-17 01:06:16 +00:00
|
|
|
end
|
2022-10-20 16:22:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- disable the element
|
|
|
|
function public.disable()
|
2023-09-17 01:06:16 +00:00
|
|
|
if protected.enabled then
|
|
|
|
protected.enabled = false
|
2023-09-23 18:31:37 +00:00
|
|
|
protected.on_disabled()
|
2023-09-20 03:51:58 +00:00
|
|
|
public.unfocus_all()
|
2023-09-17 01:06:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- can this element be focused
|
|
|
|
function public.is_focusable() return args.can_focus end
|
|
|
|
|
|
|
|
-- is this element focused
|
|
|
|
function public.is_focused() return self.focused end
|
|
|
|
|
|
|
|
-- focus the element
|
|
|
|
function public.focus()
|
2023-09-20 03:51:58 +00:00
|
|
|
if args.can_focus and protected.enabled and not self.focused then
|
2023-09-17 01:06:16 +00:00
|
|
|
self.focused = true
|
|
|
|
protected.on_focused()
|
2023-10-15 21:02:48 +00:00
|
|
|
if not self.is_root then args.parent.__child_focused(public) end
|
2023-09-17 01:06:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- unfocus this element
|
|
|
|
function public.unfocus()
|
|
|
|
if args.can_focus and self.focused then
|
|
|
|
self.focused = false
|
|
|
|
protected.on_unfocused()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- unfocus this element and all its children
|
|
|
|
function public.unfocus_all()
|
|
|
|
public.unfocus()
|
2023-10-15 20:49:03 +00:00
|
|
|
for _, child in pairs(protected.children) do child.get().unfocus_all() end
|
2022-10-20 16:22:45 +00:00
|
|
|
end
|
|
|
|
|
2022-12-11 15:51:45 +00:00
|
|
|
-- custom recolor command, varies by element if implemented
|
|
|
|
---@vararg cpair|color color(s)
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.recolor(...) protected.recolor(...) end
|
2022-12-11 15:51:45 +00:00
|
|
|
|
2022-09-12 17:53:39 +00:00
|
|
|
-- resize attributes of the element value if supported
|
|
|
|
---@vararg number dimensions (element specific)
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.resize(...) protected.resize(...) end
|
2022-09-12 17:53:39 +00:00
|
|
|
|
2023-05-19 00:21:23 +00:00
|
|
|
-- reposition the element window<br>
|
|
|
|
-- offsets relative to parent frame are where (1, 1) would be on top of the parent's top left corner
|
|
|
|
---@param x integer x position relative to parent frame
|
|
|
|
---@param y integer y position relative to parent frame
|
2023-10-21 17:58:42 +00:00
|
|
|
function public.reposition(x, y)
|
|
|
|
protected.window.reposition(x, y)
|
|
|
|
|
|
|
|
-- record position
|
|
|
|
self.position.x, self.position.y = protected.window.getPosition()
|
|
|
|
|
|
|
|
-- shift per parent child offset
|
|
|
|
self.position.x = self.position.x + self.offset_x
|
|
|
|
self.position.y = self.position.y + self.offset_y
|
|
|
|
|
|
|
|
-- calculate mouse event bounds
|
|
|
|
self.bounds.x1 = self.position.x
|
|
|
|
self.bounds.x2 = self.position.x + protected.frame.w - 1
|
|
|
|
self.bounds.y1 = self.position.y
|
|
|
|
self.bounds.y2 = self.position.y + protected.frame.h - 1
|
|
|
|
end
|
2023-05-19 00:21:23 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- FUNCTION CALLBACKS --
|
|
|
|
|
2023-07-29 04:25:20 +00:00
|
|
|
-- handle a monitor touch or mouse click if this element is visible
|
2023-04-09 01:33:54 +00:00
|
|
|
---@param event mouse_interaction mouse interaction event
|
|
|
|
function public.handle_mouse(event)
|
2023-07-29 04:25:20 +00:00
|
|
|
if protected.window.isVisible() then
|
|
|
|
local x_ini, y_ini = event.initial.x, event.initial.y
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-07-29 04:25:20 +00:00
|
|
|
local ini_in = protected.in_window_bounds(x_ini, y_ini)
|
2022-07-24 00:08:37 +00:00
|
|
|
|
2023-07-29 04:25:20 +00:00
|
|
|
if ini_in then
|
2023-09-17 01:06:16 +00:00
|
|
|
if event.type == events.MOUSE_CLICK.UP or event.type == events.MOUSE_CLICK.DRAG then
|
2023-09-05 19:32:45 +00:00
|
|
|
-- make sure we don't handle mouse events that started before this element was made visible
|
|
|
|
if (event.initial.x ~= self.button_down[event.button].x) or (event.initial.y ~= self.button_down[event.button].y) then
|
|
|
|
return
|
|
|
|
end
|
2023-09-17 01:06:16 +00:00
|
|
|
elseif event.type == events.MOUSE_CLICK.DOWN then
|
2023-09-05 19:32:45 +00:00
|
|
|
self.button_down[event.button] = event.initial
|
|
|
|
end
|
|
|
|
|
|
|
|
local event_T = events.mouse_transposed(event, self.position.x, self.position.y)
|
2023-07-29 04:25:20 +00:00
|
|
|
protected.handle_mouse(event_T)
|
2023-10-15 21:02:48 +00:00
|
|
|
|
|
|
|
-- shift child event if the content window has moved then pass to children
|
|
|
|
local c_event_T = events.mouse_transposed(event_T, protected.mouse_window_shift.x + 1, protected.mouse_window_shift.y + 1)
|
|
|
|
for _, child in pairs(protected.children) do child.get().handle_mouse(c_event_T) end
|
2023-09-17 01:06:16 +00:00
|
|
|
elseif event.type == events.MOUSE_CLICK.DOWN or event.type == events.MOUSE_CLICK.TAP then
|
|
|
|
-- clicked out, unfocus this element and children
|
|
|
|
public.unfocus_all()
|
2023-07-29 04:25:20 +00:00
|
|
|
end
|
2023-09-17 01:06:16 +00:00
|
|
|
else
|
|
|
|
-- don't track clicks while hidden
|
2023-09-05 19:32:45 +00:00
|
|
|
self.button_down[event.button] = events.new_coord_2d(-1, -1)
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-- handle a keyboard click if this element is visible and focused
|
|
|
|
---@param event key_interaction keyboard interaction event
|
|
|
|
function public.handle_key(event)
|
|
|
|
if protected.window.isVisible() then
|
|
|
|
if self.is_root and (event.type == events.KEY_CLICK.DOWN) and (event.key == keys.tab) then
|
|
|
|
-- try to jump to the next/previous focusable field
|
|
|
|
_tab_focusable(event.shift)
|
|
|
|
else
|
|
|
|
-- handle the key event then pass to children
|
|
|
|
if self.focused then protected.handle_key(event) end
|
|
|
|
for _, child in pairs(protected.children) do child.get().handle_key(event) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-20 03:51:58 +00:00
|
|
|
-- handle text paste
|
|
|
|
---@param text string pasted text
|
|
|
|
function public.handle_paste(text)
|
|
|
|
if protected.window.isVisible() then
|
|
|
|
-- handle the paste event then pass to children
|
|
|
|
if self.focused then protected.handle_paste(text) end
|
|
|
|
for _, child in pairs(protected.children) do child.get().handle_paste(text) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- draw the element given new data
|
2022-09-12 17:53:39 +00:00
|
|
|
---@vararg any new data
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.update(...) protected.on_update(...) end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-11-06 23:41:52 +00:00
|
|
|
-- on a control request response
|
|
|
|
---@param result any
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.on_response(result) protected.response_callback(result) end
|
2022-11-06 23:41:52 +00:00
|
|
|
|
2023-05-13 12:50:13 +00:00
|
|
|
-- register a callback with a PSIL, allowing for automatic unregister on delete<br>
|
|
|
|
-- do not use graphics elements directly with PSIL subscribe()
|
|
|
|
---@param ps psil PSIL to subscribe to
|
|
|
|
---@param key string key to subscribe to
|
|
|
|
---@param func function function to link
|
|
|
|
function public.register(ps, key, func)
|
|
|
|
table.insert(self.subscriptions, { ps = ps, key = key, func = func })
|
|
|
|
ps.subscribe(key, func)
|
|
|
|
end
|
|
|
|
|
2023-05-25 21:40:16 +00:00
|
|
|
-- VISIBILITY & ANIMATIONS --
|
2022-06-11 20:44:31 +00:00
|
|
|
|
2023-09-17 01:06:16 +00:00
|
|
|
-- check if this element is visible
|
|
|
|
function public.is_visible() return protected.window.isVisible() end
|
|
|
|
|
2023-05-25 21:40:16 +00:00
|
|
|
-- show the element and enables animations by default
|
|
|
|
---@param animate? boolean true (default) to automatically resume animations
|
|
|
|
function public.show(animate)
|
2022-06-06 19:42:39 +00:00
|
|
|
protected.window.setVisible(true)
|
2023-05-25 21:40:16 +00:00
|
|
|
if animate ~= false then public.animate_all() end
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
-- hide the element and disables animations<br>
|
|
|
|
-- this alone does not cause an element to be fully hidden, it only prevents updates from being shown<br>
|
2023-10-01 04:19:16 +00:00
|
|
|
---@see graphics_element.redraw
|
2023-05-30 23:51:10 +00:00
|
|
|
---@see graphics_element.content_redraw
|
2023-10-01 04:19:16 +00:00
|
|
|
---@param clear? boolean true to visibly hide this element (redraws the parent)
|
|
|
|
function public.hide(clear)
|
2023-05-25 21:40:16 +00:00
|
|
|
public.freeze_all() -- stop animations for efficiency/performance
|
2023-09-17 01:06:16 +00:00
|
|
|
public.unfocus_all()
|
2022-06-06 19:42:39 +00:00
|
|
|
protected.window.setVisible(false)
|
2023-10-01 04:19:16 +00:00
|
|
|
if clear and args.parent then args.parent.redraw() end
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
2023-05-25 21:40:16 +00:00
|
|
|
-- start/resume animation(s)
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.animate() protected.start_anim() end
|
2023-05-25 21:40:16 +00:00
|
|
|
|
|
|
|
-- start/resume animation(s) for this element and all its children<br>
|
|
|
|
-- only animates if a window is visible
|
|
|
|
function public.animate_all()
|
|
|
|
if protected.window.isVisible() then
|
|
|
|
public.animate()
|
2023-09-17 01:06:16 +00:00
|
|
|
for _, child in pairs(protected.children) do child.get().animate_all() end
|
2023-05-25 21:40:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- freeze animation(s)
|
2023-09-17 01:06:16 +00:00
|
|
|
function public.freeze() protected.stop_anim() end
|
2023-05-25 21:40:16 +00:00
|
|
|
|
|
|
|
-- freeze animation(s) for this element and all its children
|
|
|
|
function public.freeze_all()
|
|
|
|
public.freeze()
|
2023-09-17 01:06:16 +00:00
|
|
|
for _, child in pairs(protected.children) do child.get().freeze_all() end
|
2023-05-25 21:40:16 +00:00
|
|
|
end
|
|
|
|
|
2023-10-01 04:19:16 +00:00
|
|
|
-- re-draw this element and all its children
|
|
|
|
function public.redraw()
|
2024-02-17 23:12:28 +00:00
|
|
|
local bg, fg = protected.window.getBackgroundColor(), protected.window.getTextColor()
|
2023-10-01 04:19:16 +00:00
|
|
|
protected.window.setBackgroundColor(protected.fg_bg.bkg)
|
|
|
|
protected.window.setTextColor(protected.fg_bg.fgd)
|
|
|
|
protected.window.clear()
|
2024-02-17 23:12:28 +00:00
|
|
|
protected.window.setBackgroundColor(bg)
|
|
|
|
protected.window.setTextColor(fg)
|
2023-10-01 04:19:16 +00:00
|
|
|
protected.redraw()
|
|
|
|
for _, child in pairs(protected.children) do child.get().redraw() end
|
|
|
|
end
|
2022-06-08 18:15:34 +00:00
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
-- if a content window is set, clears it then re-draws all children
|
|
|
|
function public.content_redraw()
|
|
|
|
if protected.content_window ~= nil then
|
|
|
|
protected.content_window.clear()
|
2023-09-17 01:06:16 +00:00
|
|
|
for _, child in pairs(protected.children) do child.get().redraw() end
|
2023-05-30 23:51:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
return protected
|
|
|
|
end
|
|
|
|
|
|
|
|
return element
|