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