--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/15 01:16:17 1.110 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/15 13:56:25 1.114 @@ -9,7 +9,7 @@ our ($FOCUS, $HOVER, $GRAB); # various widgets -our $TOPLEVEL; +our $ROOT; our $BUTTON_STATE; # class methods for events @@ -26,7 +26,7 @@ my ($x, $y) = ($ev->motion_x, $ev->motion_y); if (!$BUTTON_STATE) { - my $widget = $TOPLEVEL->find_widget ($x, $y); + my $widget = $ROOT->find_widget ($x, $y); $GRAB = $widget; $GRAB->update if $GRAB; @@ -34,18 +34,18 @@ $BUTTON_STATE |= 1 << ($ev->button - 1); - $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; + $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; } sub feed_sdl_button_up_event { my ($ev) = @_; my ($x, $y) = ($ev->motion_x, $ev->motion_y); - my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); + my $widget = $GRAB || $ROOT->find_widget ($x, $y); $BUTTON_STATE &= ~(1 << ($ev->button - 1)); - $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; + $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; if (!$BUTTON_STATE) { my $grab = $GRAB; undef $GRAB; @@ -58,7 +58,7 @@ my ($ev) = @_; my ($x, $y) = ($ev->motion_x, $ev->motion_y); - my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); + my $widget = $GRAB || $ROOT->find_widget ($x, $y); if ($widget != $HOVER) { my $hover = $HOVER; $HOVER = $widget; @@ -67,7 +67,20 @@ $HOVER->update if $HOVER && $HOVER->{can_hover}; } - $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; + $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; +} + +# convert position array to integers +sub harmonize { + my ($vals) = @_; + + my $rem = 0; + + for ($vals) { + my $i = int $_ + $rem; + $rem += $_ - $i; + $_ = $i; + } } ############################################################################# @@ -135,11 +148,27 @@ $self->_size_allocate ($x, $y, $w, $h); } +# return top left coordinates +sub _topleft { + my ($self, $x, $y) = @_; + + $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); +} + # translate global coordinates to local coordinate system -sub translate { +sub coord2local { my ($self, $x, $y) = @_; - $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); + my ($X, $Y) = $self->_topleft; + ($x - $X, $y - $Y) +} + +# translate local coordinates to global coordinate system +sub coord2global { + my ($self, $x, $y) = @_; + + my ($X, $Y) = $self->_topleft; + ($x + $X, $y + $Y) } sub focus_in { @@ -222,14 +251,9 @@ } sub set_parent { - my ($self, $par) = @_; - - $self->{parent} = $par; - Scalar::Util::weaken $self->{parent}; -} + my ($self, $parent) = @_; -sub get_parent { - $_[0]->{parent} + Scalar::Util::weaken ($self->{parent} = $parent); } sub update { @@ -328,13 +352,15 @@ } sub add { - my ($self, $chld) = @_; + my ($self, $child) = @_; - $chld->set_parent ($self); + $child->set_parent ($self); + + use sort 'stable'; $self->{children} = [ sort { $a->{z} <=> $b->{z} } - @{$self->{children}}, $chld + @{$self->{children}}, $child ]; $self->{w} = $self->{h} = -1; @@ -674,10 +700,10 @@ } sub add { - my ($self, $x, $y, $chld) = @_; + my ($self, $x, $y, $child) = @_; - $self->{children}[$y][$x] = $chld; - $chld->set_parent ($self); + $child->set_parent ($self); + $self->{children}[$y][$x] = $child; $self->{w} = $self->{h} = -1; $self->update; @@ -735,8 +761,8 @@ $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; - $_ = int $_ for @$ws; #TODO: avoid rounding problems - $_ = int $_ for @$hs; #TODO: avoid rounding problems + CFClient::UI::harmonize $ws; + CFClient::UI::harmonize $hs; my $y; @@ -834,10 +860,12 @@ } } + CFClient::UI::harmonize \@h; + my $y = 0; for (0 .. $#$children) { my $child = $children->[$_]; - my $h = int $h[$_]; + my $h = $h[$_]; $child->size_allocate ($y, 0, $h, $w); $y += $h; @@ -888,10 +916,12 @@ } } + CFClient::UI::harmonize \@h; + my $y = 0; for (0 .. $#$children) { my $child = $children->[$_]; - my $h = int $h[$_]; + my $h = $h[$_]; $child->size_allocate (0, $y, $w, $h); $y += $h; @@ -1560,7 +1590,7 @@ delete $self->{texture}; - $TOPLEVEL->on_refresh ($self, sub { + $ROOT->on_refresh ($self, sub { if (delete $self->{need_reflow}) { my $height = 0; @@ -1715,12 +1745,12 @@ my $sw = int $::WIDTH / 32; my $sh = int $::HEIGHT / 32; - my $sx = $::CFG->{map_shift_x}; - my $sy = $::CFG->{map_shift_y}; + my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; + my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; - glTranslate +($sx & 31) - 32, ($sy & 31) - 32, 0; + glTranslate $sx0 - 32, $sy0 - 32, 0; - my ($w, $h, $data) = $::MAP->draw ($sx >> 5, $sy >> 5, 0, 0, $sw + 1, $sh + 1); + my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); if ($::CFG->{fow_enable}) { if ($::CFG->{fow_smooth}) { # smooth fog of war @@ -1761,7 +1791,7 @@ # HACK BEGIN { - glTranslate -(($sx & 31) - 32), -(($sy & 31) - 32), 0;#remove + glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove my ($w, $h) = (250, 250); glEnable GL_BLEND; @@ -1930,19 +1960,20 @@ # TODO: use animation if ($self->{state} = !$self->{state}) { - $CFClient::UI::TOPLEVEL->add ($self->{other}); - $self->{other}->move ( - ($::WIDTH - $self->{other}{w}) * 0.5, - ($::HEIGHT - $self->{other}{h}) * 0.5, - ); + $CFClient::UI::ROOT->add ($self->{other}); + $self->{other}->move ($self->coord2global (0, $self->{h})); + $self->emit ("open"); } else { - $CFClient::UI::TOPLEVEL->remove ($self->{other}); + $CFClient::UI::ROOT->remove ($self->{other}); + $self->emit ("close"); } + + $self->emit (changed => $self->{state}); } ############################################################################# -package CFClient::UI::Toplevel; +package CFClient::UI::Root; our @ISA = CFClient::UI::Container::; @@ -1961,7 +1992,7 @@ for @{$self->{children}}; } -sub translate { +sub _topleft { my ($self, $x, $y) = @_; ($x, $y) @@ -2012,7 +2043,7 @@ package CFClient::UI; -$TOPLEVEL = new CFClient::UI::Toplevel; +$ROOT = new CFClient::UI::Root; 1