ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.48
Committed: Tue Sep 1 21:37:25 2009 UTC (14 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-2_01, rel-1_30, rel-1_29, rel-1_24, rel-1_25, HEAD
Changes since 1.47: +1 -1 lines
Log Message:
1.24

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 common::sense;
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 # warn "yuna ", $_[1]->button, " * ", $_[1]->state, "\n";#d#
99 if (($_[1]->button == 2 || ($_[1]->button == 1 && $_[1]->state * ["mod1-mask", "meta-mask"]))
100 && !$self->{in_drag}) {
101 $self->disable_tooltip;
102
103 $_[0]->grab_focus;
104 $self->{in_drag} = [$self->{x}, $self->{y}, $x, $y];
105 return 1;
106 }
107
108 0
109 });
110
111 $self->signal_connect (motion_notify_event => sub {
112 my ($self) = @_;
113
114 $self->update_tooltip;
115
116 if (my $di = $self->{in_drag}) {
117 my ($x, $y) = $self->get_pointer;
118
119 $self->set_viewport (
120 $di->[0] + $di->[2] - $x,
121 $di->[1] + $di->[3] - $y,
122 );
123
124 return 1;
125 }
126
127 0
128 });
129
130 $self->signal_connect (button_release_event => sub {
131 my ($self) = @_;
132
133 $self->enable_tooltip
134 if delete $self->{in_drag};
135
136 0
137 });
138
139 # gtk+ supports no motion compression, a major lacking feature. we have to pay for the
140 # workaround with incorrect behaviour and extra server-turnarounds.
141 $self->add_events ([qw(button_press_mask button_release_mask button-motion-mask
142 pointer-motion-mask pointer-motion-hint-mask
143 enter-notify-mask leave-notify-mask)]);
144 $self->can_focus (1);
145
146 # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) });
147 }
148
149 sub enable_tooltip {
150 my ($self) = @_;
151
152 $self->{tooltip}++;
153 $self->update_tooltip;
154 }
155
156 sub disable_tooltip {
157 my ($self) = @_;
158
159 $self->{tooltip}--;
160 $self->update_tooltip;
161 }
162
163 sub overlay {
164 my ($self, $name, $x, $y, $w, $h, $cb) = @_;
165
166 if (my $ov = delete $self->{overlay}{$name}) {
167 my ($x, $y, $w, $h) = @$ov;
168
169 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
170 }
171
172 if ($w && $h) {
173 $self->{overlay}{$name} = [$x, $y, $w, $h, $cb];
174
175 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
176 }
177 }
178
179 sub update_tooltip {
180 my ($self) = @_;
181
182 if ($self->{tooltip} >= 0
183 && $self->mapped
184 && $self->get_toplevel->has_toplevel_focus) {
185 my $screen = $self->{window}->get_screen;
186
187 if ($self->{window} == ($screen->get_display->get_window_at_pointer)[0]) {
188 my ($pscreen, $x, $y) = $screen->get_display->get_pointer;
189
190 if ($pscreen == $screen) {
191 if (!$self->{tip}) {
192 $self->{tip} = new Gtk2::Window "popup";
193 $self->{tip}->can_focus (0);
194 $self->{tip}->set_name ("gtk-tooltips");
195 $self->{tip}->set_decorated (0);
196 $self->{tip}->set_border_width (4);
197 $self->{tip}->set_has_frame (0);
198 $self->{tip}->set_resizable (0);
199 $self->{tip}->set_transient_for ($self->get_toplevel);
200 }
201
202 my ($mx, $my) = $self->coord ($self->get_pointer);
203
204 if ($self->{tipinfo}[0] != $mx || $self->{tipinfo}[1] != $my) {
205 $self->fill_tooltip ($mx, $my);
206
207 $self->{tipinfo} = [$mx, $my];
208
209 $self->overlay (_tooltip => $mx * TILESIZE, $my * TILESIZE, TILESIZE, TILESIZE, sub {
210 my ($self, $x, $y) = @_;
211
212 $self->{window}->draw_rectangle ($_ & 1 ? $self->style->black_gc : $self->style->white_gc, 0,
213 $x + $_, $y + $_,
214 TILESIZE - 1 - $_ * 2, TILESIZE - 1 - $_ * 2)
215 for 0..3;
216 });
217
218 my $req = $self->{tip}->size_request;
219 $self->{tip}->resize ($req->width, $req->height);
220 }
221
222 $self->{tip}->move ($x + TILESIZE, $y);
223 $self->{tip}->show_all;
224
225 return;
226 }
227 }
228 }
229
230 $self->overlay ("_tooltip");
231 delete $self->{tipinfo};
232 (delete $self->{tip})->destroy if $self->{tip};
233 }
234
235 sub fill_tooltip {
236 my ($self, $x, $y) = @_;
237
238 $self->{tip}->remove ($self->{tip}->get_children)
239 if $self->{tip}->get_children;
240
241 $self->{tip}->add (my $frame = new Gtk2::Frame "($x|$y)");
242
243 if ($x < 0 || $x >= $self->{map}{width}
244 || $y < 0 || $y >= $self->{map}{height}) {
245 $frame->add (new Gtk2::Label "<off-map>");
246 } else {
247 $frame->add (my $vbox = new Gtk2::VBox 0, 1);
248
249 #TODO: fill tooltip via signal, defaulting to this:
250
251 # fill tooltip with info about $x, $y
252 my $as = $self->{map}{map}[$x][$y] || [];
253 for (reverse @$as) {
254 $vbox->add (my $hbox = new Gtk2::HBox 0, 2);
255
256 # this is awful, is this really the best way?
257 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
258 $pb->fill (0x00000000);
259
260 $TILE->composite ($pb,
261 0, 0,
262 TILESIZE, TILESIZE,
263 - ($_->{_face} % CACHESTRIDE) * TILESIZE, - TILESIZE * int $_->{_face} / CACHESTRIDE,
264 1, 1, 'nearest', 255
265 );
266
267 my $a = $_->{_virtual} || $_;
268
269 $hbox->pack_start ((my $img = new_from_pixbuf Gtk2::Image $pb), 0, 1, 0);
270 $img->set_alignment (0, 0.5);
271
272 my $text = "$a->{_name}";
273 if (my $o = $ARCH{$a->{_name}}) {
274 $text .= "<small>";
275 for my $k (grep /^[^_]/, sort keys %$a) {
276 if ($a->{$k} ne $o->{$k}) {
277 if ($Glib::VERSION < 1.103) {
278 my $t = "\n$k\t$a->{$k}";
279 $t =~ s/&/&amp;/g;
280 $t =~ s/</&lt;/g;
281 $t =~ s/>/&gt;/g;
282 $text .= $t;
283 } else {
284 $text .= Glib::Markup::escape_text ("\n$k\t$a->{$k}");
285 }
286 }
287 }
288 $text .= "</small>";
289 } else {
290 $text .= Glib::Markup::escape_text ("\n<unknown archetype>");
291 }
292
293 $hbox->pack_start (my $label = new Gtk2::Label, 1, 1, 0);
294 $label->set_markup ($text);
295 $label->set_alignment (0, 0.5);
296 }
297 }
298 }
299
300 sub set_viewport {
301 my ($self, $x, $y) = @_;
302
303 my $area = $self->allocation;
304
305 $x = max 0, min $self->{width} - $area->width , $x;
306 $y = max 0, min $self->{height} - $area->height, $y;
307
308 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
309
310 ($self->{x}, $self->{y}) = ($x, $y);
311 }
312
313 sub set_map {
314 my ($self, $map) = @_;
315
316 $self->{map} = $map;
317
318 $self->{width} = $map->{width} * TILESIZE;
319 $self->{height} = $map->{height} * TILESIZE;
320
321 $self->{x} =
322 $self->{y} = 0;
323
324 my $data = delete $map->{map};
325
326 $map->{map} = [];
327
328 for my $x (0 .. $map->{width} - 1) {
329 my $col = $data->[$x];
330 for my $y (0 .. $map->{height} - 1) {
331 $self->set ($x, $y, delete $col->[$y]);
332 }
333 }
334
335 delete $self->{tipinfo}; $self->update_tooltip;
336 $self->invalidate_all;
337 }
338
339 sub coord {
340 my ($self, $x, $y) = @_;
341
342 (
343 int +($self->{x} + $x) / TILESIZE,
344 int +($self->{y} + $y) / TILESIZE,
345 )
346 }
347
348 #sub handle_key {
349 # my ($self, $key, $state) = @_;
350 #
351 # $self->prefetch_cancel;
352 #
353 # if ($state * "control-mask") {
354 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
355 # my @sel = keys %{$self->{sel}};
356 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
357 # }
358 #
359 # 1
360 #}
361
362 sub invalidate {
363 my ($self, $x, $y, $w, $h) = @_;
364
365 return unless $self->{window};
366
367 $self->queue_draw_area (
368 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
369 );
370 }
371
372 sub invalidate_all {
373 my ($self) = @_;
374
375 $self->queue_draw;
376 }
377
378 sub expose {
379 my ($self, $event) = @_;
380
381 no integer;
382
383 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
384 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
385
386 # get_rectangles is buggy in older versions
387 my @rectangles = $Gtk2::VERSION >= 1.104
388 ? $event->region->get_rectangles : $event->area;
389
390 for my $area (@rectangles) {
391 my ($x, $y, $w, $h) = $area->values; # x y w h
392
393 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
394 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
395
396 my $window = $self->{window};
397
398 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
399 $pb->fill (0xff69b400);
400
401 for my $x (@x) {
402 my $dx = ($x - $x[0]) * TILESIZE;
403 my $oss = $self->{map}{map}[$x];
404
405 for my $y (@y) {
406 my $dy = ($y - $y[0]) * TILESIZE;
407
408 for my $a (@{$oss->[$y]}) {
409 $TILE->composite ($pb,
410 $dx, $dy,
411 TILESIZE, TILESIZE,
412 $dx - ($a->{_face} % CACHESTRIDE) * TILESIZE, $dy - TILESIZE * int $a->{_face} / CACHESTRIDE,
413 1, 1, 'nearest', 255
414 );
415 }
416 }
417 }
418
419 $pb->render_to_drawable ($window, $self->style->black_gc,
420 0, 0,
421 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
422 TILESIZE * @x, TILESIZE * @y,
423 'max', 0, 0);
424 }
425
426 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
427 for values %{ $self->{overlay} || {} };
428 }
429
430 # get head from _virtual tile, returning x, y, z and @$stack
431 sub get_head {
432 my ($self, $virtual) = @_;
433
434 my ($x, $y) = @$virtual{qw(_virtual_x _virtual_y)}
435 or return;
436
437 my $stack = $self->{map}{map}[$x][$y]
438 or return;
439
440 my ($z) = grep $stack->[$_] == $virtual->{_virtual}, 0..$#$stack
441 or return;
442
443 ($x, $y, $z, $self->get ($x, $y))
444 }
445
446 sub get {
447 my ($self, $x, $y) = @_;
448
449 return unless $x >= 0 && $x < $self->{map}{width}
450 && $y >= 0 && $y < $self->{map}{height};
451
452 Storable::dclone [
453 map +{ %$_, ((exists $_->{_virtual}) ? (_virtual => 0+$_->{_virtual}) : ()) },
454 @{ $self->{map}{map}[$x][$y] || [] }
455 ]
456 }
457
458 # the caller promises us that he won't, in no circumstances,
459 # change the stack he gets.
460 sub get_ro {
461 my ($self, $x, $y) = @_;
462
463 return unless $x >= 0 && $x < $self->{map}{width}
464 && $y >= 0 && $y < $self->{map}{height};
465
466 [
467 map +{ %$_, ((exists $_->{_virtual}) ? (_virtual => 0+$_->{_virtual}) : ()) },
468 @{ $self->{map}{map}[$x][$y] || [] }
469 ]
470 }
471
472 sub set {
473 my ($self, $x, $y, $as) = @_;
474
475 my $data = $self->{map}{map};
476
477 my $prev_as = $data->[$x][$y] || [];
478
479 my ($x1, $y1, $x2, $y2) = ($x, $y) x 2;
480
481 # remove possible overlay tiles
482 for my $a (@$prev_as) {
483 next if $a->{_virtual};
484
485 if (my $more = $a->{_more}) {
486 for (@$more) {
487 my ($x, $y) = @$_;
488
489 $x1 = min $x1, $x; $y1 = min $y1, $y;
490 $x2 = max $x2, $x; $y2 = max $y2, $y;
491
492 $data->[$x][$y] = [ grep $_->{_virtual} != $a, @{ $data->[$x][$y] } ];
493 }
494 }
495 }
496
497 # preserve our overlay tiles, put them on top
498 $as = [
499 (grep !$_->{_virtual}, @$as),
500 (grep $_->{_virtual}, @$prev_as),
501 ];
502
503 for my $a (@$as) {
504 next if $a->{_virtual};
505
506 my $o = $ARCH{$a->{_name}} || $ARCH{empty_archetype}
507 or (warn "archetype $a->{_name} is unknown at ($x|$y)\n"), next;
508
509 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
510 unless ($face) {
511 $face = $FACE{"blank.x11"}
512 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
513 }
514
515 $a->{_face} = $face->{idx};
516
517 if ($face->{w} > 1 || $face->{h} > 1) {
518 # bigfaces
519
520 $x2 = max $x2, $x + $face->{w} - 1;
521 $y2 = max $y2, $y + $face->{h} - 1;
522
523 for my $ox (0 .. $face->{w} - 1) {
524 for my $oy (0 .. $face->{h} - 1) {
525 next unless $ox || $oy;
526
527 push @{ $a->{_more} }, [$x+$ox, $y+$oy];
528 push @{ $data->[$x+$ox][$y+$oy] }, {
529 _virtual => $a,
530 _virtual_x => $x,
531 _virtual_y => $y,
532 _face => $face->{idx} + $ox + $oy * $face->{w},
533 };
534 }
535 }
536
537 } elsif ($o->{more}) {
538 # linked faces, slowest and most annoying
539
540 while ($o = $o->{more}) {
541 my $face = $FACE{$o->{face} || "blank.111"};
542 unless ($face) {
543 $face = $FACE{"blank.x11"}
544 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
545 }
546
547 $x1 = min $x1, $x + $o->{x}; $y1 = min $y1, $y + $o->{y};
548 $x2 = max $x2, $x + $o->{x}; $y2 = max $y2, $y + $o->{y};
549
550 push @{ $a->{_more} }, [$x + $o->{x}, $y + $o->{y}];
551 push @{ $data->[$x+$o->{x}][$y+$o->{y}] }, {
552 _virtual => $a,
553 _virtual_x => $x,
554 _virtual_y => $y,
555 _face => $face->{idx},
556 };
557 }
558 }
559 }
560
561 $data->[$x][$y] = $as;
562
563 $self->queue_draw_area (
564 $x1 * TILESIZE - $self->{x}, $y1 * TILESIZE - $self->{y},
565 ($x2 - $x1 + 1) * TILESIZE, ($y2 - $y1 + 1) * TILESIZE,
566 );
567
568 delete $self->{tipinfo}; $self->update_tooltip;
569
570 }
571
572 sub change_begin {
573 my ($self, $title) = @_;
574
575 $self->{change} ||= {
576 title => $title,
577 };
578 $self->{change}{nest}++;
579 }
580
581 sub change_stack {
582 my ($self, $x, $y, $as) = @_;
583
584 $self->{change}{map}[$x][$y] ||= [$x, $y, $self->{map}{map}[$x][$y]];
585
586 $self->signal_emit (stack_change => $x, $y, $as);
587 }
588
589 sub change_end {
590 my ($self) = @_;
591
592 --$self->{change}{nest} and return;
593
594 my $change = delete $self->{change};
595
596 delete $change->{nest};
597
598 $change->{set} = [
599 grep $_,
600 map @$_,
601 grep $_,
602 @{ delete $change->{map} || [] }
603 ];
604
605 @{ $change->{set} } or return;
606
607 $change
608 }
609
610 sub change_swap {
611 my ($self, $change) = @_;
612
613 for (@{ $change->{set} }) {
614 my $stack = $self->get ($_->[0], $_->[1]);
615 $self->set ($_->[0], $_->[1], $_->[2]);
616 $self->signal_emit (swap_stack_change => $_->[0], $_->[1], $_->[2]);
617 $_->[2] = $stack;
618 }
619
620 $self->invalidate_all;
621 }
622
623 =back
624
625 =head1 AUTHOR
626
627 Marc Lehmann <schmorp@schmorp.de>
628
629 =cut
630
631 1
632