--- deliantra/Deliantra-Client/DC/UI.pm 2006/06/18 17:13:12 1.305 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/07/24 04:24:43 1.337 @@ -81,11 +81,24 @@ if $FOCUS; } +sub check_hover { + my ($widget) = @_; + + if ($widget != $HOVER) { + my $hover = $HOVER; $HOVER = $widget; + + $hover->update if $hover && $hover->{can_hover}; + $HOVER->update if $HOVER && $HOVER->{can_hover}; + + $TOOLTIP_WATCHER->start; + } +} + sub feed_sdl_button_down_event { my ($ev) = @_; my ($x, $y) = ($ev->{x}, $ev->{y}); - if (!$BUTTON_STATE) { + unless ($BUTTON_STATE) { my $widget = $ROOT->find_widget ($x, $y); $GRAB = $widget; @@ -96,26 +109,34 @@ $BUTTON_STATE |= 1 << ($ev->{button} - 1); - $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) - if $GRAB; + if ($GRAB) { + if ($ev->{button} == 4 || $ev->{button} == 5) { + # mousewheel + $ev->{dx} = 0; + $ev->{dy} = $ev->{button} * 2 - 9; + $GRAB->emit (mouse_wheel => $ev); + } else { + $GRAB->emit (button_down => $ev) + } + } } sub feed_sdl_button_up_event { my ($ev) = @_; - my ($x, $y) = ($ev->{x}, $ev->{y}); - my $widget = $GRAB || $ROOT->find_widget ($x, $y); + my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y}); $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); - $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) - if $GRAB; + $GRAB->emit (button_up => $ev) + if $GRAB && $ev->{button} != 4 && $ev->{button} != 5; - if (!$BUTTON_STATE) { + unless ($BUTTON_STATE) { my $grab = $GRAB; undef $GRAB; $grab->update if $grab; $GRAB->update if $GRAB; + check_hover $widget; $TOOLTIP_WATCHER->cb->(); } } @@ -126,16 +147,9 @@ my $widget = $GRAB || $ROOT->find_widget ($x, $y); - if ($widget != $HOVER) { - my $hover = $HOVER; $HOVER = $widget; - - $hover->update if $hover && $hover->{can_hover}; - $HOVER->update if $HOVER && $HOVER->{can_hover}; - - $TOOLTIP_WATCHER->start; - } + check_hover $widget; - $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) + $HOVER->emit (mouse_motion => $ev) if $HOVER; } @@ -195,6 +209,14 @@ ############################################################################# +package CFClient::UI::Event; + +sub xy { + $_[1]->coord2local ($_[0]{x}, $_[0]{y}) +} + +############################################################################# + package CFClient::UI::Base; use strict; @@ -337,14 +359,18 @@ Carp::confess "size_request is abstract"; } +sub baseline_shift { + 0 +} + sub configure { my ($self, $x, $y, $w, $h) = @_; if ($self->{aspect}) { my ($ow, $oh) = ($w, $h); - $w = List::Util::min $w, int $h * $self->{aspect}; - $h = List::Util::min $h, int $w / $self->{aspect}; + $w = List::Util::min $w, CFClient::ceil $h * $self->{aspect}; + $h = List::Util::min $h, CFClient::ceil $w / $self->{aspect}; # use alignment to adjust x, y @@ -405,6 +431,8 @@ sub coord2local { my ($self, $x, $y) = @_; + Carp::confess unless $self->{parent};#d# + $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) } @@ -412,6 +440,8 @@ sub coord2global { my ($self, $x, $y) = @_; + Carp::confess unless $self->{parent};#d# + $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) } @@ -421,10 +451,9 @@ return if $FOCUS == $self; return unless $self->{can_focus}; - my $focus = $FOCUS; $FOCUS = $self; + $FOCUS = $self; - $focus->update if $focus; - $FOCUS->update; + $self->update; 0 } @@ -434,9 +463,9 @@ return unless $FOCUS == $self; - my $focus = $FOCUS; undef $FOCUS; + undef $FOCUS; - $focus->update if $focus; #? + $self->update; $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus unless $FOCUS; @@ -447,34 +476,58 @@ sub grab_focus { my ($self) = @_; + $FOCUS->emit ("focus_out") if $FOCUS; $self->emit ("focus_in"); } -sub invoke_mouse_motion { 1 } -sub invoke_button_up { 1 } -sub invoke_key_down { 1 } -sub invoke_key_up { 1 } +sub invoke_mouse_motion { 0 } +sub invoke_button_up { 0 } +sub invoke_key_down { 0 } +sub invoke_key_up { 0 } +sub invoke_mouse_wheel { 0 } sub invoke_button_down { my ($self, $ev, $x, $y) = @_; $self->grab_focus; - 1 + 0 } sub connect { my ($self, $signal, $cb) = @_; push @{ $self->{signal_cb}{$signal} }, $cb; + + defined wantarray and CFClient::guard { + @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, + @{ $self->{signal_cb}{$signal} }; + } } +my %has_coords = ( + button_down => 1, + button_up => 1, + mouse_motion => 1, + mouse_wheel => 1, +); + sub emit { my ($self, $signal, @args) = @_; - (List::Util::sum +(map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}), # before - ($self->can ("invoke_$signal") || sub { 1 })->($self, @args)) # closure - || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent + # I do not really like this solution, but I dislike duplication + # and needlessly verbose code, too. + my @append + = $has_coords{$signal} + ? $args[0]->xy ($self) + : (); + + #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# + + #d##TODO# stop propagating at first true, do not use sum + (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before + || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure + || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent } sub find_widget { @@ -536,8 +589,6 @@ # update screen rectangle local $draw_x = $draw_x + $self->{x}; local $draw_y = $draw_y + $self->{y}; - local $draw_w = $draw_x + $self->{w}; - local $draw_h = $draw_y + $self->{h}; # skip widgets that are entirely outside the drawing area return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) @@ -586,6 +637,8 @@ sub DESTROY { my ($self) = @_; + return if CFClient::in_destruct; + delete $WIDGET{$self+0}; eval { $self->destroy }; @@ -680,6 +733,14 @@ $self } +sub realloc { + my ($self) = @_; + + $self->{force_realloc} = 1; + $self->{force_size_alloc} = 1; + $self->SUPER::realloc; +} + sub add { my ($self, @widgets) = @_; @@ -974,8 +1035,9 @@ ; $self = $class->SUPER::new ( - vp => (new CFClient::UI::ViewPort expand => 1), - slider => $slider, + vp => (new CFClient::UI::ViewPort expand => 1), + can_events => 1, + slider => $slider, %arg, ); @@ -985,34 +1047,46 @@ $self } +#TODO# update range on size_allocate depending on child + sub add { my ($self, $widget) = @_; $self->{vp}->add ($self->{child} = $widget); } +sub invoke_mouse_wheel { + my ($self, $ev) = @_; + + return 0 unless $ev->{dy}; # only vertical movements + + $self->{slider}->emit (mouse_wheel => $ev); + + 1 +} + +sub update_slider { + my ($self) = @_; + + $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); +} + sub update { my ($self) = @_; $self->SUPER::update; - # todo: overwrite size_allocate of child - my $child = $self->{vp}->child; - $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); + $self->update_slider; } sub invoke_size_allocate { my ($self, $w, $h) = @_; - my $child = $self->{vp}->child; - $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); + $self->update_slider; $self->SUPER::invoke_size_allocate ($w, $h) } -#TODO# update range on size_allocate depending on child -# update viewport offset on scroll - ############################################################################# package CFClient::UI::Frame; @@ -1077,8 +1151,8 @@ border_bg => [1, 1, 1, 1], border => 0.6, can_events => 1, - min_w => 16, - min_h => 16, + min_w => 64, + min_h => 32, %arg, ); @@ -1092,8 +1166,8 @@ if ($self->{has_close_button}) { $self->{close_button} = new CFClient::UI::ImageButton - image => 'x1_close.png', - on_activate => sub { $self->hide }; + path => 'x1_close.png', + on_activate => sub { $self->emit ("delete") }; $self->CFClient::UI::Container::add ($self->{close_button}); } @@ -1152,6 +1226,14 @@ 1 } +sub invoke_delete { + my ($self) = @_; + + $self->hide; + + 1 +} + sub invoke_button_down { my ($self, $ev, $x, $y) = @_; @@ -1284,11 +1366,16 @@ } sub add { - my ($self, $x, $y, $child) = @_; + my ($self) = shift; - $child->set_parent ($self); - $self->{children}[$y][$x] = $child; + while (@_) { + my ($x, $y, $child) = splice @_, 0, 3, (); + $child->set_parent ($self); + $self->{children}[$y][$x] = $child; + } + $self->{force_realloc} = 1; + $self->{force_size_alloc} = 1; $self->realloc; } @@ -1439,35 +1526,35 @@ my ($self, $w, $h) = @_; my $space = $self->{vertical} ? $h : $w; - my $children = $self->{children}; + my @children = $self->visible_children; my @req; if ($self->{homogeneous}) { - @req = ($space / (@$children || 1)) x @$children; + @req = ($space / (@children || 1)) x @children; } else { - @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; + @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children; my $req = List::Util::sum @req; if ($req > $space) { # ah well, not enough space $_ *= $space / $req for @req; } else { - my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; + my $expand = (List::Util::sum map $_->{expand}, @children) || 1; $space = ($space - $req) / $expand; # remaining space to give away - $req[$_] += $space * $children->[$_]{expand} - for 0 .. $#$children; + $req[$_] += $space * $children[$_]{expand} + for 0 .. $#children; } } CFClient::UI::harmonize \@req; my $pos = 0; - for (0 .. $#$children) { + for (0 .. $#children) { my $alloc = $req[$_]; - $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); + $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); $pos += $alloc; } @@ -1550,16 +1637,6 @@ $self } -sub escape($) { - local $_ = $_[0]; - - s/&/&/g; - s/>/>/g; - s/{text} eq "T$text"; $self->{text} = "T$text"; - $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; $self->{layout}->set_text ($text); delete $self->{size_req}; @@ -1596,7 +1672,6 @@ my $rgba = $markup =~ /span.*(?:foreground|background)/; - $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; $self->{layout}->set_markup ($markup); delete $self->{size_req}; @@ -1618,6 +1693,7 @@ if (exists $self->{template}) { $self->{template}->set_font ($self->{font}) if $self->{font}; + $self->{template}->set_width ($self->{max_w} || -1); $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); my ($w2, $h2) = $self->{template}->size; @@ -1632,6 +1708,10 @@ @{ $self->{size_req} } } +sub baseline_shift { + $_[0]{layout}->descent +} + sub invoke_size_allocate { my ($self, $w, $h) = @_; @@ -1647,6 +1727,7 @@ my ($self, $fontsize) = @_; $self->{fontsize} = $fontsize; + delete $self->{size_req}; delete $self->{texture}; $self->realloc; @@ -1656,6 +1737,7 @@ my ($self) = @_; delete $self->{size_req}; + delete $self->{texture}; $self->SUPER::reconfigure; } @@ -1665,7 +1747,7 @@ $self->SUPER::_draw; # draw background, if applicable - my $tex = $self->{texture} ||= do { + my $size = $self->{texture} ||= do { $self->{layout}->set_foreground (@{$self->{fg}}); $self->{layout}->set_font ($self->{font}) if $self->{font}; $self->{layout}->set_width ($self->{w}); @@ -1673,34 +1755,23 @@ $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); - new_from_layout CFClient::Texture $self->{layout} + [$self->{layout}->size] }; unless (exists $self->{ox}) { $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} - : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} - : ($self->{w} - $tex->{w}) * 0.5); + : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} + : ($self->{w} - $size->[0]) * 0.5); $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} - : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} - : ($self->{h} - $tex->{h}) * 0.5); + : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} + : ($self->{h} - $size->[1]) * 0.5); }; - glEnable GL_TEXTURE_2D; - - my $w = List::Util::min $self->{w} + 4, $tex->{w}; - my $h = List::Util::min $self->{h} + 2, $tex->{h}; - - if ($tex->{format} == GL_ALPHA) { - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - glColor @{$self->{fg}}; - $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h); - } else { - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h); - } + my $w = List::Util::min $self->{w} + 4, $size->[0]; + my $h = List::Util::min $self->{h} + 2, $size->[1]; - glDisable GL_TEXTURE_2D; + $self->{layout}->render ($self->{ox}, $self->{oy}); } ############################################################################# @@ -1821,8 +1892,8 @@ # byte-index to char-index my $text = $self->{text}; - utf8::encode $text; - $self->{cursor} = length substr $text, 0, $idx; + utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; + $self->{cursor} = length $text; $self->_set_text ($self->{text}); $self->update; @@ -1893,6 +1964,7 @@ if ($sym == 13) { unshift @{$self->{history}}, my $txt = $self->get_text; + $self->{history_pointer} = -1; $self->{history_saveback} = ''; $self->emit (activate => $txt); @@ -1983,42 +2055,6 @@ ############################################################################# -package CFClient::UI::ImageButton; - -our @ISA = CFClient::UI::Image::; - -use CFClient::OpenGL; - -my %textures; - -sub new { - my $class = shift; - - my $self = $class->SUPER::new ( - padding_x => 4, - padding_y => 4, - fg => [1, 1, 1], - active_fg => [0, 0, 1], - can_hover => 1, - align => 0, - valign => 0, - can_events => 1, - @_ - ); -} - -sub invoke_button_up { - my ($self, $ev, $x, $y) = @_; - - $self->emit ("activate") - if $x >= 0 && $x < $self->{w} - && $y >= 0 && $y < $self->{h}; - - 1 -} - -############################################################################# - package CFClient::UI::CheckBox; our @ISA = CFClient::UI::DrawBG::; @@ -2051,13 +2087,20 @@ (6) x 2 } +sub toggle { + my ($self) = @_; + + $self->{state} = !$self->{state}; + $self->emit (changed => $self->{state}); + $self->update; +} + sub invoke_button_down { my ($self, $ev, $x, $y) = @_; if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { - $self->{state} = !$self->{state}; - $self->emit (changed => $self->{state}); + $self->toggle; } else { return 0 } @@ -2092,33 +2135,49 @@ our @ISA = CFClient::UI::Base::; use CFClient::OpenGL; -use Carp qw/confess/; -our %loaded_images; +our %texture_cache; sub new { my $class = shift; - my $self = $class->SUPER::new (can_events => 0, @_); - - $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; + my $self = $class->SUPER::new ( + can_events => 0, + @_, + ); - $loaded_images{$self->{image}} ||= - new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; + $self->{path} || $self->{tex} + or Carp::croak "'path' or 'tex' attributes required"; - my $tex = $self->{tex} = $loaded_images{$self->{image}}; + $self->{tex} ||= $texture_cache{$self->{path}} ||= + new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; - Scalar::Util::weaken $loaded_images{$self->{image}}; + Scalar::Util::weaken $texture_cache{$self->{path}}; - $self->{aspect} = $tex->{w} / $tex->{h}; + $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; $self } +sub STORABLE_freeze { + my ($self, $cloning) = @_; + + $self->{path} + or die "cannot serialise CFClient::UI::Image on non-loadable images\n"; + + $self->{path} +} + +sub STORABLE_attach { + my ($self, $cloning, $path) = @_; + + $self->new (path => $path) +} + sub size_request { my ($self) = @_; - ($self->{tex}->{w}, $self->{tex}->{h}) + ($self->{tex}{w}, $self->{tex}{h}) } sub _draw { @@ -2138,13 +2197,49 @@ glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - $tex->draw_quad_alpha (0, 0, $w, $h); + $tex->draw_quad (0, 0, $w, $h); glDisable GL_TEXTURE_2D; } ############################################################################# +package CFClient::UI::ImageButton; + +our @ISA = CFClient::UI::Image::; + +use CFClient::OpenGL; + +my %textures; + +sub new { + my $class = shift; + + my $self = $class->SUPER::new ( + padding_x => 4, + padding_y => 4, + fg => [1, 1, 1], + active_fg => [0, 0, 1], + can_hover => 1, + align => 0, + valign => 0, + can_events => 1, + @_ + ); +} + +sub invoke_button_up { + my ($self, $ev, $x, $y) = @_; + + $self->emit ("activate") + if $x >= 0 && $x < $self->{w} + && $y >= 0 && $y < $self->{h}; + + 1 +} + +############################################################################# + package CFClient::UI::VGauge; our @ISA = CFClient::UI::Base::; @@ -2237,6 +2332,9 @@ my $h1 = $self->{h} * (1 - $ycut1); my $h2 = $self->{h} * (1 - $ycut2); + my $h3 = $self->{h}; + + $_ = $_ * (284-4)/288 + 4/288 for ($h1, $h2, $h3); glEnable GL_BLEND; glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, @@ -2265,8 +2363,8 @@ glBindTexture GL_TEXTURE_2D, $t3->{name}; glBegin GL_QUADS; glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; - glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; - glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; + glTexCoord 0 , $t3->{t}; glVertex 0 , $h3; + glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3; glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; glEnd; } @@ -2411,7 +2509,7 @@ $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; - $self->mouse_motion ($ev, $x, $y) + $self->invoke_mouse_motion ($ev, $x, $y) } sub invoke_mouse_motion { @@ -2432,6 +2530,16 @@ 1 } +sub invoke_mouse_wheel { + my ($self, $ev) = @_; + + my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; + + $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); + + ! ! $delta +} + sub update { my ($self) = @_; @@ -2540,12 +2648,12 @@ my $self = $class->SUPER::new ( fontsize => 1, - can_events => 0, + can_events => 1, indent => 0, #font => default_font @_, - layout => (new CFClient::Layout 1), + layout => (new CFClient::Layout), par => [], height => 0, children => [ @@ -2566,30 +2674,61 @@ $self->reflow; } +sub size_request { + my ($self) = @_; + + my ($empty, $slider) = @{ $self->{children} }; + + local $self->{children} = [$empty, $slider]; + $self->SUPER::size_request +} + sub invoke_size_allocate { my ($self, $w, $h) = @_; + my ($empty, $slider, @other) = @{ $self->{children} }; + $_->configure (@$_{qw(x y req_w req_h)}) for @other; + $self->{layout}->set_font ($self->{font}) if $self->{font}; $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); - $self->{layout}->set_width ($self->{children}[0]{w}); + $self->{layout}->set_width ($empty->{w}); $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); $self->reflow; + local $self->{children} = [$empty, $slider]; $self->SUPER::invoke_size_allocate ($w, $h) } -sub text_size { - my ($self, $text, $indent) = @_; +sub invoke_mouse_wheel { + my ($self, $ev) = @_; + + return 0 unless $ev->{dy}; # only vertical movements + + $self->{children}[1]->emit (mouse_wheel => $ev); + + 1 +} + +sub get_layout { + my ($self, $para) = @_; my $layout = $self->{layout}; + $layout->set_font ($self->{font}) if $self->{font}; + $layout->set_foreground (@{$para->{fg}}); $layout->set_height ($self->{fontsize} * $::FONTSIZE); - $layout->set_width ($self->{children}[0]{w} - $indent); + $layout->set_width ($self->{children}[0]{w} - $para->{indent}); $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); - $layout->set_markup ($text); - - $layout->size + $layout->set_markup ($para->{markup}); + + $layout->set_shapes ( + map + +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), + @{$para->{widget}} + ); + + $layout } sub reflow { @@ -2609,21 +2748,40 @@ sub clear { my ($self) = @_; + my (undef, undef, @other) = @{ $self->{children} }; + $self->remove ($_) for @other; + $self->{par} = []; $self->{height} = 0; $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); } sub add_paragraph { - my ($self, $color, $text, $indent) = @_; + my ($self, $color, $para, $indent) = @_; - for my $line (split /\n/, $text) { - my ($w, $h) = $self->text_size ($line); - $self->{height} += $h; - push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; - } + my ($text, @w) = ref $para ? @$para : $para; - $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); + $para = { + w => 1e10, + wrapped => 1, + fg => $color, + indent => $indent, + markup => $text, + widget => \@w, + }; + + $self->add (@w) if @w; + push @{$self->{par}}, $para; + + $self->{need_reflow}++; + $self->update; +} + +sub scroll_to_bottom { + my ($self) = @_; + + $self->{scroll_to_bottom} = 1; + $self->update; } sub update { @@ -2641,30 +2799,30 @@ if (delete $self->{need_reflow}) { my $height = 0; - my $layout = $self->{layout}; - - $layout->set_height ($self->{fontsize} * $::FONTSIZE); - - for (@{$self->{par}}) { - if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support - $layout->set_width ($W - $_->[3]); - $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); - $layout->set_markup ($_->[4]); + for my $para (@{$self->{par}}) { + if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { + my $layout = $self->get_layout ($para); my ($w, $h) = $layout->size; - $_->[0] = $w + $_->[3]; - $_->[1] = $h; + + $para->{w} = $w + $para->{indent}; + $para->{h} = $h; + $para->{wrapped} = $layout->has_wrapped; } - $height += $_->[1]; + $height += $para->{h}; } $self->{height} = $height; - $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); + $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); delete $self->{texture}; } + if (delete $self->{scroll_to_bottom}) { + $self->{children}[1]->set_value (1e10); + } + $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { glClearColor 0, 0, 0, 0; glClear GL_COLOR_BUFFER_BIT; @@ -2676,37 +2834,44 @@ my $y = 0; - my $layout = $self->{layout}; + for my $para (@{$self->{par}}) { + my $h = $para->{h}; + + if ($y0 < $y + $h && $y < $y1) { - $layout->set_font ($self->{font}) if $self->{font}; + my $layout = $self->get_layout ($para); - glEnable GL_BLEND; - #TODO# not correct in windows where rgba is forced off - glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; + $layout->render ($para->{indent}, $y - $y0); - for my $par (@{$self->{par}}) { - my $h = $par->[1]; + if (my @w = @{ $para->{widget} }) { + my @s = $layout->get_shapes; - if ($y0 < $y + $h && $y < $y1) { - $layout->set_foreground (@{ $par->[2] }); - $layout->set_width ($W - $par->[3]); - $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); - $layout->set_markup ($par->[4]); + for (@w) { + my ($dx, $dy) = splice @s, 0, 2, (); - my ($w, $h, $data, $format, $internalformat) = $layout->render; + $_->{x} = $dx + $para->{indent}; + $_->{y} = $dy + $y - $y0; - glRasterPos $par->[3], $y - $y0; - glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; + $_->draw; + } + } } $y += $h; } - - glDisable GL_BLEND; }; }); } +sub reconfigure { + my ($self) = @_; + + $self->SUPER::reconfigure; + + $_->{w} = 1e10 for @{ $self->{par} }; + $self->reflow; +} + sub _draw { my ($self) = @_; @@ -2717,7 +2882,6 @@ glDisable GL_TEXTURE_2D; $self->{children}[1]->draw; - } ############################################################################# @@ -2851,12 +3015,16 @@ my $widget = $self->{owner} or return; - my ($x, $y) = $widget->coord2global ($widget->{w}, 0); + if ($widget->{visible}) { + my ($x, $y) = $widget->coord2global ($widget->{w}, 0); - ($x, $y) = $widget->coord2global (-$self->{w}, 0) - if $x + $self->{w} > $self->{root}{w}; + ($x, $y) = $widget->coord2global (-$self->{w}, 0) + if $x + $self->{w} > $self->{root}{w}; - $self->move_abs ($x, $y); + $self->move_abs ($x, $y); + } else { + $self->hide; + } }); } @@ -2892,7 +3060,7 @@ package CFClient::UI::Face; -our @ISA = CFClient::UI::Base::; +our @ISA = CFClient::UI::DrawBG::; use CFClient::OpenGL; @@ -2939,6 +3107,8 @@ return unless $::CONN; + $self->SUPER::_draw; + my $face; if ($self->{frame}) { @@ -3000,11 +3170,26 @@ # handle various types of items, only text for now if (!ref $widget) { - $widget = new CFClient::UI::Label - can_hover => 1, - can_events => 1, - markup => $widget, - tooltip => $tooltip + if ($widget =~ /\t/) { + my ($left, $right) = split /\t/, $widget, 2; + + $widget = new CFClient::UI::HBox + can_hover => 1, + can_events => 1, + tooltip => $tooltip, + children => [ + (new CFClient::UI::Label markup => $left, expand => 1), + (new CFClient::UI::Label markup => $right, align => +1), + ], + ; + + } else { + $widget = new CFClient::UI::Label + can_hover => 1, + can_events => 1, + markup => $widget, + tooltip => $tooltip; + } } $self->{item}{$widget} = $item; @@ -3083,6 +3268,12 @@ if @{ $self->{children} }; } +sub get_current_page { + my ($self) = @_; + + $self->{current} +} + sub set_current_page { my ($self, $page_or_widget) = @_; @@ -3158,6 +3349,12 @@ $self->{multiplexer}->add ($widget); } +sub get_current_page { + my ($self) = @_; + + $self->{multiplexer}->get_current_page +} + sub set_current_page { my ($self, $page) = @_; @@ -3167,7 +3364,7 @@ ############################################################################# -package CFClient::UI::Combobox; +package CFClient::UI::Selector; use utf8; @@ -3343,7 +3540,9 @@ }; } - $self->reorder; + $ROOT->on_refresh (reorder => sub { + $self->reorder; + }); } sub reconfigure { @@ -3368,240 +3567,62 @@ package CFClient::UI::Inventory; -our @ISA = CFClient::UI::ScrolledWindow::; +our @ISA = CFClient::UI::Table::; sub new { my $class = shift; my $self = $class->SUPER::new ( - child => (new CFClient::UI::Table col_expand => [0, 1, 0]), + col_expand => [0, 1, 0], + items => [], @_, ); + $self->set_sort_order (undef); + $self } -sub set_items { - my ($self, $items) = @_; - - $self->{child}->clear; - return unless $items; +sub update_items { + my ($self) = @_; - my @items = sort { - ($a->{type} <=> $b->{type}) - or ($a->{name} cmp $b->{name}) - } @$items; + $self->clear; - $self->{real_items} = \@items; + my @item = $self->{sort}->(@{ $self->{items} }); + my @adds; my $row = 0; - for my $item (@items) { + for my $item ($self->{sort}->(@{ $self->{items} })) { CFClient::Item::update_widgets $item; - $self->{child}->add (0, $row, $item->{face_widget}); - $self->{child}->add (1, $row, $item->{desc_widget}); - $self->{child}->add (2, $row, $item->{weight_widget}); + push @adds, 0, $row, $item->{face_widget}; + push @adds, 1, $row, $item->{desc_widget}; + push @adds, 2, $row, $item->{weight_widget}; $row++; } -} - -############################################################################# - -package CFClient::UI::BindEditor; - -our @ISA = CFClient::UI::FancyFrame::; - -sub new { - my $class = shift; - - my $self = $class->SUPER::new (binding => [], commands => [], @_); - - $self->add (my $vb = new CFClient::UI::VBox); - - - $vb->add ($self->{rec_btn} = new CFClient::UI::Button - text => "start recording", - tooltip => "Start/Stops recording of actions." - ."All subsequent actions after the recording started will be captured." - ."The actions are displayed after the record was stopped." - ."To bind the action you have to click on the 'Bind' button", - on_activate => sub { - unless ($self->{recording}) { - $self->start; - } else { - $self->stop; - } - }); - - $vb->add (new CFClient::UI::Label text => "Actions:"); - $vb->add ($self->{cmdbox} = new CFClient::UI::VBox); - - $vb->add (new CFClient::UI::Label text => "Bound to: "); - $vb->add (my $hb = new CFClient::UI::HBox); - $hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1); - $hb->add (new CFClient::UI::Button - text => "bind", - tooltip => "This opens a query where you have to press the key combination to bind the recorded actions", - on_activate => sub { - $self->ask_for_bind; - }); - - $vb->add (my $hb = new CFClient::UI::HBox); - $hb->add (new CFClient::UI::Button - text => "ok", - expand => 1, - tooltip => "This closes the binding editor and saves the binding", - on_activate => sub { - $self->hide; - $self->commit; - }); - - $hb->add (new CFClient::UI::Button - text => "cancel", - expand => 1, - tooltip => "This closes the binding editor without saving", - on_activate => sub { - $self->hide; - $self->{binding_cancel}->() - if $self->{binding_cancel}; - }); - - $self->update_binding_widgets; - - $self -} - -sub commit { - my ($self) = @_; - my ($mod, $sym, $cmds) = $self->get_binding; - if ($sym != 0 && @$cmds > 0) { - $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym) - ."'. Don't forget 'Save Config'!"); - $self->{binding_change}->($mod, $sym, $cmds) - if $self->{binding_change}; - } else { - $::STATUSBOX->add ("No action bound, no key or action specified!"); - $self->{binding_cancel}->() - if $self->{binding_cancel}; - } -} - -sub start { - my ($self) = @_; - - $self->{rec_btn}->set_text ("stop recording"); - $self->{recording} = 1; - $self->clear_command_list; - $::CONN->start_record if $::CONN; -} - -sub stop { - my ($self) = @_; - - $self->{rec_btn}->set_text ("start recording"); - $self->{recording} = 0; - - my $rec; - $rec = $::CONN->stop_record if $::CONN; - return unless ref $rec eq 'ARRAY'; - $self->set_command_list ($rec); -} - - -sub ask_for_bind_and_commit { - my ($self) = @_; - $self->ask_for_bind (1); -} - -sub ask_for_bind { - my ($self, $commit, $end_cb) = @_; - - CFClient::Binder::open_binding_dialog (sub { - my ($mod, $sym) = @_; - $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? - $self->update_binding_widgets; - $self->commit if $commit; - $end_cb->() if $end_cb; - }); -} -# $mod and $sym are the modifiers and key symbol -# $cmds is a array ref of strings (the commands) -# $cb is the callback that is executed on OK -# $ccb is the callback that is executed on CANCEL and -# when the binding was unsuccessful on OK -sub set_binding { - my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_; - - $self->clear_command_list; - $self->{recording} = 0; - $self->{rec_btn}->set_text ("start recording"); - - $self->{binding} = [$mod, $sym]; - $self->{commands} = $cmds; - - $self->{binding_change} = $cb; - $self->{binding_cancel} = $ccb; - - $self->update_binding_widgets; -} - -# this is a shortcut method that asks for a binding -# and then just binds it. -sub do_quick_binding { - my ($self, $cmds, $end_cb) = @_; - $self->set_binding (undef, undef, $cmds, sub { - $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2]; - }); - $self->ask_for_bind (1, $end_cb); + $self->add (@adds); } -sub update_binding_widgets { - my ($self) = @_; - my ($mod, $sym, $cmds) = $self->get_binding; - $self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym)); - $self->set_command_list ($cmds); -} +sub set_sort_order { + my ($self, $order) = @_; -sub get_binding { - my ($self) = @_; - return ( - $self->{binding}->[0], - $self->{binding}->[1], - [ grep { defined $_ } @{$self->{commands}} ] - ); -} + $self->{sort} = $order ||= sub { + sort { + $a->{type} <=> $b->{type} + or $a->{name} cmp $b->{name} + } @_ + }; -sub clear_command_list { - my ($self) = @_; - $self->{cmdbox}->clear (); + $self->update_items; } -sub set_command_list { - my ($self, $cmds) = @_; - - $self->{cmdbox}->clear (); - $self->{commands} = $cmds; - - my $idx = 0; - - for (@$cmds) { - $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox); - - my $i = $idx; - $hb->add (new CFClient::UI::Label text => $_); - $hb->add (new CFClient::UI::Button - text => "delete", - tooltip => "Deletes the action from the record", - on_activate => sub { - $self->{cmdbox}->remove ($hb); - $cmds->[$i] = undef; - }); - +sub set_items { + my ($self, $items) = @_; - $idx++ - } + $self->{items} = [$items ? values %$items : ()]; + $self->update_items; } ############################################################################# @@ -3664,7 +3685,7 @@ } elsif ($ev->{button} == 3) { (new CFClient::UI::Menu items => [ - ["bind cast $spell->{name} to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], + ["bind cast $spell->{name} to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], ["bind invoke $spell->{name} to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], ], )->popup ($ev); @@ -3920,6 +3941,15 @@ $w = 0 if $w < 0; $h = 0 if $h < 0; + $w = max $widget->{min_w}, $w; + $h = max $widget->{min_h}, $h; + +# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; +# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; + + $w = min $widget->{max_w}, $w if exists $widget->{max_w}; + $h = min $widget->{max_h}, $h if exists $widget->{max_h}; + $w = int $w + 0.5; $h = int $h + 0.5;