aboutsummaryrefslogtreecommitdiff
path: root/Perl/demos/Tk/demos/zinc_lib/icon_zoom_resize.pl
blob: 7e578e45cb025e9654f7f799f03ec62c2d9633ac (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
#!/usr/bin/perl
# $Id: icon_zoom_resize.pl 1420 2004-04-30 11:35:18Z lecoanet $
# this simple demo has been developped by C. Mertz <mertz@cena.fr>

package icon_zoom__resize; # for avoiding symbol re-use between different demos

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

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


my $defaultfont = '-adobe-helvetica-bold-r-normal--*-140-*-*-*-*-*-*';
my $mw = MainWindow->new();
my $text = $mw->Scrolled(qw/Text -relief sunken -borderwidth 2 -setgrid true
	      -height 7 -scrollbars ''/);
$text->pack(qw/-expand yes -fill both/);

$text->insert('0.0',
	      'This demo needs openGL for rescaling/rotating the icon
   You can transform this earth gif image with your mouse:
     Drag-Button 1 for zooming the earth,
     Drag-Button 2 for rotating the earth,
     Drag-Button 3 for moving the earth,
     Shift-Drag-Button 1 for modifying the earth transparency'
	      );

my $zinc = $mw->Zinc(-width => 350, -height => 250,
		     -render => 1,
		     -font => "10x20", # usually fonts are sets in resources
		                       # but for this example it is set in the code!
		     -borderwidth => 3, -relief => 'sunken',
		     )->pack;

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

# the following image is included in Perl/Tk distrib
my $image = $zinc->Photo('earth.gif', -file => Tk->findINC('demos/images/earth.gif'));

my $earth = $zinc->add('icon', $earth_group,
		      -image => $image,
		      -composescale => 1,
		      -composerotation => 1,
		      );
$zinc->add('text', $earth_group,
           -position => [30,30],
#	   -connecteditem => $earth,
	   -text => "try to zoom/resize the earth!\nWorks even without openGL!!",
	   -color => "white",
	   -composescale => 1,
	   -composerotation => 1,
	   );

$zinc->Tk::bind('<ButtonPress-1>', [\&press, \&zoom]);
$zinc->Tk::bind('<ButtonRelease-1>', [\&release]);

$zinc->Tk::bind('<ButtonPress-2>', [\&press, \&rotate]);
$zinc->Tk::bind('<ButtonRelease-2>', [\&release]);

$zinc->Tk::bind('<ButtonPress-3>', [\&press, \&motion]);
$zinc->Tk::bind('<ButtonRelease-3>', [\&release]);
    

$zinc->Tk::bind('<Shift-ButtonPress-1>', [\&press, \&modifyAlpha]);
$zinc->Tk::bind('<Shift-ButtonRelease-1>', [\&release]);



#
# Controls for the window transform.
#
my ($cur_x, $cur_y, $cur_angle);
sub press {
    my ($zinc, $action) = @_;
    my $ev = $zinc->XEvent();
    $cur_x = $ev->x;
    $cur_y = $ev->y;
    $cur_angle = atan2($cur_y, $cur_x);
    $zinc->Tk::bind('<Motion>', [$action]);
}

sub modifyAlpha {
    my ($zinc) = @_;
    my $ev = $zinc->XEvent();
    my $lx = $ev->x;
    my $xrate = $lx /  $zinc->cget(-width);

    $xrate = 0 if $xrate < 0;
    $xrate = 1 if $xrate > 1;

    my $alpha = $xrate * 100;
    
    $zinc->itemconfigure($earth_group, -alpha => $alpha);
}


sub motion {
    my ($zinc) = @_;
    my $ev = $zinc->XEvent();
    my $lx = $ev->x;
    my $ly = $ev->y;
    my @res;
    
    @res = $zinc->transform($earth_group, [$lx, $ly, $cur_x, $cur_y]);
    $zinc->translate($earth_group, $res[0] - $res[2], $res[1] - $res[3]);
    $cur_x = $lx;
    $cur_y = $ly;
}

sub zoom {
    my ($zinc, $self) = @_;
    my $ev = $zinc->XEvent();
    my $lx = $ev->x;
    my $ly = $ev->y;
    my $maxx;
    my $maxy;
    my $sx;
    my $sy;
    
    if ($lx > $cur_x) {
	$maxx = $lx;
    } else {
	$maxx = $cur_x;
    }
    if ($ly > $cur_y) {
	$maxy = $ly
    } else {
	$maxy = $cur_y;
    }
    return if ($maxx == 0 || $maxy == 0);
    $sx = 1.0 + ($lx - $cur_x)/$maxx;
    $sy = 1.0 + ($ly - $cur_y)/$maxy;
    $cur_x = $lx;
    $cur_y = $ly;
    $zinc->scale($earth_group, $sx, $sy);
}

sub rotate {
    my ($zinc) = @_;
    my $ev = $zinc->XEvent();
    my $lx = $ev->x;
    my $ly = $ev->y;
    my $langle;
    
    $langle = atan2($ly, $lx);
    $zinc->rotate($earth_group, -($langle - $cur_angle));
    $cur_angle = $langle;
}

sub release {
    my ($zinc) = @_;
    $zinc->Tk::bind('<Motion>', '');
}

Tk::MainLoop;