From fc141413219511b984d42071a63efb6fb098d4cb Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 23 Jul 2022 20:08:37 -0400 Subject: [PATCH] #73 unit overview parent/child setup, fixed touch events by setting up children for elements --- coordinator/startup.lua | 2 +- coordinator/ui/components/unit_overview.lua | 1 - coordinator/ui/layout/unit_view.lua | 6 ++- graphics/element.lua | 60 +++++++++++++++++++-- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/coordinator/startup.lua b/coordinator/startup.lua index b6728f7..e685797 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -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 diff --git a/coordinator/ui/components/unit_overview.lua b/coordinator/ui/components/unit_overview.lua index 30f887b..0f70ad8 100644 --- a/coordinator/ui/components/unit_overview.lua +++ b/coordinator/ui/components/unit_overview.lua @@ -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 diff --git a/coordinator/ui/layout/unit_view.lua b/coordinator/ui/layout/unit_view.lua index f84d216..45ce5ed 100644 --- a/coordinator/ui/layout/unit_view.lua +++ b/coordinator/ui/layout/unit_view.lua @@ -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} diff --git a/graphics/element.lua b/graphics/element.lua index 274681e..ee4b633 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -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