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.302 by root, Mon Jun 12 13:26:14 2006 UTC vs.
Revision 1.328 by root, Sat Jul 22 13:43:05 2006 UTC

82} 82}
83 83
84sub feed_sdl_button_down_event { 84sub feed_sdl_button_down_event {
85 my ($ev) = @_; 85 my ($ev) = @_;
86 my ($x, $y) = ($ev->{x}, $ev->{y}); 86 my ($x, $y) = ($ev->{x}, $ev->{y});
87
88 warn "button down $ev->{button}\n";#d#
87 89
88 if (!$BUTTON_STATE) { 90 if (!$BUTTON_STATE) {
89 my $widget = $ROOT->find_widget ($x, $y); 91 my $widget = $ROOT->find_widget ($x, $y);
90 92
91 $GRAB = $widget; 93 $GRAB = $widget;
281 undef $HOVER if $HOVER == $self; 283 undef $HOVER if $HOVER == $self;
282 284
283 $CFClient::UI::TOOLTIP_WATCHER->cb->() 285 $CFClient::UI::TOOLTIP_WATCHER->cb->()
284 if $TOOLTIP->{owner} == $self; 286 if $TOOLTIP->{owner} == $self;
285 287
286 $self->focus_out; 288 $self->emit ("focus_out");
287
288 $self->emit (visibility_change => 0); 289 $self->emit (visibility_change => 0);
289} 290}
290 291
291sub set_visibility { 292sub set_visibility {
292 my ($self, $visible) = @_; 293 my ($self, $visible) = @_;
334} 335}
335 336
336sub size_request { 337sub size_request {
337 require Carp; 338 require Carp;
338 Carp::confess "size_request is abstract"; 339 Carp::confess "size_request is abstract";
340}
341
342sub baseline_shift {
343 0
339} 344}
340 345
341sub configure { 346sub configure {
342 my ($self, $x, $y, $w, $h) = @_; 347 my ($self, $x, $y, $w, $h) = @_;
343 348
367 372
368 $self->{root}{size_alloc}{$self+0} = $self; 373 $self->{root}{size_alloc}{$self+0} = $self;
369 } 374 }
370} 375}
371 376
372sub size_allocate {
373 # nothing to be done
374}
375
376sub children { 377sub children {
377 # nop 378 # nop
378} 379}
379 380
380sub visible_children { 381sub visible_children {
418 my ($self, $x, $y) = @_; 419 my ($self, $x, $y) = @_;
419 420
420 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 421 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
421} 422}
422 423
423sub focus_in { 424sub invoke_focus_in {
424 my ($self) = @_; 425 my ($self) = @_;
425 426
426 return if $FOCUS == $self; 427 return if $FOCUS == $self;
427 return unless $self->{can_focus}; 428 return unless $self->{can_focus};
428 429
429 my $focus = $FOCUS; $FOCUS = $self; 430 $FOCUS = $self;
430 431
431 $self->_emit (focus_in => $focus); 432 $self->update;
432 433
433 $focus->update if $focus; 434 0
434 $FOCUS->update;
435} 435}
436 436
437sub focus_out { 437sub invoke_focus_out {
438 my ($self) = @_; 438 my ($self) = @_;
439 439
440 return unless $FOCUS == $self; 440 return unless $FOCUS == $self;
441 441
442 my $focus = $FOCUS; undef $FOCUS; 442 undef $FOCUS;
443 443
444 $self->_emit (focus_out => $focus); 444 $self->update;
445 445
446 $focus->update if $focus; #?
447
448 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 446 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
449 unless $FOCUS; 447 unless $FOCUS;
450}
451 448
449 0
450}
451
452sub grab_focus {
453 my ($self) = @_;
454
455 $FOCUS->emit ("focus_out") if $FOCUS;
456 $self->emit ("focus_in");
457}
458
452sub mouse_motion { 0 } 459sub invoke_mouse_motion { 0 }
453sub button_up { 0 } 460sub invoke_button_up { 0 }
454sub key_down { 0 } 461sub invoke_key_down { 0 }
455sub key_up { 0 } 462sub invoke_key_up { 0 }
456 463
457sub button_down { 464sub invoke_button_down {
458 my ($self, $ev, $x, $y) = @_; 465 my ($self, $ev, $x, $y) = @_;
459 466
460 $self->focus_in; 467 $self->grab_focus;
468
469 warn "button down $ev->{button} $x $y\n";#d#
461 470
462 0 471 0
472}
473
474sub connect {
475 my ($self, $signal, $cb) = @_;
476
477 push @{ $self->{signal_cb}{$signal} }, $cb;
478}
479
480sub emit {
481 my ($self, $signal, @args) = @_;
482
483 #d##TODO# stop propagating at first true, do not use sum
484 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before
485 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure
486 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
463} 487}
464 488
465sub find_widget { 489sub find_widget {
466 my ($self, $x, $y) = @_; 490 my ($self, $x, $y) = @_;
467 491
477sub set_parent { 501sub set_parent {
478 my ($self, $parent) = @_; 502 my ($self, $parent) = @_;
479 503
480 Scalar::Util::weaken ($self->{parent} = $parent); 504 Scalar::Util::weaken ($self->{parent} = $parent);
481 $self->set_visible if $parent->{visible}; 505 $self->set_visible if $parent->{visible};
482}
483
484sub connect {
485 my ($self, $signal, $cb) = @_;
486
487 push @{ $self->{signal_cb}{$signal} }, $cb;
488}
489
490sub _emit {
491 my ($self, $signal, @args) = @_;
492
493 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
494}
495
496sub emit {
497 my ($self, $signal, @args) = @_;
498
499 $self->_emit ($signal, @args)
500 || $self->$signal (@args);
501}
502
503sub visibility_change {
504 #my ($self, $visible) = @_;
505} 506}
506 507
507sub realloc { 508sub realloc {
508 my ($self) = @_; 509 my ($self) = @_;
509 510
542 return unless $self->{h} && $self->{w}; 543 return unless $self->{h} && $self->{w};
543 544
544 # update screen rectangle 545 # update screen rectangle
545 local $draw_x = $draw_x + $self->{x}; 546 local $draw_x = $draw_x + $self->{x};
546 local $draw_y = $draw_y + $self->{y}; 547 local $draw_y = $draw_y + $self->{y};
547 local $draw_w = $draw_x + $self->{w};
548 local $draw_h = $draw_y + $self->{h};
549 548
550 # skip widgets that are entirely outside the drawing area 549 # skip widgets that are entirely outside the drawing area
551 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) 550 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
552 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); 551 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
553 552
592} 591}
593 592
594sub DESTROY { 593sub DESTROY {
595 my ($self) = @_; 594 my ($self) = @_;
596 595
596 return if CFClient::in_destruct;
597
597 delete $WIDGET{$self+0}; 598 delete $WIDGET{$self+0};
598 599
599 eval { $self->destroy }; 600 eval { $self->destroy };
600 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 601 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
601} 602}
789 790
790sub size_request { 791sub size_request {
791 $_[0]{children}[0]->size_request 792 $_[0]{children}[0]->size_request
792} 793}
793 794
794sub size_allocate { 795sub invoke_size_allocate {
795 my ($self, $w, $h) = @_; 796 my ($self, $w, $h) = @_;
796 797
797 $self->{children}[0]->configure (0, 0, $w, $h); 798 $self->{children}[0]->configure (0, 0, $w, $h);
799
800 1
798} 801}
799 802
800############################################################################# 803#############################################################################
801 804
802# back-buffered drawing area 805# back-buffered drawing area
818 821
819 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 822 $ROOT->on_post_alloc ($self => sub { $self->render_child });
820 $self->SUPER::update; 823 $self->SUPER::update;
821} 824}
822 825
823sub size_allocate { 826sub invoke_size_allocate {
824 my ($self, $w, $h) = @_; 827 my ($self, $w, $h) = @_;
825 828
826 $self->SUPER::size_allocate ($w, $h);
827 $self->update; 829 $self->update;
830
831 $self->SUPER::invoke_size_allocate ($w, $h)
828} 832}
829 833
830sub _render { 834sub _render {
831 my ($self) = @_; 835 my ($self) = @_;
832 836
893 $h = 10 if $self->{scroll_y}; 897 $h = 10 if $self->{scroll_y};
894 898
895 ($w, $h) 899 ($w, $h)
896} 900}
897 901
898sub size_allocate { 902sub invoke_size_allocate {
899 my ($self, $w, $h) = @_; 903 my ($self, $w, $h) = @_;
900 904
901 my $child = $self->child; 905 my $child = $self->child;
902 906
903 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w}; 907 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w};
904 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h}; 908 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h};
905 909
906 $self->child->configure (0, 0, $w, $h); 910 $self->child->configure (0, 0, $w, $h);
907 $self->update; 911 $self->update;
912
913 1
908} 914}
909 915
910sub set_offset { 916sub set_offset {
911 my ($self, $x, $y) = @_; 917 my ($self, $x, $y) = @_;
912 918
986 $self->add ($child) if $child; 992 $self->add ($child) if $child;
987 993
988 $self 994 $self
989} 995}
990 996
997#TODO# update range on size_allocate depending on child
998
991sub add { 999sub add {
992 my ($self, $widget) = @_; 1000 my ($self, $widget) = @_;
993 1001
994 $self->{vp}->add ($self->{child} = $widget); 1002 $self->{vp}->add ($self->{child} = $widget);
995} 1003}
996 1004
1005sub invoke_button_down {
1006 my ($self, $ev) = @_;
1007
1008 warn "button down $ev->{button}\n";#d#
1009
1010 0
1011}
1012
1013sub update_slider {
1014 my ($self) = @_;
1015
1016 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]);
1017}
1018
997sub update { 1019sub update {
998 my ($self) = @_; 1020 my ($self) = @_;
999 1021
1000 $self->SUPER::update; 1022 $self->SUPER::update;
1001 1023
1002 # todo: overwrite size_allocate of child 1024 $self->update_slider;
1003 my $child = $self->{vp}->child;
1004 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1005} 1025}
1006 1026
1007sub size_allocate { 1027sub invoke_size_allocate {
1008 my ($self, $w, $h) = @_; 1028 my ($self, $w, $h) = @_;
1009 1029
1030 $self->update_slider;
1031
1010 $self->SUPER::size_allocate ($w, $h); 1032 $self->SUPER::invoke_size_allocate ($w, $h)
1011
1012 my $child = $self->{vp}->child;
1013 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1014} 1033}
1015
1016#TODO# update range on size_allocate depending on child
1017# update viewport offset on scroll
1018 1034
1019############################################################################# 1035#############################################################################
1020 1036
1021package CFClient::UI::Frame; 1037package CFClient::UI::Frame;
1022 1038
1078 my $self = $class->SUPER::new ( 1094 my $self = $class->SUPER::new (
1079 bg => [1, 1, 1, 1], 1095 bg => [1, 1, 1, 1],
1080 border_bg => [1, 1, 1, 1], 1096 border_bg => [1, 1, 1, 1],
1081 border => 0.6, 1097 border => 0.6,
1082 can_events => 1, 1098 can_events => 1,
1083 min_w => 16, 1099 min_w => 64,
1084 min_h => 16, 1100 min_h => 32,
1085 %arg, 1101 %arg,
1086 ); 1102 );
1087 1103
1088 $self->{title_widget} = new CFClient::UI::Label 1104 $self->{title_widget} = new CFClient::UI::Label
1089 align => 0, 1105 align => 0,
1090 valign => 1, 1106 valign => 1,
1091 text => $self->{title}, 1107 text => $self->{title},
1092 fontsize => $self->{border}, 1108 fontsize => $self->{border},
1093 if exists $self->{title}; 1109 if exists $self->{title};
1094 1110
1111 if ($self->{has_close_button}) {
1112 $self->{close_button} =
1113 new CFClient::UI::ImageButton
1114 path => 'x1_close.png',
1115 on_activate => sub { $self->emit ("delete") };
1116
1117 $self->CFClient::UI::Container::add ($self->{close_button});
1118 }
1119
1095 $self 1120 $self
1096} 1121}
1097 1122
1098sub add { 1123sub add {
1099 my ($self, @widgets) = @_; 1124 my ($self, @widgets) = @_;
1100 1125
1101 $self->SUPER::add (@widgets); 1126 $self->SUPER::add (@widgets);
1127 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button};
1102 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1128 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1103} 1129}
1104 1130
1105sub border { 1131sub border {
1106 int $_[0]{border} * $::FONTSIZE 1132 int $_[0]{border} * $::FONTSIZE
1109sub size_request { 1135sub size_request {
1110 my ($self) = @_; 1136 my ($self) = @_;
1111 1137
1112 $self->{title_widget}->size_request 1138 $self->{title_widget}->size_request
1113 if $self->{title_widget}; 1139 if $self->{title_widget};
1140
1141 $self->{close_button}->size_request
1142 if $self->{close_button};
1114 1143
1115 my ($w, $h) = $self->SUPER::size_request; 1144 my ($w, $h) = $self->SUPER::size_request;
1116 1145
1117 ( 1146 (
1118 $w + $self->border * 2, 1147 $w + $self->border * 2,
1119 $h + $self->border * 2, 1148 $h + $self->border * 2,
1120 ) 1149 )
1121} 1150}
1122 1151
1123sub size_allocate { 1152sub invoke_size_allocate {
1124 my ($self, $w, $h) = @_; 1153 my ($self, $w, $h) = @_;
1125 1154
1126 if ($self->{title_widget}) { 1155 if ($self->{title_widget}) {
1127 $self->{title_widget}{w} = $w; 1156 $self->{title_widget}{w} = $w;
1128 $self->{title_widget}{h} = $h; 1157 $self->{title_widget}{h} = $h;
1129 $self->{title_widget}->size_allocate ($w, $h); 1158 $self->{title_widget}->invoke_size_allocate ($w, $h);
1130 } 1159 }
1131 1160
1132 my $border = $self->border; 1161 my $border = $self->border;
1133 1162
1134 $h -= List::Util::max 0, $border * 2; 1163 $h -= List::Util::max 0, $border * 2;
1135 $w -= List::Util::max 0, $border * 2; 1164 $w -= List::Util::max 0, $border * 2;
1165
1166 $self->child->configure ($border, $border, $w, $h);
1167
1168 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border)
1169 if $self->{close_button};
1170
1171 1
1172}
1173
1174sub invoke_delete {
1175 my ($self) = @_;
1176
1177 $self->hide;
1136 1178
1137 $self->child->configure ($border, $border, $w, $h); 1179 1
1138} 1180}
1139 1181
1140sub button_down { 1182sub invoke_button_down {
1141 my ($self, $ev, $x, $y) = @_; 1183 my ($self, $ev, $x, $y) = @_;
1142 1184
1143 my ($w, $h) = @$self{qw(w h)}; 1185 my ($w, $h) = @$self{qw(w h)};
1144 my $border = $self->border; 1186 my $border = $self->border;
1145 1187
1176 1218
1177 ($x, $y) = ($ev->{x}, $ev->{y}); 1219 ($x, $y) = ($ev->{x}, $ev->{y});
1178 1220
1179 $self->move_abs ($bx + $x - $ox, $by + $y - $oy); 1221 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1180 # HACK: the next line is required to enforce placement 1222 # HACK: the next line is required to enforce placement
1181 $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h}); 1223 $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h});
1182 }; 1224 };
1183 } else { 1225 } else {
1184 return 0; 1226 return 0;
1185 } 1227 }
1186 1228
1187 1 1229 1
1188} 1230}
1189 1231
1190sub button_up { 1232sub invoke_button_up {
1191 my ($self, $ev, $x, $y) = @_; 1233 my ($self, $ev, $x, $y) = @_;
1192 1234
1193 !!delete $self->{motion} 1235 ! ! delete $self->{motion}
1194} 1236}
1195 1237
1196sub mouse_motion { 1238sub invoke_mouse_motion {
1197 my ($self, $ev, $x, $y) = @_; 1239 my ($self, $ev, $x, $y) = @_;
1198 1240
1199 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1241 $self->{motion}->($ev, $x, $y) if $self->{motion};
1200 1242
1201 !!$self->{motion} 1243 ! ! $self->{motion}
1202} 1244}
1203 1245
1204sub _draw { 1246sub _draw {
1205 my ($self) = @_; 1247 my ($self) = @_;
1206 1248
1235 $child->draw; 1277 $child->draw;
1236 1278
1237 if ($self->{title_widget}) { 1279 if ($self->{title_widget}) {
1238 glTranslate 0, $border - $self->{h}; 1280 glTranslate 0, $border - $self->{h};
1239 $self->{title_widget}->_draw; 1281 $self->{title_widget}->_draw;
1282
1283 glTranslate 0, - ($border - $self->{h});
1240 } 1284 }
1285
1286 $self->{close_button}->draw
1287 if $self->{close_button};
1241} 1288}
1242 1289
1243############################################################################# 1290#############################################################################
1244 1291
1245package CFClient::UI::Table; 1292package CFClient::UI::Table;
1324 (sum @$ws), 1371 (sum @$ws),
1325 (sum @$hs), 1372 (sum @$hs),
1326 ) 1373 )
1327} 1374}
1328 1375
1329sub size_allocate { 1376sub invoke_size_allocate {
1330 my ($self, $w, $h) = @_; 1377 my ($self, $w, $h) = @_;
1331 1378
1332 my ($ws, $hs) = $self->get_wh; 1379 my ($ws, $hs) = $self->get_wh;
1333 1380
1334 my $req_w = (sum @$ws) || 1; 1381 my $req_w = (sum @$ws) || 1;
1366 } 1413 }
1367 1414
1368 $y += $row_h; 1415 $y += $row_h;
1369 } 1416 }
1370 1417
1418 1
1371} 1419}
1372 1420
1373sub find_widget { 1421sub find_widget {
1374 my ($self, $x, $y) = @_; 1422 my ($self, $x, $y) = @_;
1375 1423
1412 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1460 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1413 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1461 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1414 ) 1462 )
1415} 1463}
1416 1464
1417sub size_allocate { 1465sub invoke_size_allocate {
1418 my ($self, $w, $h) = @_; 1466 my ($self, $w, $h) = @_;
1419 1467
1420 my $space = $self->{vertical} ? $h : $w; 1468 my $space = $self->{vertical} ? $h : $w;
1421 my $children = $self->{children}; 1469 my @children = $self->visible_children;
1422 1470
1423 my @req; 1471 my @req;
1424 1472
1425 if ($self->{homogeneous}) { 1473 if ($self->{homogeneous}) {
1426 @req = ($space / (@$children || 1)) x @$children; 1474 @req = ($space / (@children || 1)) x @children;
1427 } else { 1475 } else {
1428 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; 1476 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children;
1429 my $req = List::Util::sum @req; 1477 my $req = List::Util::sum @req;
1430 1478
1431 if ($req > $space) { 1479 if ($req > $space) {
1432 # ah well, not enough space 1480 # ah well, not enough space
1433 $_ *= $space / $req for @req; 1481 $_ *= $space / $req for @req;
1434 } else { 1482 } else {
1435 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; 1483 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1436 1484
1437 $space = ($space - $req) / $expand; # remaining space to give away 1485 $space = ($space - $req) / $expand; # remaining space to give away
1438 1486
1439 $req[$_] += $space * $children->[$_]{expand} 1487 $req[$_] += $space * $children[$_]{expand}
1440 for 0 .. $#$children; 1488 for 0 .. $#children;
1441 } 1489 }
1442 } 1490 }
1443 1491
1444 CFClient::UI::harmonize \@req; 1492 CFClient::UI::harmonize \@req;
1445 1493
1446 my $pos = 0; 1494 my $pos = 0;
1447 for (0 .. $#$children) { 1495 for (0 .. $#children) {
1448 my $alloc = $req[$_]; 1496 my $alloc = $req[$_];
1449 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1497 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1450 1498
1451 $pos += $alloc; 1499 $pos += $alloc;
1452 } 1500 }
1453 1501
1454 1 1502 1
1557 my ($self, $text) = @_; 1605 my ($self, $text) = @_;
1558 1606
1559 return if $self->{text} eq "T$text"; 1607 return if $self->{text} eq "T$text";
1560 $self->{text} = "T$text"; 1608 $self->{text} = "T$text";
1561 1609
1562 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1563 $self->{layout}->set_text ($text); 1610 $self->{layout}->set_text ($text);
1564 1611
1565 delete $self->{size_req}; 1612 delete $self->{size_req};
1566 $self->realloc; 1613 $self->realloc;
1567 $self->update; 1614 $self->update;
1573 return if $self->{text} eq "M$markup"; 1620 return if $self->{text} eq "M$markup";
1574 $self->{text} = "M$markup"; 1621 $self->{text} = "M$markup";
1575 1622
1576 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1623 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1577 1624
1578 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1579 $self->{layout}->set_markup ($markup); 1625 $self->{layout}->set_markup ($markup);
1580 1626
1581 delete $self->{size_req}; 1627 delete $self->{size_req};
1582 $self->realloc; 1628 $self->realloc;
1583 $self->update; 1629 $self->update;
1595 1641
1596 my ($w, $h) = $self->{layout}->size; 1642 my ($w, $h) = $self->{layout}->size;
1597 1643
1598 if (exists $self->{template}) { 1644 if (exists $self->{template}) {
1599 $self->{template}->set_font ($self->{font}) if $self->{font}; 1645 $self->{template}->set_font ($self->{font}) if $self->{font};
1646 $self->{template}->set_width ($self->{max_w} || -1);
1600 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1647 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1601 1648
1602 my ($w2, $h2) = $self->{template}->size; 1649 my ($w2, $h2) = $self->{template}->size;
1603 1650
1604 $w = List::Util::max $w, $w2; 1651 $w = List::Util::max $w, $w2;
1609 }; 1656 };
1610 1657
1611 @{ $self->{size_req} } 1658 @{ $self->{size_req} }
1612} 1659}
1613 1660
1661sub baseline_shift {
1662 $_[0]{layout}->descent
1663}
1664
1614sub size_allocate { 1665sub invoke_size_allocate {
1615 my ($self, $w, $h) = @_; 1666 my ($self, $w, $h) = @_;
1616 1667
1617 delete $self->{ox}; 1668 delete $self->{ox};
1618 1669
1619 delete $self->{texture} 1670 delete $self->{texture}
1620 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w}; 1671 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1672
1673 1
1621} 1674}
1622 1675
1623sub set_fontsize { 1676sub set_fontsize {
1624 my ($self, $fontsize) = @_; 1677 my ($self, $fontsize) = @_;
1625 1678
1626 $self->{fontsize} = $fontsize; 1679 $self->{fontsize} = $fontsize;
1680 delete $self->{size_req};
1627 delete $self->{texture}; 1681 delete $self->{texture};
1628 1682
1629 $self->realloc; 1683 $self->realloc;
1630} 1684}
1631 1685
1632sub reconfigure { 1686sub reconfigure {
1633 my ($self) = @_; 1687 my ($self) = @_;
1634 1688
1635 delete $self->{size_req}; 1689 delete $self->{size_req};
1690 delete $self->{texture};
1636 1691
1637 $self->SUPER::reconfigure; 1692 $self->SUPER::reconfigure;
1638} 1693}
1639 1694
1640sub _draw { 1695sub _draw {
1641 my ($self) = @_; 1696 my ($self) = @_;
1642 1697
1643 $self->SUPER::_draw; # draw background, if applicable 1698 $self->SUPER::_draw; # draw background, if applicable
1644 1699
1645 my $tex = $self->{texture} ||= do { 1700 my $size = $self->{texture} ||= do {
1646 $self->{layout}->set_foreground (@{$self->{fg}}); 1701 $self->{layout}->set_foreground (@{$self->{fg}});
1647 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1702 $self->{layout}->set_font ($self->{font}) if $self->{font};
1648 $self->{layout}->set_width ($self->{w}); 1703 $self->{layout}->set_width ($self->{w});
1649 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1704 $self->{layout}->set_ellipsise ($self->{ellipsise});
1650 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1705 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1651 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1706 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1652 1707
1653 new_from_layout CFClient::Texture $self->{layout} 1708 [$self->{layout}->size]
1654 }; 1709 };
1655 1710
1656 unless (exists $self->{ox}) { 1711 unless (exists $self->{ox}) {
1657 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1712 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1658 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1713 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x}
1659 : ($self->{w} - $tex->{w}) * 0.5); 1714 : ($self->{w} - $size->[0]) * 0.5);
1660 1715
1661 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1716 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1662 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1717 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1663 : ($self->{h} - $tex->{h}) * 0.5); 1718 : ($self->{h} - $size->[1]) * 0.5);
1664 }; 1719 };
1665 1720
1666 glEnable GL_TEXTURE_2D;
1667
1668 my $w = List::Util::min $self->{w} + 4, $tex->{w}; 1721 my $w = List::Util::min $self->{w} + 4, $size->[0];
1669 my $h = List::Util::min $self->{h} + 2, $tex->{h}; 1722 my $h = List::Util::min $self->{h} + 2, $size->[1];
1670 1723
1671 if ($tex->{format} == GL_ALPHA) { 1724 $self->{layout}->render ($self->{ox}, $self->{oy});
1672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1673 glColor @{$self->{fg}};
1674 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1675 } else {
1676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1677 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1678 }
1679
1680 glDisable GL_TEXTURE_2D;
1681} 1725}
1682 1726
1683############################################################################# 1727#############################################################################
1684 1728
1685package CFClient::UI::EntryBase; 1729package CFClient::UI::EntryBase;
1718 1762
1719 $text =~ s/./*/g if $self->{hidden}; 1763 $text =~ s/./*/g if $self->{hidden};
1720 $self->{layout}->set_text ("$text "); 1764 $self->{layout}->set_text ("$text ");
1721 delete $self->{size_req}; 1765 delete $self->{size_req};
1722 1766
1723 $self->_emit (changed => $self->{text}); 1767 $self->emit (changed => $self->{text});
1724 1768
1725 $self->realloc; 1769 $self->realloc;
1726 $self->update; 1770 $self->update;
1727} 1771}
1728 1772
1743 my ($w, $h) = $self->SUPER::size_request; 1787 my ($w, $h) = $self->SUPER::size_request;
1744 1788
1745 ($w + 1, $h) # add 1 for cursor 1789 ($w + 1, $h) # add 1 for cursor
1746} 1790}
1747 1791
1748sub key_down { 1792sub invoke_key_down {
1749 my ($self, $ev) = @_; 1793 my ($self, $ev) = @_;
1750 1794
1751 my $mod = $ev->{mod}; 1795 my $mod = $ev->{mod};
1752 my $sym = $ev->{sym}; 1796 my $sym = $ev->{sym};
1753 my $uni = $ev->{unicode}; 1797 my $uni = $ev->{unicode};
1765 } elsif ($sym == CFClient::SDLK_HOME) { 1809 } elsif ($sym == CFClient::SDLK_HOME) {
1766 $self->{cursor} = 0; 1810 $self->{cursor} = 0;
1767 } elsif ($sym == CFClient::SDLK_END) { 1811 } elsif ($sym == CFClient::SDLK_END) {
1768 $self->{cursor} = length $text; 1812 $self->{cursor} = length $text;
1769 } elsif ($uni == 27) { 1813 } elsif ($uni == 27) {
1770 $self->_emit ('escape'); 1814 $self->emit ('escape');
1771 } elsif ($uni) { 1815 } elsif ($uni) {
1772 substr $text, $self->{cursor}++, 0, chr $uni; 1816 substr $text, $self->{cursor}++, 0, chr $uni;
1773 } else { 1817 } else {
1774 return 0; 1818 return 0;
1775 } 1819 }
1779 $self->realloc; 1823 $self->realloc;
1780 1824
1781 1 1825 1
1782} 1826}
1783 1827
1784sub focus_in { 1828sub invoke_focus_in {
1785 my ($self) = @_; 1829 my ($self) = @_;
1786 1830
1787 $self->{last_activity} = $::NOW; 1831 $self->{last_activity} = $::NOW;
1788 1832
1789 $self->SUPER::focus_in; 1833 $self->SUPER::invoke_focus_in
1790} 1834}
1791 1835
1792sub button_down { 1836sub invoke_button_down {
1793 my ($self, $ev, $x, $y) = @_; 1837 my ($self, $ev, $x, $y) = @_;
1794 1838
1795 $self->SUPER::button_down ($ev, $x, $y); 1839 $self->SUPER::invoke_button_down ($ev, $x, $y);
1796 1840
1797 my $idx = $self->{layout}->xy_to_index ($x, $y); 1841 my $idx = $self->{layout}->xy_to_index ($x, $y);
1798 1842
1799 # byte-index to char-index 1843 # byte-index to char-index
1800 my $text = $self->{text}; 1844 my $text = $self->{text};
1801 utf8::encode $text; 1845 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1802 $self->{cursor} = length substr $text, 0, $idx; 1846 $self->{cursor} = length $text;
1803 1847
1804 $self->_set_text ($self->{text}); 1848 $self->_set_text ($self->{text});
1805 $self->update; 1849 $self->update;
1806 1850
1807 1 1851 1
1808} 1852}
1809 1853
1810sub mouse_motion { 1854sub invoke_mouse_motion {
1811 my ($self, $ev, $x, $y) = @_; 1855 my ($self, $ev, $x, $y) = @_;
1812# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1856# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1813 1857
1814 0 1858 1
1815} 1859}
1816 1860
1817sub _draw { 1861sub _draw {
1818 my ($self) = @_; 1862 my ($self) = @_;
1819 1863
1860 1904
1861our @ISA = CFClient::UI::EntryBase::; 1905our @ISA = CFClient::UI::EntryBase::;
1862 1906
1863use CFClient::OpenGL; 1907use CFClient::OpenGL;
1864 1908
1865sub key_down { 1909sub invoke_key_down {
1866 my ($self, $ev) = @_; 1910 my ($self, $ev) = @_;
1867 1911
1868 my $sym = $ev->{sym}; 1912 my $sym = $ev->{sym};
1869 1913
1870 if ($sym == 13) { 1914 if ($sym == 13) {
1871 unshift @{$self->{history}}, 1915 unshift @{$self->{history}},
1872 my $txt = $self->get_text; 1916 my $txt = $self->get_text;
1917
1873 $self->{history_pointer} = -1; 1918 $self->{history_pointer} = -1;
1874 $self->{history_saveback} = ''; 1919 $self->{history_saveback} = '';
1875 $self->_emit (activate => $txt); 1920 $self->emit (activate => $txt);
1876 $self->update; 1921 $self->update;
1877 1922
1878 } elsif ($sym == CFClient::SDLK_UP) { 1923 } elsif ($sym == CFClient::SDLK_UP) {
1879 if ($self->{history_pointer} < 0) { 1924 if ($self->{history_pointer} < 0) {
1880 $self->{history_saveback} = $self->get_text; 1925 $self->{history_saveback} = $self->get_text;
1896 } else { 1941 } else {
1897 $self->set_text ($self->{history_saveback}); 1942 $self->set_text ($self->{history_saveback});
1898 } 1943 }
1899 1944
1900 } else { 1945 } else {
1901 return $self->SUPER::key_down ($ev) 1946 return $self->SUPER::invoke_key_down ($ev)
1902 } 1947 }
1903 1948
1904 1 1949 1
1905} 1950}
1906 1951
1930 can_events => 1, 1975 can_events => 1,
1931 @_ 1976 @_
1932 ) 1977 )
1933} 1978}
1934 1979
1935sub activate { }
1936
1937sub button_up { 1980sub invoke_button_up {
1938 my ($self, $ev, $x, $y) = @_; 1981 my ($self, $ev, $x, $y) = @_;
1939 1982
1940 $self->emit ("activate") 1983 $self->emit ("activate")
1941 if $x >= 0 && $x < $self->{w} 1984 if $x >= 0 && $x < $self->{w}
1942 && $y >= 0 && $y < $self->{h}; 1985 && $y >= 0 && $y < $self->{h};
1956 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 1999 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1957 2000
1958 glDisable GL_TEXTURE_2D; 2001 glDisable GL_TEXTURE_2D;
1959 2002
1960 $self->SUPER::_draw; 2003 $self->SUPER::_draw;
2004}
2005
2006#############################################################################
2007
2008package CFClient::UI::ImageButton;
2009
2010our @ISA = CFClient::UI::Image::;
2011
2012use CFClient::OpenGL;
2013
2014my %textures;
2015
2016sub new {
2017 my $class = shift;
2018
2019 my $self = $class->SUPER::new (
2020 padding_x => 4,
2021 padding_y => 4,
2022 fg => [1, 1, 1],
2023 active_fg => [0, 0, 1],
2024 can_hover => 1,
2025 align => 0,
2026 valign => 0,
2027 can_events => 1,
2028 @_
2029 );
2030}
2031
2032sub invoke_button_up {
2033 my ($self, $ev, $x, $y) = @_;
2034
2035 $self->emit ("activate")
2036 if $x >= 0 && $x < $self->{w}
2037 && $y >= 0 && $y < $self->{h};
2038
2039 1
1961} 2040}
1962 2041
1963############################################################################# 2042#############################################################################
1964 2043
1965package CFClient::UI::CheckBox; 2044package CFClient::UI::CheckBox;
1992 my ($self) = @_; 2071 my ($self) = @_;
1993 2072
1994 (6) x 2 2073 (6) x 2
1995} 2074}
1996 2075
2076sub toggle {
2077 my ($self) = @_;
2078
2079 $self->{state} = !$self->{state};
2080 $self->emit (changed => $self->{state});
2081 $self->update;
2082}
2083
1997sub button_down { 2084sub invoke_button_down {
1998 my ($self, $ev, $x, $y) = @_; 2085 my ($self, $ev, $x, $y) = @_;
1999 2086
2000 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 2087 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2001 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 2088 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2002 $self->{state} = !$self->{state}; 2089 $self->toggle;
2003 $self->_emit (changed => $self->{state});
2004 } else { 2090 } else {
2005 return 0 2091 return 0
2006 } 2092 }
2007 2093
2008 1 2094 1
2033package CFClient::UI::Image; 2119package CFClient::UI::Image;
2034 2120
2035our @ISA = CFClient::UI::Base::; 2121our @ISA = CFClient::UI::Base::;
2036 2122
2037use CFClient::OpenGL; 2123use CFClient::OpenGL;
2038use Carp qw/confess/;
2039 2124
2040our %loaded_images; 2125our %texture_cache;
2041 2126
2042sub new { 2127sub new {
2043 my $class = shift; 2128 my $class = shift;
2044 2129
2045 my $self = $class->SUPER::new (can_events => 0, @_); 2130 my $self = $class->SUPER::new (
2131 can_events => 0,
2132 @_,
2133 );
2046 2134
2047 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 2135 $self->{path} || $self->{tex}
2136 or Carp::croak "'path' or 'tex' attributes required";
2048 2137
2049 $loaded_images{$self->{image}} ||= 2138 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2050 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 2139 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2051 2140
2052 my $tex = $self->{tex} = $loaded_images{$self->{image}}; 2141 Scalar::Util::weaken $texture_cache{$self->{path}};
2053 2142
2054 Scalar::Util::weaken $loaded_images{$self->{image}}; 2143 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2055
2056 $self->{aspect} = $tex->{w} / $tex->{h};
2057 2144
2058 $self 2145 $self
2059} 2146}
2060 2147
2061sub size_request { 2148sub size_request {
2062 my ($self) = @_; 2149 my ($self) = @_;
2063 2150
2064 ($self->{tex}->{w}, $self->{tex}->{h}) 2151 ($self->{tex}{w}, $self->{tex}{h})
2065} 2152}
2066 2153
2067sub _draw { 2154sub _draw {
2068 my ($self) = @_; 2155 my ($self) = @_;
2069 2156
2178 my $ycut1 = max 0, min 1, $ycut; 2265 my $ycut1 = max 0, min 1, $ycut;
2179 my $ycut2 = max 0, min 1, $ycut - 1; 2266 my $ycut2 = max 0, min 1, $ycut - 1;
2180 2267
2181 my $h1 = $self->{h} * (1 - $ycut1); 2268 my $h1 = $self->{h} * (1 - $ycut1);
2182 my $h2 = $self->{h} * (1 - $ycut2); 2269 my $h2 = $self->{h} * (1 - $ycut2);
2270 my $h3 = $self->{h};
2271
2272 $_ = $_ * (284-4)/288 + 4/288 for ($h1, $h2, $h3);
2183 2273
2184 glEnable GL_BLEND; 2274 glEnable GL_BLEND;
2185 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 2275 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2186 GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2276 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2187 glEnable GL_TEXTURE_2D; 2277 glEnable GL_TEXTURE_2D;
2206 2296
2207 if ($t3) { 2297 if ($t3) {
2208 glBindTexture GL_TEXTURE_2D, $t3->{name}; 2298 glBindTexture GL_TEXTURE_2D, $t3->{name};
2209 glBegin GL_QUADS; 2299 glBegin GL_QUADS;
2210 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; 2300 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
2211 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; 2301 glTexCoord 0 , $t3->{t}; glVertex 0 , $h3;
2212 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; 2302 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3;
2213 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; 2303 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
2214 glEnd; 2304 glEnd;
2215 } 2305 }
2216 2306
2217 glDisable GL_BLEND; 2307 glDisable GL_BLEND;
2305 $self->update; 2395 $self->update;
2306 2396
2307 $self 2397 $self
2308} 2398}
2309 2399
2310sub changed { }
2311
2312sub set_range { 2400sub set_range {
2313 my ($self, $range) = @_; 2401 my ($self, $range) = @_;
2314 2402
2315 ($range, $self->{range}) = ($self->{range}, $range); 2403 ($range, $self->{range}) = ($self->{range}, $range);
2316 2404
2336 if $unit; 2424 if $unit;
2337 2425
2338 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2426 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2339 2427
2340 if ($value != $old_value) { 2428 if ($value != $old_value) {
2341 $self->_emit (changed => $value); 2429 $self->emit (changed => $value);
2342 $self->update; 2430 $self->update;
2343 } 2431 }
2344} 2432}
2345 2433
2346sub size_request { 2434sub size_request {
2347 my ($self) = @_; 2435 my ($self) = @_;
2348 2436
2349 ($self->{req_w}, $self->{req_h}) 2437 ($self->{req_w}, $self->{req_h})
2350} 2438}
2351 2439
2352sub button_down { 2440sub invoke_button_down {
2353 my ($self, $ev, $x, $y) = @_; 2441 my ($self, $ev, $x, $y) = @_;
2354 2442
2355 $self->SUPER::button_down ($ev, $x, $y); 2443 $self->SUPER::invoke_button_down ($ev, $x, $y);
2356 2444
2357 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2445 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2358 2446
2359 $self->mouse_motion ($ev, $x, $y) 2447 $self->invoke_mouse_motion ($ev, $x, $y)
2360} 2448}
2361 2449
2362sub mouse_motion { 2450sub invoke_mouse_motion {
2363 my ($self, $ev, $x, $y) = @_; 2451 my ($self, $ev, $x, $y) = @_;
2364 2452
2365 if ($GRAB == $self) { 2453 if ($GRAB == $self) {
2366 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2454 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2367 2455
2488 can_events => 0, 2576 can_events => 0,
2489 indent => 0, 2577 indent => 0,
2490 #font => default_font 2578 #font => default_font
2491 @_, 2579 @_,
2492 2580
2493 layout => (new CFClient::Layout 1), 2581 layout => (new CFClient::Layout),
2494 par => [], 2582 par => [],
2495 height => 0, 2583 height => 0,
2496 children => [ 2584 children => [
2497 (new CFClient::UI::Empty expand => 1), 2585 (new CFClient::UI::Empty expand => 1),
2498 (new CFClient::UI::Slider vertical => 1), 2586 (new CFClient::UI::Slider vertical => 1),
2509 2597
2510 $self->{fontsize} = $fontsize; 2598 $self->{fontsize} = $fontsize;
2511 $self->reflow; 2599 $self->reflow;
2512} 2600}
2513 2601
2602sub size_request {
2603 my ($self) = @_;
2604
2605 my ($empty, $slider) = @{ $self->{children} };
2606
2607 local $self->{children} = [$empty, $slider];
2608 $self->SUPER::size_request
2609}
2610
2514sub size_allocate { 2611sub invoke_size_allocate {
2515 my ($self, $w, $h) = @_; 2612 my ($self, $w, $h) = @_;
2516 2613
2517 $self->SUPER::size_allocate ($w, $h); 2614 my ($empty, $slider, @other) = @{ $self->{children} };
2615 $_->configure (@$_{qw(x y req_w req_h)}) for @other;
2518 2616
2519 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2617 $self->{layout}->set_font ($self->{font}) if $self->{font};
2520 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2618 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2521 $self->{layout}->set_width ($self->{children}[0]{w}); 2619 $self->{layout}->set_width ($empty->{w});
2522 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 2620 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2523 2621
2524 $self->reflow; 2622 $self->reflow;
2525}
2526 2623
2527sub text_size { 2624 local $self->{children} = [$empty, $slider];
2528 my ($self, $text, $indent) = @_; 2625 $self->SUPER::invoke_size_allocate ($w, $h)
2626}
2627
2628sub get_layout {
2629 my ($self, $para) = @_;
2529 2630
2530 my $layout = $self->{layout}; 2631 my $layout = $self->{layout};
2531 2632
2633 $layout->set_font ($self->{font}) if $self->{font};
2634 $layout->set_foreground (@{$para->{fg}});
2532 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2635 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2533 $layout->set_width ($self->{children}[0]{w} - $indent); 2636 $layout->set_width ($self->{children}[0]{w} - $para->{indent});
2534 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 2637 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2535 $layout->set_markup ($text); 2638 $layout->set_markup ($para->{markup});
2639
2640 $layout->set_shapes (
2641 map
2642 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2643 @{$para->{widget}}
2536 2644 );
2645
2537 $layout->size 2646 $layout
2538} 2647}
2539 2648
2540sub reflow { 2649sub reflow {
2541 my ($self) = @_; 2650 my ($self) = @_;
2542 2651
2551 $self->{children}[1]->set_value ($offset); 2660 $self->{children}[1]->set_value ($offset);
2552} 2661}
2553 2662
2554sub clear { 2663sub clear {
2555 my ($self) = @_; 2664 my ($self) = @_;
2665
2666 my (undef, undef, @other) = @{ $self->{children} };
2667 $self->remove ($_) for @other;
2556 2668
2557 $self->{par} = []; 2669 $self->{par} = [];
2558 $self->{height} = 0; 2670 $self->{height} = 0;
2559 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 2671 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2560} 2672}
2561 2673
2562sub add_paragraph { 2674sub add_paragraph {
2563 my ($self, $color, $text, $indent) = @_; 2675 my ($self, $color, $para, $indent) = @_;
2564 2676
2565 for my $line (split /\n/, $text) { 2677 my ($text, @w) = ref $para ? @$para : $para;
2566 my ($w, $h) = $self->text_size ($line); 2678
2567 $self->{height} += $h; 2679 $para = {
2568 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; 2680 w => 1e10,
2681 wrapped => 1,
2682 fg => $color,
2683 indent => $indent,
2684 markup => $text,
2685 widget => \@w,
2569 } 2686 };
2570 2687
2571 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); 2688 $self->add (@w) if @w;
2689 push @{$self->{par}}, $para;
2690
2691 $self->{need_reflow}++;
2692 $self->update;
2693}
2694
2695sub scroll_to_bottom {
2696 my ($self) = @_;
2697
2698 $self->{scroll_to_bottom} = 1;
2699 $self->update;
2572} 2700}
2573 2701
2574sub update { 2702sub update {
2575 my ($self) = @_; 2703 my ($self) = @_;
2576 2704
2584 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2712 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2585 2713
2586 if (delete $self->{need_reflow}) { 2714 if (delete $self->{need_reflow}) {
2587 my $height = 0; 2715 my $height = 0;
2588 2716
2589 my $layout = $self->{layout};
2590
2591 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2592
2593 for (@{$self->{par}}) { 2717 for my $para (@{$self->{par}}) {
2594 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2718 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2595 $layout->set_width ($W - $_->[3]); 2719 my $layout = $self->get_layout ($para);
2596 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2597 $layout->set_markup ($_->[4]);
2598 my ($w, $h) = $layout->size; 2720 my ($w, $h) = $layout->size;
2599 $_->[0] = $w + $_->[3]; 2721
2600 $_->[1] = $h; 2722 $para->{w} = $w + $para->{indent};
2723 $para->{h} = $h;
2724 $para->{wrapped} = $layout->has_wrapped;
2601 } 2725 }
2602 2726
2603 $height += $_->[1]; 2727 $height += $para->{h};
2604 } 2728 }
2605 2729
2606 $self->{height} = $height; 2730 $self->{height} = $height;
2607 2731
2608 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2732 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2609 2733
2610 delete $self->{texture}; 2734 delete $self->{texture};
2735 }
2736
2737 if (delete $self->{scroll_to_bottom}) {
2738 $self->{children}[1]->set_value (1e10);
2611 } 2739 }
2612 2740
2613 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2741 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2614 glClearColor 0, 0, 0, 0; 2742 glClearColor 0, 0, 0, 0;
2615 glClear GL_COLOR_BUFFER_BIT; 2743 glClear GL_COLOR_BUFFER_BIT;
2619 my $y0 = $top; 2747 my $y0 = $top;
2620 my $y1 = $top + $H; 2748 my $y1 = $top + $H;
2621 2749
2622 my $y = 0; 2750 my $y = 0;
2623 2751
2624 my $layout = $self->{layout};
2625
2626 $layout->set_font ($self->{font}) if $self->{font};
2627
2628 glEnable GL_BLEND;
2629 #TODO# not correct in windows where rgba is forced off
2630 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2631
2632 for my $par (@{$self->{par}}) { 2752 for my $para (@{$self->{par}}) {
2633 my $h = $par->[1]; 2753 my $h = $para->{h};
2634 2754
2635 if ($y0 < $y + $h && $y < $y1) { 2755 if ($y0 < $y + $h && $y < $y1) {
2636 $layout->set_foreground (@{ $par->[2] });
2637 $layout->set_width ($W - $par->[3]);
2638 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2639 $layout->set_markup ($par->[4]);
2640 2756
2641 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2757 my $layout = $self->get_layout ($para);
2642 2758
2643 glRasterPos $par->[3], $y - $y0; 2759 $layout->render ($para->{indent}, $y - $y0);
2644 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2760
2761 if (my @w = @{ $para->{widget} }) {
2762 my @s = $layout->get_shapes;
2763
2764 for (@w) {
2765 my ($dx, $dy) = splice @s, 0, 2, ();
2766
2767 $_->{x} = $dx + $para->{indent};
2768 $_->{y} = $dy + $y - $y0;
2769
2770 $_->draw;
2771 }
2772 }
2645 } 2773 }
2646 2774
2647 $y += $h; 2775 $y += $h;
2648 } 2776 }
2649
2650 glDisable GL_BLEND;
2651 }; 2777 };
2652 }); 2778 });
2779}
2780
2781sub reconfigure {
2782 my ($self) = @_;
2783
2784 $self->SUPER::reconfigure;
2785
2786 $_->{w} = 1e10 for @{ $self->{par} };
2787 $self->reflow;
2653} 2788}
2654 2789
2655sub _draw { 2790sub _draw {
2656 my ($self) = @_; 2791 my ($self) = @_;
2657 2792
2660 glColor 0, 0, 0, 1; 2795 glColor 0, 0, 0, 1;
2661 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2796 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2662 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2663 2798
2664 $self->{children}[1]->draw; 2799 $self->{children}[1]->draw;
2665
2666} 2800}
2667 2801
2668############################################################################# 2802#############################################################################
2669 2803
2670package CFClient::UI::Animator; 2804package CFClient::UI::Animator;
2779 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2913 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2780 2914
2781 ($w + 4, $h + 4) 2915 ($w + 4, $h + 4)
2782} 2916}
2783 2917
2784sub size_allocate { 2918sub invoke_size_allocate {
2785 my ($self, $w, $h) = @_; 2919 my ($self, $w, $h) = @_;
2786 2920
2787 $self->SUPER::size_allocate ($w - 4, $h - 4); 2921 $self->SUPER::invoke_size_allocate ($w - 4, $h - 4)
2788} 2922}
2789 2923
2790sub visibility_change { 2924sub invoke_visibility_change {
2791 my ($self, $visible) = @_; 2925 my ($self, $visible) = @_;
2792 2926
2793 return unless $visible; 2927 return unless $visible;
2794 2928
2795 $self->{root}->on_post_alloc ("move_$self" => sub { 2929 $self->{root}->on_post_alloc ("move_$self" => sub {
2943 for my $item (@{ $self->{items} }) { 3077 for my $item (@{ $self->{items} }) {
2944 my ($widget, $cb, $tooltip) = @$item; 3078 my ($widget, $cb, $tooltip) = @$item;
2945 3079
2946 # handle various types of items, only text for now 3080 # handle various types of items, only text for now
2947 if (!ref $widget) { 3081 if (!ref $widget) {
3082 if ($widget =~ /\t/) {
3083 my ($left, $right) = split /\t/, $widget, 2;
3084
3085 $widget = new CFClient::UI::HBox
3086 can_hover => 1,
3087 can_events => 1,
3088 tooltip => $tooltip,
3089 children => [
3090 (new CFClient::UI::Label markup => $left, expand => 1),
3091 (new CFClient::UI::Label markup => $right, align => +1),
3092 ],
3093 ;
3094
3095 } else {
2948 $widget = new CFClient::UI::Label 3096 $widget = new CFClient::UI::Label
2949 can_hover => 1, 3097 can_hover => 1,
2950 can_events => 1, 3098 can_events => 1,
2951 markup => $widget, 3099 markup => $widget,
2952 tooltip => $tooltip 3100 tooltip => $tooltip;
3101 }
2953 } 3102 }
2954 3103
2955 $self->{item}{$widget} = $item; 3104 $self->{item}{$widget} = $item;
2956 3105
2957 $self->{vbox}->add ($widget); 3106 $self->{vbox}->add ($widget);
2962 3111
2963# popup given the event (must be a mouse button down event currently) 3112# popup given the event (must be a mouse button down event currently)
2964sub popup { 3113sub popup {
2965 my ($self, $ev) = @_; 3114 my ($self, $ev) = @_;
2966 3115
2967 $self->_emit ("popdown"); 3116 $self->emit ("popdown");
2968 3117
2969 # maybe save $GRAB? must be careful about events... 3118 # maybe save $GRAB? must be careful about events...
2970 $GRAB = $self; 3119 $GRAB = $self;
2971 $self->{button} = $ev->{button}; 3120 $self->{button} = $ev->{button};
2972 3121
2973 $self->show; 3122 $self->show;
2974 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3123 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2975} 3124}
2976 3125
2977sub mouse_motion { 3126sub invoke_mouse_motion {
2978 my ($self, $ev, $x, $y) = @_; 3127 my ($self, $ev, $x, $y) = @_;
2979 3128
2980 # TODO: should use vbox->find_widget or so 3129 # TODO: should use vbox->find_widget or so
2981 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 3130 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2982 $self->{hover} = $self->{item}{$HOVER}; 3131 $self->{hover} = $self->{item}{$HOVER};
2983 3132
2984 0 3133 0
2985} 3134}
2986 3135
2987sub button_up { 3136sub invoke_button_up {
2988 my ($self, $ev, $x, $y) = @_; 3137 my ($self, $ev, $x, $y) = @_;
2989 3138
2990 if ($ev->{button} == $self->{button}) { 3139 if ($ev->{button} == $self->{button}) {
2991 undef $GRAB; 3140 undef $GRAB;
2992 $self->hide; 3141 $self->hide;
2993 3142
2994 $self->_emit ("popdown"); 3143 $self->emit ("popdown");
2995 $self->{hover}[1]->() if $self->{hover}; 3144 $self->{hover}[1]->() if $self->{hover};
2996 } else { 3145 } else {
2997 return 0 3146 return 0
2998 } 3147 }
2999 3148
3026 3175
3027 $self->{current} = $self->{children}[0] 3176 $self->{current} = $self->{children}[0]
3028 if @{ $self->{children} }; 3177 if @{ $self->{children} };
3029} 3178}
3030 3179
3180sub get_current_page {
3181 my ($self) = @_;
3182
3183 $self->{current}
3184}
3185
3031sub set_current_page { 3186sub set_current_page {
3032 my ($self, $page_or_widget) = @_; 3187 my ($self, $page_or_widget) = @_;
3033 3188
3034 my $widget = ref $page_or_widget 3189 my $widget = ref $page_or_widget
3035 ? $page_or_widget 3190 ? $page_or_widget
3036 : $self->{children}[$page_or_widget]; 3191 : $self->{children}[$page_or_widget];
3037 3192
3038 $self->{current} = $widget; 3193 $self->{current} = $widget;
3039 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3194 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3040 3195
3041 $self->_emit (page_changed => $self->{current}); 3196 $self->emit (page_changed => $self->{current});
3042 3197
3043 $self->realloc; 3198 $self->realloc;
3044} 3199}
3045 3200
3046sub visible_children { 3201sub visible_children {
3051 my ($self) = @_; 3206 my ($self) = @_;
3052 3207
3053 $self->{current}->size_request 3208 $self->{current}->size_request
3054} 3209}
3055 3210
3056sub size_allocate { 3211sub invoke_size_allocate {
3057 my ($self, $w, $h) = @_; 3212 my ($self, $w, $h) = @_;
3058 3213
3059 $self->{current}->configure (0, 0, $w, $h); 3214 $self->{current}->configure (0, 0, $w, $h);
3215
3216 1
3060} 3217}
3061 3218
3062sub _draw { 3219sub _draw {
3063 my ($self) = @_; 3220 my ($self) = @_;
3064 3221
3099 ); 3256 );
3100 3257
3101 $self->{multiplexer}->add ($widget); 3258 $self->{multiplexer}->add ($widget);
3102} 3259}
3103 3260
3261sub get_current_page {
3262 my ($self) = @_;
3263
3264 $self->{multiplexer}->get_current_page
3265}
3266
3104sub set_current_page { 3267sub set_current_page {
3105 my ($self, $page) = @_; 3268 my ($self, $page) = @_;
3106 3269
3107 $self->{multiplexer}->set_current_page ($page); 3270 $self->{multiplexer}->set_current_page ($page);
3108 $self->_emit (page_changed => $self->{multiplexer}{current}); 3271 $self->emit (page_changed => $self->{multiplexer}{current});
3109} 3272}
3110 3273
3111############################################################################# 3274#############################################################################
3112 3275
3113package CFClient::UI::Combobox; 3276package CFClient::UI::Combobox;
3128 $self->_set_value ($self->{value}); 3291 $self->_set_value ($self->{value});
3129 3292
3130 $self 3293 $self
3131} 3294}
3132 3295
3133sub button_down { 3296sub invoke_button_down {
3134 my ($self, $ev) = @_; 3297 my ($self, $ev) = @_;
3135 3298
3136 my @menu_items; 3299 my @menu_items;
3137 3300
3138 for (@{ $self->{options} }) { 3301 for (@{ $self->{options} }) {
3159 my ($self, $value) = @_; 3322 my ($self, $value) = @_;
3160 3323
3161 return unless $self->{value} ne $value; 3324 return unless $self->{value} ne $value;
3162 3325
3163 $self->_set_value ($value); 3326 $self->_set_value ($value);
3164 $self->_emit (changed => $value); 3327 $self->emit (changed => $value);
3165} 3328}
3166 3329
3167############################################################################# 3330#############################################################################
3168 3331
3169package CFClient::UI::Statusbox; 3332package CFClient::UI::Statusbox;
3284 count => 1, 3447 count => 1,
3285 %arg, 3448 %arg,
3286 }; 3449 };
3287 } 3450 }
3288 3451
3452 $ROOT->on_refresh (reorder => sub {
3289 $self->reorder; 3453 $self->reorder;
3454 });
3290} 3455}
3291 3456
3292sub reconfigure { 3457sub reconfigure {
3293 my ($self) = @_; 3458 my ($self) = @_;
3294 3459
3309 3474
3310############################################################################# 3475#############################################################################
3311 3476
3312package CFClient::UI::Inventory; 3477package CFClient::UI::Inventory;
3313 3478
3314our @ISA = CFClient::UI::ScrolledWindow::; 3479our @ISA = CFClient::UI::Table::;
3315 3480
3316sub new { 3481sub new {
3317 my $class = shift; 3482 my $class = shift;
3318 3483
3319 my $self = $class->SUPER::new ( 3484 my $self = $class->SUPER::new (
3320 child => (new CFClient::UI::Table col_expand => [0, 1, 0]), 3485 col_expand => [0, 1, 0],
3321 @_, 3486 @_,
3322 ); 3487 );
3323 3488
3324 $self 3489 $self
3325} 3490}
3326 3491
3327sub set_items { 3492sub set_items {
3328 my ($self, $items) = @_; 3493 my ($self, $items) = @_;
3329 3494
3330 $self->{child}->clear; 3495 $self->clear;
3331 return unless $items; 3496 return unless $items;
3332 3497
3333 my @items = sort { 3498 my @items = sort {
3334 ($a->{type} <=> $b->{type}) 3499 ($a->{type} <=> $b->{type})
3335 or ($a->{name} cmp $b->{name}) 3500 or ($a->{name} cmp $b->{name})
3336 } @$items; 3501 } values %$items;
3337 3502
3338 $self->{real_items} = \@items; 3503 $self->{real_items} = \@items;
3339 3504
3340 my $row = 0; 3505 my $row = 0;
3341 for my $item (@items) { 3506 for my $item (@items) {
3342 CFClient::Item::update_widgets $item; 3507 CFClient::Item::update_widgets $item;
3343 3508
3344 $self->{child}->add (0, $row, $item->{face_widget}); 3509 $self->add (0, $row, $item->{face_widget});
3345 $self->{child}->add (1, $row, $item->{desc_widget}); 3510 $self->add (1, $row, $item->{desc_widget});
3346 $self->{child}->add (2, $row, $item->{weight_widget}); 3511 $self->add (2, $row, $item->{weight_widget});
3347 3512
3348 $row++; 3513 $row++;
3349 }
3350}
3351
3352#############################################################################
3353
3354package CFClient::UI::BindEditor;
3355
3356our @ISA = CFClient::UI::FancyFrame::;
3357
3358sub new {
3359 my $class = shift;
3360
3361 my $self = $class->SUPER::new (binding => [], commands => [], @_);
3362
3363 $self->add (my $vb = new CFClient::UI::VBox);
3364
3365
3366 $vb->add ($self->{rec_btn} = new CFClient::UI::Button
3367 text => "start recording",
3368 tooltip => "Start/Stops recording of actions."
3369 ."All subsequent actions after the recording started will be captured."
3370 ."The actions are displayed after the record was stopped."
3371 ."To bind the action you have to click on the 'Bind' button",
3372 on_activate => sub {
3373 unless ($self->{recording}) {
3374 $self->start;
3375 } else {
3376 $self->stop;
3377 }
3378 });
3379
3380 $vb->add (new CFClient::UI::Label text => "Actions:");
3381 $vb->add ($self->{cmdbox} = new CFClient::UI::VBox);
3382
3383 $vb->add (new CFClient::UI::Label text => "Bound to: ");
3384 $vb->add (my $hb = new CFClient::UI::HBox);
3385 $hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1);
3386 $hb->add (new CFClient::UI::Button
3387 text => "bind",
3388 tooltip => "This opens a query where you have to press the key combination to bind the recorded actions",
3389 on_activate => sub {
3390 $self->ask_for_bind;
3391 });
3392
3393 $vb->add (my $hb = new CFClient::UI::HBox);
3394 $hb->add (new CFClient::UI::Button
3395 text => "ok",
3396 expand => 1,
3397 tooltip => "This closes the binding editor and saves the binding",
3398 on_activate => sub {
3399 $self->hide;
3400 $self->commit;
3401 });
3402
3403 $hb->add (new CFClient::UI::Button
3404 text => "cancel",
3405 expand => 1,
3406 tooltip => "This closes the binding editor without saving",
3407 on_activate => sub {
3408 $self->hide;
3409 $self->{binding_cancel}->()
3410 if $self->{binding_cancel};
3411 });
3412
3413 $self->update_binding_widgets;
3414
3415 $self
3416}
3417
3418sub commit {
3419 my ($self) = @_;
3420 my ($mod, $sym, $cmds) = $self->get_binding;
3421 if ($sym != 0 && @$cmds > 0) {
3422 $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym)
3423 ."'. Don't forget 'Save Config'!");
3424 $self->{binding_change}->($mod, $sym, $cmds)
3425 if $self->{binding_change};
3426 } else {
3427 $::STATUSBOX->add ("No action bound, no key or action specified!");
3428 $self->{binding_cancel}->()
3429 if $self->{binding_cancel};
3430 }
3431}
3432
3433sub start {
3434 my ($self) = @_;
3435
3436 $self->{rec_btn}->set_text ("stop recording");
3437 $self->{recording} = 1;
3438 $self->clear_command_list;
3439 $::CONN->start_record if $::CONN;
3440}
3441
3442sub stop {
3443 my ($self) = @_;
3444
3445 $self->{rec_btn}->set_text ("start recording");
3446 $self->{recording} = 0;
3447
3448 my $rec;
3449 $rec = $::CONN->stop_record if $::CONN;
3450 return unless ref $rec eq 'ARRAY';
3451 $self->set_command_list ($rec);
3452}
3453
3454
3455sub ask_for_bind_and_commit {
3456 my ($self) = @_;
3457 $self->ask_for_bind (1);
3458}
3459
3460sub ask_for_bind {
3461 my ($self, $commit) = @_;
3462
3463 CFClient::Binder::open_binding_dialog (sub {
3464 my ($mod, $sym) = @_;
3465 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak?
3466 $self->update_binding_widgets;
3467 $self->commit if $commit;
3468 });
3469}
3470
3471# $mod and $sym are the modifiers and key symbol
3472# $cmds is a array ref of strings (the commands)
3473# $cb is the callback that is executed on OK
3474# $ccb is the callback that is executed on CANCEL and
3475# when the binding was unsuccessful on OK
3476sub set_binding {
3477 my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_;
3478
3479 $self->clear_command_list;
3480 $self->{recording} = 0;
3481 $self->{rec_btn}->set_text ("start recording");
3482
3483 $self->{binding} = [$mod, $sym];
3484 $self->{commands} = $cmds;
3485
3486 $self->{binding_change} = $cb;
3487 $self->{binding_cancel} = $ccb;
3488
3489 $self->update_binding_widgets;
3490}
3491
3492# this is a shortcut method that asks for a binding
3493# and then just binds it.
3494sub do_quick_binding {
3495 my ($self, $cmds) = @_;
3496 $self->set_binding (undef, undef, $cmds, sub {
3497 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2];
3498 });
3499 $self->ask_for_bind (1);
3500}
3501
3502sub update_binding_widgets {
3503 my ($self) = @_;
3504 my ($mod, $sym, $cmds) = $self->get_binding;
3505 $self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym));
3506 $self->set_command_list ($cmds);
3507}
3508
3509sub get_binding {
3510 my ($self) = @_;
3511 return (
3512 $self->{binding}->[0],
3513 $self->{binding}->[1],
3514 [ grep { defined $_ } @{$self->{commands}} ]
3515 );
3516}
3517
3518sub clear_command_list {
3519 my ($self) = @_;
3520 $self->{cmdbox}->clear ();
3521}
3522
3523sub set_command_list {
3524 my ($self, $cmds) = @_;
3525
3526 $self->{cmdbox}->clear ();
3527 $self->{commands} = $cmds;
3528
3529 my $idx = 0;
3530
3531 for (@$cmds) {
3532 $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox);
3533
3534 my $i = $idx;
3535 $hb->add (new CFClient::UI::Label text => $_);
3536 $hb->add (new CFClient::UI::Button
3537 text => "delete",
3538 tooltip => "Deletes the action from the record",
3539 on_activate => sub {
3540 $self->{cmdbox}->remove ($hb);
3541 $cmds->[$i] = undef;
3542 });
3543
3544
3545 $idx++
3546 } 3514 }
3547} 3515}
3548 3516
3549############################################################################# 3517#############################################################################
3550 3518
3604 } elsif ($ev->{button} == 2) { 3572 } elsif ($ev->{button} == 2) {
3605 $::CONN->user_send ("invoke $spell->{name}"); 3573 $::CONN->user_send ("invoke $spell->{name}");
3606 } elsif ($ev->{button} == 3) { 3574 } elsif ($ev->{button} == 3) {
3607 (new CFClient::UI::Menu 3575 (new CFClient::UI::Menu
3608 items => [ 3576 items => [
3609 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], 3577 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3610 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], 3578 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3611 ], 3579 ],
3612 )->popup ($ev); 3580 )->popup ($ev);
3613 } else { 3581 } else {
3614 return 0; 3582 return 0;
3709 $coord = $max - $size if $coord > $max - $size; 3677 $coord = $max - $size if $coord > $max - $size;
3710 3678
3711 int $coord + 0.5 3679 int $coord + 0.5
3712} 3680}
3713 3681
3714sub size_allocate { 3682sub invoke_size_allocate {
3715 my ($self, $w, $h) = @_; 3683 my ($self, $w, $h) = @_;
3716 3684
3717 for my $child ($self->children) { 3685 for my $child ($self->children) {
3718 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3686 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3719 3687
3723 $X = _to_pixel $X, $W, $self->{w}; 3691 $X = _to_pixel $X, $W, $self->{w};
3724 $Y = _to_pixel $Y, $H, $self->{h}; 3692 $Y = _to_pixel $Y, $H, $self->{h};
3725 3693
3726 $child->configure ($X, $Y, $W, $H); 3694 $child->configure ($X, $Y, $W, $H);
3727 } 3695 }
3696
3697 1
3728} 3698}
3729 3699
3730sub coord2local { 3700sub coord2local {
3731 my ($self, $x, $y) = @_; 3701 my ($self, $x, $y) = @_;
3732 3702
3858 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 3828 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3859 3829
3860 $w = 0 if $w < 0; 3830 $w = 0 if $w < 0;
3861 $h = 0 if $h < 0; 3831 $h = 0 if $h < 0;
3862 3832
3833 $w = max $widget->{min_w}, $w;
3834 $h = max $widget->{min_h}, $h;
3835
3836 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3837 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3838
3863 $w = int $w + 0.5; 3839 $w = int $w + 0.5;
3864 $h = int $h + 0.5; 3840 $h = int $h + 0.5;
3865 3841
3866 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 3842 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3867 $widget->{old_w} = $widget->{w}; 3843 $widget->{old_w} = $widget->{w};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines