//
// Camera positions bookmarks 1.2
//
// Use [Ctrl]+[0], [1], ..., [9] to store cam positions
// Use [0], [1], ..., [9] to load stored camera positions
// Use [Shift]+[0], [1], ..., [9] to move smoothly to cam position
//   (will happen only during capturing)
//

// -- settings --
set cams_moveframes 50 // duration - how many frames will the movement take
// note: you set time here, not the distance nor the speed. distance is given by the two points of the movement, speed is given as distance/time (frames)

bind ctrl +cams_save_mode
bind shift +cams_move_mode


// -- print info --
echo "$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-"
echo "Camera bookmarks config"
echo "$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-$-"
echo "Use $[ctrl$]+$[num$] to save camera position, $[num$] to load it"
echo "Use $[shift$]+$[num$] to start camera movement to camera positions 1, 2, ..., num"

// -- code -- (don't touch this)

set cams_bindn 0
// in other words: for (i=0; i<10; i++) { bind i cams_go i }
alias cams_bindnext "cams_bindc; inc cams_bindn 1; if $cams_bindn < 10 cams_bindnext" 
alias cams_bindc "bind $cams_bindn cams_go $cams_bindn"
cams_bindnext

set cams_bindmode 0

alias +cams_save_mode "set cams_bindmode 1"
alias -cams_save_mode "set cams_bindmode 0"
alias +cams_move_mode "set cams_bindmode 2"
alias -cams_move_mode "set cams_bindmode 0"
 
alias cams_go "if $cams_bindmode == 0 cams_load %1 else if $cams_bindmode == 1 cams_save %1 else cams_move %1"
alias cams_load "cam_angles $cam_angles_saved%1; cam_pos $cam_pos_saved%1"
alias cams_save "set cam_angles_saved%1 $cam_angles; set cam_pos_saved%1 $cam_pos; cams_save_detailed %1"

// this saves each value to separated variable. it's being used in the move
alias cams_save_detailed "cams_save_detang %1; cams_save_detpos %1"
alias cams_save_detang "set cams_angles_saved_pitch%1 $cam_angles_pitch; set cams_angles_saved_yaw%1 $cam_angles_yaw; set cams_angles_saved_roll%1 $cam_angles_roll"
alias cams_save_detpos "set cams_pos_saved_x%1 $cam_pos_x; set cams_pos_saved_y%1 $cam_pos_y; set cams_pos_saved_z%1 $cam_pos_z"

// here we do the calculations for the smooth camera movement
// note: I really should have written this in TCL :)

alias cams_move "set cams_part_moves %1; set cams_part_curm 1; if $cams_part_moves >= 1 cams_move_partial $cams_part_curm"
alias cams_move_partial "cams_calc_steps %1; cams_store_alias; cams_replace_alias; set cams_cappedframes 0"
alias cams_store_alias "set_alias_str cams_stored_alias f_captureframe"
// alias cams_restore_alias "alias f_captureframe $cams_stored_alias"
alias cams_finish_move "inc cams_part_curm; if $cams_part_curm <= $cams_part_moves cams_move_partial $cams_part_curm else cams_restore_alias"
alias cams_restore_alias "alias f_captureframe" // yes, this will clear f_captureframe alias, not "clean" but better behavior than something else
alias cams_replace_alias "alias f_captureframe cams_moveframe"
alias cams_calc_steps "cams_calc_diffs %1; cams_calc_divs"
alias cams_calc_diffs "set_calc cams_step_x $cams_pos_saved_x%1 - $cam_pos_x; set_calc cams_step_y $cams_pos_saved_y%1 - $cam_pos_y; set_calc cams_step_z $cams_pos_saved_z%1 - $cam_pos_z; cams_calc_yaw_diff %1; set_calc cams_step_pitch $cams_angles_saved_pitch%1 - $cam_angles_pitch"

// will calculate the horizontal turn and check if it's bigger than 180 degrees
alias cams_calc_yaw_diff "set_calc cams_step_yaw $cams_angles_saved_yaw%1 - $cam_angles_yaw; if $cams_step_yaw > 180 cams_calc_yaw_complement; if $cams_step_yaw < -180 cams_calc_yaw_comp_neg" // get the shortest of the two possible ways: CW/CCW
// if the turn was bigger than 180 degrees, it will use 360 degrees complement
alias cams_calc_yaw_complement "set_calc cams_step_yaw $cams_step_yaw - 360"
alias cams_calc_yaw_comp_neg "set_calc cams_step_yaw $cams_step_yaw + 360"
alias cams_calc_divs "set_calc cams_step_x $cams_step_x / $cams_moveframes; set_calc cams_step_y $cams_step_y / $cams_moveframes; set_calc cams_step_z $cams_step_z / $cams_moveframes; set_calc cams_step_yaw $cams_step_yaw / $cams_moveframes; set_calc cams_step_pitch $cams_step_pitch / $cams_moveframes"
alias cams_moveframe "cams_move_pos; cams_move_ang; inc cams_cappedframes; if $cams_cappedframes >= $cams_moveframes cams_finish_move"
alias cams_move_pos "set_calc cams_tx $cam_pos_x + $cams_step_x; set_calc cams_ty $cam_pos_y + $cams_step_y; set_calc cams_tz $cam_pos_z + $cams_step_z; cam_pos $cams_tx $cams_ty $cams_tz"
alias cams_move_ang "set_calc cams_tw $cam_angles_yaw + $cams_step_yaw; set_calc cams_tp $cam_angles_pitch + $cams_step_pitch; cam_angles $cams_tp $cams_tw 0"


// 1) calculate 6 steps
// 2) save current f_captureframe alias
// 3) set new f_captureframe alias

alias cams_clear "unset_re cam_angles."
