ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.41
Committed: Sat Aug 25 19:25:56 2007 UTC (16 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.40: +1 -1 lines
Log Message:
fixed minor bug in get_head

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