#73 unit overview parent/child setup, fixed touch events by setting up children for elements

This commit is contained in:
Mikayla Fischler 2022-07-23 20:08:37 -04:00
parent 9b21a971fe
commit fc14141321
4 changed files with 62 additions and 7 deletions

View File

@ -15,7 +15,7 @@ local config = require("coordinator.config")
local coordinator = require("coordinator.coordinator")
local renderer = require("coordinator.renderer")
local COORDINATOR_VERSION = "alpha-v0.3.6"
local COORDINATOR_VERSION = "alpha-v0.3.7"
local print = util.print
local println = util.println

View File

@ -114,7 +114,6 @@ local function make(parent, x, y, unit)
end
else
-- boiler side pipes
local steam_pipes_a = {
-- boiler 1 steam/water pipes
pipe(0, 1, 6, 1, colors.white, false, true), -- steam boiler 1 to turbine junction

View File

@ -43,11 +43,15 @@ local function init(monitor, id)
local burn_control = Div{parent=main,x=13,y=core_height+4,width=19,height=3,fg_bg=cpair(colors.gray,colors.white)}
main(scram, burn_control)
local burn_rate = SpinboxNumeric{parent=burn_control,x=2,y=1,whole_num_precision=4,fractional_precision=1,arrow_fg_bg=cpair(colors.gray,colors.white),fg_bg=cpair(colors.black,colors.white)}
local set_burn = function () print("set burn to " .. burn_rate.get_value()) end
burn_control(burn_rate)
TextBox{parent=burn_control,x=9,y=2,text="mB/t"}
PushButton{parent=burn_control,x=14,y=2,text="SET",min_width=5,fg_bg=cpair(colors.black,colors.yellow),callback=set_burn}
burn_control(PushButton{parent=burn_control,x=14,y=2,text="SET",min_width=5,fg_bg=cpair(colors.black,colors.yellow),callback=set_burn})
local annunciator = Div{parent=main,x=34,y=core_height+4}

View File

@ -26,7 +26,9 @@ function element.new(args)
p_window = nil, ---@type table
position = { x = 1, y = 1 },
child_offset = { x = 0, y = 0 },
bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1}
bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1},
children = {},
mt = {}
}
local protected = {
@ -35,6 +37,38 @@ function element.new(args)
frame = core.graphics.gframe(1, 1, 1, 1)
}
-- append a child element without a tag
local function add_child(child)
table.insert(self.children, child)
return #self.children
end
-- add a child element without a tag
function self.mt.__add(_, child) return add_child(child) end
function self.mt.__lt(_, child) return add_child(child) end
function self.mt.__le(_, child) return add_child(child) end
-- add a child element without a tag
---@param _ table ignored (self)
---@vararg table children
---@return integer|table id/ids
function self.mt.__call(_, ...)
local children = { ... }
if #children == 1 then
return add_child(children[1])
else
local ids = {}
for _, v in ipairs(children) do table.insert(ids, add_child(v)) end
return ids
end
end
-- element as string
function self.mt.__tostring()
return "graphics.element{" .. self.elem_type .. "}"-- @ " .. tostring(self)
end
-- SETUP --
-- get the parent window
@ -131,6 +165,8 @@ function element.new(args)
---@class graphics_element
local public = {}
setmetatable(public, self.mt)
-- get public interface
function protected.get() return public end
@ -139,6 +175,19 @@ function element.new(args)
-- get the window object
function public.window() return protected.window end
-- add a child element
---@param key string id
---@param child graphics_element
function public.add_child(key, child) self.children[key] = child end
-- 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
-- get the foreground/background colors
function public.get_fg_bg() return protected.fg_bg end
@ -165,10 +214,13 @@ function element.new(args)
local in_y = event.y >= self.bounds.y1 and event.y <= self.bounds.y2
if in_x and in_y then
local event_T = core.events.touch(event.monitor, (event.x - self.position.x) + 1, (event.y - self.position.y) + 1)
-- handle the touch event, transformed into the window frame
protected.handle_touch(core.events.touch(event.monitor,
(event.x - self.position.x) + 1,
(event.y - self.position.y) + 1))
protected.handle_touch(event_T)
-- pass on touch event to children
for _, val in pairs(self.children) do val.handle_touch(event_T) end
end
end