Author Topic: Add function to the gc class  (Read 23442 times)

0 Members and 1 Guest are viewing this topic.

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Add function to the gc class
« on: September 02, 2011, 11:25:19 am »
Hi everyone! I've been following this forum for quite some time now, but never actually posted something. I guess this is the first time: I've got a question...  :)

I'm trying to add methods and properties to the default gc class. I know that in "normal" lua you can just do something like this: function table.method(). So I tried the following:

Code: [Select]
function gc:method(x, y, width, height)
self:drawRect(x, y, width, height)
end

This didn't work though. I also tried to create a new class called screen that inherits from gc, but that didn't work either. Finally I tried "screen = gc:begin()", but that was no solution as well.

So my question is: how can I add my own method to the default gc class? Thanks! :)

Offline Loulou 54

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 165
  • Rating: +39/-0
    • View Profile
    • Mes programmes sur TI bank.
Re: Add function to the gc class
« Reply #1 on: September 02, 2011, 11:42:03 am »
I've seen 3 solutions :
* You can put gc as an argument :

Code: [Select]
function on.paint(gc)
method(gc)
end

function method(gc)
[...]
end

* You can also use platform.gc() instead of gc. For example : platform.gc():drawString(...
But I've had some problem with this method.. Some functions as setFont if I remember don't work well..

* Or you can copy gc in a global variable at the beginning of the on.paint function.

Code: [Select]
function on.paint(gc)
gc2=gc
end

function method()
gc2:drawString(...
end

Adriweb do this in the last version of better Lua.

:)
Some of my program available here.. :)
http://ti.bank.free.fr/index.php?mod=archives&ac=voir2&id=1471

     

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Add function to the gc class
« Reply #2 on: September 02, 2011, 11:46:35 am »
Thanks for your answer! That's not exactly what I mean though. I'm trying to add a new method to the gc, so that I can (for example) call gc:drawButton(). In other languages I would extend/subclass the gc class, but I'm not sure how to do that in TI-Lua. At first I thought I should do newGC = class(gc), but that didn't work, just like defining the function: function gc:drawButton(). How can I do this?

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Add function to the gc class
« Reply #3 on: September 02, 2011, 11:54:26 am »
I think you could achieve the same effect by doing as Loulou showed, where you basically create a whole separate function that uses the gc variable. Such as:

Code: [Select]
function on.paint(gc)
    method(gc) --is called in on.paint and is passed the gc var
end

function method(gc)
    gc:drawRect(...) --note the gc:
end

you could perhaps try calling the function
Code: [Select]
gc:method(gc) --pass the gc variable
    --Do stuff here

Though I don't know if that would work. :/
« Last Edit: September 02, 2011, 11:54:49 am by HOMER-16 »

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Add function to the gc class
« Reply #4 on: September 02, 2011, 12:00:09 pm »
I know a separate function is possible, but I think it's way cleaner to make it part of the actual gc, so that the drawButton() method sits prettily next to the drawRect() method. It sounds way more logical to me.  ;)

The latter is exactly what I'm trying to achieve, but that's unfortunately not possible using your code...

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Add function to the gc class
« Reply #5 on: September 02, 2011, 12:21:00 pm »
Code: (Lua) [Select]
gc = getmetatable(platform.gc())

function gc:test()
self:setColorRGB(0,0,0)
self:fillRect(10,10,10,10)
end


function on.paint(gc)
gc:test()
end

That should do the trick :)

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Add function to the gc class
« Reply #6 on: September 02, 2011, 12:26:06 pm »
Good ideas in this topic !

