GLBasic forum

Main forum => GLBasic - en => Topic started by: Kimaro on 2010-Apr-24

Title: Point & Click
Post by: Kimaro on 2010-Apr-24
How do I create a clickable region in GLB. I understand it's something to do with MousState but can't quite get to grips with it. :S
I'm attempting to write a point & click graphic adventure and need a code snippet within an IF-THEN statement so the program performs an action when a screen area is clicked.
Would be grateful for any help on this. ;/

By the way, I think the forum is great. There a lot of very clever coders out there.
Title: Re: Point & Click
Post by: mahan on 2010-Apr-24
Something like this?

Code (glbasic) Select
SYSTEMPOINTER TRUE

WHILE 1

DRAWRECT 50, 50, 50, 50, RGB(255, 255, 0)


MOUSESTATE mx, my, leftMouse, dummy


IF leftMouse
IF mx > 50 AND mx < 50+50 //posX + width
IF my > 50 AND my < 50+50 //posy + height
PRINT "area pointed and clicked!", 200, 200
ENDIF
ENDIF
ENDIF


SHOWSCREEN
WEND


Title: Re: Point & Click
Post by: Moru on 2010-Apr-24
You can also use sprite collisions if you need different sized objects not in the shape of rectangles. You don't even need to draw the sprite, just test for collision with it.
Title: Re: Point & Click
Post by: Kimaro on 2010-Apr-24
Thanks for the tips. Will try both methods.  :)
Title: Re: Point & Click
Post by: mahan on 2010-Apr-24
Since you wanted to check for several regions I made a function that you can reuse :)

Code (glbasic) Select
SYSTEMPOINTER TRUE


WHILE 1

DRAWRECT 50, 50, 50, 50, RGB(255, 255, 0)
IF rectClicked(50, 50, 50, 50) THEN PRINT "Left rect left-clicked", 50, 120

DRAWRECT 150, 50, 50, 50, RGB(255, 0, 255)
IF rectClicked(150, 50, 50, 50, TRUE) THEN PRINT "Right rect right-clicked", 150, 120


SHOWSCREEN
WEND


// --------------------------------- //
// rectClicked()
//
// Purpose: check if a mouse button is
// pressed when the mouse is in a
// certain rectangular area.
//
// Quirks: checkMouseRight = True
//   checks the right mouse button
//   instead of the default left mb.

FUNCTION rectClicked: x%, y%, width%, height%, checkMouseRight%=FALSE

LOCAL mx, my, ml, mr
MOUSESTATE mx, my, ml, mr

IF mx >= x AND mx <= x+width AND my >= y AND my <= y+height
IF checkMouseRight
RETURN mr
ELSE
RETURN ml
ENDIF
ENDIF
ENDFUNCTION



edit: bug in code
Title: Re: Point & Click
Post by: Kimaro on 2010-Apr-24
Thanks Mahan, that's just what I needed. :good: