… | |
… | |
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, |
… | |
… | |
514 | ); |
521 | ); |
515 | |
522 | |
516 | sub emit { |
523 | sub emit { |
517 | my ($self, $signal, @args) = @_; |
524 | my ($self, $signal, @args) = @_; |
518 | |
525 | |
519 | # I do not really like this solution, but I dislike duplication |
526 | # I do not really like this solution, but I do not like duplication |
520 | # and needlessly verbose code, too. |
527 | # and needlessly verbose code, either. |
521 | my @append |
528 | my @append |
522 | = $has_coords{$signal} |
529 | = $has_coords{$signal} |
523 | ? $args[0]->xy ($self) |
530 | ? $args[0]->xy ($self) |
524 | : (); |
531 | : (); |
525 | |
532 | |
… | |
… | |
638 | sub DESTROY { |
645 | sub DESTROY { |
639 | my ($self) = @_; |
646 | my ($self) = @_; |
640 | |
647 | |
641 | return if CFPlus::in_destruct; |
648 | return if CFPlus::in_destruct; |
642 | |
649 | |
643 | delete $WIDGET{$self+0}; |
|
|
644 | |
|
|
645 | eval { $self->destroy }; |
650 | eval { $self->destroy }; |
646 | 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}; |
647 | } |
654 | } |
648 | |
655 | |
649 | ############################################################################# |
656 | ############################################################################# |
650 | |
657 | |
651 | package CFPlus::UI::DrawBG; |
658 | package CFPlus::UI::DrawBG; |
… | |
… | |
1900 | active_fg => [0, 0, 0], |
1907 | active_fg => [0, 0, 0], |
1901 | can_hover => 1, |
1908 | can_hover => 1, |
1902 | can_focus => 1, |
1909 | can_focus => 1, |
1903 | valign => 0, |
1910 | valign => 0, |
1904 | can_events => 1, |
1911 | can_events => 1, |
|
|
1912 | ellipsise => 0, |
1905 | #text => ... |
1913 | #text => ... |
1906 | #hidden => "*", |
1914 | #hidden => "*", |
1907 | @_ |
1915 | @_ |
1908 | ) |
1916 | ) |
1909 | } |
1917 | } |
… | |
… | |
1966 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1974 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1967 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1975 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1968 | $self->{cursor} = 0; |
1976 | $self->{cursor} = 0; |
1969 | } elsif ($sym == CFPlus::SDLK_END) { |
1977 | } elsif ($sym == CFPlus::SDLK_END) { |
1970 | $self->{cursor} = length $text; |
1978 | $self->{cursor} = length $text; |
1971 | } elsif ($uni == 21) { # ctrl-u |
1979 | } elsif ($uni == 21) { # ctrl-u |
1972 | $text = ""; |
1980 | $text = ""; |
1973 | $self->{cursor} = 0; |
1981 | $self->{cursor} = 0; |
1974 | } elsif ($uni == 27) { |
1982 | } elsif ($uni == 27) { |
1975 | $self->emit ('escape'); |
1983 | $self->emit ('escape'); |
1976 | } elsif ($uni) { |
1984 | } elsif ($uni >= 0x20 || $uni == 0x0d) { |
1977 | substr $text, $self->{cursor}++, 0, chr $uni; |
1985 | substr $text, $self->{cursor}++, 0, chr $uni; |
1978 | } else { |
1986 | } else { |
1979 | return 0; |
1987 | return 0; |
1980 | } |
1988 | } |
1981 | |
1989 | |
1982 | $self->_set_text ($text); |
1990 | $self->_set_text ($text); |
1983 | |
1991 | |
1984 | $self->realloc; |
1992 | $self->realloc; |
|
|
1993 | $self->update; |
1985 | |
1994 | |
1986 | 1 |
1995 | 1 |
1987 | } |
1996 | } |
1988 | |
1997 | |
1989 | sub invoke_focus_in { |
1998 | sub invoke_focus_in { |
… | |
… | |
2058 | 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}; |
2059 | glEnd; |
2068 | glEnd; |
2060 | } |
2069 | } |
2061 | } |
2070 | } |
2062 | |
2071 | |
|
|
2072 | ############################################################################# |
|
|
2073 | |
2063 | package CFPlus::UI::Entry; |
2074 | package CFPlus::UI::Entry; |
2064 | |
2075 | |
2065 | our @ISA = CFPlus::UI::EntryBase::; |
2076 | our @ISA = CFPlus::UI::EntryBase::; |
2066 | |
2077 | |
2067 | use CFPlus::OpenGL; |
2078 | use CFPlus::OpenGL; |
… | |
… | |
2069 | sub invoke_key_down { |
2080 | sub invoke_key_down { |
2070 | my ($self, $ev) = @_; |
2081 | my ($self, $ev) = @_; |
2071 | |
2082 | |
2072 | my $sym = $ev->{sym}; |
2083 | my $sym = $ev->{sym}; |
2073 | |
2084 | |
2074 | if ($sym == 13) { |
2085 | if ($ev->{uni} == 0x0d || $sym == 13) { |
2075 | unshift @{$self->{history}}, |
2086 | unshift @{$self->{history}}, |
2076 | my $txt = $self->get_text; |
2087 | my $txt = $self->get_text; |
2077 | |
2088 | |
2078 | $self->{history_pointer} = -1; |
2089 | $self->{history_pointer} = -1; |
2079 | $self->{history_saveback} = ''; |
2090 | $self->{history_saveback} = ''; |
… | |
… | |
2100 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2111 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2101 | } else { |
2112 | } else { |
2102 | $self->set_text ($self->{history_saveback}); |
2113 | $self->set_text ($self->{history_saveback}); |
2103 | } |
2114 | } |
2104 | |
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); |
2105 | } else { |
2155 | } else { |
2106 | return $self->SUPER::invoke_key_down ($ev) |
2156 | return $self->SUPER::invoke_key_down ($ev) |
2107 | } |
2157 | } |
2108 | |
2158 | |
2109 | 1 |
2159 | 1 |
… | |
… | |
3709 | $self->SUPER::destroy; |
3759 | $self->SUPER::destroy; |
3710 | } |
3760 | } |
3711 | |
3761 | |
3712 | ############################################################################# |
3762 | ############################################################################# |
3713 | |
3763 | |
3714 | package CFPlus::UI::Inventory; |
|
|
3715 | |
|
|
3716 | our @ISA = CFPlus::UI::Table::; |
|
|
3717 | |
|
|
3718 | sub 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 | |
|
|
3732 | sub 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 | |
|
|
3754 | sub 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 | |
|
|
3767 | sub set_items { |
|
|
3768 | my ($self, $items) = @_; |
|
|
3769 | |
|
|
3770 | $self->{items} = [$items ? values %$items : ()]; |
|
|
3771 | $self->update_items; |
|
|
3772 | } |
|
|
3773 | |
|
|
3774 | ############################################################################# |
|
|
3775 | |
|
|
3776 | package CFPlus::UI::SpellList; |
|
|
3777 | |
|
|
3778 | our @ISA = CFPlus::UI::Table::; |
|
|
3779 | |
|
|
3780 | sub new { |
|
|
3781 | my $class = shift; |
|
|
3782 | |
|
|
3783 | my $self = $class->SUPER::new ( |
|
|
3784 | binding => [], |
|
|
3785 | commands => [], |
|
|
3786 | @_, |
|
|
3787 | ) |
|
|
3788 | } |
|
|
3789 | |
|
|
3790 | my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>"; |
|
|
3791 | |
|
|
3792 | my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3793 | "<b>Name</b>. The name of the spell.$TOOLTIP_ALL"); |
|
|
3794 | my @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"); |
|
|
3796 | my @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"); |
|
|
3798 | my @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"); |
|
|
3800 | my @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 | |
|
|
3803 | sub 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 | (new CFPlus::UI::Menu |
|
|
3833 | items => [ |
|
|
3834 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
|
|
3835 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
|
|
3836 | ], |
|
|
3837 | )->popup ($ev); |
|
|
3838 | } else { |
|
|
3839 | return 0; |
|
|
3840 | } |
|
|
3841 | |
|
|
3842 | 1 |
|
|
3843 | }; |
|
|
3844 | |
|
|
3845 | my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL; |
|
|
3846 | |
|
|
3847 | #TODO: add path info to tooltip |
|
|
3848 | #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); |
|
|
3849 | |
|
|
3850 | $self->add (0, $row, new CFPlus::UI::Face |
|
|
3851 | face => $spell->{face}, |
|
|
3852 | can_hover => 1, |
|
|
3853 | can_events => 1, |
|
|
3854 | tooltip => $tooltip, |
|
|
3855 | on_button_down => $spell_cb, |
|
|
3856 | ); |
|
|
3857 | |
|
|
3858 | $self->add (1, $row, new CFPlus::UI::Label |
|
|
3859 | expand => 1, |
|
|
3860 | text => $spell->{name}, |
|
|
3861 | can_hover => 1, |
|
|
3862 | can_events => 1, |
|
|
3863 | tooltip => $tooltip, |
|
|
3864 | on_button_down => $spell_cb, |
|
|
3865 | ); |
|
|
3866 | |
|
|
3867 | $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); |
|
|
3868 | $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL); |
|
|
3869 | $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); |
|
|
3870 | $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); |
|
|
3871 | } |
|
|
3872 | }); |
|
|
3873 | } |
|
|
3874 | |
|
|
3875 | sub add_spell { |
|
|
3876 | my ($self, $spell) = @_; |
|
|
3877 | |
|
|
3878 | $self->{spell}->{$spell->{name}} = $spell; |
|
|
3879 | $self->rebuild_spell_list; |
|
|
3880 | } |
|
|
3881 | |
|
|
3882 | sub remove_spell { |
|
|
3883 | my ($self, $spell) = @_; |
|
|
3884 | |
|
|
3885 | delete $self->{spell}->{$spell->{name}}; |
|
|
3886 | $self->rebuild_spell_list; |
|
|
3887 | } |
|
|
3888 | |
|
|
3889 | sub clear_spells { |
|
|
3890 | my ($self) = @_; |
|
|
3891 | |
|
|
3892 | $self->{spell} = {}; |
|
|
3893 | $self->rebuild_spell_list; |
|
|
3894 | } |
|
|
3895 | |
|
|
3896 | ############################################################################# |
|
|
3897 | |
|
|
3898 | package CFPlus::UI::Root; |
3764 | package CFPlus::UI::Root; |
3899 | |
3765 | |
3900 | our @ISA = CFPlus::UI::Container::; |
3766 | our @ISA = CFPlus::UI::Container::; |
3901 | |
3767 | |
3902 | use List::Util qw(min max); |
3768 | use List::Util qw(min max); |