Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add momentary button #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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