ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.34
Committed: Mon Apr 17 08:30:46 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.33: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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