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.278 by root, Sun Jun 4 23:05:06 2006 UTC vs.
Revision 1.308 by root, Mon Jun 19 05:18:14 2006 UTC

3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8use Event;
8 9
9use CFClient; 10use CFClient;
10use CFClient::Texture; 11use CFClient::Texture;
11 12
12our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
15our $ROOT; 16our $ROOT;
16our $TOOLTIP; 17our $TOOLTIP;
17our $BUTTON_STATE; 18our $BUTTON_STATE;
18 19
19our %WIDGET; # all widgets, weak-referenced 20our %WIDGET; # all widgets, weak-referenced
21
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
23 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->hide;
28
29 $TOOLTIP->{owner} = $widget;
30
31 return if $ENV{CFPLUS_DEBUG} & 8;
32
33 my $tip = $widget->{tooltip};
34
35 $tip = $tip->($widget) if CODE:: eq ref $tip;
36
37 $TOOLTIP->set_tooltip_from ($widget);
38 $TOOLTIP->show;
39 }
40
41 return;
42 }
43 }
44 }
45
46 $TOOLTIP->hide;
47 delete $TOOLTIP->{owner};
48});
20 49
21sub get_layout { 50sub get_layout {
22 my $layout; 51 my $layout;
23 52
24 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
39 my ($layout) = @_; 68 my ($layout) = @_;
40 69
41 $LAYOUT = $layout; 70 $LAYOUT = $layout;
42} 71}
43 72
44sub check_tooltip {
45 return if $ENV{CFPLUS_DEBUG} & 8;
46
47 if (!$GRAB) {
48 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
49 if (length $widget->{tooltip}) {
50 if ($TOOLTIP->{owner} != $widget) {
51 $TOOLTIP->hide;
52
53 $TOOLTIP->{owner} = $widget;
54
55 my $tip = $widget->{tooltip};
56
57 $tip = $tip->($widget) if CODE:: eq ref $tip;
58
59 $TOOLTIP->set_tooltip_from ($widget);
60 $TOOLTIP->show;
61 }
62
63 return;
64 }
65 }
66 }
67
68 $TOOLTIP->hide;
69 delete $TOOLTIP->{owner};
70}
71
72# class methods for events 73# class methods for events
73sub feed_sdl_key_down_event { 74sub feed_sdl_key_down_event {
74 $FOCUS->emit (key_down => $_[0]) 75 $FOCUS->emit (key_down => $_[0])
75 if $FOCUS; 76 if $FOCUS;
76} 77}
88 my $widget = $ROOT->find_widget ($x, $y); 89 my $widget = $ROOT->find_widget ($x, $y);
89 90
90 $GRAB = $widget; 91 $GRAB = $widget;
91 $GRAB->update if $GRAB; 92 $GRAB->update if $GRAB;
92 93
93 check_tooltip; 94 $TOOLTIP_WATCHER->cb->();
94 } 95 }
95 96
96 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 97 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
97 98
98 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) 99 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
113 if (!$BUTTON_STATE) { 114 if (!$BUTTON_STATE) {
114 my $grab = $GRAB; undef $GRAB; 115 my $grab = $GRAB; undef $GRAB;
115 $grab->update if $grab; 116 $grab->update if $grab;
116 $GRAB->update if $GRAB; 117 $GRAB->update if $GRAB;
117 118
118 check_tooltip; 119 $TOOLTIP_WATCHER->cb->();
119 } 120 }
120} 121}
121 122
122sub feed_sdl_motion_event { 123sub feed_sdl_motion_event {
123 my ($ev) = @_; 124 my ($ev) = @_;
129 my $hover = $HOVER; $HOVER = $widget; 130 my $hover = $HOVER; $HOVER = $widget;
130 131
131 $hover->update if $hover && $hover->{can_hover}; 132 $hover->update if $hover && $hover->{can_hover};
132 $HOVER->update if $HOVER && $HOVER->{can_hover}; 133 $HOVER->update if $HOVER && $HOVER->{can_hover};
133 134
134 check_tooltip; 135 $TOOLTIP_WATCHER->start;
135 } 136 }
136 137
137 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) 138 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
138 if $HOVER; 139 if $HOVER;
139} 140}
271 272
272 return unless $self->{visible}; 273 return unless $self->{visible};
273 274
274 $_->set_invisible for $self->children; 275 $_->set_invisible for $self->children;
275 276
277 delete $self->{visible};
276 delete $self->{root}; 278 delete $self->{root};
277 delete $self->{visible};
278 279
279 undef $GRAB if $GRAB == $self; 280 undef $GRAB if $GRAB == $self;
280 undef $HOVER if $HOVER == $self; 281 undef $HOVER if $HOVER == $self;
281 282
282 CFClient::UI::check_tooltip 283 $CFClient::UI::TOOLTIP_WATCHER->cb->()
283 if $TOOLTIP->{owner} == $self; 284 if $TOOLTIP->{owner} == $self;
284 285
285 $self->focus_out; 286 $self->emit ("focus_out");
286
287 $self->emit (visibility_change => 0); 287 $self->emit (visibility_change => 0);
288} 288}
289 289
290sub set_visibility { 290sub set_visibility {
291 my ($self, $visible) = @_; 291 my ($self, $visible) = @_;
314} 314}
315 315
316sub move_abs { 316sub move_abs {
317 my ($self, $x, $y, $z) = @_; 317 my ($self, $x, $y, $z) = @_;
318 318
319 $self->{x} = List::Util::max 0, int $x; 319 $self->{x} = List::Util::max 0, List::Util::min $self->{root}{w} - $self->{w}, int $x;
320 $self->{y} = List::Util::max 0, int $y; 320 $self->{y} = List::Util::max 0, List::Util::min $self->{root}{h} - $self->{h}, int $y;
321 $self->{z} = $z if defined $z; 321 $self->{z} = $z if defined $z;
322 322
323 $self->update; 323 $self->update;
324} 324}
325 325
366 366
367 $self->{root}{size_alloc}{$self+0} = $self; 367 $self->{root}{size_alloc}{$self+0} = $self;
368 } 368 }
369} 369}
370 370
371sub size_allocate {
372 # nothing to be done
373}
374
375sub children { 371sub children {
376 # nop 372 # nop
377} 373}
378 374
379sub visible_children { 375sub visible_children {
381} 377}
382 378
383sub set_max_size { 379sub set_max_size {
384 my ($self, $w, $h) = @_; 380 my ($self, $w, $h) = @_;
385 381
386 delete $self->{max_w}; $self->{max_w} = $w if $w; 382 $self->{max_w} = int $w if defined $w;
387 delete $self->{max_h}; $self->{max_h} = $h if $h; 383 $self->{max_h} = int $h if defined $h;
384
385 $self->realloc;
388} 386}
389 387
390sub set_tooltip { 388sub set_tooltip {
391 my ($self, $tooltip) = @_; 389 my ($self, $tooltip) = @_;
392 390
397 395
398 $self->{tooltip} = $tooltip; 396 $self->{tooltip} = $tooltip;
399 397
400 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 398 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
401 delete $CFClient::UI::TOOLTIP->{owner}; 399 delete $CFClient::UI::TOOLTIP->{owner};
402 CFClient::UI::check_tooltip; 400 $CFClient::UI::TOOLTIP_WATCHER->cb->();
403 } 401 }
404} 402}
405 403
406# translate global coordinates to local coordinate system 404# translate global coordinates to local coordinate system
407sub coord2local { 405sub coord2local {
415 my ($self, $x, $y) = @_; 413 my ($self, $x, $y) = @_;
416 414
417 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 415 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
418} 416}
419 417
420sub focus_in { 418sub invoke_focus_in {
421 my ($self) = @_; 419 my ($self) = @_;
422 420
423 return if $FOCUS == $self; 421 return if $FOCUS == $self;
424 return unless $self->{can_focus}; 422 return unless $self->{can_focus};
425 423
426 my $focus = $FOCUS; $FOCUS = $self; 424 my $focus = $FOCUS; $FOCUS = $self;
427 425
428 $self->_emit (focus_in => $focus);
429
430 $focus->update if $focus; 426 $focus->update if $focus;
431 $FOCUS->update; 427 $FOCUS->update;
432}
433 428
429 0
430}
431
434sub focus_out { 432sub invoke_focus_out {
435 my ($self) = @_; 433 my ($self) = @_;
436 434
437 return unless $FOCUS == $self; 435 return unless $FOCUS == $self;
438 436
439 my $focus = $FOCUS; undef $FOCUS; 437 my $focus = $FOCUS; undef $FOCUS;
440 438
441 $self->_emit (focus_out => $focus);
442
443 $focus->update if $focus; #? 439 $focus->update if $focus; #?
444 440
445 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 441 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
446 unless $FOCUS; 442 unless $FOCUS;
447}
448 443
444 0
445}
446
447sub grab_focus {
448 my ($self) = @_;
449
450 $self->emit ("focus_in");
451}
452
449sub mouse_motion { 0 } 453sub invoke_mouse_motion { 1 }
450sub button_up { 0 } 454sub invoke_button_up { 1 }
451sub key_down { 0 } 455sub invoke_key_down { 1 }
452sub key_up { 0 } 456sub invoke_key_up { 1 }
453 457
454sub button_down { 458sub invoke_button_down {
455 my ($self, $ev, $x, $y) = @_; 459 my ($self, $ev, $x, $y) = @_;
456 460
457 $self->focus_in; 461 $self->grab_focus;
458 462
459 0 463 1
464}
465
466sub connect {
467 my ($self, $signal, $cb) = @_;
468
469 push @{ $self->{signal_cb}{$signal} }, $cb;
470}
471
472sub emit {
473 my ($self, $signal, @args) = @_;
474
475 #d##TODO# stop propagating at first true, do not use sum
476 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before
477 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure
478 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
460} 479}
461 480
462sub find_widget { 481sub find_widget {
463 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
464 483
474sub set_parent { 493sub set_parent {
475 my ($self, $parent) = @_; 494 my ($self, $parent) = @_;
476 495
477 Scalar::Util::weaken ($self->{parent} = $parent); 496 Scalar::Util::weaken ($self->{parent} = $parent);
478 $self->set_visible if $parent->{visible}; 497 $self->set_visible if $parent->{visible};
479}
480
481sub connect {
482 my ($self, $signal, $cb) = @_;
483
484 push @{ $self->{signal_cb}{$signal} }, $cb;
485}
486
487sub _emit {
488 my ($self, $signal, @args) = @_;
489
490 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
491}
492
493sub emit {
494 my ($self, $signal, @args) = @_;
495
496 $self->_emit ($signal, @args)
497 || $self->$signal (@args);
498}
499
500sub visibility_change {
501 #my ($self, $visible) = @_;
502} 498}
503 499
504sub realloc { 500sub realloc {
505 my ($self) = @_; 501 my ($self) = @_;
506 502
590 586
591sub DESTROY { 587sub DESTROY {
592 my ($self) = @_; 588 my ($self) = @_;
593 589
594 delete $WIDGET{$self+0}; 590 delete $WIDGET{$self+0};
595 #$self->deactivate; 591
592 eval { $self->destroy };
593 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
596} 594}
597 595
598############################################################################# 596#############################################################################
599 597
600package CFClient::UI::DrawBG; 598package CFClient::UI::DrawBG;
765} 763}
766 764
767sub add { 765sub add {
768 my ($self, $child) = @_; 766 my ($self, $child) = @_;
769 767
770 $self->{children} = []; 768 $self->SUPER::remove ($_) for @{ $self->{children} };
771
772 $self->SUPER::add ($child); 769 $self->SUPER::add ($child);
773} 770}
774 771
775sub remove { 772sub remove {
776 my ($self, $widget) = @_; 773 my ($self, $widget) = @_;
785 782
786sub size_request { 783sub size_request {
787 $_[0]{children}[0]->size_request 784 $_[0]{children}[0]->size_request
788} 785}
789 786
790sub size_allocate { 787sub invoke_size_allocate {
791 my ($self, $w, $h) = @_; 788 my ($self, $w, $h) = @_;
792 789
793 $self->{children}[0]->configure (0, 0, $w, $h); 790 $self->{children}[0]->configure (0, 0, $w, $h);
791
792 1
794} 793}
795 794
796############################################################################# 795#############################################################################
797 796
798# back-buffered drawing area 797# back-buffered drawing area
814 813
815 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 814 $ROOT->on_post_alloc ($self => sub { $self->render_child });
816 $self->SUPER::update; 815 $self->SUPER::update;
817} 816}
818 817
819sub size_allocate { 818sub invoke_size_allocate {
820 my ($self, $w, $h) = @_; 819 my ($self, $w, $h) = @_;
821 820
822 $self->SUPER::size_allocate ($w, $h);
823 $self->update; 821 $self->update;
822
823 $self->SUPER::invoke_size_allocate ($w, $h)
824} 824}
825 825
826sub _render { 826sub _render {
827 my ($self) = @_; 827 my ($self) = @_;
828 828
855 my $tex = $self->{texture} 855 my $tex = $self->{texture}
856 or return; 856 or return;
857 857
858 glEnable GL_TEXTURE_2D; 858 glEnable GL_TEXTURE_2D;
859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
860 glEnable GL_BLEND;
861 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
862 glColor 0, 0, 0, 1; 860 glColor 0, 0, 0, 1;
863 861
864 $tex->draw_quad (0, 0, $w, $h); 862 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
865 863
866 glDisable GL_BLEND;
867 glDisable GL_TEXTURE_2D; 864 glDisable GL_TEXTURE_2D;
868} 865}
869 866
870############################################################################# 867#############################################################################
871 868
892 $h = 10 if $self->{scroll_y}; 889 $h = 10 if $self->{scroll_y};
893 890
894 ($w, $h) 891 ($w, $h)
895} 892}
896 893
897sub size_allocate { 894sub invoke_size_allocate {
898 my ($self, $w, $h) = @_; 895 my ($self, $w, $h) = @_;
899 896
900 my $child = $self->child; 897 my $child = $self->child;
901 898
902 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w}; 899 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w};
903 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h}; 900 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h};
904 901
905 $self->child->configure (0, 0, $w, $h); 902 $self->child->configure (0, 0, $w, $h);
906 $self->update; 903 $self->update;
904
905 1
907} 906}
908 907
909sub set_offset { 908sub set_offset {
910 my ($self, $x, $y) = @_; 909 my ($self, $x, $y) = @_;
911 910
1001 # todo: overwrite size_allocate of child 1000 # todo: overwrite size_allocate of child
1002 my $child = $self->{vp}->child; 1001 my $child = $self->{vp}->child;
1003 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 1002 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1004} 1003}
1005 1004
1006sub size_allocate { 1005sub invoke_size_allocate {
1007 my ($self, $w, $h) = @_; 1006 my ($self, $w, $h) = @_;
1008
1009 $self->SUPER::size_allocate ($w, $h);
1010 1007
1011 my $child = $self->{vp}->child; 1008 my $child = $self->{vp}->child;
1012 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 1009 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1010
1011 $self->SUPER::invoke_size_allocate ($w, $h)
1013} 1012}
1014 1013
1015#TODO# update range on size_allocate depending on child 1014#TODO# update range on size_allocate depending on child
1016# update viewport offset on scroll 1015# update viewport offset on scroll
1017 1016
1071 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1070 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1072 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1071 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1073 1072
1074sub new { 1073sub new {
1075 my ($class, %arg) = @_; 1074 my ($class, %arg) = @_;
1076
1077 my $title = delete $arg{title};
1078 1075
1079 my $self = $class->SUPER::new ( 1076 my $self = $class->SUPER::new (
1080 bg => [1, 1, 1, 1], 1077 bg => [1, 1, 1, 1],
1081 border_bg => [1, 1, 1, 1], 1078 border_bg => [1, 1, 1, 1],
1082 border => 0.6, 1079 border => 0.6,
1084 min_w => 16, 1081 min_w => 16,
1085 min_h => 16, 1082 min_h => 16,
1086 %arg, 1083 %arg,
1087 ); 1084 );
1088 1085
1089 $self->{title} = new CFClient::UI::Label 1086 $self->{title_widget} = new CFClient::UI::Label
1090 align => 0, 1087 align => 0,
1091 valign => 1, 1088 valign => 1,
1092 text => $title, 1089 text => $self->{title},
1093 fontsize => $self->{border} 1090 fontsize => $self->{border},
1094 if defined $title; 1091 if exists $self->{title};
1092
1093 if ($self->{has_close_button}) {
1094 $self->{close_button} =
1095 new CFClient::UI::ImageButton
1096 image => 'x1_close.png',
1097 on_activate => sub { $self->hide };
1098
1099 $self->CFClient::UI::Container::add ($self->{close_button});
1100 }
1095 1101
1096 $self 1102 $self
1097} 1103}
1098 1104
1099sub add { 1105sub add {
1100 my ($self, @widgets) = @_; 1106 my ($self, @widgets) = @_;
1101 1107
1102 $self->SUPER::add (@widgets); 1108 $self->SUPER::add (@widgets);
1109 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button};
1103 $self->CFClient::UI::Container::add ($self->{title}) if $self->{title}; 1110 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1104} 1111}
1105 1112
1106sub border { 1113sub border {
1107 int $_[0]{border} * $::FONTSIZE 1114 int $_[0]{border} * $::FONTSIZE
1108} 1115}
1109 1116
1110sub size_request { 1117sub size_request {
1111 my ($self) = @_; 1118 my ($self) = @_;
1112 1119
1113 $self->{title}->size_request 1120 $self->{title_widget}->size_request
1114 if $self->{title}; 1121 if $self->{title_widget};
1122
1123 $self->{close_button}->size_request
1124 if $self->{close_button};
1115 1125
1116 my ($w, $h) = $self->SUPER::size_request; 1126 my ($w, $h) = $self->SUPER::size_request;
1117 1127
1118 ( 1128 (
1119 $w + $self->border * 2, 1129 $w + $self->border * 2,
1120 $h + $self->border * 2, 1130 $h + $self->border * 2,
1121 ) 1131 )
1122} 1132}
1123 1133
1124sub size_allocate { 1134sub invoke_size_allocate {
1125 my ($self, $w, $h) = @_; 1135 my ($self, $w, $h) = @_;
1126 1136
1127 if ($self->{title}) { 1137 if ($self->{title_widget}) {
1128 $self->{title}{w} = $w; 1138 $self->{title_widget}{w} = $w;
1129 $self->{title}{h} = $h; 1139 $self->{title_widget}{h} = $h;
1130 $self->{title}->size_allocate ($w, $h); 1140 $self->{title_widget}->invoke_size_allocate ($w, $h);
1131 } 1141 }
1132 1142
1133 my $border = $self->border; 1143 my $border = $self->border;
1134 1144
1135 $h -= List::Util::max 0, $border * 2; 1145 $h -= List::Util::max 0, $border * 2;
1136 $w -= List::Util::max 0, $border * 2; 1146 $w -= List::Util::max 0, $border * 2;
1137 1147
1138 $self->child->configure ($border, $border, $w, $h); 1148 $self->child->configure ($border, $border, $w, $h);
1139}
1140 1149
1150 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border)
1151 if $self->{close_button};
1152
1153 1
1154}
1155
1141sub button_down { 1156sub invoke_button_down {
1142 my ($self, $ev, $x, $y) = @_; 1157 my ($self, $ev, $x, $y) = @_;
1143 1158
1144 my ($w, $h) = @$self{qw(w h)}; 1159 my ($w, $h) = @$self{qw(w h)};
1145 my $border = $self->border; 1160 my $border = $self->border;
1146 1161
1177 1192
1178 ($x, $y) = ($ev->{x}, $ev->{y}); 1193 ($x, $y) = ($ev->{x}, $ev->{y});
1179 1194
1180 $self->move_abs ($bx + $x - $ox, $by + $y - $oy); 1195 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1181 # HACK: the next line is required to enforce placement 1196 # HACK: the next line is required to enforce placement
1182 $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h}); 1197 $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h});
1183 }; 1198 };
1184 } else { 1199 } else {
1185 return 0; 1200 return 0;
1186 } 1201 }
1187 1202
1188 1 1203 1
1189} 1204}
1190 1205
1191sub button_up { 1206sub invoke_button_up {
1192 my ($self, $ev, $x, $y) = @_; 1207 my ($self, $ev, $x, $y) = @_;
1193 1208
1194 !!delete $self->{motion} 1209 ! ! delete $self->{motion}
1195} 1210}
1196 1211
1197sub mouse_motion { 1212sub invoke_mouse_motion {
1198 my ($self, $ev, $x, $y) = @_; 1213 my ($self, $ev, $x, $y) = @_;
1199 1214
1200 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1215 $self->{motion}->($ev, $x, $y) if $self->{motion};
1201 1216
1202 !!$self->{motion} 1217 ! ! $self->{motion}
1203} 1218}
1204 1219
1205sub _draw { 1220sub _draw {
1206 my ($self) = @_; 1221 my ($self) = @_;
1207 1222
1233 1248
1234 glDisable GL_TEXTURE_2D; 1249 glDisable GL_TEXTURE_2D;
1235 1250
1236 $child->draw; 1251 $child->draw;
1237 1252
1238 if ($self->{title}) { 1253 if ($self->{title_widget}) {
1239 glTranslate 0, $border - $self->{h}; 1254 glTranslate 0, $border - $self->{h};
1240 $self->{title}->_draw; 1255 $self->{title_widget}->_draw;
1256
1257 glTranslate 0, - ($border - $self->{h});
1241 } 1258 }
1259
1260 $self->{close_button}->draw
1261 if $self->{close_button};
1242} 1262}
1243 1263
1244############################################################################# 1264#############################################################################
1245 1265
1246package CFClient::UI::Table; 1266package CFClient::UI::Table;
1269 1289
1270 $child->set_parent ($self); 1290 $child->set_parent ($self);
1271 $self->{children}[$y][$x] = $child; 1291 $self->{children}[$y][$x] = $child;
1272 1292
1273 $self->realloc; 1293 $self->realloc;
1294}
1295
1296sub remove {
1297 my ($self, $child) = @_;
1298
1299 # TODO: not yet implemented
1274} 1300}
1275 1301
1276# TODO: move to container class maybe? send children a signal on removal? 1302# TODO: move to container class maybe? send children a signal on removal?
1277sub clear { 1303sub clear {
1278 my ($self) = @_; 1304 my ($self) = @_;
1319 (sum @$ws), 1345 (sum @$ws),
1320 (sum @$hs), 1346 (sum @$hs),
1321 ) 1347 )
1322} 1348}
1323 1349
1324sub size_allocate { 1350sub invoke_size_allocate {
1325 my ($self, $w, $h) = @_; 1351 my ($self, $w, $h) = @_;
1326 1352
1327 my ($ws, $hs) = $self->get_wh; 1353 my ($ws, $hs) = $self->get_wh;
1328 1354
1329 my $req_w = (sum @$ws) || 1; 1355 my $req_w = (sum @$ws) || 1;
1361 } 1387 }
1362 1388
1363 $y += $row_h; 1389 $y += $row_h;
1364 } 1390 }
1365 1391
1392 1
1366} 1393}
1367 1394
1368sub find_widget { 1395sub find_widget {
1369 my ($self, $x, $y) = @_; 1396 my ($self, $x, $y) = @_;
1370 1397
1407 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1434 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1408 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1435 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1409 ) 1436 )
1410} 1437}
1411 1438
1412sub size_allocate { 1439sub invoke_size_allocate {
1413 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1414 1441
1415 my $space = $self->{vertical} ? $h : $w; 1442 my $space = $self->{vertical} ? $h : $w;
1416 my $children = $self->{children}; 1443 my $children = $self->{children};
1417 1444
1539 1566
1540 delete $self->{texture}; 1567 delete $self->{texture};
1541 $self->SUPER::update; 1568 $self->SUPER::update;
1542} 1569}
1543 1570
1571sub realloc {
1572 my ($self) = @_;
1573
1574 delete $self->{ox};
1575 $self->SUPER::realloc;
1576}
1577
1544sub set_text { 1578sub set_text {
1545 my ($self, $text) = @_; 1579 my ($self, $text) = @_;
1546 1580
1547 return if $self->{text} eq "T$text"; 1581 return if $self->{text} eq "T$text";
1548 $self->{text} = "T$text"; 1582 $self->{text} = "T$text";
1549 1583
1550 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1584 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1551 $self->{layout}->set_text ($text); 1585 $self->{layout}->set_text ($text);
1552 1586
1587 delete $self->{size_req};
1553 $self->realloc; 1588 $self->realloc;
1554 $self->update; 1589 $self->update;
1555} 1590}
1556 1591
1557sub set_markup { 1592sub set_markup {
1563 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1598 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1564 1599
1565 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1600 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1566 $self->{layout}->set_markup ($markup); 1601 $self->{layout}->set_markup ($markup);
1567 1602
1603 delete $self->{size_req};
1568 $self->realloc; 1604 $self->realloc;
1569 $self->update; 1605 $self->update;
1570} 1606}
1571 1607
1572sub size_request { 1608sub size_request {
1573 my ($self) = @_; 1609 my ($self) = @_;
1574 1610
1611 $self->{size_req} ||= do {
1575 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1612 $self->{layout}->set_font ($self->{font}) if $self->{font};
1576 $self->{layout}->set_width ($self->{max_w} || -1); 1613 $self->{layout}->set_width ($self->{max_w} || -1);
1577 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1614 $self->{layout}->set_ellipsise ($self->{ellipsise});
1578 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1615 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1579 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1616 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1580 1617
1581 my ($w, $h) = $self->{layout}->size; 1618 my ($w, $h) = $self->{layout}->size;
1582 1619
1583 if (exists $self->{template}) { 1620 if (exists $self->{template}) {
1584 $self->{template}->set_font ($self->{font}) if $self->{font}; 1621 $self->{template}->set_font ($self->{font}) if $self->{font};
1585 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1622 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1586 1623
1587 my ($w2, $h2) = $self->{template}->size; 1624 my ($w2, $h2) = $self->{template}->size;
1588 1625
1589 $w = List::Util::max $w, $w2; 1626 $w = List::Util::max $w, $w2;
1590 $h = List::Util::max $h, $h2; 1627 $h = List::Util::max $h, $h2;
1628 }
1629
1630 [$w, $h]
1591 } 1631 };
1592 1632
1593 ($w, $h) 1633 @{ $self->{size_req} }
1594} 1634}
1595 1635
1596sub size_allocate { 1636sub invoke_size_allocate {
1597 my ($self, $w, $h) = @_; 1637 my ($self, $w, $h) = @_;
1598 1638
1599 delete $self->{ox}; 1639 delete $self->{ox};
1600 1640
1601 delete $self->{texture} 1641 delete $self->{texture}
1602 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w}; 1642 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1643
1644 1
1603} 1645}
1604 1646
1605sub set_fontsize { 1647sub set_fontsize {
1606 my ($self, $fontsize) = @_; 1648 my ($self, $fontsize) = @_;
1607 1649
1608 $self->{fontsize} = $fontsize; 1650 $self->{fontsize} = $fontsize;
1609 delete $self->{texture}; 1651 delete $self->{texture};
1610 1652
1611 $self->realloc; 1653 $self->realloc;
1654}
1655
1656sub reconfigure {
1657 my ($self) = @_;
1658
1659 delete $self->{size_req};
1660
1661 $self->SUPER::reconfigure;
1612} 1662}
1613 1663
1614sub _draw { 1664sub _draw {
1615 my ($self) = @_; 1665 my ($self) = @_;
1616 1666
1636 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1686 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1637 : ($self->{h} - $tex->{h}) * 0.5); 1687 : ($self->{h} - $tex->{h}) * 0.5);
1638 }; 1688 };
1639 1689
1640 glEnable GL_TEXTURE_2D; 1690 glEnable GL_TEXTURE_2D;
1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1691
1692 my $w = List::Util::min $self->{w} + 4, $tex->{w};
1693 my $h = List::Util::min $self->{h} + 2, $tex->{h};
1642 1694
1643 if ($tex->{format} == GL_ALPHA) { 1695 if ($tex->{format} == GL_ALPHA) {
1696 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1644 glColor @{$self->{fg}}; 1697 glColor @{$self->{fg}};
1645 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); 1698 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1646 } else { 1699 } else {
1700 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1647 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); 1701 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1648 } 1702 }
1649 1703
1650 glDisable GL_TEXTURE_2D; 1704 glDisable GL_TEXTURE_2D;
1651} 1705}
1652 1706
1669 can_hover => 1, 1723 can_hover => 1,
1670 can_focus => 1, 1724 can_focus => 1,
1671 valign => 0, 1725 valign => 0,
1672 can_events => 1, 1726 can_events => 1,
1673 #text => ... 1727 #text => ...
1728 #hidden => "*",
1674 @_ 1729 @_
1675 ) 1730 )
1676} 1731}
1677 1732
1678sub _set_text { 1733sub _set_text {
1685 $self->{last_activity} = $::NOW; 1740 $self->{last_activity} = $::NOW;
1686 $self->{text} = $text; 1741 $self->{text} = $text;
1687 1742
1688 $text =~ s/./*/g if $self->{hidden}; 1743 $text =~ s/./*/g if $self->{hidden};
1689 $self->{layout}->set_text ("$text "); 1744 $self->{layout}->set_text ("$text ");
1745 delete $self->{size_req};
1690 1746
1691 $self->_emit (changed => $self->{text}); 1747 $self->emit (changed => $self->{text});
1748
1749 $self->realloc;
1692 $self->update; 1750 $self->update;
1693} 1751}
1694 1752
1695sub set_text { 1753sub set_text {
1696 my ($self, $text) = @_; 1754 my ($self, $text) = @_;
1697 1755
1698 $self->{cursor} = length $text; 1756 $self->{cursor} = length $text;
1699 $self->_set_text ($text); 1757 $self->_set_text ($text);
1700
1701 $self->realloc;
1702} 1758}
1703 1759
1704sub get_text { 1760sub get_text {
1705 $_[0]{text} 1761 $_[0]{text}
1706} 1762}
1711 my ($w, $h) = $self->SUPER::size_request; 1767 my ($w, $h) = $self->SUPER::size_request;
1712 1768
1713 ($w + 1, $h) # add 1 for cursor 1769 ($w + 1, $h) # add 1 for cursor
1714} 1770}
1715 1771
1716sub key_down { 1772sub invoke_key_down {
1717 my ($self, $ev) = @_; 1773 my ($self, $ev) = @_;
1718 1774
1719 my $mod = $ev->{mod}; 1775 my $mod = $ev->{mod};
1720 my $sym = $ev->{sym}; 1776 my $sym = $ev->{sym};
1721 my $uni = $ev->{unicode}; 1777 my $uni = $ev->{unicode};
1733 } elsif ($sym == CFClient::SDLK_HOME) { 1789 } elsif ($sym == CFClient::SDLK_HOME) {
1734 $self->{cursor} = 0; 1790 $self->{cursor} = 0;
1735 } elsif ($sym == CFClient::SDLK_END) { 1791 } elsif ($sym == CFClient::SDLK_END) {
1736 $self->{cursor} = length $text; 1792 $self->{cursor} = length $text;
1737 } elsif ($uni == 27) { 1793 } elsif ($uni == 27) {
1738 $self->_emit ('escape'); 1794 $self->emit ('escape');
1739 } elsif ($uni) { 1795 } elsif ($uni) {
1740 substr $text, $self->{cursor}++, 0, chr $uni; 1796 substr $text, $self->{cursor}++, 0, chr $uni;
1741 } else { 1797 } else {
1742 return 0; 1798 return 0;
1743 } 1799 }
1747 $self->realloc; 1803 $self->realloc;
1748 1804
1749 1 1805 1
1750} 1806}
1751 1807
1752sub focus_in { 1808sub invoke_focus_in {
1753 my ($self) = @_; 1809 my ($self) = @_;
1754 1810
1755 $self->{last_activity} = $::NOW; 1811 $self->{last_activity} = $::NOW;
1756 1812
1757 $self->SUPER::focus_in; 1813 $self->SUPER::invoke_focus_in
1758} 1814}
1759 1815
1760sub button_down { 1816sub invoke_button_down {
1761 my ($self, $ev, $x, $y) = @_; 1817 my ($self, $ev, $x, $y) = @_;
1762 1818
1763 $self->SUPER::button_down ($ev, $x, $y); 1819 $self->SUPER::invoke_button_down ($ev, $x, $y);
1764 1820
1765 my $idx = $self->{layout}->xy_to_index ($x, $y); 1821 my $idx = $self->{layout}->xy_to_index ($x, $y);
1766 1822
1767 # byte-index to char-index 1823 # byte-index to char-index
1768 my $text = $self->{text}; 1824 my $text = $self->{text};
1773 $self->update; 1829 $self->update;
1774 1830
1775 1 1831 1
1776} 1832}
1777 1833
1778sub mouse_motion { 1834sub invoke_mouse_motion {
1779 my ($self, $ev, $x, $y) = @_; 1835 my ($self, $ev, $x, $y) = @_;
1780# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1836# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1781 1837
1782 0 1838 1
1783} 1839}
1784 1840
1785sub _draw { 1841sub _draw {
1786 my ($self) = @_; 1842 my ($self) = @_;
1787 1843
1828 1884
1829our @ISA = CFClient::UI::EntryBase::; 1885our @ISA = CFClient::UI::EntryBase::;
1830 1886
1831use CFClient::OpenGL; 1887use CFClient::OpenGL;
1832 1888
1833sub key_down { 1889sub invoke_key_down {
1834 my ($self, $ev) = @_; 1890 my ($self, $ev) = @_;
1835 1891
1836 my $sym = $ev->{sym}; 1892 my $sym = $ev->{sym};
1837 1893
1838 if ($sym == 13) { 1894 if ($sym == 13) {
1839 unshift @{$self->{history}}, 1895 unshift @{$self->{history}},
1840 my $txt = $self->get_text; 1896 my $txt = $self->get_text;
1897
1841 $self->{history_pointer} = -1; 1898 $self->{history_pointer} = -1;
1842 $self->{history_saveback} = ''; 1899 $self->{history_saveback} = '';
1843 $self->_emit (activate => $txt); 1900 $self->emit (activate => $txt);
1844 $self->update; 1901 $self->update;
1845 1902
1846 } elsif ($sym == CFClient::SDLK_UP) { 1903 } elsif ($sym == CFClient::SDLK_UP) {
1847 if ($self->{history_pointer} < 0) { 1904 if ($self->{history_pointer} < 0) {
1848 $self->{history_saveback} = $self->get_text; 1905 $self->{history_saveback} = $self->get_text;
1864 } else { 1921 } else {
1865 $self->set_text ($self->{history_saveback}); 1922 $self->set_text ($self->{history_saveback});
1866 } 1923 }
1867 1924
1868 } else { 1925 } else {
1869 return $self->SUPER::key_down ($ev) 1926 return $self->SUPER::invoke_key_down ($ev)
1870 } 1927 }
1871 1928
1872 1 1929 1
1873} 1930}
1874 1931
1898 can_events => 1, 1955 can_events => 1,
1899 @_ 1956 @_
1900 ) 1957 )
1901} 1958}
1902 1959
1903sub activate { }
1904
1905sub button_up { 1960sub invoke_button_up {
1906 my ($self, $ev, $x, $y) = @_; 1961 my ($self, $ev, $x, $y) = @_;
1907 1962
1908 $self->emit ("activate") 1963 $self->emit ("activate")
1909 if $x >= 0 && $x < $self->{w} 1964 if $x >= 0 && $x < $self->{w}
1910 && $y >= 0 && $y < $self->{h}; 1965 && $y >= 0 && $y < $self->{h};
1913} 1968}
1914 1969
1915sub _draw { 1970sub _draw {
1916 my ($self) = @_; 1971 my ($self) = @_;
1917 1972
1918 local $self->{fg} = $self->{fg}; 1973 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
1919
1920 if ($GRAB == $self) {
1921 $self->{fg} = $self->{active_fg};
1922 }
1923 1974
1924 glEnable GL_TEXTURE_2D; 1975 glEnable GL_TEXTURE_2D;
1925 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1976 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1926 glColor 0, 0, 0, 1; 1977 glColor 0, 0, 0, 1;
1927 1978
1928 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 1979 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1929 1980
1930 glDisable GL_TEXTURE_2D; 1981 glDisable GL_TEXTURE_2D;
1931 1982
1932 $self->SUPER::_draw; 1983 $self->SUPER::_draw;
1984}
1985
1986#############################################################################
1987
1988package CFClient::UI::ImageButton;
1989
1990our @ISA = CFClient::UI::Image::;
1991
1992use CFClient::OpenGL;
1993
1994my %textures;
1995
1996sub new {
1997 my $class = shift;
1998
1999 my $self = $class->SUPER::new (
2000 padding_x => 4,
2001 padding_y => 4,
2002 fg => [1, 1, 1],
2003 active_fg => [0, 0, 1],
2004 can_hover => 1,
2005 align => 0,
2006 valign => 0,
2007 can_events => 1,
2008 @_
2009 );
2010}
2011
2012sub invoke_button_up {
2013 my ($self, $ev, $x, $y) = @_;
2014
2015 $self->emit ("activate")
2016 if $x >= 0 && $x < $self->{w}
2017 && $y >= 0 && $y < $self->{h};
2018
2019 1
1933} 2020}
1934 2021
1935############################################################################# 2022#############################################################################
1936 2023
1937package CFClient::UI::CheckBox; 2024package CFClient::UI::CheckBox;
1964 my ($self) = @_; 2051 my ($self) = @_;
1965 2052
1966 (6) x 2 2053 (6) x 2
1967} 2054}
1968 2055
1969sub button_down { 2056sub invoke_button_down {
1970 my ($self, $ev, $x, $y) = @_; 2057 my ($self, $ev, $x, $y) = @_;
1971 2058
1972 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 2059 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1973 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 2060 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1974 $self->{state} = !$self->{state}; 2061 $self->{state} = !$self->{state};
1975 $self->_emit (changed => $self->{state}); 2062 $self->emit (changed => $self->{state});
1976 } else { 2063 } else {
1977 return 0 2064 return 0
1978 } 2065 }
1979 2066
1980 1 2067 1
2277 $self->update; 2364 $self->update;
2278 2365
2279 $self 2366 $self
2280} 2367}
2281 2368
2282sub changed { }
2283
2284sub set_range { 2369sub set_range {
2285 my ($self, $range) = @_; 2370 my ($self, $range) = @_;
2286 2371
2287 ($range, $self->{range}) = ($self->{range}, $range); 2372 ($range, $self->{range}) = ($self->{range}, $range);
2288 2373
2289 $self->update
2290 if "@$range" ne "@{$self->{range}}"; 2374 if ("@$range" ne "@{$self->{range}}") {
2375 $self->update;
2376 $self->set_value ($self->{range}[0]);
2377 }
2291} 2378}
2292 2379
2293sub set_value { 2380sub set_value {
2294 my ($self, $value) = @_; 2381 my ($self, $value) = @_;
2295 2382
2306 if $unit; 2393 if $unit;
2307 2394
2308 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2395 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2309 2396
2310 if ($value != $old_value) { 2397 if ($value != $old_value) {
2311 $self->_emit (changed => $value); 2398 $self->emit (changed => $value);
2312 $self->update; 2399 $self->update;
2313 } 2400 }
2314} 2401}
2315 2402
2316sub size_request { 2403sub size_request {
2317 my ($self) = @_; 2404 my ($self) = @_;
2318 2405
2319 ($self->{req_w}, $self->{req_h}) 2406 ($self->{req_w}, $self->{req_h})
2320} 2407}
2321 2408
2322sub button_down { 2409sub invoke_button_down {
2323 my ($self, $ev, $x, $y) = @_; 2410 my ($self, $ev, $x, $y) = @_;
2324 2411
2325 $self->SUPER::button_down ($ev, $x, $y); 2412 $self->SUPER::invoke_button_down ($ev, $x, $y);
2326 2413
2327 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2414 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2328 2415
2329 $self->mouse_motion ($ev, $x, $y) 2416 $self->invoke_mouse_motion ($ev, $x, $y)
2330} 2417}
2331 2418
2332sub mouse_motion { 2419sub invoke_mouse_motion {
2333 my ($self, $ev, $x, $y) = @_; 2420 my ($self, $ev, $x, $y) = @_;
2334 2421
2335 if ($GRAB == $self) { 2422 if ($GRAB == $self) {
2336 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2423 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2337 2424
2442sub set_range { shift->{slider}->set_range (@_) } 2529sub set_range { shift->{slider}->set_range (@_) }
2443sub set_value { shift->{slider}->set_value (@_) } 2530sub set_value { shift->{slider}->set_value (@_) }
2444 2531
2445############################################################################# 2532#############################################################################
2446 2533
2447package CFClient::UI::TextView; 2534package CFClient::UI::TextScroller;
2448 2535
2449our @ISA = CFClient::UI::HBox::; 2536our @ISA = CFClient::UI::HBox::;
2450 2537
2451use CFClient::OpenGL; 2538use CFClient::OpenGL;
2452 2539
2454 my $class = shift; 2541 my $class = shift;
2455 2542
2456 my $self = $class->SUPER::new ( 2543 my $self = $class->SUPER::new (
2457 fontsize => 1, 2544 fontsize => 1,
2458 can_events => 0, 2545 can_events => 0,
2546 indent => 0,
2459 #font => default_font 2547 #font => default_font
2460 @_, 2548 @_,
2461 2549
2462 layout => (new CFClient::Layout 1), 2550 layout => (new CFClient::Layout 1),
2463 par => [], 2551 par => [],
2478 2566
2479 $self->{fontsize} = $fontsize; 2567 $self->{fontsize} = $fontsize;
2480 $self->reflow; 2568 $self->reflow;
2481} 2569}
2482 2570
2483sub size_allocate { 2571sub invoke_size_allocate {
2484 my ($self, $w, $h) = @_; 2572 my ($self, $w, $h) = @_;
2485
2486 $self->SUPER::size_allocate ($w, $h);
2487 2573
2488 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2574 $self->{layout}->set_font ($self->{font}) if $self->{font};
2489 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2575 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2490 $self->{layout}->set_width ($self->{children}[0]{w}); 2576 $self->{layout}->set_width ($self->{children}[0]{w});
2577 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2491 2578
2492 $self->reflow; 2579 $self->reflow;
2580
2581 $self->SUPER::invoke_size_allocate ($w, $h)
2493} 2582}
2494 2583
2495sub text_size { 2584sub text_size {
2496 my ($self, $text, $indent) = @_; 2585 my ($self, $text, $indent) = @_;
2497 2586
2498 my $layout = $self->{layout}; 2587 my $layout = $self->{layout};
2499 2588
2500 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2589 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2501 $layout->set_width ($self->{children}[0]{w} - $indent); 2590 $layout->set_width ($self->{children}[0]{w} - $indent);
2591 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2502 $layout->set_markup ($text); 2592 $layout->set_markup ($text);
2503 2593
2504 $layout->size 2594 $layout->size
2505} 2595}
2506 2596
2545 2635
2546 return unless $self->{h} > 0; 2636 return unless $self->{h} > 0;
2547 2637
2548 delete $self->{texture}; 2638 delete $self->{texture};
2549 2639
2550 $ROOT->on_post_alloc ($self, sub { 2640 $ROOT->on_post_alloc ($self => sub {
2551 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2641 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2552 2642
2553 if (delete $self->{need_reflow}) { 2643 if (delete $self->{need_reflow}) {
2554 my $height = 0; 2644 my $height = 0;
2555 2645
2558 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2648 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2559 2649
2560 for (@{$self->{par}}) { 2650 for (@{$self->{par}}) {
2561 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2651 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2562 $layout->set_width ($W - $_->[3]); 2652 $layout->set_width ($W - $_->[3]);
2653 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2563 $layout->set_markup ($_->[4]); 2654 $layout->set_markup ($_->[4]);
2564 my ($w, $h) = $layout->size; 2655 my ($w, $h) = $layout->size;
2565 $_->[0] = $w + $_->[3]; 2656 $_->[0] = $w + $_->[3];
2566 $_->[1] = $h; 2657 $_->[1] = $h;
2567 } 2658 }
2570 } 2661 }
2571 2662
2572 $self->{height} = $height; 2663 $self->{height} = $height;
2573 2664
2574 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2665 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2575 2666
2576 delete $self->{texture}; 2667 delete $self->{texture};
2577 } 2668 }
2578 2669
2579 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2670 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2580 glClearColor 0.5, 0.5, 0.5, 0; 2671 glClearColor 0, 0, 0, 0;
2581 glClear GL_COLOR_BUFFER_BIT; 2672 glClear GL_COLOR_BUFFER_BIT;
2582 2673
2583 my $top = int $self->{children}[1]{range}[0]; 2674 my $top = int $self->{children}[1]{range}[0];
2584 2675
2585 my $y0 = $top; 2676 my $y0 = $top;
2599 my $h = $par->[1]; 2690 my $h = $par->[1];
2600 2691
2601 if ($y0 < $y + $h && $y < $y1) { 2692 if ($y0 < $y + $h && $y < $y1) {
2602 $layout->set_foreground (@{ $par->[2] }); 2693 $layout->set_foreground (@{ $par->[2] });
2603 $layout->set_width ($W - $par->[3]); 2694 $layout->set_width ($W - $par->[3]);
2695 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2604 $layout->set_markup ($par->[4]); 2696 $layout->set_markup ($par->[4]);
2605 2697
2606 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2698 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2607 2699
2608 glRasterPos $par->[3], $y - $y0; 2700 glRasterPos $par->[3], $y - $y0;
2620sub _draw { 2712sub _draw {
2621 my ($self) = @_; 2713 my ($self) = @_;
2622 2714
2623 glEnable GL_TEXTURE_2D; 2715 glEnable GL_TEXTURE_2D;
2624 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2625 glColor 1, 1, 1, 1; 2717 glColor 0, 0, 0, 1;
2626 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2718 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2627 glDisable GL_TEXTURE_2D; 2719 glDisable GL_TEXTURE_2D;
2628 2720
2629 $self->{children}[1]->draw; 2721 $self->{children}[1]->draw;
2630 2722
2631} 2723}
2722 $tooltip .= "\n\n" . (ref $widget) . "\n" 2814 $tooltip .= "\n\n" . (ref $widget) . "\n"
2723 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 2815 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2724 . "req $widget->{req_w} $widget->{req_h}\n" 2816 . "req $widget->{req_w} $widget->{req_h}\n"
2725 . "visible $widget->{visible}"; 2817 . "visible $widget->{visible}";
2726 } 2818 }
2819
2820 $tooltip =~ s/^\n+//;
2821 $tooltip =~ s/\n+$//;
2727 2822
2728 $self->add (new CFClient::UI::Label 2823 $self->add (new CFClient::UI::Label
2729 markup => $tooltip, 2824 markup => $tooltip,
2730 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2825 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2731 fontsize => 0.8, 2826 fontsize => 0.8,
2741 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2836 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2742 2837
2743 ($w + 4, $h + 4) 2838 ($w + 4, $h + 4)
2744} 2839}
2745 2840
2746sub size_allocate { 2841sub invoke_size_allocate {
2747 my ($self, $w, $h) = @_; 2842 my ($self, $w, $h) = @_;
2748 2843
2749 $self->SUPER::size_allocate ($w - 4, $h - 4); 2844 $self->SUPER::invoke_size_allocate ($w - 4, $h - 4)
2750} 2845}
2751 2846
2752sub visibility_change { 2847sub invoke_visibility_change {
2753 my ($self, $visible) = @_; 2848 my ($self, $visible) = @_;
2754 2849
2755 return unless $visible; 2850 return unless $visible;
2756 2851
2757 $self->{root}->on_post_alloc ("move_$self" => sub { 2852 $self->{root}->on_post_alloc ("move_$self" => sub {
2759 or return; 2854 or return;
2760 2855
2761 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 2856 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2762 2857
2763 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 2858 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2764 if $x + $self->{w} > $::WIDTH; 2859 if $x + $self->{w} > $self->{root}{w};
2765 2860
2766 $self->move_abs ($x, $y); 2861 $self->move_abs ($x, $y);
2767 }); 2862 });
2768} 2863}
2769 2864
2858 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 2953 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2859 2954
2860 if ($tex) { 2955 if ($tex) {
2861 glEnable GL_TEXTURE_2D; 2956 glEnable GL_TEXTURE_2D;
2862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2957 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2863 glColor 1, 1, 1, 1; 2958 glColor 0, 0, 0, 1;
2864 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2959 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2865 glDisable GL_TEXTURE_2D; 2960 glDisable GL_TEXTURE_2D;
2866 } 2961 }
2867} 2962}
2868 2963
2869sub DESTROY { 2964sub destroy {
2870 my ($self) = @_; 2965 my ($self) = @_;
2871 2966
2872 $self->{timer}->cancel 2967 $self->{timer}->cancel
2873 if $self->{timer}; 2968 if $self->{timer};
2874 2969
2875 $self->SUPER::DESTROY; 2970 $self->SUPER::destroy;
2876} 2971}
2877 2972
2878############################################################################# 2973#############################################################################
2879 2974
2880package CFClient::UI::Buttonbar; 2975package CFClient::UI::Buttonbar;
2901 ); 2996 );
2902 2997
2903 $self->add ($self->{vbox} = new CFClient::UI::VBox); 2998 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2904 2999
2905 for my $item (@{ $self->{items} }) { 3000 for my $item (@{ $self->{items} }) {
2906 my ($widget, $cb) = @$item; 3001 my ($widget, $cb, $tooltip) = @$item;
2907 3002
2908 # handle various types of items, only text for now 3003 # handle various types of items, only text for now
2909 if (!ref $widget) { 3004 if (!ref $widget) {
2910 $widget = new CFClient::UI::Label 3005 $widget = new CFClient::UI::Label
2911 can_hover => 1, 3006 can_hover => 1,
2912 can_events => 1, 3007 can_events => 1,
2913 text => $widget; 3008 markup => $widget,
3009 tooltip => $tooltip
2914 } 3010 }
2915 3011
2916 $self->{item}{$widget} = $item; 3012 $self->{item}{$widget} = $item;
2917 3013
2918 $self->{vbox}->add ($widget); 3014 $self->{vbox}->add ($widget);
2923 3019
2924# popup given the event (must be a mouse button down event currently) 3020# popup given the event (must be a mouse button down event currently)
2925sub popup { 3021sub popup {
2926 my ($self, $ev) = @_; 3022 my ($self, $ev) = @_;
2927 3023
2928 $self->_emit ("popdown"); 3024 $self->emit ("popdown");
2929 3025
2930 # maybe save $GRAB? must be careful about events... 3026 # maybe save $GRAB? must be careful about events...
2931 $GRAB = $self; 3027 $GRAB = $self;
2932 $self->{button} = $ev->{button}; 3028 $self->{button} = $ev->{button};
2933 3029
2934 $self->show; 3030 $self->show;
2935 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3031 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2936} 3032}
2937 3033
2938sub mouse_motion { 3034sub invoke_mouse_motion {
2939 my ($self, $ev, $x, $y) = @_; 3035 my ($self, $ev, $x, $y) = @_;
2940 3036
2941 # TODO: should use vbox->find_widget or so 3037 # TODO: should use vbox->find_widget or so
2942 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 3038 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2943 $self->{hover} = $self->{item}{$HOVER}; 3039 $self->{hover} = $self->{item}{$HOVER};
2944 3040
2945 0 3041 0
2946} 3042}
2947 3043
2948sub button_up { 3044sub invoke_button_up {
2949 my ($self, $ev, $x, $y) = @_; 3045 my ($self, $ev, $x, $y) = @_;
2950 3046
2951 if ($ev->{button} == $self->{button}) { 3047 if ($ev->{button} == $self->{button}) {
2952 undef $GRAB; 3048 undef $GRAB;
2953 $self->hide; 3049 $self->hide;
2954 3050
2955 $self->_emit ("popdown"); 3051 $self->emit ("popdown");
2956 $self->{hover}[1]->() if $self->{hover}; 3052 $self->{hover}[1]->() if $self->{hover};
2957 } else { 3053 } else {
2958 return 0 3054 return 0
2959 } 3055 }
2960 3056
2997 : $self->{children}[$page_or_widget]; 3093 : $self->{children}[$page_or_widget];
2998 3094
2999 $self->{current} = $widget; 3095 $self->{current} = $widget;
3000 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3096 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3001 3097
3002 $self->_emit (page_changed => $self->{current}); 3098 $self->emit (page_changed => $self->{current});
3003 3099
3004 $self->realloc; 3100 $self->realloc;
3005} 3101}
3006 3102
3007sub visible_children { 3103sub visible_children {
3012 my ($self) = @_; 3108 my ($self) = @_;
3013 3109
3014 $self->{current}->size_request 3110 $self->{current}->size_request
3015} 3111}
3016 3112
3017sub size_allocate { 3113sub invoke_size_allocate {
3018 my ($self, $w, $h) = @_; 3114 my ($self, $w, $h) = @_;
3019 3115
3020 $self->{current}->configure (0, 0, $w, $h); 3116 $self->{current}->configure (0, 0, $w, $h);
3117
3118 1
3021} 3119}
3022 3120
3023sub _draw { 3121sub _draw {
3024 my ($self) = @_; 3122 my ($self) = @_;
3025 3123
3064 3162
3065sub set_current_page { 3163sub set_current_page {
3066 my ($self, $page) = @_; 3164 my ($self, $page) = @_;
3067 3165
3068 $self->{multiplexer}->set_current_page ($page); 3166 $self->{multiplexer}->set_current_page ($page);
3069 $self->_emit (page_changed => $self->{multiplexer}{current}); 3167 $self->emit (page_changed => $self->{multiplexer}{current});
3070} 3168}
3071 3169
3072############################################################################# 3170#############################################################################
3073 3171
3074package CFClient::UI::Statusbox; 3172package CFClient::UI::Combobox;
3075 3173
3174use utf8;
3175
3076our @ISA = CFClient::UI::VBox::; 3176our @ISA = CFClient::UI::Button::;
3077 3177
3078sub new { 3178sub new {
3079 my $class = shift; 3179 my $class = shift;
3080 3180
3081 $class->SUPER::new ( 3181 my $self = $class->SUPER::new (
3182 options => [], # [value, title, longdesc], ...
3183 value => undef,
3184 @_,
3185 );
3186
3187 $self->_set_value ($self->{value});
3188
3189 $self
3190}
3191
3192sub invoke_button_down {
3193 my ($self, $ev) = @_;
3194
3195 my @menu_items;
3196
3197 for (@{ $self->{options} }) {
3198 my ($value, $title, $tooltip) = @$_;
3199
3200 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3201 }
3202
3203 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
3204}
3205
3206sub _set_value {
3207 my ($self, $value) = @_;
3208
3209 my ($item) = grep $_->[0] eq $value, @{ $self->{options} }
3210 or return;
3211
3212 $self->{value} = $item->[0];
3213 $self->set_markup ("$item->[1] ⇓");
3214 $self->set_tooltip ($item->[2]);
3215}
3216
3217sub set_value {
3218 my ($self, $value) = @_;
3219
3220 return unless $self->{value} ne $value;
3221
3222 $self->_set_value ($value);
3223 $self->emit (changed => $value);
3224}
3225
3226#############################################################################
3227
3228package CFClient::UI::Statusbox;
3229
3230our @ISA = CFClient::UI::VBox::;
3231
3232sub new {
3233 my $class = shift;
3234
3235 my $self = $class->SUPER::new (
3082 fontsize => 0.8, 3236 fontsize => 0.8,
3083 @_, 3237 @_,
3084 ) 3238 );
3239
3240 Scalar::Util::weaken (my $this = $self);
3241
3242 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3243
3244 $self
3085} 3245}
3086 3246
3087sub reorder { 3247sub reorder {
3088 my ($self) = @_; 3248 my ($self) = @_;
3089 my $NOW = time; 3249 my $NOW = Time::HiRes::time;
3250
3251 # freeze display when hovering over any label
3252 return if $CFClient::UI::TOOLTIP->{owner}
3253 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label},
3254 values %{ $self->{item} };
3090 3255
3091 while (my ($k, $v) = each %{ $self->{item} }) { 3256 while (my ($k, $v) = each %{ $self->{item} }) {
3092 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3257 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3093 } 3258 }
3094 3259
3097 my @items = sort { 3262 my @items = sort {
3098 $a->{pri} <=> $b->{pri} 3263 $a->{pri} <=> $b->{pri}
3099 or $b->{id} <=> $a->{id} 3264 or $b->{id} <=> $a->{id}
3100 } values %{ $self->{item} }; 3265 } values %{ $self->{item} };
3101 3266
3267 $self->{timer}->interval (1);
3268
3102 my $count = 10 + 1; 3269 my $count = 10 + 1;
3103 for my $item (@items) { 3270 for my $item (@items) {
3104 last unless --$count; 3271 last unless --$count;
3105 3272
3106 push @widgets, $item->{label} ||= do { 3273 my $label = $item->{label} ||= do {
3107 # TODO: doesn't handle markup well (read as: at all) 3274 # TODO: doesn't handle markup well (read as: at all)
3108 my $short = $item->{count} > 1 3275 my $short = $item->{count} > 1
3109 ? "<b>$item->{count} ×</b> $item->{text}" 3276 ? "<b>$item->{count} ×</b> $item->{text}"
3110 : $item->{text}; 3277 : $item->{text};
3111 3278
3119 tooltip => $item->{tooltip}, 3286 tooltip => $item->{tooltip},
3120 tooltip_font => $::FONT_PROP, 3287 tooltip_font => $::FONT_PROP,
3121 tooltip_width => 0.67, 3288 tooltip_width => 0.67,
3122 fontsize => $item->{fontsize} || $self->{fontsize}, 3289 fontsize => $item->{fontsize} || $self->{fontsize},
3123 max_w => $::WIDTH * 0.44, 3290 max_w => $::WIDTH * 0.44,
3124 fg => $item->{fg}, 3291 fg => [@{ $item->{fg} }],
3125 can_events => 1, 3292 can_events => 1,
3126 can_hover => 1 3293 can_hover => 1
3127 }; 3294 };
3295
3296 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3297 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3298 $label->update;
3299 $label->set_max_size (undef, $label->{req_h} * $diff)
3300 if $diff < 1;
3301 $self->{timer}->interval (1/30);
3302 } else {
3303 $label->{fg}[3] = $item->{fg}[3] || 1;
3304 }
3305
3306 push @widgets, $label;
3128 } 3307 }
3129 3308
3130 $self->clear; 3309 $self->clear;
3131 $self->SUPER::add (reverse @widgets); 3310 $self->SUPER::add (reverse @widgets);
3132} 3311}
3137 $text =~ s/^\s+//; 3316 $text =~ s/^\s+//;
3138 $text =~ s/\s+$//; 3317 $text =~ s/\s+$//;
3139 3318
3140 return unless $text; 3319 return unless $text;
3141 3320
3142 my $timeout = time + ((delete $arg{timeout}) || 60); 3321 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
3143 3322
3144 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 3323 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
3145 3324
3146 if (my $item = $self->{item}{$group}) { 3325 if (my $item = $self->{item}{$group}) {
3147 if ($item->{text} eq $text) { 3326 if ($item->{text} eq $text) {
3148 $item->{count}++; 3327 $item->{count}++;
3149 } else { 3328 } else {
3150 $item->{count} = 1; 3329 $item->{count} = 1;
3151 $item->{text} = $item->{tooltip} = $text; 3330 $item->{text} = $item->{tooltip} = $text;
3152 } 3331 }
3153 $item->{id} = ++$self->{id}; 3332 $item->{id} += 0.2;#d#
3154 $item->{timeout} = $timeout; 3333 $item->{timeout} = $timeout;
3155 delete $item->{label}; 3334 delete $item->{label};
3156 } else { 3335 } else {
3157 $self->{item}{$group} = { 3336 $self->{item}{$group} = {
3158 id => ++$self->{id}, 3337 id => ++$self->{id},
3177 3356
3178 $self->reorder; 3357 $self->reorder;
3179 $self->SUPER::reconfigure; 3358 $self->SUPER::reconfigure;
3180} 3359}
3181 3360
3361sub destroy {
3362 my ($self) = @_;
3363
3364 $self->{timer}->cancel;
3365
3366 $self->SUPER::destroy;
3367}
3368
3182############################################################################# 3369#############################################################################
3183 3370
3184package CFClient::UI::Inventory; 3371package CFClient::UI::Inventory;
3185 3372
3186our @ISA = CFClient::UI::ScrolledWindow::; 3373our @ISA = CFClient::UI::ScrolledWindow::;
3328 my ($self) = @_; 3515 my ($self) = @_;
3329 $self->ask_for_bind (1); 3516 $self->ask_for_bind (1);
3330} 3517}
3331 3518
3332sub ask_for_bind { 3519sub ask_for_bind {
3333 my ($self, $commit) = @_; 3520 my ($self, $commit, $end_cb) = @_;
3334 3521
3335 CFClient::Binder::open_binding_dialog (sub { 3522 CFClient::Binder::open_binding_dialog (sub {
3336 my ($mod, $sym) = @_; 3523 my ($mod, $sym) = @_;
3337 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? 3524 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak?
3338 $self->update_binding_widgets; 3525 $self->update_binding_widgets;
3339 $self->commit if $commit; 3526 $self->commit if $commit;
3527 $end_cb->() if $end_cb;
3340 }); 3528 });
3341} 3529}
3342 3530
3343# $mod and $sym are the modifiers and key symbol 3531# $mod and $sym are the modifiers and key symbol
3344# $cmds is a array ref of strings (the commands) 3532# $cmds is a array ref of strings (the commands)
3362} 3550}
3363 3551
3364# this is a shortcut method that asks for a binding 3552# this is a shortcut method that asks for a binding
3365# and then just binds it. 3553# and then just binds it.
3366sub do_quick_binding { 3554sub do_quick_binding {
3367 my ($self, $cmds) = @_; 3555 my ($self, $cmds, $end_cb) = @_;
3368 $self->set_binding (undef, undef, $cmds, sub { 3556 $self->set_binding (undef, undef, $cmds, sub {
3369 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2]; 3557 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2];
3370 }); 3558 });
3371 $self->ask_for_bind (1); 3559 $self->ask_for_bind (1, $end_cb);
3372} 3560}
3373 3561
3374sub update_binding_widgets { 3562sub update_binding_widgets {
3375 my ($self) = @_; 3563 my ($self) = @_;
3376 my ($mod, $sym, $cmds) = $self->get_binding; 3564 my ($mod, $sym, $cmds) = $self->get_binding;
3432 commands => [], 3620 commands => [],
3433 @_, 3621 @_,
3434 ) 3622 )
3435} 3623}
3436 3624
3437# XXX: Do sorting? Argl... 3625my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3626
3627my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3628 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3629my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3630 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3631my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3632 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3633my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3634 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3635my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3636 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3637
3638sub rebuild_spell_list {
3639 my ($self) = @_;
3640
3641 $CFClient::UI::ROOT->on_refresh ($self => sub {
3642 $self->clear;
3643
3644 return unless $::CONN;
3645
3646 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3647 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3648 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3649 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3650 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3651
3652 my $row = 0;
3653
3654 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3655 my $spell = $self->{spell}{$_};
3656
3657 $row++;
3658
3659 my $spell_cb = sub {
3660 my ($widget, $ev) = @_;
3661
3662 if ($ev->{button} == 1) {
3663 $::CONN->user_send ("cast $spell->{name}");
3664 } elsif ($ev->{button} == 2) {
3665 $::CONN->user_send ("invoke $spell->{name}");
3666 } elsif ($ev->{button} == 3) {
3667 (new CFClient::UI::Menu
3668 items => [
3669 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3670 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3671 ],
3672 )->popup ($ev);
3673 } else {
3674 return 0;
3675 }
3676
3677 1
3678 };
3679
3680 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3681
3682 #TODO: add path info to tooltip
3683 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3684
3685 $self->add (0, $row, new CFClient::UI::Face
3686 face => $spell->{face},
3687 can_hover => 1,
3688 can_events => 1,
3689 tooltip => $tooltip,
3690 on_button_down => $spell_cb,
3691 );
3692
3693 $self->add (1, $row, new CFClient::UI::Label
3694 expand => 1,
3695 text => $spell->{name},
3696 can_hover => 1,
3697 can_events => 1,
3698 tooltip => $tooltip,
3699 on_button_down => $spell_cb,
3700 );
3701
3702 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3703 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3704 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3705 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3706 }
3707 });
3708}
3709
3438sub add_spell { 3710sub add_spell {
3439 my ($self, $spell) = @_; 3711 my ($self, $spell) = @_;
3712
3440 $self->{spells}->{$spell->{name}} = $spell; 3713 $self->{spell}->{$spell->{name}} = $spell;
3441 3714 $self->rebuild_spell_list;
3442 $self->add (0, $self->{tbl_idx}, new CFClient::UI::Face
3443 face => $spell->{face},
3444 can_hover => 1,
3445 can_events => 1,
3446 tooltip => $spell->{message});
3447
3448 $self->add (1, $self->{tbl_idx}, new CFClient::UI::Label
3449 text => $spell->{name},
3450 can_hover => 1,
3451 can_events => 1,
3452 tooltip => $spell->{message},
3453 expand => 1);
3454
3455 $self->add (2, $self->{tbl_idx}, new CFClient::UI::Label
3456 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3457 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3458 expand => 1);
3459
3460 $self->add (3, $self->{tbl_idx}++, new CFClient::UI::Button
3461 text => "bind to key",
3462 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) });
3463}
3464
3465sub rebuild_spell_list {
3466 my ($self) = @_;
3467 $self->{tbl_idx} = 0;
3468 $self->add_spell ($_) for values %{$self->{spells}};
3469} 3715}
3470 3716
3471sub remove_spell { 3717sub remove_spell {
3472 my ($self, $spell) = @_; 3718 my ($self, $spell) = @_;
3719
3473 delete $self->{spells}->{$spell->{name}}; 3720 delete $self->{spell}->{$spell->{name}};
3474 $self->rebuild_spell_list; 3721 $self->rebuild_spell_list;
3475} 3722}
3476 3723
3724sub clear_spells {
3725 my ($self) = @_;
3726
3727 $self->{spell} = {};
3728 $self->rebuild_spell_list;
3729}
3730
3477############################################################################# 3731#############################################################################
3478 3732
3479package CFClient::UI::Root; 3733package CFClient::UI::Root;
3480 3734
3481our @ISA = CFClient::UI::Container::; 3735our @ISA = CFClient::UI::Container::;
3736
3737use List::Util qw(min max);
3482 3738
3483use CFClient::OpenGL; 3739use CFClient::OpenGL;
3484 3740
3485sub new { 3741sub new {
3486 my $class = shift; 3742 my $class = shift;
3513 $coord = $max - $size if $coord > $max - $size; 3769 $coord = $max - $size if $coord > $max - $size;
3514 3770
3515 int $coord + 0.5 3771 int $coord + 0.5
3516} 3772}
3517 3773
3518sub size_allocate { 3774sub invoke_size_allocate {
3519 my ($self, $w, $h) = @_; 3775 my ($self, $w, $h) = @_;
3520 3776
3521 for my $child ($self->children) { 3777 for my $child ($self->children) {
3522 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3778 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3523 3779
3527 $X = _to_pixel $X, $W, $self->{w}; 3783 $X = _to_pixel $X, $W, $self->{w};
3528 $Y = _to_pixel $Y, $H, $self->{h}; 3784 $Y = _to_pixel $Y, $H, $self->{h};
3529 3785
3530 $child->configure ($X, $Y, $W, $H); 3786 $child->configure ($X, $Y, $W, $H);
3531 } 3787 }
3788
3789 1
3532} 3790}
3533 3791
3534sub coord2local { 3792sub coord2local {
3535 my ($self, $x, $y) = @_; 3793 my ($self, $x, $y) = @_;
3536 3794
3621 3879
3622 delete $queue{$widget+0}; 3880 delete $queue{$widget+0};
3623 3881
3624 my ($w, $h) = $widget->size_request; 3882 my ($w, $h) = $widget->size_request;
3625 3883
3626 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; 3884 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3627 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; 3885 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3886
3887 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3888 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3628 3889
3629 $w = $widget->{force_w} if exists $widget->{force_w}; 3890 $w = $widget->{force_w} if exists $widget->{force_w};
3630 $h = $widget->{force_h} if exists $widget->{force_h}; 3891 $h = $widget->{force_h} if exists $widget->{force_h};
3631 3892
3632 if ($widget->{req_w} != $w || $widget->{req_h} != $h 3893 if ($widget->{req_w} != $w || $widget->{req_h} != $h

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines