--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 20:44:49 1.75 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/12 00:40:45 1.81 @@ -45,7 +45,7 @@ $BUTTON_STATE &= ~(1 << ($ev->button - 1)); - $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; + $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; if (!$BUTTON_STATE) { my $grab = $GRAB; undef $GRAB; @@ -81,14 +81,22 @@ sub new { my $class = shift; - bless { + my $self = bless { x => 0, y => 0, z => 0, w => -1, h => -1, @_ - }, $class + }, $class; + + for (keys %$self) { + if (/^connect_(.*)$/) { + $self->connect ($1 => delete $self->{$_}); + } + } + + $self } sub move { @@ -183,7 +191,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; @@ -202,17 +210,6 @@ warn "no draw defined for $self\n"; } -sub bbox { - my ($self) = @_; - my ($w, $h) = $self->size_request; - ( - $self->{x}, - $self->{y}, - $self->{x} = $w, - $self->{y} = $h - ) -} - sub find_widget { my ($self, $x, $y) = @_; @@ -223,8 +220,6 @@ () } -sub del_parent { $_[0]->{parent} = undef } - sub set_parent { my ($self, $par) = @_; @@ -277,8 +272,8 @@ # range [value, low, high, page] $class->SUPER::new ( - bg => [0, 0, 0, 0.4], - active_bg => [1, 1, 1], + bg => [0, 0, 0, 0.2], + active_bg => [1, 1, 1, 0.5], @_ ) } @@ -288,13 +283,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; } ############################################################################# @@ -327,9 +327,8 @@ } sub add { - my ($self, $chld, $expand) = @_; + my ($self, $chld) = @_; - $chld->{expand} = $expand; $chld->set_parent ($self); $self->{children} = [ @@ -535,6 +534,8 @@ sub size_request { my ($self) = @_; + return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; + my ($w, $h) = $self->SUPER::size_request; $h += $tex[1]->{h}; @@ -550,10 +551,10 @@ $self->_size_allocate ($x, $y, $w, $h) or return; - $h -= $tex[1]->{h}; - $h -= $tex[4]->{h}; - $w -= $tex[2]->{w}; - $w -= $tex[3]->{w}; + $h -= $tex[1]{h}; + $h -= $tex[4]{h}; + $w -= $tex[2]{w}; + $w -= $tex[3]{w}; $h = $h < 0 ? 0 : $h; $w = $w < 0 ? 0 : $w; @@ -563,6 +564,54 @@ $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); } +sub button_down { + my ($self, $ev, $x, $y) = @_; + + if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w} + && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) { + + my ($ox, $oy) = ($ev->button_x, $ev->button_y); + my ($bw, $bh) = ($self->{w}, $self->{h}); + + $self->{motion} = sub { + my ($ev, $x, $y) = @_; + + ($x, $y) = ($ev->motion_x, $ev->motion_y); + + $self->{user_w} = $bw + $x - $ox; + $self->{user_h} = $bh + $y - $oy; + $self->update; + }; + + } elsif ($x >= 0 && $x < $self->{w} + && $y >= 0 && $y < $tex[1]{h}) { + + my ($ox, $oy) = ($ev->button_x, $ev->button_y); + my ($bx, $by) = ($self->{x}, $self->{y}); + + $self->{motion} = sub { + my ($ev, $x, $y) = @_; + + ($x, $y) = ($ev->motion_x, $ev->motion_y); + + $self->move ($bx + $x - $ox, $by + $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 _draw { my ($self) = @_; @@ -587,6 +636,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 +647,8 @@ $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); - glDisable GL_BLEND; glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; $self->child->draw; @@ -614,6 +664,15 @@ use SDL::OpenGL; +sub new { + my $class = shift; + + $class->SUPER::new ( + col_expand => [], + @_ + ) +} + sub add { my ($self, $x, $y, $chld) = @_; @@ -652,8 +711,8 @@ my ($ws, $hs) = $self->get_wh; ( - (List::Util::sum @$ws), - (List::Util::sum @$hs), + (sum @$ws), + (sum @$hs), ) } @@ -664,12 +723,17 @@ my ($ws, $hs) = $self->get_wh; - my $req_w = List::Util::sum @$ws; - my $req_h = List::Util::sum @$hs; + my $req_w = sum @$ws; + my $req_h = sum @$hs; + + # TODO: nicer code && do row_expand + my @col_expand = @{$self->{col_expand}}; + @col_expand = (1) x @$ws unless @col_expand; + my $col_expand = (sum @col_expand) || 1; # linearly scale sizes - $_ *= $req_w / $w for @$ws; - $_ *= $req_h / $h for @$hs; + $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; + $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; my $y; @@ -696,6 +760,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 +786,71 @@ ############################################################################# -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 / $req_h for @h; + } else { + my $exp = List::Util::sum map $_->{expand}, @$children; + $exp ||= 1; + + for (0 .. $#$children) { + my $child = $children->[$_]; + + my $alloc_h = $h[$_]; + $alloc_h += ($h - $req_h) * $child->{expand} / $exp; + $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) = @_; @@ -738,19 +878,15 @@ if ($req_h > $h) { # ah well, not enough space - $_ = $h[$_] * $h / $req_h for @h; + $_ *= $h / $req_h for @h; } else { - my @exp = grep $_->{expand}, @$children; - @exp = @$children unless @exp; + my $exp = List::Util::sum map $_->{expand}, @$children; + $exp ||= 1; - 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; + $h[$_] += ($h - $req_h) * $child->{expand} / $exp; } } @@ -776,11 +912,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 ); @@ -806,6 +943,7 @@ $self->{layout}->set_markup ($text); delete $self->{texture}; +# $self->{w} = $self->{h} = -1; $self->update; } @@ -820,17 +958,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 { @@ -853,6 +986,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->{height}); new_from_layout CFClient::Texture $self->{layout} }; @@ -864,11 +998,11 @@ 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); + $tex->draw_quad ($x, $self->{padding}); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -888,8 +1022,8 @@ $class->SUPER::new ( fg => [1, 1, 1], - bg => [0, 0, 0, 0.4], - active_bg => [1, 1, 1], + bg => [0, 0, 0, 0.2], + active_bg => [1, 1, 1, 0.5], active_fg => [0, 0, 0], @_ ) @@ -905,8 +1039,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 +1089,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 +1117,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 +1142,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; @@ -1026,35 +1166,107 @@ ############################################################################# -package CFClient::UI::Slider; +package CFClient::UI::Button; -use strict; +our @ISA = CFClient::UI::Label::; +use SDL; use SDL::OpenGL; -our @ISA = CFClient::UI::DrawBG::; +sub new { + my $class = shift; -sub size_request { + $class->SUPER::new ( + padding => 4, + fg => [1, 1, 1], + bg => [1, 1, 1, 0.2], + active_bg => [0, 0, 0, 0.5], + active_fg => [1, 1, 0], + border_fg => [1, 1, 0], + @_ + ) +} + +sub button_up { + my ($self, $ev, $x, $y) = @_; + + if ($x >= 0 && $x < $self->{w} + && $y >= 0 && $y < $self->{h}) { + $self->emit ("activate"); + } +} + +sub _draw { my ($self) = @_; - my $w = 50; - my $h = 10; + local $self->{fg} = $self->{fg}; - $self->{vertical} ? ($h, $w) : ($w, $h) + glEnable GL_BLEND; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + + glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly + + glBegin GL_LINE_LOOP; + glColor @{$self->{border_fg}}; + glVertex 1 , 1; + glVertex 1 , $self->{h} - 2; + glVertex $self->{w} - 2, $self->{h} - 2; + glVertex $self->{w} - 2, 1; + glEnd; + + if ($GRAB == $self) { + glColor @{$self->{active_bg}}; + $self->{fg} = $self->{active_fg}; + } else { + glColor @{$self->{bg}}; + } + + glBegin GL_QUADS; + glVertex 2 , 2; + glVertex 2 , $self->{h} - 2; + glVertex $self->{w} - 2, $self->{h} - 2; + glVertex $self->{w} - 2, 2; + glEnd; + glDisable GL_BLEND; + + $self->SUPER::_draw; } +############################################################################# + +package CFClient::UI::Slider; + +use strict; + +use SDL::OpenGL; + +our @ISA = CFClient::UI::DrawBG::; + 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 { @@ -1109,6 +1321,7 @@ $w -= $page; $page &= ~1; glTranslate $page * 0.5, 0, 0; + $page ||= 2; glColor @$fg; glBegin GL_LINES; @@ -1169,8 +1382,8 @@ sub size_request { ( - 1 + int $::WIDTH / 32, - 1 + int $::HEIGHT / 32, + 1 + 32 * int $::WIDTH / 32, + 1 + 32 * int $::HEIGHT / 32, ) } @@ -1181,7 +1394,7 @@ $self->SUPER::update; } -sub _draw { +sub draw { my ($self) = @_; if (delete $self->{need_update}) {