=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 Suexporter

			@@model = Sketchup.active_model 
			@@path = File.dirname(__FILE__) # this plugin's folder
			@@rnd_export = "rnd_export.html"
			@@export_files = File.join(@@path, '/ExportFiles/')

			@@export_dlg = nil

			def initialize()

				@suexport_2_file =  File.join(@@path, @@rnd_export)

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

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

				@@export_dlg.set_size(225, 600)
				@@export_dlg.set_file(@suexport_2_file)
				@@export_dlg.show()

			end #def



			def push_frame(dialog,data)

				params = query_to_hash(data) # parse state information

				########################################
				###Click Export#########################
				########################################
				if params['exporter'].to_s == "1"

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

					if @@model.path==""

						UI.messagebox("Save 'SKP' model before exporting.")

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

					else

						if params['antialias'].to_s == "true"
							@anti = true
						else
							@anti = false
						end

						if params['transparent'].to_s == "true"
							@trans = true
						else
							@trans = false
						end

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

						model = Sketchup.active_model
						view = model.active_view
						pages = model.pages
						
			
						if params['all_single'].to_s == "all"
							pages.each do |page|
								
								pages.selected_page = page
								UI.messagebox("Press 'OK' after #{page.name} is done loading.")

								folder=File.dirname(model.path)
								scene=model.pages.selected_page.name.gsub(/[^A-Za-z0-9_-]/,"_")
								file=model.title+"_"+scene+".#{params['sel_type'].to_s}"
								path=File.join(folder,file).tr("\\","/")

								keys = {
								   :filename => path,
								   :width => params['getwidth'].to_i,
								   :height => params['getheight'].to_i,
								   :antialias => @anti,
								   :compression => 0.9,
								   :transparent => @trans
								}

								Sketchup.active_model.active_view.write_image(keys)

							end

						else

							page = pages.selected_page
							UI.messagebox("Press 'OK' after #{page.name} is done loading.")

							folder=File.dirname(model.path)
							scene=model.pages.selected_page.name.gsub(/[^A-Za-z0-9_-]/,"_")
							file=model.title+"_"+scene+".#{params['sel_type'].to_s}"
							path=File.join(folder,file).tr("\\","/")

							keys = {
								   :filename => path,
								   :width => params['getwidth'].to_i,
								   :height => params['getheight'].to_i,
								   :antialias => @anti,
								   :compression => 0.9,
								   :transparent => @trans
								}

								Sketchup.active_model.active_view.write_image(keys)

						end

						UI.messagebox("All scenes have been successfully exported!")
						folder=File.dirname(model.path)
						UI.openURL(folder)

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

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

					end

				end

				########################################
				###Click Close##########################
				########################################
				if params['done'].to_s == "1"
					Sketchup.send_action('selectSelectionTool:') #Selection Tool
					@@export_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__ )