Skip to content

Commit

Permalink
feat(LibGUI): add momentary button (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
wimalopaan authored Oct 17, 2024
1 parent e38937a commit 8c0b517
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sdcard/c480x272/WIDGETS/LibGUI/libgui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,64 @@ function M.newGUI()

-----------------------------------------------------------------------------------------------

-- create a momentary button
function gui.momentaryButton(x, y, w, h, title, callBack, flags)
local self = {
title = title,
callBack = callBack or _.doNothing,
flags = bit32.bor(flags or M.flags, CENTER, VCENTER),
disabled = false,
hidden = false
}

function self.draw(focused)
local fg = M.colors.primary2
local bg = M.colors.focus
local border = M.colors.active

if self.value then
fg = M.colors.primary3
bg = M.colors.active
border = M.colors.focus
end

gui.drawFilledRectangle(x, y, w, h, bg)
gui.drawText(x + w / 2, y + h / 2, self.title, bit32.bor(fg, self.flags))

if focused then
gui.drawRectangle(x - 2, y - 2, w + 4, h + 4, border, 2)
end

if self.disabled then
gui.drawFilledRectangle(x, y, w, h, GREY, 7)
end
end

function self.onEvent(event, touchState)
if (event == EVT_TOUCH_FIRST) then
if (self.covers(touchState.x, touchState.y)) then
gui.editing = true;
self.value = true;
return self.callBack(self);
end
elseif (event == EVT_VIRTUAL_ENTER_LONG) then
gui.editing = true;
self.value = true;
return self.callBack(self);
elseif ((event == EVT_TOUCH_BREAK) or (event == EVT_VIRTUAL_EXIT)) then
gui.editing = false;
self.value = false;
return self.callBack(self);
end
end

addElement(self, x, y, w, h)

return self
end

-----------------------------------------------------------------------------------------------

-- Create a toggle button that turns on/off. callBack gets true/false
function gui.toggleButton(x, y, w, h, title, value, callBack, flags)
local self = {
Expand Down
3 changes: 3 additions & 0 deletions sdcard/c480x272/WIDGETS/LibGUI/loadable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ local menuItems = {

gui.menu(COL4, TOP + ROW, WIDTH, 5 * ROW, menuItems, function(menu) playNumber(menu.selected, 0) end)

-- momentary button
gui.momentaryButton(COL1, TOP + 5 * ROW, WIDTH, HEIGHT, "Momentary");

-- Horizontal slider
gui.label(COL1, TOP + 6 * ROW, WIDTH, HEIGHT, "Horizontal slider:", BOLD)
local horizontalSliderLabel = gui.label(COL1 + 2 * WIDTH, TOP + 7 * ROW, 30, HEIGHT, "", RIGHT)
Expand Down
58 changes: 58 additions & 0 deletions sdcard/c480x320/WIDGETS/LibGUI/libgui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,64 @@ function M.newGUI()

-----------------------------------------------------------------------------------------------

-- create a momentary button
function gui.momentaryButton(x, y, w, h, title, callBack, flags)
local self = {
title = title,
callBack = callBack or _.doNothing,
flags = bit32.bor(flags or M.flags, CENTER, VCENTER),
disabled = false,
hidden = false
}

function self.draw(focused)
local fg = M.colors.primary2
local bg = M.colors.focus
local border = M.colors.active

if self.value then
fg = M.colors.primary3
bg = M.colors.active
border = M.colors.focus
end

gui.drawFilledRectangle(x, y, w, h, bg)
gui.drawText(x + w / 2, y + h / 2, self.title, bit32.bor(fg, self.flags))

if focused then
gui.drawRectangle(x - 2, y - 2, w + 4, h + 4, border, 2)
end

if self.disabled then
gui.drawFilledRectangle(x, y, w, h, GREY, 7)
end
end

function self.onEvent(event, touchState)
if (event == EVT_TOUCH_FIRST) then
if (self.covers(touchState.x, touchState.y)) then
gui.editing = true;
self.value = true;
return self.callBack(self);
end
elseif (event == EVT_VIRTUAL_ENTER_LONG) then
gui.editing = true;
self.value = true;
return self.callBack(self);
elseif ((event == EVT_TOUCH_BREAK) or (event == EVT_VIRTUAL_EXIT)) then
gui.editing = false;
self.value = false;
return self.callBack(self);
end
end

addElement(self, x, y, w, h)

return self
end

-----------------------------------------------------------------------------------------------

-- Create a toggle button that turns on/off. callBack gets true/false
function gui.toggleButton(x, y, w, h, title, value, callBack, flags)
local self = {
Expand Down
3 changes: 3 additions & 0 deletions sdcard/c480x320/WIDGETS/LibGUI/loadable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ local menuItems = {

gui.menu(COL4, TOP + ROW, WIDTH, 5 * ROW, menuItems, function(menu) playNumber(menu.selected, 0) end)

-- momentary button
gui.momentaryButton(COL1, TOP + 5 * ROW, WIDTH, HEIGHT, "Momentary");

-- Horizontal slider
gui.label(COL1, TOP + 6 * ROW, WIDTH, HEIGHT, "Horizontal slider:", BOLD)
local horizontalSliderLabel = gui.label(COL1 + 2 * WIDTH, TOP + 7 * ROW, 30, HEIGHT, "", RIGHT)
Expand Down

0 comments on commit 8c0b517

Please sign in to comment.