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