Author Topic: Functions in Types - Button example  (Read 5005 times)

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Functions in Types - Button example
« on: 2010-Aug-04 »
This is a codesnippet where I show how to use the functions in a type for e.g. a button (not very hard but it could be a help for beginners) :)

Code: (glbasic) [Select]
// --------------------------------- //
// Project: button_test
// Start: Wednesday, August 04, 2010
// IDE Version: 8.050
//This is a codesnippet for a button with functions within the type declaration


SYSTEMPOINTER TRUE
LIMITFPS -1

TYPE tObject
w%//width
h%//heigth
x%//coord-x
y%//coord-y
mh%//mousehover
md%//mousedown
mousefree%//if the mousebutton is free it´s true

FUNCTION init: w%,h%,x%,y%//Set the values of the object
self.w=w
self.h=h
self.x=x
self.y=y
ENDFUNCTION

FUNCTION check_mouse:mx,my//Check if the mouse is over the button
IF mx>self.x AND mx<self.x+self.w AND my>self.y AND my<self.y+self.h
self.mh=TRUE
ELSE
self.mh=FALSE
ENDIF
ENDFUNCTION

FUNCTION check_mouseDown:mx,my,b1//check if the button is clicked and change a value
check_mouse(mx,my)
IF self.mh//if the mouse is over the button
IF b1//if the mousebutton is pressed
IF self.mousefree//if the mousebutton is free
SELECT self.md
CASE TRUE
self.md=FALSE
CASE FALSE
self.md=TRUE
ENDSELECT
self.mousefree=FALSE
ENDIF
ELSE
self.mousefree=TRUE
ENDIF
ENDIF
ENDFUNCTION
ENDTYPE

GLOBAL button AS tObject//create a new button
button.init(100,50,0,0)//initialize the button values

WHILE TRUE//MAIN LOOP

IF button.md THEN DRAWRECT 100,100,100,100,RGB(0,255,0)//If the button is clicked


LOCAL mx,my,b1,b2//local mousestates
MOUSESTATE mx,my,b1,b2

button.check_mouseDown(mx,my,b1)//check if the mousebutton is pressed / clicked


SHOWSCREEN
WEND//END OF MAIN LOOP
END

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Re: Functions in Types - Button example
« Reply #1 on: 2010-Aug-05 »
this sample might be a little bit confusing. You'll have to click in the area 0, 0, 100, 50 (button.init) on the screen and the button is shown at another place ?!?

Guess if you change the call for button.init to

button.init(100,100,100,100)//initialize the button values

it will work as expected.

Secondly the button isn't shown at first. You'll have to find it in the dark :)
so if you change

IF button.md THEN DRAWRECT 100,100,100,100,RGB(0,255,0)//If the button is clicked

to

IF button.md
   DRAWRECT 100,100,100,100,RGB(0,255,0)//If the button is clicked
ELSE
   DRAWRECT 100,100,100,100,RGB(255,0,0)
ENDIF


it's shown in every case and changes the color by clicking on it

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Re: Functions in Types - Button example
« Reply #2 on: 2010-Aug-05 »
Haha, yes you´re right :)
That´s the problem... You know what you do if you write a program and you think another people understand that.

Thanks for the reply ;)

Offline phaelax

  • Mc. Print
  • *
  • Posts: 36
    • View Profile
Re: Functions in Types - Button example
« Reply #3 on: 2010-Oct-16 »
very helpful to me as I wasn't aware we could use functions inside a udt.  Makes GLB a little less BASIC and a little more OO.

Offline okee

  • Dr. Type
  • ****
  • Posts: 324
    • View Profile
Re: Functions in Types - Button example
« Reply #4 on: 2010-Dec-13 »
Yeah, makes the source a lot more readable.
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)