Code
; ***
; *** Click on Sprite Demo
; ***
; *** Kaeru Gaman, Dec.10 2008
; ***
; *** PB Ver. 4.20
; ***
EnableExplicit
; ****************************************
; *** Check if Point is inside Box
Procedure PointColl( x1.l, y1.l, x2.l, y2.l, a2.l, b2.l )
Protected Coll = #False
If x1 >= x2 And x1 <= x2 + a2 And y1 >= y2 And y1 <= y2 + b2
Coll = #True
EndIf
ProcedureReturn Coll
EndProcedure
#No_of_Elements = 19
#TextCol = $FFE0C0
Structure GadgetElement
SprNr.l
X.l
Y.l
W.l
H.l
C1.l
C2.l
T.s
EndStructure
Define ColR.l, ColG.l, ColB.l ; color channels
Define n.l ; Loopcounter
Define MX.l ; Mouse X
Define MY.l ; Mouse Y
Define MBL.l ; Mouse Button Left
Define MBR.l ; Mouse Button Right
Define MOver.l ; No of Gadget under the Mouse
Define EXIT.l ; Exit Flag
Dim Gadget.GadgetElement ( #No_of_Elements )
InitSprite()
InitKeyboard()
InitMouse()
OpenScreen( 1024, 768, 32, "Click Sprite Demo" )
; ****************************************
; *** Create a number of random elements
For n=1 To #No_of_Elements
Gadget( n )\W = Random( 128 ) + 64 ; Width
Gadget( n )\H = Random( 64 ) + 28 ; Height
Gadget( n )\X = Random( 1023 - Gadget( n )\W ) ; X-Pos
Gadget( n )\Y = Random( 767 - Gadget( n )\H ) ; Y-Pos
ColR = Random( 95 ) + 32 ; Red of Darker Color
ColG = Random( 95 ) + 32 ; Green of Darker Color
ColB = Random( 95 ) + 32 ; Blue of Darker Color
Gadget( n )\C1 = RGB( ColR , ColG , ColB ) ; Darker Color
Gadget( n )\C2 = RGB( ColR * 2, ColG * 2, ColB * 2 ) ; Lighter Color
Gadget( n )\T = "No. " + Str(n) ; simple text
; create a sprite for this gadget
Gadget( n )\SprNr = CreateSprite( #PB_Any, Gadget( n )\W, Gadget( n )\H )
StartDrawing( SpriteOutput( Gadget( n )\SprNr ) )
DrawingMode( #PB_2DDrawing_Transparent )
Box( 0, 0, Gadget( n )\W , Gadget( n )\H , Gadget( n )\C2 )
Box( 4, 4, Gadget( n )\W-8, Gadget( n )\H-8, Gadget( n )\C1 )
DrawText( 6,6, Gadget( n )\T, Gadget( n )\C2 )
StopDrawing()
Next
CreateSprite( 0, 32, 32 )
StartDrawing( SpriteOutput( 0 ) )
Line( 0, 0, 24, 8, $00FFFF )
Line( 0, 0, 8, 24, $00FFFF )
Line( 0, 0, 32, 32, $00FFFF )
StopDrawing()
Repeat
ExamineKeyboard()
ExamineMouse()
MX = MouseX()
MY = MouseY()
MBL = MouseButton( #PB_MouseButton_Left )
MBR = MouseButton( #PB_MouseButton_Right )
If KeyboardPushed( #PB_Key_Escape )
EXIT = 1
EndIf
ClearScreen( $201008 )
MOver = 0
For n=1 To #No_of_Elements
DisplaySprite( Gadget( n )\SprNr, Gadget( n )\X, Gadget( n )\Y )
If PointColl( MX, MY, Gadget( n )\X, Gadget( n )\Y, Gadget( n )\W, Gadget( n )\H )
MOver = n
EndIf
Next
StartDrawing( ScreenOutput() )
DrawingMode( #PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent )
If MOver > 0
If MBL
Box( Gadget( MOver )\X, Gadget( MOver )\Y, Gadget( MOver )\W, Gadget( MOver )\H, $00FFFF )
DrawText( 1, 1, "Clicked On: " + Str( MOver ), #TextCol )
Else
Box( Gadget( MOver )\X, Gadget( MOver )\Y, Gadget( MOver )\W, Gadget( MOver )\H, $0000FF )
DrawText( 1, 1, "Mouse Over: " + Str( MOver ), #TextCol )
EndIf
EndIf
StopDrawing()
DisplayTransparentSprite( 0, MX, MY )
FlipBuffers( #PB_Screen_SmartSynchronization )
Until EXIT = 1
End