--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 20:44:49 1.75 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/14 11:32:22 1.100 @@ -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 { @@ -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) = @_; @@ -138,6 +146,7 @@ my ($self) = @_; return if $FOCUS == $self; + return unless $self->{can_focus}; my $focus = $FOCUS; $FOCUS = $self; $focus->update if $focus; @@ -180,10 +189,10 @@ $self->_draw; glPopMatrix; - if ($self == $HOVER) { + if ($self == $HOVER && $self->{can_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 +211,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 +221,6 @@ () } -sub del_parent { $_[0]->{parent} = undef } - sub set_parent { my ($self, $par) = @_; @@ -277,8 +273,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 +284,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 +328,8 @@ } sub add { - my ($self, $chld, $expand) = @_; + my ($self, $chld) = @_; - $chld->{expand} = $expand; $chld->set_parent ($self); $self->{children} = [ @@ -532,17 +532,30 @@ map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); +sub new { + my $class = shift; + + # TODO: user_x, user_y, overwrite moveto? + + $class->SUPER::new ( + bg => [1, 1, 1, 1], + border_bg => [1, 1, 1, 1], + border => $::FONTSIZE * 0.8, + @_ + ) +} + sub size_request { my ($self) = @_; - my ($w, $h) = $self->SUPER::size_request; + return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; - $h += $tex[1]->{h}; - $h += $tex[4]->{h}; - $w += $tex[2]->{w}; - $w += $tex[3]->{w}; + my ($w, $h) = $self->SUPER::size_request; - ($w, $h) + ( + $w + $self->{border} * 2, + $h + $self->{border} * 2, + ) } sub size_allocate { @@ -550,58 +563,94 @@ $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 -= 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); +} + +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 ($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 < $self->{border}) { + + 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) = @_; - $h = $h < 0 ? 0 : $h; - $w = $w < 0 ? 0 : $w; + delete $self->{motion}; +} - my $child = $self->child; +sub mouse_motion { + my ($self, $ev, $x, $y) = @_; - $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); + $self->{motion}->($ev, $x, $y) if $self->{motion}; } sub _draw { my ($self) = @_; - my ($w, $h) = ($self->{w}, $self->{h}); + my ($w, $h ) = ($self->{w}, $self->{h}); my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 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 $top = $tex[1]; - $top->draw_quad (0, 0, $w, $top->{h}); - - my $left = $tex[3]; - $left->draw_quad (0, $top->{h}, $left->{w}, $ch); - - my $right = $tex[2]; - $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - my $bottom = $tex[4]; - $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); + 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}); 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; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; + # TODO: repeat texture not scale my $rep_x = $cw / $bg->{w}; my $rep_y = $ch / $bg->{h}; - $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); + glColor @{ $self->{bg} }; + + $bg->{s} = $rep_x; + $bg->{t} = $rep_y; + $bg->{wrap_mode} = 1; + $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); - glDisable GL_BLEND; glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; $self->child->draw; - } ############################################################################# @@ -614,6 +663,15 @@ use SDL::OpenGL; +sub new { + my $class = shift; + + $class->SUPER::new ( + col_expand => [], + @_ + ) +} + sub add { my ($self, $x, $y, $chld) = @_; @@ -652,8 +710,8 @@ my ($ws, $hs) = $self->get_wh; ( - (List::Util::sum @$ws), - (List::Util::sum @$hs), + (sum @$ws), + (sum @$hs), ) } @@ -664,12 +722,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; @@ -681,12 +744,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; } @@ -696,6 +758,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 +784,69 @@ ############################################################################# -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); + + ($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) = @_; @@ -726,9 +862,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}; @@ -738,19 +872,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 +906,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 +937,7 @@ $self->{layout}->set_markup ($text); delete $self->{texture}; +# $self->{w} = $self->{h} = -1; $self->update; } @@ -820,17 +952,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 +980,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 +992,12 @@ 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); + glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; + $tex->draw_quad (0, 0); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -888,9 +1017,11 @@ $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], + can_hover => 1, + can_focus => 1, @_ ) } @@ -898,20 +1029,25 @@ sub _set_text { my ($self, $text) = @_; + my $old_text = $self->{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->{height}); $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; @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); + + $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? + if $old_text ne $self->{text}; } sub size_request { @@ -956,6 +1092,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 +1120,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 +1145,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; @@ -1024,37 +1167,203 @@ } } +package CFClient::UI::LineEntry; + +our @ISA = CFClient::UI::Entry::; + +use SDL; +use SDL::OpenGL; + +sub key_down { + my ($self, $ev) = @_; + + my $sym = $ev->key_sym; + + if ($sym == SDLK_RETURN) { + $self->emit (activate => $self->get_text); + $self->update; + + } else { + $self->SUPER::key_down ($ev); + } + +} + ############################################################################# -package CFClient::UI::Slider; +package CFClient::UI::Button; -use strict; +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], + can_hover => 1, + @_ + ) +} + +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, + can_hover => 1, + @_ + ) +} + sub size_request { my ($self) = @_; - my $w = 50; - my $h = 10; + ($self->{padding} * 2 + 6) x 2 +} - $self->{vertical} ? ($h, $w) : ($w, $h) +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; + +use SDL::OpenGL; + +our @ISA = CFClient::UI::DrawBG::; + +my @tex = + map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } + qw(s1_slider.png s1_slider_bg.png); + sub new { my $class = shift; # range [value, low, high, page] - $class->SUPER::new ( + # TODO: 0-width page + # TODO: req_w/h are wrong with vertical + # TODO: calculations are off + 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 => 13, + vertical => 0, + can_hover => 1, @_ - ) + ); + + $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 { @@ -1103,38 +1412,104 @@ my ($value, $lo, $hi, $page) = @{$self->{range}}; - $page = int $page * $w / ($hi - $lo); - $value = int +($value - $lo) * $w / ($hi - $lo); + # the inner_* stuff is for generating a padding for the slider handle, + # so that the handle doesn't leave the texture. This calculation isn't 100% + # correct propably, but it does the job for now + my $inner_pad = 5; # 5% of width for slider bg texture border + my $inner_pad_px = ($w / 100) * $inner_pad; # % to pixels + my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right + + $page = int $page * $inner_w / ($hi - $lo); + $value = int +($value - $lo) * $inner_w / ($hi - $lo); $w -= $page; $page &= ~1; glTranslate $page * 0.5, 0, 0; + $page ||= 2; - glColor @$fg; - glBegin GL_LINES; - glVertex 0, 0; glVertex 0, $h; - glVertex $w - 1, 0; glVertex $w - 1, $h; - glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; - glEnd; + my $knob_a = $inner_pad_px + ($value - $page * 0.5); + my $knob_b = $inner_pad_px + ($value + $page * 0.5); - my $knob_a = $value - $page * 0.5; - my $knob_b = $value + $page * 0.5; + 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; - glBegin GL_QUADS; - glColor @$fg; - glVertex $knob_a, 0; - glVertex $knob_a, $h; - glVertex $knob_b, $h; - glVertex $knob_b, 0; - - if ($knob_a < $knob_b - 2) { - glColor @$bg; - glVertex $knob_a + 1, 1; - glVertex $knob_a + 1, $h - 1; - glVertex $knob_b - 1, $h - 1; - glVertex $knob_b - 1, 1; + # draw background + $tex[1]->draw_quad (0, 0, $w, $h); + + # draw handle + $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); + + glDisable GL_BLEND; + glDisable GL_TEXTURE_2D; +} + +############################################################################# + +package CFClient::UI::TextView; + +our @ISA = CFClient::UI::HBox::; + +use SDL::OpenGL; + +sub new { + my $class = shift; + + my $self = $class->SUPER::new ( + par => [], + @_, + children => [ + (new CFClient::UI::Empty expand => 1), + (new CFClient::UI::Slider vertical => 1), + ], + ); + + $self +} + +sub add_paragraph { + my ($self, $color, $text) = @_; + + push @{$self->{par}}, [$color, $text]; + my $count = scalar @{$self->{par}}; + $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; + $self->{children}[1]->update; +} + +sub _draw { + my ($self) = @_; + + my $par = $self->{par}; + + my $bottom = (scalar @$par) - 1; + + my $w = $self->{children}[0]{w}; + my $y = $self->{h}; + + 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_MODULATE; + + # TODO: everything! + while ($y > 0 && $bottom >= 0) { + my $par = $par->[$bottom--]; + + my $layout = new CFClient::Layout; + + $layout->set_height ($::FONTSIZE); + $layout->set_width ($w); + $layout->set_text ($par->[1]); + my $tex = new_from_layout CFClient::Texture $layout; + $y -= $tex->{h}; + + glColor @{ $par->[0] }; + $tex->draw_quad (0, $y); } - glEnd; + + glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; } ############################################################################# @@ -1154,8 +1529,9 @@ my $class = shift; $class->SUPER::new ( - z => -1, - list => (glGenLists 1), + z => -1, + can_focus => 1, + list => (glGenLists 1), @_ ) } @@ -1169,8 +1545,8 @@ sub size_request { ( - 1 + int $::WIDTH / 32, - 1 + int $::HEIGHT / 32, + 1 + 32 * int $::WIDTH / 32, + 1 + 32 * int $::HEIGHT / 32, ) } @@ -1181,95 +1557,73 @@ $self->SUPER::update; } -sub _draw { +sub draw { my ($self) = @_; + $self->{need_update}++;#d# if (delete $self->{need_update}) { glNewList $self->{list}, GL_COMPILE; - my $mx = $::CONN->{mapx}; - my $my = $::CONN->{mapy}; - - my $map = $::CONN->{map}; - - my ($xofs, $yofs); - - my $sw = 1 + int $::WIDTH / 32; - my $sh = 1 + int $::HEIGHT / 32; - - if ($::CONN->{mapw} > $sw) { - $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; - } else { - $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; - } - - if ($::CONN->{maph} > $sh) { - $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; - } else { - $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; - } + my $sw = int $::WIDTH / 32; + my $sh = int $::HEIGHT / 32; - glEnable GL_TEXTURE_2D; - glEnable GL_BLEND; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; + if ($::MAP) { + my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); - my $sw4 = ($sw + 3) & ~3; - my $darkness = "\x00" x ($sw4 * $sh); - - for my $x (0 .. $sw - 1) { - my $row = $map->[$x + $xofs]; - for my $y (0 .. $sh - 1) { - - my $cell = $row->[$y + $yofs] - or next; - - my $dark = $cell->[0]; - if ($dark < 0) { - substr $darkness, $y * $sw4 + $x, 1, chr 224; - } else { - substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; + if ($::CFG->{fow_enable}) { + if ($::CFG->{fow_smooth}) { # smooth fog of war + glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; + glConvolutionFilter2D + GL_CONVOLUTION_2D, + GL_ALPHA, + 3, 3, + GL_ALPHA, GL_FLOAT, + pack "f*", + 0.1, 0.1, 0.1, + 0.1, 0.2, 0.1, + 0.1, 0.1, 0.1, + ; + glEnable GL_CONVOLUTION_2D; } - for my $num (grep $_, @$cell[1,2,3]) { - my $tex = $::CONN->{face}[$num]{texture} || next; - - my ($w, $h) = @$tex{qw(w h)}; + my $tex = new CFClient::Texture + w => $w, + h => $h, + data => $data, + internalformat => GL_ALPHA, + format => GL_ALPHA; + + glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; + + glEnable GL_BLEND; + glEnable GL_TEXTURE_2D; + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); - } + glColor +($::CFG->{fow_intensity}) x 3, 1; + $tex->draw_quad (0, 0, $w * 32, $h * 32); + + glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; } } -# if (1) { # higher quality darkness -# $lighting =~ s/(.)/$1$1$1/gs; -# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; -# -# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); -# -# $lighting = $pb->get_pixels; -# $lighting =~ s/(.)../$1/gs; -# } - - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - - $darkness = new CFClient::Texture - w => $sw4, - h => $sh, - data => $darkness, - internalformat => GL_ALPHA, - format => GL_ALPHA; - - glColor 0.45, 0.45, 0.45, 1; - $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); - - glDisable GL_TEXTURE_2D; - glDisable GL_BLEND; - glEndList; } glCallList $self->{list}; + + if ($FOCUS != $self) { + glColor 64/255, 64/255, 64/255; + glLogicOp GL_AND; + glEnable GL_COLOR_LOGIC_OP; + glBegin GL_QUADS; + glVertex 0, 0; + glVertex 0, $::HEIGHT; + glVertex $::WIDTH, $::HEIGHT; + glVertex $::WIDTH, 0; + glEnd; + glDisable GL_COLOR_LOGIC_OP; + } } my %DIR = ( @@ -1295,16 +1649,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]"); } } } @@ -1316,10 +1672,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"); } } @@ -1366,6 +1722,44 @@ } ############################################################################# + +package CFClient::UI::Flopper; + +our @ISA = CFClient::UI::Button::; + +sub new { + my $class = shift; + + my $self = $class->SUPER::new ( + state => 0, + connect_activate => \&toggle_flopper, + @_ + ); + + if ($self->{state}) { + $self->{state} = 0; + $self->toggle_flopper; + } + + $self +} + +sub toggle_flopper { + my ($self) = @_; + + # TODO: use animation + if ($self->{state} = !$self->{state}) { + $CFClient::UI::TOPLEVEL->add ($self->{other}); + $self->{other}->move ( + ($::WIDTH - $self->{other}{w}) * 0.5, + ($::HEIGHT - $self->{other}{h}) * 0.5, + ); + } else { + $CFClient::UI::TOPLEVEL->remove ($self->{other}); + } +} + +############################################################################# package CFClient::UI::Toplevel;