/*
Copyright (C) 2014 Runtime Revolution Ltd.

This file is part of LiveCode.

LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.

LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with LiveCode.  If not see <http://www.gnu.org/licenses/>.  */

widget com.livecode.widget.example


metadata version is "0.1.0"
metadata author is "Fraser J. Gordon"
metadata title is "Example Widget"


// ---------- Properties ---------- //

property fillColor get getFillColor set setFillColor
property hoverFillColor get getHoverFillColor set setHoverFillColor
//property clickFillColor get getClickFillColor set setClickFillColor

property mouseHiliteRadius get mMouseHiliteRadius set mMouseHiliteRadius


// ---------- Private instance variables ---------- //

private variable mFillColor
private variable mHoverFillColor
private variable mClickFillColor

private variable mMouseHiliteColor
private variable mMouseHiliteRadius as real

// Work-around for the lack of working "is pressed" syntax
private variable mIsClicked as boolean


// ---------- Event handlers ---------- //

public handler OnCreate() as undefined
	put color [ 1.0, 0.0, 0.0 ] into mFillColor
	put color [ 0.0, 0.0, 1.0 ] into mHoverFillColor
	put color [ 0.0, 1.0, 0.0 ] into mClickFillColor
	
	put color [ 1.0, 1.0, 0.0 ] into mMouseHiliteColor
	put 15.0 into mMouseHiliteRadius

	put false into mIsClicked
end handler

public handler OnPaint() as undefined
	variable tPaint 
	variable tRect 
	variable tPath 
 	
	if mIsClicked then
		put solid paint with mClickFillColor into tPaint
	else if the mouse position is within my bounds then
		put solid paint with mHoverFillColor into tPaint
	else
		put solid paint with mFillColor into tPaint
	end if

	put rectangle [ 0.0, 0.0, the width of my rectangle, the height of my rectangle ] into tRect
	put rectangle path of tRect into tPath
	put tPaint into the paint of this canvas
	fill tPath on this canvas

	// Draw the mouse highlight
	if the mouse position is within my bounds then
		put solid paint with mMouseHiliteColor into the paint of this canvas
		fill circle path centered at the mouse position with radius mMouseHiliteRadius on this canvas
	end if

end handler

public handler OnMouseEnter() as undefined
	// Trigger a redraw so the color can be updated
	redraw all
end handler

public handler OnMouseLeave() as undefined
	// Trigger a redraw so the color can be updated
	redraw all
end handler

public handler OnMouseMove(in pX as real, in pY as real) as undefined
	redraw all
end handler

public handler OnMouseDown(in pX as real, in pY as real, in pButtonIndex as integer) as undefined
	put true into mIsClicked
	redraw all
end handler

public handler OnMouseUp(in pX as real, in pY as real, in pButtonIndex as integer) as undefined
	put false into mIsClicked
	redraw all
end handler

public handler OnMouseCancel(in pButtonIndex as integer) as undefined
	put false into mIsClicked
	redraw all
end handler


// ----------- Private handlers ---------- //

private handler stringToColor(in pString as string) /* as Color */

end handler

private handler colorToString(in pColor /* as Color */) as string

end handler

private handler getFillColor() as string
	return colorToString(mFillColor)
end handler

private handler getHoverFillColor() as string
	return colorToString(mHoverFillColor)
end handler

private handler getClickFillColor() as string
	return colorToString(mClickFillColor)
end handler

private handler setFillColor(in pFillColor as string) as undefined
	put stringToColor(pFillColor) into mFillColor
end handler

private handler setHoverFillColor(in pHoverFillColor as string) as undefined
	put stringToColor(pHoverFillColor) into mHoverFillColor
end handler

private handler setClickFillColor(in pClickFillColor as string) as undefined
	put stringToColor(pClickFillColor) into mClickFillColor
end handler

end widget
