--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/15 02:10:22 1.111 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/17 19:21:00 1.121 @@ -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 { @@ -149,6 +178,9 @@ return unless $self->{can_focus}; my $focus = $FOCUS; $FOCUS = $self; + + $self->emit (focus_in => $focus); + $focus->update if $focus; $FOCUS->update; } @@ -159,6 +191,9 @@ return unless $FOCUS == $self; my $focus = $FOCUS; undef $FOCUS; + + $self->emit (focus_out => $focus); + $focus->update if $focus; #? } @@ -222,14 +257,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 +358,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 +573,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 +706,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 +775,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 +874,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 +930,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,12 +958,16 @@ fontsize => $::FONTSIZE, text => "", align => -1, + valign => -1, padding => 2, layout => new CFClient::Layout, %arg ); - $self->set_text ($self->{text}); + $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# + + $self->set_text (delete $self->{text}) if exists $self->{text}; + $self->set_markup (delete $self->{markup}) if exists $self->{markup}; $self } @@ -937,18 +985,19 @@ sub set_text { my ($self, $text) = @_; - $self->{text} = $text; - $self->{layout}->set_markup ($text); + $self->{layout}->set_text ($text); delete $self->{texture}; -# $self->{w} = $self->{h} = -1; $self->update; } -sub get_text { - my ($self, $text) = @_; +sub set_markup { + my ($self, $markup) = @_; - $self->{text} + $self->{layout}->set_markup ($markup); + + delete $self->{texture}; + $self->update; } sub size_request { @@ -956,6 +1005,7 @@ $self->{layout}->set_width; $self->{layout}->set_height ($self->{fontsize}); + my ($w, $h) = $self->{layout}->size; ( @@ -984,7 +1034,7 @@ my $tex = $self->{texture} ||= do { $self->{layout}->set_width ($self->{w}); - $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); + $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); new_from_layout CFClient::Texture $self->{layout} }; @@ -1000,7 +1050,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; @@ -1008,7 +1063,7 @@ ############################################################################# -package CFClient::UI::Entry; +package CFClient::UI::EntryBase; our @ISA = CFClient::UI::Label::; @@ -1025,6 +1080,7 @@ active_fg => [0, 0, 0], can_hover => 1, can_focus => 1, + valign => 0, @_ ) } @@ -1032,25 +1088,21 @@ sub _set_text { my ($self, $text) = @_; - my $old_text = $self->{text}; + delete $self->{cur_h}; - $self->{last_activity} = $::NOW; + return if $self->{text} eq $text; + $self->{last_activity} = $::NOW; $self->{text} = $text; - $self->{layout}->set_width ($self->{w}); - $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); $text =~ s/./*/g if $self->{hidden}; + $self->{layout}->set_text ("$text "); - $self->{layout}->set_markup ($self->escape_text ($text) . " "); - - $text = substr $text, 0, $self->{cursor}; - utf8::encode $text; - - @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); + $self->emit (changed => $self->{text}); +} - $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? - if $old_text ne $self->{text}; +sub get_text { + $_[0]{text} } sub size_request { @@ -1164,6 +1216,14 @@ #TODO: force update every cursor change :( if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { + + unless (exists $self->{cur_h}) { + my $text = substr $self->{text}, 0, $self->{cursor}; + utf8::encode $text; + + @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) + } + glColor @{$self->{fg}}; glBegin GL_LINES; glVertex $self->{cur_x}, $self->{cur_y}; @@ -1172,9 +1232,9 @@ } } -package CFClient::UI::LineEntry; +package CFClient::UI::Entry; -our @ISA = CFClient::UI::Entry::; +our @ISA = CFClient::UI::EntryBase::; use SDL; use SDL::OpenGL; @@ -1233,20 +1293,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 +1618,7 @@ delete $self->{texture}; - $TOPLEVEL->on_refresh ($self, sub { + $ROOT->on_refresh ($self, sub { if (delete $self->{need_reflow}) { my $height = 0; @@ -1738,7 +1796,7 @@ glEnable GL_CONVOLUTION_2D; } - my $tex = new CFClient::Texture + $self->{fow_texture} = new CFClient::Texture w => $w, h => $h, data => $data, @@ -1753,7 +1811,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; @@ -1769,12 +1827,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 +1990,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 +2022,7 @@ for @{$self->{children}}; } -sub translate { +sub _topleft { my ($self, $x, $y) = @_; ($x, $y) @@ -1979,7 +2040,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 +2073,7 @@ package CFClient::UI; -$TOPLEVEL = new CFClient::UI::Toplevel; +$ROOT = new CFClient::UI::Root; 1