#!/usr/bin/perl
#package calculate_move;
use strict;
use diagnostics;
use warnings;
use feature "say";
use Tk;
use Graphics::Magick;
use Image::Imlib2;
#use Cwd qw(getcwd);
use Tk::FileSelect;
use Text::CSV qw(csv);
use File::Copy;
use Data::Dumper qw(Dumper);
use Math::Trig;
use Math::Round;
use Clone qw(clone);

#my $cwd  = getcwd($<);


my ($mw5,@reel,@front,@back,@grid,$csv);
my $msg="hello";
my $num=0;
#my %cam;
#$cam{level_name}="none"; 
#$cam{level}=0;
my %cam=(
scene_name=>"new",
front_f=>0,
back_f=>0,
start_x=>0,
end_x=>0,
start_y=>0,
end_y=>0,
start_w=>0,
end_w=>0,
frames=>0,
proportional=>"yes",
prop=>0,
canvas_aspect=>0.5625,
object_aspect=>0.5625,
canvas_w=>3840,
canvas_h=>2160,
level_name=>"none",
level=>"1",
);
#$cam{saved_prop}=0;
#$cam{saved_inc}=0;
$cam{canvas_h}=$cam{canvas_w}*$cam{canvas_aspect};
my ($distfair,$distx,$disty,$distw,$prop,$incw,$incx,$incy,$middle,$frac)=0;
my $done="no";

#======================================#
#sub run{
$mw5 = MainWindow->new(-title => "NEW");

my $font = $mw5->fontCreate(
  -size => 14,
  -weight => "normal");
my $font2 = $mw5->fontCreate(
-size => 20,
-weight => "normal");
my $w=$mw5->screenwidth;
my $h=$mw5->screenheight;
my $w_unit=int($w*.05);my $h_unit=int($h*.05);
$mw5->geometry(($w_unit*6)."x".($h_unit*10)."+".($w_unit*7)."+".($h_unit*5));
$mw5->Label(-font => $font2, -text => "MOVE DATA")->grid(qw/ -row 1 -column 1 -sticky w/);
$mw5->Label(-font => $font, -text => "Scene  Name")->grid(qw/ -row 2 -column 0 -sticky w/);
my $entry = $mw5->Entry(-font => $font, -textvariable => \$cam{scene_name})->grid(qw/ -row 2 -column 1 -sticky w/);
$mw5->Label(-font => $font, -text => "Level")->grid(qw/ -row 3 -column 0 -sticky w/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{level})->grid(qw/ -row 3 -column 1 -sticky w/);
$entry = $mw5->Button(-font => $font, -text => '+', -command => sub{$cam{level}=$cam{level}+1;})->grid(qw/ -row 3 -column 2 -sticky w/);
$entry = $mw5->Button(-font => $font, -text => '-', -command => sub{$cam{level}=$cam{level}-1;if($cam{level}<1){$cam{level}=0}})->grid(qw/ -row 3 -column 2 -sticky e/);

$mw5->Label(-font => $font, -text => "Level Name")->grid(qw/ -row 4 -column 0 -sticky w/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{level_name})->grid(qw/ -row 4 -column 1 -sticky w/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{level_name}="none";})->grid(qw/ -row 4 -column 2 -sticky nw/);

$entry = $mw5->Label(-font => $font, -textvariable => \" ")->grid(qw/ -row 5 -column 2 -sticky nw/);
$mw5->Label(-font => $font, -text => "Canvas width pixels")->grid(qw/ -row 6 -column 0 -sticky w/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{canvas_w})->grid(qw/ -row 6 -column 1 -sticky w/);
$mw5->Label(-font => $font, -text => "Canvas aspect ratio ")->grid(qw/ -row 7 -column 0 -sticky w/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{canvas_aspect})->grid(qw/ -row 7 -column 1 -sticky w/);
$mw5->Label(-font => $font, -text => "Object aspect ratio ")->grid(qw/ -row 10 -column 0 -sticky w/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{object_aspect})->grid(qw/ -row 10 -column 1 -sticky w/);
$entry = $mw5->Button(-font => $font, -text => 'EQU', -command =>sub{$cam{object_aspect}=$cam{canvas_aspect}})->grid(qw/ -row 10 -column 2 -sticky nw/);
$mw5->Label(-font => $font, -text => "Move frames")->grid(qw/ -row 22 -column 0 -sticky w/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{frames})->grid(qw/ -row 22 -column 1 -sticky w/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{frames}=0;})->grid(qw/ -row 22 -column 2 -sticky nw/);

$mw5->Label(-font => $font, -text => "Proportional Zoom")->grid(qw/ -row 21 -column 0 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -state => 'disabled',  -textvariable => \$cam{proportional})->grid(qw/ -row 21 -column 1 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'Y/N', -command =>sub{if($cam{proportional}eq"yes"){$cam{proportional}="no";}else{$cam{proportional}="yes";}if($cam{start_w}==$cam{end_w}){$cam{proportional}="no";}       })->grid(qw/ -row 21 -column 2 -sticky nw/);
$mw5->Label(-font => $font, -text => "Move start end width")->grid(qw/ -row 24 -column 0 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{start_w})->grid(qw/ -row 24 -column 1 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{end_w})->grid(qw/ -row 24 -column 3 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'SWP', -command => sub{($cam{start_w},$cam{end_w})=($cam{end_w},$cam{start_w});})->grid(qw/ -row 24  -column 2 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{start_w}=0;$cam{end_w}=0;})->grid(qw/ -row 24 -column 4 -sticky nw/);
$mw5->Label(-font => $font, -text => "Move: front back fairings")->grid(qw/ -row 36 -column 0 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{front_f})->grid(qw/ -row 36 -column 1 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{back_f})->grid(qw/ -row 36 -column 3 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'SWP', -command => sub{($cam{back_f},$cam{front_f})=($cam{front_f},$cam{back_f});})->grid(qw/ -row 36 -column 2 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{back_f}=0;$cam{front_f}=0;})->grid(qw/ -row 36 -column 4 -sticky nw/);
$mw5->Label(-font => $font, -text => "centre start/end x")->grid(qw/ -row 30 -column 0 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{start_x})->grid(qw/ -row 30 -column 1 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{end_x})->grid(qw/ -row 30 -column 3 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'SWP', -command => sub{($cam{start_x},$cam{end_x})=($cam{end_x},$cam{start_x});})->grid(qw/ -row 30  -column 2 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{end_x}=0;$cam{start_x}=0;})->grid(qw/ -row 30 -column 4 -sticky nw/);
$mw5->Label(-font => $font, -text => "centre start/end y")->grid(qw/ -row 34 -column 0 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{start_y})->grid(qw/ -row 34 -column 1 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{end_y})->grid(qw/ -row 34 -column 3 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'SWP', -command => sub{($cam{start_y},$cam{end_y})=($cam{end_y},$cam{start_y});})->grid(qw/ -row 34  -column 2 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{end_y}=0;$cam{start_y}=0;})->grid(qw/ -row 34 -column 4 -sticky nw/);
$mw5->Label(-font => $font, -text => "proportion")->grid(qw/-row 40 -column 0 -sticky nw/);
$entry = $mw5->Entry(-font => $font, -textvariable => \$cam{prop})->grid(qw/ -row 40 -column 1 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text => 'CLR', -command => sub{$cam{prop}=0})->grid(qw/ -row 40 -column 2 -sticky nw/);
$entry = $mw5->Button(-font => $font, -text =>"CALCULATE", -command => sub{calculate($cam{prop},$cam{start_w},$cam{end_w},$cam{frames})})->grid(qw/ -row 40 -column 3 -sticky w/ );
$entry = $mw5->Button(-font => $font, -text =>"LOAD", -command => sub{load_camera($mw5)})->grid(qw/ -row 50 -column 0 -sticky w/ );
$entry = $mw5->Button(-font => $font, -text =>"SAVE", -command => sub{save_camera($mw5)})->grid(qw/ -row 50 -column 0 -sticky n/ );
#$entry = $mw5->Button(-font => $font, -text =>"FILL MOVE DATA", -command => sub{&go_cam;$mw5->update;})->grid(qw/ -row 45 -column 0 -sticky w/ );
$entry = $mw5->Button(-font => $font, -text =>"MAKE MOVE GUIDE", -command => sub{&go_cam;&make_grid})->grid(qw/ -row 45 -column 1 -sticky w/ );
$entry = $mw5->Button(-font => $font, -text =>"WRITE FRAMES DATA", -command => sub{&go_cam;&write_data})->grid(qw/ -row 45 -column 0 -sticky w/ );
$entry = $mw5->Button(-font => $font, -text =>"CLOSE", -command => sub{$mw5->destroy;})->grid(qw/ -row 50 -column 1 -sticky w/);
$entry = $mw5->Label(-font => $font, -textvariable => \" ")->grid(qw/ -row 102 -column 2 -sticky nw/);

$entry = $mw5->Label(-font => $font2, -textvariable => \$msg )->grid(qw/ -row 104 -column 0 -columnspan 3 -sticky w/);
MainLoop;

sub go_cam{
$cam{canvas_h}=$cam{canvas_w}*$cam{canvas_aspect};
if ($cam{proportional} eq"yes" and ($cam{start_w}==$cam{end_w})){
$msg=" cannot make proportional\n if start width and end width the same";
say $msg;
$cam{proportional} = "no";
return($cam{proportional});
}
if ($cam{proportional}eq "yes" and $cam{start_w} != $cam{end_w}){
&calculate;
say $cam{prop};
&go_move;

for(my $i=0;$i<=$#reel;$i++){
$reel[$i]{x}=round($reel[$i]{x});
$reel[$i]{y}=round($reel[$i]{y});
$reel[$i]{w}=round($reel[$i]{w});
$reel[$i]{h}=round($reel[$i]{w}*$cam{object_aspect});
#say " w ".$reel[$i]{w}." h ".$reel[$i]{h}." x ".$reel[$i]{x}." y ".$reel[$i]{y};
}
#return;
}
if ($cam{proportional} eq "no"){
&go_move;
for(my $i=0;$i<=$#reel;$i++){
$reel[$i]{x}=round($reel[$i]{x});
$reel[$i]{y}=round($reel[$i]{y});
$reel[$i]{w}=round($reel[$i]{w});
$reel[$i]{h}=round($reel[$i]{w}*$cam{object_aspect});#added here
#say " w ".$reel[$i]{w}." h ".$reel[$i]{h}." x ".$reel[$i]{x}." y ".$reel[$i]{y};
}
}
return;
}

sub go_move{
@reel=();
@front=();
@back=();
$distx=($cam{end_x}-$cam{start_x});
$disty=($cam{end_y}-$cam{start_y});
$distw=($cam{end_w}-$cam{start_w});
$incw=abs($cam{start_w}-$cam{end_w})/$cam{frames};
$incx=abs($cam{start_x}-$cam{end_x})/$cam{frames};
$incy=abs($cam{start_y}-$cam{end_y})/$cam{frames};
for (my $i=0;$i<=$cam{frames};$i++){
# 
$reel[$i]{w}=$cam{start_w};
$reel[$i]{x}=$cam{start_x};
$reel[$i]{y}=$cam{start_y};
$reel[$i]{h}=round($reel[$i]{w}*$cam{object_aspect});
}
if(!$cam{front_f} and !$cam{back_f}){
&no_fairings}

if($cam{front_f}){
&go_front;
@front=@{clone\@reel};
}

if($cam{back_f}){
$distfair=$cam{frames}-$cam{back_f};
($cam{start_w},$cam{end_w})=($cam{end_w},$cam{start_w});
($cam{start_x},$cam{end_x})=($cam{end_x},$cam{start_x});
($cam{start_y},$cam{end_y})=($cam{end_y},$cam{start_y});
($cam{front_f},$cam{back_f})=($cam{back_f},$cam{front_f});
&go_front;
my $ct=$cam{frames};
for(my $i=0;$i<=$cam{frames};$i++){
$back[$i]{w}=$reel[$ct-$i]{w};
$back[$i]{x}=$reel[$ct-$i]{x};
$back[$i]{y}=$reel[$ct-$i]{y};
}
($cam{start_w},$cam{end_w})=($cam{end_w},$cam{start_w});
($cam{start_x},$cam{end_x})=($cam{end_x},$cam{start_x});
($cam{start_y},$cam{end_y})=($cam{end_y},$cam{start_y});
($cam{front_f},$cam{back_f})=($cam{back_f},$cam{front_f});
if(!$cam{front_fairings}){
@reel=@{clone\@back};
}
}
if($cam{front_f} and $cam{back_f}){
&go_both;

}

}
sub no_fairings{
say " no fairings";
for (my $i=0;$i<=$cam{frames};$i++){
if($cam{start_w}<$cam{end_w} and $cam{proportional} eq "yes"){
#say "smaller";
$reel[$i]{w}=int(sqrt(($cam{start_w}**2)/((1-$cam{prop})**$i)));
$reel[$i]{x}=$cam{start_x}+($distx/$distw)*(($reel[$i]{w}-$cam{start_w}));
$reel[$i]{y}=$cam{start_y}+($disty/$distw)*(($reel[$i]{w}-$cam{start_w}));
}

if($cam{start_w}>$cam{end_w}and $cam{proportional} eq "yes"){
#say "bigger";
$reel[$i]{w}=int(sqrt(($cam{start_w}**2)*((1-$cam{prop})**$i)));
$reel[$i]{x}=$cam{start_x}+($distx/$distw)*(($reel[$i]{w}-$cam{start_w}));
$reel[$i]{y}=$cam{start_y}+($disty/$distw)*(($reel[$i]{w}-$cam{start_w}));
}
# if start_w == end_w do x and y incrementally w = start_w =below
if ($cam{proportional}eq"no"and $cam{start_w}>$cam{end_w}){
$reel[$i]{w}=$cam{start_w}-($incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_w}<$cam{end_w}){
$reel[$i]{w}=$cam{start_w}+($incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_x}>$cam{end_x}){
$reel[$i]{x}=$cam{start_x}-($incx*$i);}
if ($cam{proportional}eq"no"and $cam{start_x}<$cam{end_x}){
$reel[$i]{x}=$cam{start_x}+($incx*$i);}
if ($cam{proportional}eq"no"and $cam{start_y}>$cam{end_y}){
$reel[$i]{y}=$cam{start_y}-($incy*$i);}
if ($cam{proportional}eq"no"and $cam{start_y}<$cam{end_y}){
$reel[$i]{y}=$cam{start_y}+($incy*$i);}

if ($cam{proportional}eq"no"and $cam{start_w}==$cam{end_w}){
$reel[$i]{w}=$cam{start_w};}
if ($cam{proportional}eq"no"and $cam{start_x}==$cam{end_x}){
$reel[$i]{x}=$cam{start_x};}
if ($cam{proportional}eq"no"and $cam{start_y}==$cam{end_y}){
$reel[$i]{y}=$cam{start_y};}
#say "incw ".$incw." ". $reel[$i]{w};
}
}

sub go_front{
say "front fairings";
$distfair=$cam{frames}-$cam{front_f};
for (my $i=0;$i<=$cam{front_f}*(pi/2);$i++){


$frac=1-cos(deg2rad(($i/($cam{front_f}*(pi/2)))*90));
$incw=abs($cam{start_w}-$cam{end_w})/$cam{frames};
$incx=abs($cam{start_x}-$cam{end_x})/$cam{frames};
$incy=abs($cam{start_y}-$cam{end_y})/$cam{frames};

if($cam{start_w}<$cam{end_w}and $cam{proportional}eq"yes"){
$reel[$i]{w}=int(sqrt(($cam{start_w}**2)/((1-($cam{prop}*$frac))**$i)));
$reel[$i]{x}=$cam{start_x}+($distx/$distw)*(($reel[$i]{w}-$cam{start_w}));
$reel[$i]{y}=$cam{start_y}+($disty/$distw)*(($reel[$i]{w}-$cam{start_w}));
}
elsif($cam{start_w}>$cam{end_w}and $cam{proportional}eq "yes"){
$reel[$i]{w}=int(sqrt(($cam{start_w}**2)*((1-($cam{prop}*$frac))**$i)));
$reel[$i]{x}=$cam{start_x}+($distx/$distw)*(($reel[$i]{w}-$cam{start_w}));
$reel[$i]{y}=$cam{start_y}+($disty/$distw)*(($reel[$i]{w}-$cam{start_w}));
}
##########
if ($cam{proportional}eq"no"and $cam{start_w}>$cam{end_w}){
$reel[$i]{w}=$cam{start_w}-($frac*$incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_w}<$cam{end_w}){
$reel[$i]{w}=$cam{start_w}+($frac*$incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_x}>$cam{end_x}){
$reel[$i]{x}=$cam{start_x}-($frac*$incx*$i);}
if ($cam{proportional}eq"no"and $cam{start_x}<$cam{end_x}){
$reel[$i]{x}=$cam{start_x}+($frac*$incx*$i);}
if ($cam{proportional}eq"no"and $cam{start_y}>$cam{end_y}){
$reel[$i]{y}=$cam{start_y}-($frac*$incy*$i);}
if ($cam{proportional}eq"no"and $cam{start_y}<$cam{end_y}){
$reel[$i]{y}=$cam{start_y}+($frac*$incy*$i);}


############
#say "frac ". $frac." w ". $reel[$i]{w}. " incw =".$incw*$frac*$i;
}
$distw=($cam{end_w}-$reel[$cam{front_f}]{w});
$distx=($cam{end_x}-$reel[$cam{front_f}]{x});
$disty=($cam{end_y}-$reel[$cam{front_f}]{y});

my $prop2=1-(($reel[$cam{front_f}]{w}**2)/($cam{end_w}**2))**(1/$distfair);
$incw=abs($cam{end_w}-$reel[$cam{front_f}]{w})/($cam{frames}-$cam{front_f});
$incx=abs($cam{end_x}-$reel[$cam{front_f}]{x})/($cam{frames}-$cam{front_f});
$incy=abs($cam{end_y}-$reel[$cam{front_f}]{y})/($cam{frames}-$cam{front_f});
#say " distw ". $distw." incw ".$incw;
for (my $i=0;$i<=$distfair;$i++){
#say "  incw ". $incw;
if($cam{start_w}<$cam{end_w} and $cam{proportional}eq "yes"){
$reel[$cam{front_f}+$i]{w}=int(sqrt(($reel[$cam{front_f}]{w}**2)/((1-$prop2)**$i)));
$reel[$cam{front_f}+$i]{x}=$reel[$cam{front_f}]{x}+($distx/$distw)*(($reel[$cam{front_f}+$i]{w}-$reel[$cam{front_f}]{w}));
$reel[$cam{front_f}+$i]{y}=$reel[$cam{front_f}]{y}+($disty/$distw)*(($reel[$cam{front_f}+$i]{w}-$reel[$cam{front_f}]{w}));
}
elsif($cam{start_w}>$cam{end_w}and $cam{proportional}eq "yes"){
$prop2=1-(($cam{end_w}**2)/($reel[$cam{front_f}]{w}**2))**(1/$distfair);
$reel[$cam{front_f}+$i]{w}=int(sqrt(($reel[$cam{front_f}]{w}**2)*((1-$prop2)**$i)));
$reel[$cam{front_f}+$i]{x}=$reel[$cam{front_f}]{x}+($distx/$distw)*(($reel[$cam{front_f}+$i]{w}-$reel[$cam{front_f}]{w}));
$reel[$cam{front_f}+$i]{y}=$reel[$cam{front_f}]{y}+($disty/$distw)*(($reel[$cam{front_f}+$i]{w}-$reel[$cam{front_f}]{w}));
}

if ($cam{proportional}eq"no"and $cam{start_w}>$cam{end_w}){
$reel[$cam{front_f}+$i]{w}=$reel[$cam{front_f}]{w}-($incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_w}<$cam{end_w}){
$reel[$cam{front_f}+$i]{w}=$reel[$cam{front_f}]{w}+($incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_x}>$cam{end_x}){
$reel[$cam{front_f}+$i]{x}=$reel[$cam{front_f}]{x}-($incx*$i);}
if ($cam{proportional}eq"no"and $cam{start_x}<$cam{end_x}){
$reel[$cam{front_f}+$i]{x}=$reel[$cam{front_f}]{x}+($incx*$i);}
if ($cam{proportional}eq"no"and $cam{start_y}>$cam{end_y}){
$reel[$cam{front_f}+$i]{y}=$reel[$cam{front_f}]{y}-($incy*$i);}
if ($cam{proportional}eq"no"and $cam{start_y}<$cam{end_y}){
$reel[$cam{front_f}+$i]{y}=$reel[$cam{front_f}]{y}+($incy*$i);}

if ($cam{proportional}eq"no"and $cam{start_w}==$cam{end_w}){
$reel[$i]{w}=$cam{start_w};}
if ($cam{proportional}eq"no"and $cam{start_x}==$cam{end_x}){
$reel[$i]{x}=$cam{start_x};}
if ($cam{proportional}eq"no"and $cam{start_y}==$cam{end_y}){
$reel[$i]{y}=$cam{start_y};}
####################################

}

}

sub go_both{
say "both";
for (my $i=0;$i<=$#front;$i++){
$reel[$i]{w}=$front[$i]{w};
$reel[$i]{x}=$front[$i]{x};
$reel[$i]{y}=$front[$i]{y};
#say " front is ".$reel[$i]{w};
}
for (my $i=0;$i<=$cam{back_f};$i++){
$reel[$#reel-$i]{w}=$back[$#reel-$i]{w};
$reel[$#reel-$i]{x}=$back[$#reel-$i]{x};
$reel[$#reel-$i]{y}=$back[$#reel-$i]{y};
}

my $frames=$cam{frames}-$cam{back_f}-$cam{front_f};
$distx=$reel[$cam{frames}-$cam{back_f}]{x}-$reel[$cam{front_f}]{x};
$disty=$reel[$cam{frames}-$cam{back_f}]{y}-$reel[$cam{front_f}]{y};
$distw=$reel[$cam{frames}-$cam{back_f}]{w}-$reel[$cam{front_f}]{w};
if ($cam{start_w}<$cam{end_w}){
$prop=1-(($reel[$cam{front_f}]{w}**2)/($reel[$cam{frames}-$cam{back_f}]{w}**2))**(1/$frames);
}
elsif($cam{start_w}>$cam{end_w} ){
#$prop=1-(($reel[$cam{front_f}]{w}**2)*($reel[$cam{frames}-$cam{back_f}]{w}**2))**(1/$frames);
$prop=1-(($reel[$cam{frames}-$cam{back_f}]{w}**2) /($reel[$cam{front_f}]{w}**2) )**(1/$frames);

}
#say "prop is  ".$prop;
$incw=abs($distw)/$frames;
$incx=abs($distx)/$frames;
$incy=abs($disty)/$frames;
#my $prop2=1-(($reel[$cam{front_f}]{w}**2)/($cam{end_w}**2))**(1/$distfair);
#######################
for (my $i=0;$i<=$frames;$i++){
if($cam{start_w}<$cam{end_w} and $cam{proportional} eq "yes"){
say "smaller";
$reel[$i+$cam{front_f}]{w}=int(sqrt(($reel[$cam{front_f}]{w}**2)/((1-$prop)**$i)));
$reel[$i+$cam{front_f}]{x}=$reel[$cam{front_f}]{x}+($distx/$distw)*(($reel[$i+$cam{front_f}]{w}-$reel[$cam{front_f}]{w}));
$reel[$i+$cam{front_f}]{y}=$reel[$cam{front_f}]{y}+($disty/$distw)*(($reel[$i+$cam{front_f}]{w}-$reel[$cam{front_f}]{w}));
}
##########
#       $reel[$i]{x}=$cam{start_x}+($distx/$distw)*(($reel[$i]{w}-$cam{start_w}));
############
if($cam{start_w}>$cam{end_w}and $cam{proportional} eq "yes"){
#say "bigger";
$reel[$i+$cam{front_f}]{w}=int(sqrt(($reel[$cam{front_f}]{w}**2)*((1-$prop)**$i)));
$reel[$i+$cam{front_f}]{x}=$reel[$cam{front_f}]{x}+($distx/$distw)*(($reel[$i+$cam{front_f}]{w}-$reel[$cam{front_f}]{w}));
$reel[$i+$cam{front_f}]{y}=$reel[$cam{front_f}]{y}+($disty/$distw)*(($reel[$i+$cam{front_f}]{w}-$reel[$cam{front_f}]{w}));

}
# if start_w == end_w do x and y incrementally w = start_w =below
if ($cam{proportional}eq"no"and $cam{start_w}>$cam{end_w}){
$reel[$i+$cam{front_f}]{w}=$reel[$cam{front_f}]{w}-($incw*$i);}
if ($cam{proportional}eq"no"and $cam{start_w}<$cam{end_w}){
$reel[$i+$cam{front_f}]{w}=$reel[$cam{front_f}]{w}+($incw*$i);}

if ($cam{proportional}eq"no"and $cam{start_x}>$cam{end_x}){
$reel[$i+$cam{front_f}]{x}=$reel[$cam{front_f}]{x}-($incx*$i);}

if ($cam{proportional}eq"no"and $cam{start_x}<$cam{end_x}){
$reel[$i+$cam{front_f}]{x}=$reel[$cam{front_f}]{x}+($incx*$i);}

if ($cam{proportional}eq"no"and $cam{start_y}>$cam{end_y}){
$reel[$i+$cam{front_f}]{y}=$reel[$cam{front_f}]{y}-($incy*$i);}

if ($cam{proportional}eq"no"and $cam{start_y}<$cam{end_y}){
$reel[$i+$cam{front_f}]{y}=$reel[$cam{front_f}]{y}+($incy*$i);}


if ($cam{proportional}eq"no"and $cam{start_w}==$cam{end_w}){
$reel[$i+$cam{front_f}]{w}=$cam{start_w};}
if ($cam{proportional}eq"no"and $cam{start_x}==$cam{end_x}){
$reel[$i+$cam{front_f}]{x}=$cam{start_x};}
if ($cam{proportional}eq"no"and $cam{start_y}==$cam{end_y}){
$reel[$i+$cam{front_f}]{y}=$cam{start_y};}
#say "incw ".$incw." ". $reel[$i]{w};
}

#######################
}


# do the sums ------------#
sub calculate{
say "here calculate";
if($cam{frames} and $cam{end_w} and $cam{start_w}){$cam{prop}=0;}

if (!$cam{end_w}){
$cam{end_w}=sqrt(($cam{start_w}**2)*((1-$cam{prop})**$cam{frames}));
}
if (!$cam{prop} and $cam{start_w}<$cam{end_w}){
$cam{prop}=1-(($cam{start_w}**2)/($cam{end_w}**2))**(1/$cam{frames});
}
elsif(!$cam{prop} and $cam{start_w}>$cam{end_w} ){
$cam{prop}=1-(($cam{end_w}**2)/($cam{start_w}**2))**(1/$cam{frames});
}
if (!$cam{start_w}){
$cam{start_w}=sqrt(($cam{end_w}**2)/((1-$cam{prop})**$cam{frames}));
}
if (!$cam{frames}){
$cam{frames}=2*(log($cam{end_w}/$cam{start_w}))/(log(1-$cam{prop}));
}
$msg="Calculated proportion";
say $msg;
return($msg);
}
#---------------------##------------------------------#
sub make_grid{
&make_fill;
say "CANVAS  h- $cam{canvas_h}  w- $cam{canvas_w}";
my $bg_image='xc:white';
#my $bg_image="./images/grid_centre_3840.png";


say "here make_grid ";
my $bg=Graphics::Magick->new;
#my $bg=Image::Imlib2->new("$cam{canvas_w}"."x"."$cam{canvas_h}");
$bg->Set(size=>"$cam{canvas_w}"."x"."$cam{canvas_h}");
$bg->ReadImage($bg_image);
#$bg->ReadImage("images/grid_centre_3840.png");
#$bg->ReadImage("100.png");

# change centre numbers to top left
for (my $i=0;$i<=$cam{frames};$i++){
#$grid[$i]{x}=round($reel[$i]{x}+(($cam{canvas_w}-$reel[$i]{w})/2));
#$grid[$i]{y}=round($reel[$i]{y}+(($cam{canvas_h}-$reel[$i]{h})/2));
$grid[$i]{x}=round((($cam{canvas_w}/2)+$reel[$i]{x})-($reel[$i]{w}/2));
$grid[$i]{y}=round((($cam{canvas_h}/2)+$reel[$i]{y})-($reel[$i]{h}/2));

$grid[$i]{w}=round($reel[$i]{w});
$grid[$i]{h}=round($reel[$i]{h});
my $x2=$grid[$i]{x}+$grid[$i]{w};
my $y2=$grid[$i]{y}+$grid[$i]{h};
$bg->Draw(primitive=>'rectangle',stroke=>'red',points=>"$grid[$i]{x},$grid[$i]{y} $x2,$y2",fill=>'false');
}

my $filename = "$cam{scene_name}_$cam{level}"."_grid.png";
$bg->Write(filename=>$filename);
$bg=undef;
$msg="made move guide image\n$cam{scene_name}_$cam{level}.png";
say $msg;
#say "done it";
#say " (start-W)".$cam{start_w}." (end-W)".$cam{end_w}." (start-X)".$cam{start_x}." (end-X)".$cam{end_x}." (start-Y)".$cam{start_y}." (end-Y)". $cam{end_y}." (front-f)". $cam{front_f}." (back-f)".$cam{back_f}; 

return();
}
sub make_fill{
say "here make_fill";
for(my $i=0;$i<=$cam{frames};$i++){
# see numbers centered, rounded and add {h}
$reel[$i]{x}=round($reel[$i]{x});
$reel[$i]{y}=round($reel[$i]{y});
$reel[$i]{w}=round($reel[$i]{w});
$reel[$i]{h}=round($reel[$i]{w}*$cam{object_aspect});
say $i." w ".$reel[$i]{w}." h ".$reel[$i]{h}." x ".$reel[$i]{x}." y ".$reel[$i]{y};
}
$msg="filled frames data";
say $msg;
return;
}

#---------------------#
sub save_camera{
say "here make_camera";
if(!-d "$cam{scene_name}"){
mkdir $cam{scene_name};
	}

if (-f "$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.data"){
copy ("$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.data", 
"$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.last");
}
open( my $fh, '>', "$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.data") or die "$!";
print $fh
"frames,$cam{frames},prop,$cam{prop},start_w,$cam{start_w},end_w,$cam{end_w},front_f,$cam{front_f},back_f,$cam{back_f},start_x,$cam{start_x},end_x,$cam{end_x},start_y,$cam{start_y},end_y,$cam{end_y},proportional,$cam{proportional},canvas_w,$cam{canvas_w},canvas_aspect,$cam{canvas_aspect},canvas_h,$cam{canvas_h},level,$cam{level},scene_name,$cam{scene_name},object_aspect,$cam{object_aspect},level_name,$cam{level_name}";
close $fh;
$msg= "Saved $cam{scene_name}_$cam{level}_$cam{level_name}.data";
say $msg;
return;
}
#---------------------#
sub load_camera{
say " here load_camera";
my $start_dir="old";
my %tmp;
my $types =[
['saved', ['.data','.last']],
];
my $file=$mw5->getOpenFile(-filetypes=>$types);
if($file){
my $csv = Text::CSV->new ();
open ( my $fh , '<',"$file")or die"$!";
my $line = <$fh> ;
$csv->parse($line);
%tmp=$csv->fields();
close $file;
$csv=undef;
}
$cam{front_f}=$tmp{front_f};
$cam{back_f}=$tmp{back_f};
$cam{start_x}=$tmp{start_x};
$cam{end_x}=$tmp{end_x};
$cam{start_y}=$tmp{start_y};
$cam{end_y}=$tmp{end_y};
$cam{start_w}=$tmp{start_w};
$cam{end_w}=$tmp{end_w};
$cam{frames}=$tmp{frames};
$cam{proportional}=$tmp{proportional};
$cam{prop}=$tmp{prop};
$cam{canvas_aspect}=$tmp{canvas_aspect};
$cam{object_aspect}=$tmp{object_aspect};
$cam{canvas_w}=$tmp{canvas_w};
$cam{canvas_h}=$tmp{canvas_h};
$cam{scene_name}=$tmp{scene_name};
$cam{level}=$tmp{level};
$cam{level_name}=$tmp{level_name};
#my $thiswin = $_[0];
#
#$mw5->destroy;
#&window;
say " canvas h $cam{canvas_h}";
return{%cam};
}
# switch the start/ end about.
sub switch{say "here switch";($cam{start_w},$cam{end_w})=($cam{end_w},$cam{start_w});return;}
sub swap_centres{
say "here swap_centres";
($cam{start_x},$cam{end_x})=($cam{end_x},$cam{start_x});
($cam{start_y},$cam{end_y})=($cam{end_y},$cam{start_y});
return;
}

sub write_data{
say "here write_data";
if (-d "$cam{scene_name}"){
$msg= "directory exists";
say $msg;
}
else{
mkdir $cam{scene_name};
$msg= "made directory $cam{scene_name}";
say $msg;
}
open( my $fh, '>',"$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.wxy") or die "$!";
open( my $fh2, '>',"$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.data") or die "$!";
#for(my $i=0;$i<=$cam{frames}-1;$i++){
#say Dumper @reel;

for(my $i=0;$i<=$cam{frames};$i++){

print $fh "w,$reel[$i]{w},h,$reel[$i]{h},x,$reel[$i]{x},y,$reel[$i]{y}\n";
#print $fh "$reel[$i]{w},$reel[$i]{h},$reel[$i]{x},$reel[$i]{y}\n";
}
print $fh2 "frames,$cam{frames},prop,$cam{prop},start_w,$cam{start_w},end_w,$cam{end_w},front_f,$cam{front_f},back_f,$cam{back_f},start_x,$cam{start_x},end_x,$cam{end_x},start_y,$cam{start_y},end_y,$cam{end_y},proportional,$cam{proportional},canvas_w,$cam{canvas_w},canvas_aspect,$cam{canvas_aspect},canvas_h,$cam{canvas_h},level,$cam{level},scene_name,$cam{scene_name},object_aspect,$cam{object_aspect},level_name,$cam{level_name}";
$msg="wrote to file\n$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.wxy\n
$cam{scene_name}/$cam{scene_name}_$cam{level}_$cam{level_name}.data";
say $msg;
close $fh;
close $fh2;
return; 
}
#}
#1;




