#Include Once "fbgfx.bi"
Using FB
Namespace minigui
	Dim Shared AllEvent As EVENT
	Type Button
		x As Integer
		y As Integer
		Width As Integer
		Height As Integer
		Text As ZString Ptr
		LenText As Integer
		ForeGround As Integer
		ColorText As Integer
		AlphaColorText As UByte
		AlphaButton As UByte
		BackGround As Integer=&hf0f0f0
		Image As Any Ptr
		MouseX As Integer
		MouseY As Integer
		ClickFlag As Integer
		r As UByte
		g As UByte
		b As UByte
		a As UByte = 255
		Declare 	Constructor( _
		x As Integer,_
		y As Integer,_
		Width As Integer,_
		Height As Integer,_
		text  As ZString Ptr,_
		ForeGround As Integer,_
		ColorText As Integer, _
		AlphaColorText As UByte = &hFF, _
		AlphaButton As UByte = 0 _
		)
		Declare Destructor()
		Declare Sub DrawUp()
		Declare Sub DrawDown()
		Declare Sub Update()
		Declare Function Click() As Integer
		Declare 	Function GetPushed() As Integer
		Declare Sub GetColor(Col As Integer)
		Declare Sub SetColorText(ColorText As Integer)
		Declare Sub SetColorBg(BackGround As Integer)
		Declare Sub SetColorFg(ForeGround As Integer)
		Declare Sub SetPos(x As Integer , y As Integer)
		Declare Sub SetText(Text As ZString Ptr)
		Declare Sub SetSize(Width As Integer , Height As Integer)
	End Type

	Constructor Button( _
	x As Integer,_
	y As Integer,_
	Width As Integer,_
	Height As Integer,_
	text  As ZString Ptr,_
	ForeGround As Integer,_
	ColorText As Integer, _
	AlphaColorText As UByte = &hFF, _
	AlphaButton As UByte = 0 _
	)
	this.x = x
	this.y = y
	this.width = Width
	this.height = Height
	this.text = text
	this.AlphaColorText = AlphaColorText
	this.AlphaButton = AlphaButton
	LenText = Len(*text)
	this.ForeGround = ForeGround
	this.ColorText = ColorText
	If Width <>0 And Height<>0 Then
		Image = ImageCreate(Width,Height)
		DrawUp()
	EndIf
	End Constructor

	Destructor Button()
	If Image<>0 Then ImageDestroy(Image)
	End Destructor

	Private Sub Button.GetColor(Col As Integer)
		r = CUByte(HiWord(Col))
		g = CUByte(LoWord(HiByte(Col)))
		b = CUByte(LoWord(LoByte(Col)))
	End Sub

	Private Sub Button.DrawUp()
		GetColor(ForeGround)
		For i As Integer = 0 To height+1
			Line Image,(-1,i)-Step(Width,0),RGBA(r,g,b,a+1+(i*(255\(height))))
		Next
		GetColor(ColorText)
		Draw String Image,((Width - LenText*8)\2,(Height-8)\2),*Text,RGBA(this.r,this.g,this.b,AlphaColorText)
	End Sub

	Sub Button.DrawDown()
		GetColor(ForeGround)
		For i As Integer = 0 To height+1
			Line Image,(-1,i+1)-Step(Width,0),RGBA(r,g,b,a-(i*(255\(height))))
		Next
		GetColor(ColorText)
		Draw String Image,((Width - LenText*8)\2+2,(Height-8)\2+2),*Text,RGBA(this.r,this.g,this.b,AlphaColorText)
	End Sub

	Sub Button.Update()
		If AlphaButton = 0 Then
			Line(x-1,y)-Step(Width+1,height),BackGround,BF
		EndIf
		Put (x,y),Image,Alpha
		If AllEvent.type=EVENT_MOUSE_BUTTON_PRESS Then
			If MouseX>= x And MouseX<=x+Width And MouseY>=y And MouseY<=y+height Then
				If ClickFlag = 0 Then
					DrawDown()
				EndIf
				ClickFlag = 1
			EndIf
		EndIf
	End Sub

	Sub Button.SetColorText(ColorText As Integer)
		this.ColorText = ColorText
		DrawUp
	End Sub
	Sub Button.SetColorBg(BackGround As Integer)
		this.BackGround = BackGround
		DrawUp
	End Sub
	Sub Button.SetColorFg(ForeGround As Integer)
		this.ForeGround = ForeGround
		DrawUp
	End Sub
	Sub Button.SetText(Text As ZString Ptr)
		this.Text = Text
		LenText = Len(*text)
		DrawUp
	End Sub
	Sub Button.SetPos(x As Integer , y As Integer)
		this.x=x
		This.y=y
		DrawUp
	End Sub
	Sub Button.SetSize(Width As Integer , Height As Integer)
		If Width=0 Or Height=0 Then Exit sub
		ImageDestroy(Image)
		Image=ImageCreate(Width,Height)
		this.Width=Width
		This.Height=Height
		Cls
		DrawUp
	End Sub

	Function Button.GetPushed() As Integer
		Return ClickFlag
	End Function

	Function Button.Click() As Integer
		If AllEvent.type=EVENT_MOUSE_MOVE Then
			MouseX=AllEvent.x:MouseY=AllEvent.y
		EndIf
		If AllEvent.type=EVENT_MOUSE_BUTTON_RELEASE And ClickFlag = 1 Then
			DrawUp()
			If MouseX>= x And MouseX<=x+Width And MouseY>=y And MouseY<=y+height Then
				ClickFlag = 0
				Return 1
			EndIf
		EndIf
	End Function
End Namespace

