#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/>.
#
use strict;
use warnings;
package Linepique;
use Commun;
our @ISA=qw( Commun );
use Math::Trig;
use Data::Dumper;
our %liste;
our %created;
# todo : if Grid, use grid/4, if no grid, no grid. (not sure)
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->{OX}=$param{"-ox"};
    $self->{OY}=$param{"-oy"};
    $self->{EX}=$param{"-ex"};
    $self->{EY}=$param{"-ey"};
    $self->{WIDTH}=1;
    $self->{SELECT}=0;
    bless($self, $classe);
    $self->{LINE}=
      $::drawing->createLine($self->{OX}, $self->{OY}, $self->{EX},
        $self->{EY});
    $::liste{$self->{LINE}}=$self;
    #    $self->draw();
    $liste{$self->id}=$self;
    $self->undoDelete;
    #print Dumper $self;
    return $self;
}
sub configure {
    my $self=shift;
    my $key=uc(substr(shift, 1));
    $self->{$key}=shift;
}
sub ox {
    my $self=shift;
    if (@_) {$self->{OX}=shift}
    return $self->{OX};
}
sub oy {
    my $self=shift;
    if (@_) {$self->{OY}=shift}
    return $self->{OY};
}
sub ex {
    my $self=shift;
    if (@_) {$self->{EX}=shift}
    return $self->{EX};
}
sub ey {
    my $self=shift;
    if (@_) {$self->{EY}=shift}
    return $self->{EY};
}
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->ox, $self->oy, @_)
      && Commun->inLozenge($self->ex, $self->ey, @_);
}
sub draw {
    my $self=shift;
    $self->redraw(@_);
}
sub redraw {
    my $self=shift;
    $::drawing->coords($self->{LINE}, $self->{OX}, $self->{OY}, $self->{EX},
        $self->{EY});
    if ($Commun::view_pique) {
        $::drawing->itemconfigure($self->{LINE}, -state => "normal");
    }
    else {    # no pique
        $::drawing->itemconfigure($self->{LINE}, -state => "hidden");
    }
}
sub selectionne {
    my $self=shift;
    $self->{SELECT}=1;
    $self->redraw();
    # $::sc8->pack;
}
sub deselectionne {
    my $self=shift;
    if ($self->{SELECT}==1) {
        $self->{SELECT}=0;
        $self->redraw();
    }
}
sub copyTranslate {
    my $self=shift;
    my ($move_i, $move_j)=@_;
    Linepique->new(
        -ox => $self->ox+$move_j,
        -oy => $self->oy+$move_i,
        -ex => $self->ex+$move_j,
        -ey => $self->ey+$move_i,
    );
}
sub print {
    my $self=shift;
    return
"-type Linepique -id $self->{ID} -ox $self->{OX} -oy $self->{OY} -ex $self->{EX} -ey $self->{EY}\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;
