=begin
#-------------------------------------------------------------------------------------------------------------------------------------------------
#*************************************************************************************************
# Designed by Fredo6 - Copyright November 2008

# Permission to use this software for any purpose and without fee is hereby granted
# Distribution of this software for commercial purpose is subject to:
#  - the expressed, written consent of the author
#  - the inclusion of the present copyright notice in all copies.

# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#-----------------------------------------------------------------------------
# Name			:   FredoScale_Selection.rb
# Original Date	:   8 Dec 2010 - version 2.1
# Description	:   Selection tool for FredoScale
#-------------------------------------------------------------------------------------------------------------------------------------------------
#*************************************************************************************************
=end

module F6_FredoScale

#--------------------------------------------------------------------------------------------------------------
# Top Calling functions: create the classes and launch the tools
#--------------------------------------------------------------------------------------------------------------			 				   

#Actual launching of menus	
def F6_FredoScale.action__mapping(action_code)
	action_code = action_code.to_s
	mode = nil
	if action_code =~ /\_(B|T|A|U)/
		mode = $1
		action_code = $`
		mode = nil if mode == 'B'
	end
	F6_FredoScale.launch action_code, mode
end

def F6_FredoScale.launch(tooltype, mode=nil)
	
	#Creating the context
	hparam = {}
	hparam["ToolType"] = tooltype
	hparam["ToolMode"] = mode
	hparam["OriginalToolType"] = tooltype
	hparam["Title"] = F6_FredoScale.calculate_title tooltype, mode
	
	# Mode Alone
	if mode =~ /A/i
		hparam["Mode_Alone"] = true
		sclass = tooltype + '_AloneTool'
		lstcursors = [tooltype + '_Alone']

	#Utility tools	
	elsif mode =~ /U/i
		hparam["Mode_Util"] = true
		sclass = tooltype + '_UtilTool'
		lstcursors = [tooltype + '_Util']
		
	#Mode box	
	else
		hparam["Mode_Target"] = true if mode =~ /T/i
		sclass = tooltype + 'Tool'
		lstcursors = [tooltype, tooltype + 'Target']
		lstcursors += ['Scale', 'ScaleTarget'] unless tooltype =~ /Scale/i
		["None", "Arrow", "Arrow_Edge", "Arrow_Face", "Arrow_Exit"].each do |name| 
			hparam['Cursor_' + name] = MYPLUGIN.create_cursor(name, 0, 0)
		end	
		["Validate"].each do |name| 
			hparam['Cursor_' + name] = MYPLUGIN.create_cursor(name, 12, 23)
		end	
	end	

	#Creating an instance of the main class
	begin
		hparam["MainTool"] = eval sclass + '.new hparam'
	#rescue
		#UI.messagebox "Class #{sclass} is not implemented or in error"
		#return nil
	end	
	
	#Creating the required cursors
	lstcursors.each do |name| 
		hparam['Cursor_' + name] = MYPLUGIN.create_cursor(name, 1, 3)
	end	
	
	#Creating the Selection and Scale Tools 
	seltool = SelectionTool.new hparam
	hparam["SelectTool"] = seltool
		
	#Starting with the selection tool
	seltool._select
	LibFredo6.register_ruby 'FredoScale' if defined?(LibFredo6.register_ruby)	
	
end

#--------------------------------------------------------------------------------------------------------------
# Selection Tool for picking up entities
#   < Specific implementation over the generic class Traductor::StandardSelectionTool
#--------------------------------------------------------------------------------------------------------------			 				   

class SelectionTool

@@extend_selection = nil

def initialize(hparam)
	@hparam = hparam
	@tool = Traductor::StandardSelectionTool.new self, hparam
	@@extend_selection = MYDEFPARAM[:DEFAULT_SelectionExtended] if @@extend_selection == nil
	if @hparam["Select_NoExtend"]
		@tool.extend_selection = false
		@tool.extend_face = false
	else
		@tool.extend_selection = @@extend_selection
		@tool.extend_face = true
	end	
	#@tool.keep_selection = (@hparam["Select_KeepSelection"]) ? true : false
	@title_tool = F6_FredoScale.compute_title_tool @hparam
end

#Selection and activation of the tool
def _select
	Sketchup.active_model.select_tool @tool
end
		
def sub_cursor_set(entity, extend_selection)
	F6_FredoScale.get_cursor @hparam
end

def sub_exit_tool
	@@extend_selection = @tool.extend_selection unless @hparam["Select_NoExtend"]
	@hparam["MainTool"]._select
end

def sub_deactivate(view)
	@@extend_selection = @tool.extend_selection unless @hparam["Select_NoExtend"]
end

def sub_check_entity(entity)
	lst_types = @hparam["Select_Types"]
	lst_types = ["Face", "Edge", "Group", "ComponentInstance", "ConstructionPoint"] unless lst_types
	lst_types.include?(entity.typename)
end

#Get the title of the tool
def sub_get_title_tool
	F6_FredoScale.compute_title_tool @hparam
end

end	#class SelectionTool

#--------------------------------------------------------------------------------------------------------------
# Common class Implementation for Picking  tools  
#--------------------------------------------------------------------------------------------------------------			 				   
	
class AllPickTool

#Selection and activation of the tool
def _select
	Sketchup.active_model.select_tool @tool
end

#Simulate a move
def onMouseMove_zero
	@tool.onMouseMove_zero
end

#Get the title of the tool
def sub_get_title_tool
	F6_FredoScale.compute_title_tool @hparam
end

def sub_resume(view)
	@scaletool.resume view
end

def sub_onKeyDown(key, rpt, flags, view)
	#Function keys
	return true if @scaletool.check_function_key(key, rpt, flags, view)
	
	#Ctrl key - Center vs. Opposite
	if key == COPY_MODIFIER_KEY
		@scaletool.onKeyDown(key, rpt, flags, view)
		return true
	end	
	false
end

def sub_onKeyUp(key, rpt, flags, view)
	#Parameters
	if key == 9
		@scaletool.onKeyUp(key, rpt, flags, view)
		return true
	end	
	false
end

def sub_cursor_set
	@idcursor
end

def sub_getMenu_before(menu)
	@scaletool.common_menu menu
	false
end

end	#class AllPickTool
	
#--------------------------------------------------------------------------------------------------------------
# Tool for picking 2 points (Origin and Target)
#   < Specific implementation over the generic class Traductor::StandardPickLineTool
#--------------------------------------------------------------------------------------------------------------			 				   

class PickLineTool < AllPickTool

#Placeholder subclassing routines
def initialize(hparam)
	@hparam = hparam
	@tool = Traductor::StandardPickLineTool.new self, hparam
	@tool.set_mark_origin MARK_Target_Origin, 2, ""
	@tool.set_mark_target MARK_Target_Target, 2, ""
	@scaletool = hparam["MainTool"]
	refresh_hsh_entityID
	@idcursor = 0
end

def set_custom_axes(axes, tooltip=nil)
	@tool.set_custom_axes axes, tooltip
end

def sub_draw_before(view, pt_origin, pt_target)
	@scaletool.draw view
end

def sub_move(view, pt_origin, pt_target)
	return @scaletool.reach_target(view, pt_origin, pt_target) if pt_target
end

def refresh_hsh_entityID
	@tool.set_hsh_entityID @scaletool.get_hsh_entityID
end

def sub_onCancel(flag, view)
	@scaletool.onCancel flag, view
end

def sub_exit_tool 
	@scaletool._select
end	

def sub_getMenu_after(menu)
	menu.add_separator
	menu.add_item(T6[:MNU_SwitchToBox]) { @scaletool.return_from_target }
end

def sub_onLButtonDoubleClick(flags, x, y, view, state)
	return unless state <= 1
	@scaletool.return_from_target
end

def sub_execute(pt_origin, pt_target)
	@scaletool.execute_from_target
end

def sub_test_origin(pt_origin)
	pt = @scaletool.test_origin pt_origin
end

end	#class PickLineTool

#--------------------------------------------------------------------------------------------------------------
# Tool for picking an angle via the protractor
#   < Specific implementation over the generic class Traductor::StandardProtractorTool
#--------------------------------------------------------------------------------------------------------------			 				   

class PickAngleTool < AllPickTool

#Placeholder subclassing routines
def initialize(hparam, origin, normal, basedir=nil)
	@hparam = hparam
	@tool = Traductor::StandardProtractorTool.new self, hparam
	@tool.impose_direction origin, normal, basedir
	@scaletool = hparam["MainTool"]
	refresh_hsh_entityID
	@id_cursor = F6_FredoScale.get_cursor hparam
end

def set_custom_axes(axes, tooltip=nil)
	@tool.set_custom_axes axes, tooltip
end

def refresh_hsh_entityID
	@tool.set_hsh_entityID @scaletool.get_hsh_entityID
end

def sub_draw_before(view, state)
	@scaletool.draw view
end

def sub_execute(origin, normal, basedir, angle) 
	@scaletool.execute_from_protractor origin, normal, basedir, angle
end

def sub_rotate(view, origin, normal, basedir, angle) 
	@scaletool.rotate_from_protractor view, origin, normal, basedir, angle
end

def sub_onCancel(flag, view, state)
	@scaletool.onCancel flag, view
	sub_exit_tool if state >= 1
end

def sub_exit_tool 
	@scaletool._select
end	
	
end	#class PickAngleTool

#--------------------------------------------------------------------------------------------------------------
# Tool for moving a divider (for Stretching)
#   < Specific implementation over the generic class Traductor::StandardPickLineTool
#--------------------------------------------------------------------------------------------------------------			 				   

class PickDividerTool < AllPickTool

#Placeholder subclassing routines
def initialize(hparam, origin, axes, iaxe)
	@hparam = hparam
	@origin = origin
	@axes = axes
	@iaxe = iaxe
	@tool = Traductor::StandardPickLineTool.new self, hparam
	@tool.set_mark_origin nil
	@tool.set_mark_target MARK_Divider_Target, 2, ""
	@scaletool = hparam["MainTool"]
	@idcursor = 0
end

def sub_activate
	@tool.set_custom_axes @axes, T6[:TIP_ScalingBox]
	@tool.set_origin @origin
	@tool.lock_axis ['X', 'Y', 'Z'][@iaxe]
	@tool.set_line_style 1, '-'
end

def sub_draw_before(view, pt_origin, pt_target)
	@scaletool.draw view
end

def sub_move(view, pt_origin, pt_target)
	true
end

def sub_onCancel(flag, view)
	@scaletool.compute_divider_factor @origin
end

def sub_exit_tool 
	@scaletool._select
end	

def sub_onLButtonDoubleClick(flags, x, y, view, state)
	return unless state <= 1
	@scaletool.reset_divider_to_centre
end

def put_divider_to_centre
	@scaletool.compute_divider_factor nil
	sub_exit_tool
end

def sub_execute(pt_origin, pt_target)
	@scaletool.validate_scaling true
	sub_exit_tool
end

def sub_test_target(pt_target)
	@scaletool.compute_divider_factor pt_target
end

end	#class PickDividerTool

end	#End Module F6_FredoScale
