Posted: 10th Oct 2003 1:34
Here is a method to add user interface buttons that alternately depress and raise.

+ Code Snippet
Rem Project: ButtonMaker
Rem Created: 10/9/2003 10:22:06 AM

Rem ***** Main Source File *****

Rem ***** By Codger

set window on
set window size 1024,768

sync on
sync rate 0
set text font "ariel"
set text size 18

get image 999,1,1,2,2

type Buttons
  x as integer
  y as integer
  w as integer
  h as integer
  pressed as boolean
  Label$ as string
endtype

Global MyTimer
global dim Button(300) as Buttons
Global ActiveButton
randomize Timer()

i = 0
For y = 1 to 14
   For x = 1 to 8
   inc i
      Button(i).x = x * 110
      Button(i).y = y * 33
      Button(i).w = 100
      Button(i).h = 30
      Button(i).Label$ = "Button "+str$(i)
      Button(i).Pressed = 0
      Make_Button(i)
   next x
next y

J = 0
For y = 1 to 3
   For x = 1 to 3
   inc i
   inc j
      Button(i).x = 600+(x * 33)
      Button(i).y = 550+(y * 33)
      Button(i).w = 30
      Button(i).h = 30
      Button(i).Label$ = str$(j)
      Button(i).Pressed = 0
      Make_Button(i)
   next x
next y

   inc i
   inc j
      Button(i).x = 600+(1 * 33)
      Button(i).y = 550+(4 * 33)
      Button(i).w = 63
      Button(i).h = 30
      Button(i).Label$ = str$(j)
      Button(i).Pressed = 0
      Make_Button(i)

do
   if mouseclick() > 0 then CheckButton(0)
      set cursor 100,500
      Print "Screen FPS : ",Screen FPS()
      set cursor 100,530
      Print "Active Button : ",Button(ActiveButton).Label$
   sync
loop


Function Make_button(n)
   x = Button(n).x
   Y = Button(n).y
   W = Button(n).w
   H = Button(n).h
   Label$ = Button(n).Label$
   if sprite exist(n) then delete sprite n
   create bitmap 1,w,h
   set current bitmap 1
   ink RGB(202,202,202),RGB(0,0,0)
   box 0,0,w,h
   if Button(n).Pressed = 0 then ink RGB(249,249,249),rgb(0,0,0) else ink RGB(40,40,40),rgb(0,0,0)
   line 1,1,w,1
   line 0,0,w,0
   line 1,1,1,h
   line 0,0,0,h
   if Button(n).Pressed = 0 then ink RGB(40,40,40),rgb(0,0,0)  else ink RGB(249,249,249),rgb(0,0,0)
   Line w-1,1,w-1,h
   Line w-2,2,w-2,h
   line w-1,h-1,1,h-1
   line w-2,h-2,2,h-2
   ink RGB(0,0,0),RGB(255,255,255)
   center text w/2,h/2-(text height(Label$)/2),label$
   Get image n,0,0,w,h,0
   set current bitmap 0
   delete bitmap 1
   sprite n,x,y,n
endfunction

Function CheckButton(Target)
   ActiveButton = Pick_Sprite(Target)
   if ActiveButton <> 0 and MyTimer < Timer()
      MyTimer = Timer()+ 200
      if Button(ActiveButton).Pressed = 1
         Button(ActiveButton).Pressed = 0
         Make_Button(ActiveButton)
      else
         Button(ActiveButton).Pressed = 1
         Make_Button(ActiveButton)
      endif
   EndIf
EndFunction


Function Pick_Sprite(Target)
  sprite 999,mousex(),Mousey(),999
  hit = sprite collision(999,Target)
EndFunction hit



If you use it add me in a by-line
Posted: 1st Nov 2003 1:28
if you change line: 104

from:
+ Code Snippet
Get image n,0,0,w,h,0


to:
+ Code Snippet
Get image n,0,0,w,h,1


the text on the buttons is much clearer!
Posted: 1st Nov 2003 1:39
ok, i'm meddling...

here is a revised version where the labels on the buttons adjust to make it look like the label is part of the button when you click it not just hovering above it!

also with sharper lines...
Posted: 1st Nov 2003 19:28
ORV
Thanks for the changes I can code but I have no artistic abilities or eye for asthetics.

CODGER