SpriteClick Demo: Simple Example for GameMenu etc.

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

SpriteClick Demo: Simple Example for GameMenu etc.

Post by Kaeru Gaman »

How to create a list of Gadget Elements on a Screen,
how to display and check mouse interaction.

Code: Select all

; ***
; *** 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
oh... and have a nice day.
User avatar
idle
Always Here
Always Here
Posts: 5918
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

that's really good
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

thanks! :D

... it was just meant as a little Tutorial-like Demo,
it was requested in the German forums, and I posted it here aswell.
oh... and have a nice day.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

This was so timely is spooked me. I was just going to look for something like this. Its almost like you read my mind.

Thank You very much. :)
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Nice one :D

Many thanks
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Excellent work! Much cleaner code than I have ever written! :D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

thnx, guys. glad you like it. :D
oh... and have a nice day.
Post Reply