--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/16 17:43:17 1.120 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/18 01:51:06 1.130 @@ -114,9 +114,12 @@ sub move { my ($self, $x, $y, $z) = @_; + $self->{x} = int $x; $self->{y} = int $y; $self->{z} = $z if defined $z; + + $self->update; } sub needs_redraw { @@ -128,7 +131,7 @@ Carp::confess "size_request is abtract"; } -sub _size_allocate { +sub configure { my ($self, $x, $y, $w, $h) = @_; $self->{x} = $x; @@ -139,13 +142,11 @@ $self->{w} = $w; $self->{h} = $h; - 1 + $self->size_allocate ($w, $h); } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h); + # nothing to be done } # return top left coordinates @@ -227,7 +228,7 @@ if ($self == $HOVER && $self->{can_hover}) { my ($x, $y) = @$self{qw(x y)}; - glColor 0, 0, 1, 0.2; + glColor 1, 0.8, 0.5, 0.2; glEnable GL_BLEND; glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glBegin GL_QUADS; @@ -262,6 +263,23 @@ Scalar::Util::weaken ($self->{parent} = $parent); } +sub check_size { + my ($self) = @_; + + my ($w, $h) = $self->size_request; + + if ($w != $self->{req_w} || $h != $self->{req_h}) { + $self->{req_w} = $w; + $self->{req_h} = $h; + + $self->{parent}->check_size + if $self->{parent}; + + $self->size_allocate ($w, $h); + $self->update; + } +} + sub update { my ($self) = @_; @@ -370,7 +388,8 @@ ]; $self->{w} = $self->{h} = -1; - $self->update; + + $child->check_size; } sub remove { @@ -378,7 +397,7 @@ $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; - $self->size_allocate (0, 0, $self->{w}, $self->{h}); + $self->check_size; } sub find_widget { @@ -441,11 +460,9 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; + my ($self, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h) or return; - - $self->{children}[0]->size_allocate (0, 0, $w, $h); + $self->{children}[0]->configure (0, 0, $w, $h); } ############################################################################# @@ -481,11 +498,9 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; + my ($self, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h) or return; - - $self->child->size_allocate (0, 0, $w, $h); + $self->child->configure (0, 0, $w, $h); $self->render_chld; } @@ -511,6 +526,30 @@ ############################################################################# +package CFClient::UI::ViewPort; + +our @ISA = CFClient::UI::Window::; + +sub new { die } + +sub size_request { + my ($self) = @_; + + @$self{qw(child_w child_h)} = $self->child->size_request; + $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); + + @$self{qw(child_w child_h)} +} + +sub _draw { + my ($self) = @_; + + $self->{children}[1]->draw; +} + + +############################################################################# + package CFClient::UI::Frame; our @ISA = CFClient::UI::Bin::; @@ -530,9 +569,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h) or return; - - $self->child->size_allocate (2, 2, $w - 4, $h - 4); + $self->child->configure (2, 2, $w - 4, $h - 4); } sub _draw { @@ -592,14 +629,12 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h) or return; + my ($self, $w, $h) = @_; $h -= List::Util::max 0, $self->{border} * 2; $w -= List::Util::max 0, $self->{border} * 2; - $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); + $self->child->configure ($self->{border}, $self->{border}, $w, $h); } sub button_down { @@ -618,7 +653,7 @@ $self->{user_w} = $bw + $x - $ox; $self->{user_h} = $bh + $y - $oy; - $self->update; + $self->check_size; }; } elsif ($x >= 0 && $x < $self->{w} @@ -712,7 +747,8 @@ $self->{children}[$y][$x] = $child; $self->{w} = $self->{h} = -1; - $self->update; + + $child->check_size; } # TODO: move to container class maybe? send childs a signal on removal? @@ -757,9 +793,7 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h) or return; + my ($self, $w, $h) = @_; my ($ws, $hs) = $self->get_wh; @@ -791,7 +825,7 @@ my $col_w = $ws->[$c]; if (my $widget = $row->[$c]) { - $widget->size_allocate ($x, $y, $col_w, $row_h); + $widget->configure ($x, $y, $col_w, $row_h); } $x += $col_w; @@ -846,9 +880,7 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h); + my ($self, $w, $h) = @_; ($h, $w) = ($w, $h); @@ -880,10 +912,12 @@ for (0 .. $#$children) { my $child = $children->[$_]; my $h = $h[$_]; - $child->size_allocate ($y, 0, $h, $w); + $child->configure ($y, 0, $h, $w); $y += $h; } + + 1 } ############################################################################# @@ -906,9 +940,7 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h); + my ($self, $w, $h) = @_; my $children = $self->{children}; @@ -936,10 +968,12 @@ for (0 .. $#$children) { my $child = $children->[$_]; my $h = $h[$_]; - $child->size_allocate (0, $y, $w, $h); + $child->configure (0, $y, $w, $h); $y += $h; } + + 1 } ############################################################################# @@ -964,7 +998,10 @@ %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 } @@ -982,18 +1019,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 { @@ -1001,6 +1039,7 @@ $self->{layout}->set_width; $self->{layout}->set_height ($self->{fontsize}); + my ($w, $h) = $self->{layout}->size; ( @@ -1010,9 +1049,7 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h) or return; + my ($self, $w, $h) = @_; delete $self->{texture}; } @@ -1029,7 +1066,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} }; @@ -1040,17 +1077,19 @@ glColor @{$self->{fg}}; - my $x = + $self->{ox} = int ( $self->{align} < 0 ? $self->{padding} : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} - : ($self->{w} - $tex->{w}) * 0.5; + : ($self->{w} - $tex->{w}) * 0.5 + ); - my $y = + $self->{oy} = int ( $self->{valign} < 0 ? $self->{padding} : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} - : ($self->{h} - $tex->{h}) * 0.5; + : ($self->{h} - $tex->{h}) * 0.5 + ); - $tex->draw_quad (int $x, int $y); + $tex->draw_quad ($self->{ox}, $self->{oy}); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1058,7 +1097,7 @@ ############################################################################# -package CFClient::UI::Entry; +package CFClient::UI::EntryBase; our @ISA = CFClient::UI::Label::; @@ -1083,25 +1122,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 { @@ -1113,9 +1148,7 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->SUPER::size_allocate ($x, $y, $w, $h); + my ($self, $w, $h) = @_; $self->_set_text ($self->{text}); } @@ -1215,17 +1248,25 @@ #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}; - glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; + glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; + glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; glEnd; } } -package CFClient::UI::LineEntry; +package CFClient::UI::Entry; -our @ISA = CFClient::UI::Entry::; +our @ISA = CFClient::UI::EntryBase::; use SDL; use SDL::OpenGL; @@ -1265,8 +1306,10 @@ padding => 4, fg => [1, 1, 1], bg => [1, 1, 1, 0.2], - active_fg => [1, 1, 0], + active_fg => [0, 0, 1], can_hover => 1, + align => 0, + valign => 0, @_ ) } @@ -1335,12 +1378,6 @@ ($self->{padding} * 2 + 6) x 2 } -sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h); -} - sub button_down { my ($self, $ev, $x, $y) = @_; @@ -1376,6 +1413,113 @@ ############################################################################# +package CFClient::UI::VGauge; + +our @ISA = CFClient::UI::Base::; + +use SDL::OpenGL; + +my %tex = ( + food => [ + map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } + qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ + ], + grace => [ + map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } + qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ + ], + hp => [ + map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } + qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ + ], + mana => [ + map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } + qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ + ], +); + +# eg. VGauge->new (gauge => 'food'), default gauge: food +sub new { + my $class = shift; + + my $self = $class->SUPER::new (gauge => 'food', @_); + + $self +} + +sub size_request { + my ($self) = @_; + + my $tex = $tex{$self->{gauge}}[0]; + + @$tex{qw(w h)} +} + +sub set_max { + my ($self, $max) = @_; + + $self->{max_val} = $max; +} + +sub set_value { + my ($self, $val, $max) = @_; + + $self->set_max ($max) + if defined $max; + + $max = $self->{max_val}; + $self->{val} = $val; + + $self->update; +} + +sub _draw { + my ($self) = @_; + + my $tex = $tex{$self->{gauge}}; + + my ($w, $h) = ($self->{w}, $self->{h}); + + my $ycut = $self->{val} / ($self->{max_val} || 1); + $ycut = 1 if $self->{val} > $self->{max_val}; + + my $t1 = $tex->[0]; + my $t2 = $tex->[1]; + + 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; + + my $h1 = $self->{h} - $ycut * $self->{h}; + my $h2 = $ycut * $self->{h}; + + my $yp = 0; + + glBindTexture (GL_TEXTURE_2D, $t1->{name}); + glBegin (GL_QUADS); + glTexCoord (0, 0); glVertex (0 , $yp); + glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); + glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); + glTexCoord (1, 0); glVertex (0 + $w, $yp); + glEnd (); + + $yp += $h1; + + glBindTexture (GL_TEXTURE_2D, $t2->{name}); + glBegin (GL_QUADS); + glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); + glTexCoord (0, 1); glVertex (0 , $yp + $h2); + glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); + glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); + glEnd (); + + glDisable GL_BLEND; + glDisable GL_TEXTURE_2D; +} + +############################################################################# + package CFClient::UI::Slider; use strict; @@ -1575,14 +1719,13 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->SUPER::size_allocate ($x, $y, $w, $h); + my ($self, $w, $h) = @_; $self->{layout}->set_height ($self->{fontsize}); $self->{layout}->set_width ($self->{w}); $self->reflow; + $self->update; } sub add_paragraph { @@ -1773,8 +1916,8 @@ if ($::CFG->{fow_enable}) { if ($::CFG->{fow_smooth}) { # smooth fog of war - glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; - glConvolutionFilter2D + glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); + glConvolutionFilter2D ( GL_CONVOLUTION_2D, GL_ALPHA, 3, 3, @@ -1783,7 +1926,7 @@ 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1, - ; + ); glEnable GL_CONVOLUTION_2D; } @@ -1823,7 +1966,7 @@ w => $w, h => $h, data => $::MAP->mapmap ($w, $h), - type => GL_UNSIGNED_INT_8_8_8_8_REV; + type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; $self->{mapmap_texture}->draw_quad (100, 100); @@ -1877,9 +2020,9 @@ my $sym = $ev->key_sym; if ($sym == SDLK_KP5) { - $::CONN->user_send ("command stay fire"); + $::CONN->user_send ("stay fire"); } elsif ($sym == SDLK_a) { - $::CONN->user_send ("command apply"); + $::CONN->user_send ("apply"); } elsif ($sym == SDLK_QUOTE) { $self->emit ('activate_console'); } elsif ($sym == SDLK_SLASH) { @@ -1887,12 +2030,12 @@ } elsif (exists $DIR{$sym}) { if ($mod & KMOD_SHIFT) { $self->{shft}++; - $::CONN->user_send ("command fire $DIR{$sym}[0]"); + $::CONN->user_send ("fire $DIR{$sym}[0]"); } elsif ($mod & KMOD_CTRL) { $self->{ctrl}++; - $::CONN->user_send ("command run $DIR{$sym}[0]"); + $::CONN->user_send ("run $DIR{$sym}[0]"); } else { - $::CONN->user_send ("command $DIR{$sym}[1]"); + $::CONN->user_send ("$DIR{$sym}[1]"); } } } @@ -1904,10 +2047,10 @@ my $sym = $ev->key_sym; if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { - $::CONN->user_send ("command fire_stop"); + $::CONN->user_send ("fire_stop"); } if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { - $::CONN->user_send ("command run_stop"); + $::CONN->user_send ("run_stop"); } } @@ -2000,16 +2143,22 @@ use SDL::OpenGL; +sub check_size { + my ($self) = @_; + + $self->configure (0, 0, $::WITH, $::HEIGHT); +} + sub size_request { ($::WIDTH, $::HEIGHT) } -sub size_allocate { +sub configure { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h); + $self->SUPER::configure ($x, $y, $w, $h); - $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) + $_->configure ($_->{x}, $_->{y}, $_->size_request) for @{$self->{children}}; } @@ -2022,16 +2171,14 @@ sub update { my ($self) = @_; - $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); + $self->check_size; ::refresh (); } sub add { - my ($self, $widget) = @_; - - $self->SUPER::add ($widget); + my ($self, $child) = @_; - $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request); + $self->SUPER::add ($child); } sub on_refresh {