aboutsummaryrefslogtreecommitdiff
path: root/Perl/demos/Tk/demos/zinc_lib/counter.pl
blob: a0e95345f1c27f1c882a0ae4e8ffc8f414cc416e (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/perl
# This simple demo has been developped by C. Schlienger <celine@intuilab.com>

package counter; # 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;
use constant;

my constant $PI=3.1416;

my $boldfont = '-adobe-helvetica-bold-r-normal--20-240-100-100-p-182-iso8859-1';

my $mw = MainWindow->new();

###################################################
# Zinc
###################################################

my $zinc = $mw->Zinc(-width => 700, -height => 400,
		     -font => "10x20", 
		     -borderwidth => 3, 
		     -relief => 'sunken',
		     -render => 1,
		    )->pack;

if ($zinc->cget(-render)) {
    $zinc->add('rectangle', 1,
	       [0,0,700,400],
	       -filled => 1,  -linewidth => 0,
	       -fillcolor => "=axial 90 |red;40|green;40 50|blue;40"
	       );
} else { ## no openGL rendering!
    # creating a curve in the background to demonstrate the clipping
    # of the hole in the counter
    $zinc->add('curve', 1, [30,30, 350,150, 670,30, 400,200, 670,370, 350,250, 30,370, 300,200, 30,30],
	       -filled => 1,
	       -fillcolor => "tan",
	       );
}

# The explanation displayed when running this demo
$zinc->add('text', 1,
	   -position=> [10,10],
	   -text => 'This toy-appli shows a simple counter. It is made thanks
to clipping and contours : this is the only way to do this.
You can drag the counter. Observe that the color of the background
of the counter is the same as the one of the window (use of clips)',
	   -font => "10x20",
	   );

###################################################
# Les positions
###################################################

#--------------------------------
# Carre dans lequel sera inscrit le cercle du compteur
#---------------------------------

my $x0=250;
my $y0=100;
my $x1=$x0+200;
my $y1=$y0+200;

#--------------------------------
# Rectangle dans lequel defileront les chiffres
#---------------------------------

my $x2=$x0+50;
my $y2=$y0+130;
my $x3=$x1-50;
my $y3=$y1-50;


###################################################
# Chiffres clippes
###################################################

my $general_group = $zinc->add('group',1, -visible => 1);

my $clipped_group1 = $zinc->add('group',$general_group, -visible => 1);

#--------------------------------
# Clipping items
#---------------------------------

my $clipping_item1 = $zinc->add('curve', $clipped_group1,
			       [$x2,$y2,$x3,$y2,$x3,$y3,$x2,$y3,$x2,$y2]
			      );

#--------------------------------
# Clipped items
#---------------------------------

my $group1=$zinc->add('group',$clipped_group1);

my $ecart=17;

# Il y a deux listes de chifres pour centaines, dizaines, unites,
# pour assurer l'enchainement des chiffres quand le temps passe
# (cf. : actions automatiques)

#--------------------------------
# Centaines
#---------------------------------

my $cent = $zinc->add('group',$group1, -visible => 1,);
my $xc=$x2+20;
my $yc=$y2;


my $nbc1=$zinc->add('text', $cent,
	   -font => $boldfont,
	   -text => "0
1
2
3
4
5
6
7
8
9",
	   -anchor => 'nw',
	   -position => [$xc, $yc],
);
my $nbc2=$zinc->add('text', $cent,
	   -font => $boldfont,
	   -text => "0
1
2
3
4
5
6
7
8
9",
	   -anchor => 'nw',
	   -position => [$xc, $yc+210],
);
#--------------------------------
# Dixaines
#---------------------------------

my $dix = $zinc->add('group',$group1, -visible => 1);

my $xd=$xc+30;
my $yd=$y2;
my $nbd1=$zinc->add('text', $dix,
	   -font => $boldfont,
	   -text => "0
1
2
3
4
5
6
7
8
9",
	   -anchor => 'nw',
	   -position => [$xd,$yd]);

my $nbd2=$zinc->add('text', $dix,
	   -font => $boldfont,
	   -text => "0
1
2
3
4
5
6
7
8
9",
	   -anchor => 'nw',
	   -position => [$xd,$yd+210]);
#--------------------------------
# Unites
#---------------------------------

my $unit = $zinc->add('group',$group1, -visible => 1);
my $xu=$xd+30;
my $yu=$y2;
my $nbu1=$zinc->add('text', $unit,
		    -font => $boldfont,
		    -text => "0
1
2
3
4
5
6
7
8
9",
		    -anchor => 'nw',
		    -position => [$xu, $yu]);

my $nbu2=$zinc->add('text', $unit,
		    -font => $boldfont,
		    -text => "0
1
2
3
4
5
6
7
8
9",
		    -anchor => 'nw',
		    -position => [$xu, $yu+210]);

#--------------------------------
# Clip
#---------------------------------

$zinc->itemconfigure($clipped_group1, -clip => $clipping_item1);


###################################################
# Cadran clippe
###################################################

my $clipped_group2 = $zinc->add('group',$general_group, -visible => 1);

#--------------------------------
# Clipping items
#---------------------------------

my $clipping_item2 = $zinc->add('curve', $clipped_group2,
			       [0,0,700,0,700,700,0,700,0,0],
				-linewidth=>0,
			      );

$zinc->contour($clipping_item2,"add",0,[$x2,$y2,$x3,$y2,$x3,$y3,$x2,$y3,$x2,$y2]);

#--------------------------------
# Clipped items
#---------------------------------

my $group2=$zinc->add('group',$clipped_group2);

my $cercle=$zinc->add('arc',$group2,[$x0,$y0,$x1,$y1],
			 -visible=>1,
			 -filled=>1,
			 -fillcolor=>"yellow",);

my $fleche=$zinc-> add('curve', $group2, [$x0+40,$y0+40,$x1-100,$y1-25],
		       -firstend => [10, 10, 10],
		       -linewidth => 7,
		       -linecolor=>"red",
	    );

#--------------------------------
# Clip
#---------------------------------

$zinc->itemconfigure($clipped_group2, -clip => $clipping_item2);

# this translation if for having an "interesting" background in the counter hole
# when we do not have openGL and a gradient in the background
$zinc->translate($general_group,0,21); 

###################################################
# Actions automatiques
###################################################

#--------------------------------
# Variables
#---------------------------------
# Pour le timer
my $repeat=10;

# Pour la rotation
my @centre=($x1-100,$y1-25);
my $pas=40;
my $angle=+$PI/$pas;
my $nb_tot=12;
my $nb=0;

# Pour la translation des centaines
my @c_c1=$zinc->itemcget($nbc1,-position);
my @c_c2=$zinc->itemcget($nbc2,-position);
my $nbtour_cent=2;

# Pour la translation des dizaines
my @c_d1=$zinc->itemcget($nbd1,-position);
my @c_d2=$zinc->itemcget($nbd2,-position);
my $nbtour_dix=2;

# Pour la translation des unites
my @c_u1=$zinc->itemcget($nbu1,-position);
my @c_u2=$zinc->itemcget($nbu2,-position);
my $nbtour_unit=2;


#--------------------------------
# Timer
#---------------------------------
my $timer = $zinc->repeat($repeat, [\&refresh]);

$mw->OnDestroy(\&destroyTimersub );

my $timerIsDead = 0;
sub destroyTimersub {
    $timerIsDead = 1;
    $mw->afterCancel($timer);
    # the timer is not really cancelled when using zinc-demos! 
}

#--------------------------------
# Actions
#---------------------------------
sub refresh {
  #--------------------------------
  # Rotation de la fleche
  #---------------------------------
  return if $timerIsDead;   # the timer is still running when using zinc-demos!
  $zinc->rotate($fleche,$angle,$centre[0],$centre[1]);
  $nb+=1;
  if (($nb==$nb_tot)&&($angle==$PI/$pas))
    {
      $nb=0;
      $angle=-$PI/$pas;
    } 
  else{
    if(($nb==$nb_tot)&&($angle==-$PI/$pas)){
      $nb=0;
      $angle=+$PI/$pas;
    }
  }
  #--------------------------------
  # Deplacement du texte
  #---------------------------------

  #--------------------------------
  # Centaines
  #---------------------------------
  $zinc->translate($cent,0,-0.01);

  my @coords_c1=$zinc->transform($cent,$group1,[$c_c1[0],$c_c1[1]]);
  if(int($coords_c1[1])==$yc-210){
    $zinc->itemconfigure($nbc1,-position=>[$xc,$yc+($nbtour_cent*210)]);
    $nbtour_cent+=1;
    @c_c1=$zinc->itemcget($nbc1,-position);
  }

  my @coords_c2=$zinc->transform($cent,$group1,[$c_c2[0],$c_c2[1]]);
  if($coords_c2[1]==$yc-210){
    $zinc->itemconfigure($nbc2,-position=>[$xc,$yc+($nbtour_cent*210)]);
    $nbtour_cent+=1;
    @c_c2=$zinc->itemcget($nbc2,-position);
  }

  #-------------------------------- 
  #Dixaines
  #---------------------------------
  $zinc->translate($dix,0,-0.1);

  my @coords_d1=$zinc->transform($dix,$group1,[$c_d1[0],$c_d1[1]]);
  if(int($coords_d1[1])==$yd-210){
    $zinc->itemconfigure($nbd1,-position=>[$xd,$yd+($nbtour_dix*210)]);
    $nbtour_dix+=1;
    @c_d1=$zinc->itemcget($nbd1,-position);
  }

  my @coords_d2=$zinc->transform($dix,$group1,[$c_d2[0],$c_d2[1]]);
  if($coords_d2[1]==$yd-210){
    $zinc->itemconfigure($nbd2,-position=>[$xd,$yd+($nbtour_dix*210)]);
    $nbtour_dix+=1;
    @c_d2=$zinc->itemcget($nbd2,-position);
  }


  #--------------------------------
  # Unites
  #---------------------------------
  $zinc->translate($unit,0,-1);

  my @coords_u1=$zinc->transform($unit,$group1,[$c_u1[0],$c_u1[1]]);
  if($coords_u1[1]==$yu-210){
    $zinc->itemconfigure($nbu1,-position=>[$xu,$yu+($nbtour_unit*210)]);
    $nbtour_unit+=1;
    @c_u1=$zinc->itemcget($nbu1,-position);
  }

  my @coords_u2=$zinc->transform($unit,$group1,[$c_u2[0],$c_u2[1]]);
  if($coords_u2[1]==$yu-210){
    $zinc->itemconfigure($nbu2,-position=>[$xu,$yu+($nbtour_unit*210)]);
    $nbtour_unit+=1;
    @c_u2=$zinc->itemcget($nbu2,-position);
  }

}

###################################################
# Actions manuelles
###################################################

#---------------------------------------------
# Drag and drop the counter
#---------------------------------------------

my ($prev_x, $prev_y);
$zinc -> bind($cercle,'<ButtonPress-1>'=>[\&move_on] ); 


#"move_on" state#
sub move_on{
    $prev_x=$zinc->XEvent()->x;
    $prev_y=$zinc->XEvent()->y;
    # move the counter
    $zinc -> bind($cercle,'<Motion>'=> [\&move]);
    $zinc -> bind($cercle,'<ButtonRelease-1>'=> [\&move_off]); #"move_off" state
}


#"move_off" state#
sub move_off{
  $zinc -> bind($cercle,'<Motion>'=>""); 
  $zinc -> bind($cercle,'<ButtonRelease-1>'=>"");
}

#move the counter#
sub move{
  my $x=$zinc->XEvent()->x,
  my $y=$zinc->XEvent()->y;
  $zinc->translate($clipped_group1,$x-$prev_x,$y-$prev_y);
  $zinc->translate($clipped_group2,$x-$prev_x,$y-$prev_y);
  ($prev_x,$prev_y) = ($x,$y);
}
 
Tk::MainLoop;