ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.43
Committed: Wed Dec 26 18:05:00 2007 UTC (16 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-1_222, rel-1_221, rel-1_2, rel-1_22
Changes since 1.42: +4 -4 lines
Log Message:
initial deliantra check-in

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.43 Deliantra::MapWidget - Gtk2 widget displaying cf maps
4 root 1.1
5     =head1 SYNOPSIS
6    
7 root 1.43 use Deliantra::MapWidget;
8 root 1.1
9     =head1 DESCRIPTION
10    
11     =head2 METHODS
12    
13     =over 4
14    
15     =cut
16    
17 root 1.43 package Deliantra::MapWidget;
18 root 1.1
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 root 1.43 use Deliantra;
26 root 1.1
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 elmex 1.39 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 root 1.31 };
43 root 1.1
44     use List::Util qw(min max);
45    
46     sub INIT_INSTANCE {
47     my ($self) = @_;
48    
49 root 1.7 $self->signal_connect (destroy => sub {
50     my ($self) = @_;
51 root 1.8
52 root 1.7 $self->{tip}->destroy if $self->{tip};
53 root 1.8
54 root 1.7 %$self = ();
55 root 1.8
56 root 1.7 0
57     });
58 root 1.1 $self->signal_connect (realize => sub {
59     my ($self) = @_;
60    
61     $self->{window} = $self->window;
62    
63     1
64     });
65    
66 root 1.7 $self->set_redraw_on_allocate (0);
67 root 1.1 $self->double_buffered (0);
68    
69 root 1.9 $self->{tooltip} = -1; # need focus in first
70    
71 root 1.7 # reduces unnecessary redraws
72 root 1.9 $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 root 1.7
78 root 1.1 $self->signal_connect (size_request => sub {
79 root 1.10 $_[1]->width (TILESIZE);
80     $_[1]->height (TILESIZE);
81 root 1.1
82     1
83     });
84    
85 root 1.2 $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 });
86 root 1.1
87     $self->signal_connect_after (configure_event => sub {
88     $self->set_viewport ($self->{x}, $self->{y});
89 root 1.2
90     0
91 root 1.1 });
92    
93     $self->signal_connect (button_press_event => sub {
94     my ($self, $event) = @_;
95    
96 root 1.5 my ($x, $y) = ($event->x, $event->y);
97 root 1.2
98 root 1.5 if ($_[1]->button == 2 && !$self->{in_drag}) {
99     $self->disable_tooltip;
100 root 1.1
101     $_[0]->grab_focus;
102     $self->{in_drag} = [$self->{x}, $self->{y}, $x, $y];
103 root 1.2 return 1;
104 root 1.1 }
105    
106 root 1.2 0
107 root 1.1 });
108    
109     $self->signal_connect (motion_notify_event => sub {
110     my ($self) = @_;
111    
112 root 1.2 $self->update_tooltip;
113 root 1.1
114     if (my $di = $self->{in_drag}) {
115 root 1.2 my ($x, $y) = $self->get_pointer;
116    
117 root 1.1 $self->set_viewport (
118     $di->[0] + $di->[2] - $x,
119     $di->[1] + $di->[3] - $y,
120     );
121    
122 root 1.2 return 1;
123 root 1.1 }
124    
125 root 1.2 0
126 root 1.1 });
127    
128     $self->signal_connect (button_release_event => sub {
129     my ($self) = @_;
130    
131 root 1.5 $self->enable_tooltip
132     if delete $self->{in_drag};
133 root 1.2
134 root 1.5 0
135 root 1.1 });
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 root 1.8 $self->add_events ([qw(button_press_mask button_release_mask button-motion-mask
140 root 1.2 pointer-motion-mask pointer-motion-hint-mask
141     enter-notify-mask leave-notify-mask)]);
142 root 1.1 $self->can_focus (1);
143    
144     # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) });
145     }
146    
147 root 1.2 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 root 1.3 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 root 1.27 if ($w && $h) {
171 root 1.3 $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 root 1.2 sub update_tooltip {
178     my ($self) = @_;
179    
180 root 1.9 if ($self->{tooltip} >= 0
181     && $self->mapped
182     && $self->get_toplevel->has_toplevel_focus) {
183 root 1.2 my $screen = $self->{window}->get_screen;
184    
185 root 1.9 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 root 1.2
200 root 1.9 my ($mx, $my) = $self->coord ($self->get_pointer);
201 root 1.2
202 root 1.9 if ($self->{tipinfo}[0] != $mx || $self->{tipinfo}[1] != $my) {
203     $self->fill_tooltip ($mx, $my);
204 root 1.2
205 root 1.9 $self->{tipinfo} = [$mx, $my];
206 root 1.3
207 root 1.9 $self->overlay (_tooltip => $mx * TILESIZE, $my * TILESIZE, TILESIZE, TILESIZE, sub {
208     my ($self, $x, $y) = @_;
209 root 1.3
210 root 1.9 $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 root 1.18 });
215 root 1.3
216 root 1.18 my $req = $self->{tip}->size_request;
217     $self->{tip}->resize ($req->width, $req->height);
218 root 1.9 }
219 root 1.3
220 root 1.9 $self->{tip}->move ($x + TILESIZE, $y);
221 root 1.3 $self->{tip}->show_all;
222 root 1.9
223     return;
224 root 1.2 }
225     }
226     }
227    
228 root 1.3 $self->overlay ("_tooltip");
229 root 1.2 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 root 1.32 $frame->add (new Gtk2::Label "<off-map>");
244 root 1.2 } else {
245 root 1.32 $frame->add (my $vbox = new Gtk2::VBox 0, 1);
246 root 1.2
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 root 1.19 for (reverse @$as) {
252 root 1.6 $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 root 1.18 $TILE->composite ($pb,
259 root 1.6 0, 0,
260     TILESIZE, TILESIZE,
261 root 1.37 - ($_->{_face} % CACHESTRIDE) * TILESIZE, - TILESIZE * int $_->{_face} / CACHESTRIDE,
262 root 1.6 1, 1, 'nearest', 255
263     );
264    
265 root 1.32 my $a = $_->{_virtual} || $_;
266    
267     $hbox->pack_start ((my $img = new_from_pixbuf Gtk2::Image $pb), 0, 1, 0);
268 root 1.6 $img->set_alignment (0, 0.5);
269    
270 root 1.32 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 root 1.33 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 root 1.34 $text .= Glib::Markup::escape_text ("\n$k\t$a->{$k}");
283 root 1.33 }
284 root 1.32 }
285     }
286     $text .= "</small>";
287     } else {
288 elmex 1.38 $text .= Glib::Markup::escape_text ("\n<unknown archetype>");
289 root 1.32 }
290    
291     $hbox->pack_start (my $label = new Gtk2::Label, 1, 1, 0);
292     $label->set_markup ($text);
293 root 1.5 $label->set_alignment (0, 0.5);
294 root 1.2 }
295     }
296     }
297    
298 root 1.1 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 root 1.19 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 root 1.11 delete $self->{tipinfo}; $self->update_tooltip;
334 root 1.1 $self->invalidate_all;
335     }
336    
337     sub coord {
338     my ($self, $x, $y) = @_;
339    
340     (
341 root 1.4 int +($self->{x} + $x) / TILESIZE,
342     int +($self->{y} + $y) / TILESIZE,
343 root 1.1 )
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 root 1.18 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 root 1.22 my @rectangles = $Gtk2::VERSION >= 1.104
386 root 1.18 ? $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 root 1.19 my $oss = $self->{map}{map}[$x];
402 root 1.18
403     for my $y (@y) {
404     my $dy = ($y - $y[0]) * TILESIZE;
405    
406 root 1.19 for my $a (@{$oss->[$y]}) {
407 root 1.18 $TILE->composite ($pb,
408     $dx, $dy,
409     TILESIZE, TILESIZE,
410 root 1.37 $dx - ($a->{_face} % CACHESTRIDE) * TILESIZE, $dy - TILESIZE * int $a->{_face} / CACHESTRIDE,
411 root 1.18 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 root 1.40 # get head from _virtual tile, returning x, y, z and @$stack
429     sub get_head {
430     my ($self, $virtual) = @_;
431    
432 elmex 1.41 my ($x, $y) = @$virtual{qw(_virtual_x _virtual_y)}
433 root 1.40 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 root 1.18 sub get {
445     my ($self, $x, $y) = @_;
446    
447 root 1.21 return unless $x >= 0 && $x < $self->{map}{width}
448     && $y >= 0 && $y < $self->{map}{height};
449    
450 root 1.40 Storable::dclone [
451     map +{ %$_, ((exists $_->{_virtual}) ? (_virtual => 0+$_->{_virtual}) : ()) },
452     @{ $self->{map}{map}[$x][$y] || [] }
453     ]
454 root 1.18 }
455    
456 elmex 1.42 # the caller promises us that he won't, in no circumstances,
457     # change the stack he gets.
458     sub get_ro {
459     my ($self, $x, $y) = @_;
460    
461     return unless $x >= 0 && $x < $self->{map}{width}
462     && $y >= 0 && $y < $self->{map}{height};
463    
464     [
465     map +{ %$_, ((exists $_->{_virtual}) ? (_virtual => 0+$_->{_virtual}) : ()) },
466     @{ $self->{map}{map}[$x][$y] || [] }
467     ]
468     }
469    
470 root 1.18 sub set {
471     my ($self, $x, $y, $as) = @_;
472    
473 root 1.19 my $data = $self->{map}{map};
474    
475     my $prev_as = $data->[$x][$y] || [];
476    
477     my ($x1, $y1, $x2, $y2) = ($x, $y) x 2;
478    
479     # remove possible overlay tiles
480     for my $a (@$prev_as) {
481     next if $a->{_virtual};
482    
483     if (my $more = $a->{_more}) {
484     for (@$more) {
485     my ($x, $y) = @$_;
486    
487     $x1 = min $x1, $x; $y1 = min $y1, $y;
488     $x2 = max $x2, $x; $y2 = max $y2, $y;
489 elmex 1.35
490 root 1.19 $data->[$x][$y] = [ grep $_->{_virtual} != $a, @{ $data->[$x][$y] } ];
491     }
492     }
493     }
494    
495     # preserve our overlay tiles, put them on top
496     $as = [
497     (grep !$_->{_virtual}, @$as),
498     (grep $_->{_virtual}, @$prev_as),
499     ];
500    
501     for my $a (@$as) {
502     next if $a->{_virtual};
503    
504 root 1.29 my $o = $ARCH{$a->{_name}} || $ARCH{empty_archetype}
505     or (warn "archetype $a->{_name} is unknown at ($x|$y)\n"), next;
506 root 1.19
507 elmex 1.36 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
508     unless ($face) {
509     $face = $FACE{"blank.x11"}
510     or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
511     }
512 root 1.18
513 root 1.19 $a->{_face} = $face->{idx};
514    
515     if ($face->{w} > 1 || $face->{h} > 1) {
516     # bigfaces
517    
518 root 1.25 $x2 = max $x2, $x + $face->{w} - 1;
519     $y2 = max $y2, $y + $face->{h} - 1;
520    
521 root 1.19 for my $ox (0 .. $face->{w} - 1) {
522     for my $oy (0 .. $face->{h} - 1) {
523 root 1.25 next unless $ox || $oy;
524    
525 root 1.20 push @{ $a->{_more} }, [$x+$ox, $y+$oy];
526 root 1.19 push @{ $data->[$x+$ox][$y+$oy] }, {
527     _virtual => $a,
528     _virtual_x => $x,
529     _virtual_y => $y,
530     _face => $face->{idx} + $ox + $oy * $face->{w},
531     };
532     }
533     }
534 root 1.18
535 root 1.19 } elsif ($o->{more}) {
536     # linked faces, slowest and most annoying
537 root 1.18
538 root 1.19 while ($o = $o->{more}) {
539 elmex 1.36 my $face = $FACE{$o->{face} || "blank.111"};
540     unless ($face) {
541     $face = $FACE{"blank.x11"}
542     or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
543     }
544 root 1.19
545 root 1.26 $x1 = min $x1, $x + $o->{x}; $y1 = min $y1, $y + $o->{y};
546     $x2 = max $x2, $x + $o->{x}; $y2 = max $y2, $y + $o->{y};
547 root 1.19
548 root 1.26 push @{ $a->{_more} }, [$x + $o->{x}, $y + $o->{y}];
549 root 1.19 push @{ $data->[$x+$o->{x}][$y+$o->{y}] }, {
550     _virtual => $a,
551     _virtual_x => $x,
552     _virtual_y => $y,
553     _face => $face->{idx},
554     };
555     }
556     }
557 root 1.18 }
558    
559 root 1.19 $data->[$x][$y] = $as;
560 root 1.18
561 root 1.19 $self->queue_draw_area (
562     $x1 * TILESIZE - $self->{x}, $y1 * TILESIZE - $self->{y},
563 root 1.25 ($x2 - $x1 + 1) * TILESIZE, ($y2 - $y1 + 1) * TILESIZE,
564 root 1.19 );
565    
566     delete $self->{tipinfo}; $self->update_tooltip;
567 root 1.18
568     }
569    
570 root 1.23 sub change_begin {
571     my ($self, $title) = @_;
572    
573     $self->{change} ||= {
574     title => $title,
575     };
576     $self->{change}{nest}++;
577     }
578    
579 root 1.18 sub change_stack {
580     my ($self, $x, $y, $as) = @_;
581 root 1.1
582 root 1.18 $self->{change}{map}[$x][$y] ||= [$x, $y, $self->{map}{map}[$x][$y]];
583 root 1.1
584 root 1.31 $self->signal_emit (stack_change => $x, $y, $as);
585 root 1.18 }
586 root 1.1
587 root 1.18 sub change_end {
588     my ($self) = @_;
589 root 1.11
590 root 1.18 --$self->{change}{nest} and return;
591 root 1.1
592 root 1.18 my $change = delete $self->{change};
593 root 1.1
594 root 1.18 delete $change->{nest};
595 root 1.1
596 root 1.18 $change->{set} = [
597     grep $_,
598     map @$_,
599     grep $_,
600     @{ delete $change->{map} || [] }
601     ];
602 root 1.1
603 root 1.18 @{ $change->{set} } or return;
604 root 1.1
605 root 1.18 $change
606     }
607 root 1.1
608 root 1.18 sub change_swap {
609     my ($self, $change) = @_;
610 root 1.1
611 root 1.18 for (@{ $change->{set} }) {
612 root 1.24 my $stack = $self->get ($_->[0], $_->[1]);
613     $self->set ($_->[0], $_->[1], $_->[2]);
614 elmex 1.39 $self->signal_emit (swap_stack_change => $_->[0], $_->[1], $_->[2]);
615 root 1.24 $_->[2] = $stack;
616     }
617 root 1.1
618 root 1.24 $self->invalidate_all;
619 root 1.1 }
620    
621     =back
622    
623     =head1 AUTHOR
624    
625     Marc Lehmann <schmorp@schmorp.de>
626    
627     =cut
628    
629     1
630