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