--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/17 19:50:22 1.123 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/19 06:06:45 1.134 @@ -98,8 +98,6 @@ x => 0, y => 0, z => 0, - w => -1, - h => -1, @_ }, $class; @@ -112,11 +110,30 @@ $self } +sub show { + my ($self) = @_; + + return if $self->{parent}; + + $CFClient::UI::ROOT->add ($self); +} + +sub hide { + my ($self) = @_; + + return unless $self->{parent}; + + $self->{parent}->remove ($self); +} + 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 +145,7 @@ Carp::confess "size_request is abtract"; } -sub _size_allocate { +sub configure { my ($self, $x, $y, $w, $h) = @_; $self->{x} = $x; @@ -139,13 +156,12 @@ $self->{w} = $w; $self->{h} = $h; - 1 + $self->size_allocate ($w, $h); + $self->update; } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h); + # nothing to be done } # return top left coordinates @@ -262,6 +278,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) = @_; @@ -272,14 +302,15 @@ sub connect { my ($self, $signal, $cb) = @_; - push @{ $self->{cb}{$signal} }, $cb; + push @{ $self->{signal_cb}{$signal} }, $cb; } sub emit { my ($self, $signal, @args) = @_; - $_->($self, @args) - for @{$self->{cb}{$signal} || []}; + for my $cb (@{$self->{signal_cb}{$signal} || []}) { + $cb->($self, @args); + } } sub DESTROY { @@ -369,16 +400,17 @@ @{$self->{children}}, $child ]; - $self->{w} = $self->{h} = -1; - $self->update; + $child->check_size; } sub remove { - my ($self, $widget) = @_; + my ($self, $child) = @_; + + delete $child->{parent}; - $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; + $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; - $self->size_allocate (0, 0, $self->{w}, $self->{h}); + $self->check_size; } sub find_widget { @@ -418,11 +450,11 @@ } sub add { - my ($self, $widget) = @_; + my ($self, $child) = @_; $self->{children} = []; - $self->SUPER::add ($widget); + $self->SUPER::add ($child); } sub remove { @@ -441,11 +473,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 +511,9 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h) or return; + my ($self, $w, $h) = @_; - $self->child->size_allocate (0, 0, $w, $h); + $self->child->configure (0, 0, $w, $h); $self->render_chld; } @@ -511,6 +539,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 +582,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 { @@ -573,11 +623,15 @@ $class->SUPER::new ( bg => [1, 1, 1, 1], border_bg => [1, 1, 1, 1], - border => int $::FONTSIZE * 0.8, + border => 0.8, @_ ) } +sub border { + int $_[0]{border} * $::FONTSIZE +} + sub size_request { my ($self) = @_; @@ -586,27 +640,27 @@ my ($w, $h) = $self->SUPER::size_request; ( - $w + $self->{border} * 2, - $h + $self->{border} * 2, + $w + $self->border * 2, + $h + $self->border * 2, ) } 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; + $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 { my ($self, $ev, $x, $y) = @_; - if ($x < $self->{w} && $x >= $self->{w} - $self->{border} - && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { + my $border = $self->border; + + if ($x < $self->{w} && $x >= $self->{w} - $border + && $y < $self->{h} && $y >= $self->{h} - $border) { my ($ox, $oy) = ($ev->button_x, $ev->button_y); my ($bw, $bh) = ($self->{w}, $self->{h}); @@ -618,11 +672,11 @@ $self->{user_w} = $bw + $x - $ox; $self->{user_h} = $bh + $y - $oy; - $self->update; + $self->check_size; }; } elsif ($x >= 0 && $x < $self->{w} - && $y >= 0 && $y < $self->{border}) { + && $y >= 0 && $y < $border) { my ($ox, $oy) = ($ev->button_x, $ev->button_y); my ($bx, $by) = ($self->{x}, $self->{y}); @@ -661,11 +715,13 @@ glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; + my $border = $self->border; + glColor @{ $self->{border_bg} }; - $tex[1]->draw_quad (0, 0, $w, $self->{border}); - $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); - $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); - $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); + $tex[1]->draw_quad (0, 0, $w, $border); + $tex[3]->draw_quad (0, $border, $border, $ch); + $tex[2]->draw_quad ($w - $border, $border, $border, $ch); + $tex[4]->draw_quad (0, $h - $border, $w, $border); my $bg = $tex[0]; @@ -678,7 +734,7 @@ $bg->{s} = $rep_x; $bg->{t} = $rep_y; $bg->{wrap_mode} = 1; - $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); + $bg->draw_quad ($border, $border, $cw, $ch); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -711,8 +767,7 @@ $child->set_parent ($self); $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 +812,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 +844,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 +899,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 +931,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 +959,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 +987,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 } ############################################################################# @@ -955,7 +1008,7 @@ my $self = $class->SUPER::new ( fg => [1, 1, 1], - fontsize => $::FONTSIZE, + fontsize => 1, text => "", align => -1, valign => -1, @@ -1004,7 +1057,7 @@ my ($self) = @_; $self->{layout}->set_width; - $self->{layout}->set_height ($self->{fontsize}); + $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); my ($w, $h) = $self->{layout}->size; @@ -1015,18 +1068,9 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h) or return; - - delete $self->{texture}; -} - -sub update { - my ($self) = @_; + my ($self, $w, $h) = @_; delete $self->{texture}; - $self->SUPER::update; } sub _draw { @@ -1034,7 +1078,7 @@ my $tex = $self->{texture} ||= do { $self->{layout}->set_width ($self->{w}); - $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); + $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); new_from_layout CFClient::Texture $self->{layout} }; @@ -1094,6 +1138,8 @@ return if $self->{text} eq $text; + delete $self->{texture}; + $self->{last_activity} = $::NOW; $self->{text} = $text; @@ -1116,9 +1162,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}); } @@ -1278,6 +1322,8 @@ bg => [1, 1, 1, 0.2], active_fg => [0, 0, 1], can_hover => 1, + align => 0, + valign => 0, @_ ) } @@ -1346,12 +1392,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) = @_; @@ -1387,6 +1427,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 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; + glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; + glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; + glEnd; + + $yp += $h1; + + glBindTexture GL_TEXTURE_2D, $t2->{name}; + glBegin GL_QUADS; + glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; + glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; + glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; + glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; + glEnd; + + glDisable GL_BLEND; + glDisable GL_TEXTURE_2D; +} + +############################################################################# + package CFClient::UI::Slider; use strict; @@ -1411,8 +1558,8 @@ fg => [1, 1, 1], active_fg => [0, 0, 0], range => [0, 0, 100, 10], - req_w => 40, - req_h => 13, + req_w => 20, + req_h => 20, vertical => 0, can_hover => 1, inner_pad => 5, @@ -1532,9 +1679,7 @@ my $class = shift; my $self = $class->SUPER::new ( - req_w => $::WIDTH / 6, - req_h => $::HEIGHT / 6, - fontsize => $::FONTSIZE, + fontsize => 1, @_, layout => (new CFClient::Layout), @@ -1565,7 +1710,7 @@ my $layout = $self->{layout}; - $layout->set_height ($self->{fontsize}); + $layout->set_height ($self->{fontsize} * $::FONTSIZE); $layout->set_width ($self->{w}); $layout->set_text ($text); @@ -1579,19 +1724,13 @@ $self->update; } -sub size_request { - my ($self) = @_; - - ($self->{req_w}, $self->{req_h}) -} - sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; + my ($self, $w, $h) = @_; - $self->SUPER::size_allocate ($x, $y, $w, $h); + $self->SUPER::size_allocate ($w, $h); - $self->{layout}->set_height ($self->{fontsize}); - $self->{layout}->set_width ($self->{w}); + $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); + $self->{layout}->set_width ($self->{children}[0]{w}); $self->reflow; } @@ -1783,9 +1922,9 @@ my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); if ($::CFG->{fow_enable}) { - if ($::CFG->{fow_smooth}) { # smooth fog of war - glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; - glConvolutionFilter2D + if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war + glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); + glConvolutionFilter2D ( GL_CONVOLUTION_2D, GL_ALPHA, 3, 3, @@ -1794,7 +1933,7 @@ 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1, - ; + ); glEnable GL_CONVOLUTION_2D; } @@ -1834,7 +1973,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); @@ -2011,16 +2150,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}}; } @@ -2033,16 +2178,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 {