TGC Codebase Backup



Draggable 2D Sprite Buttons by RaceGT

12th Feb 2007 20:29
Summary

Draggable 2D Sprite Buttons.



Description

Draggable 2D Sprite Buttons.

*Fixed "Sprite 0" bug when mouse is clicked when NOT on a button.

*Fixed bug when mouse is moved too fast to create collision with target (current) button.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: interfaces3
Rem Created: 7/8/2006 2:21:46 AM
Rem Purpose: To create interface buttons with sprites

Rem YetToDo: Make an alternate image for the button as pressed in, that just calculates a darker color
`            for the button, as well as reversed bevel (change sprite image when pressed)
`            DONE

Rem ***** Main Source File *****
`NOW IT WORKS.

`Set Display Mode 800,600,16,1
Sync On : Sync Rate 0
Randomize Timer()
Sync : Sync
numbuttons=6
free_sprite_image=2

Dim button_sprite(numbuttons)

`Dim button_image(numbuttons)  `Not Pressed
Dim button_image_0(numbuttons) `Not Pressed
Dim button_colors#(numbuttons,4) as DWORD
Dim button_image_1(numbuttons) `Pressed

Dim button_x(numbuttons)
Dim button_y(numbuttons)
Dim sprite_button(numbuttons+10)


`Load Image "BG.png",1,1
Box 0,0,Screen Width()-1,Screen Height()-1,RGB(Rnd(255),Rnd(255),Rnd(255)),RGB(Rnd(255),Rnd(255),Rnd(255)),RGB(Rnd(255),Rnd(255),Rnd(255)),RGB(Rnd(255),Rnd(255),Rnd(255))
Get Image 1,0,0,Screen Width()-1,Screen Height()-1,1

mouse_sprite_image=FreeImage()
Box 0,0,10,10,RGB(Rnd(255),Rnd(255),Rnd(255)),RGB(Rnd(255),Rnd(255),Rnd(255)),RGB(Rnd(255),Rnd(255),Rnd(255)),RGB(Rnd(255),Rnd(255),Rnd(255))
Get image mouse_sprite_image,0,0,1,1
Sprite 1,mousex(),mousey(),mouse_sprite_image

`Lock Pixels
`Create Bitmap 1,200,200
`Set Current Bitmap 1

x=0 : y=0 : xcounter=0
temp_image = FreeImage()
Box 0,0,99,19,RGB(95,95,95),RGB(255,255,255),RGB(55,55,55),RGB(100,100,100)
Box 1,1,98,18,RGB(0,0,0),RGB(0,0,0),RGB(0,0,0),RGB(0,0,0)
Get Image temp_image,0,0,99,19,1 `outline box
outline_sprite=FreeSprite()
Sprite outline_sprite,20,20,temp_image
`Sync : Sync : wait key
Flip Sprite outline_sprite
Mirror Sprite outline_sprite

temp_image_2 = FreeImage()

For b=1 to numbuttons
   `the fancy outline.....  LL,TL,LR,TR
   Box 0,0,99,19,RGB(95,95,95),RGB(255,255,255),RGB(55,55,55),RGB(100,100,100)
   `the box fill.....
   For cc=1 to 4
      c1=RND(255)
      c2=RND(255)
      c3=RND(255)
      button_colors#(b,cc)=RGB(c1,c2,c3)
   Next cc
   Box 1,1,98,18,button_colors#(b,1),button_colors#(b,2),button_colors#(b,3),button_colors#(b,4)

   Get Image temp_image_2,0,0,99,19,1 : Rem Get the temporary fill image

   free_sprite = FreeSprite()
   button_sprite(b)=free_sprite

   free_image = FreeImage()
   button_image_0(b)=free_image

   Center Text 49,2,"Button "+ STR$(b)
   Get Image button_image_0(b),0,0,99,19,1
   `Un-Pressed Version (button up)

   free_image = FreeImage()
   button_image_1(b)=free_image : Get Image button_image_1(b),0,0,99,19,1 : rem Temporary GET IMAGE, so that image exists and is not taken

   Paste Image temp_image_2,0,0

   Center Text 50,3,"Button "+ STR$(b)
   Fade Bitmap 0,70
   Paste Sprite outline_sprite,0,0
   Get Image button_image_1(b),0,0,99,19,1

   `Set up button properties...
   `button_x(b)=Rnd(700)+20
   `button_y(b)=Rnd(500)+50
   button_x(b)=x*100
   button_y(b)=y*21  + 60
   INC xcounter : Inc x
   If xcounter>7 Then xcounter=0: x=0 : INC y

   Sprite button_sprite(b),button_x(b),button_y(b),button_image_0(b)

   sprite_button(button_sprite(b)) = b `which sprite goes to which button?
   SET SPRITE PRIORITY button_sprite(b), numbuttons-b :rem it HAS to be done in reverse order, or else the sprite layers will be wrong (for detection of overlapping sprites)
   Sync
Next b
last_button_sprite=button_sprite(b-1)+1
Delete Image temp_image :Delete Image temp_image_2 : Delete Sprite outline_sprite

`Set Current Bitmap 0
`Unlock Pixels
`Delete Bitmap 1
`BackDrop off


Do

   Paste Image 1,0,0
   fps$="FPS: "+str$(screen fps())
   text 10,40,fps$

   Sprite 1,mousex()-1,mousey()-1,mouse_sprite_image

   If mouseclick()=1
      clicked=SPRITE COLLISION(1,0)
      If (has_clicked=0)
         If (clicked<>0)
            cur_sprite = clicked
            cur_button = sprite_button(cur_sprite)
            offset_x = mousex() - Sprite X(cur_sprite)
            offset_y = mousey() - Sprite Y(cur_sprite)
            has_clicked=1
            SET SPRITE PRIORITY cur_sprite, last_button_sprite `If you try to get funny and set this to something like 10,000 {to make SURE it's always drawn on top), it will make the program act like there really are 10,000 sprites, and slow the program to a crawl, as if there really are 10,000 sprites.  'last_button_sprite' is set to 1 higher than the last sprite used for buttons, so it will be higher than any of the sprites used for buttons, and so will show on top/over the other button sprites.
         EndIF
      Else
         Sprite cur_sprite, mousex() - offset_x , mousey()-offset_y , button_image_1(cur_button)
      EndIF
      Text 10,2,"Hit with Button "+STR$(cur_button)
      Text 10,14,"offset X: "+STR$(offset_x)+"  offset Y: "+STR$(offset_y)
   Else
      If has_clicked=1
         has_clicked=0 : SET SPRITE PRIORITY cur_sprite, cur_sprite
         Sprite cur_sprite, mousex() - offset_x , mousey()-offset_y , button_image_0(cur_button)
         clicked=0
      EndIF
   EndIF

   Sync
Loop























Function FreeSprite
   repeat
      inc spritecounter
      if Sprite Exist(spritecounter)=0 then found=1
   until found
endfunction spritecounter

Function FreeImage
   repeat
      inc imagecounter
      if Image Exist(imagecounter)=0 then found=1
   until found
endfunction imagecounter