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.389 by root, Sat Jul 21 13:02:05 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;
898 glClear GL_COLOR_BUFFER_BIT; 912 glClear GL_COLOR_BUFFER_BIT;
899 913
900 { 914 {
901 package CFPlus::UI::Base; 915 package CFPlus::UI::Base;
902 916
903 ($draw_x, $draw_y, $draw_w, $draw_h) = 917 local ($draw_x, $draw_y, $draw_w, $draw_h) =
904 (0, 0, $self->{w}, $self->{h}); 918 (0, 0, $self->{w}, $self->{h});
919
920 $self->_render;
905 } 921 }
906
907 $self->_render;
908 }; 922 };
909} 923}
910 924
911sub _draw { 925sub _draw {
912 my ($self) = @_; 926 my ($self) = @_;
913
914 my ($w, $h) = @$self{qw(w h)};
915 927
916 my $tex = $self->{texture} 928 my $tex = $self->{texture}
917 or return; 929 or return;
918 930
919 glEnable GL_TEXTURE_2D; 931 glEnable GL_TEXTURE_2D;
920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 932 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
921 glColor 0, 0, 0, 1; 933 glColor 0, 0, 0, 1;
922 934
923 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 935 $tex->draw_quad_alpha_premultiplied (0, 0);
924 936
925 glDisable GL_TEXTURE_2D; 937 glDisable GL_TEXTURE_2D;
926} 938}
927 939
928############################################################################# 940#############################################################################
929 941
930package CFPlus::UI::ViewPort; 942package CFPlus::UI::ViewPort;
943
944use List::Util qw(min max);
931 945
932our @ISA = CFPlus::UI::Window::; 946our @ISA = CFPlus::UI::Window::;
933 947
934sub new { 948sub new {
935 my $class = shift; 949 my $class = shift;
944sub size_request { 958sub size_request {
945 my ($self) = @_; 959 my ($self) = @_;
946 960
947 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 961 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
948 962
949 $w = 10 if $self->{scroll_x}; 963 $w = 1 if $self->{scroll_x};
950 $h = 10 if $self->{scroll_y}; 964 $h = 1 if $self->{scroll_y};
951 965
952 ($w, $h) 966 ($w, $h)
953} 967}
954 968
955sub invoke_size_allocate { 969sub invoke_size_allocate {
967} 981}
968 982
969sub set_offset { 983sub set_offset {
970 my ($self, $x, $y) = @_; 984 my ($self, $x, $y) = @_;
971 985
986 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
987 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
988
989 if ($x != $self->{view_x} or $y != $self->{view_y}) {
972 $self->{view_x} = int $x; 990 $self->{view_x} = $x;
973 $self->{view_y} = int $y; 991 $self->{view_y} = $y;
974 992
993 $self->emit (changed => $x, $y);
975 $self->update; 994 $self->update;
995 }
976} 996}
977 997
978# hmm, this does not work for topleft of $self... but we should not ask for that 998# hmm, this does not work for topleft of $self... but we should not ask for that
979sub coord2local { 999sub coord2local {
980 my ($self, $x, $y) = @_; 1000 my ($self, $x, $y) = @_;
995 my ($self, $x, $y) = @_; 1015 my ($self, $x, $y) = @_;
996 1016
997 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1017 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
998 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1018 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
999 ) { 1019 ) {
1000 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1020 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1001 } else { 1021 } else {
1002 $self->CFPlus::UI::Base::find_widget ($x, $y) 1022 $self->CFPlus::UI::Base::find_widget ($x, $y)
1003 } 1023 }
1004} 1024}
1005 1025
1016 1036
1017############################################################################# 1037#############################################################################
1018 1038
1019package CFPlus::UI::ScrolledWindow; 1039package CFPlus::UI::ScrolledWindow;
1020 1040
1021our @ISA = CFPlus::UI::HBox::; 1041our @ISA = CFPlus::UI::Table::;
1022 1042
1023sub new { 1043sub new {
1024 my ($class, %arg) = @_; 1044 my ($class, %arg) = @_;
1025 1045
1026 my $child = delete $arg{child}; 1046 my $child = delete $arg{child};
1027 1047
1028 my $self; 1048 my $self;
1029 1049
1030 my $slider = new CFPlus::UI::Slider 1050 my $hslider = new CFPlus::UI::Slider
1051 vertical => 0,
1052 range => [0, 0, 1, 0.01], # HACK fix
1053 on_changed => sub {
1054 $self->{hpos} = $_[1];
1055 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1056 },
1057 ;
1058
1059 my $vslider = new CFPlus::UI::Slider
1031 vertical => 1, 1060 vertical => 1,
1032 range => [0, 0, 1, 0.01], # HACK fix 1061 range => [0, 0, 1, 0.01], # HACK fix
1033 on_changed => sub { 1062 on_changed => sub {
1034 $self->{vp}->set_offset (0, $_[1]); 1063 $self->{vpos} = $_[1];
1064 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1035 }, 1065 },
1036 ; 1066 ;
1037 1067
1038 $self = $class->SUPER::new ( 1068 $self = $class->SUPER::new (
1039 vp => (new CFPlus::UI::ViewPort expand => 1), 1069 scroll_x => 0,
1070 scroll_y => 1,
1040 can_events => 1, 1071 can_events => 1,
1041 slider => $slider, 1072 hslider => $hslider,
1073 vslider => $vslider,
1074 col_expand => [1, 0],
1075 row_expand => [1, 0],
1042 %arg, 1076 %arg,
1043 ); 1077 );
1044 1078
1045 $self->SUPER::add ($self->{vp}, $self->{slider}); 1079 $self->{vp} = new CFPlus::UI::ViewPort
1080 expand => 1,
1081 scroll_x => $self->{scroll_x},
1082 scroll_y => $self->{scroll_y},
1083 on_changed => sub {
1084 my ($vp, $x, $y) = @_;
1085
1086 $vp->{parent}{hslider}->set_value ($x);
1087 $vp->{parent}{vslider}->set_value ($y);
1088
1089 0
1090 },
1091 ;
1092
1093 $self->SUPER::add_at (0, 0, $self->{vp});
1094
1046 $self->add ($child) if $child; 1095 $self->add ($child) if $child;
1047 1096
1048 $self 1097 $self
1049} 1098}
1050 1099
1054 my ($self, $widget) = @_; 1103 my ($self, $widget) = @_;
1055 1104
1056 $self->{vp}->add ($self->{child} = $widget); 1105 $self->{vp}->add ($self->{child} = $widget);
1057} 1106}
1058 1107
1108sub update_slider {
1109 my ($self) = @_;
1110
1111 my $child = ($self->{vp} or return)->child;
1112
1113 my ($w1, $w2) = ($child->{w}, $self->{vp}{w});
1114 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1115
1116 my $visible = $w1 > $w2;
1117 if ($visible != $self->{hslider}{visible}) {
1118 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider})
1119 : $self->{hslider}->hide;
1120 }
1121
1122 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1123 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1124
1125 my $visible = $h1 > $h2;
1126 if ($visible != $self->{vslider}{visible}) {
1127 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider})
1128 : $self->{vslider}->hide;
1129 }
1130}
1131
1132sub update {
1133 my ($self) = @_;
1134
1135 $self->update_slider;
1136 $self->SUPER::update;
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
1363 1591
1364sub new { 1592sub new {
1365 my $class = shift; 1593 my $class = shift;
1366 1594
1367 $class->SUPER::new ( 1595 $class->SUPER::new (
1596 children => [],
1368 col_expand => [], 1597 col_expand => [],
1598 row_expand => [],
1369 @_, 1599 @_,
1370 ) 1600 )
1371} 1601}
1372 1602
1373sub children { 1603sub children {
1374 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1604 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1375} 1605}
1376 1606
1607# TODO: store row/col info in child widget and use standard add/del
1377sub add { 1608sub add {
1378 my ($self) = shift; 1609 my $self = shift;
1610
1611 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d#
1612 $self->add_at (@_);
1613}
1614
1615sub add_at {
1616 my $self = shift;
1379 1617
1380 while (@_) { 1618 while (@_) {
1381 my ($x, $y, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1620
1621 $child->{row} = $row;
1622 $child->{col} = $col;
1623
1624 $child->{rowspan} ||= 1;
1625 $child->{colspan} ||= 1;
1626
1382 $child->set_parent ($self); 1627 $child->set_parent ($self);
1383 $self->{children}[$y][$x] = $child; 1628 $self->{children}[$row][$col] = $child;
1384 } 1629 }
1385 1630
1386 $self->{force_realloc} = 1; 1631 $self->{force_realloc} = 1;
1387 $self->{force_size_alloc} = 1; 1632 $self->{force_size_alloc} = 1;
1388 $self->realloc; 1633 $self->realloc;
1389} 1634}
1390 1635
1391sub remove { 1636sub remove {
1392 my ($self, $child) = @_; 1637 my ($self, $child) = @_;
1393 1638
1394 # TODO: not yet implemented 1639 for (@{ $self->{children} }) {
1640 for (@{ $_ || [] }) {
1641 $_ = undef if $_ == $child;
1642 }
1643 }
1395} 1644}
1396 1645
1397# TODO: move to container class maybe? send children a signal on removal? 1646# TODO: move to container class maybe? send children a signal on removal?
1398sub clear { 1647sub clear {
1399 my ($self) = @_; 1648 my ($self) = @_;
1412sub get_wh { 1661sub get_wh {
1413 my ($self) = @_; 1662 my ($self) = @_;
1414 1663
1415 my (@w, @h); 1664 my (@w, @h);
1416 1665
1417 for my $y (0 .. $#{$self->{children}}) { 1666 my @children = $self->children;
1418 my $row = $self->{children}[$y]
1419 or next;
1420 1667
1421 for my $x (0 .. $#$row) { 1668 for my $widget (sort { $b->{rowspan} * $b->{colspan} <=> $a->{rowspan} * $a->{colspan} } @children) {
1422 my $widget = $row->[$x] 1669 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)};
1423 or next;
1424 my ($w, $h) = @$widget{qw(req_w req_h)};
1425 1670
1426 $w[$x] = max $w[$x], $w; 1671 my $sw = sum @w[$c .. $c + $cs - 1];
1427 $h[$y] = max $h[$y], $h; 1672 my $sh = sum @h[$r .. $r + $rs - 1];
1673
1674 if ($w > $sw) {
1675 $_ += ($w - $sw) / $cs for @w[$c .. $c + $cs - 1];
1676 }
1677
1678 if ($h > $sh) {
1679 $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1];
1428 } 1680 }
1429 } 1681 }
1430 1682
1431 (\@w, \@h) 1683 (\@w, \@h)
1432} 1684}
1448 my ($ws, $hs) = $self->get_wh; 1700 my ($ws, $hs) = $self->get_wh;
1449 1701
1450 my $req_w = (sum @$ws) || 1; 1702 my $req_w = (sum @$ws) || 1;
1451 my $req_h = (sum @$hs) || 1; 1703 my $req_h = (sum @$hs) || 1;
1452 1704
1453 # TODO: nicer code && do row_expand 1705 # now linearly scale the rows/columns to the allocated size
1454 my @col_expand = @{$self->{col_expand}}; 1706 my @col_expand = @{$self->{col_expand}};
1455 @col_expand = (1) x @$ws unless @col_expand; 1707 @col_expand = (1) x @$ws unless @col_expand;
1456 my $col_expand = (sum @col_expand) || 1; 1708 my $col_expand = (sum @col_expand) || 1;
1457 1709
1458 # linearly scale sizes
1459 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1710 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1460 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1461 1711
1462 CFPlus::UI::harmonize $ws; 1712 CFPlus::UI::harmonize $ws;
1713
1714 my @row_expand = @{$self->{row_expand}};
1715 @row_expand = (1) x @$ws unless @row_expand;
1716 my $row_expand = (sum @row_expand) || 1;
1717
1718 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1719
1463 CFPlus::UI::harmonize $hs; 1720 CFPlus::UI::harmonize $hs;
1464 1721
1465 my $y; 1722 my (@x, @y);
1466 1723
1724 for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1725 for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1726
1467 for my $r (0 .. $#{$self->{children}}) { 1727 for my $widget ($self->children) {
1468 my $row = $self->{children}[$r] 1728 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)};
1469 or next;
1470 1729
1471 my $x = 0; 1730 $widget->configure (
1472 my $row_h = $hs->[$r]; 1731 $x[$c], $y[$r],
1732 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1473 1733 );
1474 for my $c (0 .. $#$row) {
1475 my $col_w = $ws->[$c];
1476
1477 if (my $widget = $row->[$c]) {
1478 $widget->configure ($x, $y, $col_w, $row_h);
1479 }
1480
1481 $x += $col_w;
1482 }
1483
1484 $y += $row_h;
1485 } 1734 }
1486 1735
1487 1 1736 1
1488} 1737}
1489 1738
1507 my ($self) = @_; 1756 my ($self) = @_;
1508 1757
1509 for (grep $_, @{$self->{children}}) { 1758 for (grep $_, @{$self->{children}}) {
1510 $_->draw for grep $_, @$_; 1759 $_->draw for grep $_, @$_;
1511 } 1760 }
1761}
1762
1763#############################################################################
1764
1765package CFPlus::UI::Fixed;
1766
1767use List::Util qw(min max);
1768
1769our @ISA = CFPlus::UI::Container::;
1770
1771sub add {
1772 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1773
1774 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1775 $self->SUPER::add ($child);
1776}
1777
1778sub _scale($$$) {
1779 my ($mode, $val, $max) = @_;
1780
1781 $mode eq "abs" ? $val
1782 : $mode eq "rel" ? $val * $max
1783 : 0
1784}
1785
1786sub size_request {
1787 my ($self) = @_;
1788
1789 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1790
1791 # determine overall size by querying abs widgets
1792 for my $child ($self->visible_children) {
1793 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1794
1795 if ($pos eq "abs") {
1796 $w = _scale $size, $w, $child->{req_w};
1797 $h = _scale $size, $h, $child->{req_h};
1798
1799 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1800 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1801 }
1802 }
1803
1804 my $W = $x2 - $x1;
1805 my $H = $y2 - $y1;
1806
1807 # now layout remaining widgets
1808 for my $child ($self->visible_children) {
1809 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1810
1811 if ($pos ne "abs") {
1812 $x = _scale $pos, $x, $W;
1813 $y = _scale $pos, $x, $H;
1814 $w = _scale $size, $w, $child->{req_w};
1815 $h = _scale $size, $h, $child->{req_h};
1816
1817 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1818 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1819 }
1820 }
1821
1822 my $W = $x2 - $x1;
1823 my $H = $y2 - $y1;
1824
1825 ($W, $H)
1826}
1827
1828sub invoke_size_allocate {
1829 my ($self, $W, $H) = @_;
1830
1831 for my $child ($self->visible_children) {
1832 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1833
1834 $x = _scale $pos, $x, $W;
1835 $y = _scale $pos, $x, $H;
1836 $w = _scale $size, $w, $child->{req_w};
1837 $h = _scale $size, $h, $child->{req_h};
1838
1839 $child->configure ($x, $y, $w, $h);
1840 }
1841
1842 1
1512} 1843}
1513 1844
1514############################################################################# 1845#############################################################################
1515 1846
1516package CFPlus::UI::Box; 1847package CFPlus::UI::Box;
1802 active_fg => [0, 0, 0], 2133 active_fg => [0, 0, 0],
1803 can_hover => 1, 2134 can_hover => 1,
1804 can_focus => 1, 2135 can_focus => 1,
1805 valign => 0, 2136 valign => 0,
1806 can_events => 1, 2137 can_events => 1,
2138 ellipsise => 0,
1807 #text => ... 2139 #text => ...
1808 #hidden => "*", 2140 #hidden => "*",
1809 @_ 2141 @_
1810 ) 2142 )
1811} 2143}
1855 my $mod = $ev->{mod}; 2187 my $mod = $ev->{mod};
1856 my $sym = $ev->{sym}; 2188 my $sym = $ev->{sym};
1857 my $uni = $ev->{unicode}; 2189 my $uni = $ev->{unicode};
1858 2190
1859 my $text = $self->get_text; 2191 my $text = $self->get_text;
2192
2193 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
1860 2194
1861 if ($uni == 8) { 2195 if ($uni == 8) {
1862 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2196 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1863 } elsif ($uni == 127) { 2197 } elsif ($uni == 127) {
1864 substr $text, $self->{cursor}, 1, ""; 2198 substr $text, $self->{cursor}, 1, "";
1865 } elsif ($sym == CFPlus::SDLK_LEFT) { 2199 } elsif ($sym == CFPlus::SDLK_LEFT) {
1866 --$self->{cursor} if $self->{cursor}; 2200 --$self->{cursor} if $self->{cursor};
1867 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2201 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1868 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2202 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1869 } elsif ($sym == CFPlus::SDLK_HOME) { 2203 } elsif ($sym == CFPlus::SDLK_HOME) {
2204 # what a hack
2205 $self->{cursor} =
2206 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2207 ? length $1
2208 : 0;
2209 } elsif ($sym == CFPlus::SDLK_END) {
2210 # uh, again
2211 $self->{cursor} =
2212 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2213 ? $self->{cursor} + length $1
2214 : length $self->{text};
2215 } elsif ($uni == 21) { # ctrl-u
2216 $text = "";
1870 $self->{cursor} = 0; 2217 $self->{cursor} = 0;
1871 } elsif ($sym == CFPlus::SDLK_END) {
1872 $self->{cursor} = length $text;
1873 } elsif ($uni == 27) { 2218 } elsif ($uni == 27) {
1874 $self->emit ('escape'); 2219 $self->emit ('escape');
1875 } elsif ($uni) { 2220 } elsif ($uni == 0x0d) {
2221 substr $text, $self->{cursor}++, 0, "\012";
2222 } elsif ($uni >= 0x20) {
1876 substr $text, $self->{cursor}++, 0, chr $uni; 2223 substr $text, $self->{cursor}++, 0, chr $uni;
1877 } else { 2224 } else {
1878 return 0; 2225 return 0;
1879 } 2226 }
1880 2227
1881 $self->_set_text ($text); 2228 $self->_set_text ($text);
1882 2229
1883 $self->realloc; 2230 $self->realloc;
2231 $self->update;
1884 2232
1885 1 2233 1
1886} 2234}
1887 2235
1888sub invoke_focus_in { 2236sub invoke_focus_in {
1950 utf8::encode $text; 2298 utf8::encode $text;
1951 2299
1952 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2300 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1953 } 2301 }
1954 2302
1955 glColor @{$self->{fg}};
1956 glBegin GL_LINES; 2303 glBegin GL_LINES;
1957 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2304 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}; 2305 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1959 glEnd; 2306 glEnd;
1960 } 2307 }
1961} 2308}
2309
2310#############################################################################
1962 2311
1963package CFPlus::UI::Entry; 2312package CFPlus::UI::Entry;
1964 2313
1965our @ISA = CFPlus::UI::EntryBase::; 2314our @ISA = CFPlus::UI::EntryBase::;
1966 2315
1969sub invoke_key_down { 2318sub invoke_key_down {
1970 my ($self, $ev) = @_; 2319 my ($self, $ev) = @_;
1971 2320
1972 my $sym = $ev->{sym}; 2321 my $sym = $ev->{sym};
1973 2322
1974 if ($sym == 13) { 2323 if ($ev->{uni} == 0x0d || $sym == 13) {
1975 unshift @{$self->{history}}, 2324 unshift @{$self->{history}},
1976 my $txt = $self->get_text; 2325 my $txt = $self->get_text;
1977 2326
1978 $self->{history_pointer} = -1; 2327 $self->{history_pointer} = -1;
1979 $self->{history_saveback} = ''; 2328 $self->{history_saveback} = '';
2009 1 2358 1
2010} 2359}
2011 2360
2012############################################################################# 2361#############################################################################
2013 2362
2363package CFPlus::UI::TextEdit;
2364
2365our @ISA = CFPlus::UI::EntryBase::;
2366
2367use CFPlus::OpenGL;
2368
2369sub move_cursor_ver {
2370 my ($self, $dy) = @_;
2371
2372 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2373
2374 $y += $dy;
2375
2376 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2377 $self->{cursor} = $index;
2378 delete $self->{cur_h};
2379 $self->update;
2380 return;
2381 }
2382}
2383
2384sub invoke_key_down {
2385 my ($self, $ev) = @_;
2386
2387 my $sym = $ev->{sym};
2388
2389 if ($sym == CFPlus::SDLK_UP) {
2390 $self->move_cursor_ver (-1);
2391 } elsif ($sym == CFPlus::SDLK_DOWN) {
2392 $self->move_cursor_ver (+1);
2393 } else {
2394 return $self->SUPER::invoke_key_down ($ev)
2395 }
2396
2397 1
2398}
2399
2400#############################################################################
2401
2014package CFPlus::UI::Button; 2402package CFPlus::UI::Button;
2015 2403
2016our @ISA = CFPlus::UI::Label::; 2404our @ISA = CFPlus::UI::Label::;
2017 2405
2018use CFPlus::OpenGL; 2406use CFPlus::OpenGL;
2019 2407
2020my @tex = 2408my @tex =
2021 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2409 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2022 qw(b1_button_active.png); 2410 qw(b1_button_inactive.png b1_button_active.png);
2023 2411
2024sub new { 2412sub new {
2025 my $class = shift; 2413 my $class = shift;
2026 2414
2027 $class->SUPER::new ( 2415 $class->SUPER::new (
2028 padding_x => 4, 2416 padding_x => 4,
2029 padding_y => 4, 2417 padding_y => 4,
2030 fg => [1, 1, 1], 2418 fg => [1.0, 1.0, 1.0],
2031 active_fg => [0, 0, 1], 2419 active_fg => [0.8, 0.8, 0.8],
2032 can_hover => 1, 2420 can_hover => 1,
2033 align => 0, 2421 align => 0,
2034 valign => 0, 2422 valign => 0,
2035 can_events => 1, 2423 can_events => 1,
2036 @_ 2424 @_
2054 2442
2055 glEnable GL_TEXTURE_2D; 2443 glEnable GL_TEXTURE_2D;
2056 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2444 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2057 glColor 0, 0, 0, 1; 2445 glColor 0, 0, 0, 1;
2058 2446
2447 my $tex = $tex[$GRAB == $self];
2059 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2448 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2060 2449
2061 glDisable GL_TEXTURE_2D; 2450 glDisable GL_TEXTURE_2D;
2062 2451
2063 $self->SUPER::_draw; 2452 $self->SUPER::_draw;
2064} 2453}
2160 or Carp::croak "'path' or 'tex' attributes required"; 2549 or Carp::croak "'path' or 'tex' attributes required";
2161 2550
2162 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2551 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2163 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2552 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2164 2553
2165 Scalar::Util::weaken $texture_cache{$self->{path}}; 2554 CFPlus::weaken $texture_cache{$self->{path}};
2166 2555
2167 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2556 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2168 2557
2169 $self 2558 $self
2170} 2559}
2543sub invoke_mouse_wheel { 2932sub invoke_mouse_wheel {
2544 my ($self, $ev) = @_; 2933 my ($self, $ev) = @_;
2545 2934
2546 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2935 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2547 2936
2937 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2938
2548 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 2939 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2549 2940
2550 ! ! $delta 2941 ! ! $delta
2551} 2942}
2552 2943
2553sub update { 2944sub update {
2663 #font => default_font 3054 #font => default_font
2664 @_, 3055 @_,
2665 3056
2666 layout => (new CFPlus::Layout), 3057 layout => (new CFPlus::Layout),
2667 par => [], 3058 par => [],
3059 max_par => 0,
2668 height => 0, 3060 height => 0,
2669 children => [ 3061 children => [
2670 (new CFPlus::UI::Empty expand => 1), 3062 (new CFPlus::UI::Empty expand => 1),
2671 (new CFPlus::UI::Slider vertical => 1), 3063 (new CFPlus::UI::Slider vertical => 1),
2672 ], 3064 ],
2751sub set_offset { 3143sub set_offset {
2752 my ($self, $offset) = @_; 3144 my ($self, $offset) = @_;
2753 3145
2754 # todo: base offset on lines or so, not on pixels 3146 # todo: base offset on lines or so, not on pixels
2755 $self->{children}[1]->set_value ($offset); 3147 $self->{children}[1]->set_value ($offset);
3148}
3149
3150sub current_paragraph {
3151 my ($self) = @_;
3152
3153 $self->{top_paragraph} - 1
3154}
3155
3156sub scroll_to {
3157 my ($self, $para) = @_;
3158
3159 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3160
3161 $self->{scroll_to} = $para;
3162 $self->update;
2756} 3163}
2757 3164
2758sub clear { 3165sub clear {
2759 my ($self) = @_; 3166 my ($self) = @_;
2760 3167
2782 3189
2783 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3190 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2784 push @{$self->{par}}, $para; 3191 push @{$self->{par}}, $para;
2785 } 3192 }
2786 3193
3194 if (my $max = $self->{max_par}) {
3195 shift @{$self->{par}} while @{$self->{par}} > $max;
3196 }
3197
2787 $self->{need_reflow}++; 3198 $self->{need_reflow}++;
2788 $self->update; 3199 $self->update;
2789} 3200}
2790 3201
2791sub scroll_to_bottom { 3202sub scroll_to_bottom {
2792 my ($self) = @_; 3203 my ($self) = @_;
2793 3204
2794 $self->{scroll_to_bottom} = 1; 3205 $self->{scroll_to} = $#{$self->{par}};
2795 $self->update; 3206 $self->update;
2796} 3207}
2797 3208
3209sub force_uptodate {
3210 my ($self) = @_;
3211
3212 if (delete $self->{need_reflow}) {
3213 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3214
3215 my $height = 0;
3216
3217 for my $para (@{$self->{par}}) {
3218 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3219 my $layout = $self->get_layout ($para);
3220 my ($w, $h) = $layout->size;
3221
3222 $para->{w} = $w + $para->{indent};
3223 $para->{h} = $h;
3224 $para->{wrapped} = $layout->has_wrapped;
3225 }
3226
3227 $para->{y} = $height;
3228 $height += $para->{h};
3229 }
3230
3231 $self->{height} = $height;
3232 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3233
3234 delete $self->{texture};
3235 }
3236
3237 if (my $paridx = delete $self->{scroll_to}) {
3238 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3239 }
3240}
3241
2798sub update { 3242sub update {
2799 my ($self) = @_; 3243 my ($self) = @_;
2800 3244
2801 $self->SUPER::update; 3245 $self->SUPER::update;
2802 3246
2803 return unless $self->{h} > 0; 3247 return unless $self->{h} > 0;
2804 3248
2805 delete $self->{texture}; 3249 delete $self->{texture};
2806 3250
2807 $ROOT->on_post_alloc ($self => sub { 3251 $ROOT->on_post_alloc ($self => sub {
3252 $self->force_uptodate;
3253
2808 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3254 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 3255
2837 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3256 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2838 glClearColor 0, 0, 0, 0; 3257 glClearColor 0, 0, 0, 0;
2839 glClear GL_COLOR_BUFFER_BIT; 3258 glClear GL_COLOR_BUFFER_BIT;
2840 3259
3260 package CFPlus::UI::Base;
3261 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3262 (0, 0, $self->{w}, $self->{h});
3263
3264 my $top = int $self->{children}[1]{range}[0];
3265
3266 my $paridx = 0;
3267 my $top_paragraph;
2841 my $top = int $self->{children}[1]{range}[0]; 3268 my $top = int $self->{children}[1]{range}[0];
2842 3269
2843 my $y0 = $top; 3270 my $y0 = $top;
2844 my $y1 = $top + $H; 3271 my $y1 = $top + $H;
2845 3272
2846 my $y = 0;
2847
2848 for my $para (@{$self->{par}}) { 3273 for my $para (@{$self->{par}}) {
2849 my $h = $para->{h}; 3274 my $h = $para->{h};
3275 my $y = $para->{y};
2850 3276
2851 if ($y0 < $y + $h && $y < $y1) { 3277 if ($y0 < $y + $h && $y < $y1) {
2852
2853 my $layout = $self->get_layout ($para); 3278 my $layout = $self->get_layout ($para);
2854 3279
2855 $layout->render ($para->{indent}, $y - $y0); 3280 $layout->render ($para->{indent}, $y - $y0);
2856 3281
2857 if (my @w = @{ $para->{widget} }) { 3282 if (my @w = @{ $para->{widget} }) {
2866 $_->draw; 3291 $_->draw;
2867 } 3292 }
2868 } 3293 }
2869 } 3294 }
2870 3295
2871 $y += $h; 3296 $paridx++;
3297 $top_paragraph ||= $paridx if $y >= $top;
2872 } 3298 }
3299
3300 $self->{top_paragraph} = $top_paragraph;
2873 }; 3301 };
2874 }); 3302 });
2875} 3303}
2876 3304
2877sub reconfigure { 3305sub reconfigure {
3080 3508
3081sub new { 3509sub new {
3082 my $class = shift; 3510 my $class = shift;
3083 3511
3084 my $self = $class->SUPER::new ( 3512 my $self = $class->SUPER::new (
3513 size_w => 32,
3514 size_h => 8,
3085 aspect => 1, 3515 aspect => 1,
3086 can_events => 0, 3516 can_events => 0,
3087 @_, 3517 @_,
3088 ); 3518 );
3089 3519
3090 if ($self->{anim} && $self->{animspeed}) { 3520 if ($self->{anim} && $self->{animspeed}) {
3091 Scalar::Util::weaken (my $widget = $self); 3521 CFPlus::weaken (my $widget = $self);
3092 3522
3523 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3524 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3093 $self->{timer} = Event->timer ( 3525 $self->{timer} = Event->timer (
3094 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3095 hard => 1, 3526 parked => 1,
3096 interval => $self->{animspeed},
3097 cb => sub { 3527 cb => sub {
3528 return unless $::CONN && $widget;
3529
3098 ++$widget->{frame}; 3530 ++$widget->{frame};
3531 $widget->update_face;
3099 $widget->update; 3532 $widget->update;
3533
3534 $widget->update_timer;
3100 }, 3535 },
3101 ); 3536 );
3537
3538 $self->update_face;
3539 $self->update_timer;
3102 } 3540 }
3103 3541
3104 $self 3542 $self
3105} 3543}
3106 3544
3545sub update_timer {
3546 my ($self) = @_;
3547
3548 return unless $self->{timer};
3549
3550 if ($self->{visible}) {
3551 $self->{timer}->at (
3552 $self->{anim_start}
3553 + $self->{animspeed}
3554 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3555 );
3556 $self->{timer}->start;
3557 } else {
3558 $self->{timer}->stop;
3559 }
3560}
3561
3562sub update_face {
3563 my ($self) = @_;
3564
3565 return unless $::CONN;
3566
3567 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3568 if ($anim && @$anim) {
3569 delete $self->{wait_face};
3570 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3571 }
3572 }
3573}
3574
3107sub size_request { 3575sub size_request {
3108 (32, 8) 3576 my ($self) = @_;
3577
3578 if ($::CONN) {
3579 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3580 if (my $tex = $::CONN->{texture}[$faceid]) {
3581 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3582 } else {
3583 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3584 $self->realloc;
3585 });
3586 }
3587 }
3588 }
3589
3590 ($self->{size_w} || 8, $self->{size_h} || 8)
3109} 3591}
3110 3592
3111sub update { 3593sub update {
3112 my ($self) = @_; 3594 my ($self) = @_;
3113 3595
3114 return unless $self->{visible}; 3596 return unless $self->{visible};
3115 3597
3116 $self->SUPER::update; 3598 $self->SUPER::update;
3117} 3599}
3118 3600
3601sub invoke_visibility_change {
3602 my ($self) = @_;
3603
3604 $self->update_timer;
3605
3606 0
3607}
3608
3119sub _draw { 3609sub _draw {
3120 my ($self) = @_; 3610 my ($self) = @_;
3121 3611
3122 return unless $::CONN; 3612 return unless $::CONN;
3123 3613
3124 $self->SUPER::_draw; 3614 $self->SUPER::_draw;
3125 3615
3126 my $face;
3127
3128 if ($self->{frame}) {
3129 my $anim = $::CONN->{anim}[$self->{anim}]; 3616 my $faceid = $::CONN->{faceid}[$self->{face}]
3130 3617 or return;
3131 $face = $anim->[ $self->{frame} % @$anim ]
3132 if $anim && @$anim;
3133 }
3134 3618
3135 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 3619 my $tex = $::CONN->{texture}[$faceid];
3136 3620
3137 if ($tex) { 3621 if ($tex) {
3138 glEnable GL_TEXTURE_2D; 3622 glEnable GL_TEXTURE_2D;
3139 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3623 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3140 glColor 0, 0, 0, 1; 3624 glColor 0, 0, 0, 1;
3144} 3628}
3145 3629
3146sub destroy { 3630sub destroy {
3147 my ($self) = @_; 3631 my ($self) = @_;
3148 3632
3149 $self->{timer}->cancel 3633 (delete $self->{timer})->cancel
3150 if $self->{timer}; 3634 if $self->{timer};
3151 3635
3152 $self->SUPER::destroy; 3636 $self->SUPER::destroy;
3153} 3637}
3154 3638
3156 3640
3157package CFPlus::UI::Buttonbar; 3641package CFPlus::UI::Buttonbar;
3158 3642
3159our @ISA = CFPlus::UI::HBox::; 3643our @ISA = CFPlus::UI::HBox::;
3160 3644
3161# TODO: should actualyl wrap buttons and other goodies. 3645# TODO: should actually wrap buttons and other goodies.
3162 3646
3163############################################################################# 3647#############################################################################
3164 3648
3165package CFPlus::UI::Menu; 3649package CFPlus::UI::Menu;
3166 3650
3167our @ISA = CFPlus::UI::FancyFrame::; 3651our @ISA = CFPlus::UI::Toplevel::;
3168 3652
3169use CFPlus::OpenGL; 3653use CFPlus::OpenGL;
3170 3654
3171sub new { 3655sub new {
3172 my $class = shift; 3656 my $class = shift;
3350} 3834}
3351 3835
3352sub add { 3836sub add {
3353 my ($self, $title, $widget, $tooltip) = @_; 3837 my ($self, $title, $widget, $tooltip) = @_;
3354 3838
3355 Scalar::Util::weaken $self; 3839 CFPlus::weaken $self;
3356 3840
3357 $self->{buttonbar}->add (new CFPlus::UI::Button 3841 $self->{buttonbar}->add (new CFPlus::UI::Button
3358 markup => $title, 3842 markup => $title,
3359 tooltip => $tooltip, 3843 tooltip => $tooltip,
3360 on_activate => sub { $self->set_current_page ($widget) }, 3844 on_activate => sub { $self->set_current_page ($widget) },
3444 my $self = $class->SUPER::new ( 3928 my $self = $class->SUPER::new (
3445 fontsize => 0.8, 3929 fontsize => 0.8,
3446 @_, 3930 @_,
3447 ); 3931 );
3448 3932
3449 Scalar::Util::weaken (my $this = $self); 3933 CFPlus::weaken (my $this = $self);
3450 3934
3451 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3935 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3452 3936
3453 $self 3937 $self
3454} 3938}
3577 $self->SUPER::destroy; 4061 $self->SUPER::destroy;
3578} 4062}
3579 4063
3580############################################################################# 4064#############################################################################
3581 4065
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; 4066package CFPlus::UI::Root;
3767 4067
3768our @ISA = CFPlus::UI::Container::; 4068our @ISA = CFPlus::UI::Container::;
3769 4069
3770use List::Util qw(min max); 4070use List::Util qw(min max);
3777 my $self = $class->SUPER::new ( 4077 my $self = $class->SUPER::new (
3778 visible => 1, 4078 visible => 1,
3779 @_, 4079 @_,
3780 ); 4080 );
3781 4081
3782 Scalar::Util::weaken ($self->{root} = $self); 4082 CFPlus::weaken ($self->{root} = $self);
3783 4083
3784 $self 4084 $self
3785} 4085}
3786 4086
3787sub size_request { 4087sub size_request {
3982 while ($self->{post_alloc_hook}) { 4282 while ($self->{post_alloc_hook}) {
3983 $_->() 4283 $_->()
3984 for values %{delete $self->{post_alloc_hook}}; 4284 for values %{delete $self->{post_alloc_hook}};
3985 } 4285 }
3986 4286
3987
3988 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4287 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3989 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4288 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3990 glClear GL_COLOR_BUFFER_BIT; 4289 glClear GL_COLOR_BUFFER_BIT;
3991 4290
3992 glMatrixMode GL_PROJECTION; 4291 glMatrixMode GL_PROJECTION;
3996 glLoadIdentity; 4295 glLoadIdentity;
3997 4296
3998 { 4297 {
3999 package CFPlus::UI::Base; 4298 package CFPlus::UI::Base;
4000 4299
4001 ($draw_x, $draw_y, $draw_w, $draw_h) = 4300 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4002 (0, 0, $self->{w}, $self->{h}); 4301 (0, 0, $self->{w}, $self->{h});
4003 }
4004 4302
4005 $self->_draw; 4303 $self->_draw;
4304 }
4006} 4305}
4007 4306
4008############################################################################# 4307#############################################################################
4009 4308
4010package CFPlus::UI; 4309package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines