
#Created by Dale Martens AKA 'Whaat'
#Released 6 Oct 2008 As-is and without warranty of any kind.

require 'sketchup.rb'


if( not file_loaded?("smoothStepAnim.rb") )

menu=UI.menu("Plugins")
menu.add_separator
menu.add_item("Export Smooth Animation") {(SmoothStepAnimation.new(true))}
menu.add_item("View Smooth Animation") {(SmoothStepAnimation.new(false))}


end
file_loaded("smoothstepAnim.rb")

class SmoothStepAnimation

def initialize(export)   #export should be true or false

@export=false
@delay=0.0

if export
	
	prompts=["Frame Rate","Width","Height","Format","Compression"]  
	values=["15.0","400","300","JPG","1.0"]
	format_choices="JPG|PNG"
	enums=[nil,nil,nil,format_choices,nil]
	results=UI.inputbox(prompts,values,enums,"Animation Options")
	if results
		@frame_rate=results[0].to_f
		@width=results[1].to_i
		@height=results[2].to_i
		@format=results[3]
		@comp=results[4].to_f
		if @comp<0.1 or @comp>1.0
			UI.messagebox("Compression must be between 0.1 and 1.0")
			@export=false
			return
		end
		@delay=1.0/@frame_rate
		def_filename="untitled."+@format.downcase
		path=UI.savepanel "Export Animation","",def_filename
		if path
			@export_path=File.dirname(path)
			@filename=File.basename(path,".*")   #remove the file extension
			@export=true
		end
	end
	
end


@model=Sketchup.active_model
@view=@model.active_view
@pages=@model.pages
@total_time=@pages.slideshow_time
@t0=Time.new  #will hold the start time
@t=0.0   #will hold the current time
@num_pages=@pages.count
@frame=0  
@def_trans_time=@model.options["PageOptions"]["TransitionTime"]
@current_page_index=0
@current_page=@pages[@current_page_index]
@current_page_start=0.0
@current_page_trans=@current_page.transition_time
@current_page_trans=@def_trans_time if @current_page_trans==-1.0
@current_page_end=@current_page_start+@current_page_trans

valid_delay_time=self.validate_page_delay(@pages)
if not valid_delay_time
	UI.messagebox("Please set all scene delay times to 0.0")
	return
end 
Sketchup.active_model.active_view.animation=self  #start the animation
	

end

#######################
def nextFrame(view)

return false if @current_page_index==@pages.count
if @export
	@t=@t+@delay
else
	@t=(Time.new-@t0).to_f
end

if @t.to_f>=@current_page_end   		#move to the next page if we are at the end of the transition time
	@current_page_index+=1
	@current_page=@pages[@current_page_index]
	@current_page_start=@t.to_f
	@current_page_trans=@current_page.transition_time
	@current_page_trans=@def_trans_time if @current_page_trans==-1.0
	@current_page_end=@current_page_start+@current_page_trans
end


smoothed_time=smooth(@current_page_start,@current_page_end,@t)*(@current_page_end-@current_page_start)+@current_page_start
@pages.show_frame_at(smoothed_time)

if @export
	@image_path=File.join(@export_path,@filename+"#{@frame}"+".#{@format.downcase}")
	Sketchup.set_status_text("Exporting frame #{@frame} to #{@image_path}")
	begin
		view.write_image(@image_path,@width,@height,true,@comp)
	rescue
		UI.messagebox("Error exporting animation frame.  Check animation parameters and retry.")
		raise
	end
	@frame+=1
end


#view.show_frame(@delay)
view.show_frame()


end

#####
def stop

#p "animation stopped by SU"

end

def pause

#p "animation paused by SU"

end
######################This method is not used
def export_animation()


#export the first page
view.write_image(filename,400,300,true)

(num_pages-1).times {|i|
	page=pages[i+1]  #the current page
	start_time=t
	trans_time=page.transition_time
	trans_time=def_trans_time if trans_time==-1.0
	end_time=start_time+trans_time
	p "page #{i} - start time #{start_time} - transition time #{trans_time} - end time #{end_time}"
	num_frames=(trans_time*frame_rate).abs.to_i
	time_step=1.0/frame_rate
	p "time step = #{time_step}"

	num_frames.times {|j|
		filename=File.join(File.dirname(filename),"test#{frame}.jpg")
		smoothed_time=smooth(start_time,end_time,t)*(end_time-start_time)+start_time
		pages.show_frame_at(smoothed_time)
		p "writing file #{filename} - t=#{t} - time=#{smoothed_time}"
		view.write_image(filename,400,300,true)
		#view.invalidate
		#sleep(time_step)		
		frame+=1
		t+=time_step
	}
}

UI.messagebox("export complete")
UI.openURL(path)


end

######################
def smooth(min,max,input) 

r=(input.to_f-min.to_f)/(max.to_f-min.to_f)
return r*r*(3.0-2.0*r)

end

#####################
def validate_page_delay(pages)

def_delay_time=Sketchup.active_model.options["SlideshowOptions"]["SlideTime"]
valid=true
pages.each {|page|
	if page.delay_time==-1
		valid=false if def_delay_time.abs>0.0
	elsif page.delay_time.abs>0.0 
		valid=false
	end
}

return valid

end


end #class