mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#84 auto-incrementing x with line break function, removed need for get_offset by having parent prepare child template
This commit is contained in:
@ -11,7 +11,7 @@ local element = {}
|
|||||||
---@field parent? graphics_element
|
---@field parent? graphics_element
|
||||||
---@field id? string element id
|
---@field id? string element id
|
||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? integer 1 if omitted
|
---@field y? integer next line if omitted
|
||||||
---@field offset_x? integer 0 if omitted
|
---@field offset_x? integer 0 if omitted
|
||||||
---@field offset_y? integer 0 if omitted
|
---@field offset_y? integer 0 if omitted
|
||||||
---@field width? integer parent width if omitted
|
---@field width? integer parent width if omitted
|
||||||
@ -30,6 +30,7 @@ function element.new(args)
|
|||||||
position = { x = 1, y = 1 },
|
position = { x = 1, y = 1 },
|
||||||
child_offset = { x = 0, y = 0 },
|
child_offset = { x = 0, y = 0 },
|
||||||
bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1},
|
bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1},
|
||||||
|
next_y = 1,
|
||||||
children = {},
|
children = {},
|
||||||
mt = {}
|
mt = {}
|
||||||
}
|
}
|
||||||
@ -46,17 +47,20 @@ function element.new(args)
|
|||||||
return "graphics.element{" .. self.elem_type .. "} @ " .. tostring(self)
|
return "graphics.element{" .. self.elem_type .. "} @ " .. tostring(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- SETUP --
|
---@class graphics_element
|
||||||
|
local public = {}
|
||||||
|
|
||||||
-- get the parent window
|
setmetatable(public, self.mt)
|
||||||
self.p_window = args.window
|
|
||||||
if self.p_window == nil and args.parent ~= nil then
|
|
||||||
self.p_window = args.parent.window()
|
|
||||||
end
|
|
||||||
|
|
||||||
-- check window
|
-------------------------
|
||||||
assert(self.p_window, "graphics.element{" .. self.elem_type .. "}: no parent window provided")
|
-- 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
|
-- get frame coordinates/size
|
||||||
if args.gframe ~= nil then
|
if args.gframe ~= nil then
|
||||||
protected.frame.x = args.gframe.x
|
protected.frame.x = args.gframe.x
|
||||||
@ -66,7 +70,7 @@ function element.new(args)
|
|||||||
else
|
else
|
||||||
local w, h = self.p_window.getSize()
|
local w, h = self.p_window.getSize()
|
||||||
protected.frame.x = args.x or 1
|
protected.frame.x = args.x or 1
|
||||||
protected.frame.y = args.y or 1
|
protected.frame.y = args.y or next_y
|
||||||
protected.frame.w = args.width or w
|
protected.frame.w = args.width or w
|
||||||
protected.frame.h = args.height or h
|
protected.frame.h = args.height or h
|
||||||
end
|
end
|
||||||
@ -82,15 +86,14 @@ function element.new(args)
|
|||||||
|
|
||||||
-- apply offsets
|
-- apply offsets
|
||||||
if args.parent ~= nil then
|
if args.parent ~= nil then
|
||||||
-- offset x/y
|
|
||||||
local offset_x, offset_y = args.parent.get_offset()
|
|
||||||
x = x + offset_x
|
|
||||||
y = y + offset_y
|
|
||||||
|
|
||||||
-- constrain to parent inner width/height
|
-- constrain to parent inner width/height
|
||||||
local w, h = self.p_window.getSize()
|
local w, h = self.p_window.getSize()
|
||||||
f.w = math.min(f.w, w - ((2 * offset_x) + (f.x - 1)))
|
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)))
|
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
|
end
|
||||||
|
|
||||||
-- check frame
|
-- check frame
|
||||||
@ -100,7 +103,7 @@ function element.new(args)
|
|||||||
assert(f.h >= 1, "graphics.element{" .. self.elem_type .. "}: frame height not >= 1")
|
assert(f.h >= 1, "graphics.element{" .. self.elem_type .. "}: frame height not >= 1")
|
||||||
|
|
||||||
-- create window
|
-- create window
|
||||||
protected.window = window.create(self.p_window, x, y, f.w, f.h, true)
|
protected.window = window.create(self.p_window, f.x, f.y, f.w, f.h, true)
|
||||||
|
|
||||||
-- init colors
|
-- init colors
|
||||||
if args.fg_bg ~= nil then
|
if args.fg_bg ~= nil then
|
||||||
@ -122,8 +125,7 @@ function element.new(args)
|
|||||||
self.bounds.x2 = self.position.x + f.w - 1
|
self.bounds.x2 = self.position.x + f.w - 1
|
||||||
self.bounds.y1 = self.position.y
|
self.bounds.y1 = self.position.y
|
||||||
self.bounds.y2 = self.position.y + f.h - 1
|
self.bounds.y2 = self.position.y + f.h - 1
|
||||||
|
end
|
||||||
-- PROTECTED FUNCTIONS --
|
|
||||||
|
|
||||||
-- handle a touch event
|
-- handle a touch event
|
||||||
---@param event table monitor_touch event
|
---@param event table monitor_touch event
|
||||||
@ -139,43 +141,56 @@ function element.new(args)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class graphics_element
|
-- get public interface
|
||||||
local public = {}
|
|
||||||
|
|
||||||
setmetatable(public, self.mt)
|
|
||||||
|
|
||||||
-- get public interface and wrap up element creation
|
|
||||||
---@return graphics_element element, element_id id
|
---@return graphics_element element, element_id id
|
||||||
function protected.complete()
|
function protected.get() return public, self.id end
|
||||||
if not self.define_completed then
|
|
||||||
self.define_completed = true
|
|
||||||
|
|
||||||
if args.parent then
|
-----------
|
||||||
self.id = args.parent.__add_child(args.id, public)
|
-- SETUP --
|
||||||
|
-----------
|
||||||
|
|
||||||
|
-- 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
|
end
|
||||||
|
|
||||||
return public, self.id
|
-- check window
|
||||||
|
assert(self.p_window, "graphics.element{" .. self.elem_type .. "}: no parent window provided")
|
||||||
|
|
||||||
|
-- prepare the template
|
||||||
|
if args.parent == nil then
|
||||||
|
protected.prepare_template(0, 0, 1)
|
||||||
else
|
else
|
||||||
assert("graphics.element{" .. self.elem_type .. "}: illegal duplicate call to complete()")
|
self.id = args.parent.__add_child(args.id, protected)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
----------------------
|
||||||
-- PUBLIC FUNCTIONS --
|
-- PUBLIC FUNCTIONS --
|
||||||
|
----------------------
|
||||||
|
|
||||||
-- get the window object
|
-- get the window object
|
||||||
function public.window() return protected.window end
|
function public.window() return protected.window end
|
||||||
|
|
||||||
|
-- CHILD ELEMENTS --
|
||||||
|
|
||||||
-- add a child element
|
-- add a child element
|
||||||
---@param key string|nil id
|
---@param key string|nil id
|
||||||
---@param child graphics_element
|
---@param child graphics_template
|
||||||
---@return graphics_element element, integer|string key
|
---@return integer|string key
|
||||||
function public.__add_child(key, child)
|
function public.__add_child(key, child)
|
||||||
|
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()
|
||||||
|
|
||||||
if key == nil then
|
if key == nil then
|
||||||
table.insert(self.children, child)
|
table.insert(self.children, child_element)
|
||||||
return child, #self.children
|
return #self.children
|
||||||
else
|
else
|
||||||
self.children[key] = child
|
self.children[key] = child_element
|
||||||
return child, key
|
return key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -203,15 +218,16 @@ function element.new(args)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- AUTO-PLACEMENT --
|
||||||
|
|
||||||
|
-- skip a line for automatically placed elements
|
||||||
|
function public.line_break() self.next_y = self.next_y + 1 end
|
||||||
|
|
||||||
|
-- PROPERTIES --
|
||||||
|
|
||||||
-- get the foreground/background colors
|
-- get the foreground/background colors
|
||||||
function public.get_fg_bg() return protected.fg_bg end
|
function public.get_fg_bg() return protected.fg_bg end
|
||||||
|
|
||||||
-- get offset from this element's frame
|
|
||||||
---@return integer x, integer y
|
|
||||||
function public.get_offset()
|
|
||||||
return self.child_offset.x, self.child_offset.y
|
|
||||||
end
|
|
||||||
|
|
||||||
-- get element width
|
-- get element width
|
||||||
function public.width()
|
function public.width()
|
||||||
return protected.frame.w
|
return protected.frame.w
|
||||||
@ -222,6 +238,13 @@ function element.new(args)
|
|||||||
return protected.frame.h
|
return protected.frame.h
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- get the control value reading
|
||||||
|
function public.get_value()
|
||||||
|
return protected.get_value()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- FUNCTION CALLBACKS --
|
||||||
|
|
||||||
-- handle a monitor touch
|
-- handle a monitor touch
|
||||||
---@param event monitor_touch monitor touch event
|
---@param event monitor_touch monitor touch event
|
||||||
function public.handle_touch(event)
|
function public.handle_touch(event)
|
||||||
@ -244,10 +267,7 @@ function element.new(args)
|
|||||||
protected.on_update(...)
|
protected.on_update(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- get the control value reading
|
-- VISIBILITY --
|
||||||
function public.get_value()
|
|
||||||
return protected.get_value()
|
|
||||||
end
|
|
||||||
|
|
||||||
-- show the element
|
-- show the element
|
||||||
function public.show()
|
function public.show()
|
||||||
|
@ -63,7 +63,7 @@ local function push_button(args)
|
|||||||
args.callback()
|
args.callback()
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return push_button
|
return push_button
|
||||||
|
@ -65,7 +65,7 @@ local function scram_button(args)
|
|||||||
args.callback()
|
args.callback()
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return scram_button
|
return scram_button
|
||||||
|
@ -112,7 +112,7 @@ local function spinbox(args)
|
|||||||
---@return number|integer
|
---@return number|integer
|
||||||
function e.get_value() return value end
|
function e.get_value() return value end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return spinbox
|
return spinbox
|
||||||
|
@ -73,7 +73,7 @@ local function switch_button(args)
|
|||||||
args.callback(state)
|
args.callback(state)
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return switch_button
|
return switch_button
|
||||||
|
@ -15,7 +15,7 @@ local element = require("graphics.element")
|
|||||||
---@param args displaybox_args
|
---@param args displaybox_args
|
||||||
local function displaybox(args)
|
local function displaybox(args)
|
||||||
-- create new graphics element base object
|
-- create new graphics element base object
|
||||||
return element.new(args).complete()
|
return element.new(args).get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return displaybox
|
return displaybox
|
||||||
|
@ -17,7 +17,7 @@ local element = require("graphics.element")
|
|||||||
---@return graphics_element element, element_id id
|
---@return graphics_element element, element_id id
|
||||||
local function div(args)
|
local function div(args)
|
||||||
-- create new graphics element base object
|
-- create new graphics element base object
|
||||||
return element.new(args).complete()
|
return element.new(args).get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return div
|
return div
|
||||||
|
@ -93,7 +93,7 @@ local function data(args)
|
|||||||
-- initial value draw
|
-- initial value draw
|
||||||
e.on_update(args.value)
|
e.on_update(args.value)
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
@ -99,7 +99,7 @@ local function hbar(args)
|
|||||||
-- initialize to 0
|
-- initialize to 0
|
||||||
e.on_update(0)
|
e.on_update(0)
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return hbar
|
return hbar
|
||||||
|
@ -62,7 +62,7 @@ local function icon(args)
|
|||||||
-- initial icon draw
|
-- initial icon draw
|
||||||
e.on_update(args.value or 1)
|
e.on_update(args.value or 1)
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return icon
|
return icon
|
||||||
|
@ -45,7 +45,7 @@ local function indicator_light(args)
|
|||||||
e.on_update(false)
|
e.on_update(false)
|
||||||
e.window.write(args.label)
|
e.window.write(args.label)
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return indicator_light
|
return indicator_light
|
||||||
|
@ -72,7 +72,7 @@ local function state_indicator(args)
|
|||||||
-- initial draw
|
-- initial draw
|
||||||
e.on_update(args.value or 1)
|
e.on_update(args.value or 1)
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return state_indicator
|
return state_indicator
|
||||||
|
@ -78,7 +78,7 @@ local function vbar(args)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return vbar
|
return vbar
|
||||||
|
@ -141,7 +141,7 @@ local function pipenet(args)
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return pipenet
|
return pipenet
|
||||||
|
@ -110,7 +110,7 @@ local function rectangle(args)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return rectangle
|
return rectangle
|
||||||
|
@ -52,7 +52,7 @@ local function textbox(args)
|
|||||||
e.window.write(lines[i])
|
e.window.write(lines[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return textbox
|
return textbox
|
||||||
|
@ -79,7 +79,7 @@ local function tiling(args)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.complete()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
|
||||||
return tiling
|
return tiling
|
||||||
|
Reference in New Issue
Block a user