aboutsummaryrefslogtreecommitdiff
path: root/Perl/demos/Tk/demos/zinc_lib/simple_interaction_track.pl
blob: 2ceb9256184a809c4c9596c41b8720ac2e789bab (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/perl
# $Id$
# This simple demo has been developped by C. Schlienger <celine@intuilab.com>

package simple_interaction_track; # for avoiding symbol collision between different demos

use vars qw( $VERSION );
($VERSION) = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/);


use Tk;
use Tk::Zinc;
use strict;

my $mw = MainWindow->new();



###########################################
# Zinc
###########################################
my $zinc_width=600;
my $zinc_height=500;
my $zinc = $mw->Zinc(-width => $zinc_width, -height => $zinc_height,
		     -font => "10x20",
		     -borderwidth => 3, -relief => 'sunken',
		     )->pack;

# The explanation displayed when running this demo
$zinc->add('text', 1,
	   -position=> [10,10],
	   -text => 'This toy-appli shows some interactions on different parts
of a flight track item. The following operations are possible:
 - Drag Button 1 on the track to move it.
      Please Note the position history (past positions)
 - Enter/Leave flight label fields
 - Enter/Leave the speedvector, symbol (i.e. current position),
     label, or leader',
	   -font => "9x15",
	   );

###########################################
# Track
###########################################

#the label format (6 formats for 6 fields)#
my $labelformat = "x80x60+0+0 x60a0^0^0 x30a0^0>1 a0a0>2>1 x30a0>3>1 a0a0^0>2";

#the track#
my $x=250;
my $y=200;
my $track=$zinc->add('track', 1, 6, # 6 is the number of field in the flightlabel
		     -labelformat => $labelformat,
		     -position => [$x, $y],#position of the marker
		     -speedvector => [30, -15],#ccords of the speed vector
		     -markersize => 10,
		     );
# moving the track, to display past positions
foreach my $i (0..5) {  $zinc->coords($track,[$x+$i*10,$y-$i*5]); }

#fields of the label#
$zinc->itemconfigure($track, 0,#configuration of field 0 of the label
		     -filled => 0,
		     -bordercolor => 'DarkGreen',
		     -border => "contour",
		    );
$zinc->itemconfigure($track, 1,
		     -filled => 1,
		     -backcolor => 'gray60',
		     -text => "AFR6128");
$zinc->itemconfigure($track, 2,
		     -filled => 0,
		     -backcolor => 'gray65',
		     -text => "390");
$zinc->itemconfigure($track, 3,
		     -filled => 0,
		     -backcolor => 'gray65',
		     -text => "/");
$zinc->itemconfigure($track, 4,
		     -filled => 0,
		     -backcolor => 'gray65',
		     -text => "350");
$zinc->itemconfigure($track, 5,
		     -filled => 0,
		     -backcolor => 'gray65',
		     -text => "TUR");



###########################################
# Events on the track
###########################################
#---------------------------------------------
# Enter/Leave a field of the label of the track
#---------------------------------------------

foreach my $field (0..5) {
  #Entering the field $field higlights it#
  $zinc->bind("$track:$field", 
	      '<Enter>',
	      sub {
		if ($field==0){ 
		  higlight_label_on();
#		  print "CP=", $zinc->currentpart, "\n";
		}
		else{
		  highlight_fields_on($field);
#		  print "CP=", $zinc->currentpart, "\n";
		}
		
	      });
  #Leaving the field cancels the highlight of $field#
  $zinc->bind("$track:$field", 
	      '<Leave>',
	      sub {
		if($field==0){
		  higlight_label_off();
		}
		else{
		  if ($field==1){
		    highlight_field1_off();
		  }
		  else{
		    highlight_other_fields_off($field);
		  }
		}
	      });
}

#fonction#
sub higlight_label_on{
   $zinc->itemconfigure('current', 0,
			-filled => 0,
			-bordercolor => 'red',
			-border => "contour",
		    );
  
}
sub higlight_label_off{
   $zinc->itemconfigure('current', 0,
			-filled => 0,
			-bordercolor => 'DarkGreen',
			-border => "contour",
		    );
  
  
}

sub highlight_fields_on{
  my $field=$_[0];
  $zinc->itemconfigure('current', $field,
		       -border => 'contour',
		       -filled => 1,
		       -color => 'white'
		      );
  
}
sub highlight_field1_off{
    $zinc->itemconfigure('current', 1,
			 -border => '',
			 -filled => 1,
			 -color => 'black',
			 -backcolor => 'gray60'
			);
  
}

sub highlight_other_fields_off{
  my $field=$_[0];
  $zinc->itemconfigure('current', $field,
		       -border => '',
		       -filled => 0,
		       -color => 'black',
		       -backcolor => 'gray65'
		      );
}
#---------------------------------------------
# Enter/Leave other parts of the track
#---------------------------------------------
$zinc->bind("$track:position", 
	      '<Enter>',
	      sub {  $zinc->itemconfigure($track,
					  -symbolcolor=>"red",
					  );
#		  print "CP=", $zinc->currentpart, "\n";
		 });
$zinc->bind("$track:position", 
	      '<Leave>',
	      sub {  $zinc->itemconfigure($track,
					  -symbolcolor=>"black",
					  );
		 });

$zinc->bind("$track:speedvector", 
	      '<Enter>',
	      sub {  $zinc->itemconfigure($track,
					  -speedvectorcolor=>"red",
					  );
		 });
$zinc->bind("$track:speedvector", '<Leave>',
    sub {  $zinc->itemconfigure($track,
				-speedvectorcolor=>"black",
				);
       });

$zinc->bind("$track:leader", '<Enter>',
    sub {  $zinc->itemconfigure($track,
				-leadercolor=>"red",
				);
       });

$zinc->bind("$track:leader", '<Leave>',
    sub {  $zinc->itemconfigure($track,
				-leadercolor=>"black",
				);
       });

#---------------------------------------------
# Drag and drop the track
#---------------------------------------------
#Binding to ButtonPress event -> "move_on" state#
$zinc -> bind($track,'<ButtonPress-1>'=>[ sub { &select_color_on(); #change the color
						&move_on($_[1],$_[2]); #"move_on" state
					    }, Tk::Ev('x'),Tk::Ev('y') ]); 

#Binding to ButtonRelease event -> "move_off" state#
$zinc -> bind($track,'<ButtonRelease-1>'=>sub{&select_color_off(); #change the color
					      &move_off();}); #"move_off" state

#"move_on" state#
sub move_on{
    my ($xi,$yi)=@_;
    #Binding to Motion event -> move the track#
    $zinc -> bind($track,'<Motion>'=>
		  [sub{move($xi,$yi,$_[1],$_[2]); #move the track
		       $xi=$_[1];
		       $yi=$_[2];
		   },Tk::Ev('x'),Tk::Ev('y')]); 
}

#"move_off" state#
sub move_off{
    #Motion event not allowed on track
    $zinc -> bind($track,'<Motion>'=>""); 
}

#move the track#
sub move{
    my ($xi,$yi,$x,$y)=@_;
    select_color_on();
    my @coords=$zinc->coords($track);
    $zinc->coords($track,[$coords[0]+$x-$xi,$coords[1]+$y-$yi]);
}


sub select_color_on{
    $zinc->itemconfigure($track,
			 -speedvectorcolor=>"white",
			 -markercolor=>"white",
			 -leadercolor=>"white" );
}

sub select_color_off{
  $zinc->itemconfigure($track,
		       -speedvectorcolor=>"black",
		       -markercolor=>"black",
		       -leadercolor=>"black" );
}
 Tk::MainLoop;