ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.45
Committed: Tue Jan 13 06:13:11 2009 UTC (15 years, 4 months ago) by root
Branch: MAIN
Changes since 1.44: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Deliantra::MapWidget - Gtk2 widget displaying cf maps
4
5 =head1 SYNOPSIS
6
7 use Deliantra::MapWidget;
8
9 =head1 DESCRIPTION
10
11 =head2 METHODS
12
13 =over 4
14
15 =cut
16
17 package Deliantra::MapWidget;
18
19 use strict;
20
21 use Glib;
22 use Gtk2;
23 use Storable ();
24
25 use Deliantra;
26
27 use Glib::Object::Subclass
28 'Gtk2::DrawingArea',
29 signals => {
30 stack_change => {
31 flags => [qw/run-last/],
32 return_type => undef,
33 param_types => ["Glib::Int", "Glib::Int", "Glib::Scalar"],
34 class_closure => \&set,
35 },
36 swap_stack_change => {
37 flags => [qw/run-last/],
38 return_type => undef,
39 param_types => ["Glib::Int", "Glib::Int", "Glib::Scalar"],
40 #class_closure => \&set,
41 },
42 };
43
44 use List::Util qw(min max);
45
46 sub INIT_INSTANCE {
47 my ($self) = @_;
48
49 $self->signal_connect (destroy => sub {
50 my ($self) = @_;
51
52 $self->{tip}->destroy if $self->{tip};
53
54 %$self = ();
55
56 0
57 });
58 $self->signal_connect (realize => sub {
59 my ($self) = @_;
60
61 $self->{window} = $self->window;
62
63 1
64 });
65
66 $self->set_redraw_on_allocate (0);
67 $self->double_buffered (0);
68
69 $self->{tooltip} = -1; # need focus in first
70
71 # reduces unnecessary redraws
72 $self->signal_connect (focus_in_event => sub { $self->enable_tooltip; 1 });
73 $self->signal_connect (focus_out_event => sub { $self->disable_tooltip; 1 });
74
75 $self->signal_connect_after (enter_notify_event => sub { $self->update_tooltip; 0 });
76 $self->signal_connect_after (leave_notify_event => sub { $self->update_tooltip; 0 });
77
78 $self->signal_connect (size_request => sub {
79 $_[1]->width (TILESIZE);
80 $_[1]->height (TILESIZE);
81
82 1
83 });
84
85 $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 });
86
87 $self->signal_connect_after (configure_event => sub {
88 $self->set_viewport ($self->{x}, $self->{y});
89
90 0
91 });
92
93 $self->signal_connect (button_press_event => sub {
94 my ($self, $event) = @_;
95
96 my ($x, $y) = ($event->x, $event->y);
97
98 if (($_[1]->button == 2 || ($_[1]->button == 1 && $_[1]->state * ["mod1-mask", "meta-mask"]))
99 && !$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 ($w && $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 0, 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 (reverse @$as) {
253 $vbox->add (my $hbox = new Gtk2::HBox 0, 2);
254
255 # this is awful, is this really the best way?
256 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
257 $pb->fill (0x00000000);
258
259 $TILE->composite ($pb,
260 0, 0,
261 TILESIZE, TILESIZE,
262 - ($_->{_face} % CACHESTRIDE) * TILESIZE, - TILESIZE * int $_->{_face} / CACHESTRIDE,
263 1, 1, 'nearest', 255
264 );
265
266 my $a = $_->{_virtual} || $_;
267
268 $hbox->pack_start ((my $img = new_from_pixbuf Gtk2::Image $pb), 0, 1, 0);
269 $img->set_alignment (0, 0.5);
270
271 my $text = "$a->{_name}";
272 if (my $o = $ARCH{$a->{_name}}) {
273 $text .= "<small>";
274 for my $k (grep /^[^_]/, sort keys %$a) {
275 if ($a->{$k} ne $o->{$k}) {
276 if ($Glib::VERSION < 1.103) {
277 my $t = "\n$k\t$a->{$k}";
278 $t =~ s/&/&amp;/g;
279 $t =~ s/</&lt;/g;
280 $t =~ s/>/&gt;/g;
281 $text .= $t;
282 } else {
283 $text .= Glib::Markup::escape_text ("\n$k\t$a->{$k}");
284 }
285 }
286 }
287 $text .= "</small>";
288 } else {
289 $text .= Glib::Markup::escape_text ("\n<unknown archetype>");
290 }
291
292 $hbox->pack_start (my $label = new Gtk2::Label, 1, 1, 0);
293 $label->set_markup ($text);
294 $label->set_alignment (0, 0.5);
295 }
296 }
297 }
298
299 sub set_viewport {
300 my ($self, $x, $y) = @_;
301
302 my $area = $self->allocation;
303
304 $x = max 0, min $self->{width} - $area->width , $x;
305 $y = max 0, min $self->{height} - $area->height, $y;
306
307 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
308
309 ($self->{x}, $self->{y}) = ($x, $y);
310 }
311
312 sub set_map {
313 my ($self, $map) = @_;
314
315 $self->{map} = $map;
316
317 $self->{width} = $map->{width} * TILESIZE;
318 $self->{height} = $map->{height} * TILESIZE;
319
320 $self->{x} =
321 $self->{y} = 0;
322
323 my $data = delete $map->{map};
324
325 $map->{map} = [];
326
327 for my $x (0 .. $map->{width} - 1) {
328 my $col = $data->[$x];
329 for my $y (0 .. $map->{height} - 1) {
330 $self->set ($x, $y, delete $col->[$y]);
331 }
332 }
333
334 delete $self->{tipinfo}; $self->update_tooltip;
335 $self->invalidate_all;
336 }
337
338 sub coord {
339 my ($self, $x, $y) = @_;
340
341 (
342 int +($self->{x} + $x) / TILESIZE,
343 int +($self->{y} + $y) / TILESIZE,
344 )
345 }
346
347 #sub handle_key {
348 # my ($self, $key, $state) = @_;
349 #
350 # $self->prefetch_cancel;
351 #
352 # if ($state * "control-mask") {
353 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
354 # my @sel = keys %{$self->{sel}};
355 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
356 # }
357 #
358 # 1
359 #}
360
361 sub invalidate {
362 my ($self, $x, $y, $w, $h) = @_;
363
364 return unless $self->{window};
365
366 $self->queue_draw_area (
367 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
368 );
369 }
370
371 sub invalidate_all {
372 my ($self) = @_;
373
374 $self->queue_draw;
375 }
376
377 sub expose {
378 my ($self, $event) = @_;
379
380 no integer;
381
382 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
383 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
384
385 # get_rectangles is buggy in older versions
386 my @rectangles = $Gtk2::VERSION >= 1.104
387 ? $event->region->get_rectangles : $event->area;
388
389 for my $area (@rectangles) {
390 my ($x, $y, $w, $h) = $area->values; # x y w h
391
392 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
393 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
394
395 my $window = $self->{window};
396
397 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
398 $pb->fill (0xff69b400);
399
400 for my $x (@x) {
401 my $dx = ($x - $x[0]) * TILESIZE;
402 my $oss = $self->{map}{map}[$x];
403
404 for my $y (@y) {
405 my $dy = ($y - $y[0]) * TILESIZE;
406
407 for my $a (@{$oss->[$y]}) {
408 $TILE->composite ($pb,
409 $dx, $dy,
410 TILESIZE, TILESIZE,
411 $dx - ($a->{_face} % CACHESTRIDE) * TILESIZE, $dy - TILESIZE * int $a->{_face} / CACHESTRIDE,
412 1, 1, 'nearest', 255
413 );
414 }
415 }
416 }
417
418 $pb->render_to_drawable ($window, $self->style->black_gc,
419 0, 0,
420 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
421 TILESIZE * @x, TILESIZE * @y,
422 'max', 0, 0);
423 }
424
425 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
426 for values %{ $self->{overlay} || {} };
427 }
428
429 # get head from _virtual tile, returning x, y, z and @$stack
430 sub get_head {
431 my ($self, $virtual) = @_;
432
433 my ($x, $y) = @$virtual{qw(_virtual_x _virtual_y)}
434 or return;
435
436 my $stack = $self->{map}{map}[$x][$y]
437 or return;
438
439 my ($z) = grep $stack->[$_] == $virtual->{_virtual}, 0..$#$stack
440 or return;
441
442 ($x, $y, $z, $self->get ($x, $y))
443 }
444
445 sub get {
446 my ($self, $x, $y) = @_;
447
448 return unless $x >= 0 && $x < $self->{map}{width}
449 && $y >= 0 && $y < $self->{map}{height};
450
451 Storable::dclone [
452 map +{ %$_, ((exists $_->{_virtual}) ? (_virtual => 0+$_->{_virtual}) : ()) },
453 @{ $self->{map}{map}[$x][$y] || [] }
454 ]
455 }
456
457 # the caller promises us that he won't, in no circumstances,
458 # change the stack he gets.
459 sub get_ro {
460 my ($self, $x, $y) = @_;
461
462 return unless $x >= 0 && $x < $self->{map}{width}
463 && $y >= 0 && $y < $self->{map}{height};
464
465 [
466 map +{ %$_, ((exists $_->{_virtual}) ? (_virtual => 0+$_->{_virtual}) : ()) },
467 @{ $self->{map}{map}[$x][$y] || [] }
468 ]
469 }
470
471 sub set {
472 my ($self, $x, $y, $as) = @_;
473
474 my $data = $self->{map}{map};
475
476 my $prev_as = $data->[$x][$y] || [];
477
478 my ($x1, $y1, $x2, $y2) = ($x, $y) x 2;
479
480 # remove possible overlay tiles
481 for my $a (@$prev_as) {
482 next if $a->{_virtual};
483
484 if (my $more = $a->{_more}) {
485 for (@$more) {
486 my ($x, $y) = @$_;
487
488 $x1 = min $x1, $x; $y1 = min $y1, $y;
489 $x2 = max $x2, $x; $y2 = max $y2, $y;
490
491 $data->[$x][$y] = [ grep $_->{_virtual} != $a, @{ $data->[$x][$y] } ];
492 }
493 }
494 }
495
496 # preserve our overlay tiles, put them on top
497 $as = [
498 (grep !$_->{_virtual}, @$as),
499 (grep $_->{_virtual}, @$prev_as),
500 ];
501
502 for my $a (@$as) {
503 next if $a->{_virtual};
504
505 my $o = $ARCH{$a->{_name}} || $ARCH{empty_archetype}
506 or (warn "archetype $a->{_name} is unknown at ($x|$y)\n"), next;
507
508 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
509 unless ($face) {
510 $face = $FACE{"blank.x11"}
511 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
512 }
513
514 $a->{_face} = $face->{idx};
515
516 if ($face->{w} > 1 || $face->{h} > 1) {
517 # bigfaces
518
519 $x2 = max $x2, $x + $face->{w} - 1;
520 $y2 = max $y2, $y + $face->{h} - 1;
521
522 for my $ox (0 .. $face->{w} - 1) {
523 for my $oy (0 .. $face->{h} - 1) {
524 next unless $ox || $oy;
525
526 push @{ $a->{_more} }, [$x+$ox, $y+$oy];
527 push @{ $data->[$x+$ox][$y+$oy] }, {
528 _virtual => $a,
529 _virtual_x => $x,
530 _virtual_y => $y,
531 _face => $face->{idx} + $ox + $oy * $face->{w},
532 };
533 }
534 }
535
536 } elsif ($o->{more}) {
537 # linked faces, slowest and most annoying
538
539 while ($o = $o->{more}) {
540 my $face = $FACE{$o->{face} || "blank.111"};
541 unless ($face) {
542 $face = $FACE{"blank.x11"}
543 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
544 }
545
546 $x1 = min $x1, $x + $o->{x}; $y1 = min $y1, $y + $o->{y};
547 $x2 = max $x2, $x + $o->{x}; $y2 = max $y2, $y + $o->{y};
548
549 push @{ $a->{_more} }, [$x + $o->{x}, $y + $o->{y}];
550 push @{ $data->[$x+$o->{x}][$y+$o->{y}] }, {
551 _virtual => $a,
552 _virtual_x => $x,
553 _virtual_y => $y,
554 _face => $face->{idx},
555 };
556 }
557 }
558 }
559
560 $data->[$x][$y] = $as;
561
562 $self->queue_draw_area (
563 $x1 * TILESIZE - $self->{x}, $y1 * TILESIZE - $self->{y},
564 ($x2 - $x1 + 1) * TILESIZE, ($y2 - $y1 + 1) * TILESIZE,
565 );
566
567 delete $self->{tipinfo}; $self->update_tooltip;
568
569 }
570
571 sub change_begin {
572 my ($self, $title) = @_;
573
574 $self->{change} ||= {
575 title => $title,
576 };
577 $self->{change}{nest}++;
578 }
579
580 sub change_stack {
581 my ($self, $x, $y, $as) = @_;
582
583 $self->{change}{map}[$x][$y] ||= [$x, $y, $self->{map}{map}[$x][$y]];
584
585 $self->signal_emit (stack_change => $x, $y, $as);
586 }
587
588 sub change_end {
589 my ($self) = @_;
590
591 --$self->{change}{nest} and return;
592
593 my $change = delete $self->{change};
594
595 delete $change->{nest};
596
597 $change->{set} = [
598 grep $_,
599 map @$_,
600 grep $_,
601 @{ delete $change->{map} || [] }
602 ];
603
604 @{ $change->{set} } or return;
605
606 $change
607 }
608
609 sub change_swap {
610 my ($self, $change) = @_;
611
612 for (@{ $change->{set} }) {
613 my $stack = $self->get ($_->[0], $_->[1]);
614 $self->set ($_->[0], $_->[1], $_->[2]);
615 $self->signal_emit (swap_stack_change => $_->[0], $_->[1], $_->[2]);
616 $_->[2] = $stack;
617 }
618
619 $self->invalidate_all;
620 }
621
622 =back
623
624 =head1 AUTHOR
625
626 Marc Lehmann <schmorp@schmorp.de>
627
628 =cut
629
630 1
631