Because no one asked for it ( )I went ahead and wrote a simple version of what I’m thinking of. It’s a little clumsy, but it shows the ease of use I personally would have loved as a newcomer.
This is the only code necessary in main
:
function draw()
background(colorYouChoose)
button("hello world")
button("sweet")
end
…that gets you two simple buttons that you can position as you like, and will retain their positions between project launches.
You can add as many buttons as you like just by adding more button(name)
statements to draw
I didn’t put in any callback functionality for this example because that implementation is a problem already solved elsewhere.
--# Main
-- SimpleButton
function setup()
--[[ commented out testing code:
local runningTest = false
if runningTest then
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)
end
]]
end
-- This function gets called once every frame
function draw()
background(255, 177, 0, 255)
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.buttons[name] = {x=math.random(WIDTH),y=math.random(HEIGHT), width=WIDTH/6, height=HEIGHT/15}
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 CurrentTouch.state == MOVING and draggable and shouldRespond then
buttonHandler.dragButton(name, vec2( CurrentTouch.x, CurrentTouch.y))
elseif CurrentTouch.state == ENDED then
buttonHandler.savePositions()
end
end
-- buttonHandler setup
buttonHandler = {}
buttonHandler.buttons = {}
buttonHandler.configured = false
buttonHandler.configure = function ()
parameter.boolean("draggable", false)
buttonHandler.configured = true
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=WIDTH/6, height=HEIGHT/15}\
"
end
saveProjectTab("ButtonTables",dataString)
end
--# ButtonTables
buttonHandler.buttons['sweet'] = {x = 148.0, y = 493.5, width=WIDTH/6, height=HEIGHT/15}
buttonHandler.buttons['hello world'] = {x = 148.0, y = 393.5, width=WIDTH/6, height=HEIGHT/15}
…you all seem to be off on a much different track here, though, so I’ll leave you to it. I just wanted to give a concrete example of my idea.