ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.45
Committed: Tue Jan 13 06:13:11 2009 UTC (15 years, 4 months ago) by root
Branch: MAIN
Changes since 1.44: +0 -1 lines
Log Message:
*** empty log message ***

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