--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 22:14:13 1.77 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/12 23:15:39 1.91 @@ -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 { @@ -127,7 +135,7 @@ $self->_size_allocate ($x, $y, $w, $h); } -# translate global koordinates to local coordinate system +# translate global coordinates to local coordinate system sub translate { my ($self, $x, $y) = @_; @@ -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) = @_; @@ -278,7 +273,7 @@ $class->SUPER::new ( bg => [0, 0, 0, 0.2], - active_bg => [1, 1, 1], + active_bg => [1, 1, 1, 0.5], @_ ) } @@ -539,10 +534,9 @@ sub size_request { my ($self) = @_; - my ($w, $h) = - $self->{user_w} && $self->{user_h} - ? ($self->{user_w}, $self->{user_h}) - : ($self->SUPER::size_request); + 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}; $h += $tex[4]->{h}; @@ -645,8 +639,6 @@ 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; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; my $rep_x = $cw / $bg->{w}; my $rep_y = $ch / $bg->{h}; @@ -670,6 +662,15 @@ use SDL::OpenGL; +sub new { + my $class = shift; + + $class->SUPER::new ( + col_expand => [], + @_ + ) +} + sub add { my ($self, $x, $y, $chld) = @_; @@ -708,8 +709,8 @@ my ($ws, $hs) = $self->get_wh; ( - (List::Util::sum @$ws), - (List::Util::sum @$hs), + (sum @$ws), + (sum @$hs), ) } @@ -720,12 +721,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 - $_ *= $w / $req_w for @$ws; - $_ *= $h / $req_h for @$hs; + $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; + $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; my $y; @@ -737,12 +743,11 @@ my $row_h = $hs->[$r]; for my $c (0 .. $#$row) { - my $widget = $row->[$c] - or next; - my $col_w = $ws->[$c]; - $widget->size_allocate ($x, $y, $col_w, $row_h); + if (my $widget = $row->[$c]) { + $widget->size_allocate ($x, $y, $col_w, $row_h); + } $x += $col_w; } @@ -798,9 +803,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h) or return; - - return unless $self->{w}; + $self->_size_allocate ($x, $y, $w, $h); ($h, $w) = ($w, $h); @@ -812,7 +815,7 @@ if ($req_h > $h) { # ah well, not enough space - $_ = $h[$_] * $h / $req_h for @h; + $_ *= $h / $req_h for @h; } else { my $exp = List::Util::sum map $_->{expand}, @$children; $exp ||= 1; @@ -858,9 +861,7 @@ sub size_allocate { my ($self, $x, $y, $w, $h) = @_; - $self->_size_allocate ($x, $y, $w, $h) or return; - - return unless $self->{h}; + $self->_size_allocate ($x, $y, $w, $h); my $children = $self->{children}; @@ -870,7 +871,7 @@ if ($req_h > $h) { # ah well, not enough space - $_ = $h[$_] * $h / $req_h for @h; + $_ *= $h / $req_h for @h; } else { my $exp = List::Util::sum map $_->{expand}, @$children; $exp ||= 1; @@ -935,6 +936,7 @@ $self->{layout}->set_markup ($text); delete $self->{texture}; +# $self->{w} = $self->{h} = -1; $self->update; } @@ -977,6 +979,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} }; @@ -992,7 +995,8 @@ : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} : ($self->{w} - $tex->{w}) * 0.5; - $tex->draw_quad ($x, $self->{padding}); + glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0; + $tex->draw_quad (0, 0); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1013,7 +1017,7 @@ $class->SUPER::new ( fg => [1, 1, 1], bg => [0, 0, 0, 0.2], - active_bg => [1, 1, 1], + active_bg => [1, 1, 1, 0.5], active_fg => [0, 0, 0], @_ ) @@ -1026,6 +1030,7 @@ $self->{text} = $text; $self->{layout}->set_width ($self->{w}); + $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); $text =~ s/./*/g if $self->{hidden}; @@ -1156,6 +1161,138 @@ ############################################################################# +package CFClient::UI::Button; + +our @ISA = CFClient::UI::Label::; + +use SDL; +use SDL::OpenGL; + +my @tex = + map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } + qw(b1_button_active.png); + +sub new { + my $class = shift; + + $class->SUPER::new ( + padding => 4, + fg => [1, 1, 1], + bg => [1, 1, 1, 0.2], + active_fg => [1, 1, 0], +# active_bg => [0, 0, 0, 0.5], +# 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) = @_; + + 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}; + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; + + $tex->draw_quad (0, 0, $self->{w}, $self->{h}); + + glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; + + $self->SUPER::_draw; +} + +############################################################################# + +package CFClient::UI::CheckBox; + +our @ISA = CFClient::UI::DrawBG::; + +use SDL; +use SDL::OpenGL; + +sub new { + my $class = shift; + + $class->SUPER::new ( + padding => 2, + fg => [1, 1, 1], + active_fg => [1, 1, 0], + state => 0, + @_ + ) +} + +sub size_request { + my ($self) = @_; + + ($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) = @_; + + if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} + && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { + $self->{state} = !$self->{state}; + $self->emit (changed => $self->{state}); + } +} + +sub _draw { + my ($self) = @_; + + $self->SUPER::_draw; + + glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; + + my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; + + glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; + + glBegin GL_LINE_LOOP; + glVertex 0 , 0; + glVertex 0 , $s; + glVertex $s, $s; + glVertex $s, 0; + glEnd; + + if ($self->{state}) { + glBegin GL_LINES; + glVertex 0 , 0; + glVertex $s, $s; + glVertex $s, 0; + glVertex 0 , $s; + glEnd; + } +} + +############################################################################# + package CFClient::UI::Slider; use strict; @@ -1243,6 +1380,7 @@ $w -= $page; $page &= ~1; glTranslate $page * 0.5, 0, 0; + $page ||= 2; glColor @$fg; glBegin GL_LINES; @@ -1347,6 +1485,7 @@ glEnable GL_BLEND; glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; + glColor 1, 1, 1, 1; my $sw4 = ($sw + 3) & ~3; my $darkness = "\x00" x ($sw4 * $sh); @@ -1387,6 +1526,19 @@ glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; + if ($::CFG->{fow_smooth}) { # smooth fog of war + my @conv = ( + 0.05, 0.05, 0.05, + 0.05, 0.60, 0.05, + 0.05, 0.05, 0.05, + ); + CFClient::glConvolutionParameteri GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_REPLICATE_BORDER; + CFClient::glConvolutionFilter2D GL_CONVOLUTION_2D, + GL_ALPHA, 3, 3, GL_ALPHA, GL_FLOAT, + pack "f*", @conv; + glEnable GL_CONVOLUTION_2D; + } + $darkness = new CFClient::Texture w => $sw4, h => $sh, @@ -1394,9 +1546,11 @@ internalformat => GL_ALPHA, format => GL_ALPHA; - glColor 0.45, 0.45, 0.45, 1; + glColor +($::CFG->{fow_intensity}) x 3, 1; $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); + glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; + glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1404,6 +1558,18 @@ } glCallList $self->{list}; + + if ($FOCUS != $self) { + glEnable GL_BLEND; + glColor 0, 0, 1, 0.4; + glBegin GL_QUADS; + glVertex 0, 0; + glVertex 0, $::HEIGHT; + glVertex $::WIDTH, $::HEIGHT; + glVertex $::WIDTH, 0; + glEnd; + glDisable GL_BLEND; + } } my %DIR = ( @@ -1429,16 +1595,18 @@ my $sym = $ev->key_sym; if ($sym == SDLK_KP5) { - $::CONN->send ("command stay fire"); + $::CONN->user_send ("command stay fire"); + } elsif ($sym == SDLK_a) { + $::CONN->user_send ("command apply"); } elsif (exists $DIR{$sym}) { if ($mod & KMOD_SHIFT) { $self->{shft}++; - $::CONN->send ("command fire $DIR{$sym}[0]"); + $::CONN->user_send ("command fire $DIR{$sym}[0]"); } elsif ($mod & KMOD_CTRL) { $self->{ctrl}++; - $::CONN->send ("command run $DIR{$sym}[0]"); + $::CONN->user_send ("command run $DIR{$sym}[0]"); } else { - $::CONN->send ("command $DIR{$sym}[1]"); + $::CONN->user_send ("command $DIR{$sym}[1]"); } } } @@ -1450,10 +1618,10 @@ my $sym = $ev->key_sym; if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { - $::CONN->send ("command fire_stop"); + $::CONN->user_send ("command fire_stop"); } if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { - $::CONN->send ("command run_stop"); + $::CONN->user_send ("command run_stop"); } }