ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.18
Committed: Sun Feb 12 04:50:24 2006 UTC (18 years, 3 months ago) by root
Branch: MAIN
Changes since 1.17: +171 -70 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 #my ($TILE_MINX, $TILE_MINY, $TILE_MAXX, $TILE_MAXY);
32 #
33 #for (values %TILE) {
34 # $TILE_MAXX = max $TILE_MAXX, $_->{w} - 1;
35 # $TILE_MAXY = max $TILE_MAXY, $_->{h} - 1;
36 #}
37 #
38 #for (values %ARCH) {
39 # for (; $_; $_ = $_->{more}) {
40 # $TILE_MINX = min $TILE_MINX, $_->{x};
41 # $TILE_MINY = min $TILE_MINY, $_->{y};
42 # $TILE_MAXX = max $TILE_MAXX, $_->{x};
43 # $TILE_MAXY = max $TILE_MAXY, $_->{y};
44 # }
45 #}
46
47 sub INIT_INSTANCE {
48 my ($self) = @_;
49
50 $self->signal_connect (destroy => sub {
51 my ($self) = @_;
52
53 $self->{tip}->destroy if $self->{tip};
54
55 %$self = ();
56
57 0
58 });
59 $self->signal_connect (realize => sub {
60 my ($self) = @_;
61
62 $self->{window} = $self->window;
63
64 1
65 });
66
67 $self->set_redraw_on_allocate (0);
68 $self->double_buffered (0);
69
70 $self->{tooltip} = -1; # need focus in first
71
72 # reduces unnecessary redraws
73 $self->signal_connect (focus_in_event => sub { $self->enable_tooltip; 1 });
74 $self->signal_connect (focus_out_event => sub { $self->disable_tooltip; 1 });
75
76 $self->signal_connect_after (enter_notify_event => sub { $self->update_tooltip; 0 });
77 $self->signal_connect_after (leave_notify_event => sub { $self->update_tooltip; 0 });
78
79 $self->signal_connect (size_request => sub {
80 $_[1]->width (TILESIZE);
81 $_[1]->height (TILESIZE);
82
83 1
84 });
85
86 $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 });
87
88 $self->signal_connect_after (configure_event => sub {
89 $self->set_viewport ($self->{x}, $self->{y});
90
91 0
92 });
93
94 $self->signal_connect (button_press_event => sub {
95 my ($self, $event) = @_;
96
97 my ($x, $y) = ($event->x, $event->y);
98
99 if ($_[1]->button == 2 && !$self->{in_drag}) {
100 $self->disable_tooltip;
101
102 $_[0]->grab_focus;
103 $self->{in_drag} = [$self->{x}, $self->{y}, $x, $y];
104 return 1;
105 }
106
107 0
108 });
109
110 $self->signal_connect (motion_notify_event => sub {
111 my ($self) = @_;
112
113 $self->update_tooltip;
114
115 if (my $di = $self->{in_drag}) {
116 my ($x, $y) = $self->get_pointer;
117
118 $self->set_viewport (
119 $di->[0] + $di->[2] - $x,
120 $di->[1] + $di->[3] - $y,
121 );
122
123 return 1;
124 }
125
126 0
127 });
128
129 $self->signal_connect (button_release_event => sub {
130 my ($self) = @_;
131
132 $self->enable_tooltip
133 if delete $self->{in_drag};
134
135 0
136 });
137
138 # gtk+ supports no motion compression, a major lacking feature. we have to pay for the
139 # workaround with incorrect behaviour and extra server-turnarounds.
140 $self->add_events ([qw(button_press_mask button_release_mask button-motion-mask
141 pointer-motion-mask pointer-motion-hint-mask
142 enter-notify-mask leave-notify-mask)]);
143 $self->can_focus (1);
144
145 # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) });
146 }
147
148 sub enable_tooltip {
149 my ($self) = @_;
150
151 $self->{tooltip}++;
152 $self->update_tooltip;
153 }
154
155 sub disable_tooltip {
156 my ($self) = @_;
157
158 $self->{tooltip}--;
159 $self->update_tooltip;
160 }
161
162 sub overlay {
163 my ($self, $name, $x, $y, $w, $h, $cb) = @_;
164
165 if (my $ov = delete $self->{overlay}{$name}) {
166 my ($x, $y, $w, $h) = @$ov;
167
168 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
169 }
170
171 if ($h) {
172 $self->{overlay}{$name} = [$x, $y, $w, $h, $cb];
173
174 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
175 }
176 }
177
178 sub update_tooltip {
179 my ($self) = @_;
180
181 if ($self->{tooltip} >= 0
182 && $self->mapped
183 && $self->get_toplevel->has_toplevel_focus) {
184 my $screen = $self->{window}->get_screen;
185
186 if ($self->{window} == ($screen->get_display->get_window_at_pointer)[0]) {
187 my ($pscreen, $x, $y) = $screen->get_display->get_pointer;
188
189 if ($pscreen == $screen) {
190 if (!$self->{tip}) {
191 $self->{tip} = new Gtk2::Window "popup";
192 $self->{tip}->can_focus (0);
193 $self->{tip}->set_name ("gtk-tooltips");
194 $self->{tip}->set_decorated (0);
195 $self->{tip}->set_border_width (4);
196 $self->{tip}->set_has_frame (0);
197 $self->{tip}->set_resizable (0);
198 $self->{tip}->set_transient_for ($self->get_toplevel);
199 }
200
201 my ($mx, $my) = $self->coord ($self->get_pointer);
202
203 if ($self->{tipinfo}[0] != $mx || $self->{tipinfo}[1] != $my) {
204 $self->fill_tooltip ($mx, $my);
205
206 $self->{tipinfo} = [$mx, $my];
207
208 $self->overlay (_tooltip => $mx * TILESIZE, $my * TILESIZE, TILESIZE, TILESIZE, sub {
209 my ($self, $x, $y) = @_;
210
211 $self->{window}->draw_rectangle ($_ & 1 ? $self->style->black_gc : $self->style->white_gc, 0,
212 $x + $_, $y + $_,
213 TILESIZE - 1 - $_ * 2, TILESIZE - 1 - $_ * 2)
214 for 0..3;
215 });
216
217 my $req = $self->{tip}->size_request;
218 $self->{tip}->resize ($req->width, $req->height);
219 }
220
221 $self->{tip}->move ($x + TILESIZE, $y);
222 $self->{tip}->show_all;
223
224 return;
225 }
226 }
227 }
228
229 $self->overlay ("_tooltip");
230 delete $self->{tipinfo};
231 (delete $self->{tip})->destroy if $self->{tip};
232 }
233
234 sub fill_tooltip {
235 my ($self, $x, $y) = @_;
236
237 $self->{tip}->remove ($self->{tip}->get_children)
238 if $self->{tip}->get_children;
239
240 $self->{tip}->add (my $frame = new Gtk2::Frame "($x|$y)");
241
242 if ($x < 0 || $x >= $self->{map}{width}
243 || $y < 0 || $y >= $self->{map}{height}) {
244 $frame->add (new Gtk2::Label "<off-map>");
245 } else {
246 $frame->add (my $vbox = new Gtk2::VBox 1, 1);
247
248 #TODO: fill tooltip via signal, defaulting to this:
249
250 # fill tooltip with info about $x, $y
251 my $as = $self->{map}{map}[$x][$y] || [];
252 for my $a (reverse @$as) {
253 $vbox->add (my $hbox = new Gtk2::HBox 0, 2);
254
255 my $tile = $FACE{ $a->{face} || $ARCH{$a->{_name}}{face} };
256
257 # this is awful, is this really the best way?
258 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
259 $pb->fill (0x00000000);
260
261 $TILE->composite ($pb,
262 0, 0,
263 TILESIZE, TILESIZE,
264 - ($tile->{idx} % 64) * TILESIZE, - TILESIZE * int $tile->{idx} / 64,
265 1, 1, 'nearest', 255
266 );
267
268 $hbox->add (my $img = new_from_pixbuf Gtk2::Image $pb);
269 $img->set_alignment (0, 0.5);
270
271 $hbox->add (my $label = new Gtk2::Label $a->{_name});
272 $label->set_alignment (0, 0.5);
273 }
274 }
275 }
276
277 sub set_viewport {
278 my ($self, $x, $y) = @_;
279
280 my $area = $self->allocation;
281
282 $x = max 0, min $self->{width} - $area->width , $x;
283 $y = max 0, min $self->{height} - $area->height, $y;
284
285 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
286
287 ($self->{x}, $self->{y}) = ($x, $y);
288 }
289
290 sub set_map {
291 my ($self, $map) = @_;
292
293 $self->{map} = $map;
294
295 $self->{width} = $map->{width} * TILESIZE;
296 $self->{height} = $map->{height} * TILESIZE;
297
298 $self->{x} =
299 $self->{y} = 0;
300
301 delete $self->{face};
302 delete $self->{face_extents};
303 $self->update_map (0, 0, $map->{width} - 1, $map->{height} - 1);
304 delete $self->{tipinfo}; $self->update_tooltip;
305 $self->invalidate_all;
306 }
307
308 sub coord {
309 my ($self, $x, $y) = @_;
310
311 (
312 int +($self->{x} + $x) / TILESIZE,
313 int +($self->{y} + $y) / TILESIZE,
314 )
315 }
316
317 #sub handle_key {
318 # my ($self, $key, $state) = @_;
319 #
320 # $self->prefetch_cancel;
321 #
322 # if ($state * "control-mask") {
323 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
324 # my @sel = keys %{$self->{sel}};
325 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
326 # }
327 #
328 # 1
329 #}
330
331 sub invalidate {
332 my ($self, $x, $y, $w, $h) = @_;
333
334 return unless $self->{window};
335
336 $self->queue_draw_area (
337 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
338 );
339 }
340
341 sub invalidate_all {
342 my ($self) = @_;
343
344 $self->queue_draw;
345 }
346
347 sub expose {
348 my ($self, $event) = @_;
349
350 no integer;
351
352 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
353 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
354
355 # get_rectangles is buggy in older versions
356 my @rectangles = $Gtk2::VERSION > 1.115
357 ? $event->region->get_rectangles : $event->area;
358
359 for my $area (@rectangles) {
360 my ($x, $y, $w, $h) = $area->values; # x y w h
361
362 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
363 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
364
365 my $window = $self->{window};
366
367 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
368 $pb->fill (0xff69b400);
369
370 for my $x (@x) {
371 my $dx = ($x - $x[0]) * TILESIZE;
372 my $oss = $self->{face}[$x];
373
374 for my $y (@y) {
375 my $dy = ($y - $y[0]) * TILESIZE;
376
377 for my $idx (@{$oss->[$y]}) {
378 $TILE->composite ($pb,
379 $dx, $dy,
380 TILESIZE, TILESIZE,
381 $dx - ($idx % 64) * TILESIZE, $dy - TILESIZE * int $idx / 64,
382 1, 1, 'nearest', 255
383 );
384 }
385 }
386 }
387
388 $pb->render_to_drawable ($window, $self->style->black_gc,
389 0, 0,
390 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
391 TILESIZE * @x, TILESIZE * @y,
392 'max', 0, 0);
393 }
394
395 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
396 for values %{ $self->{overlay} || {} };
397 }
398
399 sub update_map {
400 my ($self, $x1, $y1, $x2, $y2) = @_;
401
402 delete $self->{tipinfo}; $self->update_tooltip;
403
404 # push @{ $self->{queue_draw_areay} }, [$x * TILESIZE - $self->{x},
405 # $y * TILESIZE - $self->{y},
406 # $w * TILESIZE, $h * TILESIZE];
407
408 # we store precomputed tile info into $self->{face}
409 # and the minimum/maximum extents into $self->{face_extents}
410 my $map = $self->{map}{map};
411 my $ov = $self->{face} ||= [];
412 my $ext = $self->{face_extents} ||= [];
413
414 $x1 = max 0, min $self->{map}{width} - 1, $x1;
415 $y1 = max 0, min $self->{map}{height} - 1, $y1;
416 $x2 = max 0, min $self->{map}{width} - 1, $x2;
417 $y2 = max 0, min $self->{map}{height} - 1, $y2;
418
419 # extend update area as neccessary to include all bigfaces
420 # that overlap the update area
421 for (;;) {
422 my $redo;
423 for my $x ($x1 .. $x2) {
424 my $col = $ext->[$x] ||= [];
425 for my $y ($y1 .. $y2) {
426 if (my $ext = $col->[$y]) {
427 ($redo = 1), ($x1 = $ext->[0]) if $ext->[0] < $x1;
428 ($redo = 1), ($y1 = $ext->[1]) if $ext->[1] < $y1;
429 ($redo = 1), ($x2 = $ext->[2]) if $ext->[2] > $x2;
430 ($redo = 1), ($y2 = $ext->[3]) if $ext->[3] > $y2;
431 }
432
433 # reset extents
434 $col->[$y] = [$x, $y, $x, $y];
435 }
436 }
437 $redo or last;
438 }
439
440 $x1 = max 0, min $self->{map}{width} - 1, $x1;
441 $y1 = max 0, min $self->{map}{height} - 1, $y1;
442 $x2 = max 0, min $self->{map}{width} - 1, $x2;
443 $y2 = max 0, min $self->{map}{height} - 1, $y2;
444
445 $self->queue_draw_area (
446 $x1 * TILESIZE - $self->{x}, $y1 * TILESIZE - $self->{y},
447 ($x2 - $x1 + 1) * TILESIZE , ($y2 - $y1 + 1) * TILESIZE ,
448 );
449
450 my @ov;
451
452 # update overlay map with bigfaces and chained faces
453 my $a;
454 for my $x ($x1 .. $x2) {
455 my $ass = $map->[$x];
456 my $oss = $ov->[$x] ||= [];
457 for my $y ($y1 .. $y2) {
458 my $os = $oss->[$y] = [];
459 for my $a (@{ $ass->[$y] || [] }) {
460 my $o = $ARCH{$a->{_name}}
461 or (warn "arch '$a->{_name}' not found at ($x|$y)\n"), next;
462
463 my $tile = $FACE{$a->{face} || $o->{face}}
464 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
465
466 if ($tile->{w} > 1 || $tile->{h} > 1) {
467 # bigfaces
468 my $maxx = $x + $tile->{w} - 1;
469 my $maxy = $y + $tile->{h} - 1;
470
471 for my $ox (0 .. $tile->{w} - 1) {
472 for my $oy (0 .. $tile->{h} - 1) {
473 my $ext = $ext->[$x + $ox][$y + $oy];
474
475 $ext->[0] = min $ext->[0], $x;
476 $ext->[1] = min $ext->[1], $y;
477 $ext->[2] = max $ext->[2], $maxx;
478 $ext->[3] = max $ext->[3], $maxy;
479
480 push @ov, [$x + $ox, $y + $oy,
481 $tile->{idx} + $ox + $oy * $tile->{w}];
482 }
483 }
484
485 } elsif ($o->{more}) {
486 # linked faces, slowest and most annoying
487
488 my ($minx, $miny, $maxx, $maxy);
489
490 for (my $o = $o; $o; $o = $o->{more}) {
491 $minx = min $minx, $o->{x};
492 $miny = min $miny, $o->{y};
493 $maxx = max $maxx, $o->{x};
494 $maxy = max $maxy, $o->{y};
495 }
496
497 $minx += $x; $miny += $y;
498 $maxx += $x; $maxy += $y;
499
500 for (my $o = $o; $o; $o = $o->{more}) {
501 my $tile = $FACE{$o->{face}}
502 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
503
504 my $ext = $ext->[$x + $o->{x}][$y + $o->{y}];
505
506 $ext->[0] = min $ext->[0], $minx;
507 $ext->[1] = min $ext->[1], $miny;
508 $ext->[2] = max $ext->[2], $maxx;
509 $ext->[3] = max $ext->[3], $maxy;
510
511 push @ov, [$x + $o->{x}, $y + $o->{y}, $tile->{idx}];
512 }
513
514 } else {
515 # single face
516 push @$os, $tile->{idx};
517
518 }
519 }
520 }
521 }
522
523 # bigger faces always on top, I don't give a shit to those who think otherwise
524 for (@ov) {
525 my ($x, $y, $idx) = @$_;
526
527 next if $x < 0 || $self->{map}{width} <= $x
528 || $y < 0 || $self->{map}{height} <= $y;
529
530 push @{ $ov->[$x][$y] }, $idx;
531 }
532
533 # dump extent info
534 # for my $x ($x1 .. $x2) {
535 # for my $y ($y1 .. $y2) {
536 # my $ext = $ext->[$x][$y];
537 # if ($ext->[0] != $x || $ext->[1] != $y || $ext->[2] != $x || $ext->[3] != $y) {
538 # warn "EXT $x $y (@$ext)\n";#d#
539 # }
540 # }
541 # }
542 }
543
544 sub change_begin {
545 my ($self, $title) = @_;
546
547 $self->{change} ||= {
548 title => $title,
549 };
550 $self->{change}{nest}++;
551 }
552
553 sub get {
554 my ($self, $x, $y) = @_;
555
556 $self->{map}{map}[$x][$y] || []
557 }
558
559 sub set {
560 my ($self, $x, $y, $as) = @_;
561
562 $self->{map}{map}[$x][$y] = $as;
563
564 $self->{update} ||= [$x, $y, $x, $y];
565
566 for (@$as) {
567 my ($x1, $y1, $x2, $y2) = arch_extents $_;
568
569 $self->{update}[0] = min $self->{update}[0], $x + $x1;
570 $self->{update}[1] = min $self->{update}[1], $y + $y1;
571 $self->{update}[2] = max $self->{update}[2], $x + $x2;
572 $self->{update}[3] = max $self->{update}[3], $y + $y2;
573 }
574
575 $self->{update_idle} ||= add Glib::Idle sub {
576 delete $self->{update_idle};
577 my $coord = delete $self->{update};
578
579 $self->update_map (@$coord);
580
581 0
582 }, undef, 10;
583 }
584
585 sub change_stack {
586 my ($self, $x, $y, $as) = @_;
587
588 $self->{change}{map}[$x][$y] ||= [$x, $y, $self->{map}{map}[$x][$y]];
589
590 $self->set ($x, $y, $as);
591 }
592
593 sub change_end {
594 my ($self) = @_;
595
596 --$self->{change}{nest} and return;
597
598 my $change = delete $self->{change};
599
600 delete $change->{nest};
601
602 $change->{set} = [
603 grep $_,
604 map @$_,
605 grep $_,
606 @{ delete $change->{map} || [] }
607 ];
608
609 @{ $change->{set} } or return;
610
611 $change
612 }
613
614 sub change_swap {
615 my ($self, $change) = @_;
616
617 for (@{ $change->{set} }) {
618 my $stack = \$self->{map}{map}[$_->[0]][$_->[1]];
619
620 ($$stack, $_->[2]) = ($_->[2], $$stack);
621 }
622 }
623
624 =back
625
626 =head1 AUTHOR
627
628 Marc Lehmann <schmorp@schmorp.de>
629
630 =cut
631
632 1
633