Computer Science Canada

Simple Button Class

Author:  Degensquared [ Mon Dec 17, 2007 5:28 pm ]
Post subject:  Simple Button Class

I have just learned how to use classes and objects, so I decided to use my new found knowledge to save myself some time in the future Razz.

This simple button class allows you to create button objects quickly, and check if they are pressed with a few easy commands.

Command List

button -> initialize: initializes the position, color and toggle depression animation.

button -> newlabel: allows you to put text onto the button. (for now only one font, in one size, but that will change. Razz)

button -> clicked: checks if the button was clicked this frame and returns if it was clicked last frame.

button -> draw: draws the button, the depression animation, and the label for each button


Enjoy! Smile

Author:  SNIPERDUDE [ Thu Dec 20, 2007 6:45 pm ]
Post subject:  RE:Simple Button Class

wow, Button/GUI classes seem to be growing popular in subject.

Good work! Very Happy

Author:  Degensquared [ Thu Dec 20, 2007 7:11 pm ]
Post subject:  RE:Simple Button Class

Thanks Very Happy I'm planning on adding custom sizes and fonts for the labels Smile

Author:  Lawlly [ Thu Dec 20, 2007 7:15 pm ]
Post subject:  Re: Simple Button Class

It looks useful to me but I don't understand how I can use it.

I'm thinking of creating 3 buttons for a game I'm making. When you click it it selects a map. How can I use this for that? Of course if I were to use it, I'd note it as borrowed code.

Author:  Degensquared [ Thu Dec 20, 2007 9:19 pm ]
Post subject:  Re: Simple Button Class

Well, if you look at the example program, it should be kind of clear, but if not here you go:

You would need to set up 3 variables, each a pointer to the button class.
and the variables to check if the buttons have been clicked

create the objects using the new command,

initialize each of them.

check if they were clicked and are now not clicked

draw the buttons.


code:

var button1,button2,button3 : ^button
var lastclicked, clicked : boolean

new button1
new button2
new button3

button1 -> initialize (x1,y1,x2,y2, do you want depression animation, color of the button)
...
button3 ...

button1 -> clicked (clicked, lastclicked)

if lastclicked and not clicked then
     do whatever
end if

...
button3 ...

button1 -> draw
...
button3 -> draw



Loop that (make sure you use offscreenonly and View.Update!)
and it should work Very Happy

Author:  SNIPERDUDE [ Sun Dec 23, 2007 9:47 pm ]
Post subject:  Re: Simple Button Class

do note that if you made your programme so the user or programmer only had to call one line, it would make it easier.
for example:

code:

loop
    drawButton (any inside parameters such as X, Y, colour, text, procedure to be executed when pressed)
    View.Update
    exit when hasch
end loop

Author:  Degensquared [ Thu Dec 27, 2007 11:04 pm ]
Post subject:  RE:Simple Button Class

hmm... well I personally like to split my programs into, input, calculations, and output. so that's why I split the draw and clicked commands, but I do think I could have labels included in the initialization.


: