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