aboutsummaryrefslogtreecommitdiff
path: root/exemples/Lien.pm
blob: d8665bc3cde7b29797d18177fea1d083c0a7333e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Lien;
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU GPL General Public License
#	as published by the Free Software Foundation; either version 2
#	of the License, or (at your option) any later version.
#
#	This program 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 this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA,
#	or refer to http://www.gnu.org/copyleft/gpl.html
#

use strict;

use MTools;
use MTools::MGroup;
use MTools::GUI::MCurve;

use vars qw /@ISA/;


BEGIN 
{
	@ISA = qw /MTools::MGroup/;
}

sub new {
	my ($class, $parent, $comete_1, $comete_2, %options) = @_;
	my $self = new MTools::MGroup ($parent);
	bless $self, $class;
	
	$self -> recordProperty ('comete_1', $comete_1);	
	$self -> recordProperty ('comete_2', $comete_2);
	$self -> recordProperty ('distance', 100);
	$self -> recordProperty ('c_x', 0);
	$self -> recordProperty ('c_y', 0);
	$self -> recordProperty ('warning', 0);
	
	$self -> {__lien} = new MTools::GUI::MCurve ($self, [[0, 0], [0, 0]],
		-linecolor => defined $options {color} ? $options {color} : '#243033;60', 
		-linestyle => 'dashed',
		-linewidth => defined $options {width} ? $options {width} : 2, 
		-filled => 0, -visible => 1, -sensitive => 0
	);
	$self -> plisten ('warning', sub {
		my ($src, $key, $val, $old) = @_;
		if ($val)
		{
			$self -> {__lien} -> mconfigure (
				-linecolor => 'red',
				-linewidth => 8,
				-linestyle => 'simple',
			);
		}
		else
		{
			$self -> {__lien} -> mconfigure (
				-linecolor => '#243033;60',
				-linestyle => 'dashed',
				-linewidth => defined $options {width} ? $options {width} : 2,
			);
		}
	});	
	$self -> {__c1_x} = $comete_1 -> plisten ('x', [$self, 'redraw']);
	$self -> {__c1_y} = $comete_1 -> plisten ('y', [$self, 'redraw']);
	$self -> {__c2_x} = $comete_2 -> plisten ('x', [$self, 'redraw']);
	$self -> {__c2_y} = $comete_2 -> plisten ('y', [$self, 'redraw']);
	
	$comete_1 -> addRattrapage ($self);
	$comete_2 -> addRattrapage ($self);
	
	return $self;
}

sub mdelete {
	my ($self) = @_;
	my $comete_1 = $self -> mget ('comete_1');
	my $comete_2 = $self -> mget ('comete_2');
	$comete_1 -> unplisten ('x', $self -> {__c1_x});
	$comete_1 -> unplisten ('y', $self -> {__c1_y});
	$comete_2 -> unplisten ('x', $self -> {__c2_x});
	$comete_2 -> unplisten ('y', $self -> {__c2_y});
	$comete_1 -> removeRattrapage ($self);
	$comete_2 -> removeRattrapage ($self);
	MTools::mdelete ($self);
}

sub redraw {
	my ($self) = @_;
	my $comete_1 = $self -> mget ('comete_1');
	my $comete_2 = $self -> mget ('comete_2');
	my $x0 = $comete_1 -> mget ('x');
	my $y0 = $comete_1 -> mget ('y');
	my $x1 = $comete_2 -> mget ('x');
	my $y1 = $comete_2 -> mget ('y');
	$self -> {__lien} -> coords ([$x0, $y0, $x1, $y1]);
	my $cautra_x0 = $comete_1 -> mget ('cautrax');
	my $cautra_y0 = $comete_1 -> mget ('cautray');
	my $cautra_x1 = $comete_2 -> mget ('cautrax');
	my $cautra_y1 = $comete_2 -> mget ('cautray');
	my $dist = sqrt (($cautra_x1 - $cautra_x0) ** 2 + ($cautra_y1 - $cautra_y0) ** 2);
	$self -> mconfigure (
		distance => int ($dist * 10) / 10,
		c_x => ($x0 + $x1) / 2,
		c_y => ($y0 + $y1) / 2,
	);
	
}

1;