Until we get Jvm’s ultra-configurable button class, I thought I’d share some primitive code that will put buttons on screen for you ASAP.
Making a button just takes one line of code in the draw() function.
Type button("label text")
and you will get a button with that label drawn on screen.
You can make as many buttons as you want this way.
You don’t have to hand-code any button location because you can drag the buttons to where you want them while the program runs. The buttons will remember where they were placed and preserve their location even after you close the project.
--# Main
-- SimpleButton
displayMode(OVERLAY)
-- Use this function to perform your initial setup
function setup()
--[[
commented-out testing code:
testButtonName = "hello world"
button(testButtonName)
local touch = {x=10,y=19}
buttonHandler.savePositions(testButtonName, touch)
print("saved position")
buttonHandler.dragButton(testButtonName, touch)
print("dragged button")
loadedPosition = buttonHandler.positionOf(testButtonName)
print("loaded position is", loadedPosition.x, loadedPosition.y)
actionTestButtonName = "action button"
buttonHandler.defaultButton(actionTestButtonName)
print("action button defined")
local actionTest = function() print("test action fired") end
buttonAction(actionTestButtonName, actionTest)
print("buttonAction defined")
buttonHandler.doAction(actionTestButtonName)
print("action performed")
print("SUCCESS")
local actionTest2 = function() print("test action 2 fired") end
buttonAction("hello world", actionTest)
buttonAction("sweet", actionTest2)
]]
end
function draw()
background(255, 177, 0, 255)
--to show a button just add a button(name) statement here
--you don't need to do anything else
button("hello world")
button("sweet")
end
--# SimpleButton
button = function(name)
if buttonHandler.configured == false then
buttonHandler.configure()
end
pushStyle()
strokeWidth(3)
fill(255, 0, 0, 0)
local buttonColor = color(250, 250, 250, 255)
stroke(buttonColor)
rectMode(CENTER)
if buttonHandler.buttons[name] == nil then
buttonHandler.defaultButton(name)
end
buttonTable = buttonHandler.buttons[name]
rect(buttonTable.x,buttonTable.y,buttonTable.width,buttonTable.height)
fill(buttonColor)
text(name, buttonTable.x, buttonTable.y)
popStyle()
shouldRespond = buttonHandler.touchIsInside(name)
if shouldRespond then
if CurrentTouch.state == BEGAN and activatedButton == nil then
activatedButton = name
elseif activatedButton ~= name then
return
end
if buttons_are_draggable then
if CurrentTouch.state == MOVING then
buttonHandler.dragButton(name, vec2( CurrentTouch.x, CurrentTouch.y))
elseif CurrentTouch.state == ENDED then
buttonHandler.savePositions()
activatedButton = nil
end
elseif CurrentTouch.state == ENDED then
buttonHandler.doAction(name)
activatedButton = nil
end
end
end
buttonAction = function(name, action)
if buttonHandler.buttons[name]== nil then
buttonHandler.defaultButton(name)
end
buttonHandler.buttons[name].action = action
end
-- buttonHandler setup
buttonHandler = {}
buttonHandler.defaultWidth = 160
buttonHandler.defaultHeight = 55
buttonHandler.buttons = {}
buttonHandler.configured = false
buttonHandler.configure = function ()
parameter.boolean("buttons are draggable", false)
buttonHandler.configured = true
end
buttonHandler.defaultButton = function(name)
buttonHandler.buttons[name] = {x=math.random(WIDTH),y=math.random(HEIGHT), width=buttonHandler.defaultWidth, height=buttonHandler.defaultHeight}
end
buttonHandler.doAction = function(name)
if buttonHandler.buttons[name].action == nil then
print("in 'setup()', use 'buttonAction(name, action)' to define an action for this button")
return
end
buttonHandler.buttons[name].action()
end
buttonHandler.touchIsInside = function(name)
insideX = math.abs(CurrentTouch.prevX-buttonHandler.buttons[name].x) < buttonHandler.buttons[name].width /2
insideY = math.abs(CurrentTouch.prevY-buttonHandler.buttons[name].y) < buttonHandler.buttons[name].height /2
if insideX and insideY then
return true
end
return false
end
buttonHandler.dragButton = function (name, touch)
buttonHandler.buttons[name].x = touch.x
buttonHandler.buttons[name].y = touch.y
end
buttonHandler.positionOf = function (name)
return buttonHandler.buttons[name]
end
buttonHandler.savePositions = function (name, position)
dataString = ""
for name, buttonValues in pairs(buttonHandler.buttons) do
dataString = dataString.."buttonHandler.buttons['"..name.."'] = {x = "..buttonValues.x
dataString = dataString..", y = "..buttonValues.y
dataString = dataString..", width=buttonHandler.defaultWidth, height=buttonHandler.defaultHeight}\
"
end
saveProjectTab("ButtonTables",dataString)
end
--# ButtonTables
buttonHandler.buttons['hello world'] = {x = 734.5, y = 651.0, width=buttonHandler.defaultWidth, height=buttonHandler.defaultHeight}
buttonHandler.buttons['sweet'] = {x = 363.5, y = 581.0, width=buttonHandler.defaultWidth, height=buttonHandler.defaultHeight}
You can also add actions to the buttons very easily. Tapping a button while the project is running will tell you how to do it.
Being super-simple, they’re not very configurable beyond that, but they’ll get you up and running.
I hope someone finds this useful!
I also hope somebody feels inspired to make the buttons have rounded corners, and then shares that code, because that would be cool.