Computer Science Canada

Buttons module

Author:  evildaddy911 [ Sat Feb 25, 2012 8:18 am ]
Post subject:  Buttons module

I was having difficulties using the GUI module, so i decided to create my own.
Here is the first part, text buttons.

Enable the module by putting it in the same directory as the program; then put import buttons at the top of the program
the module contains:

buttons.draw (Text : string, Bottom_X, Bottom_Y, Font_Size : int)
draws the button; colors it gray when mouse is hovering over it

buttons.clicked (Text : string, Bottom_X, Bottom_Y, Font_Size : int) : boolean
returns a boolean indicating if the button has been clicked

Coming soon will be sliders.

Author:  Raknarg [ Sat Feb 25, 2012 9:39 am ]
Post subject:  RE:Buttons module

This is good, but I would look into classes instead. What you have at the moment is something that can be used for more than one object, but ineffectively. If you use classes, you can actually create seperate buttons with their own procedures and variables.

Author:  evildaddy911 [ Sat Feb 25, 2012 4:39 pm ]
Post subject:  RE:Buttons module

i read the classes#1 tutorial and i dont really understand it... so what your saying is you want me to change it so i can have multiple buttons' data stored to callup later, like Pic.New/Font.New and Pic.Draw/Font.Draw?

Author:  Raknarg [ Sat Feb 25, 2012 8:32 pm ]
Post subject:  RE:Buttons module

That's pretty close, actually. You can try looking at this if you like: http://compsci.ca/v3/viewtopic.php?t=30797

So there isnt much of a difference. Essentially, you're saving all those procedures you have in that module, but you're putting it in a variable called a pointer. You set this pointer like I have in my program:

var but : pointer to button
new button, but

That sets the variable but to be part of the class button. I then create it with the "new" statement.

After that, you can use the procedure's by calling the pointer. See, the cool thing about this is that although every object made from that class will be basically the same, they have different values, and so they act differently. It's incredibly useful.

Personally how I like to set up my procedures is to first have an initialize, where you set all the variables for that object. Then I have the draw and clicking procedures/functions, which use the variables I got from the initialization.

You can always pm me or a mod if you're still confused.

Author:  evildaddy911 [ Sat Feb 25, 2012 8:57 pm ]
Post subject:  Re: Buttons module

Probably could use modules still, using something like

var buttonNo: array 1..1000 of
record
Active: boolean
Text:string
Fontsize: int
end record

function New ( text:string, fontsize:int):int
for i:1..1000
if ~buttonNo(i).active then
button(i).Text:=text
button(i).Active:=true
button(i).Fontsize:=fontsize
result i
end if
end for
end New

function draw (ButtonNo,x,y:int):boolean
?
?
end draw

procedure delete(ButtonNo)
buttonNo.Active:=false
end delete

Author:  Raknarg [ Sat Feb 25, 2012 9:09 pm ]
Post subject:  RE:Buttons module

You could. However, there's a couple other reasons. First of all, classes are more organized, as you can keep everything seperate. There's also the fact that learning about them will be very useful, because there's more ways to use them then how we've just described right now.

However, you're right, you can do that if you like.


: