From 529951f9988c21167db07b564289851ac663c90a Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Fri, 7 Oct 2022 11:21:17 -0400 Subject: [PATCH] automatically show current burn rate in burn rate spinbox --- coordinator/startup.lua | 2 +- coordinator/ui/components/unit_detail.lua | 3 ++ .../elements/controls/spinbox_numeric.lua | 47 ++++++++----------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 2fbe51d..766b7ff 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -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 diff --git a/coordinator/ui/components/unit_detail.lua b/coordinator/ui/components/unit_detail.lua index 36291cd..899d50e 100644 --- a/coordinator/ui/components/unit_detail.lua +++ b/coordinator/ui/components/unit_detail.lua @@ -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", diff --git a/graphics/elements/controls/spinbox_numeric.lua b/graphics/elements/controls/spinbox_numeric.lua index ac363b2..1f61a5b 100644 --- a/graphics/elements/controls/spinbox_numeric.lua +++ b/graphics/elements/controls/spinbox_numeric.lua @@ -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()