… | |
… | |
262 | |
262 | |
263 | sub destroy { |
263 | sub destroy { |
264 | my ($self) = @_; |
264 | my ($self) = @_; |
265 | |
265 | |
266 | $self->hide; |
266 | $self->hide; |
|
|
267 | $self->emit ("destroy"); |
267 | %$self = (); |
268 | %$self = (); |
268 | } |
269 | } |
269 | |
270 | |
270 | sub show { |
271 | sub show { |
271 | my ($self) = @_; |
272 | my ($self) = @_; |
… | |
… | |
502 | |
503 | |
503 | defined wantarray and CFPlus::guard { |
504 | defined wantarray and CFPlus::guard { |
504 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
505 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
505 | @{ $self->{signal_cb}{$signal} }; |
506 | @{ $self->{signal_cb}{$signal} }; |
506 | } |
507 | } |
|
|
508 | } |
|
|
509 | |
|
|
510 | sub disconnect_all { |
|
|
511 | my ($self, $signal) = @_; |
|
|
512 | |
|
|
513 | delete $self->{signal_cb}{$signal}; |
507 | } |
514 | } |
508 | |
515 | |
509 | my %has_coords = ( |
516 | my %has_coords = ( |
510 | button_down => 1, |
517 | button_down => 1, |
511 | button_up => 1, |
518 | button_up => 1, |
… | |
… | |
523 | ? $args[0]->xy ($self) |
530 | ? $args[0]->xy ($self) |
524 | : (); |
531 | : (); |
525 | |
532 | |
526 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
533 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
527 | |
534 | |
528 | if ($self->{tooltip} =~ /Apply.*audio/) { |
|
|
529 | warn "2 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d# |
|
|
530 | warn "3 $self->{parent}\n";#d# |
|
|
531 | } |
|
|
532 | |
|
|
533 | #d##TODO# stop propagating at first true, do not use sum |
535 | #d##TODO# stop propagating at first true, do not use sum |
534 | my $res = |
|
|
535 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
536 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
536 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
537 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
537 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
538 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
538 | ; |
|
|
539 | if ($self->{tooltip} =~ /Apply.*audio/) { |
|
|
540 | warn "4 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d# |
|
|
541 | warn "5 $self->{parent}\n";#d# |
|
|
542 | } |
|
|
543 | |
|
|
544 | $res |
|
|
545 | } |
539 | } |
546 | |
540 | |
547 | sub find_widget { |
541 | sub find_widget { |
548 | my ($self, $x, $y) = @_; |
542 | my ($self, $x, $y) = @_; |
549 | |
543 | |
… | |
… | |
651 | sub DESTROY { |
645 | sub DESTROY { |
652 | my ($self) = @_; |
646 | my ($self) = @_; |
653 | |
647 | |
654 | return if CFPlus::in_destruct; |
648 | return if CFPlus::in_destruct; |
655 | |
649 | |
656 | delete $WIDGET{$self+0}; |
|
|
657 | |
|
|
658 | eval { $self->destroy }; |
650 | eval { $self->destroy }; |
659 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
651 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
|
|
652 | |
|
|
653 | delete $WIDGET{$self+0}; |
660 | } |
654 | } |
661 | |
655 | |
662 | ############################################################################# |
656 | ############################################################################# |
663 | |
657 | |
664 | package CFPlus::UI::DrawBG; |
658 | package CFPlus::UI::DrawBG; |
… | |
… | |
1913 | active_fg => [0, 0, 0], |
1907 | active_fg => [0, 0, 0], |
1914 | can_hover => 1, |
1908 | can_hover => 1, |
1915 | can_focus => 1, |
1909 | can_focus => 1, |
1916 | valign => 0, |
1910 | valign => 0, |
1917 | can_events => 1, |
1911 | can_events => 1, |
|
|
1912 | ellipsise => 0, |
1918 | #text => ... |
1913 | #text => ... |
1919 | #hidden => "*", |
1914 | #hidden => "*", |
1920 | @_ |
1915 | @_ |
1921 | ) |
1916 | ) |
1922 | } |
1917 | } |
… | |
… | |
1979 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1974 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1980 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1975 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1981 | $self->{cursor} = 0; |
1976 | $self->{cursor} = 0; |
1982 | } elsif ($sym == CFPlus::SDLK_END) { |
1977 | } elsif ($sym == CFPlus::SDLK_END) { |
1983 | $self->{cursor} = length $text; |
1978 | $self->{cursor} = length $text; |
1984 | } elsif ($uni == 21) { # ctrl-u |
1979 | } elsif ($uni == 21) { # ctrl-u |
1985 | $text = ""; |
1980 | $text = ""; |
1986 | $self->{cursor} = 0; |
1981 | $self->{cursor} = 0; |
1987 | } elsif ($uni == 27) { |
1982 | } elsif ($uni == 27) { |
1988 | $self->emit ('escape'); |
1983 | $self->emit ('escape'); |
1989 | } elsif ($uni) { |
1984 | } elsif ($uni >= 0x20 || $uni == 0x0d) { |
1990 | substr $text, $self->{cursor}++, 0, chr $uni; |
1985 | substr $text, $self->{cursor}++, 0, chr $uni; |
1991 | } else { |
1986 | } else { |
1992 | return 0; |
1987 | return 0; |
1993 | } |
1988 | } |
1994 | |
1989 | |
1995 | $self->_set_text ($text); |
1990 | $self->_set_text ($text); |
1996 | |
1991 | |
1997 | $self->realloc; |
1992 | $self->realloc; |
|
|
1993 | $self->update; |
1998 | |
1994 | |
1999 | 1 |
1995 | 1 |
2000 | } |
1996 | } |
2001 | |
1997 | |
2002 | sub invoke_focus_in { |
1998 | sub invoke_focus_in { |
… | |
… | |
2071 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2067 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2072 | glEnd; |
2068 | glEnd; |
2073 | } |
2069 | } |
2074 | } |
2070 | } |
2075 | |
2071 | |
|
|
2072 | ############################################################################# |
|
|
2073 | |
2076 | package CFPlus::UI::Entry; |
2074 | package CFPlus::UI::Entry; |
2077 | |
2075 | |
2078 | our @ISA = CFPlus::UI::EntryBase::; |
2076 | our @ISA = CFPlus::UI::EntryBase::; |
2079 | |
2077 | |
2080 | use CFPlus::OpenGL; |
2078 | use CFPlus::OpenGL; |
… | |
… | |
2082 | sub invoke_key_down { |
2080 | sub invoke_key_down { |
2083 | my ($self, $ev) = @_; |
2081 | my ($self, $ev) = @_; |
2084 | |
2082 | |
2085 | my $sym = $ev->{sym}; |
2083 | my $sym = $ev->{sym}; |
2086 | |
2084 | |
2087 | if ($sym == 13) { |
2085 | if ($ev->{uni} == 0x0d || $sym == 13) { |
2088 | unshift @{$self->{history}}, |
2086 | unshift @{$self->{history}}, |
2089 | my $txt = $self->get_text; |
2087 | my $txt = $self->get_text; |
2090 | |
2088 | |
2091 | $self->{history_pointer} = -1; |
2089 | $self->{history_pointer} = -1; |
2092 | $self->{history_saveback} = ''; |
2090 | $self->{history_saveback} = ''; |
… | |
… | |
2113 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2111 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2114 | } else { |
2112 | } else { |
2115 | $self->set_text ($self->{history_saveback}); |
2113 | $self->set_text ($self->{history_saveback}); |
2116 | } |
2114 | } |
2117 | |
2115 | |
|
|
2116 | } else { |
|
|
2117 | return $self->SUPER::invoke_key_down ($ev) |
|
|
2118 | } |
|
|
2119 | |
|
|
2120 | 1 |
|
|
2121 | } |
|
|
2122 | |
|
|
2123 | ############################################################################# |
|
|
2124 | |
|
|
2125 | package CFPlus::UI::TextEdit; |
|
|
2126 | |
|
|
2127 | our @ISA = CFPlus::UI::EntryBase::; |
|
|
2128 | |
|
|
2129 | use CFPlus::OpenGL; |
|
|
2130 | |
|
|
2131 | sub move_cursor_ver { |
|
|
2132 | my ($self, $dy) = @_; |
|
|
2133 | |
|
|
2134 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
|
|
2135 | |
|
|
2136 | $y += $dy; |
|
|
2137 | |
|
|
2138 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
|
|
2139 | $self->{cursor} = $index; |
|
|
2140 | delete $self->{cur_h}; |
|
|
2141 | $self->update; |
|
|
2142 | return; |
|
|
2143 | } |
|
|
2144 | } |
|
|
2145 | |
|
|
2146 | sub invoke_key_down { |
|
|
2147 | my ($self, $ev) = @_; |
|
|
2148 | |
|
|
2149 | my $sym = $ev->{sym}; |
|
|
2150 | |
|
|
2151 | if ($sym == CFPlus::SDLK_UP) { |
|
|
2152 | $self->move_cursor_ver (-1); |
|
|
2153 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
|
|
2154 | $self->move_cursor_ver (+1); |
2118 | } else { |
2155 | } else { |
2119 | return $self->SUPER::invoke_key_down ($ev) |
2156 | return $self->SUPER::invoke_key_down ($ev) |
2120 | } |
2157 | } |
2121 | |
2158 | |
2122 | 1 |
2159 | 1 |
… | |
… | |
3722 | $self->SUPER::destroy; |
3759 | $self->SUPER::destroy; |
3723 | } |
3760 | } |
3724 | |
3761 | |
3725 | ############################################################################# |
3762 | ############################################################################# |
3726 | |
3763 | |
3727 | package CFPlus::UI::Inventory; |
|
|
3728 | |
|
|
3729 | our @ISA = CFPlus::UI::Table::; |
|
|
3730 | |
|
|
3731 | sub new { |
|
|
3732 | my $class = shift; |
|
|
3733 | |
|
|
3734 | my $self = $class->SUPER::new ( |
|
|
3735 | col_expand => [0, 1, 0], |
|
|
3736 | items => [], |
|
|
3737 | @_, |
|
|
3738 | ); |
|
|
3739 | |
|
|
3740 | $self->set_sort_order (undef); |
|
|
3741 | |
|
|
3742 | $self |
|
|
3743 | } |
|
|
3744 | |
|
|
3745 | sub update_items { |
|
|
3746 | my ($self) = @_; |
|
|
3747 | |
|
|
3748 | $self->clear; |
|
|
3749 | |
|
|
3750 | my @item = $self->{sort}->(@{ $self->{items} }); |
|
|
3751 | |
|
|
3752 | my @adds; |
|
|
3753 | my $row = 0; |
|
|
3754 | for my $item ($self->{sort}->(@{ $self->{items} })) { |
|
|
3755 | CFPlus::Item::update_widgets $item; |
|
|
3756 | |
|
|
3757 | push @adds, 0, $row, $item->{face_widget}; |
|
|
3758 | push @adds, 1, $row, $item->{desc_widget}; |
|
|
3759 | push @adds, 2, $row, $item->{weight_widget}; |
|
|
3760 | |
|
|
3761 | $row++; |
|
|
3762 | } |
|
|
3763 | |
|
|
3764 | $self->add (@adds); |
|
|
3765 | } |
|
|
3766 | |
|
|
3767 | sub set_sort_order { |
|
|
3768 | my ($self, $order) = @_; |
|
|
3769 | |
|
|
3770 | $self->{sort} = $order ||= sub { |
|
|
3771 | sort { |
|
|
3772 | $a->{type} <=> $b->{type} |
|
|
3773 | or $a->{name} cmp $b->{name} |
|
|
3774 | } @_ |
|
|
3775 | }; |
|
|
3776 | |
|
|
3777 | $self->update_items; |
|
|
3778 | } |
|
|
3779 | |
|
|
3780 | sub set_items { |
|
|
3781 | my ($self, $items) = @_; |
|
|
3782 | |
|
|
3783 | $self->{items} = [$items ? values %$items : ()]; |
|
|
3784 | $self->update_items; |
|
|
3785 | } |
|
|
3786 | |
|
|
3787 | ############################################################################# |
|
|
3788 | |
|
|
3789 | package CFPlus::UI::SpellList; |
|
|
3790 | |
|
|
3791 | our @ISA = CFPlus::UI::Table::; |
|
|
3792 | |
|
|
3793 | sub new { |
|
|
3794 | my $class = shift; |
|
|
3795 | |
|
|
3796 | my $self = $class->SUPER::new ( |
|
|
3797 | binding => [], |
|
|
3798 | commands => [], |
|
|
3799 | @_, |
|
|
3800 | ) |
|
|
3801 | } |
|
|
3802 | |
|
|
3803 | my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>"; |
|
|
3804 | |
|
|
3805 | my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3806 | "<b>Name</b>. The name of the spell.$TOOLTIP_ALL"); |
|
|
3807 | my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3808 | "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3809 | my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3810 | "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3811 | my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3812 | "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL"); |
|
|
3813 | my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3814 | "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL"); |
|
|
3815 | |
|
|
3816 | sub rebuild_spell_list { |
|
|
3817 | my ($self) = @_; |
|
|
3818 | |
|
|
3819 | $CFPlus::UI::ROOT->on_refresh ($self => sub { |
|
|
3820 | $self->clear; |
|
|
3821 | |
|
|
3822 | return unless $::CONN; |
|
|
3823 | |
|
|
3824 | $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME); |
|
|
3825 | $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL); |
|
|
3826 | $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL); |
|
|
3827 | $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP); |
|
|
3828 | $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG); |
|
|
3829 | |
|
|
3830 | my $row = 0; |
|
|
3831 | |
|
|
3832 | for (sort { $a cmp $b } keys %{ $self->{spell} }) { |
|
|
3833 | my $spell = $self->{spell}{$_}; |
|
|
3834 | |
|
|
3835 | $row++; |
|
|
3836 | |
|
|
3837 | my $spell_cb = sub { |
|
|
3838 | my ($widget, $ev) = @_; |
|
|
3839 | |
|
|
3840 | if ($ev->{button} == 1) { |
|
|
3841 | $::CONN->user_send ("cast $spell->{name}"); |
|
|
3842 | } elsif ($ev->{button} == 2) { |
|
|
3843 | $::CONN->user_send ("invoke $spell->{name}"); |
|
|
3844 | } elsif ($ev->{button} == 3) { |
|
|
3845 | (new CFPlus::UI::Menu |
|
|
3846 | items => [ |
|
|
3847 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
|
|
3848 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
|
|
3849 | ], |
|
|
3850 | )->popup ($ev); |
|
|
3851 | } else { |
|
|
3852 | return 0; |
|
|
3853 | } |
|
|
3854 | |
|
|
3855 | 1 |
|
|
3856 | }; |
|
|
3857 | |
|
|
3858 | my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL; |
|
|
3859 | |
|
|
3860 | #TODO: add path info to tooltip |
|
|
3861 | #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); |
|
|
3862 | |
|
|
3863 | $self->add (0, $row, new CFPlus::UI::Face |
|
|
3864 | face => $spell->{face}, |
|
|
3865 | can_hover => 1, |
|
|
3866 | can_events => 1, |
|
|
3867 | tooltip => $tooltip, |
|
|
3868 | on_button_down => $spell_cb, |
|
|
3869 | ); |
|
|
3870 | |
|
|
3871 | $self->add (1, $row, new CFPlus::UI::Label |
|
|
3872 | expand => 1, |
|
|
3873 | text => $spell->{name}, |
|
|
3874 | can_hover => 1, |
|
|
3875 | can_events => 1, |
|
|
3876 | tooltip => $tooltip, |
|
|
3877 | on_button_down => $spell_cb, |
|
|
3878 | ); |
|
|
3879 | |
|
|
3880 | $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); |
|
|
3881 | $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL); |
|
|
3882 | $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); |
|
|
3883 | $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); |
|
|
3884 | } |
|
|
3885 | }); |
|
|
3886 | } |
|
|
3887 | |
|
|
3888 | sub add_spell { |
|
|
3889 | my ($self, $spell) = @_; |
|
|
3890 | |
|
|
3891 | $self->{spell}->{$spell->{name}} = $spell; |
|
|
3892 | $self->rebuild_spell_list; |
|
|
3893 | } |
|
|
3894 | |
|
|
3895 | sub remove_spell { |
|
|
3896 | my ($self, $spell) = @_; |
|
|
3897 | |
|
|
3898 | delete $self->{spell}->{$spell->{name}}; |
|
|
3899 | $self->rebuild_spell_list; |
|
|
3900 | } |
|
|
3901 | |
|
|
3902 | sub clear_spells { |
|
|
3903 | my ($self) = @_; |
|
|
3904 | |
|
|
3905 | $self->{spell} = {}; |
|
|
3906 | $self->rebuild_spell_list; |
|
|
3907 | } |
|
|
3908 | |
|
|
3909 | ############################################################################# |
|
|
3910 | |
|
|
3911 | package CFPlus::UI::Root; |
3764 | package CFPlus::UI::Root; |
3912 | |
3765 | |
3913 | our @ISA = CFPlus::UI::Container::; |
3766 | our @ISA = CFPlus::UI::Container::; |
3914 | |
3767 | |
3915 | use List::Util qw(min max); |
3768 | use List::Util qw(min max); |