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