=head1 NAME Crossfire::MapWidget - Gtk2 widget displaying cf maps =head1 SYNOPSIS use Crossfire::MapWidget; =head1 DESCRIPTION =head2 METHODS =over 4 =cut package Crossfire::MapWidget; use strict; use Gtk2; use Crossfire; use Crossfire::Tilecache; use Glib::Object::Subclass 'Gtk2::DrawingArea'; use List::Util qw(min max); sub INIT_INSTANCE { my ($self) = @_; $self->signal_connect (destroy => sub { %{$_[0]} = () }); $self->signal_connect (realize => sub { my ($self) = @_; $self->{window} = $self->window; 1 }); $self->set_redraw_on_allocate (0); # nope $self->double_buffered (0); $self->signal_connect (size_request => sub { $_[1]->width (20 * TILESIZE); $_[1]->height (20 * TILESIZE); 1 }); $self->signal_connect (expose_event => sub { $self->expose ($_[1]); 1 }); $self->signal_connect_after (configure_event => sub { $self->set_viewport ($self->{x}, $self->{y}); 0 }); # reduces unnecessary redraws $self->signal_connect_after (focus_in_event => sub { 1 }); $self->signal_connect_after (focus_out_event => sub { 1 }); $self->{tooltip} = -1; # initially disabled $self->signal_connect_after (enter_notify_event => sub { $self->enable_tooltip; 0 }); $self->signal_connect_after (leave_notify_event => sub { $self->disable_tooltip; 0 }); $self->signal_connect (button_press_event => sub { my ($self, $event) = @_; my ($x, $y) = ($event->x, $event->y); if ($_[1]->button == 2 && !$self->{in_drag}) { $self->disable_tooltip; $_[0]->grab_focus; $self->{in_drag} = [$self->{x}, $self->{y}, $x, $y]; return 1; } 0 }); $self->signal_connect (motion_notify_event => sub { my ($self) = @_; $self->update_tooltip; if (my $di = $self->{in_drag}) { my ($x, $y) = $self->get_pointer; $self->set_viewport ( $di->[0] + $di->[2] - $x, $di->[1] + $di->[3] - $y, ); return 1; } 0 }); $self->signal_connect (button_release_event => sub { my ($self) = @_; $self->enable_tooltip if delete $self->{in_drag}; 0 }); # gtk+ supports no motion compression, a major lacking feature. we have to pay for the # workaround with incorrect behaviour and extra server-turnarounds. $self->add_events ([qw(key_press_mask key_release_mask button_press_mask button_release_mask button-motion-mask pointer-motion-mask pointer-motion-hint-mask enter-notify-mask leave-notify-mask)]); $self->can_focus (1); # $self->signal_connect (key_press_event => sub { $self->handle_key ($_[1]->keyval, $_[1]->state) }); } sub enable_tooltip { my ($self) = @_; $self->{tooltip}++; $self->update_tooltip; } sub disable_tooltip { my ($self) = @_; $self->{tooltip}--; $self->update_tooltip; } sub overlay { my ($self, $name, $x, $y, $w, $h, $cb) = @_; if (my $ov = delete $self->{overlay}{$name}) { my ($x, $y, $w, $h) = @$ov; $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h); } if ($h) { $self->{overlay}{$name} = [$x, $y, $w, $h, $cb]; $self->queue_draw_area ($x - $self->{x}, $y - $self->{y}, $w, $h); } } sub update_tooltip { my ($self) = @_; if ($self->{tooltip} >= 0) { my $screen = $self->{window}->get_screen; my ($pscreen, $x, $y) = $screen->get_display->get_pointer; if ($pscreen == $screen) { if (!$self->{tip}) { $self->{tip} = new Gtk2::Window "popup"; $self->{tip}->can_focus (0); $self->{tip}->set_name ("gtk-tooltips"); $self->{tip}->set_decorated (0); $self->{tip}->set_border_width (4); $self->{tip}->set_has_frame (0); $self->{tip}->set_resizable (0); } my ($mx, $my) = $self->coord ($self->get_pointer); if ($self->{tipinfo}[0] != $mx || $self->{tipinfo}[1] != $my) { $self->fill_tooltip ($mx, $my); $self->{tipinfo} = [$mx, $my]; $self->overlay (_tooltip => $mx * TILESIZE, $my * TILESIZE, TILESIZE, TILESIZE, sub { my ($self, $x, $y) = @_; $self->{window}->draw_rectangle ($_ & 1 ? $self->style->black_gc : $self->style->white_gc, 0, $x + $_, $y + $_, TILESIZE - 1 - $_ * 2, TILESIZE - 1 - $_ * 2) for 0..3; my $req = $self->{tip}->size_request; $self->{tip}->resize ($req->width, $req->height); }); $self->{tip}->show_all; } $self->{tip}->move ($x + TILESIZE, $y); return; } } $self->overlay ("_tooltip"); delete $self->{tipinfo}; (delete $self->{tip})->destroy if $self->{tip}; } sub fill_tooltip { my ($self, $x, $y) = @_; $self->{tip}->remove ($self->{tip}->get_children) if $self->{tip}->get_children; $self->{tip}->add (my $frame = new Gtk2::Frame "($x|$y)"); $frame->set_shadow_type ("none"); if ($x < 0 || $x >= $self->{map}{width} || $y < 0 || $y >= $self->{map}{height}) { $frame->add (new Gtk2::Label ""); } else { $frame->add (my $vbox = new Gtk2::VBox 1, 2); #TODO: fill tooltip via signal, defaulting to this: # fill tooltip with info about $x, $y my $as = $self->{map}{map}[$x][$y] || []; for my $a (@$as) { $vbox->add (my $label = new Gtk2::Label $a->{_name}); $label->set_alignment (0, 0.5); } } } sub set_viewport { my ($self, $x, $y) = @_; my $area = $self->allocation; $x = max 0, min $self->{width} - $area->width , $x; $y = max 0, min $self->{height} - $area->height, $y; $self->window->scroll ($self->{x} - $x, $self->{y} - $y); ($self->{x}, $self->{y}) = ($x, $y); } sub set_map { my ($self, $map) = @_; $self->{map} = $map; $self->{width} = $map->{width} * TILESIZE; $self->{height} = $map->{height} * TILESIZE; $self->{x} = $self->{y} = 0; delete $self->{face}; $self->update_map (0, 0, $self->{width}, $self->{height}); delete $self->{tipinfo}; $self->update_tooltip; $self->invalidate_all; } sub coord { my ($self, $x, $y) = @_; ( int +($self->{x} + $x) / TILESIZE, int +($self->{y} + $y) / TILESIZE, ) } #sub handle_key { # my ($self, $key, $state) = @_; # # $self->prefetch_cancel; # # if ($state * "control-mask") { # if ($key == $Gtk2::Gdk::Keysyms{g}) { # my @sel = keys %{$self->{sel}}; # $self->generate_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}}); # } elsif ($key == $Gtk2::Gdk::Keysyms{a}) { # $self->select_all; # } elsif ($key == $Gtk2::Gdk::Keysyms{A}) { # $self->select_range ($self->{offs}, $self->{offs} + $self->{cols} * $self->{page} - 1); # } elsif ($key == $Gtk2::Gdk::Keysyms{s}) { # $self->rescan; # } elsif ($key == $Gtk2::Gdk::Keysyms{d}) { # $self->unlink (keys %{$self->{sel}}); # } elsif ($key == $Gtk2::Gdk::Keysyms{u}) { # my @sel = keys %{$self->{sel}}; # $self->update_thumbnails (@sel ? @sel : 0 .. $#{$self->{entry}}); # # } elsif ($key == $Gtk2::Gdk::Keysyms{Return}) { # $self->cursor_move (0) unless $self->cursor_valid; # $self->emit_activate ($self->{cursor}); # } elsif ($key == $Gtk2::Gdk::Keysyms{space}) { # $self->cursor_move (1) or return 1 # if $self->{cursor_current} || !$self->cursor_valid; # $self->emit_activate ($self->{cursor}) if $self->cursor_valid; # $self->prefetch (1); # } elsif ($key == $Gtk2::Gdk::Keysyms{BackSpace}) { # $self->cursor_move (-1) or return 1; # $self->emit_activate ($self->{cursor}) if $self->cursor_valid; # $self->prefetch (-1); # # } else { # return 0; # } # } else { # if ($key == $Gtk2::Gdk::Keysyms{Page_Up}) { # my $value = $self->{adj}->value; # $self->{adj}->set_value ($value >= $self->{page} ? $value - $self->{page} : 0); # $self->clear_cursor; # } elsif ($key == $Gtk2::Gdk::Keysyms{Page_Down}) { # my $value = $self->{adj}->value + $self->{page}; # $self->{adj}->set_value ($value <= $self->{maxrow} ? $value : $self->{maxrow}); # $self->clear_cursor; # # } elsif ($key == $Gtk2::Gdk::Keysyms{Home}) { # $self->{adj}->set_value (0); # $self->clear_cursor; # } elsif ($key == $Gtk2::Gdk::Keysyms{End}) { # $self->{adj}->set_value ($self->{maxrow}); # $self->clear_cursor; # # } elsif ($key == $Gtk2::Gdk::Keysyms{Up}) { # $self->cursor_move (-$self->{cols}); # } elsif ($key == $Gtk2::Gdk::Keysyms{Down}) { # $self->cursor_move (+$self->{cols}); # } elsif ($key == $Gtk2::Gdk::Keysyms{Left}) { # $self->cursor_move (-1); # } elsif ($key == $Gtk2::Gdk::Keysyms{Right}) { # $self->cursor_move (+1); # # } elsif ($key == $Gtk2::Gdk::Keysyms{Return}) { # $self->cursor_move (0) unless $self->cursor_valid; # $self->emit_activate ($self->{cursor}); # } elsif ($key == $Gtk2::Gdk::Keysyms{space}) { # $self->cursor_move (1) or return 1 # if $self->{cursor_current} || !$self->cursor_valid; # $self->emit_activate ($self->{cursor}) if $self->cursor_valid; # $self->prefetch (1); # } elsif ($key == $Gtk2::Gdk::Keysyms{BackSpace}) { # $self->cursor_move (-1) or return 1; # $self->emit_activate ($self->{cursor}) if $self->cursor_valid; # $self->prefetch (-1); # # } elsif ($key == ord '^') { # $self->updir if exists $self->{dir}; # # } elsif (($key >= (ord '0') && $key <= (ord '9')) # || ($key >= (ord 'a') && $key <= (ord 'z'))) { # # $key = chr $key; # # my ($idx, $cursor) = (0, 0); # # $self->clear_selection; # # for my $entry (@{$self->{entry}}) { # $idx++; # $cursor = $idx if $key gt lcfirst $entry->[1]; # } # # if ($cursor < @{$self->{entry}}) { # delete $self->{cursor_current}; # $self->{sel}{$cursor} = $self->{entry}[$cursor]; # $self->{cursor} = $cursor; # # $self->{adj}->set_value (min $self->{maxrow}, $cursor / $self->{cols}); # $self->emit_sel_changed; # $self->invalidate_all; # } # } else { # return 0; # } # } # # 1 #} sub invalidate { my ($self, $x, $y, $w, $h) = @_; return unless $self->{window}; $self->queue_draw_area ( map $_ * TILESIZE, $x - 1 , $y - 1, $w + 2, $h + 2 ); } sub invalidate_all { my ($self) = @_; $self->queue_draw; } sub update_map { my ($self, $x, $y, $w, $h) = @_; delete $self->{overlay}; my $map = $self->{map}{map}; my $ov = $self->{face} ||= []; $x = 0; $w = $self->{map}{width}; $y = 0; $h = $self->{map}{height}; my @x = ($x .. $x + $w - 1); my @y = ($y .. $y + $h - 1); my $TC = \%Crossfire::Tilecache::TILECACHE; my @ov; # update overlay map with bigfaces and chained faces for my $x (@x) { my $ass = $self->{map}{map}[$x]; my $oss = $ov->[$x] ||= []; for my $y (@y) { my $os = $oss->[$y] = []; for my $a (@{ $ass->[$y] || [] }) { my $o = $ARCH->{$a->{_name}} or (warn "arch '$a->{_name}' not found at ($x|$y)\n"), next; my $tile = $TC->{$a->{face} || $o->{face}} or (warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"), next; if ($tile->{w} > 1 || $tile->{h} > 1) { # bigfaces for my $ox (0 .. $tile->{w} - 1) { for my $oy (0 .. $tile->{h} - 1) { push @ov, [$x + $ox, $y + $oy, $tile->{idx} + $ox + $oy * $tile->{w}]; } } } elsif ($o->{more}) { # linked faces do { my $tile = $TC->{$o->{face}} or (warn "no gfx found for arch '$a->{_name}' at ($x*|$y*)\n"), next; push @ov, [$x + $o->{x}, $y + $o->{y}, $tile->{idx}]; } while $o = $o->{more}; } else { # single face push @$os, $tile->{idx}; } } } } # bigger faces always on top, I don't give a shit to those who think otherwise for (@ov) { my ($x, $y, $idx) = @$_; push @{ $ov->[$x][$y] }, $idx; } } sub expose { my ($self, $event) = @_; no integer; my $ox = $self->{x}; my $ix = int $ox / TILESIZE; my $oy = $self->{y}; my $iy = int $oy / TILESIZE; # get_rectangles is buggy in older versions for my $area ($Gtk2::VERSION > 1.115 ? $event->region->get_rectangles : $event->area) { my ($x, $y, $w, $h) = $area->values; # x y w h my @x = ((int ($ox + $x) / TILESIZE) .. int +($ox + $x + $w + TILESIZE - 1) / TILESIZE); my @y = ((int ($oy + $y) / TILESIZE) .. int +($oy + $y + $h + TILESIZE - 1) / TILESIZE); my $PB = $Crossfire::Tilecache::TILECACHE; my $window = $self->{window}; my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 0, 8, TILESIZE * (@x + 1), TILESIZE * (@y + 1); $pb->fill (0x00000000); for my $x (@x) { my $dx = ($x - $x[0]) * TILESIZE; my $oss = $self->{face}[$x]; for my $y (@y) { my $dy = ($y - $y[0]) * TILESIZE; for my $idx (@{$oss->[$y]}) { $PB->composite ($pb, $dx, $dy, TILESIZE, TILESIZE, $dx - ($idx % 64) * TILESIZE, $dy - TILESIZE * int $idx / 64, 1, 1, 'nearest', 255 ); } } } $pb->render_to_drawable ($window, $self->style->black_gc, 0, 0, $x[0] * TILESIZE - $ox, $y[0] * TILESIZE - $oy, TILESIZE * @x, TILESIZE * @y, 'max', 0, 0); } $_->[4]->($self, $_->[0] - $self->{x}, $_->[1] - $self->{y}) for values %{ $self->{overlay} || {} }; } =back =head1 AUTHOR Marc Lehmann =cut 1