blib/lib/Draft/TkGui.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 146 CC 2 SCAR 9.3
CC
2
Cov
0%
_items_end_drag
6.0
new
2.0
Draw
2.0
CRAP 2.5
1 package Draft::TkGui;
2  
3 =head1 NAME
4  
5 Draft::TkGui - Display a Draft world
6  
7 =head1 SYNOPSIS
8  
9 Opens a Tk GUI window, displays a drawing and allows some user
10 interaction.
11  
12 =cut
13  
14 use strict;
15 use warnings;
16  
17 =pod
18  
19 =head1 DESCRIPTION
20  
21 This module knows a little bit about Draft drawings and displays
22 them on screen using L<TK::WorldCanvas> which takes care of all the
23 drawing, panning etc..
24  
25 This module is a sub-class of L<Tk::WorldCanvas> and so inherits
26 all L<Tk::Canvas> methods.
27  
28 =cut
29  
30 use Draft;
31 use Tk::WorldCanvas;
32 use File::Atomism;
33 use File::Atomism::utils qw /Undo Redo/;
34  
35 use vars qw /@ISA/;
36 @ISA = qw /Tk::WorldCanvas/;
37  
38 =pod
39  
40 =head1 USAGE
41  
42 Create a Draft::TkGui object like so:
43  
44 my $canvas = Draft::TkGui->new;
45  
46 This should show a window and display the drawing, some interaction
47 is possible:
48  
49 =over 2
50  
51 =item *
52  
53 Use the 'i' and 'o' keys to zoom in and out.
54  
55 =item *
56  
57 Use the left mouse button to drag items around the screen.
58  
59 =item *
60  
61 Use the middle mouse button to pan around the viewport.
62  
63 =back
64  
65 =cut
66  
67 sub new
68 {
69 my $class = shift;
70 $class = ref $class || $class;
71  
72 my $top = MainWindow->new;
73  
74 my $self = $top->WorldCanvas (-width => '297m', -height => '210m');
75  
76 $self->pack (-expand => 'yes', -fill => 'both');
77  
78 # make items change colour with mouse-over
79  
80 $self->bind ('all', '<Any-Enter>' => [\&_items_enter]);
81 $self->bind ('all', '<Any-Leave>' => [\&_items_leave]);
82  
83 # i and o zoom in and out
84  
85 $self->CanvasBind('<i>' => sub {$self->zoom (1.25)});
86 $self->CanvasBind('<o>' => sub {$self->zoom (0.8)});
87  
88 $self->CanvasBind ('<Control-Key-z>' => sub {Undo ($Draft::WORLD->{$Draft::PATH}->{_path}); $_[0]->Draw});
89 $self->CanvasBind ('<Control-Key-y>' => sub {Redo ($Draft::WORLD->{$Draft::PATH}->{_path}); $_[0]->Draw});
90  
91 #$self->CanvasBind ('MouseWheel' => sub {$self->zoom (1.25)});
92  
93 # left-mouse is used to move anything
94  
95 $self->CanvasBind ('<1>' =>
96 sub {$self->_items_start_drag ($Tk::event->x, $Tk::event->y)});
97  
98 $self->CanvasBind ('<B1-Motion>' =>
99 sub {$self->_items_drag ($Tk::event->x, $Tk::event->y)});
100  
101 $self->CanvasBind ('<ButtonRelease-1>' =>
102 sub {$self->_items_end_drag ($Tk::event->x, $Tk::event->y)});
103  
104 # middle-mouse is used to pan the viewport
105  
106 $self->CanvasBind ('<2>' =>
107 sub {$self->scan ('mark', $Tk::event->x, $Tk::event->y); $self->configure (-cursor => 'fleur');});
108  
109 $self->CanvasBind ('<B2-Motion>' =>
110 sub {$self->scan ('dragto', $Tk::event->x, $Tk::event->y, 1)});
111  
112 $self->CanvasBind ('<ButtonRelease-2>' =>
113 sub {$self->configure (-cursor => '');});
114  
115 # canvas gets the focus and a redraw with a mouse-over
116  
117 $self->CanvasBind ('<Any-Enter>' => sub {$_[0]->CanvasFocus; $_[0]->Draw});
118  
119 $self = bless $self, $class;
120 return $self;
121 }
122  
123 =pod
124  
125 You can redraw the canvas window like so:
126  
127 $canvas->Draw;
128  
129 This scans all drawing elements and updates the display with any
130 changes. No files are accessed unnecessarily, so feel free to call
131 this method as often as you like; by default this method is called
132 whenever a mouse pointer enters the canvas area.
133  
134 =cut
135  
136 sub Draw
137 {
138 my $self = shift;
139  
140 my $drawing = $Draft::WORLD->{$Draft::PATH};
141 my $offset = [0, 0, 0];
142  
143 $drawing->Draw ($self, $offset, [], []);
144  
145 $self->delete (keys %{$File::Atomism::EVENT->{_old}});
146  
147 $drawing->Draw ($self, $offset, [], []);
148  
149 undef $File::Atomism::EVENT->{_old};
150 undef $File::Atomism::EVENT->{_new};
151 }
152  
153 sub _items_start_drag
154 {
155 my $self = shift;
156 my ($x, $y) = @_;
157  
158 $self->{iinfo}->{lastX} = $self->{iinfo}->{startX} = $self->worldx ($x);
159 $self->{iinfo}->{lastY} = $self->{iinfo}->{startY} = $self->worldy ($y);
160 }
161  
162 sub _items_drag
163 {
164 my $self = shift;
165 my ($x, $y) = @_;
166  
167 my @tags = $self->gettags ('current');
168 my $tag = shift @tags || return;
169  
170 $self->move ($tag, $self->worldx ($x) - $self->{iinfo}->{lastX},
171 $self->worldy ($y) - $self->{iinfo}->{lastY});
172  
173 $self->{iinfo}->{lastX} = $self->worldx ($x);
174 $self->{iinfo}->{lastY} = $self->worldy ($y);
175 }
176  
177 sub _items_end_drag
178 {
179 my $self = shift;
180 my ($x, $y) = @_;
181  
182 my @tags = $self->gettags ('current');
183 my $tag = shift @tags || return;
184  
185 my $moveX = $self->worldx ($x) - $self->{iinfo}->{startX};
186 my $moveY = $self->worldy ($y) - $self->{iinfo}->{startY};
187  
188 return if ($moveX == 0 and $moveY == 0);
189  
190 my @path = split '/', $tag;
191 my $file = pop @path;
192 my $folder = (join '/', @path) . '/';
193  
194 my $item = $Draft::WORLD->{$folder}->{$tag};
195  
196 $item->Move ([$moveX, $moveY, 0]);
197 system 'sync';
198 }
199  
200 sub _items_enter
201 {
202 my $self = shift;
203  
204 my @tags = $self->gettags ('current');
205  
206 my $tag = shift @tags;
207  
208 for my $tag (@tags) {$self->itemconfigure ($tag, -fill => 'Orange')}
209  
210 # http://tmml.sourceforge.net/doc/tk/cursors.html
211 $self->configure (-cursor => 'fleur');
212  
213 $self->itemconfigure ($tag, -fill => 'Red');
214 }
215  
216 sub _items_leave
217 {
218 my $self = shift;
219  
220 my @tags = $self->gettags ('current');
221  
222 $self->configure (-cursor => '');
223  
224 for my $tag (@tags) {$self->itemconfigure ($tag, -fill => 'Black')}
225 }
226  
227  
228 1;