#91 get and set values for all controls/indicators and textbox
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
-- Button Graphics Element
|
||||
|
||||
local element = require("graphics.element")
|
||||
|
||||
local util = require("scada-common.util")
|
||||
|
||||
---@class button_option
|
||||
@@ -33,9 +34,6 @@ local function multi_button(args)
|
||||
-- single line
|
||||
args.height = 3
|
||||
|
||||
-- button state (convert nil to 1 if missing)
|
||||
local state = args.default or 1
|
||||
|
||||
-- determine widths
|
||||
local max_width = 1
|
||||
for i = 1, #args.options do
|
||||
@@ -52,6 +50,9 @@ local function multi_button(args)
|
||||
-- create new graphics element base object
|
||||
local e = element.new(args)
|
||||
|
||||
-- button state (convert nil to 1 if missing)
|
||||
e.value = args.default or 1
|
||||
|
||||
-- calculate required button information
|
||||
local next_x = 2
|
||||
for i = 1, #args.options do
|
||||
@@ -72,7 +73,7 @@ local function multi_button(args)
|
||||
|
||||
e.window.setCursorPos(opt._start_x, 2)
|
||||
|
||||
if state == i then
|
||||
if e.value == i then
|
||||
-- show as pressed
|
||||
e.window.setTextColor(opt.active_fg_bg.fgd)
|
||||
e.window.setBackgroundColor(opt.active_fg_bg.bkg)
|
||||
@@ -86,9 +87,6 @@ local function multi_button(args)
|
||||
end
|
||||
end
|
||||
|
||||
-- initial draw
|
||||
draw()
|
||||
|
||||
-- handle touch
|
||||
---@param event monitor_touch monitor touch event
|
||||
function e.handle_touch(event)
|
||||
@@ -98,14 +96,25 @@ local function multi_button(args)
|
||||
local opt = args.options[i] ---@type button_option
|
||||
|
||||
if event.x >= opt._start_x and event.x <= opt._end_x then
|
||||
state = i
|
||||
e.value = i
|
||||
draw()
|
||||
args.callback(state)
|
||||
args.callback(e.value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- set the value
|
||||
---@param val integer new value
|
||||
function e.set_value(val)
|
||||
e.value = val
|
||||
draw()
|
||||
args.callback(e.value)
|
||||
end
|
||||
|
||||
-- initial draw
|
||||
draw()
|
||||
|
||||
return e.get()
|
||||
end
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
local tcd = require("scada-common.tcallbackdsp")
|
||||
|
||||
local core = require("graphics.core")
|
||||
local element = require("graphics.element")
|
||||
|
||||
---@class push_button_args
|
||||
@@ -52,12 +53,14 @@ local function push_button(args)
|
||||
function e.handle_touch(event)
|
||||
if args.active_fg_bg ~= nil then
|
||||
-- show as pressed
|
||||
e.value = true
|
||||
e.window.setTextColor(args.active_fg_bg.fgd)
|
||||
e.window.setBackgroundColor(args.active_fg_bg.bkg)
|
||||
draw()
|
||||
|
||||
-- show as unpressed in 0.25 seconds
|
||||
tcd.dispatch(0.25, function ()
|
||||
e.value = false
|
||||
e.window.setTextColor(e.fg_bg.fgd)
|
||||
e.window.setBackgroundColor(e.fg_bg.bkg)
|
||||
draw()
|
||||
@@ -68,6 +71,12 @@ local function push_button(args)
|
||||
args.callback()
|
||||
end
|
||||
|
||||
-- set the value
|
||||
---@param val boolean new value
|
||||
function e.set_value(val)
|
||||
if val then e.handle_touch(core.events.touch("", 1, 1)) end
|
||||
end
|
||||
|
||||
-- initial draw
|
||||
draw()
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
local tcd = require("scada-common.tcallbackdsp")
|
||||
|
||||
local core = require("graphics.core")
|
||||
|
||||
local element = require("graphics.element")
|
||||
|
||||
---@class scram_button_args
|
||||
@@ -65,6 +64,12 @@ local function scram_button(args)
|
||||
args.callback()
|
||||
end
|
||||
|
||||
-- set the value
|
||||
---@param val boolean new value
|
||||
function e.set_value(val)
|
||||
if val then e.handle_touch(core.events.touch("", 1, 1)) end
|
||||
end
|
||||
|
||||
return e.get()
|
||||
end
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
-- Spinbox Numeric Graphics Element
|
||||
|
||||
local element = require("graphics.element")
|
||||
|
||||
local util = require("scada-common.util")
|
||||
|
||||
---@class spinbox_args
|
||||
@@ -19,7 +20,6 @@ local util = require("scada-common.util")
|
||||
---@return graphics_element element, element_id id
|
||||
local function spinbox(args)
|
||||
-- properties
|
||||
local value = args.default or 0.0
|
||||
local digits = {}
|
||||
local wn_prec = args.whole_num_precision
|
||||
local fr_prec = args.fractional_precision
|
||||
@@ -33,11 +33,6 @@ local function spinbox(args)
|
||||
|
||||
assert(type(args.arrow_fg_bg) == "table", "graphics.element.spinbox_numeric: arrow_fg_bg is a required field")
|
||||
|
||||
local initial_str = util.sprintf(fmt_init, value)
|
||||
|
||||
---@diagnostic disable-next-line: discard-returns
|
||||
initial_str:gsub("%d", function(char) table.insert(digits, char) end)
|
||||
|
||||
-- determine widths
|
||||
args.width = wn_prec + fr_prec + util.trinary(fr_prec > 0, 1, 0)
|
||||
args.height = 3
|
||||
@@ -45,6 +40,14 @@ local function spinbox(args)
|
||||
-- create new graphics element base object
|
||||
local e = element.new(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,7 +65,7 @@ local function spinbox(args)
|
||||
-- zero the value
|
||||
local function zero()
|
||||
for i = 1, #digits do digits[i] = 0 end
|
||||
value = 0
|
||||
e.value = 0
|
||||
end
|
||||
|
||||
-- print out the current value
|
||||
@@ -70,7 +73,42 @@ local function spinbox(args)
|
||||
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, value))
|
||||
e.window.write(util.sprintf(fmt, e.value))
|
||||
end
|
||||
|
||||
-- update the value per digits table
|
||||
local function update_value()
|
||||
e.value = 0
|
||||
for i = 1, #digits do
|
||||
local pow = math.abs(wn_prec - i)
|
||||
if i <= wn_prec then
|
||||
e.value = e.value + (digits[i] * (10 ^ pow))
|
||||
else
|
||||
e.value = e.value + (digits[i] * (10 ^ -pow))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- enforce numeric limits
|
||||
local function enforce_limits()
|
||||
-- min 0
|
||||
if e.value < 0 then
|
||||
zero()
|
||||
-- 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()
|
||||
end
|
||||
end
|
||||
|
||||
-- update value and show
|
||||
local function parse_and_show()
|
||||
update_value()
|
||||
enforce_limits()
|
||||
show_num()
|
||||
end
|
||||
|
||||
-- init with the default value
|
||||
@@ -90,27 +128,16 @@ local function spinbox(args)
|
||||
digits[idx] = digits[idx] - 1
|
||||
end
|
||||
|
||||
-- update value
|
||||
value = 0
|
||||
for i = 1, #digits do
|
||||
local pow = math.abs(wn_prec - i)
|
||||
if i <= wn_prec then
|
||||
value = value + (digits[i] * (10 ^ pow))
|
||||
else
|
||||
value = value + (digits[i] * (10 ^ -pow))
|
||||
end
|
||||
end
|
||||
|
||||
-- min 0
|
||||
if value < 0 then zero() end
|
||||
|
||||
show_num()
|
||||
parse_and_show()
|
||||
end
|
||||
end
|
||||
|
||||
-- get current value
|
||||
---@return number|integer
|
||||
function e.get_value() return value end
|
||||
-- set the value
|
||||
---@param val number number to show
|
||||
function e.set_value(val)
|
||||
e.value = val
|
||||
parse_and_show()
|
||||
end
|
||||
|
||||
return e.get()
|
||||
end
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
local tcd = require("scada-common.tcallbackdsp")
|
||||
|
||||
local core = require("graphics.core")
|
||||
|
||||
local element = require("graphics.element")
|
||||
|
||||
---@class start_button_args
|
||||
@@ -65,6 +64,12 @@ local function start_button(args)
|
||||
args.callback()
|
||||
end
|
||||
|
||||
-- set the value
|
||||
---@param val boolean new value
|
||||
function e.set_value(val)
|
||||
if val then e.handle_touch(core.events.touch("", 1, 1)) end
|
||||
end
|
||||
|
||||
return e.get()
|
||||
end
|
||||
|
||||
|
||||
@@ -26,9 +26,6 @@ local function switch_button(args)
|
||||
-- single line
|
||||
args.height = 1
|
||||
|
||||
-- button state (convert nil to false if missing)
|
||||
local state = args.default or false
|
||||
|
||||
-- determine widths
|
||||
local text_width = string.len(args.text)
|
||||
args.width = math.max(text_width + 2, args.min_width)
|
||||
@@ -36,12 +33,15 @@ local function switch_button(args)
|
||||
-- create new graphics element base object
|
||||
local e = element.new(args)
|
||||
|
||||
-- button state (convert nil to false if missing)
|
||||
e.value = args.default or false
|
||||
|
||||
local h_pad = math.floor((e.frame.w - text_width) / 2)
|
||||
local v_pad = math.floor(e.frame.h / 2) + 1
|
||||
|
||||
-- show the button state
|
||||
local function draw_state()
|
||||
if state then
|
||||
if e.value then
|
||||
-- show as pressed
|
||||
e.window.setTextColor(args.active_fg_bg.fgd)
|
||||
e.window.setBackgroundColor(args.active_fg_bg.bkg)
|
||||
@@ -64,11 +64,22 @@ local function switch_button(args)
|
||||
---@diagnostic disable-next-line: unused-local
|
||||
function e.handle_touch(event)
|
||||
-- toggle state
|
||||
state = not state
|
||||
e.value = not e.value
|
||||
draw_state()
|
||||
|
||||
-- call the touch callback with state
|
||||
args.callback(state)
|
||||
args.callback(e.value)
|
||||
end
|
||||
|
||||
-- set the value
|
||||
---@param val boolean new value
|
||||
function e.set_value(val)
|
||||
-- set state
|
||||
e.value = val
|
||||
draw_state()
|
||||
|
||||
-- call the touch callback with state
|
||||
args.callback(e.value)
|
||||
end
|
||||
|
||||
return e.get()
|
||||
|
||||
Reference in New Issue
Block a user