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