--- deliantra/Deliantra-Client/DC/UI.pm 2006/05/15 18:23:33 1.207 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/05/20 15:37:27 1.218 @@ -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; + } } ############################################################################# @@ -673,13 +693,6 @@ glClear GL_COLOR_BUFFER_BIT; $self->_render; -# glColorMask 1, 1, 1, 0; -# glEnable GL_BLEND; -# glBlendFunc GL_SRC_ALPHA, GL_ZERO; -# glRasterPos 0, 0; -# glCopyPixels 0, 0, $self->{w}, $self->{h}; -# glDisable GL_BLEND; -# glColorMask 1, 1, 1, 1; }; } @@ -693,7 +706,7 @@ glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - glColor 0, 0, 0, 1; + glColor 1, 1, 1, 1; $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); @@ -786,7 +799,7 @@ ; $self = $class->SUPER::new ( - vp => (new CFClient::UI::ViewPort), + vp => (new CFClient::UI::ViewPort expand => 1), slider => $slider, @_, ); @@ -897,7 +910,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 +1281,7 @@ package CFClient::UI::Label; -our @ISA = CFClient::UI::Base::; +our @ISA = CFClient::UI::DrawBG::; use CFClient::OpenGL; @@ -1277,9 +1290,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 +1321,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 +1412,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 +1495,8 @@ $self->{cursor} = length $text; $self->_set_text ($text); - $self->check_size; $self->update; + $self->check_size; } sub get_text { @@ -1523,6 +1546,7 @@ $self->_set_text ($text); $self->update; + $self->check_size; } sub focus_in { @@ -1661,7 +1685,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 +1742,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,12 +2045,14 @@ 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, vertical => 0, can_hover => 1, - inner_pad => .05, + inner_pad => 0.02, @_ ); @@ -2125,8 +2152,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}; @@ -2252,7 +2279,7 @@ } $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { - glClearColor 0, 0, 0, 0; + glClearColor 0.5, 0.5, 0.5, 0; glClear GL_COLOR_BUFFER_BIT; glEnable GL_TEXTURE_2D; @@ -2294,7 +2321,7 @@ glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; glColor 1, 1, 1, 1; - $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); + $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); glDisable GL_TEXTURE_2D; $self->{children}[1]->draw; @@ -2403,11 +2430,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), ); } @@ -2463,10 +2491,26 @@ sub new { my $class = shift; - $class->SUPER::new ( + my $self = $class->SUPER::new ( aspect => 1, @_, - ) + ); + + if ($self->{anim} && $self->{animspeed}) { + Scalar::Util::weaken (my $widget = $self); + + $self->{timer} = Event->timer ( + at => $self->{animspeed} * int $::NOW / $self->{animspeed}, + hard => 1, + interval => $self->{animspeed}, + cb => sub { + ++$widget->{frame}; + $widget->update; + }, + ); + } + + $self } sub size_request { @@ -2477,9 +2521,18 @@ my ($self) = @_; return unless $::CONN;#d# manage and cache textures differently - my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; - # TODO animation + my $face; + + if ($self->{frame}) { + my $anim = $::CONN->{anim}[$self->{anim}]; + + $face = $anim->[ $self->{frame} % @$anim ] + if $anim && @$anim; + } + + my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; + if ($tex) { glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; @@ -2489,22 +2542,28 @@ } } +sub DESTROY { + my ($self) = @_; + + $self->{timer}->cancel + if $self->{timer}; + + $self->SUPER::DESTROY; +} + ############################################################################# package CFClient::UI::InventoryItem; our @ISA = CFClient::UI::HBox::; -sub new { - my $class = shift; +sub _item_to_desc { + my ($item) = @_; - my %args = @_; - - my $item = delete $args{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,29 +2580,52 @@ $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_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) { my @menu_items = ( ["examine", sub { $::CONN->send ("examine $item->{tag}") }], - ["mark", sub { $::CONN->send ("mark $item->{tag}") }], + ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }], ["apply", sub { $::CONN->send ("apply $item->{tag}") }], - ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], - [ - $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", - sub { $::CONN->send ("lock ". () ."$item->{tag}") }, - ], + ( + $item->{flags} & Crossfire::Protocol::F_LOCKED + ? ( + ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }], + ) + : ( + ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }], + ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], + ) + ), ); CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); @@ -2554,6 +2636,7 @@ %args ); + $self->add (new CFClient::UI::Face can_events => 0, face => $item->{face}, @@ -2561,14 +2644,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 +2692,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 +2784,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 +2821,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 +2828,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 +2866,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 +2876,16 @@ $self->reorder; } +sub reconfigure { + my ($self) = @_; + + delete $_->{label} + for values %{ $self->{item} || {} }; + + $self->reorder; + $self->SUPER::reconfigure; +} + ############################################################################# package CFClient::UI::Root; @@ -2962,7 +3068,7 @@ package CFClient::UI; $ROOT = new CFClient::UI::Root; -$TOOLTIP = new CFClient::UI::Tooltip; +$TOOLTIP = new CFClient::UI::Tooltip z => 900; 1