--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/14 20:27:35 1.107 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/19 21:38:05 1.137 @@ -9,7 +9,7 @@ our ($FOCUS, $HOVER, $GRAB); # various widgets -our $TOPLEVEL; +our $ROOT; our $BUTTON_STATE; # class methods for events @@ -23,29 +23,29 @@ sub feed_sdl_button_down_event { my ($ev) = @_; - my ($x, $y) = ($ev->motion_x, $ev->motion_y); + my ($x, $y) = ($ev->{x}, $ev->{y}); if (!$BUTTON_STATE) { - my $widget = $TOPLEVEL->find_widget ($x, $y); + my $widget = $ROOT->find_widget ($x, $y); $GRAB = $widget; $GRAB->update if $GRAB; } - $BUTTON_STATE |= 1 << ($ev->button - 1); + $BUTTON_STATE |= 1 << ($ev->{button} - 1); - $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; + $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; } sub feed_sdl_button_up_event { my ($ev) = @_; - my ($x, $y) = ($ev->motion_x, $ev->motion_y); + my ($x, $y) = ($ev->{x}, $ev->{y}); - my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); + my $widget = $GRAB || $ROOT->find_widget ($x, $y); - $BUTTON_STATE &= ~(1 << ($ev->button - 1)); + $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); - $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; + $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; if (!$BUTTON_STATE) { my $grab = $GRAB; undef $GRAB; @@ -56,9 +56,9 @@ sub feed_sdl_motion_event { my ($ev) = @_; - my ($x, $y) = ($ev->motion_x, $ev->motion_y); + my ($x, $y) = ($ev->{x}, $ev->{y}); - my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); + my $widget = $GRAB || $ROOT->find_widget ($x, $y); if ($widget != $HOVER) { my $hover = $HOVER; $HOVER = $widget; @@ -67,7 +67,20 @@ $HOVER->update if $HOVER && $HOVER->{can_hover}; } - $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; + $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; +} + +# convert position array to integers +sub harmonize { + my ($vals) = @_; + + my $rem = 0; + + for (@$vals) { + my $i = int $_ + $rem; + $rem += $_ - $i; + $_ = $i; + } } ############################################################################# @@ -85,8 +98,6 @@ x => 0, y => 0, z => 0, - w => -1, - h => -1, @_ }, $class; @@ -99,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} = $x; - $self->{y} = $y; + + $self->{x} = int $x; + $self->{y} = int $y; $self->{z} = $z if defined $z; + + $self->update; } sub needs_redraw { @@ -115,7 +145,7 @@ Carp::confess "size_request is abtract"; } -sub _size_allocate { +sub configure { my ($self, $x, $y, $w, $h) = @_; $self->{x} = $x; @@ -126,20 +156,35 @@ $self->{w} = $w; $self->{h} = $h; - 1 + $self->size_allocate ($w, $h); + $self->update; } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; + # nothing to be done +} + +# return top left coordinates +sub _topleft { + my ($self, $x, $y) = @_; - $self->_size_allocate ($x, $y, $w, $h); + $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); } # translate global coordinates to local coordinate system -sub translate { +sub coord2local { + my ($self, $x, $y) = @_; + + my ($X, $Y) = $self->_topleft; + ($x - $X, $y - $Y) +} + +# translate local coordinates to global coordinate system +sub coord2global { my ($self, $x, $y) = @_; - $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); + my ($X, $Y) = $self->_topleft; + ($x + $X, $y + $Y) } sub focus_in { @@ -149,6 +194,9 @@ return unless $self->{can_focus}; my $focus = $FOCUS; $FOCUS = $self; + + $self->emit (focus_in => $focus); + $focus->update if $focus; $FOCUS->update; } @@ -159,6 +207,9 @@ return unless $FOCUS == $self; my $focus = $FOCUS; undef $FOCUS; + + $self->emit (focus_out => $focus); + $focus->update if $focus; #? } @@ -192,7 +243,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; @@ -222,14 +273,23 @@ } sub set_parent { - my ($self, $par) = @_; + my ($self, $parent) = @_; - $self->{parent} = $par; - Scalar::Util::weaken $self->{parent}; + Scalar::Util::weaken ($self->{parent} = $parent); } -sub get_parent { - $_[0]->{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 { @@ -242,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 { @@ -328,25 +389,28 @@ } sub add { - my ($self, $chld) = @_; + my ($self, $child) = @_; + + $child->set_parent ($self); - $chld->set_parent ($self); + use sort 'stable'; $self->{children} = [ sort { $a->{z} <=> $b->{z} } - @{$self->{children}}, $chld + @{$self->{children}}, $child ]; - $self->{w} = $self->{h} = -1; - $self->update; + $child->check_size; } sub remove { - my ($self, $widget) = @_; + my ($self, $child) = @_; - $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; + delete $child->{parent}; - $self->size_allocate (0, 0, $self->{w}, $self->{h}); + $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; + + $self->check_size; } sub find_widget { @@ -386,11 +450,11 @@ } sub add { - my ($self, $widget) = @_; + my ($self, $child) = @_; $self->{children} = []; - $self->SUPER::add ($widget); + $self->SUPER::add ($child); } sub remove { @@ -409,11 +473,9 @@ } sub size_allocate { - my ($self, $x, $y, $w, $h) = @_; - - $self->_size_allocate ($x, $y, $w, $h) or return; + my ($self, $w, $h) = @_; - $self->{children}[0]->size_allocate (0, 0, $w, $h); + $self->{children}[0]->configure (0, 0, $w, $h); } ############################################################################# @@ -449,11 +511,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; } @@ -479,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::; @@ -498,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 { @@ -541,11 +623,15 @@ $class->SUPER::new ( bg => [1, 1, 1, 1], border_bg => [1, 1, 1, 1], - border => $::FONTSIZE * 0.8, + border => 0.8, @_ ) } +sub border { + int $_[0]{border} * $::FONTSIZE +} + sub size_request { my ($self) = @_; @@ -554,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}); @@ -586,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}); @@ -629,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]; @@ -646,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; @@ -674,12 +762,19 @@ } sub add { - my ($self, $x, $y, $chld) = @_; + my ($self, $x, $y, $child) = @_; - $self->{children}[$y][$x] = $chld; - $chld->set_parent ($self); + $child->set_parent ($self); + $self->{children}[$y][$x] = $child; + + $child->check_size; +} - $self->{w} = $self->{h} = -1; +# TODO: move to container class maybe? send childs a signal on removal? +sub clear { + my ($self) = @_; + + delete $self->{children}; $self->update; } @@ -717,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; @@ -735,8 +828,8 @@ $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; - $_ = int $_ for @$ws; #TODO: avoid rounding problems - $_ = int $_ for @$hs; #TODO: avoid rounding problems + CFClient::UI::harmonize $ws; + CFClient::UI::harmonize $hs; my $y; @@ -751,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; @@ -806,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); @@ -834,14 +925,18 @@ } } + CFClient::UI::harmonize \@h; + my $y = 0; for (0 .. $#$children) { my $child = $children->[$_]; - my $h = int $h[$_]; - $child->size_allocate ($y, 0, $h, $w); + my $h = $h[$_]; + $child->configure ($y, 0, $h, $w); $y += $h; } + + 1 } ############################################################################# @@ -864,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}; @@ -888,14 +981,18 @@ } } + CFClient::UI::harmonize \@h; + my $y = 0; for (0 .. $#$children) { my $child = $children->[$_]; - my $h = int $h[$_]; - $child->size_allocate (0, $y, $w, $h); + my $h = $h[$_]; + $child->configure (0, $y, $w, $h); $y += $h; } + + 1 } ############################################################################# @@ -911,15 +1008,19 @@ my $self = $class->SUPER::new ( fg => [1, 1, 1], - fontsize => $::FONTSIZE, + fontsize => 1, 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 } @@ -937,25 +1038,27 @@ 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 { 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; ( @@ -965,26 +1068,17 @@ } 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}; } -sub update { - my ($self) = @_; - - delete $self->{texture}; - $self->SUPER::update; -} - sub _draw { my ($self) = @_; 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} * $::FONTSIZE); new_from_layout CFClient::Texture $self->{layout} }; @@ -995,12 +1089,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 + ); + + $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 (int $x, int +($self->{h} - $tex->{h}) * 0.5); + $tex->draw_quad ($self->{ox}, $self->{oy}); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -1008,11 +1109,10 @@ ############################################################################# -package CFClient::UI::Entry; +package CFClient::UI::EntryBase; our @ISA = CFClient::UI::Label::; -use SDL; use SDL::OpenGL; sub new { @@ -1025,6 +1125,7 @@ active_fg => [0, 0, 0], can_hover => 1, can_focus => 1, + valign => 0, @_ ) } @@ -1032,25 +1133,23 @@ 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; + delete $self->{texture}; + + $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 { @@ -1062,9 +1161,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}); } @@ -1080,26 +1177,25 @@ sub key_down { my ($self, $ev) = @_; - my $mod = $ev->key_mod; - my $sym = $ev->key_sym; - - my $uni = $ev->key_unicode; + my $mod = $ev->{mod}; + my $sym = $ev->{sym}; + my $uni = $ev->{unicode}; my $text = $self->get_text; - if ($sym == SDLK_BACKSPACE) { + if ($sym == 8) { substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; - } elsif ($sym == SDLK_DELETE) { + } elsif ($sym == 127) { substr $text, $self->{cursor}, 1, ""; - } elsif ($sym == SDLK_LEFT) { + } elsif ($sym == CFClient::SDLK_LEFT) { --$self->{cursor} if $self->{cursor}; - } elsif ($sym == SDLK_RIGHT) { + } elsif ($sym == CFClient::SDLK_RIGHT) { ++$self->{cursor} if $self->{cursor} < length $self->{text}; - } elsif ($sym == SDLK_HOME) { + } elsif ($sym == CFClient::SDLK_HOME) { $self->{cursor} = 0; - } elsif ($sym == SDLK_END) { + } elsif ($sym == CFClient::SDLK_END) { $self->{cursor} = length $text; - } elsif ($sym == SDLK_ESCAPE) { + } elsif ($sym == 27) { $self->emit ('escape'); } elsif ($uni) { substr $text, $self->{cursor}++, 0, chr $uni; @@ -1164,27 +1260,34 @@ #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; sub key_down { my ($self, $ev) = @_; - my $sym = $ev->key_sym; + my $sym = $ev->{sym}; - if ($sym == SDLK_RETURN) { + if ($sym == 13) { $self->emit (activate => $self->get_text); $self->update; @@ -1200,7 +1303,6 @@ our @ISA = CFClient::UI::Label::; -use SDL; use SDL::OpenGL; my @tex = @@ -1214,8 +1316,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, @_ ) } @@ -1233,20 +1337,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; @@ -1264,7 +1366,6 @@ map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } qw(c1_checkbox_bg.png c1_checkbox_active.png); -use SDL; use SDL::OpenGL; sub new { @@ -1286,12 +1387,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) = @_; @@ -1327,6 +1422,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; @@ -1351,8 +1553,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, @@ -1472,9 +1674,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), @@ -1505,7 +1705,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); @@ -1519,19 +1719,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; } @@ -1560,7 +1754,7 @@ delete $self->{texture}; - $TOPLEVEL->on_refresh ($self, sub { + $ROOT->on_refresh ($self, sub { if (delete $self->{need_reflow}) { my $height = 0; @@ -1628,177 +1822,6 @@ ############################################################################# -package CFClient::UI::MapWidget; - -use strict; - -use List::Util qw(min max); - -use SDL; -use SDL::OpenGL; - -our @ISA = CFClient::UI::Base::; - -sub new { - my $class = shift; - - $class->SUPER::new ( - z => -1, - can_focus => 1, - list => (glGenLists 1), - @_ - ) -} - -sub key_down { - print "MAPKEYDOWN\n"; -} - -sub key_up { -} - -sub size_request { - ( - 1 + 32 * int $::WIDTH / 32, - 1 + 32 * int $::HEIGHT / 32, - ) -} - -sub update { - my ($self) = @_; - - $self->{need_update} = 1; - $self->SUPER::update; -} - -sub draw { - my ($self) = @_; - - $self->{need_update}++;#d# - if (delete $self->{need_update}) { - glNewList $self->{list}, GL_COMPILE; - - my $sw = int $::WIDTH / 32; - my $sh = int $::HEIGHT / 32; - - if ($::MAP) { - my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); - - 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; - } - - 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; - - glColor +($::CFG->{fow_intensity}) x 3, 1; - $tex->draw_quad (0, 0, $w * 32, $h * 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 = ( - SDLK_KP8, [1, "north"], - SDLK_KP9, [2, "northeast"], - SDLK_KP6, [3, "east"], - SDLK_KP3, [4, "southeast"], - SDLK_KP2, [5, "south"], - SDLK_KP1, [6, "southwest"], - SDLK_KP4, [7, "west"], - SDLK_KP7, [8, "northwest"], - - SDLK_UP, [1, "north"], - SDLK_RIGHT, [3, "east"], - SDLK_DOWN, [5, "south"], - SDLK_LEFT, [7, "west"], -); - -sub key_down { - my ($self, $ev) = @_; - - my $mod = $ev->key_mod; - my $sym = $ev->key_sym; - - if ($sym == SDLK_KP5) { - $::CONN->user_send ("command stay fire"); - } elsif ($sym == SDLK_a) { - $::CONN->user_send ("command apply"); - } elsif ($sym == SDLK_QUOTE) { - $self->emit ('activate_console'); - } elsif ($sym == SDLK_SLASH) { - $self->emit ('activate_console' => '/'); - } elsif (exists $DIR{$sym}) { - if ($mod & KMOD_SHIFT) { - $self->{shft}++; - $::CONN->user_send ("command fire $DIR{$sym}[0]"); - } elsif ($mod & KMOD_CTRL) { - $self->{ctrl}++; - $::CONN->user_send ("command run $DIR{$sym}[0]"); - } else { - $::CONN->user_send ("command $DIR{$sym}[1]"); - } - } -} - -sub key_up { - my ($self, $ev) = @_; - - my $mod = $ev->key_mod; - my $sym = $ev->key_sym; - - if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { - $::CONN->user_send ("command fire_stop"); - } - if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { - $::CONN->user_send ("command run_stop"); - } -} - -############################################################################# - package CFClient::UI::Animator; use SDL::OpenGL; @@ -1867,38 +1890,45 @@ # 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, - ); + $CFClient::UI::ROOT->add ($self->{other}); + $self->{other}->move ($self->coord2global (0, $self->{h})); + $self->emit ("open"); } else { - $CFClient::UI::TOPLEVEL->remove ($self->{other}); + $CFClient::UI::ROOT->remove ($self->{other}); + $self->emit ("close"); } + + $self->emit (changed => $self->{state}); } ############################################################################# -package CFClient::UI::Toplevel; +package CFClient::UI::Root; our @ISA = CFClient::UI::Container::; 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}}; } -sub translate { +sub _topleft { my ($self, $x, $y) = @_; ($x, $y) @@ -1907,16 +1937,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 ($widget->{x}, $widget->{y}, $widget->size_request); + $self->SUPER::add ($child); } sub on_refresh { @@ -1949,7 +1977,7 @@ package CFClient::UI; -$TOPLEVEL = new CFClient::UI::Toplevel; +$ROOT = new CFClient::UI::Root; 1