From ded5d5e733ee6b3e858daaabad9e8e573961d45d Mon Sep 17 00:00:00 2001 From: shmuely Date: Sat, 25 Nov 2023 23:13:53 +0200 Subject: [PATCH] libgui: support min/max on numbers libgui: update text offsets for 2.9 libgui: support scripts, and not just widgets --- sdcard/c480x272/WIDGETS/LibGUI/libgui.lua | 371 +++++++++++--------- sdcard/c480x272/WIDGETS/LibGUI/loadable.lua | 16 +- sdcard/c480x272/WIDGETS/LibGUI/main.lua | 13 +- 3 files changed, 220 insertions(+), 180 deletions(-) diff --git a/sdcard/c480x272/WIDGETS/LibGUI/libgui.lua b/sdcard/c480x272/WIDGETS/LibGUI/libgui.lua index 570db63a..3fe9dbf0 100644 --- a/sdcard/c480x272/WIDGETS/LibGUI/libgui.lua +++ b/sdcard/c480x272/WIDGETS/LibGUI/libgui.lua @@ -2,8 +2,11 @@ -- The dynamically loadable part of the shared Lua GUI library. -- -- -- -- Author: Jesper Frickmann -- --- Date: 2022-11-20 -- --- Version: 1.0.2 -- +-- Version: 1.0.0 Date: 2021-12-20 -- +-- Version: 1.0.1 Date: 2022-05-05 -- +-- Version: 1.0.2 Date: 2022-11-20 -- +-- Version: 1.0.2 Date: 2023-07 -- +-- Version: 1.0.3 Date: 2023-12 -- -- -- -- Copyright (C) EdgeTX -- -- -- @@ -19,18 +22,27 @@ -- GNU General Public License for more details. -- --------------------------------------------------------------------------- -local lib = { } +local app_ver = "1.0.3" + +local M = { } +M._ = {} -- internal members -function lib.getVer() - return "1.0.2" -end -- Radius of slider dot -local SLIDER_DOT_RADIUS = 10 +M.SLIDER_DOT_RADIUS = 10 + +-- better font size names +M.FONT_SIZES = { + FONT_38 = XXLSIZE, -- 38px + FONT_16 = DBLSIZE, -- 16px + FONT_12 = MIDSIZE, -- 12px + FONT_8 = 0, -- Default 8px + FONT_6 = SMLSIZE, -- 6px +} -- Default flags and colors, can be changed by client -lib.flags = 0 -lib.colors = { +M.flags = 0 +M.colors = { primary1 = COLOR_THEME_PRIMARY1, primary2 = COLOR_THEME_PRIMARY2, primary3 = COLOR_THEME_PRIMARY3, @@ -39,8 +51,12 @@ lib.colors = { active = COLOR_THEME_ACTIVE, } +function M.getVer() + return app_ver +end + -- Return true if the first arg matches any of the following args -local function match(x, ...) +function M.match(x, ...) for i, y in ipairs({ ... }) do if x == y then return true @@ -49,10 +65,9 @@ local function match(x, ...) return false end -lib.match = match -- Create a new GUI object with interactive screen elements -function lib.newGUI() +function M.newGUI() local gui = { x = 0, y = 0, @@ -65,6 +80,25 @@ function lib.newGUI() local scrolling = false local lastEvent = 0 + + function gui.lcdSizeTextFixed(txt, font_size) + local ts_w, ts_h = lcd.sizeText(txt, font_size) + + local v_offset = 0 + if font_size == M.FONT_SIZES.FONT_38 then + v_offset = -11 + elseif font_size == M.FONT_SIZES.FONT_16 then + v_offset = -5 + elseif font_size == M.FONT_SIZES.FONT_12 then + v_offset = -4 + elseif font_size == M.FONT_SIZES.FONT_8 then + v_offset = -3 + elseif font_size == M.FONT_SIZES.FONT_6 then + v_offset = 0 + end + return ts_w, ts_h +2*v_offset, v_offset + end + -- Translate coordinates for sub-GUIs function gui.translate(x, y) if gui.parent then @@ -116,7 +150,9 @@ function lib.newGUI() function gui.drawText(x, y, text, flags, inversColor) x, y = gui.translate(x, y) - lcd.drawText(x, y, text, flags, inversColor) + local ts_w, ts_h, v_offset = gui.lcdSizeTextFixed(text, M.FONT_SIZES.FONT_8) + lcd.drawText(x, y + v_offset, text, flags, inversColor) + --lcd.drawText(x, y, text, flags, inversColor) end function gui.drawTextLines(x, y, w, h, text, flags) @@ -160,8 +196,8 @@ function lib.newGUI() if #elements == 1 then return end - color = color or lib.colors.active - gui.drawRectangle(x - 2, y - 2, w + 4, h + 4, color, 2) + color = color or M.colors.active + gui.drawRectangle(x - 4, y - 2, w + 8, h + 2, color, 2) end -- drawFocus(...) -- Move focus to another element @@ -219,27 +255,20 @@ function lib.newGUI() -- Show prompt function gui.showPrompt(prompt) - lib.prompt = prompt + M.prompt = prompt end -- Dismiss prompt function gui.dismissPrompt() - lib.prompt = nil + M.prompt = nil end ----------------------------------------------------------------------------------------------- -- Run an event cycle function gui.run(event, touchState) - if not event then -- widget mode; event == nil - if lib.widgetRefresh then - lib.widgetRefresh() - else - gui.drawText(1, 1, "No widget refresh") - gui.drawText(1, 25, "function was loaded.") - end - else -- full screen mode; event is a value - gui.draw(false) + gui.draw(false) + if event ~= nil then gui.onEvent(event, touchState) end lastEvent = event @@ -253,7 +282,7 @@ function lib.newGUI() end if focused then if gui.parent.editing then - drawFocus(0, 0, gui.w, gui.h, lib.colors.edit) + drawFocus(0, 0, gui.w, gui.h, M.colors.edit) else drawFocus(0, 0, gui.w, gui.h) end @@ -261,8 +290,8 @@ function lib.newGUI() local guiFocus = not gui.parent or (focused and gui.parent.editing) for idx, element in ipairs(elements) do -- Clients may provide an update function for elements - if element.update then - element.update(element) + if element.onUpdate then + element.onUpdate(element) end if not element.hidden then element.draw(focus == idx and guiFocus) @@ -279,89 +308,97 @@ function lib.newGUI() return end -- Is there an active prompt? - if lib.prompt and not lib.showingPrompt then - lib.showingPrompt = true - lib.prompt.run(event, touchState) - lib.showingPrompt = false + if M.prompt and not M.showingPrompt then + M.showingPrompt = true + M.prompt.run(event, touchState) + M.showingPrompt = false return end - if event ~= 0 then -- non-zero event; process it - if not gui.parent or gui.parent.editing then - -- Translate touch coordinates if offset - if touchState then - touchState.x = touchState.x - gui.x - touchState.y = touchState.y - gui.y - if touchState.startX then - touchState.startX = touchState.startX - gui.x - touchState.startY = touchState.startY - gui.y - end - -- "Un-convert" ENTER to TAP - if event == EVT_VIRTUAL_ENTER then - event = EVT_TOUCH_TAP - end - end - -- ETX 2.8 rc 4 bug fix - if scrolling and event == EVT_VIRTUAL_ENTER_LONG then + if event == 0 then + return + end + + if gui.parent and not gui.parent.editing then + if event == EVT_VIRTUAL_ENTER then + gui.parent.editing = true + end + return + end + + -- non-zero event; process it + -- Translate touch coordinates if offset + if touchState then + touchState.x = touchState.x - gui.x + touchState.y = touchState.y - gui.y + if touchState.startX then + touchState.startX = touchState.startX - gui.x + touchState.startY = touchState.startY - gui.y + end + -- "Un-convert" ENTER to TAP + if event == EVT_VIRTUAL_ENTER then + event = EVT_TOUCH_TAP + end + end + + -- ETX 2.8 rc 4 bug fix + if scrolling and event == EVT_VIRTUAL_ENTER_LONG then + return + end + -- If we put a finger down on a menu item and immediately slide, then we can scroll + if event == EVT_TOUCH_SLIDE then + if not scrolling then + return + end + else + scrolling = false + end + + -- "Pre-processing" of touch events to simplify subsequent handling and support scrolling etc. + if event == EVT_TOUCH_FIRST then + if elements[focus].covers(touchState.x, touchState.y) then + scrolling = true + else + if gui.editing then return - end - -- If we put a finger down on a menu item and immediately slide, then we can scroll - if event == EVT_TOUCH_SLIDE then - if not scrolling then - return - end else - scrolling = false - end - -- "Pre-processing" of touch events to simplify subsequent handling and support scrolling etc. - if event == EVT_TOUCH_FIRST then - if elements[focus].covers(touchState.x, touchState.y) then - scrolling = true - else - if gui.editing then - return - else - -- Did we touch another element? - for idx, element in ipairs(elements) do - if not (element.disabled or element.hidden) and element.covers(touchState.x, touchState.y) then - focus = idx - scrolling = true - end - end + -- Did we touch another element? + for idx, element in ipairs(elements) do + if not (element.disabled or element.hidden) and element.covers(touchState.x, touchState.y) then + focus = idx + scrolling = true end end - elseif event == EVT_TOUCH_TAP or (event == EVT_TOUCH_BREAK and lastEvent == EVT_TOUCH_FIRST) then - if elements[focus].covers(touchState.x, touchState.y) then - -- Convert TAP on focused element to ENTER - event = EVT_VIRTUAL_ENTER - elseif gui.editing then - -- Convert a TAP off the element being edited to EXIT - event = EVT_VIRTUAL_EXIT - end end + end + elseif event == EVT_TOUCH_TAP or (event == EVT_TOUCH_BREAK and lastEvent == EVT_TOUCH_FIRST) then + if elements[focus].covers(touchState.x, touchState.y) then + -- Convert TAP on focused element to ENTER + event = EVT_VIRTUAL_ENTER + elseif gui.editing then + -- Convert a TAP off the element being edited to EXIT + event = EVT_VIRTUAL_EXIT + end + end - if gui.editing then -- Send the event directly to the element being edited - elements[focus].onEvent(event, touchState) - elseif event == EVT_VIRTUAL_NEXT then -- Move focus - moveFocus(1) - elseif event == EVT_VIRTUAL_PREV then - moveFocus(-1) - elseif event == EVT_VIRTUAL_EXIT and gui.parent then - gui.parent.editing = false - else - if handles[event] then - -- Is it being handled? Handler can modify event - event = handles[event](event, touchState) - -- If handler returned false or nil, then we are done - if not event then - return - end - end - elements[focus].onEvent(event, touchState) + if gui.editing then -- Send the event directly to the element being edited + elements[focus].onEvent(event, touchState) + elseif event == EVT_VIRTUAL_NEXT then -- Move focus + moveFocus(1) + elseif event == EVT_VIRTUAL_PREV then + moveFocus(-1) + elseif event == EVT_VIRTUAL_EXIT and gui.parent then + gui.parent.editing = false + else + if handles[event] then + -- Is it being handled? Handler can modify event + event = handles[event](event, touchState) + -- If handler returned false or nil, then we are done + if not event then + return end - elseif event == EVT_VIRTUAL_ENTER then - gui.parent.editing = true end + elements[focus].onEvent(event, touchState) end end -- onEvent(...) @@ -371,8 +408,9 @@ function lib.newGUI() function gui.label(x, y, w, h, title, flags) local self = { title = title, - flags = bit32.bor(flags or lib.flags, VCENTER, lib.colors.primary1), - disabled = true + flags = bit32.bor(flags or M.flags, VCENTER, M.colors.primary1), + disabled = true, + hidden= false, } function self.draw(focused) @@ -400,7 +438,7 @@ function lib.newGUI() function gui.labelLines(x, y, w, h, title, flags) local self = { title = title, - flags = bit32.bor(flags or flags, VCENTER, lib.colors.primary1), + flags = bit32.bor(flags or M.flags, VCENTER, M.colors.primary1), disabled = true, hidden= false, } @@ -431,7 +469,7 @@ function lib.newGUI() local self = { title = title, callBack = callBack or doNothing, - flags = bit32.bor(flags or lib.flags, CENTER, VCENTER), + flags = bit32.bor(flags or M.flags, CENTER, VCENTER), disabled = false, hidden= false } @@ -441,8 +479,8 @@ function lib.newGUI() drawFocus(x, y, w, h) end - gui.drawFilledRectangle(x, y, w, h, lib.colors.focus) - gui.drawText(x + w / 2, y + h / 2, self.title, bit32.bor(lib.colors.primary2, self.flags)) + gui.drawFilledRectangle(x, y, w, h, M.colors.focus) + gui.drawText(x + w / 2, y + h / 2, self.title, bit32.bor(M.colors.primary2, self.flags)) if self.disabled then gui.drawFilledRectangle(x, y, w, h, GREY, 7) @@ -467,20 +505,20 @@ function lib.newGUI() title = title, value = value, callBack = callBack or doNothing, - flags = bit32.bor(flags or lib.flags, CENTER, VCENTER), + flags = bit32.bor(flags or M.flags, CENTER, VCENTER), disabled = false, hidden= false } function self.draw(focused) - local fg = lib.colors.primary2 - local bg = lib.colors.focus - local border = lib.colors.active + local fg = M.colors.primary2 + local bg = M.colors.focus + local border = M.colors.active if self.value then - fg = lib.colors.primary3 - bg = lib.colors.active - border = lib.colors.focus + fg = M.colors.primary3 + bg = M.colors.active + border = M.colors.focus end if focused then @@ -509,27 +547,30 @@ function lib.newGUI() ----------------------------------------------------------------------------------------------- -- Create a number that can be edited - function gui.number(x, y, w, h, value, onChangeValue, flags) + function gui.number(x, y, w, h, value, onChangeValue, flags, min, max) local self = { value = value, onChangeValue = onChangeValue or onChangeDefault, - flags = bit32.bor(flags or lib.flags, VCENTER), + flags = bit32.bor(flags or M.flags, VCENTER), editable = true, disabled = false, - hidden= false + hidden= false, + min_val = min, + max_val = max } + local d0 function self.draw(focused) local flags = getFlags(self) - local fg = lib.colors.primary1 + local fg = M.colors.primary1 if focused then drawFocus(x, y, w, h) if gui.editing then - fg = lib.colors.primary2 - gui.drawFilledRectangle(x, y, w, h, lib.colors.edit) + fg = M.colors.primary2 + gui.drawFilledRectangle(x, y, w, h, M.colors.edit) end end if type(self.value) == "string" then @@ -547,9 +588,13 @@ function lib.newGUI() self.value = value gui.editing = false elseif event == EVT_VIRTUAL_INC then - self.value = self.onChangeValue(1, self) + if self.value < self.max_val then + self.value = self.onChangeValue(1, self) + end elseif event == EVT_VIRTUAL_DEC then - self.value = self.onChangeValue(-1, self) + if self.value > self.min_val then + self.value = self.onChangeValue(-1, self) + end elseif event == EVT_TOUCH_FIRST then d0 = 0 elseif event == EVT_TOUCH_SLIDE then @@ -577,7 +622,7 @@ function lib.newGUI() local self = { tmr = tmr, onChangeValue = onChangeValue or onChangeDefault, - flags = bit32.bor(flags or lib.flags, VCENTER), + flags = bit32.bor(flags or M.flags, VCENTER), disabled = false, hidden= false, editable = true @@ -587,7 +632,7 @@ function lib.newGUI() function self.draw(focused) local flags = getFlags(self) - local fg = lib.colors.primary1 + local fg = M.colors.primary1 -- self.value overrides the timer value local value = self.value or model.getTimer(self.tmr).value @@ -595,8 +640,8 @@ function lib.newGUI() drawFocus(x, y, w, h) if gui.editing then - fg = lib.colors.primary2 - gui.drawFilledRectangle(x, y, w, h, lib.colors.edit) + fg = M.colors.primary2 + gui.drawFilledRectangle(x, y, w, h, M.colors.edit) end end if type(value) == "string" then @@ -652,7 +697,7 @@ function lib.newGUI() function gui.menu(x, y, w, h, items, callBack, flags) local self = { items = items or { "No items!" }, - flags = bit32.bor(flags or lib.flags, VCENTER), + flags = bit32.bor(flags or M.flags, VCENTER), disabled = false, hidden= false, editable = true, @@ -689,10 +734,10 @@ function lib.newGUI() local bgColor if focused and gui.editing then - bgColor = lib.colors.edit + bgColor = M.colors.edit else selected = self.selected - bgColor = lib.colors.focus + bgColor = M.colors.focus end for i = 0, visibleCount - 1 do @@ -701,9 +746,9 @@ function lib.newGUI() if j == selected then gui.drawFilledRectangle(x, y, w, lh, bgColor) - gui.drawText(align(x, w, flags), y + lh / 2, self.items[j], bit32.bor(lib.colors.primary2, flags)) + gui.drawText(align(x, w, flags), y + lh / 2, self.items[j], bit32.bor(M.colors.primary2, flags)) else - gui.drawText(align(x, w, flags), y + lh / 2, self.items[j], bit32.bor(lib.colors.primary1, flags)) + gui.drawText(align(x, w, flags), y + lh / 2, self.items[j], bit32.bor(M.colors.primary1, flags)) end end @@ -716,7 +761,7 @@ function lib.newGUI() local visibleCount = math.min(visibleCount, #self.items) if moving ~= 0 then - if match(event, EVT_TOUCH_FIRST, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then + if M.match(event, EVT_TOUCH_FIRST, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then moving = 0 event = 0 else @@ -754,7 +799,7 @@ function lib.newGUI() if event == EVT_TOUCH_FIRST then scrolling = true firstVisibleScrolling = firstVisible - elseif match(event, EVT_VIRTUAL_NEXT, EVT_VIRTUAL_PREV) then + elseif M.match(event, EVT_VIRTUAL_NEXT, EVT_VIRTUAL_PREV) then if event == EVT_VIRTUAL_NEXT then selected = math.min(#self.items, selected + 1) elseif event == EVT_VIRTUAL_PREV then @@ -790,12 +835,12 @@ function lib.newGUI() function gui.dropDown(x, y, w, h, items, selected, callBack, flags) callBack = callBack or doNothing - flags = flags or lib.flags + flags = flags or M.flags local self local showingMenu local drawingMenu - local dropDown = lib.newGUI() + local dropDown = M.newGUI() local lh = select(2, lcd.sizeText("", flags)) local height = math.min(0.75 * LCD_H, #items * lh) local top = (LCD_H - height) / 2 @@ -814,8 +859,8 @@ function lib.newGUI() dismissMenu() return end - dropDown.drawFilledRectangle(x, top, w, height, lib.colors.primary2) - dropDown.drawRectangle(x - 2, top - 2, w + 4, height + 4, lib.colors.primary1, 2) + dropDown.drawFilledRectangle(x, top, w, height, M.colors.primary2) + dropDown.drawRectangle(x - 2, top - 2, w + 4, height + 4, M.colors.primary1, 2) drawingMenu = true end @@ -833,7 +878,7 @@ function lib.newGUI() drawingMenu = false drawMenu(focused) else - local flags = bit32.bor(VCENTER, lib.colors.primary1, getFlags(self)) + local flags = bit32.bor(VCENTER, M.colors.primary1, getFlags(self)) if focused then drawFocus(x, y, w, h) @@ -841,8 +886,8 @@ function lib.newGUI() gui.drawText(align(x, w, flags), y + h / 2, self.items[self.selected], flags) local dd = lh / 2 local yy = y + (h - dd) / 2 - local xx = x + w - 1.15 * dd - gui.drawTriangle(x + w, yy, (x + w + xx) / 2, yy + dd, xx, yy, lib.colors.primary1) + local xx = (x-5) + w - 1.15 * dd + gui.drawTriangle(x-5 + w, yy, (x-5 + w + xx) / 2, yy + dd, xx, yy, M.colors.primary1) end end @@ -891,22 +936,22 @@ function lib.newGUI() function self.draw(focused) local xdot = x + w * (self.value - self.min) / (self.max - self.min) - local colorBar = lib.colors.primary3 - local colorDot = lib.colors.primary2 - local colorDotBorder = lib.colors.primary3 + local colorBar = M.colors.primary3 + local colorDot = M.colors.primary2 + local colorDotBorder = M.colors.primary3 if focused then - colorDotBorder = lib.colors.active + colorDotBorder = M.colors.active if gui.editing or scrolling then - colorBar = lib.colors.primary1 - colorDot = lib.colors.edit + colorBar = M.colors.primary1 + colorDot = M.colors.edit end end gui.drawFilledRectangle(x, y - 2, w, 5, colorBar) - gui.drawFilledCircle(xdot, y, SLIDER_DOT_RADIUS, colorDot) + gui.drawFilledCircle(xdot, y, M.SLIDER_DOT_RADIUS, colorDot) for i = -1, 1 do - gui.drawCircle(xdot, y, SLIDER_DOT_RADIUS + i, colorDotBorder) + gui.drawCircle(xdot, y, M.SLIDER_DOT_RADIUS + i, colorDotBorder) end end @@ -914,7 +959,7 @@ function lib.newGUI() local v0 = self.value if gui.editing then - if match(event, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then + if M.match(event, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then gui.editing = false elseif event == EVT_VIRTUAL_INC then self.value = math.min(self.max, self.value + self.delta) @@ -939,7 +984,7 @@ function lib.newGUI() function self.covers(p, q) local xdot = x + w * (self.value - self.min) / (self.max - self.min) - return ((p - xdot) ^ 2 + (q - y) ^ 2 <= 2 * SLIDER_DOT_RADIUS ^ 2) + return ((p - xdot) ^ 2 + (q - y) ^ 2 <= 2 * M.SLIDER_DOT_RADIUS ^ 2) end addElement(self) @@ -963,22 +1008,22 @@ function lib.newGUI() function self.draw(focused) local ydot = y + h * (1 - (self.value - self.min) / (self.max - self.min)) - local colorBar = lib.colors.primary3 - local colorDot = lib.colors.primary2 - local colorDotBorder = lib.colors.primary3 + local colorBar = M.colors.primary3 + local colorDot = M.colors.primary2 + local colorDotBorder = M.colors.primary3 if focused then - colorDotBorder = lib.colors.active + colorDotBorder = M.colors.active if gui.editing or scrolling then - colorBar = lib.colors.primary1 - colorDot = lib.colors.edit + colorBar = M.colors.primary1 + colorDot = M.colors.edit end end gui.drawFilledRectangle(x - 2, y, 5, h, colorBar) - gui.drawFilledCircle(x, ydot, SLIDER_DOT_RADIUS, colorDot) + gui.drawFilledCircle(x, ydot, M.SLIDER_DOT_RADIUS, colorDot) for i = -1, 1 do - gui.drawCircle(x, ydot, SLIDER_DOT_RADIUS + i, colorDotBorder) + gui.drawCircle(x, ydot, M.SLIDER_DOT_RADIUS + i, colorDotBorder) end end @@ -986,7 +1031,7 @@ function lib.newGUI() local v0 = self.value if gui.editing then - if match(event, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then + if M.match(event, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then gui.editing = false elseif event == EVT_VIRTUAL_INC then self.value = math.min(self.max, self.value + self.delta) @@ -1011,7 +1056,7 @@ function lib.newGUI() function self.covers(p, q) local ydot = y + h * (1 - (self.value - self.min) / (self.max - self.min)) - return ((p - x) ^ 2 + (q - ydot) ^ 2 <= 2 * SLIDER_DOT_RADIUS ^ 2) + return ((p - x) ^ 2 + (q - ydot) ^ 2 <= 2 * M.SLIDER_DOT_RADIUS ^ 2) end addElement(self) @@ -1023,7 +1068,7 @@ function lib.newGUI() -- Create a custom element function gui.custom(self, x, y, w, h) self.gui = gui - self.lib = lib + self.lib = M function self.drawFocus(color) drawFocus(self.x or x, self.y or y, self.w or w, self.h or h, color) @@ -1054,7 +1099,7 @@ function lib.newGUI() -- Create a nested gui function gui.gui(x, y, w, h) - local self = lib.newGUI() + local self = M.newGUI() self.parent = gui self.editing = false self.x, self.y, self.w, self.h = x, y, w, h @@ -1072,4 +1117,4 @@ function lib.newGUI() end -- gui(...) -return lib +return M diff --git a/sdcard/c480x272/WIDGETS/LibGUI/loadable.lua b/sdcard/c480x272/WIDGETS/LibGUI/loadable.lua index 5c415048..41dde0c4 100644 --- a/sdcard/c480x272/WIDGETS/LibGUI/loadable.lua +++ b/sdcard/c480x272/WIDGETS/LibGUI/loadable.lua @@ -41,9 +41,10 @@ local HEIGHT = 24 -- The widget table will be returned to the main script local widget = { } --- Load the GUI library by calling the global function declared in the main script. --- As long as LibGUI is on the SD card, any widget can call loadGUI() because it is global. -local libGUI = loadGUI() +-- Load the GUI library. +-- Note: for backward & forward compatibility, each script should come with it's own version of libgui. +local libGUI = loadScript("/WIDGETS/LibGUI/libgui.lua")() + -- Instantiate a new GUI object local gui = libGUI.newGUI() @@ -88,7 +89,7 @@ subGUI.number(COL2s, 0, WIDTH, HEIGHT, 0) -- A drop-down with physical switches subGUI.label(0, ROW, WIDTH, HEIGHT, "Drop-down:") -labelDropDown = subGUI.label(0, 2 * ROW, 2 * WIDTH, HEIGHT, "") +local labelDropDown = subGUI.label(0, 2 * ROW, 2 * WIDTH, HEIGHT, "") local dropDownIndices = { } local dropDownItems = { } @@ -216,7 +217,12 @@ end -- This function is called from the refresh(...) function in the main script function widget.refresh(event, touchState) - gui.run(event, touchState) + gui.run(event, touchState) + if event == nil then + lcd.drawFilledRectangle(0, 100-10, 480, 70, RED) + lcd.drawText(40, 100, "This script is running currently in \"widget\"mode") + lcd.drawText(40, 125, "It only work in app-mode (full screen)") + end end -- Return to the create(...) function in the main script diff --git a/sdcard/c480x272/WIDGETS/LibGUI/main.lua b/sdcard/c480x272/WIDGETS/LibGUI/main.lua index 6523e5a2..ee47694e 100644 --- a/sdcard/c480x272/WIDGETS/LibGUI/main.lua +++ b/sdcard/c480x272/WIDGETS/LibGUI/main.lua @@ -21,17 +21,6 @@ -- GNU General Public License for more details. -- --------------------------------------------------------------------------- local name = "LibGUI" -local libGUI - --- Return GUI library table -function loadGUI() - if not libGUI then - -- Loadable code chunk is called immediately and returns libGUI - libGUI = loadScript("/WIDGETS/" .. name .. "/libgui.lua") - end - - return libGUI() -end --------------------------------------------------------------------------- -- The following widget implementation demonstrates how to use the -- @@ -52,7 +41,7 @@ end local function background(widget) end -local options = { +local options = { } local function update(widget, options)