--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 20:44:49 1.75 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 21:24:09 1.76 @@ -183,7 +183,7 @@ if ($self == $HOVER) { my ($x, $y) = @$self{qw(x y)}; - glColor 1, 1, 1, 0.1; + glColor 0, 0, 1, 0.2; glEnable GL_BLEND; glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glBegin GL_QUADS; @@ -277,7 +277,7 @@ # range [value, low, high, page] $class->SUPER::new ( - bg => [0, 0, 0, 0.4], + bg => [0, 0, 0, 0.2], active_bg => [1, 1, 1], @_ ) @@ -288,13 +288,18 @@ my ($w, $h) = @$self{qw(w h)}; + glEnable GL_BLEND; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; + glBegin GL_QUADS; glVertex 0 , 0; glVertex 0 , $h; glVertex $w, $h; glVertex $w, 0; glEnd; + + glDisable GL_BLEND; } ############################################################################# @@ -587,6 +592,7 @@ $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); my $bg = $tex[0]; + glBindTexture GL_TEXTURE_2D, $bg->{name}; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; @@ -597,8 +603,8 @@ $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); - glDisable GL_BLEND; glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; $self->child->draw; @@ -696,6 +702,22 @@ } +sub find_widget { + my ($self, $x, $y) = @_; + + $x -= $self->{x}; + $y -= $self->{y}; + + my $res; + + for (grep $_, map @$_, grep $_, @{ $self->{children} }) { + $res = $_->find_widget ($x, $y) + and return $res; + } + + $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) +} + sub _draw { my ($self) = @_; @@ -706,11 +728,73 @@ ############################################################################# -package CFClient::UI::VBox; +package CFClient::UI::HBox; + +# TODO: wrap into common Box base class our @ISA = CFClient::UI::Container::; -use SDL::OpenGL; +sub size_request { + my ($self) = @_; + + my @alloc = map [$_->size_request], @{$self->{children}}; + + ( + (List::Util::sum map $_->[0], @alloc), + (List::Util::max map $_->[1], @alloc), + ) +} + +sub size_allocate { + my ($self, $x, $y, $w, $h) = @_; + + $self->_size_allocate ($x, $y, $w, $h) or return; + + return unless $self->{w}; + + ($h, $w) = ($w, $h); + + my $children = $self->{children}; + + my @h = map +($_->size_request)[0], @$children; + + my $req_h = List::Util::sum @h; + + if ($req_h > $h) { + # ah well, not enough space + $_ = $h[$_] * $h / $req_h for @h; + } else { + my @exp = grep $_->{expand}, @$children; + @exp = @$children unless @exp; + + my %exp = map +($_ => 1), @exp; + + for (0 .. $#$children) { + my $child = $children->[$_]; + + my $alloc_h = $h[$_]; + $alloc_h += ($h - $req_h) / @exp if $exp{$child}; + $h[$_] = $alloc_h; + } + } + + my $y = 0; + for (0 .. $#$children) { + my $child = $children->[$_]; + my $h = $h[$_]; + $child->size_allocate ($y, 0, $h, $w); + + $y += $h; + } +} + +############################################################################# + +package CFClient::UI::VBox; + +# TODO: wrap into common Box base class + +our @ISA = CFClient::UI::Container::; sub size_request { my ($self) = @_; @@ -776,11 +860,12 @@ my ($class, %arg) = @_; my $self = $class->SUPER::new ( - fg => [1, 1, 1], - height => $::FONTSIZE, - text => "", - align => -1, - layout => new CFClient::Layout, + fg => [1, 1, 1], + height => $::FONTSIZE, + text => "", + align => -1, + padding => 2, + layout => new CFClient::Layout, %arg ); @@ -820,17 +905,12 @@ $self->{layout}->set_width; $self->{layout}->set_height ($self->{height}); - $self->{layout}->size -# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack -# ( -# $self->{texture}{w}, -# $self->{texture}{h}, -# ) -# } else { -# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; -# -# ($w, $h) -# } + my ($w, $h) = $self->{layout}->size; + + ( + $w + $self->{padding} * 2, + $h + $self->{padding} * 2, + ) } sub size_allocate { @@ -864,8 +944,8 @@ glColor @{$self->{fg}}; my $x = - $self->{align} < 0 ? 0 - : $self->{align} > 0 ? $self->{w} - $tex->{w} + $self->{align} < 0 ? $self->{padding} + : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} : ($self->{w} - $tex->{w}) * 0.5; $tex->draw_quad ($x, 0); @@ -888,7 +968,7 @@ $class->SUPER::new ( fg => [1, 1, 1], - bg => [0, 0, 0, 0.4], + bg => [0, 0, 0, 0.2], active_bg => [1, 1, 1], active_fg => [0, 0, 0], @_ @@ -905,8 +985,7 @@ $text =~ s/./*/g if $self->{hidden}; - - $self->{layout}->set_markup ($self->escape_text ($text)); + $self->{layout}->set_markup ($self->escape_text ($text) . " "); $text = substr $text, 0, $self->{cursor}; utf8::encode $text; @@ -956,6 +1035,10 @@ --$self->{cursor} if $self->{cursor}; } elsif ($sym == SDLK_RIGHT) { ++$self->{cursor} if $self->{cursor} < length $self->{text}; + } elsif ($sym == SDLK_HOME) { + $self->{cursor} = 0; + } elsif ($sym == SDLK_END) { + $self->{cursor} = length $text; } elsif ($uni) { substr $text, $self->{cursor}++, 0, chr $uni; } @@ -980,7 +1063,7 @@ my $idx = $self->{layout}->xy_to_index ($x, $y); # byte-index to char-index - my $text = $self->{layout}; + my $text = $self->{text}; utf8::encode $text; $self->{cursor} = length substr $text, 0, $idx; @@ -1005,12 +1088,15 @@ glColor @{$self->{bg}}; } + glEnable GL_BLEND; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glBegin GL_QUADS; glVertex 0 , 0; glVertex 0 , $self->{h}; glVertex $self->{w}, $self->{h}; glVertex $self->{w}, 0; glEnd; + glDisable GL_BLEND; $self->SUPER::_draw; @@ -1034,27 +1120,31 @@ our @ISA = CFClient::UI::DrawBG::; -sub size_request { - my ($self) = @_; - - my $w = 50; - my $h = 10; - - $self->{vertical} ? ($h, $w) : ($w, $h) -} - sub new { my $class = shift; # range [value, low, high, page] - $class->SUPER::new ( + my $self = $class->SUPER::new ( fg => [1, 1, 1], active_fg => [0, 0, 0], range => [0, 0, 100, 10], - vertical => 1, + req_w => 40, + req_h => 10, + vertical => 0, @_ - ) + ); + + $self +} + +sub size_request { + my ($self) = @_; + + my $w = $self->{req_w}; + my $h = $self->{req_h}; + + $self->{vertical} ? ($h, $w) : ($w, $h) } sub button_down {