From 9c32074b56c53dfc8e2b8855e2357fb8e6bc323c Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 17 Nov 2022 11:58:14 -0500 Subject: [PATCH] #128 limit max burn rate control to actual max burn rate --- coordinator/startup.lua | 2 +- coordinator/ui/components/unit_detail.lua | 1 + .../elements/controls/spinbox_numeric.lua | 59 ++++++++++++++----- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 36df275..9564160 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -17,7 +17,7 @@ local config = require("coordinator.config") local coordinator = require("coordinator.coordinator") local renderer = require("coordinator.renderer") -local COORDINATOR_VERSION = "alpha-v0.6.13" +local COORDINATOR_VERSION = "alpha-v0.6.14" 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 34850ef..26b2074 100644 --- a/coordinator/ui/components/unit_detail.lua +++ b/coordinator/ui/components/unit_detail.lua @@ -247,6 +247,7 @@ local function init(parent, id) 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) + r_ps.subscribe("max_burn", function (v) burn_rate.set_max(v) end) local dis_colors = cpair(colors.white, colors.lightGray) diff --git a/graphics/elements/controls/spinbox_numeric.lua b/graphics/elements/controls/spinbox_numeric.lua index 23065e2..10f74bd 100644 --- a/graphics/elements/controls/spinbox_numeric.lua +++ b/graphics/elements/controls/spinbox_numeric.lua @@ -6,6 +6,8 @@ local util = require("scada-common.util") ---@class spinbox_args ---@field default? number default value, defaults to 0.0 +---@field min? number default 0, currently must be 0 or greater +---@field max? number default max number that can be displayed with the digits configuration ---@field whole_num_precision integer number of whole number digits ---@field fractional_precision integer number of fractional digits ---@field arrow_fg_bg cpair arrow foreground/background colors @@ -61,6 +63,7 @@ local function spinbox(args) local function set_digits() local initial_str = util.sprintf(fmt_init, e.value) + digits = {} ---@diagnostic disable-next-line: discard-returns initial_str:gsub("%d", function (char) table.insert(digits, char) end) end @@ -85,13 +88,23 @@ local function spinbox(args) if e.value < 0 then 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 - for i = 1, #digits do digits[i] = 9 end - - -- re-update value - update_value() + -- min configured + elseif (type(args.min) == "number") and (e.value < args.min) then + -- cap at min + e.value = args.min + set_digits() + else + -- max printable + if string.len(util.sprintf(fmt, e.value)) > args.width then + -- max out to all 9s + for i = 1, #digits do digits[i] = 9 end + update_value() + -- max configured + elseif (type(args.max) == "number") and (e.value > args.max) then + -- cap at max + e.value = args.max + set_digits() + end end -- draw @@ -110,16 +123,18 @@ local function spinbox(args) -- only handle if on an increment or decrement arrow if e.enabled and event.x ~= dec_point_x then local idx = util.trinary(event.x > dec_point_x, event.x - 1, event.x) - if event.y == 1 then - -- increment - digits[idx] = digits[idx] + 1 - elseif event.y == 3 then - -- decrement - digits[idx] = digits[idx] - 1 - end + if digits[idx] ~= nil then + if event.y == 1 then + -- increment + digits[idx] = digits[idx] + 1 + elseif event.y == 3 then + -- decrement + digits[idx] = digits[idx] - 1 + end - update_value() - show_num() + update_value() + show_num() + end end end @@ -132,6 +147,18 @@ local function spinbox(args) show_num() end + -- set minimum input value + ---@param min integer minimum allowed value + function e.set_min(min) + if min >= 0 then args.min = min end + end + + -- set maximum input value + ---@param max integer maximum allowed value + function e.set_max(max) + args.max = max + end + return e.get() end