--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/15 01:16:17 1.110 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/16 07:25:30 1.119 @@ -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; + } } ############################################################################# @@ -101,8 +114,8 @@ sub move { my ($self, $x, $y, $z) = @_; - $self->{x} = $x; - $self->{y} = $y; + $self->{x} = int $x; + $self->{y} = int $y; $self->{z} = $z if defined $z; } @@ -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) = @_; + + $child->set_parent ($self); - $chld->set_parent ($self); + use sort 'stable'; $self->{children} = [ sort { $a->{z} <=> $b->{z} } - @{$self->{children}}, $chld + @{$self->{children}}, $child ]; $self->{w} = $self->{h} = -1; @@ -541,7 +567,7 @@ $class->SUPER::new ( bg => [1, 1, 1, 1], border_bg => [1, 1, 1, 1], - border => $::FONTSIZE * 0.8, + border => int $::FONTSIZE * 0.8, @_ ) } @@ -674,15 +700,23 @@ } 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; } +# TODO: move to container class maybe? send childs a signal on removal? +sub clear { + my ($self) = @_; + + delete $self->{children}; + $self->update; +} + sub get_wh { my ($self) = @_; @@ -735,8 +769,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 +868,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 +924,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; @@ -914,6 +952,7 @@ fontsize => $::FONTSIZE, text => "", align => -1, + valign => -1, padding => 2, layout => new CFClient::Layout, %arg @@ -1000,7 +1039,12 @@ : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} : ($self->{w} - $tex->{w}) * 0.5; - $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); + my $y = + $self->{valign} < 0 ? $self->{padding} + : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} + : ($self->{h} - $tex->{h}) * 0.5; + + $tex->draw_quad (int $x, int $y); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1025,6 +1069,7 @@ active_fg => [0, 0, 0], can_hover => 1, can_focus => 1, + valign => 0, @_ ) } @@ -1233,20 +1278,18 @@ my ($self) = @_; local $self->{fg} = $self->{fg}; - my $tex = $tex[0]; - - glEnable GL_BLEND; - glEnable GL_TEXTURE_2D; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; if ($GRAB == $self) { $self->{fg} = $self->{active_fg}; } - glBindTexture GL_TEXTURE_2D, $tex->{name}; + glEnable GL_BLEND; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; + glColor 0, 0, 0, 1; - $tex->draw_quad (0, 0, $self->{w}, $self->{h}); + $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1560,7 +1603,7 @@ delete $self->{texture}; - $TOPLEVEL->on_refresh ($self, sub { + $ROOT->on_refresh ($self, sub { if (delete $self->{need_reflow}) { my $height = 0; @@ -1715,12 +1758,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 @@ -1738,7 +1781,7 @@ glEnable GL_CONVOLUTION_2D; } - my $tex = new CFClient::Texture + $self->{fow_texture} = new CFClient::Texture w => $w, h => $h, data => $data, @@ -1753,7 +1796,7 @@ glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; glColor +($::CFG->{fow_intensity}) x 3, 1; - $tex->draw_quad (0, 0, $w * 32, $h * 32); + $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1761,7 +1804,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; @@ -1769,12 +1812,14 @@ glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - CFClient::Texture->new ( - w => $w, - h => $h, - data => $::MAP->mapmap ($w, $h), - type => GL_UNSIGNED_INT_8_8_8_8_REV - )->draw_quad (100, 100); + $self->{mapmap_texture} = + new CFClient::Texture + w => $w, + h => $h, + data => $::MAP->mapmap ($w, $h), + type => GL_UNSIGNED_INT_8_8_8_8_REV; + + $self->{mapmap_texture}->draw_quad (100, 100); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1930,19 +1975,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 +2007,7 @@ for @{$self->{children}}; } -sub translate { +sub _topleft { my ($self, $x, $y) = @_; ($x, $y) @@ -1979,7 +2025,7 @@ $self->SUPER::add ($widget); - $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); + $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request); } sub on_refresh { @@ -2012,7 +2058,7 @@ package CFClient::UI; -$TOPLEVEL = new CFClient::UI::Toplevel; +$ROOT = new CFClient::UI::Root; 1