2022-06-14 16:05:49 +00:00
|
|
|
--
|
|
|
|
-- Generic Graphics Element
|
|
|
|
--
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
local core = require("graphics.core")
|
|
|
|
|
|
|
|
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-19 15:35:17 +00:00
|
|
|
---@field offset_x? integer 0 if omitted
|
|
|
|
---@field offset_y? integer 0 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
|
|
|
|
|
2022-09-21 19:53:51 +00:00
|
|
|
---@alias graphics_args graphics_args_generic
|
|
|
|
---|waiting_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
|
2022-12-13 20:18:29 +00:00
|
|
|
---|radio_button_args
|
2022-09-21 19:53:51 +00:00
|
|
|
---|spinbox_args
|
|
|
|
---|switch_button_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
|
|
|
|
---|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
|
2022-09-21 19:53:51 +00:00
|
|
|
---|state_indicator_args
|
|
|
|
---|tristate_indicator_light_args
|
|
|
|
---|vbar_args
|
|
|
|
---|colormap_args
|
|
|
|
---|displaybox_args
|
|
|
|
---|div_args
|
|
|
|
---|pipenet_args
|
|
|
|
---|rectangle_args
|
|
|
|
---|textbox_args
|
|
|
|
---|tiling_args
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- a base graphics element, should not be created on its own
|
2022-09-21 19:53:51 +00:00
|
|
|
---@param args graphics_args arguments
|
2022-06-06 19:42:39 +00:00
|
|
|
function element.new(args)
|
|
|
|
local self = {
|
2022-07-28 14:09:34 +00:00
|
|
|
id = -1,
|
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,
|
2022-06-06 19:42:39 +00:00
|
|
|
p_window = nil, ---@type table
|
|
|
|
position = { x = 1, y = 1 },
|
2022-06-19 15:20:09 +00:00
|
|
|
child_offset = { x = 0, y = 0 },
|
2022-07-24 00:08:37 +00:00
|
|
|
bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1},
|
2022-07-28 15:17:34 +00:00
|
|
|
next_y = 1,
|
2022-07-24 00:08:37 +00:00
|
|
|
children = {},
|
|
|
|
mt = {}
|
2022-06-06 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 14:09:34 +00:00
|
|
|
---@class graphics_template
|
2022-06-06 19:42:39 +00:00
|
|
|
local protected = {
|
2022-10-20 16:22:45 +00:00
|
|
|
enabled = true,
|
2022-09-12 16:59:28 +00:00
|
|
|
value = nil, ---@type any
|
2022-06-06 19:42:39 +00:00
|
|
|
window = nil, ---@type table
|
2022-06-08 16:27:28 +00:00
|
|
|
fg_bg = core.graphics.cpair(colors.white, colors.black),
|
2022-06-06 19:42:39 +00:00
|
|
|
frame = core.graphics.gframe(1, 1, 1, 1)
|
|
|
|
}
|
|
|
|
|
2022-07-24 00:08:37 +00:00
|
|
|
-- element as string
|
|
|
|
function self.mt.__tostring()
|
2022-07-28 14:09:34 +00:00
|
|
|
return "graphics.element{" .. self.elem_type .. "} @ " .. tostring(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
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-------------------------
|
|
|
|
-- PROTECTED FUNCTIONS --
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
-- prepare the template
|
|
|
|
---@param offset_x integer x offset
|
|
|
|
---@param offset_y integer y offset
|
|
|
|
---@param next_y integer next line if no y was provided
|
|
|
|
function protected.prepare_template(offset_x, offset_y, next_y)
|
|
|
|
-- 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
|
|
|
|
protected.frame.y = args.y or next_y
|
|
|
|
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
|
|
|
-- inner offsets
|
|
|
|
if args.offset_x ~= nil then self.child_offset.x = args.offset_x end
|
|
|
|
if args.offset_y ~= nil then self.child_offset.y = args.offset_y end
|
|
|
|
|
|
|
|
-- adjust window frame if applicable
|
|
|
|
local f = protected.frame
|
|
|
|
local x = f.x
|
|
|
|
local y = f.y
|
|
|
|
|
|
|
|
-- apply offsets
|
|
|
|
if args.parent ~= nil then
|
|
|
|
-- constrain to parent inner width/height
|
|
|
|
local w, h = self.p_window.getSize()
|
|
|
|
f.w = math.min(f.w, w - ((2 * offset_x) + (f.x - 1)))
|
|
|
|
f.h = math.min(f.h, h - ((2 * offset_y) + (f.y - 1)))
|
|
|
|
|
|
|
|
-- offset x/y
|
|
|
|
f.x = x + offset_x
|
|
|
|
f.y = y + offset_y
|
|
|
|
end
|
2022-06-25 18:27:15 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- check frame
|
|
|
|
assert(f.x >= 1, "graphics.element{" .. self.elem_type .. "}: frame x not >= 1")
|
|
|
|
assert(f.y >= 1, "graphics.element{" .. self.elem_type .. "}: frame y not >= 1")
|
|
|
|
assert(f.w >= 1, "graphics.element{" .. self.elem_type .. "}: frame width not >= 1")
|
|
|
|
assert(f.h >= 1, "graphics.element{" .. self.elem_type .. "}: frame height not >= 1")
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- create window
|
|
|
|
protected.window = window.create(self.p_window, f.x, f.y, f.w, f.h, 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
|
|
|
|
protected.fg_bg = args.fg_bg
|
|
|
|
elseif args.parent ~= nil then
|
|
|
|
protected.fg_bg = args.parent.get_fg_bg()
|
|
|
|
end
|
2022-06-16 15:19:32 +00:00
|
|
|
|
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
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- calculate bounds
|
|
|
|
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
|
|
|
|
end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
|
|
|
-- handle a touch event
|
|
|
|
---@param event table monitor_touch event
|
|
|
|
function protected.handle_touch(event)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle data value changes
|
2022-10-20 16:22:45 +00:00
|
|
|
---@vararg any value(s)
|
2022-06-06 19:42:39 +00:00
|
|
|
function protected.on_update(...)
|
|
|
|
end
|
|
|
|
|
2022-11-06 23:41:52 +00:00
|
|
|
-- callback on control press responses
|
|
|
|
---@param result any
|
|
|
|
function protected.response_callback(result)
|
|
|
|
end
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
-- get value
|
2022-06-11 20:44:31 +00:00
|
|
|
function protected.get_value()
|
2022-09-12 16:59:28 +00:00
|
|
|
return protected.value
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set value
|
|
|
|
---@param value any value to set
|
|
|
|
function protected.set_value(value)
|
2022-10-20 16:22:45 +00:00
|
|
|
end
|
|
|
|
|
2022-11-17 17:00:00 +00:00
|
|
|
-- set minimum input value
|
|
|
|
---@param min integer minimum allowed value
|
|
|
|
function protected.set_min(min)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set maximum input value
|
|
|
|
---@param max integer maximum allowed value
|
|
|
|
function protected.set_max(max)
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:22:45 +00:00
|
|
|
-- enable the control
|
|
|
|
function protected.enable()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable the control
|
|
|
|
function protected.disable()
|
2022-06-11 20:44:31 +00:00
|
|
|
end
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
-- custom recolor command, varies by element if implemented
|
|
|
|
---@vararg cpair|color color(s)
|
|
|
|
function protected.recolor(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- custom resize command, varies by element if implemented
|
|
|
|
---@vararg integer sizing
|
|
|
|
function protected.resize(...)
|
|
|
|
end
|
|
|
|
|
2022-09-10 19:14:48 +00:00
|
|
|
-- start animations
|
|
|
|
function protected.start_anim()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- stop animations
|
|
|
|
function protected.stop_anim()
|
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- get public interface
|
|
|
|
---@return graphics_element element, element_id id
|
|
|
|
function protected.get() return public, self.id end
|
2022-06-06 19:42:39 +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
|
|
|
|
assert(self.p_window, "graphics.element{" .. self.elem_type .. "}: no parent window provided")
|
2022-07-28 14:09:34 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- prepare the template
|
|
|
|
if args.parent == nil then
|
|
|
|
protected.prepare_template(0, 0, 1)
|
|
|
|
else
|
|
|
|
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
|
|
|
|
function public.window() return protected.window end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- CHILD ELEMENTS --
|
|
|
|
|
2022-07-24 00:08:37 +00:00
|
|
|
-- add a child element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@param key string|nil id
|
2022-07-28 15:17:34 +00:00
|
|
|
---@param child graphics_template
|
|
|
|
---@return integer|string key
|
2022-07-28 14:09:34 +00:00
|
|
|
function public.__add_child(key, child)
|
2022-07-28 15:17:34 +00:00
|
|
|
child.prepare_template(self.child_offset.x, self.child_offset.y, self.next_y)
|
|
|
|
|
|
|
|
self.next_y = child.frame.y + child.frame.h
|
|
|
|
|
|
|
|
local child_element = child.get()
|
|
|
|
|
2022-07-28 14:09:34 +00:00
|
|
|
if key == nil then
|
2022-07-28 15:17:34 +00:00
|
|
|
table.insert(self.children, child_element)
|
|
|
|
return #self.children
|
2022-07-28 14:09:34 +00:00
|
|
|
else
|
2022-07-28 15:17:34 +00:00
|
|
|
self.children[key] = child_element
|
|
|
|
return key
|
2022-07-28 14:09:34 +00:00
|
|
|
end
|
|
|
|
end
|
2022-07-24 00:08:37 +00:00
|
|
|
|
|
|
|
-- get a child element
|
|
|
|
---@return graphics_element
|
|
|
|
function public.get_child(key) return self.children[key] end
|
|
|
|
|
|
|
|
-- remove child
|
|
|
|
---@param key string|integer
|
|
|
|
function public.remove(key) self.children[key] = nil end
|
|
|
|
|
2022-07-28 14:15:12 +00:00
|
|
|
-- attempt to get a child element by ID (does not include this element itself)
|
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)
|
2022-07-28 14:15:12 +00:00
|
|
|
if self.children[id] == nil then
|
|
|
|
for _, child in pairs(self.children) do
|
|
|
|
local elem = child.get_element_by_id(id)
|
|
|
|
if elem ~= nil then return elem end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return self.children[id]
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
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
|
|
|
|
function public.line_break() self.next_y = self.next_y + 1 end
|
|
|
|
|
|
|
|
-- PROPERTIES --
|
|
|
|
|
2022-06-16 15:19:32 +00:00
|
|
|
-- get the foreground/background colors
|
2022-09-12 17:53:39 +00:00
|
|
|
---@return cpair fg_bg
|
2022-06-16 15:19:32 +00:00
|
|
|
function public.get_fg_bg() return protected.fg_bg end
|
|
|
|
|
2022-11-26 21:18:31 +00:00
|
|
|
-- get element x
|
|
|
|
---@return integer x
|
|
|
|
function public.get_x()
|
|
|
|
return protected.frame.x
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get element y
|
|
|
|
---@return integer y
|
|
|
|
function public.get_y()
|
|
|
|
return protected.frame.y
|
|
|
|
end
|
|
|
|
|
2022-07-14 17:47:39 +00:00
|
|
|
-- get element width
|
2022-09-12 17:53:39 +00:00
|
|
|
---@return integer width
|
2022-07-14 17:47:39 +00:00
|
|
|
function public.width()
|
|
|
|
return protected.frame.w
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get element height
|
2022-09-12 17:53:39 +00:00
|
|
|
---@return integer height
|
2022-07-14 17:47:39 +00:00
|
|
|
function public.height()
|
|
|
|
return protected.frame.h
|
|
|
|
end
|
|
|
|
|
2022-09-12 17:53:39 +00:00
|
|
|
-- get the element value
|
|
|
|
---@return any value
|
2022-07-28 15:17:34 +00:00
|
|
|
function public.get_value()
|
|
|
|
return protected.get_value()
|
|
|
|
end
|
|
|
|
|
2022-09-12 17:53:39 +00:00
|
|
|
-- set the element value
|
|
|
|
---@param value any new value
|
|
|
|
function public.set_value(value)
|
|
|
|
protected.set_value(value)
|
|
|
|
end
|
|
|
|
|
2022-11-17 17:00:00 +00:00
|
|
|
-- set minimum input value
|
|
|
|
---@param min integer minimum allowed value
|
|
|
|
function public.set_min(min)
|
|
|
|
protected.set_min(min)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set maximum input value
|
|
|
|
---@param max integer maximum allowed value
|
|
|
|
function public.set_max(max)
|
|
|
|
protected.set_max(max)
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:22:45 +00:00
|
|
|
-- enable the element
|
|
|
|
function public.enable()
|
|
|
|
protected.enabled = true
|
|
|
|
protected.enable()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable the element
|
|
|
|
function public.disable()
|
|
|
|
protected.enabled = false
|
|
|
|
protected.disable()
|
|
|
|
end
|
|
|
|
|
2022-12-11 15:51:45 +00:00
|
|
|
-- custom recolor command, varies by element if implemented
|
|
|
|
---@vararg cpair|color color(s)
|
|
|
|
function public.recolor(...)
|
|
|
|
protected.recolor(...)
|
|
|
|
end
|
|
|
|
|
2022-09-12 17:53:39 +00:00
|
|
|
-- resize attributes of the element value if supported
|
|
|
|
---@vararg number dimensions (element specific)
|
|
|
|
function public.resize(...)
|
|
|
|
protected.resize(...)
|
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- FUNCTION CALLBACKS --
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- handle a monitor touch
|
|
|
|
---@param event monitor_touch monitor touch event
|
|
|
|
function public.handle_touch(event)
|
|
|
|
local in_x = event.x >= self.bounds.x1 and event.x <= self.bounds.x2
|
|
|
|
local in_y = event.y >= self.bounds.y1 and event.y <= self.bounds.y2
|
|
|
|
|
|
|
|
if in_x and in_y then
|
2022-07-24 00:08:37 +00:00
|
|
|
local event_T = core.events.touch(event.monitor, (event.x - self.position.x) + 1, (event.y - self.position.y) + 1)
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- handle the touch event, transformed into the window frame
|
2022-07-24 00:08:37 +00:00
|
|
|
protected.handle_touch(event_T)
|
|
|
|
|
|
|
|
-- pass on touch event to children
|
|
|
|
for _, val in pairs(self.children) do val.handle_touch(event_T) end
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- draw the element given new data
|
2022-09-12 17:53:39 +00:00
|
|
|
---@vararg any new data
|
2022-06-08 18:15:34 +00:00
|
|
|
function public.update(...)
|
2022-06-06 19:42:39 +00:00
|
|
|
protected.on_update(...)
|
|
|
|
end
|
|
|
|
|
2022-11-06 23:41:52 +00:00
|
|
|
-- on a control request response
|
|
|
|
---@param result any
|
|
|
|
function public.on_response(result)
|
|
|
|
protected.response_callback(result)
|
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
-- VISIBILITY --
|
2022-06-11 20:44:31 +00:00
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- show the element
|
|
|
|
function public.show()
|
|
|
|
protected.window.setVisible(true)
|
2022-09-10 19:14:48 +00:00
|
|
|
protected.start_anim()
|
|
|
|
|
|
|
|
for i = 1, #self.children do
|
|
|
|
self.children[i].show()
|
|
|
|
end
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- hide the element
|
|
|
|
function public.hide()
|
2022-09-10 19:14:48 +00:00
|
|
|
protected.stop_anim()
|
|
|
|
for i = 1, #self.children do
|
|
|
|
self.children[i].hide()
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
protected.window.setVisible(false)
|
|
|
|
end
|
|
|
|
|
2022-06-08 18:15:34 +00:00
|
|
|
-- re-draw the element
|
|
|
|
function public.redraw()
|
|
|
|
protected.window.redraw()
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
return protected
|
|
|
|
end
|
|
|
|
|
|
|
|
return element
|