Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: hellninjas on June 08, 2012, 09:47:12 am

Title: Taking my hand at Lua
Post by: hellninjas on June 08, 2012, 09:47:12 am
I started Lua :D and while going through a tutorial I ran into an error in my code..
Code: [Select]

    answer = "" --This is the error, i'm getting "unexpected symbol near 'char(226)" D:
    

    function on.charIn(char)

        answer = answer..char

        var.store("line3",answer)

    -- Refresh the screen after each key is pressed.

        platform.window:invalidate()

    end

    function on.backspaceKey()

        answer = answer:usub(0,-2)

        var.store("line3",answer)

        platform.window:invalidate()

    end
Also, how would I code in a menu that the user could select stuff by pushing the normal arrow keys?
EDIT: I am using ocLua right now, maybe I'll get notepad++ later :P
Title: Re: Taking my hand at Lua
Post by: jwalker on June 08, 2012, 10:03:27 am
one quick question, what os version are you using?
Title: Re: Taking my hand at Lua
Post by: hellninjas on June 08, 2012, 10:04:33 am
Already been solved, it was OS 3.1 CX, but the error was that i had (-) instead of a minus sign.
Title: Re: Taking my hand at Lua
Post by: jwalker on June 08, 2012, 10:08:17 am
that would do it, i had just tried it and didnt get any errors
Title: Re: Taking my hand at Lua
Post by: hellninjas on June 08, 2012, 10:09:18 am
Awesome, now for my other question, how would I code in a menu that the user could select stuff by pushing the normal arrow keys?
Title: Re: Taking my hand at Lua
Post by: jwalker on June 08, 2012, 10:11:28 am
do you mean like create, one of the toolpallete menus that are in the calculator app, or create your own from scratch?
Title: Re: Taking my hand at Lua
Post by: hellninjas on June 08, 2012, 10:12:31 am
My own hopefully, but the toolpallete would also be nice to learn :D
Title: Re: Taking my hand at Lua
Post by: jwalker on June 08, 2012, 10:14:51 am
I could probably make a very simple example and post it, also go to inspired-lua.org for the toolpallete question, even though it has been updated for 3.2, not alot has changed when it comes to creating the toolpallete
Title: Re: Taking my hand at Lua
Post by: Jim Bauwens on June 08, 2012, 10:14:56 am
The events [lua]on.arrowUp[/lua] and [lua]on.arrowDown[/lua] will be called when pressing up/down.
In this function you will need increment/decrement an variable that contains a number representing the menu item that is selected.
After setting this variable you need to run [lua]platform.window:invalidate()[/lua]. This will trigger the display to be invalidate, so [lua]on.paint[/lua] will run.
In on.paint you need to draw your menu stuff.

This sounds probably too complex for a beginner :P
Title: Re: Taking my hand at Lua
Post by: hellninjas on June 08, 2012, 10:16:54 am
not completely Jim but im thinking I would make "function on.arrowUp" then the rest of the code and then some sort of while statement showing while the position equals a certain amount, it will display a square in that position.. or something like that :P
Title: Re: Taking my hand at Lua
Post by: Nick on June 08, 2012, 11:38:55 am
just remember that when you want to update the screen you'l exit the while loop..

you also can't keep track of the arrowkeys while drawing.
you have to use a variable to remember the last item selected, and draw again when an arrow is pressed

