=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 { my ($self) = @_; $self->{tip}->destroy if $self->{tip}; %$self = (); 0 }); $self->signal_connect (realize => sub { my ($self) = @_; $self->{window} = $self->window; 1 }); $self->set_redraw_on_allocate (0); $self->double_buffered (0); $self->{tooltip} = -1; # need focus in first # reduces unnecessary redraws $self->signal_connect (focus_in_event => sub { $self->enable_tooltip; 1 }); $self->signal_connect (focus_out_event => sub { $self->disable_tooltip; 1 }); $self->signal_connect_after (enter_notify_event => sub { $self->update_tooltip; 0 }); $self->signal_connect_after (leave_notify_event => sub { $self->update_tooltip; 0 }); $self->signal_connect (size_request => sub { $_[1]->width (TILESIZE); $_[1]->height (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 }); $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(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 && $self->mapped && $self->get_toplevel->has_toplevel_focus) { my $screen = $self->{window}->get_screen; if ($self->{window} == ($screen->get_display->get_window_at_pointer)[0]) { 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); $self->{tip}->set_transient_for ($self->get_toplevel); } 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}->move ($x + TILESIZE, $y); $self->{tip}->show_all; 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)"); 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, 1); #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 (reverse @$as) { $vbox->add (my $hbox = new Gtk2::HBox 0, 2); my $tile = $Crossfire::Tilecache::TILECACHE{ $a->{face} || $ARCH->{$a->{_name}}{face} }; # this is awful, is this really the best way? my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE; $pb->fill (0x00000000); $Crossfire::Tilecache::TILECACHE->composite ($pb, 0, 0, TILESIZE, TILESIZE, - ($tile->{idx} % 64) * TILESIZE, - TILESIZE * int $tile->{idx} / 64, 1, 1, 'nearest', 255 ); $hbox->add (my $img = new_from_pixbuf Gtk2::Image $pb); $img->set_alignment (0, 0.5); $hbox->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}}); # } # # 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 my $a; 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; } delete $self->{tipinfo}; $self->update_tooltip; $self->queue_draw_area ($x * TILESIZE - $self->{x}, $y * TILESIZE - $self->{y}, $w * TILESIZE, $h * TILESIZE); } 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 my @rectangles = $Gtk2::VERSION > 1.115 ? $event->region->get_rectangles : $event->area; @rectangles = $event->area if @rectangles > 4; for my $area (@rectangles) { 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