automatically show current burn rate in burn rate spinbox

This commit is contained in:
Mikayla Fischler 2022-10-07 11:21:17 -04:00
parent 573c263548
commit 529951f998
3 changed files with 24 additions and 28 deletions

View File

@ -16,7 +16,7 @@ local config = require("coordinator.config")
local coordinator = require("coordinator.coordinator")
local renderer = require("coordinator.renderer")
local COORDINATOR_VERSION = "alpha-v0.5.2"
local COORDINATOR_VERSION = "alpha-v0.5.3"
local print = util.print
local println = util.println

View File

@ -246,9 +246,12 @@ local function init(parent, id)
local burn_control = Div{parent=main,x=12,y=40,width=19,height=3,fg_bg=cpair(colors.gray,colors.white)}
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)}
TextBox{parent=burn_control,x=9,y=2,text="mB/t"}
local set_burn = function () unit.set_burn(burn_rate.get_value()) end
PushButton{parent=burn_control,x=14,y=2,text="SET",min_width=5,fg_bg=cpair(colors.black,colors.yellow),active_fg_bg=cpair(colors.white,colors.gray),callback=set_burn}
r_ps.subscribe("burn_rate", function (v) burn_rate.set_value(v) end)
local opts = {
{
text = "Auto",

View File

@ -43,11 +43,6 @@ local function spinbox(args)
-- set initial value
e.value = args.default or 0.0
local initial_str = util.sprintf(fmt_init, e.value)
---@diagnostic disable-next-line: discard-returns
initial_str:gsub("%d", function (char) table.insert(digits, char) end)
-- draw the arrows
e.window.setBackgroundColor(args.arrow_fg_bg.bkg)
e.window.setTextColor(args.arrow_fg_bg.fgd)
@ -62,18 +57,12 @@ local function spinbox(args)
e.window.write(" " .. util.strrep("\x1f", fr_prec))
end
-- zero the value
local function zero()
for i = 1, #digits do digits[i] = 0 end
e.value = 0
end
-- populate digits from current value
local function set_digits()
local initial_str = util.sprintf(fmt_init, e.value)
-- print out the current value
local function show_num()
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.setTextColor(e.fg_bg.fgd)
e.window.setCursorPos(1, 2)
e.window.write(util.sprintf(fmt, e.value))
---@diagnostic disable-next-line: discard-returns
initial_str:gsub("%d", function (char) table.insert(digits, char) end)
end
-- update the value per digits table
@ -89,11 +78,13 @@ local function spinbox(args)
end
end
-- enforce numeric limits
local function enforce_limits()
-- print out the current value
local function show_num()
-- enforce limits
-- min 0
if e.value < 0 then
zero()
for i = 1, #digits do digits[i] = 0 end
e.value = 0
-- max printable
elseif string.len(util.sprintf(fmt, e.value)) > args.width then
-- max out
@ -102,13 +93,12 @@ local function spinbox(args)
-- re-update value
update_value()
end
end
-- update value and show
local function parse_and_show()
update_value()
enforce_limits()
show_num()
-- draw
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.setTextColor(e.fg_bg.fgd)
e.window.setCursorPos(1, 2)
e.window.write(util.sprintf(fmt, e.value))
end
-- init with the default value
@ -128,7 +118,8 @@ local function spinbox(args)
digits[idx] = digits[idx] - 1
end
parse_and_show()
update_value()
show_num()
end
end
@ -136,7 +127,9 @@ local function spinbox(args)
---@param val number number to show
function e.set_value(val)
e.value = val
parse_and_show()
set_digits()
show_num()
end
return e.get()