Show text with button push

Ok, very new to codea and to graphical programming (all procedural data manipulation in spreadsheets for years now).

I am trying to simply get a string of text to show up on the screen when I push a parameter button… Nothing shows up. I don’t want it drawn when I start the app, only when the button is hit, I have tried to look at included examples but can’t figure it out from those either.

I didn’t find much help online for codea (google searches) except for these forums, so I am coming back here directly again.

Any help would be greatly appreciated.

Jason

Example code:

function setup()
    parameter.action("show the text",showText)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    
end

function showText()
    text("test",HEIGHT/2,WIDTH/2)
end

Here is a possible answer


function setup()
    parameter.action("show the text",enable)
end
function enable()
    enableShowText = not enableShowText
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    if enableShowText then showText() end
end

function showText()
    fontSize(40)
    text("test",HEIGHT/2,WIDTH/2)
end

@JCKent Here’s another version. Type some text in the input area. Then press the ENTER button.


function setup()
    parameter.clear()
    parameter.text("input",enable)
    parameter.action("ENTER",function() str=input setup() end)
end

function draw()
    background(0, 0, 0, 255)
    if str~=nil then
        text(str,WIDTH/2,HEIGHT/2)
    end
end

I was assuming the text string gets added to the canvas when you call the text command from any location in the program, but it looks like where the documentation says Text(string, x, y) “Draws text at the given x, y location.”, it won’t actually draw anything to the canvas unless it is done either -in- the draw routine or from a function/subroutine called -from within- the draw routine?

Am I correct in that analysis of your solutions?

Des that mean that codea basically has no retention of what you have placed on the canvas? I.e. You have to call the text command each time codea refreshes the screen (~60/sec)?

So my test text might have shown up but for only 1/60th of a second? (Since it was called once, and not within the draw routine?)

Thanks again
Jason

It will actually draw, it’s just that with the draw function running as fast as possible, the text draws on only one frame before it’s erased again by background(). If you removed the background() call it would work, but then anything drawn on the screen would stay there. (If you want to remove the background() function, it’s also best to call backingMode(RETAINED) in setup().)

@JCKent codea draw the screen 60 times per second, by following the instructions defined in draw(). Everything is redrawn each time. That system is fairly rapid, and well suited for real time games. What you define outside the draw function is never displayed: so in your initial code, nothing was ever displayed on the screen.
The retained mode is usually used only for drawing apps.

@Jmv38 I have to disagree with you. The first post displays the word “test” when the “show the text” button is pressed. Comment out the background line of code, turn the iPad in landscape, and press the “show the text” button.

@Jmv38 Anything drawn outside of draw() IS drawn, it’s just overwritten the next frame. As @dave1707 and I said, removing background() stops it from overwriting the text. backingMode(RETAINED) is usually used without background().

Actually, you don’t even need draw(). Try this program. Just tap the screen.


function touched(t)
    text("screen touched",WIDTH/2,HEIGHT/2)   
end    

@Dave1707 and @SkyTheCoder thanks a lot for this info: i was completely unaware of that!

Interesting, I had to remove the draw() function altogether ala dave1707s example, then the text showed all the time, flickering, but it showed. If I just commented out the background line. (actually commented out everything internal to the draw function, an MT draw function) it never showed.

Adding the backingMode(RETAINED) to the setup() then removed the flicker. Which is exactly what I wanted to see in the end… Now on to the hard part… Writing my overall program.

Thanks a lot for all that info. Just barely getting the concepts through my old habit mindset.

Jason

@JCKent codea has 3 internal screen buffers that it displays in turn. So with your 1 call to text you write in 1 buffer only, hence the flicker. I assume when we use RETAINED, the text is written in the 3 buffers (instead of 1), so no flicker.

Thanks everyone, I didn’t think I would get this much info for what I thought was a dumb question. Great answers and I got what I wanted, I will definitely be back with more later I am sure.

Thanks
Jason

@JCKent I’m not sure what you’re trying to do, but it might be like what I ran into when I first started to use Codea. I wanted to convert a program I wrote in “C” to Codea. I wasn’t using any graphics, but all text. I was having problems at first until I used RETAINED mode, but once I understood how Codea did the display (text) in draw(), it was easy to display the information. I don’t use RETAINED mode for anything anymore.

Actually I am using codea right now to test simple logic of some routines that I later convert into vba. It gives me an opportunity to keep working when waiting on meetings to get started (someone is always late). But most of what I do is data manipulation. So basic tables works just like dictionaries in vba.

I need to display static results to to the screen, and the debug print area just wasn’t working well enough. Thus I ran into this little problem in the OP. Which generated far more responses than I expected. Thanks to everyone learning a little something.

As I learn more about lua, I will look more I into other types of apps, but with absolutely no skill in graphics, I am really limited. So I am basically stuck doing non graphical stuff for now due to my personal restrictions and experience.

Thanks
Jason

@JCKent If all you’re doing is displaying text to the screen, then maybe this will give you an idea. Use your finger to scroll the text up or down the screen. You can put your information into the table. Like I said earlier, I’m not sure about the information or it size that you’re displaying, so this could be modified.


displayMode(FULLSCREEN)

function setup()
    textMode(CORNER)
    yy=0
    tab={}
    for z=1,200 do   -- fill with test data
        table.insert(tab,"table entry "..z)
    end
end

function draw()
    background(40, 40, 50)
    fill(255)
    for a,b in pairs(tab) do
        text(b,100,HEIGHT-a*30+yy)
    end
end

function touched(t)
    yy=yy+t.deltaY
    if yy<0 then
        yy=0
    end
end

Awesome Dave, thanks, I will be using that so some extent. And playing with it to learn how codea and lua work.

Thanks
Jason