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.351 by root, Sun Oct 1 12:08:58 2006 UTC vs.
Revision 1.362 by root, Sat Dec 9 21:26:46 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,
514); 520);
515 521
516sub emit { 522sub emit {
517 my ($self, $signal, @args) = @_; 523 my ($self, $signal, @args) = @_;
518 524
519 # I do not really like this solution, but I dislike duplication 525 # I do not really like this solution, but I do not like duplication
520 # and needlessly verbose code, too. 526 # and needlessly verbose code, either.
521 my @append 527 my @append
522 = $has_coords{$signal} 528 = $has_coords{$signal}
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;
900 { 912 {
901 package CFPlus::UI::Base; 913 package CFPlus::UI::Base;
902 914
903 local ($draw_x, $draw_y, $draw_w, $draw_h) = 915 local ($draw_x, $draw_y, $draw_w, $draw_h) =
904 (0, 0, $self->{w}, $self->{h}); 916 (0, 0, $self->{w}, $self->{h});
917
918 $self->_render;
905 } 919 }
906
907 $self->_render;
908 }; 920 };
909} 921}
910 922
911sub _draw { 923sub _draw {
912 my ($self) = @_; 924 my ($self) = @_;
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}
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) {
1968 $self->{cursor} = 0; 1981 $self->{cursor} = 0;
1969 } elsif ($sym == CFPlus::SDLK_END) { 1982 } elsif ($sym == CFPlus::SDLK_END) {
1970 $self->{cursor} = length $text; 1983 $self->{cursor} = length $text;
1971 } elsif ($uni == 21) { # ctrl-u 1984 } elsif ($uni == 21) { # ctrl-u
1972 $text = ""; 1985 $text = "";
1973 $self->{cursor} = 0; 1986 $self->{cursor} = 0;
1974 } elsif ($uni == 27) { 1987 } elsif ($uni == 27) {
1975 $self->emit ('escape'); 1988 $self->emit ('escape');
1976 } elsif ($uni) { 1989 } elsif ($uni >= 0x20 || $uni == 0x0d) {
1977 substr $text, $self->{cursor}++, 0, chr $uni; 1990 substr $text, $self->{cursor}++, 0, chr $uni;
1978 } else { 1991 } else {
1979 return 0; 1992 return 0;
1980 } 1993 }
1981 1994
1982 $self->_set_text ($text); 1995 $self->_set_text ($text);
1983 1996
1984 $self->realloc; 1997 $self->realloc;
1998 $self->update;
1985 1999
1986 1 2000 1
1987} 2001}
1988 2002
1989sub invoke_focus_in { 2003sub invoke_focus_in {
2058 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2072 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2059 glEnd; 2073 glEnd;
2060 } 2074 }
2061} 2075}
2062 2076
2077#############################################################################
2078
2063package CFPlus::UI::Entry; 2079package CFPlus::UI::Entry;
2064 2080
2065our @ISA = CFPlus::UI::EntryBase::; 2081our @ISA = CFPlus::UI::EntryBase::;
2066 2082
2067use CFPlus::OpenGL; 2083use CFPlus::OpenGL;
2069sub invoke_key_down { 2085sub invoke_key_down {
2070 my ($self, $ev) = @_; 2086 my ($self, $ev) = @_;
2071 2087
2072 my $sym = $ev->{sym}; 2088 my $sym = $ev->{sym};
2073 2089
2074 if ($sym == 13) { 2090 if ($ev->{uni} == 0x0d || $sym == 13) {
2075 unshift @{$self->{history}}, 2091 unshift @{$self->{history}},
2076 my $txt = $self->get_text; 2092 my $txt = $self->get_text;
2077 2093
2078 $self->{history_pointer} = -1; 2094 $self->{history_pointer} = -1;
2079 $self->{history_saveback} = ''; 2095 $self->{history_saveback} = '';
2109 1 2125 1
2110} 2126}
2111 2127
2112############################################################################# 2128#############################################################################
2113 2129
2130package CFPlus::UI::TextEdit;
2131
2132our @ISA = CFPlus::UI::EntryBase::;
2133
2134use CFPlus::OpenGL;
2135
2136sub move_cursor_ver {
2137 my ($self, $dy) = @_;
2138
2139 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2140
2141 $y += $dy;
2142
2143 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2144 $self->{cursor} = $index;
2145 delete $self->{cur_h};
2146 $self->update;
2147 return;
2148 }
2149}
2150
2151sub invoke_key_down {
2152 my ($self, $ev) = @_;
2153
2154 my $sym = $ev->{sym};
2155
2156 if ($sym == CFPlus::SDLK_UP) {
2157 $self->move_cursor_ver (-1);
2158 } elsif ($sym == CFPlus::SDLK_DOWN) {
2159 $self->move_cursor_ver (+1);
2160 } else {
2161 return $self->SUPER::invoke_key_down ($ev)
2162 }
2163
2164 1
2165}
2166
2167#############################################################################
2168
2114package CFPlus::UI::Button; 2169package CFPlus::UI::Button;
2115 2170
2116our @ISA = CFPlus::UI::Label::; 2171our @ISA = CFPlus::UI::Label::;
2117 2172
2118use CFPlus::OpenGL; 2173use CFPlus::OpenGL;
2260 or Carp::croak "'path' or 'tex' attributes required"; 2315 or Carp::croak "'path' or 'tex' attributes required";
2261 2316
2262 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2317 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2263 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2318 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2264 2319
2265 Scalar::Util::weaken $texture_cache{$self->{path}}; 2320 CFPlus::weaken $texture_cache{$self->{path}};
2266 2321
2267 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2322 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2268 2323
2269 $self 2324 $self
2270} 2325}
2959 3014
2960 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3015 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2961 glClearColor 0, 0, 0, 0; 3016 glClearColor 0, 0, 0, 0;
2962 glClear GL_COLOR_BUFFER_BIT; 3017 glClear GL_COLOR_BUFFER_BIT;
2963 3018
2964 {
2965 package CFPlus::UI::Base; 3019 package CFPlus::UI::Base;
2966
2967 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3020 local ($draw_x, $draw_y, $draw_w, $draw_h) =
2968 (0, 0, $self->{w}, $self->{h}); 3021 (0, 0, $self->{w}, $self->{h});
2969 }
2970 3022
2971 my $top = int $self->{children}[1]{range}[0]; 3023 my $top = int $self->{children}[1]{range}[0];
2972 3024
2973 my $paridx = 0; 3025 my $paridx = 0;
2974 my $top_paragraph; 3026 my $top_paragraph;
3221 can_events => 0, 3273 can_events => 0,
3222 @_, 3274 @_,
3223 ); 3275 );
3224 3276
3225 if ($self->{anim} && $self->{animspeed}) { 3277 if ($self->{anim} && $self->{animspeed}) {
3226 Scalar::Util::weaken (my $widget = $self); 3278 CFPlus::weaken (my $widget = $self);
3227 3279
3228 $self->{timer} = Event->timer ( 3280 $self->{timer} = Event->timer (
3229 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3281 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3230 hard => 1, 3282 hard => 1,
3231 interval => $self->{animspeed}, 3283 interval => $self->{animspeed},
3291 3343
3292package CFPlus::UI::Buttonbar; 3344package CFPlus::UI::Buttonbar;
3293 3345
3294our @ISA = CFPlus::UI::HBox::; 3346our @ISA = CFPlus::UI::HBox::;
3295 3347
3296# TODO: should actualyl wrap buttons and other goodies. 3348# TODO: should actually wrap buttons and other goodies.
3297 3349
3298############################################################################# 3350#############################################################################
3299 3351
3300package CFPlus::UI::Menu; 3352package CFPlus::UI::Menu;
3301 3353
3485} 3537}
3486 3538
3487sub add { 3539sub add {
3488 my ($self, $title, $widget, $tooltip) = @_; 3540 my ($self, $title, $widget, $tooltip) = @_;
3489 3541
3490 Scalar::Util::weaken $self; 3542 CFPlus::weaken $self;
3491 3543
3492 $self->{buttonbar}->add (new CFPlus::UI::Button 3544 $self->{buttonbar}->add (new CFPlus::UI::Button
3493 markup => $title, 3545 markup => $title,
3494 tooltip => $tooltip, 3546 tooltip => $tooltip,
3495 on_activate => sub { $self->set_current_page ($widget) }, 3547 on_activate => sub { $self->set_current_page ($widget) },
3579 my $self = $class->SUPER::new ( 3631 my $self = $class->SUPER::new (
3580 fontsize => 0.8, 3632 fontsize => 0.8,
3581 @_, 3633 @_,
3582 ); 3634 );
3583 3635
3584 Scalar::Util::weaken (my $this = $self); 3636 CFPlus::weaken (my $this = $self);
3585 3637
3586 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3638 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3587 3639
3588 $self 3640 $self
3589} 3641}
3712 $self->SUPER::destroy; 3764 $self->SUPER::destroy;
3713} 3765}
3714 3766
3715############################################################################# 3767#############################################################################
3716 3768
3717package CFPlus::UI::Inventory;
3718
3719our @ISA = CFPlus::UI::Table::;
3720
3721sub new {
3722 my $class = shift;
3723
3724 my $self = $class->SUPER::new (
3725 col_expand => [0, 1, 0],
3726 items => [],
3727 @_,
3728 );
3729
3730 $self->set_sort_order (undef);
3731
3732 $self
3733}
3734
3735sub update_items {
3736 my ($self) = @_;
3737
3738 $self->clear;
3739
3740 my @item = $self->{sort}->(@{ $self->{items} });
3741
3742 my @adds;
3743 my $row = 0;
3744 for my $item ($self->{sort}->(@{ $self->{items} })) {
3745 CFPlus::Item::update_widgets $item;
3746
3747 push @adds, 0, $row, $item->{face_widget};
3748 push @adds, 1, $row, $item->{desc_widget};
3749 push @adds, 2, $row, $item->{weight_widget};
3750
3751 $row++;
3752 }
3753
3754 $self->add (@adds);
3755}
3756
3757sub set_sort_order {
3758 my ($self, $order) = @_;
3759
3760 $self->{sort} = $order ||= sub {
3761 sort {
3762 $a->{type} <=> $b->{type}
3763 or $a->{name} cmp $b->{name}
3764 } @_
3765 };
3766
3767 $self->update_items;
3768}
3769
3770sub set_items {
3771 my ($self, $items) = @_;
3772
3773 $self->{items} = [$items ? values %$items : ()];
3774 $self->update_items;
3775}
3776
3777#############################################################################
3778
3779package CFPlus::UI::SpellList;
3780
3781our @ISA = CFPlus::UI::Table::;
3782
3783sub new {
3784 my $class = shift;
3785
3786 my $self = $class->SUPER::new (
3787 binding => [],
3788 commands => [],
3789 @_,
3790 )
3791}
3792
3793my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3794
3795my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3796 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3797my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3798 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3799my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3800 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3801my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3802 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3803my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3804 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3805
3806sub rebuild_spell_list {
3807 my ($self) = @_;
3808
3809 $CFPlus::UI::ROOT->on_refresh ($self => sub {
3810 $self->clear;
3811
3812 return unless $::CONN;
3813
3814 $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3815 $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL);
3816 $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3817 $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3818 $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3819
3820 my $row = 0;
3821
3822 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3823 my $spell = $self->{spell}{$_};
3824
3825 $row++;
3826
3827 my $spell_cb = sub {
3828 my ($widget, $ev) = @_;
3829
3830 if ($ev->{button} == 1) {
3831 $::CONN->user_send ("cast $spell->{name}");
3832 } elsif ($ev->{button} == 2) {
3833 $::CONN->user_send ("invoke $spell->{name}");
3834 } elsif ($ev->{button} == 3) {
3835 (new CFPlus::UI::Menu
3836 items => [
3837 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3838 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3839 ],
3840 )->popup ($ev);
3841 } else {
3842 return 0;
3843 }
3844
3845 1
3846 };
3847
3848 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3849
3850 #TODO: add path info to tooltip
3851 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3852
3853 $self->add (0, $row, new CFPlus::UI::Face
3854 face => $spell->{face},
3855 can_hover => 1,
3856 can_events => 1,
3857 tooltip => $tooltip,
3858 on_button_down => $spell_cb,
3859 );
3860
3861 $self->add (1, $row, new CFPlus::UI::Label
3862 expand => 1,
3863 text => $spell->{name},
3864 can_hover => 1,
3865 can_events => 1,
3866 tooltip => $tooltip,
3867 on_button_down => $spell_cb,
3868 );
3869
3870 $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3871 $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3872 $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3873 $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3874 }
3875 });
3876}
3877
3878sub add_spell {
3879 my ($self, $spell) = @_;
3880
3881 $self->{spell}->{$spell->{name}} = $spell;
3882 $self->rebuild_spell_list;
3883}
3884
3885sub remove_spell {
3886 my ($self, $spell) = @_;
3887
3888 delete $self->{spell}->{$spell->{name}};
3889 $self->rebuild_spell_list;
3890}
3891
3892sub clear_spells {
3893 my ($self) = @_;
3894
3895 $self->{spell} = {};
3896 $self->rebuild_spell_list;
3897}
3898
3899#############################################################################
3900
3901package CFPlus::UI::Root; 3769package CFPlus::UI::Root;
3902 3770
3903our @ISA = CFPlus::UI::Container::; 3771our @ISA = CFPlus::UI::Container::;
3904 3772
3905use List::Util qw(min max); 3773use List::Util qw(min max);
3912 my $self = $class->SUPER::new ( 3780 my $self = $class->SUPER::new (
3913 visible => 1, 3781 visible => 1,
3914 @_, 3782 @_,
3915 ); 3783 );
3916 3784
3917 Scalar::Util::weaken ($self->{root} = $self); 3785 CFPlus::weaken ($self->{root} = $self);
3918 3786
3919 $self 3787 $self
3920} 3788}
3921 3789
3922sub size_request { 3790sub size_request {
4133 { 4001 {
4134 package CFPlus::UI::Base; 4002 package CFPlus::UI::Base;
4135 4003
4136 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4004 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4137 (0, 0, $self->{w}, $self->{h}); 4005 (0, 0, $self->{w}, $self->{h});
4138 }
4139 4006
4140 $self->_draw; 4007 $self->_draw;
4008 }
4141} 4009}
4142 4010
4143############################################################################# 4011#############################################################################
4144 4012
4145package CFPlus::UI; 4013package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines