ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.3
Committed: Sun Feb 5 22:53:46 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.2: +40 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 });
54
55 $self->signal_connect_after (configure_event => sub {
56 $self->set_viewport ($self->{x}, $self->{y});
57
58 0
59 });
60
61 # 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 $self->signal_connect (button_press_event => sub {
71 my ($self, $event) = @_;
72
73 $self->disable_tooltip;
74
75 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 return 1;
81 }
82
83 0
84 });
85
86 $self->signal_connect (motion_notify_event => sub {
87 my ($self) = @_;
88
89 $self->update_tooltip;
90
91 if (my $di = $self->{in_drag}) {
92 my ($x, $y) = $self->get_pointer;
93
94 $self->set_viewport (
95 $di->[0] + $di->[2] - $x,
96 $di->[1] + $di->[3] - $y,
97 );
98
99 return 1;
100 }
101
102 0
103 });
104
105 $self->signal_connect (button_release_event => sub {
106 my ($self) = @_;
107
108 $self->enable_tooltip;
109
110 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 $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 $self->can_focus (1);
122
123 # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) });
124 }
125
126 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 overlay {
141 my ($self, $name, $x, $y, $w, $h, $cb) = @_;
142
143 if (my $ov = delete $self->{overlay}{$name}) {
144 my ($x, $y, $w, $h) = @$ov;
145
146 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
147 }
148
149 if ($h) {
150 $self->{overlay}{$name} = [$x, $y, $w, $h, $cb];
151
152 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
153 }
154 }
155
156 sub update_tooltip {
157 my ($self) = @_;
158
159 if ($self->{tooltip} >= 0) {
160 my $screen = $self->{window}->get_screen;
161 my ($pscreen, $x, $y) = $screen->get_display->get_pointer;
162
163 if ($pscreen == $screen) {
164 if (!$self->{tip}) {
165 $self->{tip} = new Gtk2::Window "popup";
166 $self->{tip}->can_focus (0);
167 $self->{tip}->set_name ("gtk-tooltips");
168 $self->{tip}->set_decorated (0);
169 $self->{tip}->set_border_width (4);
170 $self->{tip}->set_has_frame (1);
171 }
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
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 }
194
195 $self->{tip}->move ($x + TILESIZE, $y);
196
197 return;
198 }
199 }
200
201 $self->overlay ("_tooltip");
202 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 $frame->set_shadow_type ("none");
215
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 $frame->add (my $vbox = new Gtk2::VBox 1, 2);
221
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 $vbox->add (my $label = new Gtk2::Label $a->{_name});
228 }
229 }
230 }
231
232 sub set_viewport {
233 my ($self, $x, $y) = @_;
234
235 my $area = $self->allocation;
236
237 $x = max 0, min $self->{width} - $area->width , $x;
238 $y = max 0, min $self->{height} - $area->height, $y;
239
240 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
241
242 ($self->{x}, $self->{y}) = ($x, $y);
243 }
244
245 sub set_map {
246 my ($self, $map) = @_;
247
248 $self->{map} = $map;
249
250 $self->{width} = $map->{width} * TILESIZE;
251 $self->{height} = $map->{height} * TILESIZE;
252
253 $self->{x} =
254 $self->{y} = 0;
255
256 delete $self->{face};
257 $self->update_map (0, 0, $self->{width}, $self->{height});
258 delete $self->{tipinfo};
259 $self->update_tooltip;
260 $self->invalidate_all;
261 }
262
263 sub coord {
264 my ($self, $x, $y) = @_;
265
266 (
267 int $self->{x} + $x / TILESIZE,
268 int $self->{y} + $y / TILESIZE,
269 )
270 }
271
272 #sub handle_key {
273 # my ($self, $key, $state) = @_;
274 #
275 # $self->prefetch_cancel;
276 #
277 # if ($state * "control-mask") {
278 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
279 # my @sel = keys %{$self->{sel}};
280 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
281 # } elsif ($key == $Gtk2::Gdk::Keysyms{a}) {
282 # $self->select_all;
283 # } elsif ($key == $Gtk2::Gdk::Keysyms{A}) {
284 # $self->select_range ($self->{offs}, $self->{offs} + $self->{cols} * $self->{page} - 1);
285 # } elsif ($key == $Gtk2::Gdk::Keysyms{s}) {
286 # $self->rescan;
287 # } elsif ($key == $Gtk2::Gdk::Keysyms{d}) {
288 # $self->unlink (keys %{$self->{sel}});
289 # } elsif ($key == $Gtk2::Gdk::Keysyms{u}) {
290 # my @sel = keys %{$self->{sel}};
291 # $self->update_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
292 #
293 # } elsif ($key == $Gtk2::Gdk::Keysyms{Return}) {
294 # $self->cursor_move (0) unless $self->cursor_valid;
295 # $self->emit_activate ($self->{cursor});
296 # } elsif ($key == $Gtk2::Gdk::Keysyms{space}) {
297 # $self->cursor_move (1) or return 1
298 # if $self->{cursor_current} || !$self->cursor_valid;
299 # $self->emit_activate ($self->{cursor}) if $self->cursor_valid;
300 # $self->prefetch (1);
301 # } elsif ($key == $Gtk2::Gdk::Keysyms{BackSpace}) {
302 # $self->cursor_move (-1) or return 1;
303 # $self->emit_activate ($self->{cursor}) if $self->cursor_valid;
304 # $self->prefetch (-1);
305 #
306 # } else {
307 # return 0;
308 # }
309 # } else {
310 # if ($key == $Gtk2::Gdk::Keysyms{Page_Up}) {
311 # my $value = $self->{adj}->value;
312 # $self->{adj}->set_value ($value >= $self->{page} ? $value - $self->{page} : 0);
313 # $self->clear_cursor;
314 # } elsif ($key == $Gtk2::Gdk::Keysyms{Page_Down}) {
315 # my $value = $self->{adj}->value + $self->{page};
316 # $self->{adj}->set_value ($value <= $self->{maxrow} ? $value : $self->{maxrow});
317 # $self->clear_cursor;
318 #
319 # } elsif ($key == $Gtk2::Gdk::Keysyms{Home}) {
320 # $self->{adj}->set_value (0);
321 # $self->clear_cursor;
322 # } elsif ($key == $Gtk2::Gdk::Keysyms{End}) {
323 # $self->{adj}->set_value ($self->{maxrow});
324 # $self->clear_cursor;
325 #
326 # } elsif ($key == $Gtk2::Gdk::Keysyms{Up}) {
327 # $self->cursor_move (-$self->{cols});
328 # } elsif ($key == $Gtk2::Gdk::Keysyms{Down}) {
329 # $self->cursor_move (+$self->{cols});
330 # } elsif ($key == $Gtk2::Gdk::Keysyms{Left}) {
331 # $self->cursor_move (-1);
332 # } elsif ($key == $Gtk2::Gdk::Keysyms{Right}) {
333 # $self->cursor_move (+1);
334 #
335 # } elsif ($key == $Gtk2::Gdk::Keysyms{Return}) {
336 # $self->cursor_move (0) unless $self->cursor_valid;
337 # $self->emit_activate ($self->{cursor});
338 # } elsif ($key == $Gtk2::Gdk::Keysyms{space}) {
339 # $self->cursor_move (1) or return 1
340 # if $self->{cursor_current} || !$self->cursor_valid;
341 # $self->emit_activate ($self->{cursor}) if $self->cursor_valid;
342 # $self->prefetch (1);
343 # } elsif ($key == $Gtk2::Gdk::Keysyms{BackSpace}) {
344 # $self->cursor_move (-1) or return 1;
345 # $self->emit_activate ($self->{cursor}) if $self->cursor_valid;
346 # $self->prefetch (-1);
347 #
348 # } elsif ($key == ord '^') {
349 # $self->updir if exists $self->{dir};
350 #
351 # } elsif (($key >= (ord '0') && $key <= (ord '9'))
352 # || ($key >= (ord 'a') && $key <= (ord 'z'))) {
353 #
354 # $key = chr $key;
355 #
356 # my ($idx, $cursor) = (0, 0);
357 #
358 # $self->clear_selection;
359 #
360 # for my $entry (@{$self->{entry}}) {
361 # $idx++;
362 # $cursor = $idx if $key gt lcfirst $entry->[1];
363 # }
364 #
365 # if ($cursor < @{$self->{entry}}) {
366 # delete $self->{cursor_current};
367 # $self->{sel}{$cursor} = $self->{entry}[$cursor];
368 # $self->{cursor} = $cursor;
369 #
370 # $self->{adj}->set_value (min $self->{maxrow}, $cursor / $self->{cols});
371 # $self->emit_sel_changed;
372 # $self->invalidate_all;
373 # }
374 # } else {
375 # return 0;
376 # }
377 # }
378 #
379 # 1
380 #}
381
382 sub invalidate {
383 my ($self, $x, $y, $w, $h) = @_;
384
385 return unless $self->{window};
386
387 $self->queue_draw_area (
388 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
389 );
390 }
391
392 sub invalidate_all {
393 my ($self) = @_;
394
395 $self->queue_draw;
396 }
397
398 sub update_map {
399 my ($self, $x, $y, $w, $h) = @_;
400
401 delete $self->{overlay};
402
403 my $map = $self->{map}{map};
404 my $ov = $self->{face} ||= [];
405
406 $x = 0; $w = $self->{map}{width};
407 $y = 0; $h = $self->{map}{height};
408
409 my @x = ($x .. $x + $w - 1);
410 my @y = ($y .. $y + $h - 1);
411
412 my $TC = \%Crossfire::Tilecache::TILECACHE;
413
414 my @ov;
415
416 # update overlay map with bigfaces and chained faces
417 for my $x (@x) {
418 my $ass = $self->{map}{map}[$x];
419 my $oss = $ov->[$x] ||= [];
420 for my $y (@y) {
421 my $os = $oss->[$y] = [];
422 for my $a (@{ $ass->[$y] || [] }) {
423 my $o = $ARCH->{$a->{_name}}
424 or (warn "arch '$a->{_name}' not found at ($x|$y)\n"), next;
425
426 my $tile = $TC->{$a->{face} || $o->{face}}
427 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
428
429 if ($tile->{w} > 1 || $tile->{h} > 1) {
430 # bigfaces
431 for my $ox (0 .. $tile->{w} - 1) {
432 for my $oy (0 .. $tile->{h} - 1) {
433 push @ov, [$x + $ox, $y + $oy,
434 $tile->{idx} + $ox + $oy * $tile->{w}];
435 }
436 }
437
438 } elsif ($o->{more}) {
439 # linked faces
440 do {
441 my $tile = $TC->{$o->{face}}
442 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
443 push @ov, [$x + $o->{x}, $y + $o->{y}, $tile->{idx}];
444 } while $o = $o->{more};
445
446 } else {
447 # single face
448 push @$os, $tile->{idx};
449
450 }
451 }
452 }
453 }
454
455 # bigger faces always on top, I don't give a shit to those who think otherwise
456 for (@ov) {
457 my ($x, $y, $idx) = @$_;
458
459 push @{ $ov->[$x][$y] }, $idx;
460 }
461 }
462
463 sub expose {
464 my ($self, $event) = @_;
465
466 no integer;
467
468 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
469 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
470
471 # get_rectangles is buggy in older versions
472 for my $area ($Gtk2::VERSION > 1.115 ? $event->region->get_rectangles : $event->area) {
473 my ($x, $y, $w, $h) = $area->values; # x y w h
474
475 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
476 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
477
478 my $PB = $Crossfire::Tilecache::TILECACHE;
479
480 my $window = $self->{window};
481
482 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
483 $pb->fill (0x00000000);
484
485 for my $x (@x) {
486 my $dx = ($x - $x[0]) * TILESIZE;
487 my $oss = $self->{face}[$x];
488
489 for my $y (@y) {
490 my $dy = ($y - $y[0]) * TILESIZE;
491
492 for my $idx (@{$oss->[$y]}) {
493 $PB->composite ($pb,
494 $dx, $dy,
495 TILESIZE, TILESIZE,
496 $dx - ($idx % 64) * TILESIZE, $dy - TILESIZE * int $idx / 64,
497 1, 1, 'nearest', 255
498 );
499 }
500 }
501 }
502
503 $pb->render_to_drawable ($window, $self->style->black_gc,
504 0, 0,
505 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
506 TILESIZE * @x, TILESIZE * @y,
507 'max', 0, 0);
508 }
509
510 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
511 for values %{ $self->{overlay} || {} };
512 }
513
514 =back
515
516 =head1 AUTHOR
517
518 Marc Lehmann <schmorp@schmorp.de>
519
520 =cut
521
522 1
523