(this is somehow the same as jim said, but i thought you didn't caught it well because of that while statement ;) )
Title: Re: Taking my hand at Lua
Post by: imo_inx on June 08, 2012, 12:47:27 pm
Im currently learning LUA too! But for computers.
Title: Re: Taking my hand at Lua
Post by: Nick on June 08, 2012, 01:03:42 pm
well, the nspire lua is quite a lot different than the pc lua, but it's nice :) you should come to our side too xp
Title: Re: Taking my hand at Lua
Post by: jwalker on June 08, 2012, 01:58:40 pm
So here is a very very very basic type of menu. It has no sub-menus because I didnt have the time to do it today.
This should be written as a class, but since you probably dont know how to make classes, I didnt write it that way to make it less confusing.
Code: [Select]
menu = {{text = "small", selected = true, id = 1},
        {text = "larger", selected = false, id = 2},
        {text = "even larger",  selected = false, id = 3},
        {text = "sml", selected = false, id = 4}}--our menu
       
menuDrawn = false--menu flag

function on.paint(gc)
    gc:setFont("serif", "r",7)
    if menuDrawn then
        --dynamicly find the width
        local width = 0
        local strHld = 0
        for i = 1, table.maxn(menu) do
            strHld = gc:getStringWidth(menu[i].text)
            if strHld > width then
                width = strHld + 1
            end
        end
       
        strHld = gc:getStringHeight("Hh") --find the strings height
        local height = strHld * table.maxn(menu)--find the total height
        --fill grey rectangle
        gc:setColorRGB(175, 175, 175)
        gc:fillRect(0, 0, width, height)
        --draw black rectangle
        gc:setColorRGB(0, 0, 0)
        gc:drawRect(0, 0, width, height)
        --draw the strings
        local y = 5
        for i = 1, table.maxn(menu) do
            if menu[i].selected then --reverse video
                gc:fillRect(0, y - 6, width, strHld)
                gc:setColorRGB(255, 255, 255)
                gc:drawString(menu[i].text, 1, y, "middle")
                gc:setColorRGB(0, 0, 0)
            else
                gc:drawString(menu[i].text, 1, y, "middle")
            end
            gc:drawLine(0, y + 7, width, y + 7)
            y = y + strHld
        end
    end
end

function on.contextMenu() --pressed ctrl+menu
    menuDrawn = not(menuDrawn)
end
function on.arrowUp() --up arrow was pressed
    local fnd = findCurSelection()
    if menu[fnd].id ~= 1 then
        menu[fnd - 1].selected = true
    else
        menu[table.maxn(menu)].selected = true
    end
    menu[fnd].selected = false
    platform.window:invalidate()
end
function on.arrowDown() --down arrow was pressed
    local fnd = findCurSelection()
    if menu[fnd].id ~= table.maxn(menu) then
        menu[fnd + 1].selected = true
    else
        menu[1].selected = true
    end
    menu[fnd].selected = false
    platform.window:invalidate()
end
function on.enterKey() --enter key was pressed
    menu[findCurSelection()].text = "You pressed Enter"
    platform.window:invalidate()
end
function findCurSelection() --find the current selected item
    for i = 1, table.maxn(menu) do
        if menu[i].selected then
            return i
        end
    end
end

since this is a very basic outline, you could do alot with it.
Title: Re: Taking my hand at Lua
Post by: Nick on June 08, 2012, 02:10:50 pm
that's nice jwalker, but i think it's a bit difficult, he just started...
Title: Re: Taking my hand at Lua
Post by: jwalker on June 08, 2012, 02:16:04 pm
It is, but he can use it as a reference once he learns more about how to program.
Title: Re: Taking my hand at Lua
Post by: cyanophycean314 on June 08, 2012, 02:57:26 pm
This is basically the menu I used in my first few games. You can change it to to be more flexible, and use objects (I don't think you know that yet.)

Code: [Select]
--Menu

function on.paint(gc)
if not init then
x = 25
y = 65
val = 1
end
gc:drawString("Option 1",40,60,"top")
gc:drawString("Option 2",40,75,"top")
gc:drawString("Option 3",40,90,"top")
gc:drawString("Option 4",40,105,"top")
gc:fillRect(x,y,10,10)
end

function on.arrowKey(dir)
--Moves the cursor
if dir == "up" then
y = y - 15
val = val - 1
elseif dir == "down" then
y = y + 15
val = val + 1
end
--Wrap around
if val == 0 then
y = 110
val = 4
elseif val == 5 then
y = 65
val = 1
end
platform.window:invalidate()
end

function on.enterKey()
if val == "whatever you want" then
"DO THIS"
elseif val == "this option" then
"DO THIS"
end
platform.window:invalidate()
end