Skip to content

Instantly share code, notes, and snippets.

@DolenzSong
Created November 8, 2017 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DolenzSong/8b763712b687fb0d282f4c25483b0a47 to your computer and use it in GitHub Desktop.
Save DolenzSong/8b763712b687fb0d282f4c25483b0a47 to your computer and use it in GitHub Desktop.
Codea button creation following the model of the built-in ellipse and rect functions
--# Main
-- SimpleButton
displayMode(OVERLAY)
-- Use this function to perform your initial setup
function setup()
-- Uncomment to run super simple tests:
--tests()
end
function draw()
background(255, 177, 0, 255)
--to show a button just add a button(name) statement to draw()
button("hello world")
--you don't need to do anything else
--the button will start in a random position
--which is fine because you can manually position it while the project runs
--and the project will save your positioning so the buttons stay in place
--buttons act like all other Codea drawing shapes
--they simply use all the currently defined styles
pushStyle()
stroke(224, 18, 18, 255)
fill(26, 153, 95, 255)
strokeWidth(8)
font("AmericanTypewriter-Bold")
fontSize(15)
button("I use custom styles")
popStyle()
--to specify a button action, just add a function after the button name
local tapResponse = function() print("you tapped me!") end
button("tap me", tapResponse)
--buttons use a default font color or a passed-in font color parameter
button("font color customized", nil, color(255,180,80,255))
--buttons use a default width and height or passed-in parameters
button("size customized", nil, nil, 300, 30)
--buttons choose initial random positions or can be positioned in code
button("positioned in code", nil, nil, nil, nil, WIDTH-200, 80)
--here's a fully customized button
pushStyle()
stroke(253, 0, 198, 255)
fill(223, 168, 159, 255)
strokeWidth(3)
font("Zapfino")
fontSize(35)
button("Fully custom, yo!", function() print("I've got my own response!") end, color(253, 0, 198, 255), 400, 150, WIDTH/2, HEIGHT/2)
popStyle()
end
--# SimpleButton
button = function(name, action, textColor, width, height, x, y)
if buttonHandler.configured == false then
buttonHandler.configure()
end
if buttonHandler.buttons[name] == nil then
buttonHandler.buttons[name] = {}
end
if action ~= nil and buttonHandler.buttons[name].action == nil then
buttonHandler.buttons[name].action = action
end
buttonHandler.positionByTouchIfTouched(name)
buttonHandler.buttons[name].x = x or buttonHandler.buttons[name].x or buttonHandler.randomX(name)
buttonHandler.buttons[name].y = y or buttonHandler.buttons[name].y or buttonHandler.randomY(name)
buttonHandler.buttons[name].width = width or buttonHandler.buttons[name].width or buttonHandler.defaultWidth
buttonHandler.buttons[name].height = height or buttonHandler.buttons[name].height or buttonHandler.defaultHeight
buttonTable = buttonHandler.buttons[name]
local wordColor = textColor or color(215, 221, 231, 255)
rectMode(CENTER)
rect(buttonTable.x,buttonTable.y,buttonTable.width,buttonTable.height)
pushStyle()
fill(wordColor)
text(name, buttonTable.x, buttonTable.y)
popStyle()
end
-- buttonHandler setup
buttonHandler = {}
buttonHandler.defaultWidth = 190
buttonHandler.defaultHeight = 65
buttonHandler.buttons = {}
buttonHandler.configured = false
buttonHandler.configure = function ()
parameter.boolean("buttons_can_be_dragged", false)
buttonHandler.configured = true
end
buttonHandler.randomX = function(name)
print("button '"..name.."' is using a random x value")
return math.random(WIDTH)
end
buttonHandler.randomY = function(name)
print("button '"..name.."' is using a random y value")
return math.random(HEIGHT)
end
buttonHandler.doAction = function(name)
if buttonHandler.buttons[name].action == nil then
print("Specify a function after the button's name to define an action for the button '"..name.."'")
return
else
buttonHandler.buttons[name].action()
end
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.positionByTouchIfTouched = function(name)
if CurrentTouch.state == BEGAN and buttonHandler.touchIsInside(name) then
activatedButton = name
end
if activatedButton == name then
if buttons_can_be_dragged 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
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 ()
dataString = "--delete this tab to erase positions created by dragging\n"
for name, buttonValues in pairs(buttonHandler.buttons) do
dataString = dataString.."buttonHandler.buttons['"..name.."'] = {x = "..buttonValues.x
dataString = dataString..", y = "..buttonValues.y
dataString = dataString..", width = "..buttonValues.width..", height = "..buttonValues.height.."}\n"
end
saveProjectTab("ButtonTables",dataString)
end
--# Tests
function tests()
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)
print("TESTS SUCCEEDED")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment