#KanDeLa.
#Copyright (C) 2011 Alain Barbason
#
#This file is part of KanDeLa.
#
#KanDeLa is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#KanDeLa is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with KanDeLa.  If not, see <http://www.gnu.org/licenses/>.
#
# todo mark a twist as transition (obligatory) or opprortunity
use strict;
use warnings;
use vars qw($sf_trait $view);
my $sf_trait;
package Trait;
use Commun;
our @ISA=qw( Commun );
use Data::Dumper;
our %liste;
sub getListe {
    my $self=shift;
    return %liste;
}
sub new {
    my $classe=shift;
    my $self=$classe->SUPER::new();
    my %param=@_;
    if ($param{"-id"}) {
        $self->{ID}=$param{"-id"};
        Commun::set_id($self->{ID});
    }
    $self->{I}=$param{"-i"};
    $self->{J}=$param{"-j"};
    my $angle=$self->{ANGLE}=$param{"-angle"};
    $self->{FILL}="red";
    $self->{SELECT}=0;
    bless($self, $classe);
    $liste{$self->id}=$self;
    $self->{LINE}=$::drawing->createLine(
        Commun::rot3($self->{J}, $self->{I}, $angle, -5, 0, +5, 0),
        -width => 3,
        -fill  => "red",
    );
    $::liste{$self->{LINE}}=$self;
    $self->undoDelete;
    #print Dumper $self;
    return $self;
}
sub i {
    my $self=shift;
    if (@_) {$self->{I}=shift}
    return $self->{I};
}
sub j {
    my $self=shift;
    if (@_) {$self->{J}=shift}
    return $self->{J};
}
sub angle {
    my $self=shift;
    if (@_) {$self->{ANGLE}=shift}
    return $self->{ANGLE};
}
sub configure {
    my $self=shift;
    my $key=uc(substr(shift, 1));
    $self->{$key}=shift;
}
sub select {
    my $self=shift;
    return $self->{SELECT};
}
sub pique {
    my $self=shift;
    return $self->{PIQUE};
}
sub inLozenge {
    my $self=shift;
    return Commun->inLozenge($self->j, $self->i, @_);
}
sub draw {
    my $self=shift;
    $self->redraw(@_);
}
sub redraw {
    my $self=shift;
    $::drawing->coords($self->{LINE},
        Commun::rot3($self->{J}, $self->{I}, $self->{ANGLE}, -5, 0, +5, 0));
    if ($Commun::view_technique) {
        $::drawing->itemconfigure($self->{LINE}, -state => "normal");
    }
    else {
        $::drawing->itemconfigure($self->{LINE}, -state => "hidden");
    }
}
sub selectionne {
    my $self=shift;
    $self->{SELECT}=1;
    $::drawing->itemconfigure($self->{LINE}, -width => 5);
    $self->redraw();
    $::sf_trait->pack();
}
sub deselectionne {
    my $self=shift;
    if ($self->{SELECT}==1) {
        $self->{SELECT}=0;
        $::drawing->itemconfigure($self->{LINE}, -width => 3);
        $self->redraw();
    }
}
sub copyTranslate {
    my $self=shift;
    my ($move_i, $move_j)=@_;
    Trait->new(
        -i     => $self->i+$move_i,
        -j     => $self->j+$move_j,
        -angle => $self->angle,
    );
}
sub move {
    my $self=shift;
    my ($move_x, $move_y)=@_;
    $self->j($self->j+$move_x);
    $self->i($self->i+$move_y);
    $self->redraw();
}
sub print {
    my $self=shift;
    return
"-type Trait -id $self->{ID} -i $self->{I} -j $self->{J} -angle $self->{ANGLE}\n";
}
sub supprime {
    my $self=shift;
    delete($liste{$self->id});
    delete($::liste{$self->{LINE}});
}
sub DESTROY {
    my $self=shift;
    $self->undoCreate;
    $self->SUPER::DESTROY();
    $self->deselectionne;
    $::drawing->delete($self->{LINE});
    #print "destroy de".$self;
}
1;
