SODA Designer: why doesn't this work?

@yojimbo2000:

I’m trying to make a really simple project called “SODA Designer”, where you can use Codea’s interactive parameters to play with the size and shape and color and etc of a SODA Button, and see the results in real time.

I started by trying make an adjustable width.

Seemed simple: I made a button called “guineaPig”, a parameter called “ButtonWidth”, and a callback function that sets the “w” variable of guineaPig to the current value of ButtonWith.

Doesn’t work. I can’t figure out why. I’ve tried a bunch of different things but none helped.

(For anyone interested: the project needs SODA as a dependency before it will actually run).

--SODA Designer

function setup()
    displayMode(OVERLAY)
    --SODA:
    profiler.init()
    Soda.setup()
    --/SODA
    guineaPig = Soda.Button{
        title = "OK", 
        x = 0.5, y = 0.5, w = 0.4, h = 40,
    }
    parameter.number("ButtonWidth", 0, 1, 0.25, changeWidth)
end

function changeWidth()
    if guineaPig ~= nil then
        guineaPig.w = ButtonWidth
    end
end

function draw()
    --SODA
    pushMatrix()
    Soda.camera()
    drawing()
    popMatrix()
    profiler.draw()
    --/SODA
end

function drawing(breakPoint)
    pushStyle()
    spriteMode(CORNER)
    sprite("Cargo Bot:Game Area", 0, 0, WIDTH, HEIGHT)
    popStyle()
    --SODA
    --in order for gaussian blur to work, do all your drawing here
    Soda.draw(breakPoint)
    --/SODA
end

function touched(touch)
    --SODA
   if Soda.touched(touch) then return end
    --/SODA
end


--MISC SODA STUFF
--user inputs:

function keyboard(key)
    Soda.keyboard(key)
end

function orientationChanged(ori)
    Soda.orientationChanged(ori)
end

--measure performance:

profiler={}

function profiler.init(quiet)    
    profiler.del=0
    profiler.c=0
    profiler.fps=0
    profiler.mem=0
    if not quiet then
        parameter.watch("profiler.fps")
        parameter.watch("profiler.mem")
    end
end

function profiler.draw()
    profiler.del = profiler.del +  DeltaTime
    profiler.c = profiler.c + 1
    if profiler.c==10 then
        profiler.fps=profiler.c/profiler.del
        profiler.del=0
        profiler.c=0
        profiler.mem=collectgarbage("count", 2)
    end
end

For comparison, the code below does work (mostly), but by using the extreme kludge of killing the button and remaking it each time.

I say it “mostly” works because it all works except for setting the text color, which again, is a mystery to me.

--SODA Designer

function setup()
    displayMode(OVERLAY)
    --SODA:
    profiler.init()
    Soda.setup()
    --/SODA
    guineaPig = Soda.Button{
        title = "OK", 
        x = 0.5, y = 0.5, w = 0.4, h = 40,
    }
    parameter.color("FillColor", color(126, 119, 134, 255), update)
    parameter.color("TextColor", color(126, 119, 134, 255), update)
    parameter.text("ButtonTitle", "type in any text", update)
    parameter.number("ButtonWidth", 0.0, 1.0, 0.25, update)
    parameter.number("ButtonHeight", 0.0, 1.0, 0.25, update)
    parameter.boolean("Shadow", true, update)
end

function update()
    if guineaPig ~= nil then
        guineaPig.kill = true
        guineaPig = Soda.Button{
        title = ButtonTitle, 
        x = 0.5, y = 0.5, w = ButtonWidth, h = ButtonHeight,
        shadow = Shadow}
        guineaPig.style.shape.fill = FillColor
        guineaPig.style.title.fill = TextColor
        guineaPig.style.text.fill = TextColor
    end
end

function draw()
    --SODA
    pushMatrix()
    Soda.camera()
    drawing()
    popMatrix()
    profiler.draw()
    --/SODA
end

function drawing(breakPoint)
    pushStyle()
    spriteMode(CORNER)
    sprite("Cargo Bot:Game Area", 0, 0, WIDTH, HEIGHT)
    popStyle()
    --SODA
    --in order for gaussian blur to work, do all your drawing here
    Soda.draw(breakPoint)
    --/SODA
end

function touched(touch)
    --SODA
   if Soda.touched(touch) then return end
    --/SODA
end


--MISC SODA STUFF
--user inputs:

function keyboard(key)
    Soda.keyboard(key)
end

function orientationChanged(ori)
    Soda.orientationChanged(ori)
end

--measure performance:

profiler={}

function profiler.init(quiet)    
    profiler.del=0
    profiler.c=0
    profiler.fps=0
    profiler.mem=0
    if not quiet then
        parameter.watch("profiler.fps")
        parameter.watch("profiler.mem")
    end
end

function profiler.draw()
    profiler.del = profiler.del +  DeltaTime
    profiler.c = profiler.c + 1
    if profiler.c==10 then
        profiler.fps=profiler.c/profiler.del
        profiler.del=0
        profiler.c=0
        profiler.mem=collectgarbage("count", 2)
    end
end

Something in init? Soda has special positioning, so new values have to be re-interpreted every time.