--- deliantra/Deliantra-Client/DC/UI.pm 2006/05/24 21:59:54 1.230 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/05/25 16:35:42 1.235 @@ -52,12 +52,12 @@ # class methods for events sub feed_sdl_key_down_event { - $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) + $FOCUS->emit (key_down => $_[0]) if $FOCUS; } sub feed_sdl_key_up_event { - $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) + $FOCUS->emit (key_up => $_[0]) if $FOCUS; } @@ -76,10 +76,8 @@ $BUTTON_STATE |= 1 << ($ev->{button} - 1); - if ($GRAB) { - ($x, $y) = $GRAB->coord2local ($x, $y); - $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); - } + $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) + if $GRAB; } sub feed_sdl_button_up_event { @@ -90,10 +88,8 @@ $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); - if ($GRAB) { - ($x, $y) = $GRAB->coord2local ($x, $y); - $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); - } + $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) + if $GRAB; if (!$BUTTON_STATE) { my $grab = $GRAB; undef $GRAB; @@ -119,10 +115,8 @@ check_tooltip; } - if ($HOVER) { - ($x, $y) = $HOVER->coord2local ($x, $y); - $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); - } + $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) + if $HOVER; } # convert position array to integers @@ -231,12 +225,27 @@ ); } -sub hide { +sub set_invisible { my ($self) = @_; + # broken show/hide model + + delete $self->{visible}; + undef $GRAB if $GRAB == $self; undef $HOVER if $HOVER == $self; + CFClient::UI::check_tooltip + if $CFClient::UI::TOOLTIP->{owner} == $self; + + $self->focus_out; +} + +sub hide { + my ($self) = @_; + + $self->set_invisible; + $self->{parent}->remove ($self) if $self->{parent}; } @@ -315,6 +324,11 @@ sub set_tooltip { my ($self, $tooltip) = @_; + $tooltip =~ s/^\s+//; + $tooltip =~ s/\s+$//; + + return if $self->{tooltip} eq $tooltip; + $self->{tooltip} = $tooltip; if ($CFClient::UI::TOOLTIP->{owner} == $self) { @@ -345,7 +359,7 @@ my $focus = $FOCUS; $FOCUS = $self; - $self->emit (focus_in => $focus); + $self->_emit (focus_in => $focus); $focus->update if $focus; $FOCUS->update; @@ -358,15 +372,18 @@ my $focus = $FOCUS; undef $FOCUS; - $self->emit (focus_out => $focus); + $self->_emit (focus_out => $focus); $focus->update if $focus; #? + + $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus + unless $FOCUS; } sub mouse_motion { } -sub button_up { } -sub key_down { } -sub key_up { } +sub button_up { } +sub key_down { } +sub key_up { } sub button_down { my ($self, $ev, $x, $y) = @_; @@ -410,10 +427,10 @@ glColor 1, 1, 0, 1; glTranslate $self->{x} + 0.375, $self->{y} + 0.375; glBegin GL_LINE_LOOP; - glVertex 0 , 0; - glVertex $self->{w}, 0; - glVertex $self->{w}, $self->{h}; - glVertex 0 , $self->{h}; + glVertex 0 , 0; + glVertex $self->{w} - 1, 0; + glVertex $self->{w} - 1, $self->{h} - 1; + glVertex 0 , $self->{h} - 1; glEnd; glPopMatrix; #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; @@ -470,12 +487,19 @@ push @{ $self->{signal_cb}{$signal} }, $cb; } -sub emit { +sub _emit { my ($self, $signal, @args) = @_; List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} } +sub emit { + my ($self, $signal, @args) = @_; + + $self->_emit ($signal, @args) + || $self->$signal (@args); +} + sub DESTROY { my ($self) = @_; @@ -746,6 +770,16 @@ our @ISA = CFClient::UI::Window::; +sub new { + my $class = shift; + + $class->SUPER::new ( + scroll_x => 0, + scroll_y => 1, + @_, + ) +} + sub size_request { my ($self) = @_; @@ -757,9 +791,10 @@ sub size_allocate { my ($self, $w, $h) = @_; - my ($cw, $ch) = @$self{qw(child_w child_h)}; -# $w = $self->{w}; - $self->child->configure (0, 0, $cw, $ch); + $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; + $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; + + $self->child->configure (0, 0, $w, $h); $self->update; } @@ -1064,7 +1099,7 @@ $class->SUPER::new ( col_expand => [], - @_ + @_, ) } @@ -1526,7 +1561,7 @@ $text =~ s/./*/g if $self->{hidden}; $self->{layout}->set_text ("$text "); - $self->emit (changed => $self->{text}); + $self->_emit (changed => $self->{text}); } sub set_text { @@ -1550,12 +1585,6 @@ ($w + 1, $h) # add 1 for cursor } -sub size_allocate { - my ($self, $w, $h) = @_; - - $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text -} - sub key_down { my ($self, $ev) = @_; @@ -1578,7 +1607,7 @@ } elsif ($sym == CFClient::SDLK_END) { $self->{cursor} = length $text; } elsif ($uni == 27) { - $self->emit ('escape'); + $self->_emit ('escape'); } elsif ($uni) { substr $text, $self->{cursor}++, 0, chr $uni; } @@ -1675,7 +1704,7 @@ my $txt = $self->get_text; $self->{history_pointer} = -1; $self->{history_saveback} = ''; - $self->emit (activate => $txt); + $self->_emit (activate => $txt); $self->update; } elsif ($sym == CFClient::SDLK_UP) { @@ -1733,13 +1762,14 @@ ) } +sub activate { } + sub button_up { my ($self, $ev, $x, $y) = @_; - if ($x >= 0 && $x < $self->{w} - && $y >= 0 && $y < $self->{h}) { - $self->emit ("activate"); - } + $self->emit ("activate") + if $x >= 0 && $x < $self->{w} + && $y >= 0 && $y < $self->{h}; } sub _draw { @@ -1801,7 +1831,7 @@ if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { $self->{state} = !$self->{state}; - $self->emit (changed => $self->{state}); + $self->_emit (changed => $self->{state}); } } @@ -2127,7 +2157,7 @@ @{$self->{range}} = ($value, $lo, $hi, $page, $unit); if ($value != $old_value) { - $self->emit (changed => $value); + $self->_emit (changed => $value); $self->update; } } @@ -2519,13 +2549,13 @@ if ($self->{state} = !$self->{state}) { $CFClient::UI::ROOT->add ($self->{other}); $self->{other}->move ($self->coord2global (0, $self->{h})); - $self->emit ("open"); + $self->_emit ("open"); } else { $CFClient::UI::ROOT->remove ($self->{other}); - $self->emit ("close"); + $self->_emit ("close"); } - $self->emit (changed => $self->{state}); + $self->_emit (changed => $self->{state}); } ############################################################################# @@ -2611,7 +2641,8 @@ my $class = shift; my $self = $class->SUPER::new ( - aspect => 1, + aspect => 1, + can_events => 0, @_, ); @@ -2680,118 +2711,6 @@ ############################################################################# -package CFClient::UI::InventoryItem; - -our @ISA = CFClient::UI::HBox::; - -sub _item_to_desc { - my ($item) = @_; - - my $desc = - $item->{nrof} < 2 - ? $item->{name} - : "$item->{nrof} × $item->{name_pl}"; - - $item->{flags} & Crossfire::Protocol::F_OPEN - and $desc .= " (open)"; - $item->{flags} & Crossfire::Protocol::F_APPLIED - and $desc .= " (applied)"; - $item->{flags} & Crossfire::Protocol::F_UNPAID - and $desc .= " (unpaid)"; - $item->{flags} & Crossfire::Protocol::F_MAGIC - and $desc .= " (magic)"; - $item->{flags} & Crossfire::Protocol::F_CURSED - and $desc .= " (cursed)"; - $item->{flags} & Crossfire::Protocol::F_DAMNED - and $desc .= " (damned)"; - $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) - . "\nleftclick - examine\nshift+leftclick - move/pickup/drop\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->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { - my $targ = $::CONN->{player}{tag}; - - if ($item->{container} == $::CONN->{player}{tag}) { - $targ = $main::OPENCONT; - } - - $::CONN->send ("move $targ $item->{tag} 0"); - } elsif ($ev->{button} == 1) { - $::CONN->send ("examine $item->{tag}"); - } 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 ". pack "N", $item->{tag}) }], - ["apply", sub { $::CONN->send ("apply $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); - } - - 1 - }, - %args - ); - - - $self->add (new CFClient::UI::Face - can_events => 0, - face => $item->{face}, - anim => $item->{anim}, - animspeed => $item->{animspeed}, - ); - - $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; our @ISA = CFClient::UI::ScrolledWindow::; @@ -2800,7 +2719,7 @@ my $class = shift; my $self = $class->SUPER::new ( - scrolled => (new CFClient::UI::Table), + scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), @_, ); @@ -2820,17 +2739,15 @@ $self->{real_items} = \@items; + my $row = 0; for my $item (@items) { - $item->{item} = $item; - $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; - $item->update_item (); - } + CFClient::Item::update_widgets $item; - my $i = 0; - for (@items) { - $self->{scrolled}->add (0, $i, $_); - my $nrof = $_->{item}->{nrof} || 1; - $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000); + $self->{scrolled}->add (0, $row, $item->{face_widget}); + $self->{scrolled}->add (1, $row, $item->{desc_widget}); + $self->{scrolled}->add (2, $row, $item->{weight_widget}); + + $row++; } # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; @@ -2883,7 +2800,7 @@ sub popup { my ($self, $ev) = @_; - $self->emit ("popdown"); + $self->_emit ("popdown"); # maybe save $GRAB? must be careful about events... $GRAB = $self; @@ -2908,7 +2825,7 @@ undef $GRAB; $self->hide; - $self->emit ("popdown"); + $self->_emit ("popdown"); $self->{hover}[1]->() if $self->{hover}; } } @@ -2981,6 +2898,8 @@ $text =~ s/^\s+//; $text =~ s/\s+$//; + return unless $text; + my $timeout = time + ((delete $arg{timeout}) || 60); my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; @@ -3123,7 +3042,7 @@ while (@children) { my $w = pop @children; push @children, $w->children; - delete $w->{visible}; + $w->set_invisible; } } @@ -3187,7 +3106,6 @@ $widget->{w} = $w; $widget->{h} = $h; - $widget->size_allocate ($w, $h); $widget->emit (size_allocate => $w, $h); } }