Author Topic: Taking my hand at Lua  (Read 20888 times)

0 Members and 1 Guest are viewing this topic.

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Taking my hand at Lua
« 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
« Last Edit: June 08, 2012, 09:48:04 am by hellninjas »

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Taking my hand at Lua
« Reply #1 on: June 08, 2012, 10:03:27 am »
one quick question, what os version are you using?
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Taking my hand at Lua
« Reply #2 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.

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Taking my hand at Lua
« Reply #3 on: June 08, 2012, 10:08:17 am »
that would do it, i had just tried it and didnt get any errors
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Taking my hand at Lua
« Reply #4 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?

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Taking my hand at Lua
« Reply #5 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?
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Taking my hand at Lua
« Reply #6 on: June 08, 2012, 10:12:31 am »
My own hopefully, but the toolpallete would also be nice to learn :D

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Taking my hand at Lua
« Reply #7 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
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Taking my hand at Lua
« Reply #8 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

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Taking my hand at Lua
« Reply #9 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

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Taking my hand at Lua
« Reply #10 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 ;) )

Offline imo_inx

  • Manman, SaviourOfTheMultiverse!
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 473
  • Rating: +27/-8
  • imo_inx
    • View Profile
Re: Taking my hand at Lua
« Reply #11 on: June 08, 2012, 12:47:27 pm »
Im currently learning LUA too! But for computers.


Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Taking my hand at Lua
« Reply #12 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

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Taking my hand at Lua
« Reply #13 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.
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Taking my hand at Lua
« Reply #14 on: June 08, 2012, 02:10:50 pm »
that's nice jwalker, but i think it's a bit difficult, he just started...