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.267 by root, Thu Jun 1 04:10:29 2006 UTC vs.
Revision 1.302 by root, Mon Jun 12 13:26: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}
171sub rescale_widgets { 172sub rescale_widgets {
172 my ($sx, $sy) = @_; 173 my ($sx, $sy) = @_;
173 174
174 for my $widget (values %WIDGET) { 175 for my $widget (values %WIDGET) {
175 if ($widget->{is_toplevel}) { 176 if ($widget->{is_toplevel}) {
176 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 177 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
177 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 178 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
178 179
179 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 180 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
180 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 181 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
181 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; 182 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
182 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; 183 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
183 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 184 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
184 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; 185 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
185 186
186 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 187 $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
187 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 188 $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
188 189
189 } 190 }
190 } 191 }
191 192
192 reconfigure_widgets; 193 reconfigure_widgets;
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->focus_out;
286 287
287 $self->emit (visibility_change => 0); 288 $self->emit (visibility_change => 0);
314} 315}
315 316
316sub move_abs { 317sub move_abs {
317 my ($self, $x, $y, $z) = @_; 318 my ($self, $x, $y, $z) = @_;
318 319
319 $self->{x} = List::Util::max 0, int $x; 320 $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; 321 $self->{y} = List::Util::max 0, List::Util::min $self->{root}{h} - $self->{h}, int $y;
321 $self->{z} = $z if defined $z; 322 $self->{z} = $z if defined $z;
322 323
323 $self->update; 324 $self->update;
324} 325}
325 326
371sub size_allocate { 372sub size_allocate {
372 # nothing to be done 373 # nothing to be done
373} 374}
374 375
375sub children { 376sub children {
377 # nop
378}
379
380sub visible_children {
381 $_[0]->children
376} 382}
377 383
378sub set_max_size { 384sub set_max_size {
379 my ($self, $w, $h) = @_; 385 my ($self, $w, $h) = @_;
380 386
381 delete $self->{max_w}; $self->{max_w} = $w if $w; 387 $self->{max_w} = int $w if defined $w;
382 delete $self->{max_h}; $self->{max_h} = $h if $h; 388 $self->{max_h} = int $h if defined $h;
389
390 $self->realloc;
383} 391}
384 392
385sub set_tooltip { 393sub set_tooltip {
386 my ($self, $tooltip) = @_; 394 my ($self, $tooltip) = @_;
387 395
392 400
393 $self->{tooltip} = $tooltip; 401 $self->{tooltip} = $tooltip;
394 402
395 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 403 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
396 delete $CFClient::UI::TOOLTIP->{owner}; 404 delete $CFClient::UI::TOOLTIP->{owner};
397 CFClient::UI::check_tooltip; 405 $CFClient::UI::TOOLTIP_WATCHER->cb->();
398 } 406 }
399} 407}
400 408
401# translate global coordinates to local coordinate system 409# translate global coordinates to local coordinate system
402sub coord2local { 410sub coord2local {
439 447
440 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 448 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
441 unless $FOCUS; 449 unless $FOCUS;
442} 450}
443 451
444sub mouse_motion { } 452sub mouse_motion { 0 }
445sub button_up { } 453sub button_up { 0 }
446sub key_down { } 454sub key_down { 0 }
447sub key_up { } 455sub key_up { 0 }
448 456
449sub button_down { 457sub button_down {
450 my ($self, $ev, $x, $y) = @_; 458 my ($self, $ev, $x, $y) = @_;
451 459
452 $self->focus_in; 460 $self->focus_in;
461
462 0
453} 463}
454 464
455sub find_widget { 465sub find_widget {
456 my ($self, $x, $y) = @_; 466 my ($self, $x, $y) = @_;
457 467
529sub draw { 539sub draw {
530 my ($self) = @_; 540 my ($self) = @_;
531 541
532 return unless $self->{h} && $self->{w}; 542 return unless $self->{h} && $self->{w};
533 543
544 # update screen rectangle
534 local $draw_x = $draw_x + $self->{x}; 545 local $draw_x = $draw_x + $self->{x};
535 local $draw_y = $draw_y + $self->{y}; 546 local $draw_y = $draw_y + $self->{y};
547 local $draw_w = $draw_x + $self->{w};
548 local $draw_h = $draw_y + $self->{h};
536 549
537 return if $draw_x + $self->{w} < 0; 550 # skip widgets that are entirely outside the drawing area
538 return if $draw_x >= $draw_w; 551 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
539 return if $draw_y + $self->{h} < 0; 552 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
540 return if $draw_y >= $draw_h;
541 553
542 glPushMatrix; 554 glPushMatrix;
543 glTranslate $self->{x}, $self->{y}, 0; 555 glTranslate $self->{x}, $self->{y}, 0;
544 $self->_draw;
545 glPopMatrix;
546 556
547 if ($self == $HOVER && $self->{can_hover}) { 557 if ($self == $HOVER && $self->{can_hover}) {
548 my ($x, $y) = @$self{qw(x y)};
549
550 glColor 1, 0.8, 0.5, 0.2; 558 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
551 glEnable GL_BLEND; 559 glEnable GL_BLEND;
552 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 560 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
553 glBegin GL_QUADS; 561 glBegin GL_QUADS;
554 glVertex $x , $y; 562 glVertex 0 , 0;
555 glVertex $x + $self->{w}, $y; 563 glVertex $self->{w}, 0;
556 glVertex $x + $self->{w}, $y + $self->{h}; 564 glVertex $self->{w}, $self->{h};
557 glVertex $x , $y + $self->{h}; 565 glVertex 0 , $self->{h};
558 glEnd; 566 glEnd;
559 glDisable GL_BLEND; 567 glDisable GL_BLEND;
560 } 568 }
561 569
562 if ($ENV{CFPLUS_DEBUG} & 1) { 570 if ($ENV{CFPLUS_DEBUG} & 1) {
563 glPushMatrix; 571 glPushMatrix;
564 glColor 1, 1, 0, 1; 572 glColor 1, 1, 0, 1;
565 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 573 glTranslate 0.375, 0.375;
566 glBegin GL_LINE_LOOP; 574 glBegin GL_LINE_LOOP;
567 glVertex 0 , 0; 575 glVertex 0 , 0;
568 glVertex $self->{w} - 1, 0; 576 glVertex $self->{w} - 1, 0;
569 glVertex $self->{w} - 1, $self->{h} - 1; 577 glVertex $self->{w} - 1, $self->{h} - 1;
570 glVertex 0 , $self->{h} - 1; 578 glVertex 0 , $self->{h} - 1;
571 glEnd; 579 glEnd;
572 glPopMatrix; 580 glPopMatrix;
573 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 581 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
574 } 582 }
583
584 $self->_draw;
585 glPopMatrix;
575} 586}
576 587
577sub _draw { 588sub _draw {
578 my ($self) = @_; 589 my ($self) = @_;
579 590
582 593
583sub DESTROY { 594sub DESTROY {
584 my ($self) = @_; 595 my ($self) = @_;
585 596
586 delete $WIDGET{$self+0}; 597 delete $WIDGET{$self+0};
587 #$self->deactivate; 598
599 eval { $self->destroy };
600 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
588} 601}
589 602
590############################################################################# 603#############################################################################
591 604
592package CFClient::UI::DrawBG; 605package CFClient::UI::DrawBG;
617 630
618 if ($color && (@$color < 4 || $color->[3])) { 631 if ($color && (@$color < 4 || $color->[3])) {
619 my ($w, $h) = @$self{qw(w h)}; 632 my ($w, $h) = @$self{qw(w h)};
620 633
621 glEnable GL_BLEND; 634 glEnable GL_BLEND;
622 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 635 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
623 glColor @$color; 636 glColor_premultiply @$color;
624 637
625 glBegin GL_QUADS; 638 glBegin GL_QUADS;
626 glVertex 0 , 0; 639 glVertex 0 , 0;
627 glVertex 0 , $h; 640 glVertex 0 , $h;
628 glVertex $w, $h; 641 glVertex $w, $h;
659our @ISA = CFClient::UI::Base::; 672our @ISA = CFClient::UI::Base::;
660 673
661sub new { 674sub new {
662 my ($class, %arg) = @_; 675 my ($class, %arg) = @_;
663 676
664 my $children = delete $arg{children} || []; 677 my $children = delete $arg{children};
665 678
666 my $self = $class->SUPER::new ( 679 my $self = $class->SUPER::new (
667 children => [], 680 children => [],
668 can_events => 0, 681 can_events => 0,
669 %arg, 682 %arg,
670 ); 683 );
684
671 $self->add ($_) for @$children; 685 $self->add (@$children)
686 if $children;
672 687
673 $self 688 $self
674} 689}
675 690
676sub add { 691sub add {
724 $x -= $self->{x}; 739 $x -= $self->{x};
725 $y -= $self->{y}; 740 $y -= $self->{y};
726 741
727 my $res; 742 my $res;
728 743
729 for (reverse @{ $self->{children} }) { 744 for (reverse $self->visible_children) {
730 $res = $_->find_widget ($x, $y) 745 $res = $_->find_widget ($x, $y)
731 and return $res; 746 and return $res;
732 } 747 }
733 748
734 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) 749 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
755} 770}
756 771
757sub add { 772sub add {
758 my ($self, $child) = @_; 773 my ($self, $child) = @_;
759 774
760 $self->{children} = []; 775 $self->SUPER::remove ($_) for @{ $self->{children} };
761
762 $self->SUPER::add ($child); 776 $self->SUPER::add ($child);
763} 777}
764 778
765sub remove { 779sub remove {
766 my ($self, $widget) = @_; 780 my ($self, $widget) = @_;
782 796
783 $self->{children}[0]->configure (0, 0, $w, $h); 797 $self->{children}[0]->configure (0, 0, $w, $h);
784} 798}
785 799
786############################################################################# 800#############################################################################
801
802# back-buffered drawing area
787 803
788package CFClient::UI::Window; 804package CFClient::UI::Window;
789 805
790our @ISA = CFClient::UI::Bin::; 806our @ISA = CFClient::UI::Bin::;
791 807
843 my $tex = $self->{texture} 859 my $tex = $self->{texture}
844 or return; 860 or return;
845 861
846 glEnable GL_TEXTURE_2D; 862 glEnable GL_TEXTURE_2D;
847 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 863 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
848 glColor 1, 1, 1, 1; 864 glColor 0, 0, 0, 1;
849 865
850 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 866 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
851 867
852 glDisable GL_TEXTURE_2D; 868 glDisable GL_TEXTURE_2D;
853} 869}
944package CFClient::UI::ScrolledWindow; 960package CFClient::UI::ScrolledWindow;
945 961
946our @ISA = CFClient::UI::HBox::; 962our @ISA = CFClient::UI::HBox::;
947 963
948sub new { 964sub new {
949 my $class = shift; 965 my ($class, %arg) = @_;
966
967 my $child = delete $arg{child};
950 968
951 my $self; 969 my $self;
952 970
953 my $slider = new CFClient::UI::Slider 971 my $slider = new CFClient::UI::Slider
954 vertical => 1, 972 vertical => 1,
959 ; 977 ;
960 978
961 $self = $class->SUPER::new ( 979 $self = $class->SUPER::new (
962 vp => (new CFClient::UI::ViewPort expand => 1), 980 vp => (new CFClient::UI::ViewPort expand => 1),
963 slider => $slider, 981 slider => $slider,
964 @_, 982 %arg,
965 ); 983 );
966 984
967 $self->{vp}->add ($self->{scrolled});
968 $self->add ($self->{vp});
969 $self->add ($self->{slider}); 985 $self->SUPER::add ($self->{vp}, $self->{slider});
986 $self->add ($child) if $child;
970 987
971 $self 988 $self
989}
990
991sub add {
992 my ($self, $widget) = @_;
993
994 $self->{vp}->add ($self->{child} = $widget);
972} 995}
973 996
974sub update { 997sub update {
975 my ($self) = @_; 998 my ($self) = @_;
976 999
1015 1038
1016 if ($self->{bg}) { 1039 if ($self->{bg}) {
1017 my ($w, $h) = @$self{qw(w h)}; 1040 my ($w, $h) = @$self{qw(w h)};
1018 1041
1019 glEnable GL_BLEND; 1042 glEnable GL_BLEND;
1020 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1043 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1021 glColor @{ $self->{bg} }; 1044 glColor_premultiply @{ $self->{bg} };
1022 1045
1023 glBegin GL_QUADS; 1046 glBegin GL_QUADS;
1024 glVertex 0 , 0; 1047 glVertex 0 , 0;
1025 glVertex 0 , $h; 1048 glVertex 0 , $h;
1026 glVertex $w, $h; 1049 glVertex $w, $h;
1048my @border = 1071my @border =
1049 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1072 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1050 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1073 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1051 1074
1052sub new { 1075sub new {
1053 my $class = shift; 1076 my ($class, %arg) = @_;
1054 1077
1055 my $self = $class->SUPER::new ( 1078 my $self = $class->SUPER::new (
1056 bg => [1, 1, 1, 1], 1079 bg => [1, 1, 1, 1],
1057 border_bg => [1, 1, 1, 1], 1080 border_bg => [1, 1, 1, 1],
1058 border => 0.6, 1081 border => 0.6,
1059 can_events => 1, 1082 can_events => 1,
1060 min_w => 16, 1083 min_w => 16,
1061 min_h => 16, 1084 min_h => 16,
1062 @_ 1085 %arg,
1063 ); 1086 );
1064 1087
1065 $self->{title} &&= new CFClient::UI::Label 1088 $self->{title_widget} = new CFClient::UI::Label
1066 align => 0, 1089 align => 0,
1067 valign => 1, 1090 valign => 1,
1068 text => $self->{title}, 1091 text => $self->{title},
1069 fontsize => $self->{border}; 1092 fontsize => $self->{border},
1093 if exists $self->{title};
1070 1094
1071 $self 1095 $self
1096}
1097
1098sub add {
1099 my ($self, @widgets) = @_;
1100
1101 $self->SUPER::add (@widgets);
1102 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1072} 1103}
1073 1104
1074sub border { 1105sub border {
1075 int $_[0]{border} * $::FONTSIZE 1106 int $_[0]{border} * $::FONTSIZE
1076} 1107}
1077 1108
1078sub size_request { 1109sub size_request {
1079 my ($self) = @_; 1110 my ($self) = @_;
1111
1112 $self->{title_widget}->size_request
1113 if $self->{title_widget};
1080 1114
1081 my ($w, $h) = $self->SUPER::size_request; 1115 my ($w, $h) = $self->SUPER::size_request;
1082 1116
1083 ( 1117 (
1084 $w + $self->border * 2, 1118 $w + $self->border * 2,
1087} 1121}
1088 1122
1089sub size_allocate { 1123sub size_allocate {
1090 my ($self, $w, $h) = @_; 1124 my ($self, $w, $h) = @_;
1091 1125
1126 if ($self->{title_widget}) {
1127 $self->{title_widget}{w} = $w;
1128 $self->{title_widget}{h} = $h;
1129 $self->{title_widget}->size_allocate ($w, $h);
1130 }
1131
1132 my $border = $self->border;
1133
1092 $h -= List::Util::max 0, $self->border * 2; 1134 $h -= List::Util::max 0, $border * 2;
1093 $w -= List::Util::max 0, $self->border * 2; 1135 $w -= List::Util::max 0, $border * 2;
1094 1136
1095 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1096 if $self->{title};
1097
1098 $self->child->configure ($self->border, $self->border, $w, $h); 1137 $self->child->configure ($border, $border, $w, $h);
1099} 1138}
1100 1139
1101sub button_down { 1140sub button_down {
1102 my ($self, $ev, $x, $y) = @_; 1141 my ($self, $ev, $x, $y) = @_;
1103 1142
1122 my $dy = $ev->{y} - $oy; 1161 my $dy = $ev->{y} - $oy;
1123 1162
1124 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1163 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1125 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1164 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1126 1165
1166 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1127 $self->realloc; 1167 $self->realloc;
1128 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1129 }; 1168 };
1130 1169
1131 } elsif ($lr ^ $td) { 1170 } elsif ($lr ^ $td) {
1132 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1171 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1133 my ($bx, $by) = ($self->{x}, $self->{y}); 1172 my ($bx, $by) = ($self->{x}, $self->{y});
1136 my ($ev, $x, $y) = @_; 1175 my ($ev, $x, $y) = @_;
1137 1176
1138 ($x, $y) = ($ev->{x}, $ev->{y}); 1177 ($x, $y) = ($ev->{x}, $ev->{y});
1139 1178
1140 $self->move_abs ($bx + $x - $ox, $by + $y - $oy); 1179 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1180 # HACK: the next line is required to enforce placement
1181 $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h});
1141 }; 1182 };
1183 } else {
1184 return 0;
1185 }
1186
1142 } 1187 1
1143} 1188}
1144 1189
1145sub button_up { 1190sub button_up {
1146 my ($self, $ev, $x, $y) = @_; 1191 my ($self, $ev, $x, $y) = @_;
1147 1192
1148 delete $self->{motion}; 1193 !!delete $self->{motion}
1149} 1194}
1150 1195
1151sub mouse_motion { 1196sub mouse_motion {
1152 my ($self, $ev, $x, $y) = @_; 1197 my ($self, $ev, $x, $y) = @_;
1153 1198
1154 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1199 $self->{motion}->($ev, $x, $y) if $self->{motion};
1200
1201 !!$self->{motion}
1155} 1202}
1156 1203
1157sub _draw { 1204sub _draw {
1158 my ($self) = @_; 1205 my ($self) = @_;
1159 1206
1207 my $child = $self->{children}[0];
1208
1160 my ($w, $h ) = ($self->{w}, $self->{h}); 1209 my ($w, $h ) = ($self->{w}, $self->{h});
1161 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1210 my ($cw, $ch) = ($child->{w}, $child->{h});
1162 1211
1163 glEnable GL_TEXTURE_2D; 1212 glEnable GL_TEXTURE_2D;
1164 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1213 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1165 1214
1166 my $border = $self->border; 1215 my $border = $self->border;
1181 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1230 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1182 } 1231 }
1183 1232
1184 glDisable GL_TEXTURE_2D; 1233 glDisable GL_TEXTURE_2D;
1185 1234
1186 $self->{title}->draw if $self->{title};
1187
1188 $self->child->draw; 1235 $child->draw;
1236
1237 if ($self->{title_widget}) {
1238 glTranslate 0, $border - $self->{h};
1239 $self->{title_widget}->_draw;
1240 }
1189} 1241}
1190 1242
1191############################################################################# 1243#############################################################################
1192 1244
1193package CFClient::UI::Table; 1245package CFClient::UI::Table;
1216 1268
1217 $child->set_parent ($self); 1269 $child->set_parent ($self);
1218 $self->{children}[$y][$x] = $child; 1270 $self->{children}[$y][$x] = $child;
1219 1271
1220 $self->realloc; 1272 $self->realloc;
1273}
1274
1275sub remove {
1276 my ($self, $child) = @_;
1277
1278 # TODO: not yet implemented
1221} 1279}
1222 1280
1223# TODO: move to container class maybe? send children a signal on removal? 1281# TODO: move to container class maybe? send children a signal on removal?
1224sub clear { 1282sub clear {
1225 my ($self) = @_; 1283 my ($self) = @_;
1486 1544
1487 delete $self->{texture}; 1545 delete $self->{texture};
1488 $self->SUPER::update; 1546 $self->SUPER::update;
1489} 1547}
1490 1548
1549sub realloc {
1550 my ($self) = @_;
1551
1552 delete $self->{ox};
1553 $self->SUPER::realloc;
1554}
1555
1491sub set_text { 1556sub set_text {
1492 my ($self, $text) = @_; 1557 my ($self, $text) = @_;
1493 1558
1494 return if $self->{text} eq "T$text"; 1559 return if $self->{text} eq "T$text";
1495 $self->{text} = "T$text"; 1560 $self->{text} = "T$text";
1496 1561
1497 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1562 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1498 $self->{layout}->set_text ($text); 1563 $self->{layout}->set_text ($text);
1499 1564
1565 delete $self->{size_req};
1500 $self->realloc; 1566 $self->realloc;
1501 $self->update; 1567 $self->update;
1502} 1568}
1503 1569
1504sub set_markup { 1570sub set_markup {
1510 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1576 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1511 1577
1512 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1578 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1513 $self->{layout}->set_markup ($markup); 1579 $self->{layout}->set_markup ($markup);
1514 1580
1581 delete $self->{size_req};
1515 $self->realloc; 1582 $self->realloc;
1516 $self->update; 1583 $self->update;
1517} 1584}
1518 1585
1519sub size_request { 1586sub size_request {
1520 my ($self) = @_; 1587 my ($self) = @_;
1521 1588
1589 $self->{size_req} ||= do {
1522 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1590 $self->{layout}->set_font ($self->{font}) if $self->{font};
1523 $self->{layout}->set_width ($self->{max_w} || -1); 1591 $self->{layout}->set_width ($self->{max_w} || -1);
1524 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1592 $self->{layout}->set_ellipsise ($self->{ellipsise});
1525 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1593 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1526 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1594 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1527 1595
1528 my ($w, $h) = $self->{layout}->size; 1596 my ($w, $h) = $self->{layout}->size;
1529 1597
1530 if (exists $self->{template}) { 1598 if (exists $self->{template}) {
1531 $self->{template}->set_font ($self->{font}) if $self->{font}; 1599 $self->{template}->set_font ($self->{font}) if $self->{font};
1532 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1600 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1533 1601
1534 my ($w2, $h2) = $self->{template}->size; 1602 my ($w2, $h2) = $self->{template}->size;
1535 1603
1536 $w = List::Util::max $w, $w2; 1604 $w = List::Util::max $w, $w2;
1537 $h = List::Util::max $h, $h2; 1605 $h = List::Util::max $h, $h2;
1606 }
1607
1608 [$w, $h]
1538 } 1609 };
1539 1610
1540 ($w, $h) 1611 @{ $self->{size_req} }
1541} 1612}
1542 1613
1543sub size_allocate { 1614sub size_allocate {
1544 my ($self, $w, $h) = @_; 1615 my ($self, $w, $h) = @_;
1545 1616
1617 delete $self->{ox};
1618
1546 delete $self->{texture} 1619 delete $self->{texture}
1547 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w}; 1620 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1548} 1621}
1549 1622
1550sub set_fontsize { 1623sub set_fontsize {
1552 1625
1553 $self->{fontsize} = $fontsize; 1626 $self->{fontsize} = $fontsize;
1554 delete $self->{texture}; 1627 delete $self->{texture};
1555 1628
1556 $self->realloc; 1629 $self->realloc;
1630}
1631
1632sub reconfigure {
1633 my ($self) = @_;
1634
1635 delete $self->{size_req};
1636
1637 $self->SUPER::reconfigure;
1557} 1638}
1558 1639
1559sub _draw { 1640sub _draw {
1560 my ($self) = @_; 1641 my ($self) = @_;
1561 1642
1567 $self->{layout}->set_width ($self->{w}); 1648 $self->{layout}->set_width ($self->{w});
1568 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1649 $self->{layout}->set_ellipsise ($self->{ellipsise});
1569 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1650 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1570 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1651 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1571 1652
1572 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1653 new_from_layout CFClient::Texture $self->{layout}
1654 };
1573 1655
1656 unless (exists $self->{ox}) {
1574 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1657 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1575 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1658 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1576 : ($self->{w} - $tex->{w}) * 0.5); 1659 : ($self->{w} - $tex->{w}) * 0.5);
1577 1660
1578 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1661 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1579 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1662 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1580 : ($self->{h} - $tex->{h}) * 0.5); 1663 : ($self->{h} - $tex->{h}) * 0.5);
1581
1582 $tex
1583 }; 1664 };
1584 1665
1585 glEnable GL_TEXTURE_2D; 1666 glEnable GL_TEXTURE_2D;
1586 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1667
1668 my $w = List::Util::min $self->{w} + 4, $tex->{w};
1669 my $h = List::Util::min $self->{h} + 2, $tex->{h};
1587 1670
1588 if ($tex->{format} == GL_ALPHA) { 1671 if ($tex->{format} == GL_ALPHA) {
1672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1589 glColor @{$self->{fg}}; 1673 glColor @{$self->{fg}};
1590 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); 1674 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1591 } else { 1675 } else {
1676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1592 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); 1677 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1593 } 1678 }
1594 1679
1595 glDisable GL_TEXTURE_2D; 1680 glDisable GL_TEXTURE_2D;
1596} 1681}
1597 1682
1614 can_hover => 1, 1699 can_hover => 1,
1615 can_focus => 1, 1700 can_focus => 1,
1616 valign => 0, 1701 valign => 0,
1617 can_events => 1, 1702 can_events => 1,
1618 #text => ... 1703 #text => ...
1704 #hidden => "*",
1619 @_ 1705 @_
1620 ) 1706 )
1621} 1707}
1622 1708
1623sub _set_text { 1709sub _set_text {
1625 1711
1626 delete $self->{cur_h}; 1712 delete $self->{cur_h};
1627 1713
1628 return if $self->{text} eq $text; 1714 return if $self->{text} eq $text;
1629 1715
1630 delete $self->{texture};
1631
1632 $self->{last_activity} = $::NOW; 1716 $self->{last_activity} = $::NOW;
1633 $self->{text} = $text; 1717 $self->{text} = $text;
1634 1718
1635 $text =~ s/./*/g if $self->{hidden}; 1719 $text =~ s/./*/g if $self->{hidden};
1636 $self->{layout}->set_text ("$text "); 1720 $self->{layout}->set_text ("$text ");
1721 delete $self->{size_req};
1637 1722
1638 $self->_emit (changed => $self->{text}); 1723 $self->_emit (changed => $self->{text});
1724
1725 $self->realloc;
1726 $self->update;
1639} 1727}
1640 1728
1641sub set_text { 1729sub set_text {
1642 my ($self, $text) = @_; 1730 my ($self, $text) = @_;
1643 1731
1644 $self->{cursor} = length $text; 1732 $self->{cursor} = length $text;
1645 $self->_set_text ($text); 1733 $self->_set_text ($text);
1646
1647 $self->realloc;
1648} 1734}
1649 1735
1650sub get_text { 1736sub get_text {
1651 $_[0]{text} 1737 $_[0]{text}
1652} 1738}
1682 $self->{cursor} = length $text; 1768 $self->{cursor} = length $text;
1683 } elsif ($uni == 27) { 1769 } elsif ($uni == 27) {
1684 $self->_emit ('escape'); 1770 $self->_emit ('escape');
1685 } elsif ($uni) { 1771 } elsif ($uni) {
1686 substr $text, $self->{cursor}++, 0, chr $uni; 1772 substr $text, $self->{cursor}++, 0, chr $uni;
1773 } else {
1774 return 0;
1687 } 1775 }
1688 1776
1689 $self->_set_text ($text); 1777 $self->_set_text ($text);
1690 1778
1691 $self->realloc; 1779 $self->realloc;
1780
1781 1
1692} 1782}
1693 1783
1694sub focus_in { 1784sub focus_in {
1695 my ($self) = @_; 1785 my ($self) = @_;
1696 1786
1711 utf8::encode $text; 1801 utf8::encode $text;
1712 $self->{cursor} = length substr $text, 0, $idx; 1802 $self->{cursor} = length substr $text, 0, $idx;
1713 1803
1714 $self->_set_text ($self->{text}); 1804 $self->_set_text ($self->{text});
1715 $self->update; 1805 $self->update;
1806
1807 1
1716} 1808}
1717 1809
1718sub mouse_motion { 1810sub mouse_motion {
1719 my ($self, $ev, $x, $y) = @_; 1811 my ($self, $ev, $x, $y) = @_;
1720# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1812# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1813
1814 0
1721} 1815}
1722 1816
1723sub _draw { 1817sub _draw {
1724 my ($self) = @_; 1818 my ($self) = @_;
1725 1819
1726 local $self->{fg} = $self->{fg}; 1820 local $self->{fg} = $self->{fg};
1727 1821
1728 if ($FOCUS == $self) { 1822 if ($FOCUS == $self) {
1729 glColor @{$self->{active_bg}}; 1823 glColor_premultiply @{$self->{active_bg}};
1730 $self->{fg} = $self->{active_fg}; 1824 $self->{fg} = $self->{active_fg};
1731 } else { 1825 } else {
1732 glColor @{$self->{bg}}; 1826 glColor_premultiply @{$self->{bg}};
1733 } 1827 }
1734 1828
1735 glEnable GL_BLEND; 1829 glEnable GL_BLEND;
1736 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1830 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1737 glBegin GL_QUADS; 1831 glBegin GL_QUADS;
1738 glVertex 0 , 0; 1832 glVertex 0 , 0;
1739 glVertex 0 , $self->{h}; 1833 glVertex 0 , $self->{h};
1740 glVertex $self->{w}, $self->{h}; 1834 glVertex $self->{w}, $self->{h};
1741 glVertex $self->{w}, 0; 1835 glVertex $self->{w}, 0;
1802 } else { 1896 } else {
1803 $self->set_text ($self->{history_saveback}); 1897 $self->set_text ($self->{history_saveback});
1804 } 1898 }
1805 1899
1806 } else { 1900 } else {
1807 $self->SUPER::key_down ($ev); 1901 return $self->SUPER::key_down ($ev)
1902 }
1903
1808 } 1904 1
1809
1810} 1905}
1811 1906
1812############################################################################# 1907#############################################################################
1813 1908
1814package CFClient::UI::Button; 1909package CFClient::UI::Button;
1843 my ($self, $ev, $x, $y) = @_; 1938 my ($self, $ev, $x, $y) = @_;
1844 1939
1845 $self->emit ("activate") 1940 $self->emit ("activate")
1846 if $x >= 0 && $x < $self->{w} 1941 if $x >= 0 && $x < $self->{w}
1847 && $y >= 0 && $y < $self->{h}; 1942 && $y >= 0 && $y < $self->{h};
1943
1944 1
1848} 1945}
1849 1946
1850sub _draw { 1947sub _draw {
1851 my ($self) = @_; 1948 my ($self) = @_;
1852 1949
1853 local $self->{fg} = $self->{fg}; 1950 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
1854
1855 if ($GRAB == $self) {
1856 $self->{fg} = $self->{active_fg};
1857 }
1858 1951
1859 glEnable GL_TEXTURE_2D; 1952 glEnable GL_TEXTURE_2D;
1860 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1953 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1861 glColor 0, 0, 0, 1; 1954 glColor 0, 0, 0, 1;
1862 1955
1906 1999
1907 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 2000 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1908 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 2001 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1909 $self->{state} = !$self->{state}; 2002 $self->{state} = !$self->{state};
1910 $self->_emit (changed => $self->{state}); 2003 $self->_emit (changed => $self->{state});
2004 } else {
2005 return 0
2006 }
2007
1911 } 2008 1
1912} 2009}
1913 2010
1914sub _draw { 2011sub _draw {
1915 my ($self) = @_; 2012 my ($self) = @_;
1916 2013
2083 2180
2084 my $h1 = $self->{h} * (1 - $ycut1); 2181 my $h1 = $self->{h} * (1 - $ycut1);
2085 my $h2 = $self->{h} * (1 - $ycut2); 2182 my $h2 = $self->{h} * (1 - $ycut2);
2086 2183
2087 glEnable GL_BLEND; 2184 glEnable GL_BLEND;
2088 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2185 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2186 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2089 glEnable GL_TEXTURE_2D; 2187 glEnable GL_TEXTURE_2D;
2090 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2188 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2091 2189
2092 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2190 glBindTexture GL_TEXTURE_2D, $t1->{name};
2093 glBegin GL_QUADS; 2191 glBegin GL_QUADS;
2214sub set_range { 2312sub set_range {
2215 my ($self, $range) = @_; 2313 my ($self, $range) = @_;
2216 2314
2217 ($range, $self->{range}) = ($self->{range}, $range); 2315 ($range, $self->{range}) = ($self->{range}, $range);
2218 2316
2219 $self->update
2220 if "@$range" ne "@{$self->{range}}"; 2317 if ("@$range" ne "@{$self->{range}}") {
2318 $self->update;
2319 $self->set_value ($self->{range}[0]);
2320 }
2221} 2321}
2222 2322
2223sub set_value { 2323sub set_value {
2224 my ($self, $value) = @_; 2324 my ($self, $value) = @_;
2225 2325
2254 2354
2255 $self->SUPER::button_down ($ev, $x, $y); 2355 $self->SUPER::button_down ($ev, $x, $y);
2256 2356
2257 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2357 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2258 2358
2259 $self->mouse_motion ($ev, $x, $y); 2359 $self->mouse_motion ($ev, $x, $y)
2260} 2360}
2261 2361
2262sub mouse_motion { 2362sub mouse_motion {
2263 my ($self, $ev, $x, $y) = @_; 2363 my ($self, $ev, $x, $y) = @_;
2264 2364
2268 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2368 my (undef, $lo, $hi, $page) = @{$self->{range}};
2269 2369
2270 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2370 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2271 2371
2272 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 2372 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2373 } else {
2374 return 0;
2375 }
2376
2273 } 2377 1
2274} 2378}
2275 2379
2276sub update { 2380sub update {
2277 my ($self) = @_; 2381 my ($self) = @_;
2278 2382
2279 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2383 delete $self->{knob_w};
2384 $self->SUPER::update;
2385}
2386
2387sub _draw {
2388 my ($self) = @_;
2389
2390 unless ($self->{knob_w}) {
2280 $self->set_value ($self->{range}[0]); 2391 $self->set_value ($self->{range}[0]);
2281 2392
2282 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2393 my ($value, $lo, $hi, $page) = @{$self->{range}};
2283 my $range = ($hi - $page - $lo) || 1e-100; 2394 my $range = ($hi - $page - $lo) || 1e-100;
2284 2395
2290 $value = ($value - $lo) / $range; 2401 $value = ($value - $lo) / $range;
2291 $value = $value * $self->{scale} + $self->{offset}; 2402 $value = $value * $self->{scale} + $self->{offset};
2292 2403
2293 $self->{knob_x} = $value - $knob_w * 0.5; 2404 $self->{knob_x} = $value - $knob_w * 0.5;
2294 $self->{knob_w} = $knob_w; 2405 $self->{knob_w} = $knob_w;
2295 }); 2406 }
2296
2297 $self->SUPER::update;
2298}
2299
2300sub _draw {
2301 my ($self) = @_;
2302 2407
2303 $self->SUPER::_draw (); 2408 $self->SUPER::_draw ();
2304 2409
2305 glScale $self->{w}, $self->{h}; 2410 glScale $self->{w}, $self->{h};
2306 2411
2367sub set_range { shift->{slider}->set_range (@_) } 2472sub set_range { shift->{slider}->set_range (@_) }
2368sub set_value { shift->{slider}->set_value (@_) } 2473sub set_value { shift->{slider}->set_value (@_) }
2369 2474
2370############################################################################# 2475#############################################################################
2371 2476
2372package CFClient::UI::TextView; 2477package CFClient::UI::TextScroller;
2373 2478
2374our @ISA = CFClient::UI::HBox::; 2479our @ISA = CFClient::UI::HBox::;
2375 2480
2376use CFClient::OpenGL; 2481use CFClient::OpenGL;
2377 2482
2379 my $class = shift; 2484 my $class = shift;
2380 2485
2381 my $self = $class->SUPER::new ( 2486 my $self = $class->SUPER::new (
2382 fontsize => 1, 2487 fontsize => 1,
2383 can_events => 0, 2488 can_events => 0,
2489 indent => 0,
2384 #font => default_font 2490 #font => default_font
2385 @_, 2491 @_,
2386 2492
2387 layout => (new CFClient::Layout 1), 2493 layout => (new CFClient::Layout 1),
2388 par => [], 2494 par => [],
2411 $self->SUPER::size_allocate ($w, $h); 2517 $self->SUPER::size_allocate ($w, $h);
2412 2518
2413 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2519 $self->{layout}->set_font ($self->{font}) if $self->{font};
2414 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2520 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2415 $self->{layout}->set_width ($self->{children}[0]{w}); 2521 $self->{layout}->set_width ($self->{children}[0]{w});
2522 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2416 2523
2417 $self->reflow; 2524 $self->reflow;
2418} 2525}
2419 2526
2420sub text_size { 2527sub text_size {
2422 2529
2423 my $layout = $self->{layout}; 2530 my $layout = $self->{layout};
2424 2531
2425 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2532 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2426 $layout->set_width ($self->{children}[0]{w} - $indent); 2533 $layout->set_width ($self->{children}[0]{w} - $indent);
2534 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2427 $layout->set_markup ($text); 2535 $layout->set_markup ($text);
2428 2536
2429 $layout->size 2537 $layout->size
2430} 2538}
2431 2539
2470 2578
2471 return unless $self->{h} > 0; 2579 return unless $self->{h} > 0;
2472 2580
2473 delete $self->{texture}; 2581 delete $self->{texture};
2474 2582
2475 $ROOT->on_post_alloc ($self, sub { 2583 $ROOT->on_post_alloc ($self => sub {
2476 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2584 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2477 2585
2478 if (delete $self->{need_reflow}) { 2586 if (delete $self->{need_reflow}) {
2479 my $height = 0; 2587 my $height = 0;
2480 2588
2483 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2591 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2484 2592
2485 for (@{$self->{par}}) { 2593 for (@{$self->{par}}) {
2486 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2594 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2487 $layout->set_width ($W - $_->[3]); 2595 $layout->set_width ($W - $_->[3]);
2596 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2488 $layout->set_markup ($_->[4]); 2597 $layout->set_markup ($_->[4]);
2489 my ($w, $h) = $layout->size; 2598 my ($w, $h) = $layout->size;
2490 $_->[0] = $w + $_->[3]; 2599 $_->[0] = $w + $_->[3];
2491 $_->[1] = $h; 2600 $_->[1] = $h;
2492 } 2601 }
2495 } 2604 }
2496 2605
2497 $self->{height} = $height; 2606 $self->{height} = $height;
2498 2607
2499 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2608 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2500 2609
2501 delete $self->{texture}; 2610 delete $self->{texture};
2502 } 2611 }
2503 2612
2504 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2613 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2505 glClearColor 0.5, 0.5, 0.5, 0; 2614 glClearColor 0, 0, 0, 0;
2506 glClear GL_COLOR_BUFFER_BIT; 2615 glClear GL_COLOR_BUFFER_BIT;
2507 2616
2508 my $top = int $self->{children}[1]{range}[0]; 2617 my $top = int $self->{children}[1]{range}[0];
2509 2618
2510 my $y0 = $top; 2619 my $y0 = $top;
2524 my $h = $par->[1]; 2633 my $h = $par->[1];
2525 2634
2526 if ($y0 < $y + $h && $y < $y1) { 2635 if ($y0 < $y + $h && $y < $y1) {
2527 $layout->set_foreground (@{ $par->[2] }); 2636 $layout->set_foreground (@{ $par->[2] });
2528 $layout->set_width ($W - $par->[3]); 2637 $layout->set_width ($W - $par->[3]);
2638 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2529 $layout->set_markup ($par->[4]); 2639 $layout->set_markup ($par->[4]);
2530 2640
2531 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2641 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2532 2642
2533 glRasterPos $par->[3], $y - $y0; 2643 glRasterPos $par->[3], $y - $y0;
2545sub _draw { 2655sub _draw {
2546 my ($self) = @_; 2656 my ($self) = @_;
2547 2657
2548 glEnable GL_TEXTURE_2D; 2658 glEnable GL_TEXTURE_2D;
2549 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2659 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2550 glColor 1, 1, 1, 1; 2660 glColor 0, 0, 0, 1;
2551 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2661 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2552 glDisable GL_TEXTURE_2D; 2662 glDisable GL_TEXTURE_2D;
2553 2663
2554 $self->{children}[1]->draw; 2664 $self->{children}[1]->draw;
2555 2665
2556} 2666}
2647 $tooltip .= "\n\n" . (ref $widget) . "\n" 2757 $tooltip .= "\n\n" . (ref $widget) . "\n"
2648 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 2758 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2649 . "req $widget->{req_w} $widget->{req_h}\n" 2759 . "req $widget->{req_w} $widget->{req_h}\n"
2650 . "visible $widget->{visible}"; 2760 . "visible $widget->{visible}";
2651 } 2761 }
2762
2763 $tooltip =~ s/^\n+//;
2764 $tooltip =~ s/\n+$//;
2652 2765
2653 $self->add (new CFClient::UI::Label 2766 $self->add (new CFClient::UI::Label
2654 markup => $tooltip, 2767 markup => $tooltip,
2655 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2768 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2656 fontsize => 0.8, 2769 fontsize => 0.8,
2684 or return; 2797 or return;
2685 2798
2686 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 2799 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2687 2800
2688 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 2801 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2689 if $x + $self->{w} > $::WIDTH; 2802 if $x + $self->{w} > $self->{root}{w};
2690 2803
2691 $self->move_abs ($x, $y); 2804 $self->move_abs ($x, $y);
2692 }); 2805 });
2693} 2806}
2694 2807
2783 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 2896 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2784 2897
2785 if ($tex) { 2898 if ($tex) {
2786 glEnable GL_TEXTURE_2D; 2899 glEnable GL_TEXTURE_2D;
2787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2900 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2788 glColor 1, 1, 1, 1; 2901 glColor 0, 0, 0, 1;
2789 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2902 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2790 glDisable GL_TEXTURE_2D; 2903 glDisable GL_TEXTURE_2D;
2791 } 2904 }
2792} 2905}
2793 2906
2794sub DESTROY { 2907sub destroy {
2795 my ($self) = @_; 2908 my ($self) = @_;
2796 2909
2797 $self->{timer}->cancel 2910 $self->{timer}->cancel
2798 if $self->{timer}; 2911 if $self->{timer};
2799 2912
2800 $self->SUPER::DESTROY; 2913 $self->SUPER::destroy;
2801} 2914}
2915
2916#############################################################################
2917
2918package CFClient::UI::Buttonbar;
2919
2920our @ISA = CFClient::UI::HBox::;
2921
2922# TODO: should actualyl wrap buttons and other goodies.
2802 2923
2803############################################################################# 2924#############################################################################
2804 2925
2805package CFClient::UI::Menu; 2926package CFClient::UI::Menu;
2806 2927
2818 ); 2939 );
2819 2940
2820 $self->add ($self->{vbox} = new CFClient::UI::VBox); 2941 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2821 2942
2822 for my $item (@{ $self->{items} }) { 2943 for my $item (@{ $self->{items} }) {
2823 my ($widget, $cb) = @$item; 2944 my ($widget, $cb, $tooltip) = @$item;
2824 2945
2825 # handle various types of items, only text for now 2946 # handle various types of items, only text for now
2826 if (!ref $widget) { 2947 if (!ref $widget) {
2827 $widget = new CFClient::UI::Label 2948 $widget = new CFClient::UI::Label
2828 can_hover => 1, 2949 can_hover => 1,
2829 can_events => 1, 2950 can_events => 1,
2830 text => $widget; 2951 markup => $widget,
2952 tooltip => $tooltip
2831 } 2953 }
2832 2954
2833 $self->{item}{$widget} = $item; 2955 $self->{item}{$widget} = $item;
2834 2956
2835 $self->{vbox}->add ($widget); 2957 $self->{vbox}->add ($widget);
2856 my ($self, $ev, $x, $y) = @_; 2978 my ($self, $ev, $x, $y) = @_;
2857 2979
2858 # TODO: should use vbox->find_widget or so 2980 # TODO: should use vbox->find_widget or so
2859 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2981 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2860 $self->{hover} = $self->{item}{$HOVER}; 2982 $self->{hover} = $self->{item}{$HOVER};
2983
2984 0
2861} 2985}
2862 2986
2863sub button_up { 2987sub button_up {
2864 my ($self, $ev, $x, $y) = @_; 2988 my ($self, $ev, $x, $y) = @_;
2865 2989
2867 undef $GRAB; 2991 undef $GRAB;
2868 $self->hide; 2992 $self->hide;
2869 2993
2870 $self->_emit ("popdown"); 2994 $self->_emit ("popdown");
2871 $self->{hover}[1]->() if $self->{hover}; 2995 $self->{hover}[1]->() if $self->{hover};
2996 } else {
2997 return 0
2998 }
2999
2872 } 3000 1
2873} 3001}
2874 3002
2875############################################################################# 3003#############################################################################
2876 3004
2877package CFClient::UI::Statusbox; 3005package CFClient::UI::Multiplexer;
2878 3006
2879our @ISA = CFClient::UI::VBox::; 3007our @ISA = CFClient::UI::Container::;
2880 3008
2881sub new { 3009sub new {
2882 my $class = shift; 3010 my $class = shift;
2883 3011
2884 $class->SUPER::new ( 3012 my $self = $class->SUPER::new (
3013 @_,
3014 );
3015
3016 $self->{current} = $self->{children}[0]
3017 if @{ $self->{children} };
3018
3019 $self
3020}
3021
3022sub add {
3023 my ($self, @widgets) = @_;
3024
3025 $self->SUPER::add (@widgets);
3026
3027 $self->{current} = $self->{children}[0]
3028 if @{ $self->{children} };
3029}
3030
3031sub set_current_page {
3032 my ($self, $page_or_widget) = @_;
3033
3034 my $widget = ref $page_or_widget
3035 ? $page_or_widget
3036 : $self->{children}[$page_or_widget];
3037
3038 $self->{current} = $widget;
3039 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3040
3041 $self->_emit (page_changed => $self->{current});
3042
3043 $self->realloc;
3044}
3045
3046sub visible_children {
3047 $_[0]{current}
3048}
3049
3050sub size_request {
3051 my ($self) = @_;
3052
3053 $self->{current}->size_request
3054}
3055
3056sub size_allocate {
3057 my ($self, $w, $h) = @_;
3058
3059 $self->{current}->configure (0, 0, $w, $h);
3060}
3061
3062sub _draw {
3063 my ($self) = @_;
3064
3065 $self->{current}->draw;
3066}
3067
3068#############################################################################
3069
3070package CFClient::UI::Notebook;
3071
3072our @ISA = CFClient::UI::VBox::;
3073
3074sub new {
3075 my $class = shift;
3076
3077 my $self = $class->SUPER::new (
3078 buttonbar => (new CFClient::UI::Buttonbar),
3079 multiplexer => (new CFClient::UI::Multiplexer expand => 1),
3080 # filter => # will be put between multiplexer and $self
3081 @_,
3082 );
3083
3084 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3085 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3086
3087 $self
3088}
3089
3090sub add {
3091 my ($self, $title, $widget, $tooltip) = @_;
3092
3093 Scalar::Util::weaken $self;
3094
3095 $self->{buttonbar}->add (new CFClient::UI::Button
3096 markup => $title,
3097 tooltip => $tooltip,
3098 on_activate => sub { $self->set_current_page ($widget) },
3099 );
3100
3101 $self->{multiplexer}->add ($widget);
3102}
3103
3104sub set_current_page {
3105 my ($self, $page) = @_;
3106
3107 $self->{multiplexer}->set_current_page ($page);
3108 $self->_emit (page_changed => $self->{multiplexer}{current});
3109}
3110
3111#############################################################################
3112
3113package CFClient::UI::Combobox;
3114
3115use utf8;
3116
3117our @ISA = CFClient::UI::Button::;
3118
3119sub new {
3120 my $class = shift;
3121
3122 my $self = $class->SUPER::new (
3123 options => [], # [value, title, longdesc], ...
3124 value => undef,
3125 @_,
3126 );
3127
3128 $self->_set_value ($self->{value});
3129
3130 $self
3131}
3132
3133sub button_down {
3134 my ($self, $ev) = @_;
3135
3136 my @menu_items;
3137
3138 for (@{ $self->{options} }) {
3139 my ($value, $title, $tooltip) = @$_;
3140
3141 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3142 }
3143
3144 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
3145}
3146
3147sub _set_value {
3148 my ($self, $value) = @_;
3149
3150 my ($item) = grep $_->[0] eq $value, @{ $self->{options} }
3151 or return;
3152
3153 $self->{value} = $item->[0];
3154 $self->set_markup ("$item->[1] ⇓");
3155 $self->set_tooltip ($item->[2]);
3156}
3157
3158sub set_value {
3159 my ($self, $value) = @_;
3160
3161 return unless $self->{value} ne $value;
3162
3163 $self->_set_value ($value);
3164 $self->_emit (changed => $value);
3165}
3166
3167#############################################################################
3168
3169package CFClient::UI::Statusbox;
3170
3171our @ISA = CFClient::UI::VBox::;
3172
3173sub new {
3174 my $class = shift;
3175
3176 my $self = $class->SUPER::new (
2885 fontsize => 0.8, 3177 fontsize => 0.8,
2886 @_, 3178 @_,
2887 ) 3179 );
3180
3181 Scalar::Util::weaken (my $this = $self);
3182
3183 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3184
3185 $self
2888} 3186}
2889 3187
2890sub reorder { 3188sub reorder {
2891 my ($self) = @_; 3189 my ($self) = @_;
2892 my $NOW = time; 3190 my $NOW = Time::HiRes::time;
3191
3192 # freeze display when hovering over any label
3193 return if $CFClient::UI::TOOLTIP->{owner}
3194 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label},
3195 values %{ $self->{item} };
2893 3196
2894 while (my ($k, $v) = each %{ $self->{item} }) { 3197 while (my ($k, $v) = each %{ $self->{item} }) {
2895 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3198 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2896 } 3199 }
2897 3200
2900 my @items = sort { 3203 my @items = sort {
2901 $a->{pri} <=> $b->{pri} 3204 $a->{pri} <=> $b->{pri}
2902 or $b->{id} <=> $a->{id} 3205 or $b->{id} <=> $a->{id}
2903 } values %{ $self->{item} }; 3206 } values %{ $self->{item} };
2904 3207
3208 $self->{timer}->interval (1);
3209
2905 my $count = 10 + 1; 3210 my $count = 10 + 1;
2906 for my $item (@items) { 3211 for my $item (@items) {
2907 last unless --$count; 3212 last unless --$count;
2908 3213
2909 push @widgets, $item->{label} ||= do { 3214 my $label = $item->{label} ||= do {
2910 # TODO: doesn't handle markup well (read as: at all) 3215 # TODO: doesn't handle markup well (read as: at all)
2911 my $short = $item->{count} > 1 3216 my $short = $item->{count} > 1
2912 ? "<b>$item->{count} ×</b> $item->{text}" 3217 ? "<b>$item->{count} ×</b> $item->{text}"
2913 : $item->{text}; 3218 : $item->{text};
2914 3219
2922 tooltip => $item->{tooltip}, 3227 tooltip => $item->{tooltip},
2923 tooltip_font => $::FONT_PROP, 3228 tooltip_font => $::FONT_PROP,
2924 tooltip_width => 0.67, 3229 tooltip_width => 0.67,
2925 fontsize => $item->{fontsize} || $self->{fontsize}, 3230 fontsize => $item->{fontsize} || $self->{fontsize},
2926 max_w => $::WIDTH * 0.44, 3231 max_w => $::WIDTH * 0.44,
2927 fg => $item->{fg}, 3232 fg => [@{ $item->{fg} }],
2928 can_events => 1, 3233 can_events => 1,
2929 can_hover => 1 3234 can_hover => 1
2930 }; 3235 };
3236
3237 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3238 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3239 $label->update;
3240 $label->set_max_size (undef, $label->{req_h} * $diff)
3241 if $diff < 1;
3242 $self->{timer}->interval (1/30);
3243 } else {
3244 $label->{fg}[3] = $item->{fg}[3] || 1;
3245 }
3246
3247 push @widgets, $label;
2931 } 3248 }
2932 3249
2933 $self->clear; 3250 $self->clear;
2934 $self->SUPER::add (reverse @widgets); 3251 $self->SUPER::add (reverse @widgets);
2935} 3252}
2940 $text =~ s/^\s+//; 3257 $text =~ s/^\s+//;
2941 $text =~ s/\s+$//; 3258 $text =~ s/\s+$//;
2942 3259
2943 return unless $text; 3260 return unless $text;
2944 3261
2945 my $timeout = time + ((delete $arg{timeout}) || 60); 3262 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
2946 3263
2947 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 3264 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2948 3265
2949 if (my $item = $self->{item}{$group}) { 3266 if (my $item = $self->{item}{$group}) {
2950 if ($item->{text} eq $text) { 3267 if ($item->{text} eq $text) {
2951 $item->{count}++; 3268 $item->{count}++;
2952 } else { 3269 } else {
2953 $item->{count} = 1; 3270 $item->{count} = 1;
2954 $item->{text} = $item->{tooltip} = $text; 3271 $item->{text} = $item->{tooltip} = $text;
2955 } 3272 }
2956 $item->{id} = ++$self->{id}; 3273 $item->{id} += 0.2;#d#
2957 $item->{timeout} = $timeout; 3274 $item->{timeout} = $timeout;
2958 delete $item->{label}; 3275 delete $item->{label};
2959 } else { 3276 } else {
2960 $self->{item}{$group} = { 3277 $self->{item}{$group} = {
2961 id => ++$self->{id}, 3278 id => ++$self->{id},
2980 3297
2981 $self->reorder; 3298 $self->reorder;
2982 $self->SUPER::reconfigure; 3299 $self->SUPER::reconfigure;
2983} 3300}
2984 3301
3302sub destroy {
3303 my ($self) = @_;
3304
3305 $self->{timer}->cancel;
3306
3307 $self->SUPER::destroy;
3308}
3309
2985############################################################################# 3310#############################################################################
2986 3311
2987package CFClient::UI::Inventory; 3312package CFClient::UI::Inventory;
2988 3313
2989our @ISA = CFClient::UI::ScrolledWindow::; 3314our @ISA = CFClient::UI::ScrolledWindow::;
2990 3315
2991sub new { 3316sub new {
2992 my $class = shift; 3317 my $class = shift;
2993 3318
2994 my $self = $class->SUPER::new ( 3319 my $self = $class->SUPER::new (
2995 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), 3320 child => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2996 @_, 3321 @_,
2997 ); 3322 );
2998 3323
2999 $self 3324 $self
3000} 3325}
3001 3326
3002sub set_items { 3327sub set_items {
3003 my ($self, $items) = @_; 3328 my ($self, $items) = @_;
3004 3329
3005 $self->{scrolled}->clear; 3330 $self->{child}->clear;
3006 return unless $items; 3331 return unless $items;
3007 3332
3008 my @items = sort { 3333 my @items = sort {
3009 ($a->{type} <=> $b->{type}) 3334 ($a->{type} <=> $b->{type})
3010 or ($a->{name} cmp $b->{name}) 3335 or ($a->{name} cmp $b->{name})
3014 3339
3015 my $row = 0; 3340 my $row = 0;
3016 for my $item (@items) { 3341 for my $item (@items) {
3017 CFClient::Item::update_widgets $item; 3342 CFClient::Item::update_widgets $item;
3018 3343
3019 $self->{scrolled}->add (0, $row, $item->{face_widget}); 3344 $self->{child}->add (0, $row, $item->{face_widget});
3020 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 3345 $self->{child}->add (1, $row, $item->{desc_widget});
3021 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 3346 $self->{child}->add (2, $row, $item->{weight_widget});
3022 3347
3023 $row++; 3348 $row++;
3024 } 3349 }
3025} 3350}
3026 3351
3124 $rec = $::CONN->stop_record if $::CONN; 3449 $rec = $::CONN->stop_record if $::CONN;
3125 return unless ref $rec eq 'ARRAY'; 3450 return unless ref $rec eq 'ARRAY';
3126 $self->set_command_list ($rec); 3451 $self->set_command_list ($rec);
3127} 3452}
3128 3453
3129# if $commit is true, the binding will be set after the user entered a key combo 3454
3455sub ask_for_bind_and_commit {
3456 my ($self) = @_;
3457 $self->ask_for_bind (1);
3458}
3459
3130sub ask_for_bind { 3460sub ask_for_bind {
3131 my ($self, $commit) = @_; 3461 my ($self, $commit) = @_;
3132 3462
3133 CFClient::Binder::open_binding_dialog (sub { 3463 CFClient::Binder::open_binding_dialog (sub {
3134 my ($mod, $sym) = @_; 3464 my ($mod, $sym) = @_;
3218 3548
3219############################################################################# 3549#############################################################################
3220 3550
3221package CFClient::UI::SpellList; 3551package CFClient::UI::SpellList;
3222 3552
3223our @ISA = CFClient::UI::FancyFrame::; 3553our @ISA = CFClient::UI::Table::;
3224 3554
3225sub new { 3555sub new {
3226 my $class = shift; 3556 my $class = shift;
3227 3557
3228 my $self = $class->SUPER::new (binding => [], commands => [], @_); 3558 my $self = $class->SUPER::new (
3229 3559 binding => [],
3230 $self->add (new CFClient::UI::ScrolledWindow 3560 commands => [],
3231 scrolled => $self->{spellbox} = new CFClient::UI::Table); 3561 @_,
3232 3562 )
3233 $self;
3234} 3563}
3235 3564
3236# XXX: Do sorting? Argl... 3565my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3566
3567my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3568 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3569my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3570 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3571my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3572 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3573my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3574 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3575my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3576 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3577
3578sub rebuild_spell_list {
3579 my ($self) = @_;
3580
3581 $CFClient::UI::ROOT->on_refresh ($self => sub {
3582 $self->clear;
3583
3584 return unless $::CONN;
3585
3586 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3587 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3588 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3589 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3590 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3591
3592 my $row = 0;
3593
3594 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3595 my $spell = $self->{spell}{$_};
3596
3597 $row++;
3598
3599 my $spell_cb = sub {
3600 my ($widget, $ev) = @_;
3601
3602 if ($ev->{button} == 1) {
3603 $::CONN->user_send ("cast $spell->{name}");
3604 } elsif ($ev->{button} == 2) {
3605 $::CONN->user_send ("invoke $spell->{name}");
3606 } elsif ($ev->{button} == 3) {
3607 (new CFClient::UI::Menu
3608 items => [
3609 ["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}"]) }],
3611 ],
3612 )->popup ($ev);
3613 } else {
3614 return 0;
3615 }
3616
3617 1
3618 };
3619
3620 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3621
3622 #TODO: add path info to tooltip
3623 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3624
3625 $self->add (0, $row, new CFClient::UI::Face
3626 face => $spell->{face},
3627 can_hover => 1,
3628 can_events => 1,
3629 tooltip => $tooltip,
3630 on_button_down => $spell_cb,
3631 );
3632
3633 $self->add (1, $row, new CFClient::UI::Label
3634 expand => 1,
3635 text => $spell->{name},
3636 can_hover => 1,
3637 can_events => 1,
3638 tooltip => $tooltip,
3639 on_button_down => $spell_cb,
3640 );
3641
3642 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3643 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3644 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3645 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3646 }
3647 });
3648}
3649
3237sub add_spell { 3650sub add_spell {
3238 my ($self, $spell) = @_; 3651 my ($self, $spell) = @_;
3652
3239 $self->{spells}->{$spell->{name}} = $spell; 3653 $self->{spell}->{$spell->{name}} = $spell;
3240 3654 $self->rebuild_spell_list;
3241 $self->{spellbox}->add (0, $self->{tbl_idx}, new CFClient::UI::Face
3242 face => $spell->{face},
3243 can_hover => 1,
3244 can_events => 1,
3245 tooltip => $spell->{message});
3246
3247 $self->{spellbox}->add (1, $self->{tbl_idx}, new CFClient::UI::Label
3248 text => $spell->{name},
3249 can_hover => 1,
3250 can_events => 1,
3251 tooltip => $spell->{message},
3252 expand => 1);
3253
3254 $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label
3255 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3256 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3257 expand => 1);
3258
3259 $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button
3260 text => "bind to key",
3261 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) });
3262}
3263
3264sub rebuild_spell_list {
3265 my ($self) = @_;
3266 $self->{tbl_idx} = 0;
3267 $self->add_spell ($_) for values %{$self->{spells}};
3268} 3655}
3269 3656
3270sub remove_spell { 3657sub remove_spell {
3271 my ($self, $spell) = @_; 3658 my ($self, $spell) = @_;
3659
3272 delete $self->{spells}->{$spell->{name}}; 3660 delete $self->{spell}->{$spell->{name}};
3273 $self->rebuild_spell_list; 3661 $self->rebuild_spell_list;
3274} 3662}
3275 3663
3664sub clear_spells {
3665 my ($self) = @_;
3666
3667 $self->{spell} = {};
3668 $self->rebuild_spell_list;
3669}
3670
3276############################################################################# 3671#############################################################################
3277 3672
3278package CFClient::UI::Root; 3673package CFClient::UI::Root;
3279 3674
3280our @ISA = CFClient::UI::Container::; 3675our @ISA = CFClient::UI::Container::;
3676
3677use List::Util qw(min max);
3281 3678
3282use CFClient::OpenGL; 3679use CFClient::OpenGL;
3283 3680
3284sub new { 3681sub new {
3285 my $class = shift; 3682 my $class = shift;
3420 3817
3421 delete $queue{$widget+0}; 3818 delete $queue{$widget+0};
3422 3819
3423 my ($w, $h) = $widget->size_request; 3820 my ($w, $h) = $widget->size_request;
3424 3821
3425 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; 3822 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3426 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; 3823 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3824
3825 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3826 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3427 3827
3428 $w = $widget->{force_w} if exists $widget->{force_w}; 3828 $w = $widget->{force_w} if exists $widget->{force_w};
3429 $h = $widget->{force_h} if exists $widget->{force_h}; 3829 $h = $widget->{force_h} if exists $widget->{force_h};
3430 3830
3431 if ($widget->{req_w} != $w || $widget->{req_h} != $h 3831 if ($widget->{req_w} != $w || $widget->{req_h} != $h

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines