ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.356 by root, Wed Dec 6 00:15:12 2006 UTC vs.
Revision 1.363 by root, Sun Dec 10 00:06:49 2006 UTC

1package CFPlus::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util ();
7use List::Util (); 6use List::Util ();
8use Event; 7use Event;
9 8
10use CFPlus; 9use CFPlus;
11use CFPlus::Pod; 10use CFPlus::Pod;
235 h => undef, 234 h => undef,
236 can_events => 1, 235 can_events => 1,
237 @_ 236 @_
238 }, $class; 237 }, $class;
239 238
240 Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 239 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
241 240
242 for (keys %$self) { 241 for (keys %$self) {
243 if (/^on_(.*)$/) { 242 if (/^on_(.*)$/) {
244 $self->connect ($1 => delete $self->{$_}); 243 $self->connect ($1 => delete $self->{$_});
245 } 244 }
262 261
263sub destroy { 262sub destroy {
264 my ($self) = @_; 263 my ($self) = @_;
265 264
266 $self->hide; 265 $self->hide;
266 $self->emit ("destroy");
267 %$self = (); 267 %$self = ();
268} 268}
269 269
270sub show { 270sub show {
271 my ($self) = @_; 271 my ($self) = @_;
502 502
503 defined wantarray and CFPlus::guard { 503 defined wantarray and CFPlus::guard {
504 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 504 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
505 @{ $self->{signal_cb}{$signal} }; 505 @{ $self->{signal_cb}{$signal} };
506 } 506 }
507}
508
509sub disconnect_all {
510 my ($self, $signal) = @_;
511
512 delete $self->{signal_cb}{$signal};
507} 513}
508 514
509my %has_coords = ( 515my %has_coords = (
510 button_down => 1, 516 button_down => 1,
511 button_up => 1, 517 button_up => 1,
523 ? $args[0]->xy ($self) 529 ? $args[0]->xy ($self)
524 : (); 530 : ();
525 531
526 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 532 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
527 533
528 #d##TODO# stop propagating at first true, do not use sum 534 for my $cb (
529 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 535 @{$self->{signal_cb}{$signal} || []}, # before
530 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 536 ($self->can ("invoke_$signal") || sub { 1 }), # closure
537 ) {
538 return $cb->($self, @args, @append) || next;
539 }
540
541 # parent
531 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 542 $self->{parent} && $self->{parent}->emit ($signal, @args)
532} 543}
533 544
534sub find_widget { 545sub find_widget {
535 my ($self, $x, $y) = @_; 546 my ($self, $x, $y) = @_;
536 547
544} 555}
545 556
546sub set_parent { 557sub set_parent {
547 my ($self, $parent) = @_; 558 my ($self, $parent) = @_;
548 559
549 Scalar::Util::weaken ($self->{parent} = $parent); 560 CFPlus::weaken ($self->{parent} = $parent);
550 $self->set_visible if $parent->{visible}; 561 $self->set_visible if $parent->{visible};
551} 562}
552 563
553sub realloc { 564sub realloc {
554 my ($self) = @_; 565 my ($self) = @_;
633 my ($self) = @_; 644 my ($self) = @_;
634 645
635 warn "no draw defined for $self\n"; 646 warn "no draw defined for $self\n";
636} 647}
637 648
649my $cntx;#d#
638sub DESTROY { 650sub DESTROY {
639 my ($self) = @_; 651 my ($self) = @_;
640 652
641 return if CFPlus::in_destruct; 653 return if CFPlus::in_destruct;
642
643 delete $WIDGET{$self+0};
644 654
645 eval { $self->destroy }; 655 eval { $self->destroy };
646 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 656 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
657
658 delete $WIDGET{$self+0};
647} 659}
648 660
649############################################################################# 661#############################################################################
650 662
651package CFPlus::UI::DrawBG; 663package CFPlus::UI::DrawBG;
1900 active_fg => [0, 0, 0], 1912 active_fg => [0, 0, 0],
1901 can_hover => 1, 1913 can_hover => 1,
1902 can_focus => 1, 1914 can_focus => 1,
1903 valign => 0, 1915 valign => 0,
1904 can_events => 1, 1916 can_events => 1,
1917 ellipsise => 0,
1905 #text => ... 1918 #text => ...
1906 #hidden => "*", 1919 #hidden => "*",
1907 @_ 1920 @_
1908 ) 1921 )
1909} 1922}
1963 } elsif ($sym == CFPlus::SDLK_LEFT) { 1976 } elsif ($sym == CFPlus::SDLK_LEFT) {
1964 --$self->{cursor} if $self->{cursor}; 1977 --$self->{cursor} if $self->{cursor};
1965 } elsif ($sym == CFPlus::SDLK_RIGHT) { 1978 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1966 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1979 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1967 } elsif ($sym == CFPlus::SDLK_HOME) { 1980 } elsif ($sym == CFPlus::SDLK_HOME) {
1981 # what a hack
1968 $self->{cursor} = 0; 1982 $self->{cursor} =
1983 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
1984 ? length $1
1985 : 0;
1969 } elsif ($sym == CFPlus::SDLK_END) { 1986 } elsif ($sym == CFPlus::SDLK_END) {
1987 # uh, again
1988 $self->{cursor} =
1989 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
1970 $self->{cursor} = length $text; 1990 ? $self->{cursor} + length $1
1991 : length $self->{text};
1971 } elsif ($uni == 21) { # ctrl-u 1992 } elsif ($uni == 21) { # ctrl-u
1972 $text = ""; 1993 $text = "";
1973 $self->{cursor} = 0; 1994 $self->{cursor} = 0;
1974 } elsif ($uni == 27) { 1995 } elsif ($uni == 27) {
1975 $self->emit ('escape'); 1996 $self->emit ('escape');
1976 } elsif ($uni) { 1997 } elsif ($uni == 0x0d) {
1998 substr $text, $self->{cursor}++, 0, "\012";
1999 } elsif ($uni >= 0x20) {
1977 substr $text, $self->{cursor}++, 0, chr $uni; 2000 substr $text, $self->{cursor}++, 0, chr $uni;
1978 } else { 2001 } else {
1979 return 0; 2002 return 0;
1980 } 2003 }
1981 2004
1982 $self->_set_text ($text); 2005 $self->_set_text ($text);
1983 2006
1984 $self->realloc; 2007 $self->realloc;
2008 $self->update;
1985 2009
1986 1 2010 1
1987} 2011}
1988 2012
1989sub invoke_focus_in { 2013sub invoke_focus_in {
2058 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2082 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2059 glEnd; 2083 glEnd;
2060 } 2084 }
2061} 2085}
2062 2086
2087#############################################################################
2088
2063package CFPlus::UI::Entry; 2089package CFPlus::UI::Entry;
2064 2090
2065our @ISA = CFPlus::UI::EntryBase::; 2091our @ISA = CFPlus::UI::EntryBase::;
2066 2092
2067use CFPlus::OpenGL; 2093use CFPlus::OpenGL;
2069sub invoke_key_down { 2095sub invoke_key_down {
2070 my ($self, $ev) = @_; 2096 my ($self, $ev) = @_;
2071 2097
2072 my $sym = $ev->{sym}; 2098 my $sym = $ev->{sym};
2073 2099
2074 if ($sym == 13) { 2100 if ($ev->{uni} == 0x0d || $sym == 13) {
2075 unshift @{$self->{history}}, 2101 unshift @{$self->{history}},
2076 my $txt = $self->get_text; 2102 my $txt = $self->get_text;
2077 2103
2078 $self->{history_pointer} = -1; 2104 $self->{history_pointer} = -1;
2079 $self->{history_saveback} = ''; 2105 $self->{history_saveback} = '';
2109 1 2135 1
2110} 2136}
2111 2137
2112############################################################################# 2138#############################################################################
2113 2139
2140package CFPlus::UI::TextEdit;
2141
2142our @ISA = CFPlus::UI::EntryBase::;
2143
2144use CFPlus::OpenGL;
2145
2146sub move_cursor_ver {
2147 my ($self, $dy) = @_;
2148
2149 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2150
2151 $y += $dy;
2152
2153 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2154 $self->{cursor} = $index;
2155 delete $self->{cur_h};
2156 $self->update;
2157 return;
2158 }
2159}
2160
2161sub invoke_key_down {
2162 my ($self, $ev) = @_;
2163
2164 my $sym = $ev->{sym};
2165
2166 if ($sym == CFPlus::SDLK_UP) {
2167 $self->move_cursor_ver (-1);
2168 } elsif ($sym == CFPlus::SDLK_DOWN) {
2169 $self->move_cursor_ver (+1);
2170 } else {
2171 return $self->SUPER::invoke_key_down ($ev)
2172 }
2173
2174 1
2175}
2176
2177#############################################################################
2178
2114package CFPlus::UI::Button; 2179package CFPlus::UI::Button;
2115 2180
2116our @ISA = CFPlus::UI::Label::; 2181our @ISA = CFPlus::UI::Label::;
2117 2182
2118use CFPlus::OpenGL; 2183use CFPlus::OpenGL;
2260 or Carp::croak "'path' or 'tex' attributes required"; 2325 or Carp::croak "'path' or 'tex' attributes required";
2261 2326
2262 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2327 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2263 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2328 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2264 2329
2265 Scalar::Util::weaken $texture_cache{$self->{path}}; 2330 CFPlus::weaken $texture_cache{$self->{path}};
2266 2331
2267 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2332 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2268 2333
2269 $self 2334 $self
2270} 2335}
3218 can_events => 0, 3283 can_events => 0,
3219 @_, 3284 @_,
3220 ); 3285 );
3221 3286
3222 if ($self->{anim} && $self->{animspeed}) { 3287 if ($self->{anim} && $self->{animspeed}) {
3223 Scalar::Util::weaken (my $widget = $self); 3288 CFPlus::weaken (my $widget = $self);
3224 3289
3225 $self->{timer} = Event->timer ( 3290 $self->{timer} = Event->timer (
3226 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3291 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3227 hard => 1, 3292 hard => 1,
3228 interval => $self->{animspeed}, 3293 interval => $self->{animspeed},
3482} 3547}
3483 3548
3484sub add { 3549sub add {
3485 my ($self, $title, $widget, $tooltip) = @_; 3550 my ($self, $title, $widget, $tooltip) = @_;
3486 3551
3487 Scalar::Util::weaken $self; 3552 CFPlus::weaken $self;
3488 3553
3489 $self->{buttonbar}->add (new CFPlus::UI::Button 3554 $self->{buttonbar}->add (new CFPlus::UI::Button
3490 markup => $title, 3555 markup => $title,
3491 tooltip => $tooltip, 3556 tooltip => $tooltip,
3492 on_activate => sub { $self->set_current_page ($widget) }, 3557 on_activate => sub { $self->set_current_page ($widget) },
3576 my $self = $class->SUPER::new ( 3641 my $self = $class->SUPER::new (
3577 fontsize => 0.8, 3642 fontsize => 0.8,
3578 @_, 3643 @_,
3579 ); 3644 );
3580 3645
3581 Scalar::Util::weaken (my $this = $self); 3646 CFPlus::weaken (my $this = $self);
3582 3647
3583 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3648 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3584 3649
3585 $self 3650 $self
3586} 3651}
3709 $self->SUPER::destroy; 3774 $self->SUPER::destroy;
3710} 3775}
3711 3776
3712############################################################################# 3777#############################################################################
3713 3778
3714package CFPlus::UI::Inventory;
3715
3716our @ISA = CFPlus::UI::Table::;
3717
3718sub new {
3719 my $class = shift;
3720
3721 my $self = $class->SUPER::new (
3722 col_expand => [0, 1, 0],
3723 items => [],
3724 @_,
3725 );
3726
3727 $self->set_sort_order (undef);
3728
3729 $self
3730}
3731
3732sub update_items {
3733 my ($self) = @_;
3734
3735 $self->clear;
3736
3737 my @item = $self->{sort}->(@{ $self->{items} });
3738
3739 my @adds;
3740 my $row = 0;
3741 for my $item ($self->{sort}->(@{ $self->{items} })) {
3742 CFPlus::Item::update_widgets $item;
3743
3744 push @adds, 0, $row, $item->{face_widget};
3745 push @adds, 1, $row, $item->{desc_widget};
3746 push @adds, 2, $row, $item->{weight_widget};
3747
3748 $row++;
3749 }
3750
3751 $self->add (@adds);
3752}
3753
3754sub set_sort_order {
3755 my ($self, $order) = @_;
3756
3757 $self->{sort} = $order ||= sub {
3758 sort {
3759 $a->{type} <=> $b->{type}
3760 or $a->{name} cmp $b->{name}
3761 } @_
3762 };
3763
3764 $self->update_items;
3765}
3766
3767sub set_items {
3768 my ($self, $items) = @_;
3769
3770 $self->{items} = [$items ? values %$items : ()];
3771 $self->update_items;
3772}
3773
3774#############################################################################
3775
3776package CFPlus::UI::SpellList;
3777
3778our @ISA = CFPlus::UI::Table::;
3779
3780sub new {
3781 my $class = shift;
3782
3783 my $self = $class->SUPER::new (
3784 binding => [],
3785 commands => [],
3786 @_,
3787 )
3788}
3789
3790my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3791
3792my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3793 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3794my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3795 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3796my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3797 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3798my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3799 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3800my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3801 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3802
3803sub rebuild_spell_list {
3804 my ($self) = @_;
3805
3806 $CFPlus::UI::ROOT->on_refresh ($self => sub {
3807 $self->clear;
3808
3809 return unless $::CONN;
3810
3811 $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3812 $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL);
3813 $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3814 $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3815 $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3816
3817 my $row = 0;
3818
3819 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3820 my $spell = $self->{spell}{$_};
3821
3822 $row++;
3823
3824 my $spell_cb = sub {
3825 my ($widget, $ev) = @_;
3826
3827 if ($ev->{button} == 1) {
3828 $::CONN->user_send ("cast $spell->{name}");
3829 } elsif ($ev->{button} == 2) {
3830 $::CONN->user_send ("invoke $spell->{name}");
3831 } elsif ($ev->{button} == 3) {
3832 my $shortname = CFPlus::shorten $spell->{name}, 14;
3833 (new CFPlus::UI::Menu
3834 items => [
3835 ["bind <i>cast $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3836 ["bind <i>invoke $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3837 ],
3838 )->popup ($ev);
3839 } else {
3840 return 0;
3841 }
3842
3843 1
3844 };
3845
3846 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3847
3848 #TODO: add path info to tooltip
3849 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3850
3851 $self->add (0, $row, new CFPlus::UI::Face
3852 face => $spell->{face},
3853 can_hover => 1,
3854 can_events => 1,
3855 tooltip => $tooltip,
3856 on_button_down => $spell_cb,
3857 );
3858
3859 $self->add (1, $row, new CFPlus::UI::Label
3860 expand => 1,
3861 text => $spell->{name},
3862 can_hover => 1,
3863 can_events => 1,
3864 tooltip => $tooltip,
3865 on_button_down => $spell_cb,
3866 );
3867
3868 $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3869 $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3870 $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3871 $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3872 }
3873 });
3874}
3875
3876sub add_spell {
3877 my ($self, $spell) = @_;
3878
3879 $self->{spell}->{$spell->{name}} = $spell;
3880 $self->rebuild_spell_list;
3881}
3882
3883sub remove_spell {
3884 my ($self, $spell) = @_;
3885
3886 delete $self->{spell}->{$spell->{name}};
3887 $self->rebuild_spell_list;
3888}
3889
3890sub clear_spells {
3891 my ($self) = @_;
3892
3893 $self->{spell} = {};
3894 $self->rebuild_spell_list;
3895}
3896
3897#############################################################################
3898
3899package CFPlus::UI::Root; 3779package CFPlus::UI::Root;
3900 3780
3901our @ISA = CFPlus::UI::Container::; 3781our @ISA = CFPlus::UI::Container::;
3902 3782
3903use List::Util qw(min max); 3783use List::Util qw(min max);
3910 my $self = $class->SUPER::new ( 3790 my $self = $class->SUPER::new (
3911 visible => 1, 3791 visible => 1,
3912 @_, 3792 @_,
3913 ); 3793 );
3914 3794
3915 Scalar::Util::weaken ($self->{root} = $self); 3795 CFPlus::weaken ($self->{root} = $self);
3916 3796
3917 $self 3797 $self
3918} 3798}
3919 3799
3920sub size_request { 3800sub size_request {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines