#Include "fbgfx.bi"
Using fb

Namespace minigui
	#Macro pushFlag()
	If ClickFlag=1 Then
		DrawDown()
	Else
		DrawUp()
	EndIf
	#EndMacro
	#Macro XYSET()
	If ImageSource<>0 Then
		ImageInfo(ImageSource,WidthSource,HeightSource)
		If xSource = -333333 Then
			this.xSource = (Width - WidthSource)\2
		Else
			this.xSource=xSource
		EndIf
		If YSource = -333333 Then
			this.ySource = (Height - HeightSource)\2
		Else
			this.ySource = YSource
		EndIf
	EndIf
	#EndMacro
	#Ifndef AllEvent
	Dim Shared AllEvent As EVENT
	#endif
	Type ImagePushButton
		x As Integer
		y As Integer
		Width As Integer
		Height As Integer
		xSource As Integer
		ySource As Integer
		WidthSource As Integer
		HeightSource As Integer
		ForeGround As Integer
		AlphaButton As UByte
		BackGround As Integer=&hf0f0f0
		Image As Any Ptr
		ImageSource  As Any Ptr
		MouseX As Integer
		MouseY As Integer
		ClickFlag As Integer = 0
		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,_
		ImageSource  As Any Ptr,_
		ForeGround As Integer,_
		xSource As Integer = -333333, _
		ySource As Integer  = -333333, _
		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 SetColorBg(BackGround As Integer)
		Declare Sub SetColorFg(ForeGround As Integer)
		Declare Sub SetPos(x As Integer , y As Integer)
		Declare Sub SetImage(ImageSource As Any Ptr,Destroy As Integer=0)
		Declare Sub SetSize(Width As Integer , Height As Integer)
	End Type

	Constructor ImagePushButton( _
	x As Integer,_
	y As Integer,_
	Width As Integer,_
	Height As Integer,_
	ImageSource  As Any Ptr,_
	ForeGround As Integer,_
	xSource As Integer , _
	ySource As Integer, _
	AlphaButton As UByte = 0 _
	)
	this.x = x
	this.y = y
	this.width = Width
	this.height = Height
	this.AlphaButton = AlphaButton
	this.ForeGround = ForeGround
	this.ImageSource = ImageSource
	XYSET()
	If Width <>0 And Height<>0 Then
		Image = ImageCreate(Width,Height)
		DrawUp()
	EndIf
	End Constructor

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

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

	Private Sub ImagePushButton.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
		Put Image,(xSource,ySource),ImageSource,(0,0)-(WidthSource,HeightSource),Add
	End Sub

	Sub ImagePushButton.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
		Put Image,(xSource+1,ySource+1),ImageSource,(0,0)-(WidthSource,HeightSource),Add
	End Sub

	Sub ImagePushButton.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
				pushFlag()
			EndIf
		EndIf
	End Sub

	Sub ImagePushButton.SetColorBg(BackGround As Integer)
		this.BackGround = BackGround
		pushFlag()
	End Sub
	Sub ImagePushButton.SetColorFg(ForeGround As Integer)
		this.ForeGround = ForeGround
		pushFlag()
	End Sub
	Sub ImagePushButton.SetImage(ImageSource As Any Ptr,Destroy As Integer)
		If Destroy=1 Then ImageDestroy(this.ImageSource)
		this.ImageSource = ImageSource
		ImageInfo(ImageSource,WidthSource,HeightSource)
		XYSET()
		pushFlag()
	End Sub
	Sub ImagePushButton.SetPos(x As Integer , y As Integer)
		this.x=x
		This.y=y
		pushFlag()
	End Sub
	Sub ImagePushButton.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
		pushFlag()
	End Sub

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

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