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.343 by root, Sun Aug 13 15:14:17 2006 UTC vs.
Revision 1.395 by root, Sat Jul 21 13:56:00 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;
23our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $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
51sub get_layout { 53sub get_layout {
52 my $layout; 54 my $layout;
111 } 113 }
112 114
113 if ($GRAB) { 115 if ($GRAB) {
114 if ($ev->{button} == 4 || $ev->{button} == 5) { 116 if ($ev->{button} == 4 || $ev->{button} == 5) {
115 # mousewheel 117 # mousewheel
116 $ev->{dx} = 0;
117 $ev->{dy} = $ev->{button} * 2 - 9; 118 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
120
121 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta;
123
118 $GRAB->emit (mouse_wheel => $ev); 124 $GRAB->emit (mouse_wheel => $ev);
119 } else { 125 } else {
120 $GRAB->emit (button_down => $ev) 126 $GRAB->emit (button_down => $ev)
121 } 127 }
122 } 128 }
235 h => undef, 241 h => undef,
236 can_events => 1, 242 can_events => 1,
237 @_ 243 @_
238 }, $class; 244 }, $class;
239 245
240 Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
241 247
242 for (keys %$self) { 248 for (keys %$self) {
243 if (/^on_(.*)$/) { 249 if (/^on_(.*)$/) {
244 $self->connect ($1 => delete $self->{$_}); 250 $self->connect ($1 => delete $self->{$_});
245 } 251 }
262 268
263sub destroy { 269sub destroy {
264 my ($self) = @_; 270 my ($self) = @_;
265 271
266 $self->hide; 272 $self->hide;
273 $self->emit ("destroy");
267 %$self = (); 274 %$self = ();
275}
276
277sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} }
268} 279}
269 280
270sub show { 281sub show {
271 my ($self) = @_; 282 my ($self) = @_;
272 283
313sub set_visibility { 324sub set_visibility {
314 my ($self, $visible) = @_; 325 my ($self, $visible) = @_;
315 326
316 return if $self->{visible} == $visible; 327 return if $self->{visible} == $visible;
317 328
318 $visible ? $self->hide 329 $visible ? $self->show
319 : $self->show; 330 : $self->hide;
320} 331}
321 332
322sub toggle_visibility { 333sub toggle_visibility {
323 my ($self) = @_; 334 my ($self) = @_;
324 335
502 513
503 defined wantarray and CFPlus::guard { 514 defined wantarray and CFPlus::guard {
504 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
505 @{ $self->{signal_cb}{$signal} }; 516 @{ $self->{signal_cb}{$signal} };
506 } 517 }
518}
519
520sub disconnect_all {
521 my ($self, $signal) = @_;
522
523 delete $self->{signal_cb}{$signal};
507} 524}
508 525
509my %has_coords = ( 526my %has_coords = (
510 button_down => 1, 527 button_down => 1,
511 button_up => 1, 528 button_up => 1,
514); 531);
515 532
516sub emit { 533sub emit {
517 my ($self, $signal, @args) = @_; 534 my ($self, $signal, @args) = @_;
518 535
519 # I do not really like this solution, but I dislike duplication 536 # I do not really like this solution, but I do not like duplication
520 # and needlessly verbose code, too. 537 # and needlessly verbose code, either.
521 my @append 538 my @append
522 = $has_coords{$signal} 539 = $has_coords{$signal}
523 ? $args[0]->xy ($self) 540 ? $args[0]->xy ($self)
524 : (); 541 : ();
525 542
526 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 543 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
527 544
528 #d##TODO# stop propagating at first true, do not use sum 545 for my $cb (
529 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 546 @{$self->{signal_cb}{$signal} || []}, # before
530 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 547 ($self->can ("invoke_$signal") || sub { 1 }), # closure
548 ) {
549 return $cb->($self, @args, @append) || next;
550 }
551
552 # parent
531 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 553 $self->{parent} && $self->{parent}->emit ($signal, @args)
532} 554}
533 555
534sub find_widget { 556#sub find_widget {
535 my ($self, $x, $y) = @_; 557# in .xs
536
537 return () unless $self->{can_events};
538
539 return $self
540 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
541 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
542
543 ()
544}
545 558
546sub set_parent { 559sub set_parent {
547 my ($self, $parent) = @_; 560 my ($self, $parent) = @_;
548 561
549 Scalar::Util::weaken ($self->{parent} = $parent); 562 CFPlus::weaken ($self->{parent} = $parent);
550 $self->set_visible if $parent->{visible}; 563 $self->set_visible if $parent->{visible};
551} 564}
552 565
553sub realloc { 566sub realloc {
554 my ($self) = @_; 567 my ($self) = @_;
633 my ($self) = @_; 646 my ($self) = @_;
634 647
635 warn "no draw defined for $self\n"; 648 warn "no draw defined for $self\n";
636} 649}
637 650
651my $cntx;#d#
638sub DESTROY { 652sub DESTROY {
639 my ($self) = @_; 653 my ($self) = @_;
640 654
641 return if CFPlus::in_destruct; 655 return if CFPlus::in_destruct;
642
643 delete $WIDGET{$self+0};
644 656
645 eval { $self->destroy }; 657 eval { $self->destroy };
646 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659
660 delete $WIDGET{$self+0};
647} 661}
648 662
649############################################################################# 663#############################################################################
650 664
651package CFPlus::UI::DrawBG; 665package CFPlus::UI::DrawBG;
727 can_events => 0, 741 can_events => 0,
728 %arg, 742 %arg,
729 ); 743 );
730 744
731 $self->add (@$children) 745 $self->add (@$children)
732 if $children; 746 if $children && @$children;
733 747
734 $self 748 $self
735} 749}
736 750
737sub realloc { 751sub realloc {
746 my ($self, @widgets) = @_; 760 my ($self, @widgets) = @_;
747 761
748 $_->set_parent ($self) 762 $_->set_parent ($self)
749 for @widgets; 763 for @widgets;
750 764
765 # TODO: only do this in widgets that need it, e.g. root, fixed
751 use sort 'stable'; 766 use sort 'stable';
752 767
753 $self->{children} = [ 768 $self->{children} = [
754 sort { $a->{z} <=> $b->{z} } 769 sort { $a->{z} <=> $b->{z} }
755 @{$self->{children}}, @widgets 770 @{$self->{children}}, @widgets
774} 789}
775 790
776sub clear { 791sub clear {
777 my ($self) = @_; 792 my ($self) = @_;
778 793
779 my $children = delete $self->{children}; 794 my $children = $self->{children};
780 $self->{children} = []; 795 $self->{children} = [];
781 796
782 for (@$children) { 797 for (@$children) {
783 delete $_->{parent}; 798 delete $_->{parent};
784 $_->hide; 799 $_->hide;
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
911sub _draw { 926sub _draw {
912 my ($self) = @_; 927 my ($self) = @_;
913
914 my ($w, $h) = @$self{qw(w h)};
915 928
916 my $tex = $self->{texture} 929 my $tex = $self->{texture}
917 or return; 930 or return;
918 931
919 glEnable GL_TEXTURE_2D; 932 glEnable GL_TEXTURE_2D;
920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 933 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
921 glColor 0, 0, 0, 1; 934 glColor 0, 0, 0, 1;
922 935
923 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 936 $tex->draw_quad_alpha_premultiplied (0, 0);
924 937
925 glDisable GL_TEXTURE_2D; 938 glDisable GL_TEXTURE_2D;
926} 939}
927 940
928############################################################################# 941#############################################################################
929 942
930package CFPlus::UI::ViewPort; 943package CFPlus::UI::ViewPort;
944
945use List::Util qw(min max);
931 946
932our @ISA = CFPlus::UI::Window::; 947our @ISA = CFPlus::UI::Window::;
933 948
934sub new { 949sub new {
935 my $class = shift; 950 my $class = shift;
944sub size_request { 959sub size_request {
945 my ($self) = @_; 960 my ($self) = @_;
946 961
947 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 962 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
948 963
949 $w = 10 if $self->{scroll_x}; 964 $w = 1 if $self->{scroll_x};
950 $h = 10 if $self->{scroll_y}; 965 $h = 1 if $self->{scroll_y};
951 966
952 ($w, $h) 967 ($w, $h)
953} 968}
954 969
955sub invoke_size_allocate { 970sub invoke_size_allocate {
967} 982}
968 983
969sub set_offset { 984sub set_offset {
970 my ($self, $x, $y) = @_; 985 my ($self, $x, $y) = @_;
971 986
987 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
988 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
989
990 if ($x != $self->{view_x} or $y != $self->{view_y}) {
972 $self->{view_x} = int $x; 991 $self->{view_x} = $x;
973 $self->{view_y} = int $y; 992 $self->{view_y} = $y;
974 993
994 $self->emit (changed => $x, $y);
975 $self->update; 995 $self->update;
996 }
976} 997}
977 998
978# hmm, this does not work for topleft of $self... but we should not ask for that 999# hmm, this does not work for topleft of $self... but we should not ask for that
979sub coord2local { 1000sub coord2local {
980 my ($self, $x, $y) = @_; 1001 my ($self, $x, $y) = @_;
995 my ($self, $x, $y) = @_; 1016 my ($self, $x, $y) = @_;
996 1017
997 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
998 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
999 ) { 1020 ) {
1000 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1001 } else { 1022 } else {
1002 $self->CFPlus::UI::Base::find_widget ($x, $y) 1023 $self->CFPlus::UI::Base::find_widget ($x, $y)
1003 } 1024 }
1004} 1025}
1005 1026
1016 1037
1017############################################################################# 1038#############################################################################
1018 1039
1019package CFPlus::UI::ScrolledWindow; 1040package CFPlus::UI::ScrolledWindow;
1020 1041
1021our @ISA = CFPlus::UI::HBox::; 1042our @ISA = CFPlus::UI::Table::;
1022 1043
1023sub new { 1044sub new {
1024 my ($class, %arg) = @_; 1045 my ($class, %arg) = @_;
1025 1046
1026 my $child = delete $arg{child}; 1047 my $child = delete $arg{child};
1027 1048
1028 my $self; 1049 my $self;
1029 1050
1030 my $slider = new CFPlus::UI::Slider 1051 my $hslider = new CFPlus::UI::Slider
1052 col => 0,
1053 row => 1,
1054 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub {
1057 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 },
1060 ;
1061
1062 my $vslider = new CFPlus::UI::Slider
1063 col => 1,
1064 row => 0,
1031 vertical => 1, 1065 vertical => 1,
1032 range => [0, 0, 1, 0.01], # HACK fix 1066 range => [0, 0, 1, 0.01], # HACK fix
1033 on_changed => sub { 1067 on_changed => sub {
1034 $self->{vp}->set_offset (0, $_[1]); 1068 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1035 }, 1070 },
1036 ; 1071 ;
1037 1072
1038 $self = $class->SUPER::new ( 1073 $self = $class->SUPER::new (
1039 vp => (new CFPlus::UI::ViewPort expand => 1), 1074 scroll_x => 0,
1075 scroll_y => 1,
1040 can_events => 1, 1076 can_events => 1,
1041 slider => $slider, 1077 hslider => $hslider,
1078 vslider => $vslider,
1079 col_expand => [1, 0],
1080 row_expand => [1, 0],
1042 %arg, 1081 %arg,
1043 ); 1082 );
1044 1083
1084 $self->{vp} = new CFPlus::UI::ViewPort
1085 col => 0,
1086 row => 0,
1087 expand => 1,
1088 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y},
1090 on_changed => sub {
1091 my ($vp, $x, $y) = @_;
1092
1093 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y);
1095
1096 0
1097 },
1098 ;
1099
1045 $self->SUPER::add ($self->{vp}, $self->{slider}); 1100 $self->SUPER::add ($self->{vp});
1101
1046 $self->add ($child) if $child; 1102 $self->add ($child) if $child;
1047 1103
1048 $self 1104 $self
1049} 1105}
1050
1051#TODO# update range on size_allocate depending on child
1052 1106
1053sub add { 1107sub add {
1054 my ($self, $widget) = @_; 1108 my ($self, $widget) = @_;
1055 1109
1056 $self->{vp}->add ($self->{child} = $widget); 1110 $self->{vp}->add ($self->{child} = $widget);
1057} 1111}
1058 1112
1113sub update_slider {
1114 my ($self) = @_;
1115
1116 my $child = ($self->{vp} or return)->child;
1117
1118 my ($w1, $w2) = ($child->{w}, $self->{vp}{w});
1119 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1120
1121 my $visible = $w1 > $w2;
1122 if ($visible != $self->{hslider_visible}) {
1123 $self->{hslider_visible} = $visible;
1124 $visible ? $self->SUPER::add ($self->{hslider})
1125 : $self->SUPER::remove ($self->{hslider});
1126 }
1127
1128 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1129 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1130
1131 my $visible = $h1 > $h2;
1132 if ($visible != $self->{vslider_visible}) {
1133 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider});
1136 }
1137}
1138
1059sub invoke_mouse_wheel { 1139sub invoke_mouse_wheel {
1060 my ($self, $ev) = @_; 1140 my ($self, $ev) = @_;
1061 1141
1062 return 0 unless $ev->{dy}; # only vertical movements
1063
1064 $self->{slider}->emit (mouse_wheel => $ev); 1142 $self->{vslider}->emit (mouse_wheel => $ev)
1143 if $ev->{dy};
1144
1145 $self->{hslider}->emit (mouse_wheel => $ev)
1146 if $ev->{dx};
1065 1147
1066 1 1148 1
1067} 1149}
1068 1150
1069sub update_slider { 1151sub invoke_button_down {
1070 my ($self) = @_; 1152 my ($self, $ev, $x, $y) = @_;
1071 1153
1072 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1154 if ($ev->{button} == 2) {
1073} 1155 $self->grab_focus;
1074 1156
1075sub update { 1157 my $ox = $self->{vp}{view_x} + $ev->{x};
1076 my ($self) = @_; 1158 my $oy = $self->{vp}{view_y} + $ev->{y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1077 1162
1078 $self->SUPER::update; 1163 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1164 $self->update;
1165 };
1079 1166
1080 $self->update_slider; 1167 return 1;
1168 }
1169
1170 0
1171}
1172
1173sub invoke_button_up {
1174 my ($self, $ev, $x, $y) = @_;
1175
1176 if (delete $self->{motion}) {
1177 return 1;
1178 }
1179
1180 0
1181}
1182
1183sub invoke_mouse_motion {
1184 my ($self, $ev, $x, $y) = @_;
1185
1186 if ($self->{motion}) {
1187 $self->{motion}->($ev, $x, $y);
1188 return 1;
1189 }
1190
1191 0
1081} 1192}
1082 1193
1083sub invoke_size_allocate { 1194sub invoke_size_allocate {
1084 my ($self, $w, $h) = @_; 1195 my ($self, $w, $h) = @_;
1085 1196
1086 $self->update_slider; 1197 $self->update_slider;
1087
1088 $self->SUPER::invoke_size_allocate ($w, $h) 1198 $self->SUPER::invoke_size_allocate ($w, $h)
1089} 1199}
1090 1200
1091############################################################################# 1201#############################################################################
1092 1202
1134 1244
1135our @ISA = CFPlus::UI::Bin::; 1245our @ISA = CFPlus::UI::Bin::;
1136 1246
1137use CFPlus::OpenGL; 1247use CFPlus::OpenGL;
1138 1248
1249sub new {
1250 my ($class, %arg) = @_;
1251
1252 if ((exists $arg{label}) && !ref $arg{label}) {
1253 $arg{label} = new CFPlus::UI::Label
1254 align => 1,
1255 valign => 0,
1256 text => $arg{label},
1257 fontsize => ($arg{border} || 0.8) * 0.75;
1258 }
1259
1260 my $self = $class->SUPER::new (
1261 # label => "",
1262 fg => [0.6, 0.3, 0.1],
1263 border => 0.8,
1264 style => 'single',
1265 %arg,
1266 );
1267
1268 $self
1269}
1270
1271sub add {
1272 my ($self, @widgets) = @_;
1273
1274 $self->SUPER::add (@widgets);
1275 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1276}
1277
1278sub border {
1279 int $_[0]{border} * $::FONTSIZE
1280}
1281
1282sub size_request {
1283 my ($self) = @_;
1284
1285 ($self->{label_w}, undef) = $self->{label}->size_request
1286 if $self->{label};
1287
1288 my ($w, $h) = $self->SUPER::size_request;
1289
1290 (
1291 $w + $self->border * 2,
1292 $h + $self->border * 2,
1293 )
1294}
1295
1296sub invoke_size_allocate {
1297 my ($self, $w, $h) = @_;
1298
1299 my $border = $self->border;
1300
1301 $w -= List::Util::max 0, $border * 2;
1302 $h -= List::Util::max 0, $border * 2;
1303
1304 if (my $label = $self->{label}) {
1305 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1306 $label->{h} = List::Util::min $h, $border;
1307 $label->invoke_size_allocate ($label->{w}, $label->{h});
1308 }
1309
1310 $self->child->configure ($border, $border, $w, $h);
1311
1312 1
1313}
1314
1315sub _draw {
1316 my ($self) = @_;
1317
1318 my $child = $self->{children}[0];
1319
1320 my $border = $self->border;
1321 my ($w, $h) = ($self->{w}, $self->{h});
1322
1323 $child->draw;
1324
1325 glColor @{$self->{fg}};
1326 glBegin GL_LINE_STRIP;
1327 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1328 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1329 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1330 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1331 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1332 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1333 glEnd;
1334
1335 if ($self->{label}) {
1336 glTranslate $border * 2, 0;
1337 $self->{label}->_draw;
1338 }
1339}
1340
1341#############################################################################
1342
1343package CFPlus::UI::Toplevel;
1344
1345our @ISA = CFPlus::UI::Bin::;
1346
1347use CFPlus::OpenGL;
1348
1139my $bg = 1349my $bg =
1140 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1350 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png",
1141 mipmap => 1, wrap => 1; 1351 mipmap => 1, wrap => 1;
1142 1352
1143my @border = 1353my @border =
1144 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1354 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1145 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1355 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1356
1357my @icon =
1358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1359 qw(x1_move.png x1_resize.png);
1146 1360
1147sub new { 1361sub new {
1148 my ($class, %arg) = @_; 1362 my ($class, %arg) = @_;
1149 1363
1150 my $self = $class->SUPER::new ( 1364 my $self = $class->SUPER::new (
1319 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1320 1534
1321 my $border = $self->border; 1535 my $border = $self->border;
1322 1536
1323 glColor @{ $self->{border_bg} }; 1537 glColor @{ $self->{border_bg} };
1324 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1325 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1326 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1327 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1541 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1542
1543 # move
1544 my $w2 = ($w - $border) * .5;
1545 my $h2 = ($h - $border) * .5;
1546 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1547 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1548 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1549
1550 # resize
1551 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1553 unless $self->{has_close_button};
1554 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1555 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1328 1556
1329 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1330 glColor @{ $self->{bg} }; 1558 glColor @{ $self->{bg} };
1331 1559
1332 # TODO: repeat texture not scale 1560 # TODO: repeat texture not scale
1353 1581
1354############################################################################# 1582#############################################################################
1355 1583
1356package CFPlus::UI::Table; 1584package CFPlus::UI::Table;
1357 1585
1358our @ISA = CFPlus::UI::Base::; 1586our @ISA = CFPlus::UI::Container::;
1359 1587
1360use List::Util qw(max sum); 1588use List::Util qw(max sum);
1361 1589
1362use CFPlus::OpenGL; 1590use CFPlus::OpenGL;
1363 1591
1364sub new { 1592sub new {
1365 my $class = shift; 1593 my $class = shift;
1366 1594
1367 $class->SUPER::new ( 1595 $class->SUPER::new (
1368 col_expand => [], 1596 col_expand => [],
1597 row_expand => [],
1369 @_, 1598 @_,
1370 ) 1599 )
1371} 1600}
1372 1601
1373sub children {
1374 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1375}
1376
1377sub add { 1602sub add {
1603 my ($self, @widgets) = @_;
1604
1605 for my $child (@widgets) {
1606 $child->{rowspan} ||= 1;
1607 $child->{colspan} ||= 1;
1608 }
1609
1610 $self->SUPER::add (@widgets);
1611}
1612
1613sub add_at {
1378 my ($self) = shift; 1614 my $self = shift;
1615
1616 my @widgets;
1379 1617
1380 while (@_) { 1618 while (@_) {
1381 my ($x, $y, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1382 $child->set_parent ($self);
1383 $self->{children}[$y][$x] = $child;
1384 }
1385 1620
1386 $self->{force_realloc} = 1; 1621 $child->{row} = $row;
1387 $self->{force_size_alloc} = 1; 1622 $child->{col} = $col;
1388 $self->realloc;
1389}
1390 1623
1391sub remove { 1624 push @widgets, $child;
1625 }
1626
1627 $self->add (@widgets);
1628}
1629
1630sub get_wh {
1392 my ($self, $child) = @_; 1631 my ($self) = @_;
1393 1632
1394 # TODO: not yet implemented 1633 my (@w, @h);
1395}
1396
1397# TODO: move to container class maybe? send children a signal on removal?
1398sub clear {
1399 my ($self) = @_;
1400 1634
1401 my @children = $self->children; 1635 my @children = $self->children;
1402 delete $self->{children}; 1636
1637 # first pass, columns
1638 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) {
1639 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)};
1640
1641 my $sw = sum @w[$c .. $c + $cs - 1];
1642
1643 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 }
1403 1646 }
1404 for (@children) {
1405 delete $_->{parent};
1406 $_->hide;
1407 }
1408 1647
1409 $self->realloc; 1648 # second pass, rows
1410} 1649 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) {
1411
1412sub get_wh {
1413 my ($self) = @_;
1414
1415 my (@w, @h);
1416
1417 for my $y (0 .. $#{$self->{children}}) {
1418 my $row = $self->{children}[$y]
1419 or next;
1420
1421 for my $x (0 .. $#$row) {
1422 my $widget = $row->[$x]
1423 or next;
1424 my ($w, $h) = @$widget{qw(req_w req_h)}; 1650 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)};
1425 1651
1426 $w[$x] = max $w[$x], $w; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1427 $h[$y] = max $h[$y], $h; 1653
1654 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1428 } 1656 }
1429 } 1657 }
1430 1658
1431 (\@w, \@h) 1659 (\@w, \@h)
1432} 1660}
1448 my ($ws, $hs) = $self->get_wh; 1676 my ($ws, $hs) = $self->get_wh;
1449 1677
1450 my $req_w = (sum @$ws) || 1; 1678 my $req_w = (sum @$ws) || 1;
1451 my $req_h = (sum @$hs) || 1; 1679 my $req_h = (sum @$hs) || 1;
1452 1680
1453 # TODO: nicer code && do row_expand 1681 # now linearly scale the rows/columns to the allocated size
1454 my @col_expand = @{$self->{col_expand}}; 1682 my @col_expand = @{$self->{col_expand}};
1455 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1456 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1457 1685
1458 # linearly scale sizes
1459 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1460 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1461 1687
1462 CFPlus::UI::harmonize $ws; 1688 CFPlus::UI::harmonize $ws;
1689
1690 my @row_expand = @{$self->{row_expand}};
1691 @row_expand = (1) x @$ws unless @row_expand;
1692 my $row_expand = (sum @row_expand) || 1;
1693
1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1695
1463 CFPlus::UI::harmonize $hs; 1696 CFPlus::UI::harmonize $hs;
1464 1697
1465 my $y; 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1466 1700
1467 for my $r (0 .. $#{$self->{children}}) { 1701 for my $widget ($self->children) {
1468 my $row = $self->{children}[$r] 1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)};
1469 or next;
1470 1703
1471 my $x = 0; 1704 $widget->configure (
1472 my $row_h = $hs->[$r]; 1705 $x[$c], $y[$r],
1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1473 1707 );
1474 for my $c (0 .. $#$row) { 1708 }
1475 my $col_w = $ws->[$c];
1476 1709
1477 if (my $widget = $row->[$c]) { 1710 1
1478 $widget->configure ($x, $y, $col_w, $row_h); 1711}
1479 }
1480 1712
1481 $x += $col_w; 1713#############################################################################
1714
1715package CFPlus::UI::Fixed;
1716
1717use List::Util qw(min max);
1718
1719our @ISA = CFPlus::UI::Container::;
1720
1721sub add {
1722 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1723
1724 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1725 $self->SUPER::add ($child);
1726}
1727
1728sub _scale($$$) {
1729 my ($mode, $val, $max) = @_;
1730
1731 $mode eq "abs" ? $val
1732 : $mode eq "rel" ? $val * $max
1733 : 0
1734}
1735
1736sub size_request {
1737 my ($self) = @_;
1738
1739 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1740
1741 # determine overall size by querying abs widgets
1742 for my $child ($self->visible_children) {
1743 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1744
1745 if ($pos eq "abs") {
1746 $w = _scale $size, $w, $child->{req_w};
1747 $h = _scale $size, $h, $child->{req_h};
1748
1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1482 } 1751 }
1752 }
1483 1753
1484 $y += $row_h; 1754 my $W = $x2 - $x1;
1755 my $H = $y2 - $y1;
1756
1757 # now layout remaining widgets
1758 for my $child ($self->visible_children) {
1759 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1760
1761 if ($pos ne "abs") {
1762 $x = _scale $pos, $x, $W;
1763 $y = _scale $pos, $x, $H;
1764 $w = _scale $size, $w, $child->{req_w};
1765 $h = _scale $size, $h, $child->{req_h};
1766
1767 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1768 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1769 }
1770 }
1771
1772 my $W = $x2 - $x1;
1773 my $H = $y2 - $y1;
1774
1775 ($W, $H)
1776}
1777
1778sub invoke_size_allocate {
1779 my ($self, $W, $H) = @_;
1780
1781 for my $child ($self->visible_children) {
1782 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1783
1784 $x = _scale $pos, $x, $W;
1785 $y = _scale $pos, $x, $H;
1786 $w = _scale $size, $w, $child->{req_w};
1787 $h = _scale $size, $h, $child->{req_h};
1788
1789 $child->configure ($x, $y, $w, $h);
1485 } 1790 }
1486 1791
1487 1 1792 1
1488}
1489
1490sub find_widget {
1491 my ($self, $x, $y) = @_;
1492
1493 $x -= $self->{x};
1494 $y -= $self->{y};
1495
1496 my $res;
1497
1498 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1499 $res = $_->find_widget ($x, $y)
1500 and return $res;
1501 }
1502
1503 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1504}
1505
1506sub _draw {
1507 my ($self) = @_;
1508
1509 for (grep $_, @{$self->{children}}) {
1510 $_->draw for grep $_, @$_;
1511 }
1512} 1793}
1513 1794
1514############################################################################# 1795#############################################################################
1515 1796
1516package CFPlus::UI::Box; 1797package CFPlus::UI::Box;
1802 active_fg => [0, 0, 0], 2083 active_fg => [0, 0, 0],
1803 can_hover => 1, 2084 can_hover => 1,
1804 can_focus => 1, 2085 can_focus => 1,
1805 valign => 0, 2086 valign => 0,
1806 can_events => 1, 2087 can_events => 1,
2088 ellipsise => 0,
1807 #text => ... 2089 #text => ...
1808 #hidden => "*", 2090 #hidden => "*",
1809 @_ 2091 @_
1810 ) 2092 )
1811} 2093}
1855 my $mod = $ev->{mod}; 2137 my $mod = $ev->{mod};
1856 my $sym = $ev->{sym}; 2138 my $sym = $ev->{sym};
1857 my $uni = $ev->{unicode}; 2139 my $uni = $ev->{unicode};
1858 2140
1859 my $text = $self->get_text; 2141 my $text = $self->get_text;
2142
2143 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
1860 2144
1861 if ($uni == 8) { 2145 if ($uni == 8) {
1862 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2146 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1863 } elsif ($uni == 127) { 2147 } elsif ($uni == 127) {
1864 substr $text, $self->{cursor}, 1, ""; 2148 substr $text, $self->{cursor}, 1, "";
1865 } elsif ($sym == CFPlus::SDLK_LEFT) { 2149 } elsif ($sym == CFPlus::SDLK_LEFT) {
1866 --$self->{cursor} if $self->{cursor}; 2150 --$self->{cursor} if $self->{cursor};
1867 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2151 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1868 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2152 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1869 } elsif ($sym == CFPlus::SDLK_HOME) { 2153 } elsif ($sym == CFPlus::SDLK_HOME) {
2154 # what a hack
2155 $self->{cursor} =
2156 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2157 ? length $1
2158 : 0;
2159 } elsif ($sym == CFPlus::SDLK_END) {
2160 # uh, again
2161 $self->{cursor} =
2162 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2163 ? $self->{cursor} + length $1
2164 : length $self->{text};
2165 } elsif ($uni == 21) { # ctrl-u
2166 $text = "";
1870 $self->{cursor} = 0; 2167 $self->{cursor} = 0;
1871 } elsif ($sym == CFPlus::SDLK_END) {
1872 $self->{cursor} = length $text;
1873 } elsif ($uni == 27) { 2168 } elsif ($uni == 27) {
1874 $self->emit ('escape'); 2169 $self->emit ('escape');
1875 } elsif ($uni) { 2170 } elsif ($uni == 0x0d) {
2171 substr $text, $self->{cursor}++, 0, "\012";
2172 } elsif ($uni >= 0x20) {
1876 substr $text, $self->{cursor}++, 0, chr $uni; 2173 substr $text, $self->{cursor}++, 0, chr $uni;
1877 } else { 2174 } else {
1878 return 0; 2175 return 0;
1879 } 2176 }
1880 2177
1881 $self->_set_text ($text); 2178 $self->_set_text ($text);
1882 2179
1883 $self->realloc; 2180 $self->realloc;
2181 $self->update;
1884 2182
1885 1 2183 1
1886} 2184}
1887 2185
1888sub invoke_focus_in { 2186sub invoke_focus_in {
1950 utf8::encode $text; 2248 utf8::encode $text;
1951 2249
1952 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2250 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1953 } 2251 }
1954 2252
1955 glColor @{$self->{fg}};
1956 glBegin GL_LINES; 2253 glBegin GL_LINES;
1957 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2254 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1958 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2255 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1959 glEnd; 2256 glEnd;
1960 } 2257 }
1961} 2258}
2259
2260#############################################################################
1962 2261
1963package CFPlus::UI::Entry; 2262package CFPlus::UI::Entry;
1964 2263
1965our @ISA = CFPlus::UI::EntryBase::; 2264our @ISA = CFPlus::UI::EntryBase::;
1966 2265
1969sub invoke_key_down { 2268sub invoke_key_down {
1970 my ($self, $ev) = @_; 2269 my ($self, $ev) = @_;
1971 2270
1972 my $sym = $ev->{sym}; 2271 my $sym = $ev->{sym};
1973 2272
1974 if ($sym == 13) { 2273 if ($ev->{uni} == 0x0d || $sym == 13) {
1975 unshift @{$self->{history}}, 2274 unshift @{$self->{history}},
1976 my $txt = $self->get_text; 2275 my $txt = $self->get_text;
1977 2276
1978 $self->{history_pointer} = -1; 2277 $self->{history_pointer} = -1;
1979 $self->{history_saveback} = ''; 2278 $self->{history_saveback} = '';
2009 1 2308 1
2010} 2309}
2011 2310
2012############################################################################# 2311#############################################################################
2013 2312
2313package CFPlus::UI::TextEdit;
2314
2315our @ISA = CFPlus::UI::EntryBase::;
2316
2317use CFPlus::OpenGL;
2318
2319sub move_cursor_ver {
2320 my ($self, $dy) = @_;
2321
2322 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2323
2324 $y += $dy;
2325
2326 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2327 $self->{cursor} = $index;
2328 delete $self->{cur_h};
2329 $self->update;
2330 return;
2331 }
2332}
2333
2334sub invoke_key_down {
2335 my ($self, $ev) = @_;
2336
2337 my $sym = $ev->{sym};
2338
2339 if ($sym == CFPlus::SDLK_UP) {
2340 $self->move_cursor_ver (-1);
2341 } elsif ($sym == CFPlus::SDLK_DOWN) {
2342 $self->move_cursor_ver (+1);
2343 } else {
2344 return $self->SUPER::invoke_key_down ($ev)
2345 }
2346
2347 1
2348}
2349
2350#############################################################################
2351
2014package CFPlus::UI::Button; 2352package CFPlus::UI::Button;
2015 2353
2016our @ISA = CFPlus::UI::Label::; 2354our @ISA = CFPlus::UI::Label::;
2017 2355
2018use CFPlus::OpenGL; 2356use CFPlus::OpenGL;
2019 2357
2020my @tex = 2358my @tex =
2021 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2359 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2022 qw(b1_button_active.png); 2360 qw(b1_button_inactive.png b1_button_active.png);
2023 2361
2024sub new { 2362sub new {
2025 my $class = shift; 2363 my $class = shift;
2026 2364
2027 $class->SUPER::new ( 2365 $class->SUPER::new (
2028 padding_x => 4, 2366 padding_x => 4,
2029 padding_y => 4, 2367 padding_y => 4,
2030 fg => [1, 1, 1], 2368 fg => [1.0, 1.0, 1.0],
2031 active_fg => [0, 0, 1], 2369 active_fg => [0.8, 0.8, 0.8],
2032 can_hover => 1, 2370 can_hover => 1,
2033 align => 0, 2371 align => 0,
2034 valign => 0, 2372 valign => 0,
2035 can_events => 1, 2373 can_events => 1,
2036 @_ 2374 @_
2054 2392
2055 glEnable GL_TEXTURE_2D; 2393 glEnable GL_TEXTURE_2D;
2056 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2394 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2057 glColor 0, 0, 0, 1; 2395 glColor 0, 0, 0, 1;
2058 2396
2397 my $tex = $tex[$GRAB == $self];
2059 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2398 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2060 2399
2061 glDisable GL_TEXTURE_2D; 2400 glDisable GL_TEXTURE_2D;
2062 2401
2063 $self->SUPER::_draw; 2402 $self->SUPER::_draw;
2064} 2403}
2160 or Carp::croak "'path' or 'tex' attributes required"; 2499 or Carp::croak "'path' or 'tex' attributes required";
2161 2500
2162 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2501 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2163 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2502 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2164 2503
2165 Scalar::Util::weaken $texture_cache{$self->{path}}; 2504 CFPlus::weaken $texture_cache{$self->{path}};
2166 2505
2167 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2506 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2168 2507
2169 $self 2508 $self
2170} 2509}
2543sub invoke_mouse_wheel { 2882sub invoke_mouse_wheel {
2544 my ($self, $ev) = @_; 2883 my ($self, $ev) = @_;
2545 2884
2546 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2885 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2547 2886
2887 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2888
2548 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 2889 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2549 2890
2550 ! ! $delta 2891 ! ! $delta
2551} 2892}
2552 2893
2553sub update { 2894sub update {
2663 #font => default_font 3004 #font => default_font
2664 @_, 3005 @_,
2665 3006
2666 layout => (new CFPlus::Layout), 3007 layout => (new CFPlus::Layout),
2667 par => [], 3008 par => [],
3009 max_par => 0,
2668 height => 0, 3010 height => 0,
2669 children => [ 3011 children => [
2670 (new CFPlus::UI::Empty expand => 1), 3012 (new CFPlus::UI::Empty expand => 1),
2671 (new CFPlus::UI::Slider vertical => 1), 3013 (new CFPlus::UI::Slider vertical => 1),
2672 ], 3014 ],
2751sub set_offset { 3093sub set_offset {
2752 my ($self, $offset) = @_; 3094 my ($self, $offset) = @_;
2753 3095
2754 # todo: base offset on lines or so, not on pixels 3096 # todo: base offset on lines or so, not on pixels
2755 $self->{children}[1]->set_value ($offset); 3097 $self->{children}[1]->set_value ($offset);
3098}
3099
3100sub current_paragraph {
3101 my ($self) = @_;
3102
3103 $self->{top_paragraph} - 1
3104}
3105
3106sub scroll_to {
3107 my ($self, $para) = @_;
3108
3109 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3110
3111 $self->{scroll_to} = $para;
3112 $self->update;
2756} 3113}
2757 3114
2758sub clear { 3115sub clear {
2759 my ($self) = @_; 3116 my ($self) = @_;
2760 3117
2782 3139
2783 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3140 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2784 push @{$self->{par}}, $para; 3141 push @{$self->{par}}, $para;
2785 } 3142 }
2786 3143
3144 if (my $max = $self->{max_par}) {
3145 shift @{$self->{par}} while @{$self->{par}} > $max;
3146 }
3147
2787 $self->{need_reflow}++; 3148 $self->{need_reflow}++;
2788 $self->update; 3149 $self->update;
2789} 3150}
2790 3151
2791sub scroll_to_bottom { 3152sub scroll_to_bottom {
2792 my ($self) = @_; 3153 my ($self) = @_;
2793 3154
2794 $self->{scroll_to_bottom} = 1; 3155 $self->{scroll_to} = $#{$self->{par}};
2795 $self->update; 3156 $self->update;
2796} 3157}
2797 3158
3159sub force_uptodate {
3160 my ($self) = @_;
3161
3162 if (delete $self->{need_reflow}) {
3163 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3164
3165 my $height = 0;
3166
3167 for my $para (@{$self->{par}}) {
3168 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3169 my $layout = $self->get_layout ($para);
3170 my ($w, $h) = $layout->size;
3171
3172 $para->{w} = $w + $para->{indent};
3173 $para->{h} = $h;
3174 $para->{wrapped} = $layout->has_wrapped;
3175 }
3176
3177 $para->{y} = $height;
3178 $height += $para->{h};
3179 }
3180
3181 $self->{height} = $height;
3182 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3183
3184 delete $self->{texture};
3185 }
3186
3187 if (my $paridx = delete $self->{scroll_to}) {
3188 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3189 }
3190}
3191
2798sub update { 3192sub update {
2799 my ($self) = @_; 3193 my ($self) = @_;
2800 3194
2801 $self->SUPER::update; 3195 $self->SUPER::update;
2802 3196
2803 return unless $self->{h} > 0; 3197 return unless $self->{h} > 0;
2804 3198
2805 delete $self->{texture}; 3199 delete $self->{texture};
2806 3200
2807 $ROOT->on_post_alloc ($self => sub { 3201 $ROOT->on_post_alloc ($self => sub {
3202 $self->force_uptodate;
3203
2808 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3204 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2809
2810 if (delete $self->{need_reflow}) {
2811 my $height = 0;
2812
2813 for my $para (@{$self->{par}}) {
2814 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2815 my $layout = $self->get_layout ($para);
2816 my ($w, $h) = $layout->size;
2817
2818 $para->{w} = $w + $para->{indent};
2819 $para->{h} = $h;
2820 $para->{wrapped} = $layout->has_wrapped;
2821 }
2822
2823 $height += $para->{h};
2824 }
2825
2826 $self->{height} = $height;
2827
2828 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2829
2830 delete $self->{texture};
2831 }
2832
2833 if (delete $self->{scroll_to_bottom}) {
2834 $self->{children}[1]->set_value (1e10);
2835 }
2836 3205
2837 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3206 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2838 glClearColor 0, 0, 0, 0; 3207 glClearColor 0, 0, 0, 0;
2839 glClear GL_COLOR_BUFFER_BIT; 3208 glClear GL_COLOR_BUFFER_BIT;
2840 3209
3210 package CFPlus::UI::Base;
3211 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3212 (0, 0, $self->{w}, $self->{h});
3213
3214 my $top = int $self->{children}[1]{range}[0];
3215
3216 my $paridx = 0;
3217 my $top_paragraph;
2841 my $top = int $self->{children}[1]{range}[0]; 3218 my $top = int $self->{children}[1]{range}[0];
2842 3219
2843 my $y0 = $top; 3220 my $y0 = $top;
2844 my $y1 = $top + $H; 3221 my $y1 = $top + $H;
2845 3222
2846 my $y = 0;
2847
2848 for my $para (@{$self->{par}}) { 3223 for my $para (@{$self->{par}}) {
2849 my $h = $para->{h}; 3224 my $h = $para->{h};
3225 my $y = $para->{y};
2850 3226
2851 if ($y0 < $y + $h && $y < $y1) { 3227 if ($y0 < $y + $h && $y < $y1) {
2852
2853 my $layout = $self->get_layout ($para); 3228 my $layout = $self->get_layout ($para);
2854 3229
2855 $layout->render ($para->{indent}, $y - $y0); 3230 $layout->render ($para->{indent}, $y - $y0);
2856 3231
2857 if (my @w = @{ $para->{widget} }) { 3232 if (my @w = @{ $para->{widget} }) {
2866 $_->draw; 3241 $_->draw;
2867 } 3242 }
2868 } 3243 }
2869 } 3244 }
2870 3245
2871 $y += $h; 3246 $paridx++;
3247 $top_paragraph ||= $paridx if $y >= $top;
2872 } 3248 }
3249
3250 $self->{top_paragraph} = $top_paragraph;
2873 }; 3251 };
2874 }); 3252 });
2875} 3253}
2876 3254
2877sub reconfigure { 3255sub reconfigure {
3080 3458
3081sub new { 3459sub new {
3082 my $class = shift; 3460 my $class = shift;
3083 3461
3084 my $self = $class->SUPER::new ( 3462 my $self = $class->SUPER::new (
3463 size_w => 32,
3464 size_h => 8,
3085 aspect => 1, 3465 aspect => 1,
3086 can_events => 0, 3466 can_events => 0,
3087 @_, 3467 @_,
3088 ); 3468 );
3089 3469
3090 if ($self->{anim} && $self->{animspeed}) { 3470 if ($self->{anim} && $self->{animspeed}) {
3091 Scalar::Util::weaken (my $widget = $self); 3471 CFPlus::weaken (my $widget = $self);
3092 3472
3473 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3474 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3093 $self->{timer} = Event->timer ( 3475 $self->{timer} = Event->timer (
3094 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3095 hard => 1, 3476 parked => 1,
3096 interval => $self->{animspeed},
3097 cb => sub { 3477 cb => sub {
3478 return unless $::CONN && $widget;
3479
3098 ++$widget->{frame}; 3480 ++$widget->{frame};
3481 $widget->update_face;
3099 $widget->update; 3482 $widget->update;
3483
3484 $widget->update_timer;
3100 }, 3485 },
3101 ); 3486 );
3487
3488 $self->update_face;
3489 $self->update_timer;
3102 } 3490 }
3103 3491
3104 $self 3492 $self
3105} 3493}
3106 3494
3495sub update_timer {
3496 my ($self) = @_;
3497
3498 return unless $self->{timer};
3499
3500 if ($self->{visible}) {
3501 $self->{timer}->at (
3502 $self->{anim_start}
3503 + $self->{animspeed}
3504 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3505 );
3506 $self->{timer}->start;
3507 } else {
3508 $self->{timer}->stop;
3509 }
3510}
3511
3512sub update_face {
3513 my ($self) = @_;
3514
3515 return unless $::CONN;
3516
3517 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3518 if ($anim && @$anim) {
3519 delete $self->{wait_face};
3520 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3521 }
3522 }
3523}
3524
3107sub size_request { 3525sub size_request {
3108 (32, 8) 3526 my ($self) = @_;
3527
3528 if ($::CONN) {
3529 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3530 if (my $tex = $::CONN->{texture}[$faceid]) {
3531 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3532 } else {
3533 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3534 $self->realloc;
3535 });
3536 }
3537 }
3538 }
3539
3540 ($self->{size_w} || 8, $self->{size_h} || 8)
3109} 3541}
3110 3542
3111sub update { 3543sub update {
3112 my ($self) = @_; 3544 my ($self) = @_;
3113 3545
3114 return unless $self->{visible}; 3546 return unless $self->{visible};
3115 3547
3116 $self->SUPER::update; 3548 $self->SUPER::update;
3117} 3549}
3118 3550
3551sub invoke_visibility_change {
3552 my ($self) = @_;
3553
3554 $self->update_timer;
3555
3556 0
3557}
3558
3119sub _draw { 3559sub _draw {
3120 my ($self) = @_; 3560 my ($self) = @_;
3121 3561
3122 return unless $::CONN; 3562 return unless $::CONN;
3123 3563
3124 $self->SUPER::_draw; 3564 $self->SUPER::_draw;
3125 3565
3126 my $face;
3127
3128 if ($self->{frame}) {
3129 my $anim = $::CONN->{anim}[$self->{anim}]; 3566 my $faceid = $::CONN->{faceid}[$self->{face}]
3130 3567 or return;
3131 $face = $anim->[ $self->{frame} % @$anim ]
3132 if $anim && @$anim;
3133 }
3134 3568
3135 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 3569 my $tex = $::CONN->{texture}[$faceid];
3136 3570
3137 if ($tex) { 3571 if ($tex) {
3138 glEnable GL_TEXTURE_2D; 3572 glEnable GL_TEXTURE_2D;
3139 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3573 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3140 glColor 0, 0, 0, 1; 3574 glColor 0, 0, 0, 1;
3144} 3578}
3145 3579
3146sub destroy { 3580sub destroy {
3147 my ($self) = @_; 3581 my ($self) = @_;
3148 3582
3149 $self->{timer}->cancel 3583 (delete $self->{timer})->cancel
3150 if $self->{timer}; 3584 if $self->{timer};
3151 3585
3152 $self->SUPER::destroy; 3586 $self->SUPER::destroy;
3153} 3587}
3154 3588
3156 3590
3157package CFPlus::UI::Buttonbar; 3591package CFPlus::UI::Buttonbar;
3158 3592
3159our @ISA = CFPlus::UI::HBox::; 3593our @ISA = CFPlus::UI::HBox::;
3160 3594
3161# TODO: should actualyl wrap buttons and other goodies. 3595# TODO: should actually wrap buttons and other goodies.
3162 3596
3163############################################################################# 3597#############################################################################
3164 3598
3165package CFPlus::UI::Menu; 3599package CFPlus::UI::Menu;
3166 3600
3167our @ISA = CFPlus::UI::FancyFrame::; 3601our @ISA = CFPlus::UI::Toplevel::;
3168 3602
3169use CFPlus::OpenGL; 3603use CFPlus::OpenGL;
3170 3604
3171sub new { 3605sub new {
3172 my $class = shift; 3606 my $class = shift;
3350} 3784}
3351 3785
3352sub add { 3786sub add {
3353 my ($self, $title, $widget, $tooltip) = @_; 3787 my ($self, $title, $widget, $tooltip) = @_;
3354 3788
3355 Scalar::Util::weaken $self; 3789 CFPlus::weaken $self;
3356 3790
3357 $self->{buttonbar}->add (new CFPlus::UI::Button 3791 $self->{buttonbar}->add (new CFPlus::UI::Button
3358 markup => $title, 3792 markup => $title,
3359 tooltip => $tooltip, 3793 tooltip => $tooltip,
3360 on_activate => sub { $self->set_current_page ($widget) }, 3794 on_activate => sub { $self->set_current_page ($widget) },
3444 my $self = $class->SUPER::new ( 3878 my $self = $class->SUPER::new (
3445 fontsize => 0.8, 3879 fontsize => 0.8,
3446 @_, 3880 @_,
3447 ); 3881 );
3448 3882
3449 Scalar::Util::weaken (my $this = $self); 3883 CFPlus::weaken (my $this = $self);
3450 3884
3451 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3885 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3452 3886
3453 $self 3887 $self
3454} 3888}
3577 $self->SUPER::destroy; 4011 $self->SUPER::destroy;
3578} 4012}
3579 4013
3580############################################################################# 4014#############################################################################
3581 4015
3582package CFPlus::UI::Inventory;
3583
3584our @ISA = CFPlus::UI::Table::;
3585
3586sub new {
3587 my $class = shift;
3588
3589 my $self = $class->SUPER::new (
3590 col_expand => [0, 1, 0],
3591 items => [],
3592 @_,
3593 );
3594
3595 $self->set_sort_order (undef);
3596
3597 $self
3598}
3599
3600sub update_items {
3601 my ($self) = @_;
3602
3603 $self->clear;
3604
3605 my @item = $self->{sort}->(@{ $self->{items} });
3606
3607 my @adds;
3608 my $row = 0;
3609 for my $item ($self->{sort}->(@{ $self->{items} })) {
3610 CFPlus::Item::update_widgets $item;
3611
3612 push @adds, 0, $row, $item->{face_widget};
3613 push @adds, 1, $row, $item->{desc_widget};
3614 push @adds, 2, $row, $item->{weight_widget};
3615
3616 $row++;
3617 }
3618
3619 $self->add (@adds);
3620}
3621
3622sub set_sort_order {
3623 my ($self, $order) = @_;
3624
3625 $self->{sort} = $order ||= sub {
3626 sort {
3627 $a->{type} <=> $b->{type}
3628 or $a->{name} cmp $b->{name}
3629 } @_
3630 };
3631
3632 $self->update_items;
3633}
3634
3635sub set_items {
3636 my ($self, $items) = @_;
3637
3638 $self->{items} = [$items ? values %$items : ()];
3639 $self->update_items;
3640}
3641
3642#############################################################################
3643
3644package CFPlus::UI::SpellList;
3645
3646our @ISA = CFPlus::UI::Table::;
3647
3648sub new {
3649 my $class = shift;
3650
3651 my $self = $class->SUPER::new (
3652 binding => [],
3653 commands => [],
3654 @_,
3655 )
3656}
3657
3658my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3659
3660my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3661 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3662my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3663 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3664my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3665 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3666my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3667 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3668my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3669 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3670
3671sub rebuild_spell_list {
3672 my ($self) = @_;
3673
3674 $CFPlus::UI::ROOT->on_refresh ($self => sub {
3675 $self->clear;
3676
3677 return unless $::CONN;
3678
3679 $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3680 $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL);
3681 $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3682 $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3683 $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3684
3685 my $row = 0;
3686
3687 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3688 my $spell = $self->{spell}{$_};
3689
3690 $row++;
3691
3692 my $spell_cb = sub {
3693 my ($widget, $ev) = @_;
3694
3695 if ($ev->{button} == 1) {
3696 $::CONN->user_send ("cast $spell->{name}");
3697 } elsif ($ev->{button} == 2) {
3698 $::CONN->user_send ("invoke $spell->{name}");
3699 } elsif ($ev->{button} == 3) {
3700 (new CFPlus::UI::Menu
3701 items => [
3702 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3703 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3704 ],
3705 )->popup ($ev);
3706 } else {
3707 return 0;
3708 }
3709
3710 1
3711 };
3712
3713 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3714
3715 #TODO: add path info to tooltip
3716 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3717
3718 $self->add (0, $row, new CFPlus::UI::Face
3719 face => $spell->{face},
3720 can_hover => 1,
3721 can_events => 1,
3722 tooltip => $tooltip,
3723 on_button_down => $spell_cb,
3724 );
3725
3726 $self->add (1, $row, new CFPlus::UI::Label
3727 expand => 1,
3728 text => $spell->{name},
3729 can_hover => 1,
3730 can_events => 1,
3731 tooltip => $tooltip,
3732 on_button_down => $spell_cb,
3733 );
3734
3735 $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3736 $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3737 $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3738 $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3739 }
3740 });
3741}
3742
3743sub add_spell {
3744 my ($self, $spell) = @_;
3745
3746 $self->{spell}->{$spell->{name}} = $spell;
3747 $self->rebuild_spell_list;
3748}
3749
3750sub remove_spell {
3751 my ($self, $spell) = @_;
3752
3753 delete $self->{spell}->{$spell->{name}};
3754 $self->rebuild_spell_list;
3755}
3756
3757sub clear_spells {
3758 my ($self) = @_;
3759
3760 $self->{spell} = {};
3761 $self->rebuild_spell_list;
3762}
3763
3764#############################################################################
3765
3766package CFPlus::UI::Root; 4016package CFPlus::UI::Root;
3767 4017
3768our @ISA = CFPlus::UI::Container::; 4018our @ISA = CFPlus::UI::Container::;
3769 4019
3770use List::Util qw(min max); 4020use List::Util qw(min max);
3777 my $self = $class->SUPER::new ( 4027 my $self = $class->SUPER::new (
3778 visible => 1, 4028 visible => 1,
3779 @_, 4029 @_,
3780 ); 4030 );
3781 4031
3782 Scalar::Util::weaken ($self->{root} = $self); 4032 CFPlus::weaken ($self->{root} = $self);
3783 4033
3784 $self 4034 $self
3785} 4035}
3786 4036
3787sub size_request { 4037sub size_request {
3982 while ($self->{post_alloc_hook}) { 4232 while ($self->{post_alloc_hook}) {
3983 $_->() 4233 $_->()
3984 for values %{delete $self->{post_alloc_hook}}; 4234 for values %{delete $self->{post_alloc_hook}};
3985 } 4235 }
3986 4236
3987
3988 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4237 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3989 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4238 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3990 glClear GL_COLOR_BUFFER_BIT; 4239 glClear GL_COLOR_BUFFER_BIT;
3991 4240
3992 glMatrixMode GL_PROJECTION; 4241 glMatrixMode GL_PROJECTION;
3996 glLoadIdentity; 4245 glLoadIdentity;
3997 4246
3998 { 4247 {
3999 package CFPlus::UI::Base; 4248 package CFPlus::UI::Base;
4000 4249
4001 ($draw_x, $draw_y, $draw_w, $draw_h) = 4250 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4002 (0, 0, $self->{w}, $self->{h}); 4251 (0, 0, $self->{w}, $self->{h});
4003 }
4004 4252
4005 $self->_draw; 4253 $self->_draw;
4254 }
4006} 4255}
4007 4256
4008############################################################################# 4257#############################################################################
4009 4258
4010package CFPlus::UI; 4259package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines