=begin

(c) RENDERIZA 2013

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:: [SU]Ch
Version:: 1.0.2
SU Version:: v8
Date:: 6/29/2013
Description:: Create Rendering Channels for compositing.
History::
1.0.0:: 6/23/2013
	* Original release.  
1.0.1:: 6/29/2013
	* Exporter for all scenes
1.0.2:: 7/5/2013
	* Export single scene
	* Default scene 
=end

module RND_Extensions

	module RND_Suchannels

		class Suchannels

			@@model = Sketchup.active_model
			@@path = File.dirname(__FILE__) # this plugin's folder
			@@rnd_suchannels = "rnd_suchannels.html"
			@@suchannels_files = File.join(@@path, '/SuchannelsFiles/')
			@@suchannels_dlg = nil

			def initialize()

				@such_1_file =  File.join(@@path, @@rnd_suchannels)

				if (!@such_1_file) then
					UI.messagebox("Cannot find file '#{@@rnd_suchannels} in folder '#{@@path}'")
					return
				end

				if @@suchannels_dlg == nil
					@@suchannels_dlg = UI::WebDialog.new("[SU]Ch", false, "[SU]Ch", 195, 600, 70, 95, true)
					@@suchannels_dlg.add_action_callback("push_frame") do |d,p|
					push_frame(d,p)
					end
				end

				@@suchannels_dlg.set_size(195, 600)
				@@suchannels_dlg.set_file(@such_1_file)
				@@suchannels_dlg.show()

			end #def



			def push_frame(dialog,data)

				params = query_to_hash(data) # parse state information

				########################################
				###Click Update#########################
				########################################
				if params['active'].to_s == "1"

					Sketchup.active_model.select_tool RND_Extensions::RND_Suchannels::Suchannels.new

					#########
					###RUN###
					#########

					model = Sketchup.active_model
					pages = model.pages
					num = pages.count

					########################################
					###Default##############################
					########################################
					if num == 0
						###Add Page###
						pages.add("Default")

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Alpha################################
					########################################
					if params['alpha'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Alpha")
						styles = Sketchup.active_model.styles
						alpha_style = File.join(@@suchannels_files, 'Alpha Style.style')
						status = styles.add_style alpha_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Clay#################################
					########################################
					if params['clay'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=50
						Sketchup.active_model.shadow_info['Dark']=15

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Clay")
						styles = Sketchup.active_model.styles
						cley_style = File.join(@@suchannels_files, 'Clay Style.style')
						status = styles.add_style cley_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Color################################
					########################################
					if params['color'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Color")
						styles = Sketchup.active_model.styles
						color_style = File.join(@@suchannels_files, 'Color Style.style')
						status = styles.add_style color_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Color by Layer#######################
					########################################
					if params['colorbylayer'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Color by Layer")
						styles = Sketchup.active_model.styles
						colorbylayer_style = File.join(@@suchannels_files, 'Color by Layer Style.style')
						status = styles.add_style colorbylayer_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Line#################################
					########################################
					if params['line'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Line")
						styles = Sketchup.active_model.styles
						line_style = File.join(@@suchannels_files, 'Line Style.style')
						status = styles.add_style line_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Profile##############################
					########################################
					if params['profile'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Profile")
						styles = Sketchup.active_model.styles
						profile_style = File.join(@@suchannels_files, 'Profile Style.style')
						status = styles.add_style profile_style, true

						###Update###
						selpage = pages.selected_page 
						selpage.update
					end

					########################################
					###Shadow###############################
					########################################
					if params['shadow'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=true
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=100
						Sketchup.active_model.shadow_info['Dark']=0

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Shadow")
						styles = Sketchup.active_model.styles
						shadow_style = File.join(@@suchannels_files, 'Shadow Style.style')
						status = styles.add_style shadow_style, true

						###Update###
						selpage = pages.selected_page 
						selpage.update
					end

					########################################
					###Texture##############################
					########################################
					if params['texture'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['DisplayFog']=false

						###Add Page###
						pages.add("Texture")
						styles = Sketchup.active_model.styles
						texture_style = File.join(@@suchannels_files, 'Texture Style.style')
						status = styles.add_style texture_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end

					########################################
					###Zdepth###############################
					########################################
					if params['zdepth'].to_s == "true"
						###Shadow Settings###
						Sketchup.active_model.shadow_info['DisplayShadows']=false
						Sketchup.active_model.shadow_info['UseSunForAllShading']=true
						Sketchup.active_model.shadow_info['Light']=0
						Sketchup.active_model.shadow_info['Dark']=100

						###Fog Settings###
						Sketchup.active_model.rendering_options['FogEndDist']=35.m
						Sketchup.active_model.rendering_options['DisplayFog']=true

						###Add Page###
						pages.add("Zdepth")
						styles = Sketchup.active_model.styles
						zdepth_style = File.join(@@suchannels_files, 'Zdepth Style.style')
						status = styles.add_style zdepth_style, true

						###Update###
						selpage = pages.selected_page
						selpage.update
					end


					activate = 0
					script = "top.active = " + activate.to_s + ";"
					dialog.execute_script(script)

					Sketchup.send_action('selectSelectionTool:') #Selection Tool

				end #Done

				########################################
				###Click Done###########################
				########################################
				if params['done'].to_s == "1"
					Sketchup.send_action('selectSelectionTool:') #Selection Tool
					@@suchannels_dlg.close
				end

			end #def



			def unescape(string)
				if string != nil
					string = string.gsub(/\+/, ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
						[$1.delete('%')].pack('H*')
					end
				end
				return string
			end #def



			def query_to_hash(query)
				param_pairs = query.split('&')
				param_hash = {}
				for param in param_pairs
					name, value = param.split('=')
					name = unescape(name)
					value = unescape(value)
					param_hash[name] = value
				end
				return param_hash
			end #def

		end #class

	end #module

end #module

file_loaded( __FILE__ )