ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.32
Committed: Mon Mar 20 02:15:36 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.31: +21 -23 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 Glib;
22 use Gtk2;
23 use Storable ();
24
25 use Crossfire;
26
27 use Glib::Object::Subclass
28 'Gtk2::DrawingArea',
29 signals => {
30 stack_change => {
31 flags => [qw/run-last/],
32 return_type => undef,
33 param_types => ["Glib::Int", "Glib::Int", "Glib::Scalar"],
34 class_closure => \&set,
35 },
36 };
37
38 use List::Util qw(min max);
39
40 sub INIT_INSTANCE {
41 my ($self) = @_;
42
43 $self->signal_connect (destroy => sub {
44 my ($self) = @_;
45
46 $self->{tip}->destroy if $self->{tip};
47
48 %$self = ();
49
50 0
51 });
52 $self->signal_connect (realize => sub {
53 my ($self) = @_;
54
55 $self->{window} = $self->window;
56
57 1
58 });
59
60 $self->set_redraw_on_allocate (0);
61 $self->double_buffered (0);
62
63 $self->{tooltip} = -1; # need focus in first
64
65 # reduces unnecessary redraws
66 $self->signal_connect (focus_in_event => sub { $self->enable_tooltip; 1 });
67 $self->signal_connect (focus_out_event => sub { $self->disable_tooltip; 1 });
68
69 $self->signal_connect_after (enter_notify_event => sub { $self->update_tooltip; 0 });
70 $self->signal_connect_after (leave_notify_event => sub { $self->update_tooltip; 0 });
71
72 $self->signal_connect (size_request => sub {
73 $_[1]->width (TILESIZE);
74 $_[1]->height (TILESIZE);
75
76 1
77 });
78
79 $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 });
80
81 $self->signal_connect_after (configure_event => sub {
82 $self->set_viewport ($self->{x}, $self->{y});
83
84 0
85 });
86
87 $self->signal_connect (button_press_event => sub {
88 my ($self, $event) = @_;
89
90 my ($x, $y) = ($event->x, $event->y);
91
92 if ($_[1]->button == 2 && !$self->{in_drag}) {
93 $self->disable_tooltip;
94
95 $_[0]->grab_focus;
96 $self->{in_drag} = [$self->{x}, $self->{y}, $x, $y];
97 return 1;
98 }
99
100 0
101 });
102
103 $self->signal_connect (motion_notify_event => sub {
104 my ($self) = @_;
105
106 $self->update_tooltip;
107
108 if (my $di = $self->{in_drag}) {
109 my ($x, $y) = $self->get_pointer;
110
111 $self->set_viewport (
112 $di->[0] + $di->[2] - $x,
113 $di->[1] + $di->[3] - $y,
114 );
115
116 return 1;
117 }
118
119 0
120 });
121
122 $self->signal_connect (button_release_event => sub {
123 my ($self) = @_;
124
125 $self->enable_tooltip
126 if delete $self->{in_drag};
127
128 0
129 });
130
131 # gtk+ supports no motion compression, a major lacking feature. we have to pay for the
132 # workaround with incorrect behaviour and extra server-turnarounds.
133 $self->add_events ([qw(button_press_mask button_release_mask button-motion-mask
134 pointer-motion-mask pointer-motion-hint-mask
135 enter-notify-mask leave-notify-mask)]);
136 $self->can_focus (1);
137
138 # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) });
139 }
140
141 sub enable_tooltip {
142 my ($self) = @_;
143
144 $self->{tooltip}++;
145 $self->update_tooltip;
146 }
147
148 sub disable_tooltip {
149 my ($self) = @_;
150
151 $self->{tooltip}--;
152 $self->update_tooltip;
153 }
154
155 sub overlay {
156 my ($self, $name, $x, $y, $w, $h, $cb) = @_;
157
158 if (my $ov = delete $self->{overlay}{$name}) {
159 my ($x, $y, $w, $h) = @$ov;
160
161 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
162 }
163
164 if ($w && $h) {
165 $self->{overlay}{$name} = [$x, $y, $w, $h, $cb];
166
167 $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h);
168 }
169 }
170
171 sub update_tooltip {
172 my ($self) = @_;
173
174 if ($self->{tooltip} >= 0
175 && $self->mapped
176 && $self->get_toplevel->has_toplevel_focus) {
177 my $screen = $self->{window}->get_screen;
178
179 if ($self->{window} == ($screen->get_display->get_window_at_pointer)[0]) {
180 my ($pscreen, $x, $y) = $screen->get_display->get_pointer;
181
182 if ($pscreen == $screen) {
183 if (!$self->{tip}) {
184 $self->{tip} = new Gtk2::Window "popup";
185 $self->{tip}->can_focus (0);
186 $self->{tip}->set_name ("gtk-tooltips");
187 $self->{tip}->set_decorated (0);
188 $self->{tip}->set_border_width (4);
189 $self->{tip}->set_has_frame (0);
190 $self->{tip}->set_resizable (0);
191 $self->{tip}->set_transient_for ($self->get_toplevel);
192 }
193
194 my ($mx, $my) = $self->coord ($self->get_pointer);
195
196 if ($self->{tipinfo}[0] != $mx || $self->{tipinfo}[1] != $my) {
197 $self->fill_tooltip ($mx, $my);
198
199 $self->{tipinfo} = [$mx, $my];
200
201 $self->overlay (_tooltip => $mx * TILESIZE, $my * TILESIZE, TILESIZE, TILESIZE, sub {
202 my ($self, $x, $y) = @_;
203
204 $self->{window}->draw_rectangle ($_ & 1 ? $self->style->black_gc : $self->style->white_gc, 0,
205 $x + $_, $y + $_,
206 TILESIZE - 1 - $_ * 2, TILESIZE - 1 - $_ * 2)
207 for 0..3;
208 });
209
210 my $req = $self->{tip}->size_request;
211 $self->{tip}->resize ($req->width, $req->height);
212 }
213
214 $self->{tip}->move ($x + TILESIZE, $y);
215 $self->{tip}->show_all;
216
217 return;
218 }
219 }
220 }
221
222 $self->overlay ("_tooltip");
223 delete $self->{tipinfo};
224 (delete $self->{tip})->destroy if $self->{tip};
225 }
226
227 sub fill_tooltip {
228 my ($self, $x, $y) = @_;
229
230 $self->{tip}->remove ($self->{tip}->get_children)
231 if $self->{tip}->get_children;
232
233 $self->{tip}->add (my $frame = new Gtk2::Frame "($x|$y)");
234
235 if ($x < 0 || $x >= $self->{map}{width}
236 || $y < 0 || $y >= $self->{map}{height}) {
237 $frame->add (new Gtk2::Label "<off-map>");
238 } else {
239 $frame->add (my $vbox = new Gtk2::VBox 0, 1);
240
241 #TODO: fill tooltip via signal, defaulting to this:
242
243 # fill tooltip with info about $x, $y
244 my $as = $self->{map}{map}[$x][$y] || [];
245 for (reverse @$as) {
246 $vbox->add (my $hbox = new Gtk2::HBox 0, 2);
247
248 # this is awful, is this really the best way?
249 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
250 $pb->fill (0x00000000);
251
252 $TILE->composite ($pb,
253 0, 0,
254 TILESIZE, TILESIZE,
255 - ($_->{_face} % 64) * TILESIZE, - TILESIZE * int $_->{_face} / 64,
256 1, 1, 'nearest', 255
257 );
258
259 my $a = $_->{_virtual} || $_;
260
261 $hbox->pack_start ((my $img = new_from_pixbuf Gtk2::Image $pb), 0, 1, 0);
262 $img->set_alignment (0, 0.5);
263
264 my $text = "$a->{_name}";
265 if (my $o = $ARCH{$a->{_name}}) {
266 $text .= "<small>";
267 for my $k (grep /^[^_]/, sort keys %$a) {
268 if ($a->{$k} ne $o->{$k}) {
269 $text .= Glib::Markup::escape_text "\n$k\t$a->{$k}";
270 }
271 }
272 $text .= "</small>";
273 } else {
274 $text .= "\n<unknown archetype>";
275 }
276
277 $hbox->pack_start (my $label = new Gtk2::Label, 1, 1, 0);
278 $label->set_markup ($text);
279 $label->set_alignment (0, 0.5);
280 }
281 }
282 }
283
284 sub set_viewport {
285 my ($self, $x, $y) = @_;
286
287 my $area = $self->allocation;
288
289 $x = max 0, min $self->{width} - $area->width , $x;
290 $y = max 0, min $self->{height} - $area->height, $y;
291
292 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
293
294 ($self->{x}, $self->{y}) = ($x, $y);
295 }
296
297 sub set_map {
298 my ($self, $map) = @_;
299
300 $self->{map} = $map;
301
302 $self->{width} = $map->{width} * TILESIZE;
303 $self->{height} = $map->{height} * TILESIZE;
304
305 $self->{x} =
306 $self->{y} = 0;
307
308 my $data = delete $map->{map};
309
310 $map->{map} = [];
311
312 for my $x (0 .. $map->{width} - 1) {
313 my $col = $data->[$x];
314 for my $y (0 .. $map->{height} - 1) {
315 $self->set ($x, $y, delete $col->[$y]);
316 }
317 }
318
319 delete $self->{tipinfo}; $self->update_tooltip;
320 $self->invalidate_all;
321 }
322
323 sub coord {
324 my ($self, $x, $y) = @_;
325
326 (
327 int +($self->{x} + $x) / TILESIZE,
328 int +($self->{y} + $y) / TILESIZE,
329 )
330 }
331
332 #sub handle_key {
333 # my ($self, $key, $state) = @_;
334 #
335 # $self->prefetch_cancel;
336 #
337 # if ($state * "control-mask") {
338 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
339 # my @sel = keys %{$self->{sel}};
340 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
341 # }
342 #
343 # 1
344 #}
345
346 sub invalidate {
347 my ($self, $x, $y, $w, $h) = @_;
348
349 return unless $self->{window};
350
351 $self->queue_draw_area (
352 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
353 );
354 }
355
356 sub invalidate_all {
357 my ($self) = @_;
358
359 $self->queue_draw;
360 }
361
362 sub expose {
363 my ($self, $event) = @_;
364
365 no integer;
366
367 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
368 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
369
370 # get_rectangles is buggy in older versions
371 my @rectangles = $Gtk2::VERSION >= 1.104
372 ? $event->region->get_rectangles : $event->area;
373
374 for my $area (@rectangles) {
375 my ($x, $y, $w, $h) = $area->values; # x y w h
376
377 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
378 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
379
380 my $window = $self->{window};
381
382 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
383 $pb->fill (0xff69b400);
384
385 for my $x (@x) {
386 my $dx = ($x - $x[0]) * TILESIZE;
387 my $oss = $self->{map}{map}[$x];
388
389 for my $y (@y) {
390 my $dy = ($y - $y[0]) * TILESIZE;
391
392 for my $a (@{$oss->[$y]}) {
393 $TILE->composite ($pb,
394 $dx, $dy,
395 TILESIZE, TILESIZE,
396 $dx - ($a->{_face} % 64) * TILESIZE, $dy - TILESIZE * int $a->{_face} / 64,
397 1, 1, 'nearest', 255
398 );
399 }
400 }
401 }
402
403 $pb->render_to_drawable ($window, $self->style->black_gc,
404 0, 0,
405 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
406 TILESIZE * @x, TILESIZE * @y,
407 'max', 0, 0);
408 }
409
410 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
411 for values %{ $self->{overlay} || {} };
412 }
413
414 sub get {
415 my ($self, $x, $y) = @_;
416
417 return unless $x >= 0 && $x < $self->{map}{width}
418 && $y >= 0 && $y < $self->{map}{height};
419
420 Storable::dclone $self->{map}{map}[$x][$y] || []
421 }
422
423 sub set {
424 my ($self, $x, $y, $as) = @_;
425
426 my $data = $self->{map}{map};
427
428 my $prev_as = $data->[$x][$y] || [];
429
430 my ($x1, $y1, $x2, $y2) = ($x, $y) x 2;
431
432 # remove possible overlay tiles
433 for my $a (@$prev_as) {
434 next if $a->{_virtual};
435
436 if (my $more = $a->{_more}) {
437 for (@$more) {
438 my ($x, $y) = @$_;
439
440 $x1 = min $x1, $x; $y1 = min $y1, $y;
441 $x2 = max $x2, $x; $y2 = max $y2, $y;
442
443 $data->[$x][$y] = [ grep $_->{_virtual} != $a, @{ $data->[$x][$y] } ];
444 }
445 }
446 }
447
448 # preserve our overlay tiles, put them on top
449 $as = [
450 (grep !$_->{_virtual}, @$as),
451 (grep $_->{_virtual}, @$prev_as),
452 ];
453
454 for my $a (@$as) {
455 next if $a->{_virtual};
456
457 my $o = $ARCH{$a->{_name}} || $ARCH{empty_archetype}
458 or (warn "archetype $a->{_name} is unknown at ($x|$y)\n"), next;
459
460 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
461 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
462
463 $a->{_face} = $face->{idx};
464
465 if ($face->{w} > 1 || $face->{h} > 1) {
466 # bigfaces
467
468 $x2 = max $x2, $x + $face->{w} - 1;
469 $y2 = max $y2, $y + $face->{h} - 1;
470
471 for my $ox (0 .. $face->{w} - 1) {
472 for my $oy (0 .. $face->{h} - 1) {
473 next unless $ox || $oy;
474
475 push @{ $a->{_more} }, [$x+$ox, $y+$oy];
476 push @{ $data->[$x+$ox][$y+$oy] }, {
477 _virtual => $a,
478 _virtual_x => $x,
479 _virtual_y => $y,
480 _face => $face->{idx} + $ox + $oy * $face->{w},
481 };
482 }
483 }
484
485 } elsif ($o->{more}) {
486 # linked faces, slowest and most annoying
487
488 while ($o = $o->{more}) {
489 my $face = $FACE{$o->{face} || "blank.111"}
490 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
491
492 $x1 = min $x1, $x + $o->{x}; $y1 = min $y1, $y + $o->{y};
493 $x2 = max $x2, $x + $o->{x}; $y2 = max $y2, $y + $o->{y};
494
495 push @{ $a->{_more} }, [$x + $o->{x}, $y + $o->{y}];
496 push @{ $data->[$x+$o->{x}][$y+$o->{y}] }, {
497 _virtual => $a,
498 _virtual_x => $x,
499 _virtual_y => $y,
500 _face => $face->{idx},
501 };
502 }
503 }
504 }
505
506 $data->[$x][$y] = $as;
507
508 $self->queue_draw_area (
509 $x1 * TILESIZE - $self->{x}, $y1 * TILESIZE - $self->{y},
510 ($x2 - $x1 + 1) * TILESIZE, ($y2 - $y1 + 1) * TILESIZE,
511 );
512
513 delete $self->{tipinfo}; $self->update_tooltip;
514
515 }
516
517 sub change_begin {
518 my ($self, $title) = @_;
519
520 $self->{change} ||= {
521 title => $title,
522 };
523 $self->{change}{nest}++;
524 }
525
526 sub change_stack {
527 my ($self, $x, $y, $as) = @_;
528
529 $self->{change}{map}[$x][$y] ||= [$x, $y, $self->{map}{map}[$x][$y]];
530
531 $self->signal_emit (stack_change => $x, $y, $as);
532 }
533
534 sub change_end {
535 my ($self) = @_;
536
537 --$self->{change}{nest} and return;
538
539 my $change = delete $self->{change};
540
541 delete $change->{nest};
542
543 $change->{set} = [
544 grep $_,
545 map @$_,
546 grep $_,
547 @{ delete $change->{map} || [] }
548 ];
549
550 @{ $change->{set} } or return;
551
552 $change
553 }
554
555 sub change_swap {
556 my ($self, $change) = @_;
557
558 for (@{ $change->{set} }) {
559 my $stack = $self->get ($_->[0], $_->[1]);
560 $self->set ($_->[0], $_->[1], $_->[2]);
561 $_->[2] = $stack;
562 }
563
564 $self->invalidate_all;
565 }
566
567 =back
568
569 =head1 AUTHOR
570
571 Marc Lehmann <schmorp@schmorp.de>
572
573 =cut
574
575 1
576