mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
added checkbox graphics element
This commit is contained in:
parent
b6835fc7d1
commit
7b9824b6f9
@ -20,6 +20,7 @@ local element = {}
|
|||||||
|
|
||||||
---@alias graphics_args graphics_args_generic
|
---@alias graphics_args graphics_args_generic
|
||||||
---|waiting_args
|
---|waiting_args
|
||||||
|
---|checkbox_args
|
||||||
---|hazard_button_args
|
---|hazard_button_args
|
||||||
---|multi_button_args
|
---|multi_button_args
|
||||||
---|push_button_args
|
---|push_button_args
|
||||||
|
84
graphics/elements/controls/checkbox.lua
Normal file
84
graphics/elements/controls/checkbox.lua
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
-- Checkbox Graphics Element
|
||||||
|
|
||||||
|
local core = require("graphics.core")
|
||||||
|
local element = require("graphics.element")
|
||||||
|
|
||||||
|
---@class checkbox_args
|
||||||
|
---@field label string checkbox text
|
||||||
|
---@field box_fg_bg cpair colors for checkbox
|
||||||
|
---@field callback function function to call on press
|
||||||
|
---@field parent graphics_element
|
||||||
|
---@field id? string element id
|
||||||
|
---@field x? integer 1 if omitted
|
||||||
|
---@field y? integer 1 if omitted
|
||||||
|
---@field fg_bg? cpair foreground/background colors
|
||||||
|
---@field hidden? boolean true to hide on initial draw
|
||||||
|
|
||||||
|
-- new checkbox control
|
||||||
|
---@param args checkbox_args
|
||||||
|
---@return graphics_element element, element_id id
|
||||||
|
local function checkbox(args)
|
||||||
|
assert(type(args.label) == "string", "graphics.elements.controls.checkbox: label is a required field")
|
||||||
|
assert(type(args.box_fg_bg) == "table", "graphics.elements.controls.checkbox: box_fg_bg is a required field")
|
||||||
|
assert(type(args.callback) == "function", "graphics.elements.controls.checkbox: callback is a required field")
|
||||||
|
|
||||||
|
args.height = 1
|
||||||
|
args.width = 3 + string.len(args.label)
|
||||||
|
|
||||||
|
-- create new graphics element base object
|
||||||
|
local e = element.new(args)
|
||||||
|
|
||||||
|
e.value = false
|
||||||
|
|
||||||
|
-- show the button state
|
||||||
|
local function draw()
|
||||||
|
e.window.setCursorPos(1, 1)
|
||||||
|
|
||||||
|
if e.value then
|
||||||
|
-- show as selected
|
||||||
|
e.window.setTextColor(args.box_fg_bg.bkg)
|
||||||
|
e.window.setBackgroundColor(args.box_fg_bg.fgd)
|
||||||
|
e.window.write("\x88")
|
||||||
|
e.window.setTextColor(args.box_fg_bg.fgd)
|
||||||
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
||||||
|
e.window.write("\x95")
|
||||||
|
else
|
||||||
|
-- show as unselected
|
||||||
|
e.window.setBackgroundColor(args.box_fg_bg.bkg)
|
||||||
|
e.window.write("\x80")
|
||||||
|
e.window.setTextColor(args.box_fg_bg.bkg)
|
||||||
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
||||||
|
e.window.write("\x95")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- handle mouse interaction
|
||||||
|
---@param event mouse_interaction mouse event
|
||||||
|
function e.handle_mouse(event)
|
||||||
|
if e.enabled and core.events.was_clicked(event.type) then
|
||||||
|
e.value = not e.value
|
||||||
|
draw()
|
||||||
|
args.callback(e.value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- set the value
|
||||||
|
---@param val integer new value
|
||||||
|
function e.set_value(val)
|
||||||
|
e.value = val
|
||||||
|
draw()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- write label text
|
||||||
|
e.window.setCursorPos(3, 1)
|
||||||
|
e.window.setTextColor(e.fg_bg.fgd)
|
||||||
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
||||||
|
e.window.write(args.label)
|
||||||
|
|
||||||
|
-- initial draw
|
||||||
|
draw()
|
||||||
|
|
||||||
|
return e.complete()
|
||||||
|
end
|
||||||
|
|
||||||
|
return checkbox
|
Loading…
x
Reference in New Issue
Block a user