--- deliantra/Deliantra-Client/DC/UI.pm 2006/06/07 07:00:30 1.298 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/06/18 17:13:12 1.305 @@ -274,8 +274,8 @@ $_->set_invisible for $self->children; - delete $self->{root}; delete $self->{visible}; + delete $self->{root}; undef $GRAB if $GRAB == $self; undef $HOVER if $HOVER == $self; @@ -283,8 +283,7 @@ $CFClient::UI::TOOLTIP_WATCHER->cb->() if $TOOLTIP->{owner} == $self; - $self->focus_out; - + $self->emit ("focus_out"); $self->emit (visibility_change => 0); } @@ -369,10 +368,6 @@ } } -sub size_allocate { - # nothing to be done -} - sub children { # nop } @@ -420,7 +415,7 @@ $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) } -sub focus_in { +sub invoke_focus_in { my ($self) = @_; return if $FOCUS == $self; @@ -428,38 +423,58 @@ my $focus = $FOCUS; $FOCUS = $self; - $self->_emit (focus_in => $focus); - $focus->update if $focus; $FOCUS->update; + + 0 } -sub focus_out { +sub invoke_focus_out { my ($self) = @_; return unless $FOCUS == $self; my $focus = $FOCUS; undef $FOCUS; - $self->_emit (focus_out => $focus); - $focus->update if $focus; #? - $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus + $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus unless $FOCUS; + + 0 +} + +sub grab_focus { + my ($self) = @_; + + $self->emit ("focus_in"); } -sub mouse_motion { 0 } -sub button_up { 0 } -sub key_down { 0 } -sub key_up { 0 } +sub invoke_mouse_motion { 1 } +sub invoke_button_up { 1 } +sub invoke_key_down { 1 } +sub invoke_key_up { 1 } -sub button_down { +sub invoke_button_down { my ($self, $ev, $x, $y) = @_; - $self->focus_in; + $self->grab_focus; - 0 + 1 +} + +sub connect { + my ($self, $signal, $cb) = @_; + + push @{ $self->{signal_cb}{$signal} }, $cb; +} + +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 } sub find_widget { @@ -481,29 +496,6 @@ $self->set_visible if $parent->{visible}; } -sub connect { - my ($self, $signal, $cb) = @_; - - push @{ $self->{signal_cb}{$signal} }, $cb; -} - -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 visibility_change { - #my ($self, $visible) = @_; -} - sub realloc { my ($self) = @_; @@ -595,7 +587,9 @@ my ($self) = @_; delete $WIDGET{$self+0}; - #$self->deactivate; + + eval { $self->destroy }; + warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; } ############################################################################# @@ -770,8 +764,7 @@ sub add { my ($self, $child) = @_; - $self->{children} = []; - + $self->SUPER::remove ($_) for @{ $self->{children} }; $self->SUPER::add ($child); } @@ -790,10 +783,12 @@ $_[0]{children}[0]->size_request } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; $self->{children}[0]->configure (0, 0, $w, $h); + + 1 } ############################################################################# @@ -819,11 +814,12 @@ $self->SUPER::update; } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; - $self->SUPER::size_allocate ($w, $h); $self->update; + + $self->SUPER::invoke_size_allocate ($w, $h) } sub _render { @@ -894,7 +890,7 @@ ($w, $h) } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; my $child = $self->child; @@ -904,6 +900,8 @@ $self->child->configure (0, 0, $w, $h); $self->update; + + 1 } sub set_offset { @@ -1003,13 +1001,13 @@ $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; - $self->SUPER::size_allocate ($w, $h); - my $child = $self->{vp}->child; $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); + + $self->SUPER::invoke_size_allocate ($w, $h) } #TODO# update range on size_allocate depending on child @@ -1074,8 +1072,6 @@ sub new { my ($class, %arg) = @_; - my $title = delete $arg{title}; - my $self = $class->SUPER::new ( bg => [1, 1, 1, 1], border_bg => [1, 1, 1, 1], @@ -1086,12 +1082,21 @@ %arg, ); - $self->{title} = new CFClient::UI::Label + $self->{title_widget} = new CFClient::UI::Label align => 0, valign => 1, - text => $title, - fontsize => $self->{border} - if defined $title; + text => $self->{title}, + fontsize => $self->{border}, + if exists $self->{title}; + + if ($self->{has_close_button}) { + $self->{close_button} = + new CFClient::UI::ImageButton + image => 'x1_close.png', + on_activate => sub { $self->hide }; + + $self->CFClient::UI::Container::add ($self->{close_button}); + } $self } @@ -1100,7 +1105,8 @@ my ($self, @widgets) = @_; $self->SUPER::add (@widgets); - $self->CFClient::UI::Container::add ($self->{title}) if $self->{title}; + $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; + $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; } sub border { @@ -1110,8 +1116,11 @@ sub size_request { my ($self) = @_; - $self->{title}->size_request - if $self->{title}; + $self->{title_widget}->size_request + if $self->{title_widget}; + + $self->{close_button}->size_request + if $self->{close_button}; my ($w, $h) = $self->SUPER::size_request; @@ -1121,24 +1130,29 @@ ) } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; - if ($self->{title}) { - $self->{title}{w} = $w; - $self->{title}{h} = $h; - $self->{title}->size_allocate ($w, $h); + if ($self->{title_widget}) { + $self->{title_widget}{w} = $w; + $self->{title_widget}{h} = $h; + $self->{title_widget}->invoke_size_allocate ($w, $h); } my $border = $self->border; $h -= List::Util::max 0, $border * 2; $w -= List::Util::max 0, $border * 2; - + $self->child->configure ($border, $border, $w, $h); + + $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border) + if $self->{close_button}; + + 1 } -sub button_down { +sub invoke_button_down { my ($self, $ev, $x, $y) = @_; my ($w, $h) = @$self{qw(w h)}; @@ -1179,7 +1193,7 @@ $self->move_abs ($bx + $x - $ox, $by + $y - $oy); # HACK: the next line is required to enforce placement - $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h}); + $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h}); }; } else { return 0; @@ -1188,18 +1202,18 @@ 1 } -sub button_up { +sub invoke_button_up { my ($self, $ev, $x, $y) = @_; - !!delete $self->{motion} + ! ! delete $self->{motion} } -sub mouse_motion { +sub invoke_mouse_motion { my ($self, $ev, $x, $y) = @_; $self->{motion}->($ev, $x, $y) if $self->{motion}; - !!$self->{motion} + ! ! $self->{motion} } sub _draw { @@ -1235,10 +1249,15 @@ $child->draw; - if ($self->{title}) { + if ($self->{title_widget}) { glTranslate 0, $border - $self->{h}; - $self->{title}->_draw; + $self->{title_widget}->_draw; + + glTranslate 0, - ($border - $self->{h}); } + + $self->{close_button}->draw + if $self->{close_button}; } ############################################################################# @@ -1273,6 +1292,12 @@ $self->realloc; } +sub remove { + my ($self, $child) = @_; + + # TODO: not yet implemented +} + # TODO: move to container class maybe? send children a signal on removal? sub clear { my ($self) = @_; @@ -1321,7 +1346,7 @@ ) } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; my ($ws, $hs) = $self->get_wh; @@ -1363,6 +1388,7 @@ $y += $row_h; } + 1 } sub find_widget { @@ -1409,7 +1435,7 @@ ) } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; my $space = $self->{vertical} ? $h : $w; @@ -1606,13 +1632,15 @@ @{ $self->{size_req} } } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; delete $self->{ox}; delete $self->{texture} unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w}; + + 1 } sub set_fontsize { @@ -1715,7 +1743,7 @@ $self->{layout}->set_text ("$text "); delete $self->{size_req}; - $self->_emit (changed => $self->{text}); + $self->emit (changed => $self->{text}); $self->realloc; $self->update; @@ -1740,7 +1768,7 @@ ($w + 1, $h) # add 1 for cursor } -sub key_down { +sub invoke_key_down { my ($self, $ev) = @_; my $mod = $ev->{mod}; @@ -1762,7 +1790,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; } else { @@ -1776,18 +1804,18 @@ 1 } -sub focus_in { +sub invoke_focus_in { my ($self) = @_; $self->{last_activity} = $::NOW; - $self->SUPER::focus_in; + $self->SUPER::invoke_focus_in } -sub button_down { +sub invoke_button_down { my ($self, $ev, $x, $y) = @_; - $self->SUPER::button_down ($ev, $x, $y); + $self->SUPER::invoke_button_down ($ev, $x, $y); my $idx = $self->{layout}->xy_to_index ($x, $y); @@ -1802,11 +1830,11 @@ 1 } -sub mouse_motion { +sub invoke_mouse_motion { my ($self, $ev, $x, $y) = @_; # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# - 0 + 1 } sub _draw { @@ -1857,7 +1885,7 @@ use CFClient::OpenGL; -sub key_down { +sub invoke_key_down { my ($self, $ev) = @_; my $sym = $ev->{sym}; @@ -1867,7 +1895,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) { @@ -1893,7 +1921,7 @@ } } else { - return $self->SUPER::key_down ($ev) + return $self->SUPER::invoke_key_down ($ev) } 1 @@ -1927,9 +1955,7 @@ ) } -sub activate { } - -sub button_up { +sub invoke_button_up { my ($self, $ev, $x, $y) = @_; $self->emit ("activate") @@ -1957,6 +1983,42 @@ ############################################################################# +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::; @@ -1989,13 +2051,13 @@ (6) x 2 } -sub button_down { +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->emit (changed => $self->{state}); } else { return 0 } @@ -2302,8 +2364,6 @@ $self } -sub changed { } - sub set_range { my ($self, $range) = @_; @@ -2333,7 +2393,7 @@ @{$self->{range}} = ($value, $lo, $hi, $page, $unit); if ($value != $old_value) { - $self->_emit (changed => $value); + $self->emit (changed => $value); $self->update; } } @@ -2344,17 +2404,17 @@ ($self->{req_w}, $self->{req_h}) } -sub button_down { +sub invoke_button_down { my ($self, $ev, $x, $y) = @_; - $self->SUPER::button_down ($ev, $x, $y); + $self->SUPER::invoke_button_down ($ev, $x, $y); $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; $self->mouse_motion ($ev, $x, $y) } -sub mouse_motion { +sub invoke_mouse_motion { my ($self, $ev, $x, $y) = @_; if ($GRAB == $self) { @@ -2506,17 +2566,17 @@ $self->reflow; } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; - $self->SUPER::size_allocate ($w, $h); - $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_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); $self->reflow; + + $self->SUPER::invoke_size_allocate ($w, $h) } sub text_size { @@ -2776,13 +2836,13 @@ ($w + 4, $h + 4) } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; - $self->SUPER::size_allocate ($w - 4, $h - 4); + $self->SUPER::invoke_size_allocate ($w - 4, $h - 4) } -sub visibility_change { +sub invoke_visibility_change { my ($self, $visible) = @_; return unless $visible; @@ -2899,13 +2959,13 @@ } } -sub DESTROY { +sub destroy { my ($self) = @_; $self->{timer}->cancel if $self->{timer}; - $self->SUPER::DESTROY; + $self->SUPER::destroy; } ############################################################################# @@ -2959,7 +3019,7 @@ sub popup { my ($self, $ev) = @_; - $self->_emit ("popdown"); + $self->emit ("popdown"); # maybe save $GRAB? must be careful about events... $GRAB = $self; @@ -2969,7 +3029,7 @@ $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); } -sub mouse_motion { +sub invoke_mouse_motion { my ($self, $ev, $x, $y) = @_; # TODO: should use vbox->find_widget or so @@ -2979,14 +3039,14 @@ 0 } -sub button_up { +sub invoke_button_up { my ($self, $ev, $x, $y) = @_; if ($ev->{button} == $self->{button}) { undef $GRAB; $self->hide; - $self->_emit ("popdown"); + $self->emit ("popdown"); $self->{hover}[1]->() if $self->{hover}; } else { return 0 @@ -3033,7 +3093,7 @@ $self->{current} = $widget; $self->{current}->configure (0, 0, $self->{w}, $self->{h}); - $self->_emit (page_changed => $self->{current}); + $self->emit (page_changed => $self->{current}); $self->realloc; } @@ -3048,10 +3108,12 @@ $self->{current}->size_request } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; $self->{current}->configure (0, 0, $w, $h); + + 1 } sub _draw { @@ -3100,7 +3162,7 @@ my ($self, $page) = @_; $self->{multiplexer}->set_current_page ($page); - $self->_emit (page_changed => $self->{multiplexer}{current}); + $self->emit (page_changed => $self->{multiplexer}{current}); } ############################################################################# @@ -3125,7 +3187,7 @@ $self } -sub button_down { +sub invoke_button_down { my ($self, $ev) = @_; my @menu_items; @@ -3156,7 +3218,7 @@ return unless $self->{value} ne $value; $self->_set_value ($value); - $self->_emit (changed => $value); + $self->emit (changed => $value); } ############################################################################# @@ -3265,7 +3327,7 @@ $item->{count} = 1; $item->{text} = $item->{tooltip} = $text; } - $item->{id} = ++$self->{id}; + $item->{id} += 0.2;#d# $item->{timeout} = $timeout; delete $item->{label}; } else { @@ -3294,12 +3356,12 @@ $self->SUPER::reconfigure; } -sub DESTROY { +sub destroy { my ($self) = @_; $self->{timer}->cancel; - $self->SUPER::DESTROY; + $self->SUPER::destroy; } ############################################################################# @@ -3453,13 +3515,14 @@ } sub ask_for_bind { - my ($self, $commit) = @_; + 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; }); } @@ -3487,11 +3550,11 @@ # this is a shortcut method that asks for a binding # and then just binds it. sub do_quick_binding { - my ($self, $cmds) = @_; + my ($self, $cmds, $end_cb) = @_; $self->set_binding (undef, undef, $cmds, sub { $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2]; }); - $self->ask_for_bind (1); + $self->ask_for_bind (1, $end_cb); } sub update_binding_widgets { @@ -3559,8 +3622,10 @@ my $TOOLTIP_ALL = "\n\nLeft click - ready spell\nMiddle click - invoke spell\nRight click - further options"; -my @TOOLTIP_NAME = (align => 0, can_events => 1, can_hover => 1, tooltip => +my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip => "Name. The name of the spell.$TOOLTIP_ALL"); +my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip => + "Skill. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL"); my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip => "Level. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL"); my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip => @@ -3574,10 +3639,13 @@ $CFClient::UI::ROOT->on_refresh ($self => sub { $self->clear; + return unless $::CONN; + $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME); - $self->add (2, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL); - $self->add (3, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP); - $self->add (4, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG); + $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL); + $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL); + $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP); + $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG); my $row = 0; @@ -3607,11 +3675,16 @@ 1 }; + my $tooltip = "$spell->{message}$TOOLTIP_ALL"; + + #TODO: add path info to tooltip + #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path}); + $self->add (0, $row, new CFClient::UI::Face face => $spell->{face}, can_hover => 1, can_events => 1, - tooltip => $spell->{message}, + tooltip => $tooltip, on_button_down => $spell_cb, ); @@ -3620,13 +3693,14 @@ text => $spell->{name}, can_hover => 1, can_events => 1, - tooltip => "$spell->{message}$TOOLTIP_ALL", + tooltip => $tooltip, on_button_down => $spell_cb, ); - $self->add (2, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL); - $self->add (3, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); - $self->add (4, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); + $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); + $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL); + $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); + $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); } }); } @@ -3645,6 +3719,13 @@ $self->rebuild_spell_list; } +sub clear_spells { + my ($self) = @_; + + $self->{spell} = {}; + $self->rebuild_spell_list; +} + ############################################################################# package CFClient::UI::Root; @@ -3688,7 +3769,7 @@ int $coord + 0.5 } -sub size_allocate { +sub invoke_size_allocate { my ($self, $w, $h) = @_; for my $child ($self->children) { @@ -3702,6 +3783,8 @@ $child->configure ($X, $Y, $W, $H); } + + 1 } sub coord2local {