#91 support resizing core map per reactor dimension updates

This commit is contained in:
Mikayla Fischler 2022-09-12 13:53:39 -04:00
parent 10c53ac4b3
commit e0ab2ade89
2 changed files with 81 additions and 38 deletions

View File

@ -251,23 +251,39 @@ function element.new(args)
-- PROPERTIES -- -- PROPERTIES --
-- get the foreground/background colors -- get the foreground/background colors
---@return cpair fg_bg
function public.get_fg_bg() return protected.fg_bg end function public.get_fg_bg() return protected.fg_bg end
-- get element width -- get element width
---@return integer width
function public.width() function public.width()
return protected.frame.w return protected.frame.w
end end
-- get element height -- get element height
---@return integer height
function public.height() function public.height()
return protected.frame.h return protected.frame.h
end end
-- get the control value reading -- get the element value
---@return any value
function public.get_value() function public.get_value()
return protected.get_value() return protected.get_value()
end end
-- set the element value
---@param value any new value
function public.set_value(value)
protected.set_value(value)
end
-- resize attributes of the element value if supported
---@vararg number dimensions (element specific)
function public.resize(...)
protected.resize(...)
end
-- FUNCTION CALLBACKS -- -- FUNCTION CALLBACKS --
-- handle a monitor touch -- handle a monitor touch
@ -288,6 +304,7 @@ function element.new(args)
end end
-- draw the element given new data -- draw the element given new data
---@vararg any new data
function public.update(...) function public.update(...)
protected.on_update(...) protected.on_update(...)
end end

View File

@ -20,8 +20,9 @@ local function core_map(args)
assert(util.is_int(args.reactor_l), "graphics.elements.indicators.coremap: reactor_l is a required field") assert(util.is_int(args.reactor_l), "graphics.elements.indicators.coremap: reactor_l is a required field")
assert(util.is_int(args.reactor_w), "graphics.elements.indicators.coremap: reactor_w is a required field") assert(util.is_int(args.reactor_w), "graphics.elements.indicators.coremap: reactor_w is a required field")
args.width = args.reactor_l -- require max dimensions
args.height = args.reactor_w args.width = 18
args.height = 18
-- inherit only foreground color -- inherit only foreground color
args.fg_bg = core.graphics.cpair(args.parent.get_fg_bg().fgd, colors.gray) args.fg_bg = core.graphics.cpair(args.parent.get_fg_bg().fgd, colors.gray)
@ -29,21 +30,19 @@ local function core_map(args)
-- create new graphics element base object -- create new graphics element base object
local e = element.new(args) local e = element.new(args)
local start_x = 2
local start_y = 2
local inner_width = e.frame.w - 2
local inner_height = e.frame.h - 2
local alternator = true local alternator = true
-- check dimensions local shift_x = 0
assert(inner_width > 0, "graphics.elements.indicators.coremap: inner_width <= 0") local shift_y = 0
assert(inner_height > 0, "graphics.elements.indicators.coremap: inner_height <= 0")
assert(start_x <= inner_width, "graphics.elements.indicators.coremap: start_x > inner_width")
assert(start_y <= inner_height, "graphics.elements.indicators.coremap: start_y > inner_height")
-- label coordinates local start_x = 2 + shift_x
local start_y = 2 + shift_y
local inner_width = e.frame.w - start_x
local inner_height = e.frame.h - start_y
-- create coordinate grid and frame
local function draw_frame()
e.window.setTextColor(colors.white) e.window.setTextColor(colors.white)
for x = 0, (inner_width - 1) do for x = 0, (inner_width - 1) do
@ -63,10 +62,11 @@ local function core_map(args)
e.window.write(util.strrep("\x8f", e.frame.w)) e.window.write(util.strrep("\x8f", e.frame.w))
e.window.setTextColor(e.fg_bg.fgd) e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg) e.window.setBackgroundColor(e.fg_bg.bkg)
end
-- draw the core -- draw the core
---@param t number temperature in K ---@param t number temperature in K
local function draw(t) local function draw_core(t)
local i = 1 local i = 1
local back_c = "F" local back_c = "F"
local text_c = "8" local text_c = "8"
@ -99,9 +99,7 @@ local function core_map(args)
-- draw pattern -- draw pattern
for y = start_y, inner_height + (start_y - 1) do for y = start_y, inner_height + (start_y - 1) do
e.window.setCursorPos(start_x, y) e.window.setCursorPos(start_x, y)
for x = 1, inner_width do for _ = 1, inner_width do
local str = util.sprintf("%02X", i)
if alternator then if alternator then
i = i + 1 i = i + 1
e.window.blit("\x07", text_c, back_c) e.window.blit("\x07", text_c, back_c)
@ -120,13 +118,41 @@ local function core_map(args)
---@param temperature number temperature in Kelvin ---@param temperature number temperature in Kelvin
function e.on_update(temperature) function e.on_update(temperature)
e.value = temperature e.value = temperature
draw(temperature) draw_core(e.value)
end end
-- set temperature to display
---@param val number degrees K
function e.set_value(val) e.on_update(val) end function e.set_value(val) e.on_update(val) end
-- initial draw at base temp -- resize reactor dimensions
e.on_update(300) ---@param reactor_l integer reactor length (rendered in 2D top-down as width)
---@param reactor_w integer reactor width (rendered in 2D top-down as height)
function e.resize(reactor_l, reactor_w)
-- enforce possible dimensions
if reactor_l > 16 then reactor_l = 16 elseif reactor_l < 3 then reactor_l = 3 end
if reactor_w > 16 then reactor_w = 16 elseif reactor_w < 3 then reactor_w = 3 end
-- update dimensions
shift_x = 8 - math.floor(reactor_l / 2)
shift_y = 8 - math.floor(reactor_w / 2)
start_x = 2 + shift_x
start_y = 2 + shift_y
inner_width = reactor_l
inner_height = reactor_w
e.window.clear()
-- re-draw
draw_frame()
e.on_update(e.value)
end
-- initial (one-time except for resize()) frame draw
draw_frame()
-- initial draw
e.on_update(0)
return e.get() return e.get()
end end