=begin
(c) 2013, Renderiza

Permission to use, copy, modify, and distribute this software for
any purpose and without fee is hereby granted, provided the above
copyright notice appear 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.

== Information
Author:: Renderiza
Name:: iSelect
Version:: 1.0.1
SU Version:: 2013
Date:: 11/6/2013
Description:: Filter selection by what you can see.
=end

module RND_Extensions
	#
	module RND_Iselect
		require 'sketchup.rb'
		require 'RND_iSelect/RND_iSelect_loader.rb'
	end
	#
	if !file_loaded?('rnd_menu_loader')
		@@rnd_tools_menu = UI.menu("Plugins").add_submenu("Renderiza Tools")
	end
	#
	#------New menu Items---------------------------
	if !file_loaded?('rnd_ew_loader')
		@@rnd_ew_menu = @@rnd_tools_menu.add_submenu("Websites")
		@@rnd_ew_menu.add_separator
		@@rnd_ew_menu.add_item("PluginStore"){UI.openURL('http://sketchucation.com/resources/pluginstore?pauthor=renderiza')}
		@@rnd_ew_menu.add_item("Extension Warehouse"){UI.openURL('http://extensions.sketchup.com/en/user/5451/store')}
		@@rnd_tools_menu.add_separator
		@@rnd_ew_menu.add_separator
	end
	#------------------------------------------------
	if !file_loaded?(__FILE__) then
		@@rnd_tools_menu.add_item('iSelect'){
		Sketchup.active_model.select_tool RND_Extensions::RND_Iselect::Iselect.new
		}
		# Add toolbar
		rnd_iselect_tb = UI::Toolbar.new "iSelect"
		rnd_iselect_cmd = UI::Command.new("iSelect"){
		Sketchup.active_model.select_tool RND_Extensions::RND_Iselect::Iselect.new
		}
		rnd_iselect_cmd.small_icon = "img/rnd_iselect_1_16.png"
		rnd_iselect_cmd.large_icon = "img/rnd_iselect_1_24.png"
		rnd_iselect_cmd.tooltip = "iSelect"
		rnd_iselect_cmd.status_bar_text = "Filter selection by what you can see."
		rnd_iselect_cmd.menu_text = "iSelect"
		rnd_iselect_tb = rnd_iselect_tb.add_item rnd_iselect_cmd
		rnd_iselect_tb.show
	end
	#
	#------Add Context Menu-------------------------
	UI.add_context_menu_handler{|menu|
		#
		menu.add_item('iSelect'){
			#
			model = Sketchup.active_model
			sel = model.selection
			peye = model.active_view.camera.eye 
			#
			sel.to_a.each { |e|
				boundbox = e.bounds
				#
				if e.is_a?(Sketchup::ComponentInstance) || e.is_a?(Sketchup::Group) 
					#
					c0 = boundbox.corner(0)
					c1 = boundbox.corner(1)
					c2 = boundbox.corner(2)
					c3 = boundbox.corner(3)
					c4 = boundbox.corner(4)
					c5 = boundbox.corner(5)
					c6 = boundbox.corner(6)
					c7 = boundbox.corner(7)
					#
					r0 = model.raytest [c0,peye]
					r1 = model.raytest [c1,peye]
					r2 = model.raytest [c2,peye]
					r3 = model.raytest [c3,peye]
					r4 = model.raytest [c4,peye]
					r5 = model.raytest [c5,peye]
					r6 = model.raytest [c6,peye]
					r7 = model.raytest [c7,peye]
					#
					if r0 and r1 and r4 and r5 || r2 and r3 and r6 and r7
						sel.remove e
					end
					#
				else
					#
					center = boundbox.center
					hit_path = model.raytest [center,peye]
					#
					if hit_path
						sel.remove e
					end
					#
				end
			}
			#
			Sketchup.send_action('selectSelectionTool:')
			#
			UI.beep
			#
		}
	}
	#
	file_loaded('rnd_ew_loader')
	file_loaded('rnd_menu_loader')
	file_loaded(__FILE__)
end