From ef73c524179211e3cfd5338d7304e5746759e0af Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 29 Jun 2022 17:40:08 -0400 Subject: [PATCH] pipenet indicator instead of pipe indicator --- graphics/core.lua | 36 +++++++ graphics/elements/indicators/pipe.lua | 110 ------------------- graphics/elements/indicators/pipenet.lua | 129 +++++++++++++++++++++++ 3 files changed, 165 insertions(+), 110 deletions(-) delete mode 100644 graphics/elements/indicators/pipe.lua create mode 100644 graphics/elements/indicators/pipenet.lua diff --git a/graphics/core.lua b/graphics/core.lua index 9e7ae4b..6e4eb92 100644 --- a/graphics/core.lua +++ b/graphics/core.lua @@ -103,6 +103,42 @@ function graphics.cpair(a, b) } end +---@class pipe +---@field x1 integer starting x, origin is 0 +---@field y1 integer starting y, origin is 0 +---@field x2 integer ending x, origin is 0 +---@field y2 integer ending y, origin is 0 +---@field w integer width +---@field h integer height +---@field color color pipe color +---@field thin boolean true for 1 subpixels, false (default) for 2 +---@field align_tr boolean false to align bottom left (default), true to align top right + +-- create a new pipe +-- +-- note: pipe coordinate origin is (0, 0) +---@param x1 integer starting x, origin is 0 +---@param y1 integer starting y, origin is 0 +---@param x2 integer ending x, origin is 0 +---@param y2 integer ending y, origin is 0 +---@param color color pipe color +---@param thin? boolean true for 1 subpixels, false (default) for 2 +---@param align_tr? boolean false to align bottom left (default), true to align top right +---@return pipe +function graphics.pipe(x1, y1, x2, y2, color, thin, align_tr) + return { + x1 = x1, + y1 = y1, + x2 = x2, + y2 = y2, + w = x2 - x1, + h = y2 - y1, + color = color, + thin = thin or false, + align_tr = align_tr or false + } +end + core.graphics = graphics return core diff --git a/graphics/elements/indicators/pipe.lua b/graphics/elements/indicators/pipe.lua deleted file mode 100644 index fbd77e8..0000000 --- a/graphics/elements/indicators/pipe.lua +++ /dev/null @@ -1,110 +0,0 @@ --- Pipe Graphics Element - -local util = require("scada-common.util") - -local element = require("graphics.element") - ----@class pipe_args ----@field parent graphics_element ----@field x? integer 1 if omitted ----@field y? integer 1 if omitted ----@field end_x integer end of pipe ----@field end_y integer end of pipe ----@field thin boolean true for 1 subpixels, false (default) for 2 ----@field align_tr? boolean false to align bottom left (default), true to align top right ----@field fg_bg? cpair foreground/background colors - --- new pipe ----@param args pipe_args -local function pipe(args) - assert(util.is_int(args.end_x), "graphics.elements.indicators.pipe: end_x is a required field") - assert(util.is_int(args.end_y), "graphics.elements.indicators.pipe: end_y is a required field") - - args.x = args.x or 1 - args.y = args.y or 1 - args.width = args.end_x - args.x - args.height = args.end_y - args.y - - -- create new graphics element base object - local e = element.new(args) - - -- draw pipe - - local align_tr = args.align_tr or false - - local x = 1 - local y = 1 - - if align_tr then - -- cross width then height - for i = 1, args.width do - if args.thin then - if i == args.width then - -- corner - e.window.blit("\x93", e.fg_bg.blit_bkg, e.fg_bg.blit_fgd) - else - e.window.blit("\x8c", e.fg_bg.blit_fgd, e.fg_bg.blit_bkg) - end - else - if i == args.width then - -- corner - e.window.blit(" ", e.fg_bg.blit_bkg, e.fg_bg.blit_fgd) - else - e.window.blit("\x8f", e.fg_bg.blit_fgd, e.fg_bg.blit_bkg) - end - end - - x = x + 1 - e.window.setCursorPos(x, y) - end - - -- back up one - x = x - 1 - - for _ = 1, args.height do - y = y + 1 - e.window.setCursorPos(x, y) - - if args.thin then - e.window.blit("\x95", e.fg_bg.blit_bkg, e.fg_bg.blit_fgd) - else - e.window.blit(" ", e.fg_bg.blit_bkg, e.fg_bg.blit_fgd) - end - end - else - -- cross height then width - for i = 1, args.height do - if args.thin then - if i == args.height then - -- corner - e.window.blit("\x8d", e.fg_bg.blit_fgd, e.fg_bg.blit_bkg) - else - e.window.blit("\x95", e.fg_bg.blit_fgd, e.fg_bg.blit_bkg) - end - else - e.window.blit(" ", e.fg_bg.blit_bkg, e.fg_bg.blit_fgd) - end - - y = y + 1 - e.window.setCursorPos(x, y) - end - - -- back up one - y = y - 1 - - for _ = 1, args.width do - x = x + 1 - e.window.setCursorPos(x, y) - - if args.thin then - e.window.blit("\x8c", e.fg_bg.blit_fgd, e.fg_bg.blit_bkg) - else - e.window.blit("\x83", e.fg_bg.blit_bkg, e.fg_bg.blit_fgd) - end - end - end - - return e.get() -end - -return pipe diff --git a/graphics/elements/indicators/pipenet.lua b/graphics/elements/indicators/pipenet.lua new file mode 100644 index 0000000..c375b3a --- /dev/null +++ b/graphics/elements/indicators/pipenet.lua @@ -0,0 +1,129 @@ +-- Pipe Graphics Element + +local util = require("scada-common.util") + +local core = require("graphics.core") +local element = require("graphics.element") + +---@class pipenet_args +---@field parent graphics_element +---@field x? integer 1 if omitted +---@field y? integer 1 if omitted +---@field pipes table pipe list +---@field bg? color background color + +-- new pipe network +---@param args pipenet_args +local function pipenet(args) + assert(type(args.pipes) == "table", "graphics.elements.indicators.pipenet: pipes is a required field") + + args.width = 0 + args.height = 0 + + -- determine width/height + for i = 1, #args.pipes do + local pipe = args.pipes[i] ---@type pipe + + local true_w = pipe.w + pipe.x1 + local true_h = pipe.h + pipe.y1 + + if true_w > args.width then args.width = true_w end + if true_h > args.height then args.height = true_h end + end + + args.x = args.x or 1 + args.y = args.y or 1 + + if args.bg ~= nil then + args.fg_bg = core.graphics.cpair(args.bg, args.bg) + end + + -- create new graphics element base object + local e = element.new(args) + + -- draw all pipes + for p = 1, #args.pipes do + local pipe = args.pipes[p] ---@type pipe + + local x = 1 + pipe.x1 + local y = 1 + pipe.y1 + + e.window.setCursorPos(x, y) + + local c = core.graphics.cpair(pipe.color, e.fg_bg.bkg) + + if pipe.align_tr then + -- cross width then height + for i = 1, pipe.w do + if pipe.thin then + if i == pipe.w then + -- corner + e.window.blit("\x93", c.blit_bkg, c.blit_fgd) + else + e.window.blit("\x8c", c.blit_fgd, c.blit_bkg) + end + else + if i == pipe.w then + -- corner + e.window.blit(" ", c.blit_bkg, c.blit_fgd) + else + e.window.blit("\x8f", c.blit_fgd, c.blit_bkg) + end + end + + x = x + 1 + e.window.setCursorPos(x, y) + end + + -- back up one + x = x - 1 + + for _ = 1, pipe.h do + y = y + 1 + e.window.setCursorPos(x, y) + + if pipe.thin then + e.window.blit("\x95", c.blit_bkg, c.blit_fgd) + else + e.window.blit(" ", c.blit_bkg, c.blit_fgd) + end + end + else + -- cross height then width + for i = 1, pipe.h do + if pipe.thin then + if i == pipe.h then + -- corner + e.window.blit("\x8d", c.blit_fgd, c.blit_bkg) + else + e.window.blit("\x95", c.blit_fgd, c.blit_bkg) + end + else + e.window.blit(" ", c.blit_bkg, c.blit_fgd) + end + + y = y + 1 + e.window.setCursorPos(x, y) + end + + -- back up one + y = y - 1 + + for _ = 1, pipe.w do + x = x + 1 + e.window.setCursorPos(x, y) + + if pipe.thin then + e.window.blit("\x8c", c.blit_fgd, c.blit_bkg) + else + e.window.blit("\x83", c.blit_bkg, c.blit_fgd) + end + end + end + + end + + return e.get() +end + +return pipenet