--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/22 12:14:45 1.148 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/23 02:22:54 1.155 @@ -10,8 +10,35 @@ our ($FOCUS, $HOVER, $GRAB); # various widgets our $ROOT; +our $TOOLTIP; our $BUTTON_STATE; +sub check_tooltip { + if (!$GRAB) { + for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { + if (exists $widget->{tooltip}) { + + if ($TOOLTIP->{owner} != $widget) { + $TOOLTIP->{owner} = $widget; + + my $tip = $widget->{tooltip}; + + $tip = $tip->($widget) if CODE:: eq ref $tip; + + $TOOLTIP->set_text ($widget->{tooltip}); + $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); + $TOOLTIP->show; + } + + return; + } + } + } + + $TOOLTIP->hide; + delete $TOOLTIP->{owner}; +} + # class methods for events sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS; @@ -30,6 +57,8 @@ $GRAB = $widget; $GRAB->update if $GRAB; + + check_tooltip; } $BUTTON_STATE |= 1 << ($ev->{button} - 1); @@ -51,6 +80,8 @@ my $grab = $GRAB; undef $GRAB; $grab->update if $grab; $GRAB->update if $GRAB; + + check_tooltip; } } @@ -65,6 +96,8 @@ $hover->update if $hover && $hover->{can_hover}; $HOVER->update if $HOVER && $HOVER->{can_hover}; + + check_tooltip; } $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; @@ -98,6 +131,7 @@ x => 0, y => 0, z => 0, + can_events => 1, @_ }, $class; @@ -280,6 +314,8 @@ sub find_widget { my ($self, $x, $y) = @_; + return () unless $self->{can_events}; + return $self if $x >= $self->{x} && $x < $self->{x} + $self->{w} && $y >= $self->{y} && $y < $self->{y} + $self->{h}; @@ -296,14 +332,15 @@ sub check_size { my ($self) = @_; + return unless $self->{parent}; + 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}; + $self->{parent}->check_size; } } @@ -380,6 +417,11 @@ our @ISA = CFClient::UI::Base::; +sub new { + my ($class, %arg) = @_; + $class->SUPER::new (can_events => 0, %arg); +} + sub size_request { (0, 0) } @@ -397,7 +439,7 @@ my $children = delete $arg{children} || []; - my $self = $class->SUPER::new (children => [], %arg); + my $self = $class->SUPER::new (children => [], can_events => 0, %arg); $self->add ($_) for @$children; $self @@ -426,6 +468,7 @@ $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; $self->check_size; + $self->update; } sub find_widget { @@ -563,7 +606,7 @@ sub size_request { my ($self) = @_; - @$self{qw(child_w child_h)} = $self->child->size_request; + @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); @$self{qw(child_w child_h)} @@ -661,6 +704,7 @@ bg => [1, 1, 1, 1], border_bg => [1, 1, 1, 1], border => 0.8, + can_events => 1, @_ ); @@ -839,7 +883,7 @@ for my $x (0 .. $#$row) { my $widget = $row->[$x] or next; - my ($w, $h) = $widget->size_request; + my ($w, $h) = @$widget{qw(req_w req_h)}; $w[$x] = max $w[$x], $w; $h[$y] = max $h[$y], $h; @@ -954,7 +998,7 @@ my $children = $self->{children}; - my @h = map +($_->size_request)[0], @$children; + my @h = map $_->{req_w}, @$children; my $req_h = List::Util::sum @h; @@ -1012,7 +1056,7 @@ my $children = $self->{children}; - my @h = map +($_->size_request)[1], @$children; + my @h = map $_->{req_h}, @$children; my $req_h = List::Util::sum @h; @@ -1063,6 +1107,7 @@ valign => -1, padding => 2, layout => new CFClient::Layout, + can_events => 0, %arg ); @@ -1094,6 +1139,7 @@ $self->{layout}->set_text ($text); delete $self->{texture}; + $self->check_size; $self->update; } @@ -1103,6 +1149,7 @@ $self->{layout}->set_markup ($markup); delete $self->{texture}; + $self->check_size; $self->update; } @@ -1139,7 +1186,9 @@ my ($self, $fontsize) = @_; $self->{fontsize} = $fontsize; + delete $self->{texture}; $self->check_size; + $self->update; } sub _draw { @@ -1195,6 +1244,7 @@ can_hover => 1, can_focus => 1, valign => 0, + can_events => 1, @_ ) } @@ -1232,7 +1282,7 @@ sub size_allocate { my ($self, $w, $h) = @_; - $self->_set_text ($self->{text}); + $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text } sub set_text { @@ -1389,6 +1439,7 @@ can_hover => 1, align => 0, valign => 0, + can_events => 1, @_ ) } @@ -1503,7 +1554,7 @@ sub new { my $class = shift; - my $self = $class->SUPER::new (@_); + my $self = $class->SUPER::new (can_events => 0, @_); $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; @@ -1672,15 +1723,16 @@ our @ISA = CFClient::UI::VBox::; sub new { - my ($class, %arg) = shift; + my ($class, %arg) = @_; my $self = $class->SUPER::new ( - @_, + tooltip => $arg{type}, + %arg, ); - $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); + $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); - $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); + $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); $self } @@ -2056,6 +2108,7 @@ my $self = $class->SUPER::new ( state => 0, connect_activate => \&toggle_flopper, + can_events => 1, @_ ); @@ -2085,6 +2138,72 @@ ############################################################################# +package CFClient::UI::Tooltip; + +our @ISA = CFClient::UI::Bin::; + +use CFClient::OpenGL; + +sub new { + my $class = shift; + + $class->SUPER::new ( + @_, + can_events => 0, + ) +} + +sub set_text { + my ($self, $text) = @_; + + $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; + $self->{label}->set_text ($text); + $self->add ($self->{label}); +} + +sub size_request { + my ($self) = @_; + + my ($w, $h) = @{$self->child}{qw(req_w req_h)}; + + $w = List::Util::min $::WIDTH * 0.2, $w; + $h = List::Util::max $::HEIGHT * 0.2, $h; + + ($w + 4, $h + 4) +} + +sub _draw { + my ($self) = @_; + + glPushMatrix; + glTranslate 0.375, 0.375; + + my ($w, $h) = @$self{qw(w h)}; + + glColor 1, 0.8, 0.4; + glBegin GL_QUADS; + glVertex 0 , 0; + glVertex 0 , $h; + glVertex $w, $h; + glVertex $w, 0; + glEnd; + + glColor 0, 0, 0; + glBegin GL_LINE_LOOP; + glVertex 0 , 0; + glVertex 0 , $h; + glVertex $w, $h; + glVertex $w, 0; + glEnd; + + glPopMatrix; + + glTranslate 2, 2; + $self->SUPER::_draw; +} + +############################################################################# + package CFClient::UI::Root; our @ISA = CFClient::UI::Container::; @@ -2094,7 +2213,7 @@ sub check_size { my ($self) = @_; - $self->configure (0, 0, $::WITH, $::HEIGHT); + $self->configure (0, 0, $::WIDTH, $::HEIGHT); } sub size_request { @@ -2106,8 +2225,13 @@ $self->SUPER::configure ($x, $y, $w, $h); - $_->configure ($_->{x}, $_->{y}, $_->size_request) - for @{$self->{children}}; + for my $child (@{$self->{children}}) { + my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; + + $X = List::Util::max 0, List::Util::min $w - $W, $X; + $Y = List::Util::max 0, List::Util::min $h - $H, $Y; + $child->configure ($X, $Y, $W,$H); + } } sub _topleft { @@ -2164,6 +2288,7 @@ package CFClient::UI; $ROOT = new CFClient::UI::Root; +$TOOLTIP = new CFClient::UI::Tooltip; 1