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