ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.8
Committed: Sun Feb 5 23:39:36 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.7: +5 -98 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Crossfire::MapWidget - Gtk2 widget displaying cf maps
4
5 =head1 SYNOPSIS
6
7 use Crossfire::MapWidget;
8
9 =head1 DESCRIPTION
10
11 =head2 METHODS
12
13 =over 4
14
15 =cut
16
17 package Crossfire::MapWidget;
18
19 use strict;
20
21 use Gtk2;
22
23 use Crossfire;
24 use Crossfire::Tilecache;
25
26 use Glib::Object::Subclass
27 'Gtk2::DrawingArea';
28
29 use List::Util qw(min max);
30
31 sub INIT_INSTANCE {
32 my ($self) = @_;
33
34 $self->signal_connect (destroy => sub {
35 my ($self) = @_;
36
37 $self->{tip}->destroy if $self->{tip};
38
39 %$self = ();
40
41 0
42 });
43 $self->signal_connect (realize => sub {
44 my ($self) = @_;
45
46 $self->{window} = $self->window;
47
48 1
49 });
50
51 $self->set_redraw_on_allocate (0);
52 $self->double_buffered (0);
53
54 # reduces unnecessary redraws
55 $self->signal_connect_after (focus_in_event => sub { 1 });
56 $self->signal_connect_after (focus_out_event => sub { 1 });
57
58 $self->signal_connect (size_request => sub {
59 $_[1]->width (20 * TILESIZE);
60 $_[1]->height (20 * TILESIZE);
61
62 1
63 });
64
65 $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 });
66
67 $self->signal_connect_after (configure_event => sub {
68 $self->set_viewport ($self->{x}, $self->{y});
69
70 0
71 });
72
73 $self->{tooltip} = -2; # need to be mapped _and_ inside
74
75 $self->signal_connect_after (map_event => sub { $self->enable_tooltip; 0 });
76 $self->signal_connect_after (unmap_event => sub { $self->disable_tooltip; 0 });
77
78 $self->signal_connect_after (enter_notify_event => sub { $self->enable_tooltip; 0 });
79 $self->signal_connect_after (leave_notify_event => sub { $self->disable_tooltip; 0 });
80
81 $self->signal_connect (button_press_event => sub {
82 my ($self, $event) = @_;
83
84 my ($x, $y) = ($event->x, $event->y);
85
86 if ($_[1]->button == 2 && !$self->{in_drag}) {
87 $self->disable_tooltip;
88
89 $_[0]->grab_focus;
90 $self->{in_drag} = [$self->{x}, $self->{y}, $x, $y];
91 return 1;
92 }
93
94 0
95 });
96
97 $self->signal_connect (motion_notify_event => sub {
98 my ($self) = @_;
99
100 $self->update_tooltip;
101
102 if (my $di = $self->{in_drag}) {
103 my ($x, $y) = $self->get_pointer;
104
105 $self->set_viewport (
106 $di->[0] + $di->[2] - $x,
107 $di->[1] + $di->[3] - $y,
108 );
109
110 return 1;
111 }
112
113 0
114 });
115
116 $self->signal_connect (button_release_event => sub {
117 my ($self) = @_;
118
119 $self->enable_tooltip
120 if delete $self->{in_drag};
121
122 0
123 });
124
125 # gtk+ supports no motion compression, a major lacking feature. we have to pay for the
126 # workaround with incorrect behaviour and extra server-turnarounds.
127 $self->add_events ([qw(button_press_mask button_release_mask button-motion-mask
128 pointer-motion-mask pointer-motion-hint-mask
129 enter-notify-mask leave-notify-mask)]);
130 $self->can_focus (1);
131
132 # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) });
133 }
134
135 sub enable_tooltip {
136 my ($self) = @_;
137
138 $self->{tooltip}++;
139 $self->update_tooltip;
140 }
141
142 sub disable_tooltip {
143 my ($self) = @_;
144
145 $self->{tooltip}--;
146 $self->update_tooltip;
147 }
148
149 sub overlay {
150 my ($self, $name, $x, $y, $w, $h, $cb) = @_;
151
152 if (my $ov = delete $self->{overlay}{$name}) {
153 my ($x, $y, $w, $h) = @$ov;
154
155 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
156 }
157
158 if ($h) {
159 $self->{overlay}{$name} = [$x, $y, $w, $h, $cb];
160
161 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
162 }
163 }
164
165 sub update_tooltip {
166 my ($self) = @_;
167
168 if ($self->{tooltip} >= 0) {
169 my $screen = $self->{window}->get_screen;
170 my ($pscreen, $x, $y) = $screen->get_display->get_pointer;
171
172 if ($pscreen == $screen) {
173 if (!$self->{tip}) {
174 $self->{tip} = new Gtk2::Window "popup";
175 $self->{tip}->can_focus (0);
176 $self->{tip}->set_name ("gtk-tooltips");
177 $self->{tip}->set_decorated (0);
178 $self->{tip}->set_border_width (4);
179 $self->{tip}->set_has_frame (0);
180 $self->{tip}->set_resizable (0);
181 $self->{tip}->set_transient_for ($self->get_toplevel);
182 }
183
184 my ($mx, $my) = $self->coord ($self->get_pointer);
185
186 if ($self->{tipinfo}[0] != $mx || $self->{tipinfo}[1] != $my) {
187 $self->fill_tooltip ($mx, $my);
188
189 $self->{tipinfo} = [$mx, $my];
190
191 $self->overlay (_tooltip => $mx * TILESIZE, $my * TILESIZE, TILESIZE, TILESIZE, sub {
192 my ($self, $x, $y) = @_;
193
194 $self->{window}->draw_rectangle ($_ & 1 ? $self->style->black_gc : $self->style->white_gc, 0,
195 $x + $_, $y + $_,
196 TILESIZE - 1 - $_ * 2, TILESIZE - 1 - $_ * 2)
197 for 0..3;
198
199 my $req = $self->{tip}->size_request;
200 $self->{tip}->resize ($req->width, $req->height);
201 });
202
203 $self->{tip}->show_all;
204 }
205
206 $self->{tip}->move ($x + TILESIZE, $y);
207
208 return;
209 }
210 }
211
212 $self->overlay ("_tooltip");
213 delete $self->{tipinfo};
214 (delete $self->{tip})->destroy if $self->{tip};
215 }
216
217 sub fill_tooltip {
218 my ($self, $x, $y) = @_;
219
220 $self->{tip}->remove ($self->{tip}->get_children)
221 if $self->{tip}->get_children;
222
223 $self->{tip}->add (my $frame = new Gtk2::Frame "($x|$y)");
224
225 # $frame->set_shadow_type ("none");
226
227 if ($x < 0 || $x >= $self->{map}{width}
228 || $y < 0 || $y >= $self->{map}{height}) {
229 $frame->add (new Gtk2::Label "<off-map>");
230 } else {
231 $frame->add (my $vbox = new Gtk2::VBox 1, 1);
232
233 #TODO: fill tooltip via signal, defaulting to this:
234
235 # fill tooltip with info about $x, $y
236 my $as = $self->{map}{map}[$x][$y] || [];
237 for my $a (@$as) {
238 $vbox->add (my $hbox = new Gtk2::HBox 0, 2);
239
240 my $tile = $Crossfire::Tilecache::TILECACHE{ $a->{face} || $ARCH->{$a->{_name}}{face} };
241
242 # this is awful, is this really the best way?
243 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
244 $pb->fill (0x00000000);
245
246 $Crossfire::Tilecache::TILECACHE->composite ($pb,
247 0, 0,
248 TILESIZE, TILESIZE,
249 - ($tile->{idx} % 64) * TILESIZE, - TILESIZE * int $tile->{idx} / 64,
250 1, 1, 'nearest', 255
251 );
252
253 $hbox->add (my $img = new_from_pixbuf Gtk2::Image $pb);
254 $img->set_alignment (0, 0.5);
255
256 $hbox->add (my $label = new Gtk2::Label $a->{_name});
257 $label->set_alignment (0, 0.5);
258 }
259 }
260 }
261
262 sub set_viewport {
263 my ($self, $x, $y) = @_;
264
265 my $area = $self->allocation;
266
267 $x = max 0, min $self->{width} - $area->width , $x;
268 $y = max 0, min $self->{height} - $area->height, $y;
269
270 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
271
272 ($self->{x}, $self->{y}) = ($x, $y);
273 }
274
275 sub set_map {
276 my ($self, $map) = @_;
277
278 $self->{map} = $map;
279
280 $self->{width} = $map->{width} * TILESIZE;
281 $self->{height} = $map->{height} * TILESIZE;
282
283 $self->{x} =
284 $self->{y} = 0;
285
286 delete $self->{face};
287 $self->update_map (0, 0, $self->{width}, $self->{height});
288 delete $self->{tipinfo};
289 $self->update_tooltip;
290 $self->invalidate_all;
291 }
292
293 sub coord {
294 my ($self, $x, $y) = @_;
295
296 (
297 int +($self->{x} + $x) / TILESIZE,
298 int +($self->{y} + $y) / TILESIZE,
299 )
300 }
301
302 #sub handle_key {
303 # my ($self, $key, $state) = @_;
304 #
305 # $self->prefetch_cancel;
306 #
307 # if ($state * "control-mask") {
308 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
309 # my @sel = keys %{$self->{sel}};
310 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
311 # }
312 #
313 # 1
314 #}
315
316 sub invalidate {
317 my ($self, $x, $y, $w, $h) = @_;
318
319 return unless $self->{window};
320
321 $self->queue_draw_area (
322 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
323 );
324 }
325
326 sub invalidate_all {
327 my ($self) = @_;
328
329 $self->queue_draw;
330 }
331
332 sub update_map {
333 my ($self, $x, $y, $w, $h) = @_;
334
335 delete $self->{overlay};
336
337 my $map = $self->{map}{map};
338 my $ov = $self->{face} ||= [];
339
340 $x = 0; $w = $self->{map}{width};
341 $y = 0; $h = $self->{map}{height};
342
343 my @x = ($x .. $x + $w - 1);
344 my @y = ($y .. $y + $h - 1);
345
346 my $TC = \%Crossfire::Tilecache::TILECACHE;
347
348 my @ov;
349
350 # update overlay map with bigfaces and chained faces
351 for my $x (@x) {
352 my $ass = $self->{map}{map}[$x];
353 my $oss = $ov->[$x] ||= [];
354 for my $y (@y) {
355 my $os = $oss->[$y] = [];
356 for my $a (@{ $ass->[$y] || [] }) {
357 my $o = $ARCH->{$a->{_name}}
358 or (warn "arch '$a->{_name}' not found at ($x|$y)\n"), next;
359
360 my $tile = $TC->{$a->{face} || $o->{face}}
361 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
362
363 if ($tile->{w} > 1 || $tile->{h} > 1) {
364 # bigfaces
365 for my $ox (0 .. $tile->{w} - 1) {
366 for my $oy (0 .. $tile->{h} - 1) {
367 push @ov, [$x + $ox, $y + $oy,
368 $tile->{idx} + $ox + $oy * $tile->{w}];
369 }
370 }
371
372 } elsif ($o->{more}) {
373 # linked faces
374 do {
375 my $tile = $TC->{$o->{face}}
376 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
377 push @ov, [$x + $o->{x}, $y + $o->{y}, $tile->{idx}];
378 } while $o = $o->{more};
379
380 } else {
381 # single face
382 push @$os, $tile->{idx};
383
384 }
385 }
386 }
387 }
388
389 # bigger faces always on top, I don't give a shit to those who think otherwise
390 for (@ov) {
391 my ($x, $y, $idx) = @$_;
392
393 push @{ $ov->[$x][$y] }, $idx;
394 }
395 }
396
397 sub expose {
398 my ($self, $event) = @_;
399
400 no integer;
401
402 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
403 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
404
405 # get_rectangles is buggy in older versions
406 for my $area ($Gtk2::VERSION > 1.115 ? $event->region->get_rectangles : $event->area) {
407 my ($x, $y, $w, $h) = $area->values; # x y w h
408
409 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
410 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
411
412 my $PB = $Crossfire::Tilecache::TILECACHE;
413
414 my $window = $self->{window};
415
416 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
417 $pb->fill (0x00000000);
418
419 for my $x (@x) {
420 my $dx = ($x - $x[0]) * TILESIZE;
421 my $oss = $self->{face}[$x];
422
423 for my $y (@y) {
424 my $dy = ($y - $y[0]) * TILESIZE;
425
426 for my $idx (@{$oss->[$y]}) {
427 $PB->composite ($pb,
428 $dx, $dy,
429 TILESIZE, TILESIZE,
430 $dx - ($idx % 64) * TILESIZE, $dy - TILESIZE * int $idx / 64,
431 1, 1, 'nearest', 255
432 );
433 }
434 }
435 }
436
437 $pb->render_to_drawable ($window, $self->style->black_gc,
438 0, 0,
439 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
440 TILESIZE * @x, TILESIZE * @y,
441 'max', 0, 0);
442 }
443
444 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
445 for values %{ $self->{overlay} || {} };
446 }
447
448 =back
449
450 =head1 AUTHOR
451
452 Marc Lehmann <schmorp@schmorp.de>
453
454 =cut
455
456 1
457