mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#63 allow hbar to have variable height, other bar improvement
This commit is contained in:
parent
11e4d89b1d
commit
1fa87132d6
@ -5,27 +5,35 @@ local util = require("scada-common.util")
|
|||||||
local element = require("graphics.element")
|
local element = require("graphics.element")
|
||||||
|
|
||||||
---@class hbar_args
|
---@class hbar_args
|
||||||
---@field bar_fg_bg cpair bar foreground/background colors
|
---@field show_percent? boolean whether or not to show the percent
|
||||||
|
---@field bar_fg_bg? cpair bar foreground/background colors if showing percent
|
||||||
---@field parent graphics_element
|
---@field parent graphics_element
|
||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? integer 1 if omitted
|
---@field y? integer 1 if omitted
|
||||||
---@field width? integer parent width if omitted
|
---@field width? integer parent width if omitted
|
||||||
|
---@field height? integer parent height if omitted
|
||||||
|
---@field gframe? graphics_frame frame instead of x/y/width/height
|
||||||
---@field fg_bg cpair foreground/background colors
|
---@field fg_bg cpair foreground/background colors
|
||||||
|
|
||||||
-- new horizontal bar
|
-- new horizontal bar
|
||||||
---@param args hbar_args
|
---@param args hbar_args
|
||||||
local function hbar(args)
|
local function hbar(args)
|
||||||
|
-- properties/state
|
||||||
local bkg = ""
|
local bkg = ""
|
||||||
local last_num_bars = -1
|
local last_num_bars = -1
|
||||||
|
|
||||||
-- create new graphics element base object
|
-- create new graphics element base object
|
||||||
local e = element.new(args)
|
local e = element.new(args)
|
||||||
|
|
||||||
-- bar width is width - 5 characters for " 100%"
|
-- bar width is width - 5 characters for " 100%" if showing percent
|
||||||
local bar_width = e.frame.w - 5
|
local bar_width = util.trinary(args.show_percent, e.frame.w - 5, e.frame.w)
|
||||||
|
|
||||||
assert(bar_width > 0, "graphics.elements.hbar: too small for bar")
|
assert(bar_width > 0, "graphics.elements.hbar: too small for bar")
|
||||||
|
|
||||||
|
-- determine bar colors
|
||||||
|
local bar_bkg = util.trinary(args.bar_fg_bg == nil, e.fg_bg.blit_bkg, args.bar_fg_bg.blit_bkg)
|
||||||
|
local bar_fgd = util.trinary(args.bar_fg_bg == nil, e.fg_bg.blit_fgd, args.bar_fg_bg.blit_fgd)
|
||||||
|
|
||||||
-- set background blit string
|
-- set background blit string
|
||||||
bkg = util.strrep(args.bar_fg_bg.blit_bkg, bar_width)
|
bkg = util.strrep(args.bar_fg_bg.blit_bkg, bar_width)
|
||||||
|
|
||||||
@ -45,34 +53,39 @@ local function hbar(args)
|
|||||||
if num_bars ~= last_num_bars then
|
if num_bars ~= last_num_bars then
|
||||||
last_num_bars = num_bars
|
last_num_bars = num_bars
|
||||||
|
|
||||||
local bar = ""
|
local fgd = ""
|
||||||
local spaces = ""
|
local spaces = ""
|
||||||
|
|
||||||
-- fill percentage
|
-- fill percentage
|
||||||
for _ = 1, num_bars / 2 do
|
for _ = 1, num_bars / 2 do
|
||||||
spaces = spaces .. " "
|
spaces = spaces .. " "
|
||||||
bar = bar .. args.bar_fg_bg.blit_fgd
|
fgd = fgd .. bar_fgd
|
||||||
end
|
end
|
||||||
|
|
||||||
-- add fractional bar if needed
|
-- add fractional bar if needed
|
||||||
if num_bars % 2 == 1 then
|
if num_bars % 2 == 1 then
|
||||||
spaces = spaces .. "\x95"
|
spaces = spaces .. "\x95"
|
||||||
bar = bar .. args.bar_fg_bg.blit_fgd
|
fgd = fgd .. bar_fgd
|
||||||
end
|
end
|
||||||
|
|
||||||
-- pad background
|
-- pad background
|
||||||
for _ = 1, bar_width - ((num_bars / 2) + num_bars % 2) do
|
for _ = 1, bar_width - ((num_bars / 2) + num_bars % 2) do
|
||||||
spaces = spaces .. " "
|
spaces = spaces .. " "
|
||||||
bar = bar .. args.bar_fg_bg.blit_bkg
|
fgd = fgd .. bar_bkg
|
||||||
end
|
end
|
||||||
|
|
||||||
e.window.setCursorPos(1, 1)
|
-- draw bar
|
||||||
e.window.blit(spaces, bar, bkg)
|
for y = 1, e.frame.h do
|
||||||
|
e.window.setCursorPos(1, y)
|
||||||
|
e.window.blit(spaces, fgd, bkg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- update percentage
|
-- update percentage
|
||||||
e.window.setCursorPos(bar_width + 1, 1)
|
if args.show_percent then
|
||||||
e.window.write(util.sprintf("%3.0f%%", fraction * 100))
|
e.window.setCursorPos(bar_width + 1, math.max(1, math.ceil(e.frame.h / 2)))
|
||||||
|
e.window.write(util.sprintf("%3.0f%%", fraction * 100))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return e.get()
|
return e.get()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
-- Horizontal Bar Graphics Element
|
-- Vertical Bar Graphics Element
|
||||||
|
|
||||||
local util = require("scada-common.util")
|
local util = require("scada-common.util")
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ local element = require("graphics.element")
|
|||||||
-- new vertical bar
|
-- new vertical bar
|
||||||
---@param args vbar_args
|
---@param args vbar_args
|
||||||
local function vbar(args)
|
local function vbar(args)
|
||||||
-- last state
|
-- properties/state
|
||||||
local last_num_bars = -1
|
local last_num_bars = -1
|
||||||
|
|
||||||
-- create new graphics element base object
|
-- create new graphics element base object
|
||||||
|
Loading…
Reference in New Issue
Block a user