Flower Maker

Main

--13 flowers
supportedOrientations(PORTRAIT)

function setup()
    displayMode(FULLSCREEN)
    itoip = true
    --itoip = false
    if itoip and WIDTH >320 then --draw itouch size on the ipad
        cs = (3.1)
    else
        cs = (3.1 * (320/WIDTH))
    end     
    makePots()
    s = Screen()
    bl = image(1,480)
    --stroke(0, 16, 255, 255)
    --stroke(0, 255, 57, 255)
    
    g = 255
    b = 0
    
    for y = 1, 480 do
        if b < 255 then b = b + 1 else g = g - 1 end
        bl:set(1,y,0,g,b,255)
    end
    
    
end

function draw() 
    background(123, 115, 30, 255)
    --for x = 1, WIDTH do
        --sprite(bl,x,HEIGHT/2)
    --end
    --sprite(bl,WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    if itoip then
        sprite(bl,160,240,320,480)
        stroke(0,0,0,255)
        strokeWidth(5)
        noFill()
        rectMode(CENTER)
        rect(160,240,320,480)
    else
        sprite(bl,WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    end
    s:draw()
end

function touched(touch)
    s:touched(touch)  
end

function keyboard(key)
    s:keyboard(key)
end

function makePots()
    pots = {}
    pots[1] = {x=160,y=240,f=Flower()}
    pots[2] = {x=320+160,y=240,f=Flower()}
    pots[3] = {x=320+320+160,y=240,f=Flower()}
    pots[4] = {x=320,y=240*2,f=Flower()}
    pots[5] = {x=320+320,y=240*2,f=Flower()}
    pots[6] = {x=160,y=240*3,f=Flower()}
    pots[7] = {x=320+160,y=240*3,f=Flower()}
    pots[8] = {x=320+320+160,y=240*3,f=Flower()}
    pots[9] = {x=320,y=240*4,f=Flower()}
    pots[10] = {x=320+320,y=240*4,f=Flower()}
    pots[11] = {x=160,y=240*5,f=Flower()}
    pots[12] = {x=320+160,y=240*5,f=Flower()}
    pots[13] = {x=320+320+160,y=240*5,f=Flower()}
end

function lrellipse(x,y,w,h,r)
    pushMatrix()
    ellipseMode(CENTER)
    translate(x+(w/2),y)
    rotate(r)
    translate(-x-(w/2),-y)
    ellipse(x,y,w,h)
    popMatrix()
end 
--# Flower
Flower = class()

function Flower:init(l,w,p,r,g,b,cr,cg,cb)
    --(petal length, petal width, number of petals ...
    --.petal red, green, blue ...
    --.center of flower red, green, blue)
    self.pt = {} --petal table
    local np
    if p == nil then p = math.random(16)+4 end
    --if p == nil then p = math.random(2)+4 end
    if l == nil then l = math.random(320/4)+50 end
    if w == nil then w = l/(math.random(8)+1) end
    if r == nil then r = math.random(256) - 1 end
    if g == nil then g = math.random(256) - 1 end
    if b == nil then b = math.random(256) - 1 end
    --if r == nil then r = math.random(64) + (64*3) end
    --if g == nil then g = math.random(64) + (64*3) end
    --if b == nil then b = math.random(64) + (64*3) end
    if cr == nil then self.cr = math.random(256) - 1 else self.cr = cr end
    if cg == nil then self.cg = math.random(256) - 1 else self.cg = cg end
    if cb == nil then self.cb = math.random(256) - 1 else self.cb = cb end
    --if cr == nil then self.cr = math.random(128) - 1 else self.cr = cr end
    --if cg == nil then self.cg = math.random(128) - 1 else self.cg = cg end
    --if cb == nil then self.cb = math.random(128) - 1 else self.cb = cb end
    self.fc = w +(math.random()*w)
    --self.fc = (math.random()*w) 
    for i = 1, p do
        local pa = (i*(360/p))+(math.random()*9)-5
        local pl = l +(math.random()*l*.1) - (l*.05)
        local pw = w +(math.random()*w*.1) - (w*.05)
        local pr = r + math.random(17) - 8
        local pg = g + math.random(17) - 8
        local pb = b + math.random(17) - 8
        self.pt[math.random()] = {pa=pa,pl=pl,pw=pw,pr=pr,pg=pg,pb=pb}
    end
    self.st = {} --stem table
    for i = 1, 10 do
        self.st[i] = math.random(256) - 1
    end
end

function Flower:draw(x,y)
    pushMatrix()
    strokeWidth(2)
    local k, v
    for k,v in pairs(self.st) do
        stroke(0, v, 0, 255)    
        line(x-5+k,y,x-5+k,y-200)
    end
    strokeWidth(1)
    stroke(0, 0, 0, 255)    
    for k,v in pairs(self.pt) do
        fill(v.pr,v.pg,v.pb,255)
        lrellipse(x-v.pl/2,y,v.pl,v.pw,v.pa)
    end
    fill(self.cr,self.cg,self.cr,255)
    ellipse(x,y,self.fc,self.fc)
    popMatrix()
end

function Flower:touched(touch)
    -- Codea does not automatically call this method
end

--# Icon
Icon = class()

function Icon:init(x,y,v)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.hx = x + 100 -- hide x
    self.hy = y + 100 -- hide y
    self.ox = x
    self.oy = y
end

function Icon:draw(hm)
    pushMatrix()
    rectMode(CENTER)
    noFill()
    strokeWidth(5)
    rect(self.x,self.y,80,120)
    popMatrix()
    if hm and self.x < self.hx then
            self.x = self.x + 1
    elseif self.x > self.ox then
            self.x = self.x - 2
    end
end

function Icon:touched(touch)
    -- Codea does not automatically call this method
end


--# Screen
Screen = class()

function Screen:init()
    self.menu = 0
    self.allpots = 1
    self.apot = 2
    self.newf = 3
    self.cscreen = self.allpots
    self.prompt = Words("Tap a flower",480,960)
    self.camera = ""
    self.mf = 1 --main flower
    self.msi = Icon(450,120) --main screen icon
    self.ti = Icon(450,240) --text icon
    self.hm = false --hide menu
    self.mcx = 450
    self.mvx = 450
    self.mhx = 550
    self.ft = ""
end

function Screen:draw()
    if self.cscreen == self.allpots then self:AllPotsDraw()
    elseif self.cscreen == self.apot then self:APotDraw()
    --elseif self.cscreen == self.newf then
    end
end

function Screen:touched(touch)
    if touch.state == BEGAN then 
        if self.cscreen == self.allpots then self:AllPotsTouch(touch)
        elseif self.cscreen == self.apot then self:APotTouch(touch)
            
            --self.help.x = touch.x*cs/2
            --self.help.y = touch.y*cs/2
        end
    end
end

function Screen:keyboard(key)
    if key ~= nil then
        --string.byte(key) == 10 
        if string.byte(key) == nil then
            if string.len(self.ftext) > 0 then
                self.ftext = string.sub(self.ftext,1,string.len(self.ftext) - 1)
            end
        else
            if self.ftext == nil then self.ftext = key
            else self.ftext = self.ftext .. key end
        end
    end
end


function Screen:AllPotsDraw()
    pushMatrix()
    scale(1/cs)   
    local i
    for i = 1, 13 do
        pots[i].f:draw(pots[i].x,pots[i].y)
    end
    self.prompt:draw()
    popMatrix()        
end

function Screen:AllPotsTouch(touch)
    local i
    for i = 1, 13 do
        local p = vec2(pots[i].x,pots[i].y)
        if p:dist(vec2(touch.x*cs,touch.y*cs)) < 100 then
            self.mf = i
            self.cscreen = self.apot
            --self.help = Words("?",(320+320+160)/2,240*2.5)
            self.prompt = Words("Take a picture",480/2,960/2)
            if itoip then
            self.camera = Words(".",295,10)   
            else 
            self.camera = Words(".",125,10)
            end
            self.hm = true
        end
    end
end

function Screen:APotDraw()
    pushMatrix()
    scale(2/cs)
    pots[self.mf].f:draw((320+160)/2,(240*3)/2)
    self.prompt:draw()
    self.camera:draw()
    if self.hm and self.mcx < self.mhx then
        self.mcx = self.mcx + 4
    elseif not self.hm and self.mcx > self.mvx then
        self.mcx = self.mcx - 16
    end
    self.msi.x = self.mcx
    self.msi:draw()
    self.ti.x = self.mcx
    self.ti:draw()    
    pushStyle()
    fontSize(32)
    fill(111, 0, 255, 255)
    text("A",self.ti.x -16,self.ti.y +32)
    text("B",self.ti.x,self.ti.y)
    text("C",self.ti.x +16,self.ti.y -32)
    fill(255, 255, 255, 255)
    font("Zapfino")
    textWrapWidth(360)
    fontSize(24)
    textAlign(CENTER)
    if self.ftext ~= nil then 
        if itoip then text(self.ftext.." ",240,600) 
        else text(self.ftext.." ",240,540) end
    end
    font()
    popStyle()
    
    translate(self.msi.x -40,self.msi.y -60)
    scale(1/12)        
    local i
    for i = 1, 13 do
        pots[i].f:draw(pots[i].x,pots[i].y)
    end
    popMatrix()
end

function Screen:APotTouch(touch)
    local t = vec2(touch.x*cs/2,touch.y*cs/2)
    if t:dist(vec2(self.msi.x,self.msi.y)) < 50 then
        self.cscreen = self.allpots
    elseif t:dist(vec2(self.ti.x,self.ti.y)) < 50 then
        self.hm = true
        showKeyboard()
    elseif touch.x*cs/2 > 100 then
        if self.hm and self.mcx >= self.mhx then
            self.hm = false
        elseif self.mcx <= self.mvx then
            self.hm = true
        end
    end
end
--# Words
Words = class()

function Words:init(words,x,y)
    self.words = words
    self.x = x
    self.y = y
    self.start = ElapsedTime
end

function Words:draw()
    pushStyle()
    rectMode(CENTER)
    ellipseMode(CENTER)
    fontSize(64)
    local fade = 0
    if (ElapsedTime - self.start) > 3 then
        fade = (ElapsedTime - self.start - 3) * 64
    end
    fill(255,0,111,255-fade)
    noStroke()
    local bwidth = fontSize()*string.len(self.words)/2
    ellipse(self.x,self.y,bwidth,fontSize()*2)
    fill(111, 0, 255, 255-fade)
    text(self.words,self.x,self.y)
    popStyle()
    -- Codea does not automatically call this method
end

function Words:touched(touch)
    -- Codea does not automatically call this method
end

Cool! I can create a postcard with a lovely message for my 7-year-old daughter tonight and show the card with iPad in morning tomorrow. She is sleeping now. Thanks.

If trees screamed…

http://www.youtube.com/watch?v=3jasSXjajzE&feature=youtube_gdata_player

Main and Screen class updated

Not to sure about the background color I picked. Should I go back to a brown but with a fade? Anyone know a good brown series?

Or make it customizable with the option of a tricolor too…