However, I should point out that using platform.gc() is discouraged, and is generally a bad idea.
(multiple reasons like the fact that it's not working the exact same way as the 'normal' gc, and for future compatibility matters... ;)  )
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Add function to the gc class
« Reply #7 on: September 02, 2011, 12:30:06 pm »
Thanks a lot, that's works like a charm! I need to improve my understanding of metatables though...

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Add function to the gc class
« Reply #8 on: September 02, 2011, 12:55:29 pm »
Would this work?

Code: [Select]
function on.paint(gc)
gc2 = getmetatable(gc)
function gc2:method(x, y, height, width)
self:setColorRGB(0,0,0)
self:fillRect(x, y, height, width)
end
--more methods to define here
function on.paint(gc) --Your normal on.paint(gc) goes in here
gc:method(10,10,10,10)
end
end

It would use the normal gc to define all the methods the first time on.paint is called then when it is done it would redefine itself.
« Last Edit: September 02, 2011, 12:55:56 pm by 3rik »
Userbars

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Add function to the gc class
« Reply #9 on: September 02, 2011, 12:56:55 pm »
Good ideas in this topic !

However, I should point out that using platform.gc() is discouraged, and is generally a bad idea.
(multiple reasons like the fact that it's not working the exact same way as the 'normal' gc, and for future compatibility matters... ;)  )

Somehow I overlooked your post... Anyways, isn't it true that the usage of platform.gc() in jimbauwens' example is no bad practice, since getmetatables is just a reference to the original metatable shared by all gc objects? I mean, you do use the passed gc in the on.paint() function, so that shouldn't give unexpected results?

Don't you agree that this way looks like a better method for a GUI library?
« Last Edit: September 02, 2011, 12:57:08 pm by Frog »

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Add function to the gc class
« Reply #10 on: September 02, 2011, 01:12:10 pm »
Good ideas in this topic !

However, I should point out that using platform.gc() is discouraged, and is generally a bad idea.
(multiple reasons like the fact that it's not working the exact same way as the 'normal' gc, and for future compatibility matters... ;)  )

Somehow I overlooked your post... Anyways, isn't it true that the usage of platform.gc() in jimbauwens' example is no bad practice, since getmetatables is just a reference to the original metatable shared by all gc objects? I mean, you do use the passed gc in the on.paint() function, so that shouldn't give unexpected results?

Don't you agree that this way looks like a better method for a GUI library?

platform.gc() is a constructor of a new independant gc object.

What does the TINspire framework do :

Code: [Select]
local gc=platform.gc()
gc:begin()
on.paint(gc)
gc:finish()

What does that mean ? On each screen refresh, it recreate an independant gc object that has a different reference.

And since it is a different object, using methods such as setFont/setColor won't change gc, but your new object you define each time you use platform.gc.

If you want to store this gc in on.paint(), it won't be drawn as it, since it has a different reference/address

In Jim method, you define, yes, one time a new object, but you pick up its meta table. It is not the same. Each gc object has the same metatable, but not the same properties.

Would this work?

It would use the normal gc to define all the methods the first time on.paint is called then when it is done it would redefine itself.
Ouch :p
Just use a boolean, like if not gc2 then ..... end in your on.paint()

but anyway, look at the fisrt part of my post, you can't do so :p
« Last Edit: September 02, 2011, 01:15:26 pm 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 Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Add function to the gc class
« Reply #11 on: September 02, 2011, 01:15:28 pm »
Good ideas in this topic !

However, I should point out that using platform.gc() is discouraged, and is generally a bad idea.
(multiple reasons like the fact that it's not working the exact same way as the 'normal' gc, and for future compatibility matters... ;)  )

Somehow I overlooked your post... Anyways, isn't it true that the usage of platform.gc() in jimbauwens' example is no bad practice, since getmetatables is just a reference to the original metatable shared by all gc objects? I mean, you do use the passed gc in the on.paint() function, so that shouldn't give unexpected results?

Don't you agree that this way looks like a better method for a GUI library?

platform.gc() is a constructor of a new independant gc object.

What does the TINspire framework is :

Code: [Select]
local gc=platform.gc()
gc:begin()
on.paint(gc)
gc:finish()

What does that mean ? On each screen refresh, it recreate an independant gc object that has a different reference.

And since it is a different object, using methods such as setFont/setColor won't change gc, but your new object you define each time you use platform.gc.

If you want to store this gc in on.paint(), it won't be drawn as it, since it has a different reference/address

In Jim method, you define, yes, one time a new object, but you pick up its meta table. It is not the same. Each gc object has the same metatable, but not the same properties.

Yes, I understand. The metatable is basically the blueprint and obviously every instance has different properties. I'm using to Jim's method to create a simple graphics library now. Should I release the code when I'm finished?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Add function to the gc class
« Reply #12 on: September 02, 2011, 01:32:12 pm »
I'm glad I could be of help :)
You don't need to release your source, but it would be nice :D

Offline Frog

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +0/-0
    • View Profile
Re: Add function to the gc class
« Reply #13 on: September 02, 2011, 01:41:58 pm »
If I think it's worth it, I'll release it. ;)

Now that I'm busy I've actually got one more question...  ::) Can you get the currently set color property from the gc? I've tried showing the table on the screen to show all properties, but that didn't work. Maybe someone knows what property the set RGB color is stored in, so I can calculate the opposite color for my fillButton function?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Add function to the gc class
« Reply #14 on: September 02, 2011, 01:44:22 pm »
I don't think that that is possible.
What I would do is create a new gc:setColorRGB (with another name) and store the current color information to a table.
However, the color can be changed when you draw an image, so you need to watch out with that.