ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.36
Committed: Fri Jan 5 14:17:49 2007 UTC (17 years, 4 months ago) by elmex
Branch: MAIN
CVS Tags: rel-0_96, rel-0_97, rel-0_98, rel-0_99, rel-2_0, rel-2_1
Changes since 1.35: +10 -4 lines
Log Message:
added another case for the face finding algorithm, to also take into
account that there is 'blank.x11'.

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 if ($Glib::VERSION < 1.103) {
270 my $t = "\n$k\t$a->{$k}";
271 $t =~ s/&/&amp;/g;
272 $t =~ s/</&lt;/g;
273 $t =~ s/>/&gt;/g;
274 $text .= $t;
275 } else {
276 $text .= Glib::Markup::escape_text ("\n$k\t$a->{$k}");
277 }
278 }
279 }
280 $text .= "</small>";
281 } else {
282 $text .= "\n<unknown archetype>";
283 }
284
285 $hbox->pack_start (my $label = new Gtk2::Label, 1, 1, 0);
286 $label->set_markup ($text);
287 $label->set_alignment (0, 0.5);
288 }
289 }
290 }
291
292 sub set_viewport {
293 my ($self, $x, $y) = @_;
294
295 my $area = $self->allocation;
296
297 $x = max 0, min $self->{width} - $area->width , $x;
298 $y = max 0, min $self->{height} - $area->height, $y;
299
300 $self->window->scroll ($self->{x} - $x, $self->{y} - $y);
301
302 ($self->{x}, $self->{y}) = ($x, $y);
303 }
304
305 sub set_map {
306 my ($self, $map) = @_;
307
308 $self->{map} = $map;
309
310 $self->{width} = $map->{width} * TILESIZE;
311 $self->{height} = $map->{height} * TILESIZE;
312
313 $self->{x} =
314 $self->{y} = 0;
315
316 my $data = delete $map->{map};
317
318 $map->{map} = [];
319
320 for my $x (0 .. $map->{width} - 1) {
321 my $col = $data->[$x];
322 for my $y (0 .. $map->{height} - 1) {
323 $self->set ($x, $y, delete $col->[$y]);
324 }
325 }
326
327 delete $self->{tipinfo}; $self->update_tooltip;
328 $self->invalidate_all;
329 }
330
331 sub coord {
332 my ($self, $x, $y) = @_;
333
334 (
335 int +($self->{x} + $x) / TILESIZE,
336 int +($self->{y} + $y) / TILESIZE,
337 )
338 }
339
340 #sub handle_key {
341 # my ($self, $key, $state) = @_;
342 #
343 # $self->prefetch_cancel;
344 #
345 # if ($state * "control-mask") {
346 # if ($key == $Gtk2::Gdk::Keysyms{g}) {
347 # my @sel = keys %{$self->{sel}};
348 # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}});
349 # }
350 #
351 # 1
352 #}
353
354 sub invalidate {
355 my ($self, $x, $y, $w, $h) = @_;
356
357 return unless $self->{window};
358
359 $self->queue_draw_area (
360 map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2
361 );
362 }
363
364 sub invalidate_all {
365 my ($self) = @_;
366
367 $self->queue_draw;
368 }
369
370 sub expose {
371 my ($self, $event) = @_;
372
373 no integer;
374
375 my $ox = $self->{x}; my $ix = int $ox / TILESIZE;
376 my $oy = $self->{y}; my $iy = int $oy / TILESIZE;
377
378 # get_rectangles is buggy in older versions
379 my @rectangles = $Gtk2::VERSION >= 1.104
380 ? $event->region->get_rectangles : $event->area;
381
382 for my $area (@rectangles) {
383 my ($x, $y, $w, $h) = $area->values; # x y w h
384
385 my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE);
386 my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE);
387
388 my $window = $self->{window};
389
390 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1);
391 $pb->fill (0xff69b400);
392
393 for my $x (@x) {
394 my $dx = ($x - $x[0]) * TILESIZE;
395 my $oss = $self->{map}{map}[$x];
396
397 for my $y (@y) {
398 my $dy = ($y - $y[0]) * TILESIZE;
399
400 for my $a (@{$oss->[$y]}) {
401 $TILE->composite ($pb,
402 $dx, $dy,
403 TILESIZE, TILESIZE,
404 $dx - ($a->{_face} % 64) * TILESIZE, $dy - TILESIZE * int $a->{_face} / 64,
405 1, 1, 'nearest', 255
406 );
407 }
408 }
409 }
410
411 $pb->render_to_drawable ($window, $self->style->black_gc,
412 0, 0,
413 $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy,
414 TILESIZE * @x, TILESIZE * @y,
415 'max', 0, 0);
416 }
417
418 $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y})
419 for values %{ $self->{overlay} || {} };
420 }
421
422 sub get {
423 my ($self, $x, $y) = @_;
424
425 return unless $x >= 0 && $x < $self->{map}{width}
426 && $y >= 0 && $y < $self->{map}{height};
427
428 Storable::dclone $self->{map}{map}[$x][$y] || []
429 }
430
431 sub set {
432 my ($self, $x, $y, $as) = @_;
433
434 my $data = $self->{map}{map};
435
436 my $prev_as = $data->[$x][$y] || [];
437
438 my ($x1, $y1, $x2, $y2) = ($x, $y) x 2;
439
440 # remove possible overlay tiles
441 for my $a (@$prev_as) {
442 next if $a->{_virtual};
443
444 if (my $more = $a->{_more}) {
445 for (@$more) {
446 my ($x, $y) = @$_;
447
448 $x1 = min $x1, $x; $y1 = min $y1, $y;
449 $x2 = max $x2, $x; $y2 = max $y2, $y;
450
451 $data->[$x][$y] = [ grep $_->{_virtual} != $a, @{ $data->[$x][$y] } ];
452 }
453 }
454 }
455
456 # preserve our overlay tiles, put them on top
457 $as = [
458 (grep !$_->{_virtual}, @$as),
459 (grep $_->{_virtual}, @$prev_as),
460 ];
461
462 for my $a (@$as) {
463 next if $a->{_virtual};
464
465 my $o = $ARCH{$a->{_name}} || $ARCH{empty_archetype}
466 or (warn "archetype $a->{_name} is unknown at ($x|$y)\n"), next;
467
468 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
469 unless ($face) {
470 $face = $FACE{"blank.x11"}
471 or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next;
472 }
473
474 $a->{_face} = $face->{idx};
475
476 if ($face->{w} > 1 || $face->{h} > 1) {
477 # bigfaces
478
479 $x2 = max $x2, $x + $face->{w} - 1;
480 $y2 = max $y2, $y + $face->{h} - 1;
481
482 for my $ox (0 .. $face->{w} - 1) {
483 for my $oy (0 .. $face->{h} - 1) {
484 next unless $ox || $oy;
485
486 push @{ $a->{_more} }, [$x+$ox, $y+$oy];
487 push @{ $data->[$x+$ox][$y+$oy] }, {
488 _virtual => $a,
489 _virtual_x => $x,
490 _virtual_y => $y,
491 _face => $face->{idx} + $ox + $oy * $face->{w},
492 };
493 }
494 }
495
496 } elsif ($o->{more}) {
497 # linked faces, slowest and most annoying
498
499 while ($o = $o->{more}) {
500 my $face = $FACE{$o->{face} || "blank.111"};
501 unless ($face) {
502 $face = $FACE{"blank.x11"}
503 or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next;
504 }
505
506 $x1 = min $x1, $x + $o->{x}; $y1 = min $y1, $y + $o->{y};
507 $x2 = max $x2, $x + $o->{x}; $y2 = max $y2, $y + $o->{y};
508
509 push @{ $a->{_more} }, [$x + $o->{x}, $y + $o->{y}];
510 push @{ $data->[$x+$o->{x}][$y+$o->{y}] }, {
511 _virtual => $a,
512 _virtual_x => $x,
513 _virtual_y => $y,
514 _face => $face->{idx},
515 };
516 }
517 }
518 }
519
520 $data->[$x][$y] = $as;
521
522 $self->queue_draw_area (
523 $x1 * TILESIZE - $self->{x}, $y1 * TILESIZE - $self->{y},
524 ($x2 - $x1 + 1) * TILESIZE, ($y2 - $y1 + 1) * TILESIZE,
525 );
526
527 delete $self->{tipinfo}; $self->update_tooltip;
528
529 }
530
531 sub change_begin {
532 my ($self, $title) = @_;
533
534 $self->{change} ||= {
535 title => $title,
536 };
537 $self->{change}{nest}++;
538 }
539
540 sub change_stack {
541 my ($self, $x, $y, $as) = @_;
542
543 $self->{change}{map}[$x][$y] ||= [$x, $y, $self->{map}{map}[$x][$y]];
544
545 $self->signal_emit (stack_change => $x, $y, $as);
546 }
547
548 sub change_end {
549 my ($self) = @_;
550
551 --$self->{change}{nest} and return;
552
553 my $change = delete $self->{change};
554
555 delete $change->{nest};
556
557 $change->{set} = [
558 grep $_,
559 map @$_,
560 grep $_,
561 @{ delete $change->{map} || [] }
562 ];
563
564 @{ $change->{set} } or return;
565
566 $change
567 }
568
569 sub change_swap {
570 my ($self, $change) = @_;
571
572 for (@{ $change->{set} }) {
573 my $stack = $self->get ($_->[0], $_->[1]);
574 $self->set ($_->[0], $_->[1], $_->[2]);
575 $_->[2] = $stack;
576 }
577
578 $self->invalidate_all;
579 }
580
581 =back
582
583 =head1 AUTHOR
584
585 Marc Lehmann <schmorp@schmorp.de>
586
587 =cut
588
589 1
590