#128 limit max burn rate control to actual max burn rate

This commit is contained in:
Mikayla Fischler 2022-11-17 11:58:14 -05:00
parent c93a386e74
commit 9c32074b56
3 changed files with 45 additions and 17 deletions

View File

@ -17,7 +17,7 @@ local config = require("coordinator.config")
local coordinator = require("coordinator.coordinator") local coordinator = require("coordinator.coordinator")
local renderer = require("coordinator.renderer") local renderer = require("coordinator.renderer")
local COORDINATOR_VERSION = "alpha-v0.6.13" local COORDINATOR_VERSION = "alpha-v0.6.14"
local print = util.print local print = util.print
local println = util.println local println = util.println

View File

@ -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} 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("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) local dis_colors = cpair(colors.white, colors.lightGray)

View File

@ -6,6 +6,8 @@ local util = require("scada-common.util")
---@class spinbox_args ---@class spinbox_args
---@field default? number default value, defaults to 0.0 ---@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 whole_num_precision integer number of whole number digits
---@field fractional_precision integer number of fractional digits ---@field fractional_precision integer number of fractional digits
---@field arrow_fg_bg cpair arrow foreground/background colors ---@field arrow_fg_bg cpair arrow foreground/background colors
@ -61,6 +63,7 @@ local function spinbox(args)
local function set_digits() local function set_digits()
local initial_str = util.sprintf(fmt_init, e.value) local initial_str = util.sprintf(fmt_init, e.value)
digits = {}
---@diagnostic disable-next-line: discard-returns ---@diagnostic disable-next-line: discard-returns
initial_str:gsub("%d", function (char) table.insert(digits, char) end) initial_str:gsub("%d", function (char) table.insert(digits, char) end)
end end
@ -85,13 +88,23 @@ local function spinbox(args)
if e.value < 0 then if e.value < 0 then
for i = 1, #digits do digits[i] = 0 end for i = 1, #digits do digits[i] = 0 end
e.value = 0 e.value = 0
-- max printable -- min configured
elseif string.len(util.sprintf(fmt, e.value)) > args.width then elseif (type(args.min) == "number") and (e.value < args.min) then
-- max out -- cap at min
for i = 1, #digits do digits[i] = 9 end e.value = args.min
set_digits()
-- re-update value else
update_value() -- 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 end
-- draw -- draw
@ -110,16 +123,18 @@ local function spinbox(args)
-- only handle if on an increment or decrement arrow -- only handle if on an increment or decrement arrow
if e.enabled and event.x ~= dec_point_x then if e.enabled and event.x ~= dec_point_x then
local idx = util.trinary(event.x > dec_point_x, event.x - 1, event.x) local idx = util.trinary(event.x > dec_point_x, event.x - 1, event.x)
if event.y == 1 then if digits[idx] ~= nil then
-- increment if event.y == 1 then
digits[idx] = digits[idx] + 1 -- increment
elseif event.y == 3 then digits[idx] = digits[idx] + 1
-- decrement elseif event.y == 3 then
digits[idx] = digits[idx] - 1 -- decrement
end digits[idx] = digits[idx] - 1
end
update_value() update_value()
show_num() show_num()
end
end end
end end
@ -132,6 +147,18 @@ local function spinbox(args)
show_num() show_num()
end 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() return e.get()
end end