--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/15 19:35:20 1.115 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/17 21:03:31 1.127 @@ -76,7 +76,7 @@ my $rem = 0; - for ($vals) { + for (@$vals) { my $i = int $_ + $rem; $rem += $_ - $i; $_ = $i; @@ -114,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; } @@ -178,6 +178,9 @@ return unless $self->{can_focus}; my $focus = $FOCUS; $FOCUS = $self; + + $self->emit (focus_in => $focus); + $focus->update if $focus; $FOCUS->update; } @@ -188,6 +191,9 @@ return unless $FOCUS == $self; my $focus = $FOCUS; undef $FOCUS; + + $self->emit (focus_out => $focus); + $focus->update if $focus; #? } @@ -221,7 +227,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; @@ -256,6 +262,20 @@ 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}; + } +} + sub update { my ($self) = @_; @@ -364,7 +384,8 @@ ]; $self->{w} = $self->{h} = -1; - $self->update; + + $child->check_size; } sub remove { @@ -372,7 +393,7 @@ $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; - $self->size_allocate (0, 0, $self->{w}, $self->{h}); + $self->check_size; } sub find_widget { @@ -505,6 +526,36 @@ ############################################################################# +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 size_allocate { + my ($self, $x, $y, $w, $h) = @_; + + $self->_size_allocate ($x, $y, $w, $h) or return; +} + +sub _draw { + my ($self) = @_; + + $self->{children}[1]->draw; +} + + +############################################################################# + package CFClient::UI::Frame; our @ISA = CFClient::UI::Bin::; @@ -567,7 +618,7 @@ $class->SUPER::new ( bg => [1, 1, 1, 1], border_bg => [1, 1, 1, 1], - border => $::FONTSIZE * 0.8, + border => int $::FONTSIZE * 0.8, @_ ) } @@ -706,7 +757,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? @@ -842,7 +894,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h); + $self->_size_allocate ($x, $y, $w, $h) or return; ($h, $w) = ($w, $h); @@ -878,6 +930,8 @@ $y += $h; } + + 1 } ############################################################################# @@ -902,7 +956,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h); + $self->_size_allocate ($x, $y, $w, $h) or return; my $children = $self->{children}; @@ -934,6 +988,8 @@ $y += $h; } + + 1 } ############################################################################# @@ -952,12 +1008,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 } @@ -975,18 +1035,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->{layout}->set_markup ($markup); - $self->{text} + delete $self->{texture}; + $self->update; } sub size_request { @@ -994,6 +1055,7 @@ $self->{layout}->set_width; $self->{layout}->set_height ($self->{fontsize}); + my ($w, $h) = $self->{layout}->size; ( @@ -1022,7 +1084,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} }; @@ -1033,12 +1095,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 + ); - $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); + $self->{oy} = int ( + $self->{valign} < 0 ? $self->{padding} + : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} + : ($self->{h} - $tex->{h}) * 0.5 + ); + + $tex->draw_quad ($self->{ox}, $self->{oy}); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1046,7 +1115,7 @@ ############################################################################# -package CFClient::UI::Entry; +package CFClient::UI::EntryBase; our @ISA = CFClient::UI::Label::; @@ -1063,6 +1132,7 @@ active_fg => [0, 0, 0], can_hover => 1, can_focus => 1, + valign => 0, @_ ) } @@ -1070,25 +1140,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 { @@ -1102,7 +1168,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->SUPER::size_allocate ($x, $y, $w, $h); + $self->SUPER::size_allocate ($x, $y, $w, $h) or return; $self->_set_text ($self->{text}); } @@ -1202,17 +1268,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; @@ -1252,8 +1326,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, @_ ) } @@ -1271,20 +1347,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; @@ -1327,7 +1401,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h); + $self->_size_allocate ($x, $y, $w, $h) or return; } sub button_down { @@ -1365,6 +1439,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; @@ -1566,12 +1747,13 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->SUPER::size_allocate ($x, $y, $w, $h); + $self->SUPER::size_allocate ($x, $y, $w, $h) or return; $self->{layout}->set_height ($self->{fontsize}); $self->{layout}->set_width ($self->{w}); $self->reflow; + $self->update; } sub add_paragraph { @@ -1776,7 +1958,7 @@ glEnable GL_CONVOLUTION_2D; } - my $tex = new CFClient::Texture + $self->{fow_texture} = new CFClient::Texture w => $w, h => $h, data => $data, @@ -1791,7 +1973,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; @@ -1807,12 +1989,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; @@ -1864,9 +2048,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) { @@ -1874,12 +2058,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]"); } } } @@ -1891,10 +2075,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"); } } @@ -1987,6 +2171,12 @@ use SDL::OpenGL; +sub check_size { + my ($self) = @_; + + $self->size_allocate (0, 0, $::WITH, $::HEIGHT); +} + sub size_request { ($::WIDTH, $::HEIGHT) } @@ -2009,7 +2199,7 @@ sub update { my ($self) = @_; - $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); + $self->check_size; ::refresh (); } @@ -2018,7 +2208,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 {