--- deliantra/Deliantra-Client/DC/UI.pm 2006/05/15 18:23:33 1.207 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/05/17 22:59:13 1.214 @@ -142,7 +142,10 @@ sub rescale_widgets { my ($sx, $sy) = @_; - for my $widget (values %WIDGET) { + # make a copy, otherwise for complains about freed values. + my @widgets = values %WIDGET; + + for my $widget (@widgets) { if ($widget->{toplevel}) { $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; @@ -284,6 +287,17 @@ delete $self->{max_h}; $self->{max_h} = $h if $h; } +sub set_tooltip { + my ($self, $tooltip) = @_; + + $self->{tooltip} = $tooltip; + + if ($CFClient::UI::TOOLTIP->{owner} == $self) { + delete $CFClient::UI::TOOLTIP->{owner}; + CFClient::UI::check_tooltip; + } +} + # translate global coordinates to local coordinate system sub coord2local { my ($self, $x, $y) = @_; @@ -457,8 +471,8 @@ # range [value, low, high, page] $class->SUPER::new ( - bg => [0, 0, 0, 0.2], - active_bg => [1, 1, 1, 0.5], + #bg => [0, 0, 0, 0.2], + #active_bg => [1, 1, 1, 0.5], @_ ) } @@ -466,20 +480,26 @@ sub _draw { my ($self) = @_; - my ($w, $h) = @$self{qw(w h)}; + my $color = $FOCUS == $self && $self->{active_bg} + ? $self->{active_bg} + : $self->{bg}; - glEnable GL_BLEND; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; + if ($color && (@$color < 4 || $color->[3])) { + my ($w, $h) = @$self{qw(w h)}; - glBegin GL_QUADS; - glVertex 0 , 0; - glVertex 0 , $h; - glVertex $w, $h; - glVertex $w, 0; - glEnd; + glEnable GL_BLEND; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + glColor @$color; - glDisable GL_BLEND; + glBegin GL_QUADS; + glVertex 0 , 0; + glVertex 0 , $h; + glVertex $w, $h; + glVertex $w, 0; + glEnd; + + glDisable GL_BLEND; + } } ############################################################################# @@ -897,7 +917,7 @@ $h -= List::Util::max 0, $self->border * 2; $w -= List::Util::max 0, $self->border * 2; - $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) + $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) if $self->{title}; $self->child->configure ($self->border, $self->border, $w, $h); @@ -1268,7 +1288,7 @@ package CFClient::UI::Label; -our @ISA = CFClient::UI::Base::; +our @ISA = CFClient::UI::DrawBG::; use CFClient::OpenGL; @@ -1277,9 +1297,13 @@ my $self = $class->SUPER::new ( fg => [1, 1, 1], + #bg => none + #active_bg => none #font => default_font #text => initial text #markup => initial narkup + #max_w => maximum pixel width + ellipsise => 3, # end layout => (new CFClient::Layout), fontsize => 1, align => -1, @@ -1304,14 +1328,14 @@ $self } -sub escape { - local $_ = $_[1]; +sub escape($) { + local $_ = $_[0]; s/&/&/g; s/>/>/g; s/{layout}->set_font ($self->{font}) if $self->{font}; $self->{layout}->set_width ($self->{max_w} || -1); + $self->{layout}->set_ellipsise ($self->{ellipsise}); + $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); my ($w, $h) = $self->{layout}->size; @@ -1393,21 +1419,25 @@ sub _draw { my ($self) = @_; + $self->SUPER::_draw; # draw background, if applicable + my $tex = $self->{texture} ||= do { $self->{layout}->set_foreground (@{$self->{fg}}); $self->{layout}->set_font ($self->{font}) if $self->{font}; $self->{layout}->set_width ($self->{w}); - $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); + $self->{layout}->set_ellipsise ($self->{ellipsise}); + $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); + $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); my $tex = new_from_layout CFClient::Texture $self->{layout}; - $self->{ox} = int $self->{align} < 0 ? $self->{padding} - : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} - : ($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; + $self->{ox} = int ($self->{align} < 0 ? $self->{padding} + : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} + : ($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 }; @@ -1472,8 +1502,8 @@ $self->{cursor} = length $text; $self->_set_text ($text); - $self->check_size; $self->update; + $self->check_size; } sub get_text { @@ -1523,6 +1553,7 @@ $self->_set_text ($text); $self->update; + $self->check_size; } sub focus_in { @@ -1661,7 +1692,6 @@ $class->SUPER::new ( padding => 4, fg => [1, 1, 1], - bg => [1, 1, 1, 0.2], active_fg => [0, 0, 1], can_hover => 1, align => 0, @@ -1719,6 +1749,8 @@ padding => 2, fg => [1, 1, 1], active_fg => [1, 1, 0], + bg => [0, 0, 0, 0.2], + active_bg => [1, 1, 1, 0.5], state => 0, can_hover => 1, @_ @@ -2020,6 +2052,8 @@ my $self = $class->SUPER::new ( fg => [1, 1, 1], active_fg => [0, 0, 0], + bg => [0, 0, 0, 0.2], + active_bg => [1, 1, 1, 0.5], range => [0, 0, 100, 10], req_w => $::WIDTH / 80, req_h => $::WIDTH / 80, @@ -2125,8 +2159,8 @@ if ($self->{vertical}) { # draw a vertical slider like a rotated horizontal slider + glTranslate 1, 0, 0; glRotate 90, 0, 0, 1; - glTranslate 0, 1, 0; } my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; @@ -2403,11 +2437,12 @@ my ($self, $widget) = @_; $self->add (new CFClient::UI::Label - markup => $widget->{tooltip}, - max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, - fontsize => 0.8, - fg => [0, 0, 0, 1], - font => ($widget->{tooltip_font} || $::FONT_PROP), + markup => $widget->{tooltip}, + max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, + fontsize => 0.8, + fg => [0, 0, 0, 1], + ellipsise => 0, + font => ($widget->{tooltip_font} || $::FONT_PROP), ); } @@ -2495,16 +2530,13 @@ our @ISA = CFClient::UI::HBox::; -sub new { - my $class = shift; - - my %args = @_; - - my $item = delete $args{item}; +sub _item_to_desc { + my ($item) = @_; - my $desc = $item->{nrof} < 2 - ? $item->{name} - : "$item->{nrof} $item->{name_pl}"; + my $desc = + $item->{nrof} < 2 + ? $item->{name} + : "$item->{nrof} $item->{name_pl}"; $item->{flags} & Crossfire::Protocol::F_OPEN and $desc .= " (open)"; @@ -2521,17 +2553,35 @@ $item->{flags} & Crossfire::Protocol::F_LOCKED and $desc .= " *"; + $desc +} + +sub new { + my $class = shift; + + my %args = @_; + + my $item = delete $args{item}; + + my $desc = _item_to_desc ($item); + my $self = $class->SUPER::new ( can_hover => 1, can_events => 1, - tooltip => (CFClient::UI::Label->escape ($desc) + tooltip => ((CFClient::UI::Label::escape $desc) . "\nleftclick - pick up\nmiddle click - apply\nrightclick - menu"), connect_button_down => sub { my ($self, $ev, $x, $y) = @_; # todo: maybe put examine on 1? but should just be a tooltip :( if ($ev->{button} == 1) { - $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); + my $targ = $::CONN->{player}{tag}; + + if ($item->{container} == $::CONN->{player}{tag}) { + $targ = $main::OPENCONT; + } + + $::CONN->send ("move $targ $item->{tag} 0"); } elsif ($ev->{button} == 2) { $::CONN->send ("apply $item->{tag}"); } elsif ($ev->{button} == 3) { @@ -2542,7 +2592,7 @@ ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], [ $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", - sub { $::CONN->send ("lock ". () ."$item->{tag}") }, + sub { $::CONN->send ("lock $item->{tag}") }, ], ); @@ -2554,6 +2604,7 @@ %args ); + $self->add (new CFClient::UI::Face can_events => 0, face => $item->{face}, @@ -2561,14 +2612,23 @@ animspeed => $item->{animspeed}, ); - $self->add (new CFClient::UI::Label - can_events => 0, - text => $desc, - ); + $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0); + + $self->{item} = $item; + + $self->update_item; $self } +sub update_item { + my ($self) = @_; + + my $desc = _item_to_desc ($self->{item}); + + $self->{name_lbl}->set_text ($desc); +} + ############################################################################# package CFClient::UI::Inventory; @@ -2600,11 +2660,8 @@ $self->{real_items} = \@items; for my $item (@items) { - my $desc = $item->{nrof} < 2 - ? $item->{name} - : "$item->{nrof} $item->{name_pl}"; - $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; + $item->update_item (); } $self->{scrolled}->add (@items); @@ -2695,6 +2752,15 @@ our @ISA = CFClient::UI::VBox::; +sub new { + my $class = shift; + + $class->SUPER::new ( + fontsize => 0.8, + @_, + ) +} + sub reorder { my ($self) = @_; my $NOW = time; @@ -2723,8 +2789,6 @@ for ($short) { s/^\s+//; s/\s+/ /g; - my $len = int 40 / $item->{fontsize}; - substr $_, $len, length, "…" if $len < length; } new CFClient::UI::Label @@ -2732,7 +2796,8 @@ tooltip => $item->{tooltip}, tooltip_font => $::FONT_PROP, tooltip_width => 0.67, - fontsize => $item->{fontsize}, + fontsize => $item->{fontsize} || $self->{fontsize}, + max_w => $::WIDTH * 0.44, fg => $item->{fg}, can_events => 1, can_hover => 1 @@ -2769,7 +2834,6 @@ text => $text, timeout => $timeout, tooltip => $text, - fontsize => 0.8, fg => [0.8, 0.8, 0.8, 0.8], pri => 0, count => 1, @@ -2780,6 +2844,16 @@ $self->reorder; } +sub reconfigure { + my ($self) = @_; + + delete $_->{label} + for values %{ $self->{item} || {} }; + + $self->reorder; + $self->SUPER::reconfigure; +} + ############################################################################# package CFClient::UI::Root; @@ -2962,7 +3036,7 @@ package CFClient::UI; $ROOT = new CFClient::UI::Root; -$TOOLTIP = new CFClient::UI::Tooltip; +$TOOLTIP = new CFClient::UI::Tooltip z => 900; 1