Each instance of a class?
Author |
Message |
Cervantes
|
Posted: Sun Feb 27, 2005 6:39 pm Post subject: Each instance of a class? |
|
|
Is there any way for Turing to go through each instance of a class and do something with it?
ex.
I want to make a button class. It looks something like this (with the procedure's code left out for easier reading)
Turing: |
unit
class BUTTON
export INIT, checkSelect, drawButton
var button :
record
x1, y1, x2, y2 : int
highlighted, selected : boolean
selectionTimer : int
selectionNum : int
boarder :
record
width, height : int
clr : int
end record
label_ :
record
text : string
font : int
x, y : int
clr : int
end record
clr :
record
current : int
main, highlight, select : int
end record
end record
proc INIT (x, y, width, height : int, boarderWidth, boarderHeight, oarderClr : int, labelText : string,
fontNum : int, labelX, labelY : int, labelClr : int, currentClr : int, mainClr, highlightClr, selectClr : int)
end INIT
procedure checkSelect
end checkSelect
proc drawButton
end drawButton
end BUTTON
|
Right. So when I want to use that in my program, I first make a bunch of instances of the BUTTON class, then I want to (inside my main loop) go through each instance of the BUTTON class and run the checkSelect procedure. Then I want to draw each button. But I don't want to have to resort to doing this:
Turing: |
import BUTTON in "location"
var quitButton : ^ BUTTON
new BUTTON, quitButton
quitButton -> INIT (parameters )
var button1 : ^ BUTTON
new BUTTON, button1
button1 -> INIT (parameters )
var button2 : ^ BUTTON
new BUTTON, button2
button2 -> INIT (parameters )
%So far, that's fine. There's no avoiding making each one and initializing each one.
loop %main loop
quitButton -> checkSelect %this is what I'm trying to avoid.
button1 -> checkSelect %doing this for every button is not cool,
button2 -> checkSelect %especially when if I've got more buttons. :'(
cls
quitButton -> drawButton
button1 -> drawButton
button2 -> drawButton
View.Update
end loop
|
Thanks in advance,
-Cervantes |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Delos
|
Posted: Sun Feb 27, 2005 6:50 pm Post subject: (No subject) |
|
|
Idea 1:
- instead of instantiating so many specific vars, why not just use an array of ints/whatever other type you need? I'm sure you can do that in a nice loop...flexible arrays could even make things easier.
Also, you might want to add procs within the class that can handle that sort of things...
Turing: |
buttonArray -> INIT_all
|
Idea 2:
If you want to keep your specifically named vars...
Each time a var is instantiated (in your unavoidable clause), add a reference/pointer/other such Post-It to an array within the class that can then be called by another procedure to do things...
Turing: |
var but1 : ^ BUTTON
but1 -> INIT (params )
% Meanwhile...inside the class!
proc INIT (prams )
new buttonCheckList, upper(buttonCheckList ) + 1
% Add info to this new entry...drawing mainly from params for specificity
%...
end INIT
|
Could work... |
|
|
|
|
|
Tony
|
Posted: Sun Feb 27, 2005 9:32 pm Post subject: (No subject) |
|
|
sorry, no time to read though that. You can dig though GUI class source to see what they are doing there. I'm pretty sure that each new instance (class.initialize) adds a pointer to self to a global, flexable array.
GUI.processEvents when loops though that array, processing each instance.
Just have to remember to remove the pointer upon disassembly of the instance |
|
|
|
|
|
wtd
|
Posted: Sun Feb 27, 2005 11:52 pm Post subject: (No subject) |
|
|
Why can't you have:
code: | var foo : array something...something of ^ BUTTON |
|
|
|
|
|
|
Cervantes
|
Posted: Mon Feb 28, 2005 10:49 am Post subject: (No subject) |
|
|
Given the somewhat limited time I've got, I went straight to wtd's suggestion, as it seemed the simplest.
Turing: |
var buttons : array 1 .. 2 of ^BUTTON
for i : 1 .. upper (buttons )
new BUTTON, buttons (i )
end for
buttons (1) -> INIT (params )
buttons (2) -> INIT (params )
loop
for i : 1 .. upper (buttons )
buttons (i ) -> checkButtons
end for
cls
for i : 1 .. upper (buttons )
buttons (i ) -> drawButtons
end for
View.Update
delay (10)
end loop
|
Nice. But, I'm now confused about how I'm going to make the button do something, when you click on it. Obviously, the code has to be in the program, not the class. But I don't want to do it like Turing's GUI has done it, as that's annoyed me to no end. So far as I know, Turing's GUI won't let you do anything with functions, or with procedures with parameters.
Any ideas?
-Cervantes
P.S. Delos, I don't understand how INIT_all would be any better, because you're way I'd have to initiate the array. This way, I have to initiate the button using a pile of parameters. I don't know, I don't think it's avoidable. |
|
|
|
|
|
wtd
|
Posted: Mon Feb 28, 2005 1:12 pm Post subject: (No subject) |
|
|
Use a real programming language. |
|
|
|
|
|
Cervantes
|
Posted: Mon Feb 28, 2005 7:02 pm Post subject: (No subject) |
|
|
wtd wrote: Use a real programming language.
Out of context, Cervantes wrote:
I don't know, I don't think it's avoidable.
After three full semesters of no computer science class, I'm back in the class. And we learn Turing. *cough*
My thinking right now is I'm going to create a function of the button that will return whether the button is clicked or not. In the main loop of the program that uses the buttons, I'll case through all the buttons and insert procedures if whichever button is true. It means I'll have to go through all the buttons, but at least it's only the second time, and at least it's the last time.
-Cervantes |
|
|
|
|
|
|
|