Author Topic: Lua Classes  (Read 14579 times)

0 Members and 1 Guest are viewing this topic.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Lua Classes
« on: July 29, 2011, 12:03:10 pm »
I tried reading the thing on inspired-lua.org, but google translate sucks, so I couldn't understand much.
Basically, what I'm asking boils down to three things: what are classes, how do you use them, and is there anything special you can do with them?
Thanks!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #1 on: July 29, 2011, 12:55:58 pm »
Well, I'll try my best to explain them.

Imagine you have a game were you can have many different cars. All the cars have different things like engine power, but still share much stuff. This is were classes come in.
With classes you can make a basic structure of an object, and create new objects based on the first one. Nspire Lua has the class() function that saves us lots of work.

Example:
Code: [Select]
car = class()

function car: init(power, type, lights, etc)
    self.power = power
    self.type = type
    self.lights = lights*2
    self.options = etc
end

function car: drive(speed)
   return self.power * speed -- this doesn't make much sense, but its just an example
end

Now, once we made our basic car class, we can define cars using this class:
Code: [Select]
volvo = car(10, "2x2", 4, {air-conditioning = True})
jeep = car(60, "4x4", 12, {air-conditioning = False, trailer-hook = True})

jeep:drive(10)       -- returns 600
volvo:drive(20)      -- returns 200

if jeep.options.trailer-hook then
   print("The jeep can have a trailer")
else
   print("No trailer for you :(")
end

This is just a basic example, and it written here without testing, so it can have some bugs.
« Last Edit: December 23, 2011, 03:04:39 pm by jimbauwens »

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Classes
« Reply #2 on: July 29, 2011, 12:59:42 pm »
Oh, I think I get it. Thanks!

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Classes
« Reply #3 on: August 17, 2011, 12:18:00 pm »
Sorry, I'm still just a bit confused.
Let's say you created a class for a button. How would you initialize it so that you could actually draw it and do stuff with it?
Or let's say that you want to be able to create something on a keypress. How could you set it up so that, theoretically, you could continue generating objects infinitely?
I kinda tried to read the inspired-lua.org explanation, but Google Translate still sucks :P
Thank you very much!
« Last Edit: August 17, 2011, 12:19:19 pm by pianoman »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #4 on: August 17, 2011, 03:18:57 pm »
Well, it really depends how you want to do it :)

First create the class and its init script. The init script for a button would set for example it label and size. Then you need to add an paint function to it, were you draw it.
Also, you should keep a table with references to all the buttons, so that you can link event to it.
Something like this:
Code: [Select]
--The table we keep all references to created buttons in
ButtonTable = {}

--Create the class
button = class()

--Create and init the button
function button:init(label, x, y, action)
    self.label = label
    self.x = x
    self.y = y
    self.action = action
    self.labelWidth = 0
    self.width = 0
    self.height = 20
    
    table.insert(ButtonTable, self)
end

--Draw the button
function button:paint(gc)
self.labelWidth = gc:getStringWidth(self.label)
self.width = self.labelWidth + 4
gc:setColorRGB(0,0,0)
gc:setFont("serif", "b", 12)

gc:drawRect(self.x, self.y, self.width, self.height)
gc:drawString(self.label, self.x+1, self.y+2, "top")
end

Well, but now you will wonder how to make it clickable and all.
Look at this full code:
Code: [Select]
--The table we keep all references to created buttons in
ButtonTable = {}

--Create the class
button = class()

--Create and init the button
function button:init(label, x, y, action)
    self.label = label
    self.x = x
    self.y = y
    self.action = action
    self.labelWidth = 0
    self.width = 0
    self.height = 20
    
    table.insert(ButtonTable, self)
end

--Draw the button
function button:paint(gc)
self.labelWidth = gc:getStringWidth(self.label)
self.width = self.labelWidth + 4
gc:setColorRGB(0,0,0)
gc:setFont("serif", "b", 12)

gc:drawRect(self.x, self.y, self.width, self.height)
gc:drawString(self.label, self.x+1, self.y+2, "top")
end

--What happens if you click the button
function button:click()
self.action()
end

--Check if clicked, and if clicked call its click method
function button:checkClick(x, y)
if y >= self.y and y <= self.y + self.height and x >= self.x and x <= self.x + self.width then
self:click()
end
end

--Check ButtonTable, and draw all the button in it
function drawButtons(gc)
for _, selectedButton in pairs(ButtonTable) do
selectedButton:paint(gc)
end
end


----------------------


--The on.create even. Here we create a sample button, and make that it will call testFunction when clicked
function on.create()
button1 = button("Test!", 100, 50, testFunction)
end


--TestFunction
function testFunction()
bclicked = true
platform.window:invalidate()
end

--The paint event
function on.paint(gc)
if bclicked then
gc:drawString("The button was clicked", 10, 10, "top")
end

drawButtons(gc)
end

--Link the click event to the buttons
function on.mouseDown(x, y)
for _, selectedButton in pairs(ButtonTable) do
selectedButton:checkClick(x, y)
end
end

This should explain most of it :)
Note that I didn't try this, so there might be some errors.

I strongly recommend you too look at the source of Adriweb's contest entry here: https://github.com/adriweb/Nspire-BreakOut .
Its a very good example of what you want to do, and should be easy to understand :)

EDIT:
Ok, tested it and fixed some bugs :) (Thanks Adriweb!)
« Last Edit: December 23, 2011, 03:03:51 pm by jimbauwens »

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Classes
« Reply #5 on: August 17, 2011, 11:20:17 pm »
I actually understood you :)
Thank you once again, jim and adriweb! :D

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #6 on: August 18, 2011, 05:33:07 am »
I'm glad I could be of help :)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Classes
« Reply #7 on: August 18, 2011, 05:51:41 am »
Jim, we really have to translate this tutorial for inspired-lua :D
« Last Edit: August 18, 2011, 05:51:51 am by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #8 on: August 18, 2011, 05:53:08 am »
Adriweb was planning to do it when he had some time.
I could do it, but I don't have much time either :p

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua Classes
« Reply #9 on: January 04, 2012, 02:21:16 pm »
Sorry to necro-post, but cant you do this instead of using classes?

Code: [Select]
car = {car1 = {TopSpeed=50,Weight=10},car2 = {TopSpeed=60,Weight=11}...}

function PrintTopSpeed
graydraw.print(car.car1.TopSpeed)
end
« Last Edit: January 04, 2012, 02:21:24 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua Classes
« Reply #10 on: January 04, 2012, 02:24:18 pm »
euh, why would you do that? you have the ease of use to play with classes..
they're really easy to handle, what's the reason you want to do it this way?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #11 on: January 04, 2012, 03:02:34 pm »
Sorry to necro-post, but cant you do this instead of using classes?

Code: [Select]
car = {car1 = {TopSpeed=50,Weight=10},car2 = {TopSpeed=60,Weight=11}...}

function PrintTopSpeed
graydraw.print(car.car1.TopSpeed)
end

You can, but for certain stuff classes is much better.
It allows you to do very nice stuff which are not possible (like, each car has its own subroutines).

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua Classes
« Reply #12 on: January 04, 2012, 03:36:11 pm »
How do you do this with LuaForWindows? (I dont think they have a class function)



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #13 on: January 04, 2012, 03:41:22 pm »
No, TI added the class() function.
However, its very easy to add that function on the pc since its a simple function :)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua Classes
« Reply #14 on: January 04, 2012, 03:42:39 pm »
i had some questions about that lua for windows

can you create a full program with it, with windows (real ones, not the OS) and stuff?