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.346 by root, Mon Aug 14 01:21:02 2006 UTC vs.
Revision 1.365 by root, Thu Apr 12 07:53:31 2007 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;
898 glClear GL_COLOR_BUFFER_BIT; 910 glClear GL_COLOR_BUFFER_BIT;
899 911
900 { 912 {
901 package CFPlus::UI::Base; 913 package CFPlus::UI::Base;
902 914
903 ($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) = @_;
1138 my ($class, %arg) = @_; 1150 my ($class, %arg) = @_;
1139 1151
1140 if ((exists $arg{label}) && !ref $arg{label}) { 1152 if ((exists $arg{label}) && !ref $arg{label}) {
1141 $arg{label} = new CFPlus::UI::Label 1153 $arg{label} = new CFPlus::UI::Label
1142 align => 1, 1154 align => 1,
1143 valign => 1, 1155 valign => 0,
1144 text => $arg{label}, 1156 text => $arg{label},
1145 fontsize => ($arg{border} || 0.8) * 0.75; 1157 fontsize => ($arg{border} || 0.8) * 0.75;
1146 } 1158 }
1147 1159
1148 my $self = $class->SUPER::new ( 1160 my $self = $class->SUPER::new (
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
1982 $self->{cursor} =
1983 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
1984 ? length $1
1985 : 0;
1986 } elsif ($sym == CFPlus::SDLK_END) {
1987 # uh, again
1988 $self->{cursor} =
1989 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
1990 ? $self->{cursor} + length $1
1991 : length $self->{text};
1992 } elsif ($uni == 21) { # ctrl-u
1993 $text = "";
1968 $self->{cursor} = 0; 1994 $self->{cursor} = 0;
1969 } elsif ($sym == CFPlus::SDLK_END) {
1970 $self->{cursor} = length $text;
1971 } elsif ($uni == 27) { 1995 } elsif ($uni == 27) {
1972 $self->emit ('escape'); 1996 $self->emit ('escape');
1973 } elsif ($uni) { 1997 } elsif ($uni == 0x0d) {
1998 substr $text, $self->{cursor}++, 0, "\012";
1999 } elsif ($uni >= 0x20) {
1974 substr $text, $self->{cursor}++, 0, chr $uni; 2000 substr $text, $self->{cursor}++, 0, chr $uni;
1975 } else { 2001 } else {
1976 return 0; 2002 return 0;
1977 } 2003 }
1978 2004
1979 $self->_set_text ($text); 2005 $self->_set_text ($text);
1980 2006
1981 $self->realloc; 2007 $self->realloc;
2008 $self->update;
1982 2009
1983 1 2010 1
1984} 2011}
1985 2012
1986sub invoke_focus_in { 2013sub invoke_focus_in {
2055 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};
2056 glEnd; 2083 glEnd;
2057 } 2084 }
2058} 2085}
2059 2086
2087#############################################################################
2088
2060package CFPlus::UI::Entry; 2089package CFPlus::UI::Entry;
2061 2090
2062our @ISA = CFPlus::UI::EntryBase::; 2091our @ISA = CFPlus::UI::EntryBase::;
2063 2092
2064use CFPlus::OpenGL; 2093use CFPlus::OpenGL;
2066sub invoke_key_down { 2095sub invoke_key_down {
2067 my ($self, $ev) = @_; 2096 my ($self, $ev) = @_;
2068 2097
2069 my $sym = $ev->{sym}; 2098 my $sym = $ev->{sym};
2070 2099
2071 if ($sym == 13) { 2100 if ($ev->{uni} == 0x0d || $sym == 13) {
2072 unshift @{$self->{history}}, 2101 unshift @{$self->{history}},
2073 my $txt = $self->get_text; 2102 my $txt = $self->get_text;
2074 2103
2075 $self->{history_pointer} = -1; 2104 $self->{history_pointer} = -1;
2076 $self->{history_saveback} = ''; 2105 $self->{history_saveback} = '';
2106 1 2135 1
2107} 2136}
2108 2137
2109############################################################################# 2138#############################################################################
2110 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
2111package CFPlus::UI::Button; 2179package CFPlus::UI::Button;
2112 2180
2113our @ISA = CFPlus::UI::Label::; 2181our @ISA = CFPlus::UI::Label::;
2114 2182
2115use CFPlus::OpenGL; 2183use CFPlus::OpenGL;
2257 or Carp::croak "'path' or 'tex' attributes required"; 2325 or Carp::croak "'path' or 'tex' attributes required";
2258 2326
2259 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2327 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2260 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;
2261 2329
2262 Scalar::Util::weaken $texture_cache{$self->{path}}; 2330 CFPlus::weaken $texture_cache{$self->{path}};
2263 2331
2264 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2332 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2265 2333
2266 $self 2334 $self
2267} 2335}
2640sub invoke_mouse_wheel { 2708sub invoke_mouse_wheel {
2641 my ($self, $ev) = @_; 2709 my ($self, $ev) = @_;
2642 2710
2643 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2711 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2644 2712
2713 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2714
2645 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 2715 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2646 2716
2647 ! ! $delta 2717 ! ! $delta
2648} 2718}
2649 2719
2650sub update { 2720sub update {
2760 #font => default_font 2830 #font => default_font
2761 @_, 2831 @_,
2762 2832
2763 layout => (new CFPlus::Layout), 2833 layout => (new CFPlus::Layout),
2764 par => [], 2834 par => [],
2835 max_par => 0,
2765 height => 0, 2836 height => 0,
2766 children => [ 2837 children => [
2767 (new CFPlus::UI::Empty expand => 1), 2838 (new CFPlus::UI::Empty expand => 1),
2768 (new CFPlus::UI::Slider vertical => 1), 2839 (new CFPlus::UI::Slider vertical => 1),
2769 ], 2840 ],
2853} 2924}
2854 2925
2855sub current_paragraph { 2926sub current_paragraph {
2856 my ($self) = @_; 2927 my ($self) = @_;
2857 2928
2858 $self->force_uptodate;
2859 $self->{top_paragraph} - 1 2929 $self->{top_paragraph} - 1
2860} 2930}
2861 2931
2862sub scroll_to { 2932sub scroll_to {
2863 my ($self, $para) = @_; 2933 my ($self, $para) = @_;
2864 2934
2865 $self->force_uptodate;
2866
2867 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para; 2935 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2868 2936
2869 $self->{children}[1]->set_value ($self->{par}[$para]{y}); 2937 $self->{scroll_to} = $para;
2938 $self->update;
2870} 2939}
2871 2940
2872sub clear { 2941sub clear {
2873 my ($self) = @_; 2942 my ($self) = @_;
2874 2943
2896 2965
2897 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 2966 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2898 push @{$self->{par}}, $para; 2967 push @{$self->{par}}, $para;
2899 } 2968 }
2900 2969
2970 if (my $max = $self->{max_par}) {
2971 shift @{$self->{par}} while @{$self->{par}} > $max;
2972 }
2973
2901 $self->{need_reflow}++; 2974 $self->{need_reflow}++;
2902 $self->update; 2975 $self->update;
2903} 2976}
2904 2977
2905sub scroll_to_bottom { 2978sub scroll_to_bottom {
2906 my ($self) = @_; 2979 my ($self) = @_;
2907 2980
2908 $self->{scroll_to_bottom} = 1; 2981 $self->{scroll_to} = $#{$self->{par}};
2909 $self->update; 2982 $self->update;
2910} 2983}
2911 2984
2912sub force_uptodate { 2985sub force_uptodate {
2913 my ($self) = @_; 2986 my ($self) = @_;
2914 2987
2915 if (delete $self->{need_reflow}) { 2988 if (delete $self->{need_reflow}) {
2916 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2989 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2917 2990
2918 my $height = 0; 2991 my $height = 0;
2919 my $paridx;
2920 my $top_paragraph;
2921 my $top = int $self->{children}[1]{range}[0];
2922 2992
2923 for my $para (@{$self->{par}}) { 2993 for my $para (@{$self->{par}}) {
2924 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { 2994 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2925 my $layout = $self->get_layout ($para); 2995 my $layout = $self->get_layout ($para);
2926 my ($w, $h) = $layout->size; 2996 my ($w, $h) = $layout->size;
2929 $para->{h} = $h; 2999 $para->{h} = $h;
2930 $para->{wrapped} = $layout->has_wrapped; 3000 $para->{wrapped} = $layout->has_wrapped;
2931 } 3001 }
2932 3002
2933 $para->{y} = $height; 3003 $para->{y} = $height;
2934
2935 $paridx++;
2936 $top_paragraph ||= $paridx if $height >= $top;
2937
2938 $height += $para->{h}; 3004 $height += $para->{h};
2939 } 3005 }
2940 3006
2941 $self->{top_paragraph} = $top_paragraph;
2942 $self->{height} = $height; 3007 $self->{height} = $height;
2943
2944 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); 3008 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2945 3009
2946 delete $self->{texture}; 3010 delete $self->{texture};
2947 } 3011 }
2948 3012
2949 if (delete $self->{scroll_to_bottom}) { 3013 if (my $paridx = delete $self->{scroll_to}) {
2950 $self->{children}[1]->set_value (1e10); 3014 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2951 } 3015 }
2952} 3016}
2953 3017
2954sub update { 3018sub update {
2955 my ($self) = @_; 3019 my ($self) = @_;
2967 3031
2968 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3032 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2969 glClearColor 0, 0, 0, 0; 3033 glClearColor 0, 0, 0, 0;
2970 glClear GL_COLOR_BUFFER_BIT; 3034 glClear GL_COLOR_BUFFER_BIT;
2971 3035
3036 package CFPlus::UI::Base;
3037 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3038 (0, 0, $self->{w}, $self->{h});
3039
3040 my $top = int $self->{children}[1]{range}[0];
3041
3042 my $paridx = 0;
3043 my $top_paragraph;
2972 my $top = int $self->{children}[1]{range}[0]; 3044 my $top = int $self->{children}[1]{range}[0];
2973 3045
2974 my $y0 = $top; 3046 my $y0 = $top;
2975 my $y1 = $top + $H; 3047 my $y1 = $top + $H;
2976 3048
2994 3066
2995 $_->draw; 3067 $_->draw;
2996 } 3068 }
2997 } 3069 }
2998 } 3070 }
3071
3072 $paridx++;
3073 $top_paragraph ||= $paridx if $y >= $top;
2999 } 3074 }
3075
3076 $self->{top_paragraph} = $top_paragraph;
3000 }; 3077 };
3001 }); 3078 });
3002} 3079}
3003 3080
3004sub reconfigure { 3081sub reconfigure {
3213 can_events => 0, 3290 can_events => 0,
3214 @_, 3291 @_,
3215 ); 3292 );
3216 3293
3217 if ($self->{anim} && $self->{animspeed}) { 3294 if ($self->{anim} && $self->{animspeed}) {
3218 Scalar::Util::weaken (my $widget = $self); 3295 CFPlus::weaken (my $widget = $self);
3219 3296
3220 $self->{timer} = Event->timer ( 3297 $self->{timer} = Event->timer (
3221 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3298 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3222 hard => 1, 3299 hard => 1,
3223 interval => $self->{animspeed}, 3300 interval => $self->{animspeed},
3283 3360
3284package CFPlus::UI::Buttonbar; 3361package CFPlus::UI::Buttonbar;
3285 3362
3286our @ISA = CFPlus::UI::HBox::; 3363our @ISA = CFPlus::UI::HBox::;
3287 3364
3288# TODO: should actualyl wrap buttons and other goodies. 3365# TODO: should actually wrap buttons and other goodies.
3289 3366
3290############################################################################# 3367#############################################################################
3291 3368
3292package CFPlus::UI::Menu; 3369package CFPlus::UI::Menu;
3293 3370
3477} 3554}
3478 3555
3479sub add { 3556sub add {
3480 my ($self, $title, $widget, $tooltip) = @_; 3557 my ($self, $title, $widget, $tooltip) = @_;
3481 3558
3482 Scalar::Util::weaken $self; 3559 CFPlus::weaken $self;
3483 3560
3484 $self->{buttonbar}->add (new CFPlus::UI::Button 3561 $self->{buttonbar}->add (new CFPlus::UI::Button
3485 markup => $title, 3562 markup => $title,
3486 tooltip => $tooltip, 3563 tooltip => $tooltip,
3487 on_activate => sub { $self->set_current_page ($widget) }, 3564 on_activate => sub { $self->set_current_page ($widget) },
3571 my $self = $class->SUPER::new ( 3648 my $self = $class->SUPER::new (
3572 fontsize => 0.8, 3649 fontsize => 0.8,
3573 @_, 3650 @_,
3574 ); 3651 );
3575 3652
3576 Scalar::Util::weaken (my $this = $self); 3653 CFPlus::weaken (my $this = $self);
3577 3654
3578 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3655 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3579 3656
3580 $self 3657 $self
3581} 3658}
3704 $self->SUPER::destroy; 3781 $self->SUPER::destroy;
3705} 3782}
3706 3783
3707############################################################################# 3784#############################################################################
3708 3785
3709package CFPlus::UI::Inventory;
3710
3711our @ISA = CFPlus::UI::Table::;
3712
3713sub 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
3727sub 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
3749sub 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
3762sub set_items {
3763 my ($self, $items) = @_;
3764
3765 $self->{items} = [$items ? values %$items : ()];
3766 $self->update_items;
3767}
3768
3769#############################################################################
3770
3771package CFPlus::UI::SpellList;
3772
3773our @ISA = CFPlus::UI::Table::;
3774
3775sub new {
3776 my $class = shift;
3777
3778 my $self = $class->SUPER::new (
3779 binding => [],
3780 commands => [],
3781 @_,
3782 )
3783}
3784
3785my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3786
3787my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3788 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3789my @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");
3791my @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");
3793my @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");
3795my @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
3798sub 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
3870sub add_spell {
3871 my ($self, $spell) = @_;
3872
3873 $self->{spell}->{$spell->{name}} = $spell;
3874 $self->rebuild_spell_list;
3875}
3876
3877sub remove_spell {
3878 my ($self, $spell) = @_;
3879
3880 delete $self->{spell}->{$spell->{name}};
3881 $self->rebuild_spell_list;
3882}
3883
3884sub clear_spells {
3885 my ($self) = @_;
3886
3887 $self->{spell} = {};
3888 $self->rebuild_spell_list;
3889}
3890
3891#############################################################################
3892
3893package CFPlus::UI::Root; 3786package CFPlus::UI::Root;
3894 3787
3895our @ISA = CFPlus::UI::Container::; 3788our @ISA = CFPlus::UI::Container::;
3896 3789
3897use List::Util qw(min max); 3790use List::Util qw(min max);
3904 my $self = $class->SUPER::new ( 3797 my $self = $class->SUPER::new (
3905 visible => 1, 3798 visible => 1,
3906 @_, 3799 @_,
3907 ); 3800 );
3908 3801
3909 Scalar::Util::weaken ($self->{root} = $self); 3802 CFPlus::weaken ($self->{root} = $self);
3910 3803
3911 $self 3804 $self
3912} 3805}
3913 3806
3914sub size_request { 3807sub size_request {
4123 glLoadIdentity; 4016 glLoadIdentity;
4124 4017
4125 { 4018 {
4126 package CFPlus::UI::Base; 4019 package CFPlus::UI::Base;
4127 4020
4128 ($draw_x, $draw_y, $draw_w, $draw_h) = 4021 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4129 (0, 0, $self->{w}, $self->{h}); 4022 (0, 0, $self->{w}, $self->{h});
4130 }
4131 4023
4132 $self->_draw; 4024 $self->_draw;
4025 }
4133} 4026}
4134 4027
4135############################################################################# 4028#############################################################################
4136 4029
4137package CFPlus::UI; 4030package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines