Code archives/Miscellaneous/Button3D

This code has been declared by its author to be Public Domain code.

Download source code

Button3D by Fernhout2006
This is a include code to make a 3D look button. There is no need for a library or another texture.
This code only work in a BB windowed enviroment.
You give in your size and colors you need.
This file need SetColors.bb include file.
;******************************************************************************
;* Button3D. Version 1.00                                                     *
;* Created: march 2006                                                        *
;* Created by: Bart Fernhout.												  *
;******************************************************************************
;* Description                                                                *
;* Create a button in a 3D look.  Its drawing on the backbuffer screen.       *
;* Then it wil be fliped to the front. The 3D colors is given during the call *
;* to this function. The function gives a true back if the button is released *
;* color 2 for button pressed is given in the input. Button down is color     *
;* 2 and 3 swaped.   Call of the function is a follow:                        *
;* Button3D (Xpos_LeftUp,Ypos_LeftUP,WithButton,HeihtButton,Color1,Color2, _  *
;*           Color3,Color4,Color5,Text)                                       *
;* Color 1 = button color                                                     *      
;* Color 2 = Left And Up side of button                                       *
;* Color 3 = Right and down side color of the button                          *
;* Color 4 = Button down color                                                *
;* Color 5 = Text color                                                       *
;*                                                                            *
;* NOTE: Even if the placing of the button is outside the visual screen there *
;* will be no checking for this.                                              *
;*                                                                            *
;* The text font is used by the standard font what is used in the program.    *
;* to change the font do this in the main program. And change back after      *
;* call of this program. Font color is not changed                            *
;******************************************************************************

;* * * *
;* Extra in main program include is SetColors_inc.BB
;* * * *

Function Button3D (XposButton,YposButton,ButWith,ButHeight,Col1$,Col2$,Col3$,Col4$,Col5$,ButText$)
;Make sure the back buffer is the target.
	
; Side colors
Col20$ = Col2$
Col30$ = Col3$
; Button color
Col10$ = Col1$
Col40$ = Col4$

; now everything what was visable is in the drawing area.
; And make a repeating area so the swapping of the button is always on screen
Repeat
;Flip
; Check if the mouse button left is down then swap colors
If (MouseX()>XposButton And MouseX()<XposButton+ButWith) And (MouseY()>YposButton And MouseY()<YposButton+ButHeight) And MouseDown(1) = 1 Then
	; Swap button in colors
	Col2$=Col30$
	Col3$=Col20$
	; button
	Col1$=Col40$
	Col4$=Col10$
Else
	; make button in colors
	Col2$=Col20$
	Col3$=Col30$
	; button
	Col1$=Col10$
	Col4$=Col40$
EndIf

; Draw the left and uppers side.
SetColor Col2$
Line XposButton,YposButton,XposButton+ButWith,YposButton
Line XposButton,YposButton+1,XposButton+ButWith,YposButton+1
Line XposButton,YposButton,XposButton,YposButton+ButHeight
Line XposButton+1,YposButton,XposButton+1,YposButton+ButHeight

;Draw the right and down side
SetColor Col3$
Line XposButton+ButWith,YposButton+1,XposButton+ButWith,YposButton+ButHeight
Line XposButton+ButWith-1,YposButton+2,XposButton+ButWith-1,YposButton+ButHeight
Line XposButton+1,YposButton+ButHeight,XposButton+ButWith,YposButton+ButHeight
Line XposButton+2,YposButton+ButHeight-1,XposButton+ButWith,YposButton+ButHeight-1

;Draw the button
SetColor Col1$
Rect XposButton+2,YposButton+2,ButWith-3,ButHeight-3

;Place the text in the center of the button program wil find out font height and with
SetColor Col5$
Text XposButton+(ButWith/2),YposButton+(ButHeight/2),ButText$,True,True

; bring it al to the front
Flip

;Check to see if the button is hit on a button if so hold program for releas mouse on button
	If MouseX()>XposButton And MouseX()<XposButton+ButWith And MouseY()>YposButton And MouseY()<YposButton+ButHeight And MouseDown(1) = 1 Then
		Mouse = True
	Else 
		If MouseX()>XposButton And MouseX()<XposButton+ButWith And MouseY()>YposButton And MouseY()<YposButton+ButHeight And Mouse=1 Then
			Mouse = False
			MouseReleas = True
		Else
			Mouse = False
			MouseReleas = False
		EndIf
	EndIf
Until Mouse = False

; Send back the button status
Return MouseReleas
End Function

Comments

Rob Farley2006
A couple of thoughts here:
* The line command is very slow, considering you are only drawing horizonal or vertical lines you're better off with the rect command.

* Why not just pass the button one colour and it automatically creates the highlight and lowlight colours?

* Use the rectsoverlap command instead of a complete if mousex()>blah blah stuff

* Why have you got a flip within the function?


Personally I'd do something like this

Graphics 800,600,32,2

Global UIR = 192
Global UIG = 192
Global UIB = 192


; Example code
ClsColor uir,uig,uib
Repeat
	Cls
	For n=1 To 10
		If Button(n*50,n*30,100,20,"Button "+n) Then Color 255,0,0:Text 0,0,"Button "+n+" clicked!!"
	Next
	Flip
Until KeyHit(1)


Function Button(x,y,Width,Height,Label$)
	Over = False
	
	If RectsOverlap (x,y,width,height,MouseX(),MouseY(),1,1) Then over = True

	If over Then Color uir+30,uig+30,uib+30 Else Color uir,uig,uib
	Rect x+1,y+1,width-2,height-2

	Color uir+50,uig+50,uib+50
	Rect x,y,width,1
	Rect x,y,1,height
	
	Color uir-50,uig-50,uib-50
	Rect x+width-1,y,1,height
	Rect x,y+height-1,width,1
	
	Color 0,0,0
	Text x+(width/2),y+(height/2),label,True,True
	
	If over And MouseDown(1) Then Return True Else Return False
End Function


Obviously you'd need to put checking so the hightlight/lowlight doesn't go beyond 0-255


mindstorms2006
I added a depress idea and put checking on high/low for the sides, but not for the depress/highlight so that you can get the base color to be red while the highlight color is green and the depress color is blue (or something like that)




RGR2006
And in case you wonder why the window flickers ... add:
SetBuffer BackBuffer()
in line 2
(I needed 3 Minutes plus an FPS-check-routine until I found out)
{Edit} Or change Graphics to Graphics3D
Here is some additional code
use right mouse to change colors
mouse over button 1 - 5 shows 5 different modes



Fernhout2006
I am already reprogram the button. Because there some drawbacks on this function.
The new function is not waiting for the mouse. But give the state of the button back according to the status of the button given to the function. And yes there will be one color given so the light and dark color wil be calculated from there.
But for now i busy making a book for beginners using Blits3D in Dutch. I did not found one book in dutch, so i wil give my country members a change to learn Blit3D to. If i use the code from someone his name wil be mentioned in my book.
If i'am finished the book wil be free. Every help or ideas for making the book is welcom. The book is devide in 2 parts. Part wil discus every command thats in Blits3D and give an working exsample to see hou its work. There must be no media or graphics in the exsampels. the rest is welcom.


Code Archives Forum