Skip to content

Instantly share code, notes, and snippets.

@Bri-G
Created May 30, 2012 07:52
Show Gist options
  • Save Bri-G/2834425 to your computer and use it in GitHub Desktop.
Save Bri-G/2834425 to your computer and use it in GitHub Desktop.
Rounded Buttons
-- Main
function setup()
saveProjectInfo("Description", "Cornered buttons demo")
saveProjectInfo("Version", "1.003 beta")
saveProjectInfo("Author", "Bri_G")
-- set up the function call parameters to play with the buttons
-- all self explanatory except cr which is the curvature control
-- note parameter range affects output, distortions are possible
parameter("cr",0,1,1)
iparameter("border",0,4,0)
iparameter("x",0,600,200)
iparameter("y",0,600,200)
iparameter("w",100,600,200)
iparameter("h",0,100,40)
butText = ""
name = ""
on = 1
off = 0
end
function draw()
-- This sets the background color to black
background(0, 0, 0)
-- Set the fill color
fill(255,0,0)
-- call the roundRect function
roundRect("trial button",x,y,w,h,cr,border)
end
function roundRect(butText,x,y,w,h,cr,border)
pushStyle()
pushMatrix()
if cr > 1 then cr = 1 end
d = h*cr
r=d/2
style("backStyle", on)
ellipse(x+r,y+r,d,d)
ellipse(x+w-r,y+r,d,d)
ellipse(x+r,y+h-r,d,d)
ellipse(x+w-r,y+h-r,d,d)
rect(x+r,y,w-d,h)
rect(x,y+r,w,h-d)
style("backStyle", off)
-- print centred text
style("fontStyle", on)
text(butText,x+w/2,y+h/2)
style("fontStyle", off)
if border > 0 then
fill(173, 50, 79, 255)
style("frontStyle", on)
ellipse(x+r,y+r,d,d)
ellipse(x+w-r,y+r,d,d)
ellipse(x+r,y+h-r,d,d)
ellipse(x+w-r,y+h-r,d,d)
rect(x+r,y,w-d,h)
rect(x,y+r,w,h-d)
style("frontStyle", off)
-- adjust internal variables for inner shape
-- retain temp copies of variables for text usage
-- then draw inner button
xx, yy, ww, hh = x, y, w, h
x = x + border
y = y + border
w = w - 2*border
h = h - 2*border
roundRect("trial button",x,y,w,h,cr,0)
end
popMatrix()
popStyle()
end
function style(name,condition)
if condition == on then
pushStyle()
pushMatrix()
if name == "butStyle" then
strokeWidth()
fill(226, 28, 28, 255)
elseif name == "backStyle" then
fill(43,68,174,255)
elseif name == "frontStyle" then
fill(136, 205, 60, 255)
elseif name == "fontStyle" then
font("Arial-BoldMT")
fontSize(17)
fill(226, 239, 37, 255)
end
elseif condition == off then
popStyle()
popMatrix()
end
end
@Bri-G
Copy link
Author

Bri-G commented May 30, 2012

This was my first attempt at constructing rounded buttons with text. I also introduced a style system where repeated styles can be called by name to minimise the number of lines in the main code.

All that remains is to add the touch detection to register selection by the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment