ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/MapWidget.pm
Revision: 1.39
Committed: Sat Aug 25 14:45:26 2007 UTC (16 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.38: +7 -0 lines
Log Message:
added swap_stack_change signal

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