Created
October 6, 2012 22:06
-
-
Save devilstower/3846323 to your computer and use it in GitHub Desktop.
Cider 1.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
-- Cider | |
-- an interactive interface builder for Codea | |
-- version 1.1 | |
-- 6 October 2012 | |
-- Mark Sumner | |
-- [email protected] | |
displayMode(FULLSCREEN) | |
minSize = 30 | |
function setup() | |
trayOpen = false | |
showCode = false | |
trayTab = Frame(WIDTH - 25, HEIGHT / 2 - 30, | |
WIDTH + 25, HEIGHT / 2 + 30) | |
trayWidth = 0 | |
trayMaxWidth = 150 | |
trayFrame = Frame(WIDTH - trayWidth, 0, trayMaxWidth, HEIGHT) | |
elements = {} | |
trayElement = {} | |
traySpeed = 20 | |
trayElement[1] = UIElement(1, "Label", 10, HEIGHT - 60, | |
130, HEIGHT - 30) | |
trayElement[2] = UIElement(2, "Button", 10, HEIGHT - 110, | |
140, HEIGHT - 80) | |
trayElement[3] = UIElement(3, "Text Box", 10, HEIGHT - 160, | |
140, HEIGHT - 130) | |
trayElement[4] = UIElement(4, "Switch;off", 10, HEIGHT - 210, | |
110, HEIGHT - 180) | |
trayElement[5] = UIElement(5, "", 10, HEIGHT - 270, | |
150, HEIGHT - 240) | |
trayElement[6] = UIElement(6, "Drop List", 10, HEIGHT - 310, | |
140, HEIGHT - 280) | |
trayElement[7] = UIElement(7, "Multi;Choice;Button", 10, | |
HEIGHT - 370, 150, HEIGHT - 330) | |
btnCode = TextButton("Show Code", 10, 50, 140, 80) | |
btnCode.background = color(147, 147, 147, 255) | |
eSelected = 0 | |
eType = 0 | |
dragElement = nil | |
leftTopSize = Frame(0, 0, 0, 0) | |
rightTopSize = Frame(0, 0, 0, 0) | |
leftBottomSize = Frame(0, 0, 0, 0) | |
rightBottomSize = Frame(0, 0, 0, 0) | |
infoRect = Frame(-90, 0, -90, 0) | |
startRect = Frame(0, 0, 0, 0) | |
stretch = 0 | |
dialog = Dialog("Properties", 200, HEIGHT - 500, | |
WIDTH - 200, HEIGHT - 30) | |
showProperties = false | |
showHandles = false | |
l = dialog.left + 100 | |
r = dialog.right - 30 | |
b = dialog.bottom + 40 | |
t = dialog.top - 75 | |
txtName = TextBox("", l, t, r, t + 30) | |
txtText = TextBox("", l, t - 40, r, t - 10) | |
txtName.selected = false | |
txtText.selected = false | |
drpFont = DropList("ArialMT;Arial-BoldMT;Copperplate;".. | |
"Courier;TimesNewRomanPSMT", l, t - 80, r, t - 50) | |
drpFont.selected = 1 | |
sldFontSize = Slider("", l, t - 140, r, t - 90, | |
8 , 48, 14) | |
dragElement = nil | |
stretch = 0 | |
end | |
function drawTray() | |
pushMatrix() | |
pushStyle() | |
translate(WIDTH - trayWidth, 0) | |
for i, e in ipairs(trayElement) do | |
e:draw() | |
end | |
btnCode:draw() | |
popStyle() | |
popMatrix() | |
end | |
function drawProperties() | |
pushStyle() | |
fontSize(14) | |
font("ArialMT") | |
dialog:draw() | |
strokeWidth(2) | |
stroke(30, 30, 30, 255) | |
line(dialog.left, txtName.bottom - 4, | |
dialog.right, txtName.bottom - 4) | |
line(dialog.left, txtText.bottom - 4, | |
dialog.right, txtText.bottom - 4) | |
line(dialog.left, drpFont.top - 32, | |
dialog.right, drpFont.top - 32) | |
textMode(CORNER) | |
textAlign(LEFT) | |
fill(0, 0, 0, 255) | |
text("Name", dialog.left + 10, txtName.bottom + 4) | |
txtName:draw() | |
text("Text", dialog.left + 10, txtText.bottom + 4) | |
txtText:draw() | |
text("Font", dialog.left + 10, drpFont.top - 24) | |
text("Font Size", dialog.left + 10, | |
sldFontSize.top - 24) | |
sldFontSize:draw() | |
drpFont:draw() | |
popStyle() | |
end | |
function draw() | |
-- This sets a dark background color | |
background(194, 194, 194, 255) | |
font("ArialMT") | |
fontSize(222) | |
fill(146, 140, 140, 29) | |
text("Cider", WIDTH / 2, HEIGHT / 2 + 200) | |
fontSize(72) | |
text("Interface Designer", WIDTH / 2, HEIGHT / 2 - 100) | |
fontSize(14) | |
-- Do your drawing here | |
for i, e in ipairs(elements) do | |
e:draw() | |
end | |
if trayOpen then | |
if trayWidth < trayMaxWidth then | |
trayWidth = trayWidth + traySpeed | |
trayTab:offset(-traySpeed , 0) | |
end | |
else | |
if trayWidth > 0 then | |
trayWidth = trayWidth - traySpeed | |
trayTab:offset(traySpeed , 0) | |
end | |
end | |
strokeWidth(2) | |
fill(216, 216, 216, 255) | |
ellipse(WIDTH - trayWidth, HEIGHT / 2, 40) | |
rect(WIDTH - trayWidth, 0, trayWidth, HEIGHT) | |
noStroke() | |
rect(WIDTH - trayWidth -5, HEIGHT / 2 - 18, 10, 36) | |
trayFrame.left = WIDTH - trayWidth | |
trayFrame.right = trayFrame.left + trayMaxWidth | |
strokeWidth(5) | |
stroke(127, 127, 127, 255) | |
line(WIDTH - trayWidth - 10, HEIGHT / 2, | |
WIDTH - trayWidth - 0, HEIGHT / 2 - 5) | |
line(WIDTH - trayWidth - 10, HEIGHT / 2, | |
WIDTH - trayWidth - 0, HEIGHT / 2 + 5) | |
if trayWidth > 0 then | |
drawTray() | |
end | |
if dragElement then | |
dragElement:draw() | |
end | |
if showHandles then | |
fontSize(14) | |
font("ArialMT") | |
strokeWidth(4) | |
stroke(78, 175, 203, 197) | |
noFill() | |
line(leftTopSize.left, leftTopSize.top, | |
leftTopSize.left + 15, leftTopSize.top) | |
line(leftTopSize.left, leftTopSize.top, | |
leftTopSize.left, leftTopSize.top - 15) | |
line(leftBottomSize.left, leftBottomSize.bottom, | |
leftBottomSize.left + 15, leftBottomSize.bottom) | |
line(leftBottomSize.left, leftBottomSize.bottom, | |
leftBottomSize.left, leftBottomSize.bottom + 15) | |
line(rightBottomSize.right - 15, rightBottomSize.bottom, | |
rightBottomSize.right, rightBottomSize.bottom) | |
line(rightBottomSize.right, rightBottomSize.bottom, | |
rightBottomSize.right, rightBottomSize.bottom + 15) | |
line(rightTopSize.right - 15, rightTopSize.top, | |
rightTopSize.right, rightTopSize.top) | |
line(rightTopSize.right, rightTopSize.top, | |
rightTopSize.right, rightTopSize.top - 15) | |
strokeWidth(1) | |
stroke(255, 255, 255, 255) | |
fill(93, 103, 230, 255) | |
ellipse(infoRect:midX(), infoRect:midY(), 25) | |
fill(255, 255, 255, 255) | |
text("i", infoRect:midX(), infoRect:midY()) | |
end | |
if showProperties then | |
drawProperties() | |
end | |
end | |
function resetHandles(x, y) | |
l = dragElement.element.left - minSize | |
b = dragElement.element.bottom - minSize | |
r = dragElement.element.right | |
t = dragElement.element.top | |
x = dragElement.element:midX() | |
--displayMode(STANDARD) | |
--print(l,b,r,t,x) | |
leftTopSize.left = l | |
leftTopSize.right = l + minSize | |
leftTopSize.bottom = t | |
leftTopSize.top = t + minSize | |
rightTopSize.left = r | |
rightTopSize.right = r + minSize | |
rightTopSize.bottom = t | |
rightTopSize.top = t + minSize | |
rightBottomSize.left = r | |
rightBottomSize.right = r + minSize | |
rightBottomSize.bottom = b | |
rightBottomSize.top = b + minSize | |
leftBottomSize.left = l | |
leftBottomSize.right = l + minSize | |
leftBottomSize.bottom = b | |
leftBottomSize.top = b + minSize | |
infoRect.left = x - minSize / 2 | |
infoRect.right = x + minSize / 2 | |
infoRect.bottom = t + minSize | |
infoRect.top = t + minSize * 2 | |
end | |
function printElements() | |
showCode = true | |
displayMode(STANDARD) | |
print("-- ===================") | |
print("-- Copy this code into the init function") | |
print("-- ===================") | |
for i, e in ipairs(elements) do | |
e:printInit() | |
end | |
print(" ") | |
print("-- ===================") | |
print("-- Copy this code into the draw function") | |
print("-- ===================") | |
for i, e in ipairs(elements) do | |
e:printDraw() | |
end | |
print(" ") | |
print("-- ===================") | |
print("-- Copy this code into the touched function") | |
print("-- ===================") | |
for i, e in ipairs(elements) do | |
e:printTouched() | |
end | |
end | |
function closeDialog() | |
showProperties = false | |
if string.len(txtName.text) > 0 then | |
dragElement.element.name = txtName.text | |
end | |
dragElement.element.text = txtText.text | |
if dragElement.type == SWITCHTYPE or | |
dragElement.type == MULTIBUTTONTYPE or | |
dragElement.type == DROPLISTTYPE then | |
dragElement.element.itemText = {} | |
dragElement.element:splitText() | |
end | |
dragElement.element.font = drpFont.itemText[drpFont.selected] | |
dragElement.element.fontSize = sldFontSize.val | |
if isKeyboardShowing() then hideKeyboard() end | |
sound(SOUND_HIT, 28774) | |
end | |
function touchedProperties(touch) | |
if touch.state == BEGAN and oldState ~= BEGAN then | |
if dialog:touched(touch) then | |
txtName.selected = false | |
txtText.selected = false | |
if txtName:touched(touch) then | |
txtName.selected = true | |
if not isKeyboardShowing() then showKeyboard() end | |
end | |
if txtText:touched(touch) then | |
txtText.selected = true | |
if not isKeyboardShowing() then showKeyboard() end | |
end | |
if drpFont:touched(touch) then | |
end | |
if sldFontSize:touched(touch) then | |
end | |
else | |
closeDialog() | |
end | |
end | |
if touch.state == MOVING or touch.state == ENDED then | |
if dialog:touched(touch) then | |
drpFont:touched(touch) | |
sldFontSize:touched(touch) | |
else | |
--closeDialog() | |
end | |
end | |
oldState = touch.state | |
end | |
function touchedHandles(touch) | |
if touch.state == BEGAN and oldState ~= BEGAN then | |
stretch = 0 | |
if leftTopSize:touched(touch) then stretch = 1 | |
elseif rightTopSize:touched(touch) then stretch = 2 | |
elseif leftBottomSize:touched(touch) then stretch = 3 | |
elseif rightBottomSize:touched(touch) then stretch = 4 | |
elseif infoRect:touched(touch) then | |
stretch = 5 | |
showProperties = true | |
txtName.text = dragElement.name | |
txtText.text = dragElement.element.text | |
sldFontSize.val = dragElement.element.fontSize | |
end | |
if stretch == 0 then | |
showHandles = false | |
end | |
elseif touch.state == MOVING then | |
x = touch.x | |
y = touch.y | |
if stretch == 1 then | |
-- top left | |
if x > rightTopSize.left - minSize then | |
x = rightTopSize.left - minSize | |
end | |
if y < leftBottomSize.top + minSize then | |
y = leftBottomSize.top + minSize | |
end | |
dragElement.element.left = x | |
dragElement.element.top = y | |
elseif stretch == 2 then | |
-- top right | |
if x < leftTopSize.right + minSize then | |
x = leftTopSize.right + minSize | |
end | |
if y < leftBottomSize.top + minSize then | |
y = leftBottomSize.top + minSize | |
end | |
dragElement.element.right = x | |
dragElement.element.top = y | |
elseif stretch == 3 then | |
-- bottom left | |
if x > rightTopSize.left - minSize then | |
x = rightTopSize.left - minSize | |
end | |
if y > leftTopSize.bottom - minSize then | |
y = leftTopSize.bottom - minSize | |
end | |
dragElement.element.left = x | |
dragElement.element.bottom = y | |
elseif stretch == 4 then | |
-- bottom right | |
if x < leftTopSize.right + minSize then | |
x = leftTopSize.right + minSize | |
end | |
if y > leftTopSize.bottom - minSize then | |
y = leftTopSize.bottom - minSize | |
end | |
dragElement.element.right = x | |
dragElement.element.bottom = y | |
end | |
resetHandles() | |
elseif touch.state == ENDED and oldState == MOVING then | |
resetHandles() | |
end | |
end | |
function placeDragElement() | |
local e | |
if dragElement then | |
e = UIElement(dragElement.type, dragElement.element.text, | |
dragElement.element.left, dragElement.element.bottom, | |
dragElement.element.right, dragElement.element.top) | |
e.element.font = dragElement.element.font | |
e.element.fontSize = dragElement.element.fontSize | |
e.element.itemText = dragElement.element.itemText | |
table.insert(elements, e) | |
dragElement = nil | |
showHandles = false | |
end | |
end | |
function liftDragElement(e, l) | |
dragElement = UIElement(e.type, e.element.text, | |
e.element.left + l, e.element.bottom, | |
e.element.right + l, e.element.top) | |
dragElement.element.font = e.element.font | |
dragElement.element.fontSize = e.element.fontSize | |
dragElement.element.itemText = e.element.itemText | |
resetHandles(dragElement) | |
showHandles = false | |
end | |
function touched(touch) | |
if showProperties then | |
touchedProperties(touch) | |
oldState = touch.state | |
return false | |
elseif showHandles and dragElement then | |
touchedHandles(touch) | |
if showProperties then return false end | |
end | |
if touch.state == BEGAN and oldState ~= BEGAN then | |
if showCode then | |
showCode = false | |
displayMode(FULLSCREEN) | |
return false | |
end | |
if dragElement and not showHandles then | |
if dragElement:touched(touch) then | |
showHandles = true | |
resetHandles(e) | |
end | |
end | |
if trayTab:touched(touch) then | |
placeDragElement() | |
trayOpen = not trayOpen | |
elseif trayOpen and trayFrame:touched(touch) then | |
if dragElement then placeDragElement() end | |
tt = Ttouch(touch) | |
tt:translate(trayFrame.left, 0) | |
if btnCode:touched(tt) then | |
printElements() | |
end | |
for i, e in ipairs(trayElement) do | |
if e:touched(tt) then | |
liftDragElement(e, trayFrame.left) | |
trayOpen = false | |
end | |
end | |
else | |
-- pick up element | |
for i, e in ipairs(elements) do | |
if e:touched(touch) then | |
if dragElement then placeDragElement() end | |
liftDragElement(e, 0) | |
table.remove(elements, i) | |
end | |
end | |
end | |
end | |
if touch.state == MOVING and dragElement | |
and showHandles == false then | |
w = dragElement.element:width() | |
h = dragElement.element:height() | |
dragElement.element.left = touch.deltaX + | |
dragElement.element.left | |
dragElement.element.right = dragElement.element.left + w | |
dragElement.element.bottom = dragElement.element.bottom + | |
touch.deltaY | |
dragElement.element.top = dragElement.element.bottom + h | |
elseif touch.state == ENDED and dragElement | |
and oldState == MOVING then | |
placeDragElement() | |
elseif touch.state == ENDED and dragElement | |
and oldState == BEGAN then | |
if dragElement:touched(touch) then | |
resetHandles(dragElement.element.left, | |
dragElement.element.right) | |
showHandles = true | |
else | |
showHandles = false | |
end | |
end | |
oldState = touch.state | |
end | |
function keyboard(key) | |
if showProperties then | |
if txtName.selected then | |
i = string.len(txtName.text) | |
if key == BACKSPACE and i > 0 then | |
txtName.text = string.sub(txtName.text, 1, i - 1) | |
else | |
txtName.text = txtName.text..key | |
end | |
end | |
if txtText.selected then | |
i = string.len(txtText.text) | |
if key == BACKSPACE and i > 0 then | |
txtText.text = string.sub(txtText.text, 1, i - 1) | |
else | |
txtText.text = txtText.text..key | |
end | |
end | |
end | |
end | |
--# UIElement | |
UIElement = class() | |
LABELTYPE = 1 | |
BUTTONTYPE = 2 | |
TEXTBOXTYPE = 3 | |
SWITCHTYPE = 4 | |
SLIDERTYPE = 5 | |
DROPLISTTYPE = 6 | |
MULTIBUTTONTYPE = 7 | |
function UIElement:init(type, text, left, bottom, right, top) | |
self.type = type | |
self.element = nil | |
if type == LABELTYPE then | |
self.element = Label(text, left, bottom, right, top) | |
self.name = "lbl"..math.random(999) | |
elseif type == BUTTONTYPE then | |
self.element = TextButton(text, left, bottom, right, top) | |
self.name = "btn"..math.random(999) | |
elseif type == TEXTBOXTYPE then | |
self.element = TextBox(text, left, bottom, right, top) | |
self.name = "txt"..math.random(999) | |
elseif type == SWITCHTYPE then | |
self.element = Switch(text, left, bottom, right, top) | |
self.name = "swt"..math.random(999) | |
elseif type == SLIDERTYPE then | |
self.element = Slider(text, left, bottom, right, top, | |
0, 100, 50) | |
self.name = "sld"..math.random(999) | |
elseif type == DROPLISTTYPE then | |
self.element = DropList(text, left, bottom, right, top) | |
self.name = "drp"..math.random(999) | |
elseif type == MULTIBUTTONTYPE then | |
self.element = MultiButton(text, left, bottom, right, top) | |
self.name = "mlb"..math.random(999) | |
end | |
end | |
function UIElement:draw() | |
self.element:draw() | |
end | |
function UIElement:printInit() | |
print(self.name.." = "..self.element:initString()) | |
if self.element.fontSize ~= 14 then | |
print(self.name..".fontSize = "..self.element.fontSize) | |
end | |
if self.element.font ~= "ArialMT" then | |
print(self.name..".font = '"..self.element.font.."'") | |
end | |
end | |
function UIElement:printDraw() | |
print(self.name..":draw()") | |
end | |
function UIElement:printTouched() | |
print(self.name..":touched(touch)") | |
end | |
function UIElement:touched(touch) | |
return self.element:ptIn(touch.x, touch.y) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment