--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/14 20:27:35 1.107 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/15 23:25:00 1.118 @@ -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) = @_; + + my ($X, $Y) = $self->_topleft; + ($x - $X, $y - $Y) +} + +# translate local coordinates to global coordinate system +sub coord2global { my ($self, $x, $y) = @_; - $self->{parent}->translate ($x - $self->{x}, $y - $self->{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; @@ -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, @_ ) } @@ -1560,7 +1605,7 @@ delete $self->{texture}; - $TOPLEVEL->on_refresh ($self, sub { + $ROOT->on_refresh ($self, sub { if (delete $self->{need_reflow}) { my $height = 0; @@ -1657,6 +1702,40 @@ sub key_up { } +sub button_down { + my ($self, $ev, $x, $y) = @_; + + $self->focus_in; + + if ($ev->button == 2) { + my ($ox, $oy) = ($ev->button_x, $ev->button_y); + my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); + + $self->{motion} = sub { + my ($ev, $x, $y) = @_; + + ($x, $y) = ($ev->motion_x, $ev->motion_y); + + $::CFG->{map_shift_x} = $bw + $x - $ox; + $::CFG->{map_shift_y} = $bh + $y - $oy; + + $self->update; + }; + } +} + +sub button_up { + my ($self, $ev, $x, $y) = @_; + + delete $self->{motion}; +} + +sub mouse_motion { + my ($self, $ev, $x, $y) = @_; + + $self->{motion}->($ev, $x, $y) if $self->{motion}; +} + sub size_request { ( 1 + 32 * int $::WIDTH / 32, @@ -1674,15 +1753,19 @@ sub draw { my ($self) = @_; - $self->{need_update}++;#d# if (delete $self->{need_update}) { glNewList $self->{list}, GL_COMPILE; - my $sw = int $::WIDTH / 32; - my $sh = int $::HEIGHT / 32; - if ($::MAP) { - my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); + my $sw = int $::WIDTH / 32; + my $sh = int $::HEIGHT / 32; + + 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 $sx0 - 32, $sy0 - 32, 0; + + 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 @@ -1700,7 +1783,7 @@ glEnable GL_CONVOLUTION_2D; } - my $tex = new CFClient::Texture + $self->{fow_texture} = new CFClient::Texture w => $w, h => $h, data => $data, @@ -1710,21 +1793,48 @@ glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; 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_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; } + + # HACK BEGIN + { + glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove + my ($w, $h) = (250, 250); + + 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; + + $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; + } + # HACK END } glEndList; } + glPushMatrix; glCallList $self->{list}; + glPopMatrix; if ($FOCUS != $self) { glColor 64/255, 64/255, 64/255; @@ -1867,19 +1977,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::; @@ -1898,7 +2009,7 @@ for @{$self->{children}}; } -sub translate { +sub _topleft { my ($self, $x, $y) = @_; ($x, $y) @@ -1916,7 +2027,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 { @@ -1949,7 +2060,7 @@ package CFClient::UI; -$TOPLEVEL = new CFClient::UI::Toplevel; +$ROOT = new CFClient::UI::Root; 1