From 6d5af983101e46be49a2e71890ffae62df109b7a Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 20 Oct 2022 12:22:45 -0400 Subject: [PATCH] graphics element enable/disable, click indication on hazard buttons --- graphics/element.lua | 23 ++++++- graphics/elements/controls/hazard_button.lua | 62 ++++++++++++------- graphics/elements/controls/multi_button.lua | 2 +- graphics/elements/controls/push_button.lua | 34 +++++----- .../elements/controls/spinbox_numeric.lua | 2 +- graphics/elements/controls/switch_button.lua | 12 ++-- 6 files changed, 88 insertions(+), 47 deletions(-) diff --git a/graphics/element.lua b/graphics/element.lua index 907cc8e..70b784c 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -60,6 +60,7 @@ function element.new(args) ---@class graphics_template local protected = { + enabled = true, value = nil, ---@type any window = nil, ---@type table fg_bg = core.graphics.cpair(colors.white, colors.black), @@ -157,6 +158,7 @@ function element.new(args) end -- handle data value changes + ---@vararg any value(s) function protected.on_update(...) end @@ -168,7 +170,14 @@ function element.new(args) -- set value ---@param value any value to set function protected.set_value(value) - return nil + end + + -- enable the control + function protected.enable() + end + + -- disable the control + function protected.disable() end -- custom recolor command, varies by element if implemented @@ -301,6 +310,18 @@ function element.new(args) protected.set_value(value) end + -- enable the element + function public.enable() + protected.enabled = true + protected.enable() + end + + -- disable the element + function public.disable() + protected.enabled = false + protected.disable() + end + -- resize attributes of the element value if supported ---@vararg number dimensions (element specific) function public.resize(...) diff --git a/graphics/elements/controls/hazard_button.lua b/graphics/elements/controls/hazard_button.lua index c931f19..c0395b9 100644 --- a/graphics/elements/controls/hazard_button.lua +++ b/graphics/elements/controls/hazard_button.lua @@ -35,37 +35,53 @@ local function hazard_button(args) e.window.write(args.text) -- draw border + ---@param accent color accent color + local function draw_border(accent) + -- top + e.window.setTextColor(args.accent) + e.window.setBackgroundColor(args.fg_bg.bkg) + e.window.setCursorPos(1, 1) + e.window.write("\x99\x89\x89\x89\x89\x89\x89\x89\x99") - -- top - e.window.setTextColor(args.accent) - e.window.setBackgroundColor(args.fg_bg.bkg) - e.window.setCursorPos(1, 1) - e.window.write("\x99\x89\x89\x89\x89\x89\x89\x89\x99") + -- center left + e.window.setCursorPos(1, 2) + e.window.setTextColor(args.fg_bg.bkg) + e.window.setBackgroundColor(args.accent) + e.window.write("\x99") - -- center left - e.window.setCursorPos(1, 2) - e.window.setTextColor(args.fg_bg.bkg) - e.window.setBackgroundColor(args.accent) - e.window.write("\x99") + -- center right + e.window.setTextColor(args.fg_bg.bkg) + e.window.setBackgroundColor(args.accent) + e.window.setCursorPos(9, 2) + e.window.write("\x99") - -- center right - e.window.setTextColor(args.fg_bg.bkg) - e.window.setBackgroundColor(args.accent) - e.window.setCursorPos(9, 2) - e.window.write("\x99") - - -- bottom - e.window.setTextColor(args.accent) - e.window.setBackgroundColor(args.fg_bg.bkg) - e.window.setCursorPos(1, 3) - e.window.write("\x99\x98\x98\x98\x98\x98\x98\x98\x99") + -- bottom + e.window.setTextColor(args.accent) + e.window.setBackgroundColor(args.fg_bg.bkg) + e.window.setCursorPos(1, 3) + e.window.write("\x99\x98\x98\x98\x98\x98\x98\x98\x99") + end -- handle touch ---@param event monitor_touch monitor touch event ---@diagnostic disable-next-line: unused-local function e.handle_touch(event) - -- call the touch callback - args.callback() + if e.enabled then + -- call the touch callback + args.callback() + + -- change text color to indicate clicked + e.window.setTextColor(args.accent) + e.window.setCursorPos(3, 2) + e.window.write(args.text) + + -- restore text color after 1 second + tcd.dispatch(1, function () + e.window.setTextColor(args.fg_bg.fgd) + e.window.setCursorPos(3, 2) + e.window.write(args.text) + end) + end end -- set the value diff --git a/graphics/elements/controls/multi_button.lua b/graphics/elements/controls/multi_button.lua index 84aa65b..1dd39b4 100644 --- a/graphics/elements/controls/multi_button.lua +++ b/graphics/elements/controls/multi_button.lua @@ -91,7 +91,7 @@ local function multi_button(args) ---@param event monitor_touch monitor touch event function e.handle_touch(event) -- determine what was pressed - if event.y == 2 then + if e.enabled and event.y == 2 then for i = 1, #args.options do local opt = args.options[i] ---@type button_option diff --git a/graphics/elements/controls/push_button.lua b/graphics/elements/controls/push_button.lua index d8a1755..d4fbcaa 100644 --- a/graphics/elements/controls/push_button.lua +++ b/graphics/elements/controls/push_button.lua @@ -51,24 +51,26 @@ local function push_button(args) ---@param event monitor_touch monitor touch event ---@diagnostic disable-next-line: unused-local 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) + if e.enabled then + 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() - end) - end - -- call the touch callback - args.callback() + -- 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() + end) + end + + -- call the touch callback + args.callback() + end end -- set the value diff --git a/graphics/elements/controls/spinbox_numeric.lua b/graphics/elements/controls/spinbox_numeric.lua index 1f61a5b..23065e2 100644 --- a/graphics/elements/controls/spinbox_numeric.lua +++ b/graphics/elements/controls/spinbox_numeric.lua @@ -108,7 +108,7 @@ local function spinbox(args) ---@param event monitor_touch monitor touch event function e.handle_touch(event) -- only handle if on an increment or decrement arrow - if 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) if event.y == 1 then -- increment diff --git a/graphics/elements/controls/switch_button.lua b/graphics/elements/controls/switch_button.lua index 2863747..cb003cb 100644 --- a/graphics/elements/controls/switch_button.lua +++ b/graphics/elements/controls/switch_button.lua @@ -63,12 +63,14 @@ local function switch_button(args) ---@param event monitor_touch monitor touch event ---@diagnostic disable-next-line: unused-local function e.handle_touch(event) - -- toggle state - e.value = not e.value - draw_state() + if e.enabled then + -- toggle state + e.value = not e.value + draw_state() - -- call the touch callback with state - args.callback(e.value) + -- call the touch callback with state + args.callback(e.value) + end end -- set the value