/*
Copyright (C) 2015 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/>.  */

/*
A multi-option selection widget, where each selectable item is numbered in sequence and displayed in
horizontally arranged circles

Name: selectedOption
Type: property

Syntax: set the selectedOption of <widget> to <pSelection>
Syntax: get the selectedOption of <widget>

Summary: Sets the selected option.
pSelection (integer): The number of the selected option.

Description:
Sets the selected option to selection number pSelection.

Name: totalOptions
Type: property
Summary: Sets the number of selections.

Syntax: set the totalOptions of <widget> to <pNumSelections>
Syntax: get the totalOptions of <widget>

pNumSelections(integer): The number of selections.

Description:
Sets the number of options available in the selector.
*/

-- declaring extension as widget, followed by identifier
widget com.livecode.widget.selector
--

-- dependancy declarations
use com.livecode.canvas
use com.livecode.widget
use com.livecode.engine

--metadata
metadata title is "Selector"
metadata author is "LiveCode"
metadata version is "1.0.2"
metadata svgicon is "M129.2,0c-13.1,0-23.8,10.6-23.9,23.7h-4.8C100.4,10.6,89.8,0,76.6,0S52.9,10.6,52.8,23.7h-5C47.6,10.6,37,0,23.9,0C10.7,0,0,10.7,0,23.9s10.7,23.9,23.9,23.9c13.1,0,23.8-10.6,23.9-23.7h5c0.1,13.1,10.8,23.7,23.9,23.7s23.8-10.6,23.9-23.7h4.8c0.1,13.1,10.8,23.7,23.9,23.7c13.2,0,23.9-10.7,23.9-23.9S142.4,0,129.2,0z M24.3,42v-5.5h5.3c0.9,0,1.5-0.2,1.9-0.5c0.4-0.3,0.6-0.9,0.6-1.6c0-0.7-0.2-1.3-0.6-1.6c-0.4-0.3-1-0.5-1.9-0.5h-3.2V13.1c0-0.7-0.1-1.2-0.3-1.4c-0.2-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1.6-0.5c0.6,0,1,0.2,1.3,0.5c0.3,0.3,0.5,0.8,0.5,1.4V32.1z M135.3,34.7c-1.6,1.4-3.9,2.1-6.7,2.1c-2.3,0-4.2-0.4-5.7-1.1c-1.5-0.8-2.2-1.7-2.2-2.8c0-0.6,0.2-1.2,0.6-1.6c0.4-0.4,1-0.6,1.6-0.6c0.3,0,1.2,0.3,2.5,0.9s2.5,0.9,3.5,0.9c1.2,0,2.2-0.3,2.8-1c0.7-0.7,1-1.6,1-2.8c0-1.3-0.3-2.3-0.8-2.8c-0.6-0.6-1.6-0.9-3.2-0.9c-0.8,0-1.4-0.2-1.7-0.5c-0.3-0.3-0.4-0.8-0.4-1.6c0-1,0.6-1.6,1.7-1.8c0.2,0,0.3,0,0.4,0c1.4-0.2,2.3-0.5,2.7-1c0.4-0.5,0.6-1.2,0.6-2.3c0-0.9-0.3-1.6-0.8-2.2c-0.5-0.5-1.2-0.8-2.1-0.8c-1,0-2,0.3-3.2,0.9c-1.2,0.6-2,0.9-2.5,0.9c-0.5,0-0.9-0.2-1.2-0.5c-0.3-0.3-0.5-0.8-0.5-1.3c0-1.1,0.8-2.1,2.3-2.9c1.5-0.8,3.4-1.2,5.7-1.2c2.3,0,4.1,0.6,5.4,1.8s2,2.7,2,4.7c0,1.3-0.3,2.4-0.9,3.2c-0.6,0.9-1.6,1.6-2.9,2.2c1.5,0.5,2.6,1.2,3.3,2.2c0.7,1,1.1,2.3,1.1,3.9C137.7,31.4,136.9,33.3,135.3,34.7z"

-- property declarations
property totalOptions		get mNumSelections		set setNumSelections
property selectedOption		get mSelected			set setSelected

--

-- private instance variables
private variable mNumSelections		as Integer
private variable mSelected			as Integer
--

public handler OnSave(out rProperties as Array)
	put the empty array into rProperties
	
	put mNumSelections into rProperties["numSelections"]
	put mSelected into rProperties["selected"]
	
	return rProperties
end handler

public handler OnLoad(in pProperties as Array)
	put pProperties["numSelections"] into mNumSelections
	put pProperties["selected"] into mSelected
end handler

public handler OnCreate()
	put 5 into mNumSelections
	put 1 into mSelected
end handler

public handler OnPaint()
	
	variable tCount as Integer
	repeat with tCount from 1 up to mNumSelections
		
		set the paint of this canvas to getPaint("background",tCount)
		fill getPath("circle",tCount) on this canvas
			
		set the font of this canvas to font getFontName() at size (my width)/(3*mNumSelections)
		set the paint of this canvas to getPaint("labels",0)
		fill text getLabel(tCount) at center of getRect("label", tCount) on this canvas
		
	end repeat
	
end handler

public handler OnMouseUp()
	put getSelected() into mSelected
	redraw all
    
	post "mouseUp" with [mSelected]
end handler

private handler getLabel(in pSelection as Integer) returns String

	variable tLabel
	put pSelection formatted as string into tLabel
	
	if pSelection > 9 then
		return char 1 to 2 of tLabel
	else
		return the first char of tLabel
	end if
	
	--return tLabel
end handler

private handler getPaint(in pObject as String, in pSelection as Integer) returns Paint
	
	if pObject is "background" then
		if pSelection is mSelected then
			return solid paint with color [51/255, 153/255, 1]
		else 
			return solid paint with color [224/255, 224/255, 224/255]
		end if
		
	else if pObject is "labels" then
		return solid paint with color [1,1,1]
	end if
	
end handler

private handler getPath(in pShape as String, in pSelection as Integer) returns Path
	variable tCenterX as Number
	put (my width)/(2*mNumSelections) + (pSelection-1)*(my width)/mNumSelections into tCenterX
	
	if pShape is "circle" then
		return circle path centered at point [tCenterX, (my height)/2] with radius (my width)/(3*mNumSelections)
	end if

end handler

private handler getRect(in pType as String, in pSelection as Integer) returns Rectangle
	variable tCenterX as Number
	put (my width)/(2*mNumSelections) + (pSelection-1)*(my width)/mNumSelections into tCenterX
	
	if pType is "label" then
		return rectangle [tCenterX-(my width)/(3*mNumSelections), 0, tCenterX+(my width)/(3*mNumSelections),my height]
	end if
	
end handler

private handler getFontName() returns String
	return the name of the font of this canvas
end handler

private handler getSelected() returns Number

	variable tZ as Integer
	variable tCenter as Real
	variable tDifference as Real
	variable tRectangle as Rectangle
	
	repeat with tZ from 1 up to mNumSelections
		put (my width)/(2*mNumSelections) + (tZ-1)*(my width)/mNumSelections into tCenter
		put (my width)/(3*mNumSelections) into tDifference
	
		put rectangle [tCenter-tDifference, (my height/2)-tDifference, tCenter+tDifference, (my height/2)+tDifference] into tRectangle
		if the click position is within tRectangle then
			return tZ
		end if
	end repeat
		
	return mSelected
end handler

public handler setNumSelections(in pSelections as Integer) returns nothing
	put pSelections into mNumSelections
	redraw all
end handler

public handler setSelected(in pSelected as Integer) returns nothing
	put pSelected into mSelected
	redraw all
end handler

end widget
