1 | package CFPlus::UI; |
1 | package CFPlus::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Scalar::Util (); |
|
|
7 | use List::Util (); |
6 | use List::Util (); |
8 | use Event; |
7 | use Event; |
9 | |
8 | |
10 | use CFPlus; |
9 | use CFPlus; |
11 | use CFPlus::Pod; |
10 | use CFPlus::Pod; |
… | |
… | |
23 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
24 | if (!$GRAB) { |
23 | if (!$GRAB) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
26 | if (length $widget->{tooltip}) { |
25 | if (length $widget->{tooltip}) { |
27 | if ($TOOLTIP->{owner} != $widget) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
|
|
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
28 | $TOOLTIP->hide; |
28 | $TOOLTIP->hide; |
29 | |
29 | |
30 | $TOOLTIP->{owner} = $widget; |
30 | $TOOLTIP->{owner} = $widget; |
|
|
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
31 | |
32 | |
32 | return if $ENV{CFPLUS_DEBUG} & 8; |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
33 | |
34 | |
34 | my $tip = $widget->{tooltip}; |
35 | my $tip = $widget->{tooltip}; |
35 | |
36 | |
… | |
… | |
43 | } |
44 | } |
44 | } |
45 | } |
45 | } |
46 | } |
46 | |
47 | |
47 | $TOOLTIP->hide; |
48 | $TOOLTIP->hide; |
|
|
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
48 | delete $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
49 | }); |
51 | }); |
50 | |
52 | |
51 | sub get_layout { |
53 | sub get_layout { |
52 | my $layout; |
54 | my $layout; |
… | |
… | |
235 | h => undef, |
237 | h => undef, |
236 | can_events => 1, |
238 | can_events => 1, |
237 | @_ |
239 | @_ |
238 | }, $class; |
240 | }, $class; |
239 | |
241 | |
240 | Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
242 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
241 | |
243 | |
242 | for (keys %$self) { |
244 | for (keys %$self) { |
243 | if (/^on_(.*)$/) { |
245 | if (/^on_(.*)$/) { |
244 | $self->connect ($1 => delete $self->{$_}); |
246 | $self->connect ($1 => delete $self->{$_}); |
245 | } |
247 | } |
… | |
… | |
262 | |
264 | |
263 | sub destroy { |
265 | sub destroy { |
264 | my ($self) = @_; |
266 | my ($self) = @_; |
265 | |
267 | |
266 | $self->hide; |
268 | $self->hide; |
|
|
269 | $self->emit ("destroy"); |
267 | %$self = (); |
270 | %$self = (); |
268 | } |
271 | } |
269 | |
272 | |
270 | sub show { |
273 | sub show { |
271 | my ($self) = @_; |
274 | my ($self) = @_; |
… | |
… | |
502 | |
505 | |
503 | defined wantarray and CFPlus::guard { |
506 | defined wantarray and CFPlus::guard { |
504 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
507 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
505 | @{ $self->{signal_cb}{$signal} }; |
508 | @{ $self->{signal_cb}{$signal} }; |
506 | } |
509 | } |
|
|
510 | } |
|
|
511 | |
|
|
512 | sub disconnect_all { |
|
|
513 | my ($self, $signal) = @_; |
|
|
514 | |
|
|
515 | delete $self->{signal_cb}{$signal}; |
507 | } |
516 | } |
508 | |
517 | |
509 | my %has_coords = ( |
518 | my %has_coords = ( |
510 | button_down => 1, |
519 | button_down => 1, |
511 | button_up => 1, |
520 | button_up => 1, |
… | |
… | |
514 | ); |
523 | ); |
515 | |
524 | |
516 | sub emit { |
525 | sub emit { |
517 | my ($self, $signal, @args) = @_; |
526 | my ($self, $signal, @args) = @_; |
518 | |
527 | |
519 | # I do not really like this solution, but I dislike duplication |
528 | # I do not really like this solution, but I do not like duplication |
520 | # and needlessly verbose code, too. |
529 | # and needlessly verbose code, either. |
521 | my @append |
530 | my @append |
522 | = $has_coords{$signal} |
531 | = $has_coords{$signal} |
523 | ? $args[0]->xy ($self) |
532 | ? $args[0]->xy ($self) |
524 | : (); |
533 | : (); |
525 | |
534 | |
526 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
535 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
527 | |
536 | |
528 | #d##TODO# stop propagating at first true, do not use sum |
537 | for my $cb ( |
529 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
538 | @{$self->{signal_cb}{$signal} || []}, # before |
530 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
539 | ($self->can ("invoke_$signal") || sub { 1 }), # closure |
|
|
540 | ) { |
|
|
541 | return $cb->($self, @args, @append) || next; |
|
|
542 | } |
|
|
543 | |
|
|
544 | # parent |
531 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
545 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
532 | } |
546 | } |
533 | |
547 | |
534 | sub find_widget { |
548 | sub find_widget { |
535 | my ($self, $x, $y) = @_; |
549 | my ($self, $x, $y) = @_; |
536 | |
550 | |
… | |
… | |
544 | } |
558 | } |
545 | |
559 | |
546 | sub set_parent { |
560 | sub set_parent { |
547 | my ($self, $parent) = @_; |
561 | my ($self, $parent) = @_; |
548 | |
562 | |
549 | Scalar::Util::weaken ($self->{parent} = $parent); |
563 | CFPlus::weaken ($self->{parent} = $parent); |
550 | $self->set_visible if $parent->{visible}; |
564 | $self->set_visible if $parent->{visible}; |
551 | } |
565 | } |
552 | |
566 | |
553 | sub realloc { |
567 | sub realloc { |
554 | my ($self) = @_; |
568 | my ($self) = @_; |
… | |
… | |
633 | my ($self) = @_; |
647 | my ($self) = @_; |
634 | |
648 | |
635 | warn "no draw defined for $self\n"; |
649 | warn "no draw defined for $self\n"; |
636 | } |
650 | } |
637 | |
651 | |
|
|
652 | my $cntx;#d# |
638 | sub DESTROY { |
653 | sub DESTROY { |
639 | my ($self) = @_; |
654 | my ($self) = @_; |
640 | |
655 | |
641 | return if CFPlus::in_destruct; |
656 | return if CFPlus::in_destruct; |
642 | |
|
|
643 | delete $WIDGET{$self+0}; |
|
|
644 | |
657 | |
645 | eval { $self->destroy }; |
658 | eval { $self->destroy }; |
646 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
659 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
|
|
660 | |
|
|
661 | delete $WIDGET{$self+0}; |
647 | } |
662 | } |
648 | |
663 | |
649 | ############################################################################# |
664 | ############################################################################# |
650 | |
665 | |
651 | package CFPlus::UI::DrawBG; |
666 | package CFPlus::UI::DrawBG; |
… | |
… | |
898 | glClear GL_COLOR_BUFFER_BIT; |
913 | glClear GL_COLOR_BUFFER_BIT; |
899 | |
914 | |
900 | { |
915 | { |
901 | package CFPlus::UI::Base; |
916 | package CFPlus::UI::Base; |
902 | |
917 | |
903 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
918 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
904 | (0, 0, $self->{w}, $self->{h}); |
919 | (0, 0, $self->{w}, $self->{h}); |
|
|
920 | |
|
|
921 | $self->_render; |
905 | } |
922 | } |
906 | |
|
|
907 | $self->_render; |
|
|
908 | }; |
923 | }; |
909 | } |
924 | } |
910 | |
925 | |
911 | sub _draw { |
926 | sub _draw { |
912 | my ($self) = @_; |
927 | my ($self) = @_; |
… | |
… | |
1900 | active_fg => [0, 0, 0], |
1915 | active_fg => [0, 0, 0], |
1901 | can_hover => 1, |
1916 | can_hover => 1, |
1902 | can_focus => 1, |
1917 | can_focus => 1, |
1903 | valign => 0, |
1918 | valign => 0, |
1904 | can_events => 1, |
1919 | can_events => 1, |
|
|
1920 | ellipsise => 0, |
1905 | #text => ... |
1921 | #text => ... |
1906 | #hidden => "*", |
1922 | #hidden => "*", |
1907 | @_ |
1923 | @_ |
1908 | ) |
1924 | ) |
1909 | } |
1925 | } |
… | |
… | |
1953 | my $mod = $ev->{mod}; |
1969 | my $mod = $ev->{mod}; |
1954 | my $sym = $ev->{sym}; |
1970 | my $sym = $ev->{sym}; |
1955 | my $uni = $ev->{unicode}; |
1971 | my $uni = $ev->{unicode}; |
1956 | |
1972 | |
1957 | my $text = $self->get_text; |
1973 | my $text = $self->get_text; |
|
|
1974 | |
|
|
1975 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
1958 | |
1976 | |
1959 | if ($uni == 8) { |
1977 | if ($uni == 8) { |
1960 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1978 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1961 | } elsif ($uni == 127) { |
1979 | } elsif ($uni == 127) { |
1962 | substr $text, $self->{cursor}, 1, ""; |
1980 | substr $text, $self->{cursor}, 1, ""; |
1963 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
1981 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
1964 | --$self->{cursor} if $self->{cursor}; |
1982 | --$self->{cursor} if $self->{cursor}; |
1965 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
1983 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
1966 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1984 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1967 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1985 | } elsif ($sym == CFPlus::SDLK_HOME) { |
|
|
1986 | # what a hack |
|
|
1987 | $self->{cursor} = |
|
|
1988 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
|
|
1989 | ? length $1 |
|
|
1990 | : 0; |
|
|
1991 | } elsif ($sym == CFPlus::SDLK_END) { |
|
|
1992 | # uh, again |
|
|
1993 | $self->{cursor} = |
|
|
1994 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
|
|
1995 | ? $self->{cursor} + length $1 |
|
|
1996 | : length $self->{text}; |
|
|
1997 | } elsif ($uni == 21) { # ctrl-u |
|
|
1998 | $text = ""; |
1968 | $self->{cursor} = 0; |
1999 | $self->{cursor} = 0; |
1969 | } elsif ($sym == CFPlus::SDLK_END) { |
|
|
1970 | $self->{cursor} = length $text; |
|
|
1971 | } elsif ($uni == 21) { # ctrl-u |
|
|
1972 | $text = ""; |
|
|
1973 | } elsif ($uni == 27) { |
2000 | } elsif ($uni == 27) { |
1974 | $self->emit ('escape'); |
2001 | $self->emit ('escape'); |
1975 | } elsif ($uni) { |
2002 | } elsif ($uni == 0x0d) { |
|
|
2003 | substr $text, $self->{cursor}++, 0, "\012"; |
|
|
2004 | } elsif ($uni >= 0x20) { |
1976 | substr $text, $self->{cursor}++, 0, chr $uni; |
2005 | substr $text, $self->{cursor}++, 0, chr $uni; |
1977 | } else { |
2006 | } else { |
1978 | return 0; |
2007 | return 0; |
1979 | } |
2008 | } |
1980 | |
2009 | |
1981 | $self->_set_text ($text); |
2010 | $self->_set_text ($text); |
1982 | |
2011 | |
1983 | $self->realloc; |
2012 | $self->realloc; |
|
|
2013 | $self->update; |
1984 | |
2014 | |
1985 | 1 |
2015 | 1 |
1986 | } |
2016 | } |
1987 | |
2017 | |
1988 | sub invoke_focus_in { |
2018 | sub invoke_focus_in { |
… | |
… | |
2057 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2087 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2058 | glEnd; |
2088 | glEnd; |
2059 | } |
2089 | } |
2060 | } |
2090 | } |
2061 | |
2091 | |
|
|
2092 | ############################################################################# |
|
|
2093 | |
2062 | package CFPlus::UI::Entry; |
2094 | package CFPlus::UI::Entry; |
2063 | |
2095 | |
2064 | our @ISA = CFPlus::UI::EntryBase::; |
2096 | our @ISA = CFPlus::UI::EntryBase::; |
2065 | |
2097 | |
2066 | use CFPlus::OpenGL; |
2098 | use CFPlus::OpenGL; |
… | |
… | |
2068 | sub invoke_key_down { |
2100 | sub invoke_key_down { |
2069 | my ($self, $ev) = @_; |
2101 | my ($self, $ev) = @_; |
2070 | |
2102 | |
2071 | my $sym = $ev->{sym}; |
2103 | my $sym = $ev->{sym}; |
2072 | |
2104 | |
2073 | if ($sym == 13) { |
2105 | if ($ev->{uni} == 0x0d || $sym == 13) { |
2074 | unshift @{$self->{history}}, |
2106 | unshift @{$self->{history}}, |
2075 | my $txt = $self->get_text; |
2107 | my $txt = $self->get_text; |
2076 | |
2108 | |
2077 | $self->{history_pointer} = -1; |
2109 | $self->{history_pointer} = -1; |
2078 | $self->{history_saveback} = ''; |
2110 | $self->{history_saveback} = ''; |
… | |
… | |
2108 | 1 |
2140 | 1 |
2109 | } |
2141 | } |
2110 | |
2142 | |
2111 | ############################################################################# |
2143 | ############################################################################# |
2112 | |
2144 | |
|
|
2145 | package CFPlus::UI::TextEdit; |
|
|
2146 | |
|
|
2147 | our @ISA = CFPlus::UI::EntryBase::; |
|
|
2148 | |
|
|
2149 | use CFPlus::OpenGL; |
|
|
2150 | |
|
|
2151 | sub move_cursor_ver { |
|
|
2152 | my ($self, $dy) = @_; |
|
|
2153 | |
|
|
2154 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
|
|
2155 | |
|
|
2156 | $y += $dy; |
|
|
2157 | |
|
|
2158 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
|
|
2159 | $self->{cursor} = $index; |
|
|
2160 | delete $self->{cur_h}; |
|
|
2161 | $self->update; |
|
|
2162 | return; |
|
|
2163 | } |
|
|
2164 | } |
|
|
2165 | |
|
|
2166 | sub invoke_key_down { |
|
|
2167 | my ($self, $ev) = @_; |
|
|
2168 | |
|
|
2169 | my $sym = $ev->{sym}; |
|
|
2170 | |
|
|
2171 | if ($sym == CFPlus::SDLK_UP) { |
|
|
2172 | $self->move_cursor_ver (-1); |
|
|
2173 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
|
|
2174 | $self->move_cursor_ver (+1); |
|
|
2175 | } else { |
|
|
2176 | return $self->SUPER::invoke_key_down ($ev) |
|
|
2177 | } |
|
|
2178 | |
|
|
2179 | 1 |
|
|
2180 | } |
|
|
2181 | |
|
|
2182 | ############################################################################# |
|
|
2183 | |
2113 | package CFPlus::UI::Button; |
2184 | package CFPlus::UI::Button; |
2114 | |
2185 | |
2115 | our @ISA = CFPlus::UI::Label::; |
2186 | our @ISA = CFPlus::UI::Label::; |
2116 | |
2187 | |
2117 | use CFPlus::OpenGL; |
2188 | use CFPlus::OpenGL; |
… | |
… | |
2259 | or Carp::croak "'path' or 'tex' attributes required"; |
2330 | or Carp::croak "'path' or 'tex' attributes required"; |
2260 | |
2331 | |
2261 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2332 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2262 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2333 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2263 | |
2334 | |
2264 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2335 | CFPlus::weaken $texture_cache{$self->{path}}; |
2265 | |
2336 | |
2266 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2337 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2267 | |
2338 | |
2268 | $self |
2339 | $self |
2269 | } |
2340 | } |
… | |
… | |
2642 | sub invoke_mouse_wheel { |
2713 | sub invoke_mouse_wheel { |
2643 | my ($self, $ev) = @_; |
2714 | my ($self, $ev) = @_; |
2644 | |
2715 | |
2645 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2716 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2646 | |
2717 | |
|
|
2718 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
|
|
2719 | |
2647 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); |
2720 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2648 | |
2721 | |
2649 | ! ! $delta |
2722 | ! ! $delta |
2650 | } |
2723 | } |
2651 | |
2724 | |
2652 | sub update { |
2725 | sub update { |
… | |
… | |
2762 | #font => default_font |
2835 | #font => default_font |
2763 | @_, |
2836 | @_, |
2764 | |
2837 | |
2765 | layout => (new CFPlus::Layout), |
2838 | layout => (new CFPlus::Layout), |
2766 | par => [], |
2839 | par => [], |
|
|
2840 | max_par => 0, |
2767 | height => 0, |
2841 | height => 0, |
2768 | children => [ |
2842 | children => [ |
2769 | (new CFPlus::UI::Empty expand => 1), |
2843 | (new CFPlus::UI::Empty expand => 1), |
2770 | (new CFPlus::UI::Slider vertical => 1), |
2844 | (new CFPlus::UI::Slider vertical => 1), |
2771 | ], |
2845 | ], |
… | |
… | |
2896 | |
2970 | |
2897 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2971 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2898 | push @{$self->{par}}, $para; |
2972 | push @{$self->{par}}, $para; |
2899 | } |
2973 | } |
2900 | |
2974 | |
|
|
2975 | if (my $max = $self->{max_par}) { |
|
|
2976 | shift @{$self->{par}} while @{$self->{par}} > $max; |
|
|
2977 | } |
|
|
2978 | |
2901 | $self->{need_reflow}++; |
2979 | $self->{need_reflow}++; |
2902 | $self->update; |
2980 | $self->update; |
2903 | } |
2981 | } |
2904 | |
2982 | |
2905 | sub scroll_to_bottom { |
2983 | sub scroll_to_bottom { |
… | |
… | |
2957 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3035 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
2958 | |
3036 | |
2959 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
3037 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
2960 | glClearColor 0, 0, 0, 0; |
3038 | glClearColor 0, 0, 0, 0; |
2961 | glClear GL_COLOR_BUFFER_BIT; |
3039 | glClear GL_COLOR_BUFFER_BIT; |
|
|
3040 | |
|
|
3041 | package CFPlus::UI::Base; |
|
|
3042 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
|
|
3043 | (0, 0, $self->{w}, $self->{h}); |
2962 | |
3044 | |
2963 | my $top = int $self->{children}[1]{range}[0]; |
3045 | my $top = int $self->{children}[1]{range}[0]; |
2964 | |
3046 | |
2965 | my $paridx = 0; |
3047 | my $paridx = 0; |
2966 | my $top_paragraph; |
3048 | my $top_paragraph; |
… | |
… | |
3213 | can_events => 0, |
3295 | can_events => 0, |
3214 | @_, |
3296 | @_, |
3215 | ); |
3297 | ); |
3216 | |
3298 | |
3217 | if ($self->{anim} && $self->{animspeed}) { |
3299 | if ($self->{anim} && $self->{animspeed}) { |
3218 | Scalar::Util::weaken (my $widget = $self); |
3300 | CFPlus::weaken (my $widget = $self); |
3219 | |
3301 | |
3220 | $self->{timer} = Event->timer ( |
3302 | $self->{timer} = Event->timer ( |
3221 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3303 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3222 | hard => 1, |
3304 | hard => 1, |
3223 | interval => $self->{animspeed}, |
3305 | interval => $self->{animspeed}, |
… | |
… | |
3283 | |
3365 | |
3284 | package CFPlus::UI::Buttonbar; |
3366 | package CFPlus::UI::Buttonbar; |
3285 | |
3367 | |
3286 | our @ISA = CFPlus::UI::HBox::; |
3368 | our @ISA = CFPlus::UI::HBox::; |
3287 | |
3369 | |
3288 | # TODO: should actualyl wrap buttons and other goodies. |
3370 | # TODO: should actually wrap buttons and other goodies. |
3289 | |
3371 | |
3290 | ############################################################################# |
3372 | ############################################################################# |
3291 | |
3373 | |
3292 | package CFPlus::UI::Menu; |
3374 | package CFPlus::UI::Menu; |
3293 | |
3375 | |
… | |
… | |
3477 | } |
3559 | } |
3478 | |
3560 | |
3479 | sub add { |
3561 | sub add { |
3480 | my ($self, $title, $widget, $tooltip) = @_; |
3562 | my ($self, $title, $widget, $tooltip) = @_; |
3481 | |
3563 | |
3482 | Scalar::Util::weaken $self; |
3564 | CFPlus::weaken $self; |
3483 | |
3565 | |
3484 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3566 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3485 | markup => $title, |
3567 | markup => $title, |
3486 | tooltip => $tooltip, |
3568 | tooltip => $tooltip, |
3487 | on_activate => sub { $self->set_current_page ($widget) }, |
3569 | on_activate => sub { $self->set_current_page ($widget) }, |
… | |
… | |
3571 | my $self = $class->SUPER::new ( |
3653 | my $self = $class->SUPER::new ( |
3572 | fontsize => 0.8, |
3654 | fontsize => 0.8, |
3573 | @_, |
3655 | @_, |
3574 | ); |
3656 | ); |
3575 | |
3657 | |
3576 | Scalar::Util::weaken (my $this = $self); |
3658 | CFPlus::weaken (my $this = $self); |
3577 | |
3659 | |
3578 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3660 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3579 | |
3661 | |
3580 | $self |
3662 | $self |
3581 | } |
3663 | } |
… | |
… | |
3704 | $self->SUPER::destroy; |
3786 | $self->SUPER::destroy; |
3705 | } |
3787 | } |
3706 | |
3788 | |
3707 | ############################################################################# |
3789 | ############################################################################# |
3708 | |
3790 | |
3709 | package CFPlus::UI::Inventory; |
|
|
3710 | |
|
|
3711 | our @ISA = CFPlus::UI::Table::; |
|
|
3712 | |
|
|
3713 | sub new { |
|
|
3714 | my $class = shift; |
|
|
3715 | |
|
|
3716 | my $self = $class->SUPER::new ( |
|
|
3717 | col_expand => [0, 1, 0], |
|
|
3718 | items => [], |
|
|
3719 | @_, |
|
|
3720 | ); |
|
|
3721 | |
|
|
3722 | $self->set_sort_order (undef); |
|
|
3723 | |
|
|
3724 | $self |
|
|
3725 | } |
|
|
3726 | |
|
|
3727 | sub update_items { |
|
|
3728 | my ($self) = @_; |
|
|
3729 | |
|
|
3730 | $self->clear; |
|
|
3731 | |
|
|
3732 | my @item = $self->{sort}->(@{ $self->{items} }); |
|
|
3733 | |
|
|
3734 | my @adds; |
|
|
3735 | my $row = 0; |
|
|
3736 | for my $item ($self->{sort}->(@{ $self->{items} })) { |
|
|
3737 | CFPlus::Item::update_widgets $item; |
|
|
3738 | |
|
|
3739 | push @adds, 0, $row, $item->{face_widget}; |
|
|
3740 | push @adds, 1, $row, $item->{desc_widget}; |
|
|
3741 | push @adds, 2, $row, $item->{weight_widget}; |
|
|
3742 | |
|
|
3743 | $row++; |
|
|
3744 | } |
|
|
3745 | |
|
|
3746 | $self->add (@adds); |
|
|
3747 | } |
|
|
3748 | |
|
|
3749 | sub set_sort_order { |
|
|
3750 | my ($self, $order) = @_; |
|
|
3751 | |
|
|
3752 | $self->{sort} = $order ||= sub { |
|
|
3753 | sort { |
|
|
3754 | $a->{type} <=> $b->{type} |
|
|
3755 | or $a->{name} cmp $b->{name} |
|
|
3756 | } @_ |
|
|
3757 | }; |
|
|
3758 | |
|
|
3759 | $self->update_items; |
|
|
3760 | } |
|
|
3761 | |
|
|
3762 | sub set_items { |
|
|
3763 | my ($self, $items) = @_; |
|
|
3764 | |
|
|
3765 | $self->{items} = [$items ? values %$items : ()]; |
|
|
3766 | $self->update_items; |
|
|
3767 | } |
|
|
3768 | |
|
|
3769 | ############################################################################# |
|
|
3770 | |
|
|
3771 | package CFPlus::UI::SpellList; |
|
|
3772 | |
|
|
3773 | our @ISA = CFPlus::UI::Table::; |
|
|
3774 | |
|
|
3775 | sub new { |
|
|
3776 | my $class = shift; |
|
|
3777 | |
|
|
3778 | my $self = $class->SUPER::new ( |
|
|
3779 | binding => [], |
|
|
3780 | commands => [], |
|
|
3781 | @_, |
|
|
3782 | ) |
|
|
3783 | } |
|
|
3784 | |
|
|
3785 | my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>"; |
|
|
3786 | |
|
|
3787 | my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3788 | "<b>Name</b>. The name of the spell.$TOOLTIP_ALL"); |
|
|
3789 | my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3790 | "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3791 | my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3792 | "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3793 | my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3794 | "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL"); |
|
|
3795 | my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3796 | "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL"); |
|
|
3797 | |
|
|
3798 | sub rebuild_spell_list { |
|
|
3799 | my ($self) = @_; |
|
|
3800 | |
|
|
3801 | $CFPlus::UI::ROOT->on_refresh ($self => sub { |
|
|
3802 | $self->clear; |
|
|
3803 | |
|
|
3804 | return unless $::CONN; |
|
|
3805 | |
|
|
3806 | $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME); |
|
|
3807 | $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL); |
|
|
3808 | $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL); |
|
|
3809 | $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP); |
|
|
3810 | $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG); |
|
|
3811 | |
|
|
3812 | my $row = 0; |
|
|
3813 | |
|
|
3814 | for (sort { $a cmp $b } keys %{ $self->{spell} }) { |
|
|
3815 | my $spell = $self->{spell}{$_}; |
|
|
3816 | |
|
|
3817 | $row++; |
|
|
3818 | |
|
|
3819 | my $spell_cb = sub { |
|
|
3820 | my ($widget, $ev) = @_; |
|
|
3821 | |
|
|
3822 | if ($ev->{button} == 1) { |
|
|
3823 | $::CONN->user_send ("cast $spell->{name}"); |
|
|
3824 | } elsif ($ev->{button} == 2) { |
|
|
3825 | $::CONN->user_send ("invoke $spell->{name}"); |
|
|
3826 | } elsif ($ev->{button} == 3) { |
|
|
3827 | (new CFPlus::UI::Menu |
|
|
3828 | items => [ |
|
|
3829 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
|
|
3830 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
|
|
3831 | ], |
|
|
3832 | )->popup ($ev); |
|
|
3833 | } else { |
|
|
3834 | return 0; |
|
|
3835 | } |
|
|
3836 | |
|
|
3837 | 1 |
|
|
3838 | }; |
|
|
3839 | |
|
|
3840 | my $tooltip = "$spell->{message}$TOOLTIP_ALL"; |
|
|
3841 | |
|
|
3842 | #TODO: add path info to tooltip |
|
|
3843 | #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); |
|
|
3844 | |
|
|
3845 | $self->add (0, $row, new CFPlus::UI::Face |
|
|
3846 | face => $spell->{face}, |
|
|
3847 | can_hover => 1, |
|
|
3848 | can_events => 1, |
|
|
3849 | tooltip => $tooltip, |
|
|
3850 | on_button_down => $spell_cb, |
|
|
3851 | ); |
|
|
3852 | |
|
|
3853 | $self->add (1, $row, new CFPlus::UI::Label |
|
|
3854 | expand => 1, |
|
|
3855 | text => $spell->{name}, |
|
|
3856 | can_hover => 1, |
|
|
3857 | can_events => 1, |
|
|
3858 | tooltip => $tooltip, |
|
|
3859 | on_button_down => $spell_cb, |
|
|
3860 | ); |
|
|
3861 | |
|
|
3862 | $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); |
|
|
3863 | $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL); |
|
|
3864 | $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); |
|
|
3865 | $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); |
|
|
3866 | } |
|
|
3867 | }); |
|
|
3868 | } |
|
|
3869 | |
|
|
3870 | sub add_spell { |
|
|
3871 | my ($self, $spell) = @_; |
|
|
3872 | |
|
|
3873 | $self->{spell}->{$spell->{name}} = $spell; |
|
|
3874 | $self->rebuild_spell_list; |
|
|
3875 | } |
|
|
3876 | |
|
|
3877 | sub remove_spell { |
|
|
3878 | my ($self, $spell) = @_; |
|
|
3879 | |
|
|
3880 | delete $self->{spell}->{$spell->{name}}; |
|
|
3881 | $self->rebuild_spell_list; |
|
|
3882 | } |
|
|
3883 | |
|
|
3884 | sub clear_spells { |
|
|
3885 | my ($self) = @_; |
|
|
3886 | |
|
|
3887 | $self->{spell} = {}; |
|
|
3888 | $self->rebuild_spell_list; |
|
|
3889 | } |
|
|
3890 | |
|
|
3891 | ############################################################################# |
|
|
3892 | |
|
|
3893 | package CFPlus::UI::Root; |
3791 | package CFPlus::UI::Root; |
3894 | |
3792 | |
3895 | our @ISA = CFPlus::UI::Container::; |
3793 | our @ISA = CFPlus::UI::Container::; |
3896 | |
3794 | |
3897 | use List::Util qw(min max); |
3795 | use List::Util qw(min max); |
… | |
… | |
3904 | my $self = $class->SUPER::new ( |
3802 | my $self = $class->SUPER::new ( |
3905 | visible => 1, |
3803 | visible => 1, |
3906 | @_, |
3804 | @_, |
3907 | ); |
3805 | ); |
3908 | |
3806 | |
3909 | Scalar::Util::weaken ($self->{root} = $self); |
3807 | CFPlus::weaken ($self->{root} = $self); |
3910 | |
3808 | |
3911 | $self |
3809 | $self |
3912 | } |
3810 | } |
3913 | |
3811 | |
3914 | sub size_request { |
3812 | sub size_request { |
… | |
… | |
4123 | glLoadIdentity; |
4021 | glLoadIdentity; |
4124 | |
4022 | |
4125 | { |
4023 | { |
4126 | package CFPlus::UI::Base; |
4024 | package CFPlus::UI::Base; |
4127 | |
4025 | |
4128 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
4026 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4129 | (0, 0, $self->{w}, $self->{h}); |
4027 | (0, 0, $self->{w}, $self->{h}); |
4130 | } |
|
|
4131 | |
4028 | |
4132 | $self->_draw; |
4029 | $self->_draw; |
|
|
4030 | } |
4133 | } |
4031 | } |
4134 | |
4032 | |
4135 | ############################################################################# |
4033 | ############################################################################# |
4136 | |
4034 | |
4137 | package CFPlus::UI; |
4035 | package CFPlus::UI; |