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.402 by root, Sat Jul 21 20:49:44 2007 UTC vs.
Revision 1.447 by root, Sat Dec 22 06:50:45 2007 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use CFPlus;
10use CFPlus::Pod; 9use CFPlus::Pod;
11use CFPlus::Texture; 10use CFPlus::Texture;
12 11
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 33
33 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
34 35
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 37 $TOOLTIP->show;
41 } 38 }
42 39
43 return; 40 return;
46 } 43 }
47 44
48 $TOOLTIP->hide; 45 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
51}); 48};
52 49
53sub get_layout { 50sub get_layout {
54 my $layout; 51 my $layout;
55 52
56 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
92 89
93 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 92
96 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
97 } 94 }
98} 95}
99 96
100sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
101 my ($ev) = @_; 98 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
108 105
109 $GRAB = $widget; 106 $GRAB = $widget;
110 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
111 108
112 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
113 } 110 }
114 111
115 if ($GRAB) { 112 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 114 # mousewheel
142 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 140 $grab->update if $grab;
144 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
145 142
146 check_hover $widget; 143 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
152 my ($ev) = @_; 149 my ($ev) = @_;
273 $self->emit ("destroy"); 270 $self->emit ("destroy");
274 %$self = (); 271 %$self = ();
275} 272}
276 273
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub show {
282 my ($self) = @_; 279 my ($self) = @_;
283 280
312 delete $self->{root}; 309 delete $self->{root};
313 310
314 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
316 313
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $CFPlus::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
319 316
320 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
322} 319}
362 359
363 $self->{force_w} = $w; 360 $self->{force_w} = $w;
364 $self->{force_h} = $h; 361 $self->{force_h} = $h;
365 362
366 $self->realloc; 363 $self->realloc;
364}
365
366# traverse the widget chain up to find the maximum "physical" size constraints
367sub get_max_wh {
368 my ($self) = @_;
369
370 return $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT)
367} 374}
368 375
369sub size_request { 376sub size_request {
370 require Carp; 377 require Carp;
371 Carp::confess "size_request is abstract"; 378 Carp::confess "size_request is abstract";
433 440
434 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
435 442
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
439 } 446 }
440} 447}
441 448
442# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
443sub coord2local { 450sub coord2local {
444 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
445 452
446 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
447 454
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 456}
450 457
451# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
452sub coord2global { 459sub coord2global {
453 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
454 461
455 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
456 463
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 465}
459 466
460sub invoke_focus_in { 467sub invoke_focus_in {
593 600
594# using global variables seems a bit hacky, but passing through all drawing 601# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 602# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 603our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 604
598sub draw { 605#sub draw {
599 my ($self) = @_; 606#CFPlus.xs
600
601 return unless $self->{h} && $self->{w};
602
603 # update screen rectangle
604 local $draw_x = $draw_x + $self->{x};
605 local $draw_y = $draw_y + $self->{y};
606
607 # skip widgets that are entirely outside the drawing area
608 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
609 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
610
611 glPushMatrix;
612 glTranslate $self->{x}, $self->{y}, 0;
613
614 if ($self == $HOVER && $self->{can_hover}) {
615 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
616 glEnable GL_BLEND;
617 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
618 glBegin GL_QUADS;
619 glVertex 0 , 0;
620 glVertex $self->{w}, 0;
621 glVertex $self->{w}, $self->{h};
622 glVertex 0 , $self->{h};
623 glEnd;
624 glDisable GL_BLEND;
625 }
626
627 if ($ENV{CFPLUS_DEBUG} & 1) {
628 glPushMatrix;
629 glColor 1, 1, 0, 1;
630 glTranslate 0.375, 0.375;
631 glBegin GL_LINE_LOOP;
632 glVertex 0 , 0;
633 glVertex $self->{w} - 1, 0;
634 glVertex $self->{w} - 1, $self->{h} - 1;
635 glVertex 0 , $self->{h} - 1;
636 glEnd;
637 glPopMatrix;
638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
639 }
640
641 $self->_draw;
642 glPopMatrix;
643}
644 607
645sub _draw { 608sub _draw {
646 my ($self) = @_; 609 my ($self) = @_;
647 610
648 warn "no draw defined for $self\n"; 611 warn "no draw defined for $self\n";
651sub DESTROY { 614sub DESTROY {
652 my ($self) = @_; 615 my ($self) = @_;
653 616
654 return if CFPlus::in_destruct; 617 return if CFPlus::in_destruct;
655 618
619 local $@;
656 eval { $self->destroy }; 620 eval { $self->destroy };
657 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
658 622
659 delete $WIDGET{$self+0}; 623 delete $WIDGET{$self+0};
660} 624}
668use strict; 632use strict;
669use CFPlus::OpenGL; 633use CFPlus::OpenGL;
670 634
671sub new { 635sub new {
672 my $class = shift; 636 my $class = shift;
673
674 # range [value, low, high, page]
675 637
676 $class->SUPER::new ( 638 $class->SUPER::new (
677 #bg => [0, 0, 0, 0.2], 639 #bg => [0, 0, 0, 0.2],
678 #active_bg => [1, 1, 1, 0.5], 640 #active_bg => [1, 1, 1, 0.5],
679 @_ 641 @_
691 my ($w, $h) = @$self{qw(w h)}; 653 my ($w, $h) = @$self{qw(w h)};
692 654
693 glEnable GL_BLEND; 655 glEnable GL_BLEND;
694 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 656 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
695 glColor_premultiply @$color; 657 glColor_premultiply @$color;
696
697 glBegin GL_QUADS;
698 glVertex 0 , 0;
699 glVertex 0 , $h;
700 glVertex $w, $h; 658 glRect 0, 0, $w, $h;
701 glVertex $w, 0;
702 glEnd;
703
704 glDisable GL_BLEND; 659 glDisable GL_BLEND;
705 } 660 }
706} 661}
707 662
708############################################################################# 663#############################################################################
769 @{$self->{children}}, @widgets 724 @{$self->{children}}, @widgets
770 ]; 725 ];
771 726
772 $self->realloc; 727 $self->realloc;
773 728
729 $self->emit (c_add => \@widgets);
730
774 map $_+0, @widgets 731 map $_+0, @widgets
775} 732}
776 733
777sub children { 734sub children {
778 @{ $_[0]{children} } 735 @{ $_[0]{children} }
779} 736}
780 737
781sub remove { 738sub remove {
782 my ($self, $child) = @_; 739 my ($self, @widgets) = @_;
783 740
741 $self->emit (c_remove => \@widgets);
742
743 for my $child (@widgets) {
784 delete $child->{parent}; 744 delete $child->{parent};
785 $child->hide; 745 $child->hide;
786
787 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 746 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
747 }
788 748
789 $self->realloc; 749 $self->realloc;
790} 750}
791 751
792sub clear { 752sub clear {
820} 780}
821 781
822sub _draw { 782sub _draw {
823 my ($self) = @_; 783 my ($self) = @_;
824 784
825 $_->draw for @{$self->{children}}; 785 $_->draw for $self->visible_children;
826} 786}
827 787
828############################################################################# 788#############################################################################
829 789
830package CFPlus::UI::Bin; 790package CFPlus::UI::Bin;
840} 800}
841 801
842sub add { 802sub add {
843 my ($self, $child) = @_; 803 my ($self, $child) = @_;
844 804
845 $self->SUPER::remove ($_) for @{ $self->{children} }; 805 $self->clear;
846 $self->SUPER::add ($child); 806 $self->SUPER::add ($child);
847} 807}
848 808
849sub remove { 809sub remove {
850 my ($self, $widget) = @_; 810 my ($self, $widget) = @_;
868 828
869 1 829 1
870} 830}
871 831
872############################################################################# 832#############################################################################
873
874# back-buffered drawing area 833# back-buffered drawing area
875 834
876package CFPlus::UI::Window; 835package CFPlus::UI::Window;
877 836
878our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
992 $self->{view_x} = $x; 951 $self->{view_x} = $x;
993 $self->{view_y} = $y; 952 $self->{view_y} = $y;
994 953
995 $self->emit (changed => $x, $y); 954 $self->emit (changed => $x, $y);
996 $self->update; 955 $self->update;
956 }
957}
958
959sub set_center {
960 my ($self, $x, $y) = @_;
961
962 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
963}
964
965sub make_visible {
966 my ($self, $x, $y, $border) = @_;
967
968 if ( $x < $self->{view_x} + $self->{w} * $border
969 || $x > $self->{view_x} + $self->{w} * (1 - $border)
970 || $y < $self->{view_y} + $self->{h} * $border
971 || $y > $self->{view_y} + $self->{h} * (1 - $border)
972 ) {
973 $self->set_center ($x, $y);
997 } 974 }
998} 975}
999 976
1000# hmm, this does not work for topleft of $self... but we should not ask for that 977# hmm, this does not work for topleft of $self... but we should not ask for that
1001sub coord2local { 978sub coord2local {
1048 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
1049 1026
1050 my $self; 1027 my $self;
1051 1028
1052 my $hslider = new CFPlus::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1053 col => 0, 1030 c_col => 0,
1054 row => 1, 1031 c_row => 1,
1055 vertical => 0, 1032 vertical => 0,
1056 range => [0, 0, 1, 0.01], # HACK fix 1033 range => [0, 0, 1, 0.01], # HACK fix
1057 on_changed => sub { 1034 on_changed => sub {
1058 $self->{hpos} = $_[1]; 1035 $self->{hpos} = $_[1];
1059 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1060 }, 1037 },
1061 ; 1038 ;
1062 1039
1063 my $vslider = new CFPlus::UI::Slider 1040 my $vslider = new CFPlus::UI::Slider
1064 col => 1, 1041 c_col => 1,
1065 row => 0, 1042 c_row => 0,
1066 vertical => 1, 1043 vertical => 1,
1067 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1068 on_changed => sub { 1045 on_changed => sub {
1069 $self->{vpos} = $_[1]; 1046 $self->{vpos} = $_[1];
1070 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1081 row_expand => [1, 0], 1058 row_expand => [1, 0],
1082 %arg, 1059 %arg,
1083 ); 1060 );
1084 1061
1085 $self->{vp} = new CFPlus::UI::ViewPort 1062 $self->{vp} = new CFPlus::UI::ViewPort
1086 col => 0, 1063 c_col => 0,
1087 row => 0, 1064 c_row => 0,
1088 expand => 1, 1065 expand => 1,
1089 scroll_x => $self->{scroll_x}, 1066 scroll_x => $self->{scroll_x},
1090 scroll_y => $self->{scroll_y}, 1067 scroll_y => $self->{scroll_y},
1091 on_changed => sub { 1068 on_changed => sub {
1092 my ($vp, $x, $y) = @_; 1069 my ($vp, $x, $y) = @_;
1114 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1115 1092
1116 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1117} 1094}
1118 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1119sub update_slider { 1100sub update_slider {
1120 my ($self) = @_; 1101 my ($self) = @_;
1121 1102
1122 my $child = ($self->{vp} or return)->child; 1103 my $child = ($self->{vp} or return)->child;
1123 1104
1236 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1237 1218
1238 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1239 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1240 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1241
1242 glBegin GL_QUADS;
1243 glVertex 0 , 0;
1244 glVertex 0 , $h;
1245 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1246 glVertex $w, 0;
1247 glEnd;
1248
1249 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1250 } 1224 }
1251 1225
1252 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1253} 1227}
1414 1388
1415sub border { 1389sub border {
1416 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1417} 1391}
1418 1392
1393sub get_max_wh {
1394 my ($self) = @_;
1395
1396 return ($self->{w}, $self->{h})
1397 if $self->{visible} && $self->{w};
1398
1399 $self->SUPER::get_max_wh
1400}
1401
1419sub size_request { 1402sub size_request {
1420 my ($self) = @_; 1403 my ($self) = @_;
1421 1404
1422 $self->{title_widget}->size_request 1405 $self->{title_widget}->size_request
1423 if $self->{title_widget}; 1406 if $self->{title_widget};
1615 1598
1616sub add { 1599sub add {
1617 my ($self, @widgets) = @_; 1600 my ($self, @widgets) = @_;
1618 1601
1619 for my $child (@widgets) { 1602 for my $child (@widgets) {
1620 $child->{rowspan} ||= 1; 1603 $child->{c_rowspan} ||= 1;
1621 $child->{colspan} ||= 1; 1604 $child->{c_colspan} ||= 1;
1622 } 1605 }
1623 1606
1624 $self->SUPER::add (@widgets); 1607 $self->SUPER::add (@widgets);
1625} 1608}
1626 1609
1630 my @widgets; 1613 my @widgets;
1631 1614
1632 while (@_) { 1615 while (@_) {
1633 my ($col, $row, $child) = splice @_, 0, 3, (); 1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1634 1617
1635 $child->{row} = $row; 1618 $child->{c_row} = $row;
1636 $child->{col} = $col; 1619 $child->{c_col} = $col;
1637 1620
1638 push @widgets, $child; 1621 push @widgets, $child;
1639 } 1622 }
1640 1623
1641 $self->add (@widgets); 1624 $self->add (@widgets);
1647 my (@w, @h); 1630 my (@w, @h);
1648 1631
1649 my @children = $self->children; 1632 my @children = $self->children;
1650 1633
1651 # first pass, columns 1634 # first pass, columns
1652 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1653 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1654 1637
1655 my $sw = sum @w[$c .. $c + $cs - 1]; 1638 my $sw = sum @w[$c .. $c + $cs - 1];
1656 1639
1657 if ($w > $sw) { 1640 if ($w > $sw) {
1658 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1659 } 1642 }
1660 } 1643 }
1661 1644
1662 # second pass, rows 1645 # second pass, rows
1663 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1664 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1665 1648
1666 my $sh = sum @h[$r .. $r + $rs - 1]; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1667 1650
1668 if ($h > $sh) { 1651 if ($h > $sh) {
1669 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1711 1694
1712 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1713 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1714 1697
1715 for my $widget ($self->children) { 1698 for my $widget ($self->children) {
1716 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1699 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1717 1700
1718 $widget->configure ( 1701 $widget->configure (
1719 $x[$c], $y[$r], 1702 $x[$c], $y[$r],
1720 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1721 ); 1704 );
1730 1713
1731use List::Util qw(min max); 1714use List::Util qw(min max);
1732 1715
1733our @ISA = CFPlus::UI::Container::; 1716our @ISA = CFPlus::UI::Container::;
1734 1717
1735sub add_fixed {
1736 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1737
1738 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1739 $self->SUPER::add ($child);
1740}
1741
1742sub _scale($$$) { 1718sub _scale($$$) {
1743 my ($mode, $val, $max) = @_; 1719 my ($rel, $val, $max) = @_;
1744 1720
1745 $mode eq "abs" ? $val 1721 $rel ? $val * $max : $val
1746 : $mode eq "rel" ? $val * $max
1747 : 0
1748} 1722}
1749 1723
1750sub size_request { 1724sub size_request {
1751 my ($self) = @_; 1725 my ($self) = @_;
1752 1726
1753 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1754 1728
1755 # determine overall size by querying abs widgets 1729 # determine overall size by querying abs widgets
1756 for my $child ($self->visible_children) { 1730 for my $child ($self->visible_children) {
1757 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1731 unless ($child->{c_rel}) {
1732 my $x = $child->{c_x};
1733 my $y = $child->{c_y};
1758 1734
1759 if ($pos eq "abs") {
1760 $w = _scale $size, $w, $child->{req_w};
1761 $h = _scale $size, $h, $child->{req_h};
1762
1763 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1764 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1765 } 1737 }
1766 } 1738 }
1767 1739
1768 my $W = $x2 - $x1; 1740 my $W = $x2 - $x1;
1769 my $H = $y2 - $y1; 1741 my $H = $y2 - $y1;
1770 1742
1771 # now layout remaining widgets 1743 # now layout remaining widgets
1772 for my $child ($self->visible_children) { 1744 for my $child ($self->visible_children) {
1773 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1745 if ($child->{c_rel}) {
1746 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1747 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1774 1748
1775 if ($pos ne "abs") {
1776 $x = _scale $pos, $x, $W;
1777 $y = _scale $pos, $x, $H;
1778 $w = _scale $size, $w, $child->{req_w};
1779 $h = _scale $size, $h, $child->{req_h};
1780
1781 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1782 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1783 } 1751 }
1784 } 1752 }
1785 1753
1786 my $W = $x2 - $x1; 1754 my $W = $x2 - $x1;
1787 my $H = $y2 - $y1; 1755 my $H = $y2 - $y1;
1791 1759
1792sub invoke_size_allocate { 1760sub invoke_size_allocate {
1793 my ($self, $W, $H) = @_; 1761 my ($self, $W, $H) = @_;
1794 1762
1795 for my $child ($self->visible_children) { 1763 for my $child ($self->visible_children) {
1796 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1764 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1765 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1797 1766
1798 $x = _scale $pos, $x, $W; 1767 $x += $child->{c_halign} * $child->{req_w};
1799 $y = _scale $pos, $x, $H; 1768 $y += $child->{c_valign} * $child->{req_h};
1800 $w = _scale $size, $w, $W;
1801 $h = _scale $size, $h, $H;
1802 1769
1803 $child->configure ($x, $y, $w, $h); 1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1804 } 1771 }
1805 1772
1806 1 1773 1
1807} 1774}
1808 1775
1812 1779
1813our @ISA = CFPlus::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1814 1781
1815sub size_request { 1782sub size_request {
1816 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1817 1786
1818 $self->{vertical} 1787 $self->{vertical}
1819 ? ( 1788 ? (
1820 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1821 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1822 ) 1791 )
1823 : ( 1792 : (
1824 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1825 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1826 ) 1795 )
1827} 1796}
1828 1797
1829sub invoke_size_allocate { 1798sub invoke_size_allocate {
1830 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1954 1923
1955 delete $self->{ox}; 1924 delete $self->{ox};
1956 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1957} 1926}
1958 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1959sub set_text { 1934sub set_text {
1960 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1961 1936
1962 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1963 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
1986 1961
1987sub size_request { 1962sub size_request {
1988 my ($self) = @_; 1963 my ($self) = @_;
1989 1964
1990 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1991 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1992 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1993 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1994 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1995 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1996 1973
1997 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2069 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2070 2047
2071 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2072 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2073 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2074 }; 2053 };
2075 2054
2076 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2055# unless ($self->{list}) {
2077 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2078 2057# CFPlus::OpenGL::glNewList $self->{list};
2079 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2058# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2059# CFPlus::OpenGL::glEndList;
2060# }
2061#
2062# CFPlus::OpenGL::glCallList $self->{list};
2063
2064 $self->{layout}->draw;
2080} 2065}
2066
2067#sub destroy {
2068# my ($self) = @_;
2069#
2070# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2071#
2072# $self->SUPER::destroy;
2073#}
2081 2074
2082############################################################################# 2075#############################################################################
2083 2076
2084package CFPlus::UI::EntryBase; 2077package CFPlus::UI::EntryBase;
2085 2078
2091 my $class = shift; 2084 my $class = shift;
2092 2085
2093 $class->SUPER::new ( 2086 $class->SUPER::new (
2094 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
2095 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
2096 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
2097 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
2098 can_hover => 1, 2093 can_hover => 1,
2099 can_focus => 1, 2094 can_focus => 1,
2100 valign => 0, 2095 valign => 0,
2101 can_events => 1, 2096 can_events => 1,
2102 ellipsise => 0, 2097 ellipsise => 0,
2242 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2243 } 2238 }
2244 2239
2245 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2246 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2247 glBegin GL_QUADS;
2248 glVertex 0 , 0;
2249 glVertex 0 , $self->{h};
2250 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2251 glVertex $self->{w}, 0;
2252 glEnd;
2253 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2254 2244
2255 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2256 2246
2257 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2259 2249
2260 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
2261 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
2262 utf8::encode $text; 2252 utf8::encode $text;
2263 2253
2264 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2254 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2265 } 2255 }
2266 2256
2257 glColor_premultiply @{$self->{active_fg}};
2267 glBegin GL_LINES; 2258 glBegin GL_LINES;
2268 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2259 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2269 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2260 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2261 glEnd;
2262
2263 glLineWidth 3;
2264 glColor @{$self->{active_outline}};
2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2266 glLineWidth 1;
2267
2268 } else {
2269 glColor @{$self->{outline}};
2270 glBegin GL_LINE_STRIP;
2271 glVertex .5, $self->{h} * .5;
2272 glVertex .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - .5, $self->{h} * .5;
2270 glEnd; 2275 glEnd;
2271 } 2276 }
2272} 2277}
2273 2278
2274############################################################################# 2279#############################################################################
2361 1 2366 1
2362} 2367}
2363 2368
2364############################################################################# 2369#############################################################################
2365 2370
2371package CFPlus::UI::ButtonBin;
2372
2373our @ISA = CFPlus::UI::Bin::;
2374
2375use CFPlus::OpenGL;
2376
2377my @tex =
2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2379 qw(b1_button_inactive.png b1_button_active.png);
2380
2381sub new {
2382 my $class = shift;
2383
2384 $class->SUPER::new (
2385 can_hover => 1,
2386 align => 0,
2387 valign => 0,
2388 can_events => 1,
2389 @_
2390 )
2391}
2392
2393sub invoke_button_up {
2394 my ($self, $ev, $x, $y) = @_;
2395
2396 $self->emit ("activate")
2397 if $x >= 0 && $x < $self->{w}
2398 && $y >= 0 && $y < $self->{h};
2399
2400 1
2401}
2402
2403sub _draw {
2404 my ($self) = @_;
2405
2406 glEnable GL_TEXTURE_2D;
2407 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2408 glColor 0, 0, 0, 1;
2409
2410 my $tex = $tex[$GRAB == $self];
2411 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2412
2413 glDisable GL_TEXTURE_2D;
2414
2415 $self->SUPER::_draw;
2416}
2417
2418#############################################################################
2419
2366package CFPlus::UI::Button; 2420package CFPlus::UI::Button;
2367 2421
2368our @ISA = CFPlus::UI::Label::; 2422our @ISA = CFPlus::UI::Label::;
2369 2423
2370use CFPlus::OpenGL; 2424use CFPlus::OpenGL;
2474sub _draw { 2528sub _draw {
2475 my ($self) = @_; 2529 my ($self) = @_;
2476 2530
2477 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2478 2532
2479 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2480 2534
2481 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2482 2536
2483 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2537 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2484 2538
2504sub new { 2558sub new {
2505 my $class = shift; 2559 my $class = shift;
2506 2560
2507 my $self = $class->SUPER::new ( 2561 my $self = $class->SUPER::new (
2508 can_events => 0, 2562 can_events => 0,
2563 scale => 1,
2509 @_, 2564 @_,
2510 ); 2565 );
2511 2566
2512 $self->{path} || $self->{tex} 2567 $self->{path} || $self->{tex}
2513 or Carp::croak "'path' or 'tex' attributes required"; 2568 or Carp::croak "'path' or 'tex' attributes required";
2538} 2593}
2539 2594
2540sub size_request { 2595sub size_request {
2541 my ($self) = @_; 2596 my ($self) = @_;
2542 2597
2543 ($self->{tex}{w}, $self->{tex}{h}) 2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2544} 2599}
2545 2600
2546sub _draw { 2601sub _draw {
2547 my ($self) = @_; 2602 my ($self) = @_;
2548 2603
2558 } 2613 }
2559 2614
2560 glEnable GL_TEXTURE_2D; 2615 glEnable GL_TEXTURE_2D;
2561 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2562 2617
2563 $tex->draw_quad (0, 0, $w, $h); 2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2564 2619
2565 glDisable GL_TEXTURE_2D; 2620 glDisable GL_TEXTURE_2D;
2566} 2621}
2567 2622
2568############################################################################# 2623#############################################################################
2587 align => 0, 2642 align => 0,
2588 valign => 0, 2643 valign => 0,
2589 can_events => 1, 2644 can_events => 1,
2590 @_ 2645 @_
2591 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2592} 2653}
2593 2654
2594sub invoke_button_up { 2655sub invoke_button_up {
2595 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2596 2657
2736 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2737} 2798}
2738 2799
2739############################################################################# 2800#############################################################################
2740 2801
2802package CFPlus::UI::Progress;
2803
2804our @ISA = CFPlus::UI::Label::;
2805
2806use CFPlus::OpenGL;
2807
2808sub new {
2809 my ($class, %arg) = @_;
2810
2811 my $self = $class->SUPER::new (
2812 fg => [1, 1, 1],
2813 bg => [0, 0, 1, 0.2],
2814 bar => [0.7, 0.5, 0.1, 0.8],
2815 outline => [0.4, 0.3, 0],
2816 fontsize => 0.9,
2817 valign => 0,
2818 align => 0,
2819 can_events => 1,
2820 ellipsise => 1,
2821 label => "%d%%",
2822 %arg,
2823 );
2824
2825 $self->set_value ($arg{value} || -1);
2826
2827 $self
2828}
2829
2830sub set_label {
2831 my ($self, $label) = @_;
2832
2833 return if $self->{label} eq $label;
2834 $self->{label} = $label;
2835
2836 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2837}
2838
2839sub set_value {
2840 my ($self, $value) = @_;
2841
2842 if ($self->{value} ne $value) {
2843 $self->{value} = $value;
2844
2845 if ($value < 0) {
2846 $self->set_text ("-");
2847 } else {
2848 $self->set_text (sprintf $self->{label}, $value * 100);
2849 }
2850
2851 $self->update;
2852 }
2853}
2854
2855sub _draw {
2856 my ($self) = @_;
2857
2858 glEnable GL_BLEND;
2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2860
2861 if ($self->{value} >= 0) {
2862 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2863
2864 glColor_premultiply @{$self->{bar}};
2865 glRect 2, 2, $s, $self->{h} - 2;
2866 glColor_premultiply @{$self->{bg}};
2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2868 }
2869
2870 glColor_premultiply @{$self->{outline}};
2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2872
2873 glDisable GL_BLEND;
2874
2875 {
2876 local $self->{bg}; # do not draw background
2877 $self->SUPER::_draw;
2878 }
2879}
2880
2881#############################################################################
2882
2883package CFPlus::UI::ExperienceProgress;
2884
2885our @ISA = CFPlus::UI::Progress::;
2886
2887sub new {
2888 my ($class, %arg) = @_;
2889
2890 my $self = $class->SUPER::new (
2891 tooltip => sub {
2892 my ($self) = @_;
2893
2894 sprintf "level %d\n%s points\n%s next level\n%s to go",
2895 $self->{lvl},
2896 ::formsep ($self->{exp}),
2897 ::formsep ($self->{nxt}),
2898 ::formsep ($self->{nxt} - $self->{exp}),
2899 },
2900 %arg
2901 );
2902
2903 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2904 if $::CONN;
2905
2906 $self
2907}
2908
2909sub DESTROY {
2910 my ($self) = @_;
2911
2912 delete $::CONN->{on_exp_update}{$self+0}
2913 if $::CONN;
2914
2915 $self->SUPER::DESTROY;
2916}
2917
2918sub set_value {
2919 my ($self, $lvl, $exp) = @_;
2920
2921 $self->{lvl} = $lvl;
2922 $self->{exp} = $exp;
2923
2924 my $v = -1;
2925
2926 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2927 my $l0 = $table->[$lvl - 1];
2928 my $l1 = $table->[$lvl];
2929
2930 $self->{nxt} = $l1;
2931
2932 $v = ($exp - $l0) / ($l1 - $l0);
2933 }
2934
2935 $self->SUPER::set_value ($v);
2936}
2937
2938#############################################################################
2939
2741package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2742 2941
2743our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2744 2943
2745sub new { 2944sub new {
2870 3069
2871 $self->SUPER::invoke_button_down ($ev, $x, $y); 3070 $self->SUPER::invoke_button_down ($ev, $x, $y);
2872 3071
2873 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3072 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2874 3073
2875 $self->invoke_mouse_motion ($ev, $x, $y) 3074 $self->invoke_mouse_motion ($ev, $x, $y);
3075
3076 1
2876} 3077}
2877 3078
2878sub invoke_mouse_motion { 3079sub invoke_mouse_motion {
2879 my ($self, $ev, $x, $y) = @_; 3080 my ($self, $ev, $x, $y) = @_;
2880 3081
2900 3101
2901 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2902 3103
2903 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2904 3105
2905 ! ! $delta 3106 1
2906} 3107}
2907 3108
2908sub update { 3109sub update {
2909 my ($self) = @_; 3110 my ($self) = @_;
2910 3111
3041} 3242}
3042 3243
3043sub size_request { 3244sub size_request {
3044 my ($self) = @_; 3245 my ($self) = @_;
3045 3246
3046 my ($empty, $slider) = @{ $self->{children} }; 3247 my ($empty, $slider) = $self->visible_children;
3047 3248
3048 local $self->{children} = [$empty, $slider]; 3249 local $self->{children} = [$empty, $slider];
3049 $self->SUPER::size_request 3250 $self->SUPER::size_request
3050} 3251}
3051 3252
3240 3441
3241 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3242 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3243 3444
3244 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3245 3447
3246 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3247 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3248 3450
3249 for (@w) { 3451 for (@w) {
3371} 3573}
3372 3574
3373sub set_tooltip_from { 3575sub set_tooltip_from {
3374 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3375 3577
3376 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3377 if $widget->{tooltip} =~ /^#(.*)$/;
3378
3379 my $tooltip = $widget->{tooltip}; 3578 my $tip = $widget->{tooltip};
3579 $tip = $tip->($widget) if "CODE" eq ref $tip;
3580
3581 $tip = CFPlus::Pod::section_label tooltip => $1
3582 if $tip =~ /^#(.*)$/;
3380 3583
3381 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3382 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3383 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3384 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3385 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3386 } 3589 }
3387 3590
3388 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3389 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3390 3593
3391 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3392 markup => $tooltip, 3595 markup => $tip,
3393 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3394 fontsize => 0.8, 3597 fontsize => 0.8,
3395 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3396 ellipsise => 0, 3599 ellipsise => 0,
3397 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3435} 3638}
3436 3639
3437sub _draw { 3640sub _draw {
3438 my ($self) = @_; 3641 my ($self) = @_;
3439 3642
3440 glTranslate 0.375, 0.375;
3441
3442 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3443 3644
3444 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3445 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
3446 glVertex 0 , 0;
3447 glVertex 0 , $h;
3448 glVertex $w, $h;
3449 glVertex $w, 0;
3450 glEnd;
3451 3647
3452 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3453 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3454 glVertex 0 , 0;
3455 glVertex 0 , $h;
3456 glVertex $w, $h;
3457 glVertex $w, 0;
3458 glEnd;
3459 3650
3460 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3461 3652
3462 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3463} 3654}
3464 3655
3465############################################################################# 3656#############################################################################
3483 3674
3484 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3485 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3486 3677
3487 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3488 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3489 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3490 parked => 1,
3491 cb => sub {
3492 return unless $::CONN && $widget; 3681 return unless $::CONN;
3493 3682
3683 my $w = $widget
3684 or return;
3685
3494 ++$widget->{frame}; 3686 ++$w->{frame};
3495 $widget->update_face; 3687 $w->update_face;
3688
3689 # somehow, $widget can go away
3496 $widget->update; 3690 $w->update;
3497
3498 $widget->update_timer; 3691 $w->update_timer;
3499 },
3500 ); 3692 };
3501 3693
3502 $self->update_face; 3694 $self->update_face;
3503 $self->update_timer; 3695 $self->update_timer;
3504 } 3696 }
3505 3697
3509sub update_timer { 3701sub update_timer {
3510 my ($self) = @_; 3702 my ($self) = @_;
3511 3703
3512 return unless $self->{timer}; 3704 return unless $self->{timer};
3513 3705
3706 $self->{timer}->stop;
3514 if ($self->{visible}) { 3707 if ($self->{visible}) {
3515 $self->{timer}->at ( 3708 $self->{timer}->set (
3516 $self->{anim_start} 3709 $self->{anim_start}
3517 + $self->{animspeed} 3710 + $self->{animspeed}
3518 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3519 ); 3712 );
3520 $self->{timer}->start; 3713 $self->{timer}->start;
3521 } else {
3522 $self->{timer}->stop;
3523 } 3714 }
3524} 3715}
3525 3716
3526sub update_face { 3717sub update_face {
3527 my ($self) = @_; 3718 my ($self) = @_;
3528 3719
3529 return unless $::CONN; 3720 if ($::CONN) {
3530
3531 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3721 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3532 if ($anim && @$anim) { 3722 if ($anim && @$anim) {
3533 delete $self->{wait_face};
3534 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3723 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3724 delete $self->{face_change_cb};
3725
3726 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3727 unless ($tex->{name} || $tex->{loading}) {
3728 $tex->upload (sub { $self->reconfigure });
3729 }
3730 }
3731 }
3535 } 3732 }
3536 } 3733 }
3537} 3734}
3538 3735
3539sub size_request { 3736sub size_request {
3540 my ($self) = @_; 3737 my ($self) = @_;
3541 3738
3542 if ($::CONN) { 3739 if ($::CONN) {
3543 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3740 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3544 if (my $tex = $::CONN->{texture}[$faceid]) { 3741 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3545 $self->{tex} = $tex; 3742 if ($tex->{name}) {
3546 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3743 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3547 } else { 3744 } elsif (!$tex->{loading}) {
3548 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3745 $tex->upload (sub { $self->reconfigure });
3549 $self->realloc;
3550 }); 3746 }
3551 } 3747 }
3748
3749 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3552 } 3750 }
3553 } 3751 }
3554 3752
3555 ($self->{size_w} || 8, $self->{size_h} || 8) 3753 ($self->{size_w} || 8, $self->{size_h} || 8)
3556} 3754}
3636 children => [ 3834 children => [
3637 (new CFPlus::UI::Label markup => $left, expand => 1), 3835 (new CFPlus::UI::Label markup => $left, expand => 1),
3638 (new CFPlus::UI::Label markup => $right, align => +1), 3836 (new CFPlus::UI::Label markup => $right, align => +1),
3639 ], 3837 ],
3640 ; 3838 ;
3641 3839
3642 } else { 3840 } else {
3643 $widget = new CFPlus::UI::Label 3841 $widget = new CFPlus::UI::Label
3644 can_hover => 1, 3842 can_hover => 1,
3645 can_events => 1, 3843 can_events => 1,
3646 markup => $widget, 3844 markup => $widget,
3771 3969
3772############################################################################# 3970#############################################################################
3773 3971
3774package CFPlus::UI::Notebook; 3972package CFPlus::UI::Notebook;
3775 3973
3974use CFPlus::OpenGL;
3975
3776our @ISA = CFPlus::UI::VBox::; 3976our @ISA = CFPlus::UI::VBox::;
3777 3977
3778sub new { 3978sub new {
3779 my $class = shift; 3979 my $class = shift;
3780 3980
3781 my $self = $class->SUPER::new ( 3981 my $self = $class->SUPER::new (
3782 buttonbar => (new CFPlus::UI::Buttonbar), 3982 buttonbar => (new CFPlus::UI::Buttonbar),
3783 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3983 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3984 active_outline => [.7, .7, 0.2],
3784 # filter => # will be put between multiplexer and $self 3985 # filter => # will be put between multiplexer and $self
3785 @_, 3986 @_,
3786 ); 3987 );
3787 3988
3788 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3989 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3789 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3990 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3790 3991
3992 {
3993 Scalar::Util::weaken (my $wself = $self);
3994
3995 $self->{multiplexer}->connect (c_add => sub {
3996 my ($mplex, $widgets) = @_;
3997
3998 for my $child (@$widgets) {
3999 Scalar::Util::weaken $child;
4000 $child->{c_tab_} ||= do {
4001 my $tab =
4002 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
4003 ? $child->{c_tab}
4004 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4005
4006 $tab->connect (activate => sub {
4007 $wself->set_current_page ($child);
4008 });
4009
4010 $tab
4011 };
4012
4013 $self->{buttonbar}->add ($child->{c_tab_});
4014 }
4015 });
4016
4017 $self->{multiplexer}->connect (c_remove => sub {
4018 my ($mplex, $widgets) = @_;
4019
4020 for my $child (@$widgets) {
4021 $wself->{buttonbar}->remove ($child->{c_tab_});
4022 }
4023 });
4024 }
4025
3791 $self 4026 $self
3792} 4027}
3793 4028
3794sub add { 4029sub add {
4030 my ($self, @widgets) = @_;
4031
4032 $self->{multiplexer}->add (@widgets)
4033}
4034
4035sub remove {
4036 my ($self, @widgets) = @_;
4037
4038 $self->{multiplexer}->remove (@widgets)
4039}
4040
4041sub pages {
4042 my ($self) = @_;
4043 $self->{multiplexer}->children
4044}
4045
4046sub add_tab {
3795 my ($self, $title, $widget, $tooltip) = @_; 4047 my ($self, $title, $widget, $tooltip) = @_;
3796 4048
3797 CFPlus::weaken $self; 4049 $title = [$title, $tooltip] unless ref $title;
4050 $widget->{c_tab} = $title;
3798 4051
3799 $self->{buttonbar}->add (new CFPlus::UI::Button
3800 markup => $title,
3801 tooltip => $tooltip,
3802 on_activate => sub { $self->set_current_page ($widget) },
3803 );
3804
3805 $self->{multiplexer}->add ($widget); 4052 $self->add ($widget);
3806} 4053}
3807 4054
3808sub get_current_page { 4055sub get_current_page {
3809 my ($self) = @_; 4056 my ($self) = @_;
3810 4057
3814sub set_current_page { 4061sub set_current_page {
3815 my ($self, $page) = @_; 4062 my ($self, $page) = @_;
3816 4063
3817 $self->{multiplexer}->set_current_page ($page); 4064 $self->{multiplexer}->set_current_page ($page);
3818 $self->emit (page_changed => $self->{multiplexer}{current}); 4065 $self->emit (page_changed => $self->{multiplexer}{current});
4066}
4067
4068sub _draw {
4069 my ($self) = @_;
4070
4071 $self->SUPER::_draw ();
4072
4073 if (my $cur = $self->{multiplexer}{current}) {
4074 if ($cur = $cur->{c_tab_}) {
4075 glTranslate $self->{buttonbar}{x} + $cur->{x},
4076 $self->{buttonbar}{y} + $cur->{y};
4077 glLineWidth 3;
4078 #glEnable GL_BLEND;
4079 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4080 glColor @{$self->{active_outline}};
4081 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4082 glLineWidth 1;
4083 #glDisable GL_BLEND;
4084 }
4085 }
3819} 4086}
3820 4087
3821############################################################################# 4088#############################################################################
3822 4089
3823package CFPlus::UI::Selector; 4090package CFPlus::UI::Selector;
3855} 4122}
3856 4123
3857sub _set_value { 4124sub _set_value {
3858 my ($self, $value) = @_; 4125 my ($self, $value) = @_;
3859 4126
3860 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4127 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4128 $item ||= $self->{options}[0]
3861 or return; 4129 or return;
3862 4130
3863 $self->{value} = $item->[0]; 4131 $self->{value} = $item->[0];
3864 $self->set_markup ("$item->[1] ⇓"); 4132 $self->set_markup ("$item->[1] ⇓");
3865 $self->set_tooltip ($item->[2]); 4133# $self->set_tooltip ($item->[2]);
3866} 4134}
3867 4135
3868sub set_value { 4136sub set_value {
3869 my ($self, $value) = @_; 4137 my ($self, $value) = @_;
3870 4138
3871 return unless $self->{value} ne $value; 4139 return unless $self->{value} ne $value;
3872 4140
3873 $self->_set_value ($value); 4141 $self->_set_value ($value);
3874 $self->emit (changed => $value); 4142 $self->emit (changed => $value);
4143}
4144
4145sub set_options {
4146 my ($self, $options) = @_;
4147
4148 $self->{options} = $options;
4149 $self->_set_value ($self->{value});
3875} 4150}
3876 4151
3877############################################################################# 4152#############################################################################
3878 4153
3879package CFPlus::UI::Statusbox; 4154package CFPlus::UI::Statusbox;
3888 @_, 4163 @_,
3889 ); 4164 );
3890 4165
3891 CFPlus::weaken (my $this = $self); 4166 CFPlus::weaken (my $this = $self);
3892 4167
3893 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3894 4169
3895 $self 4170 $self
3896} 4171}
3897 4172
3898sub reorder { 4173sub reorder {
3912 4187
3913 my @items = sort { 4188 my @items = sort {
3914 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
3915 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
3916 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
3917
3918 $self->{timer}->interval (1);
3919 4192
3920 my $count = 10 + 1; 4193 my $count = 10 + 1;
3921 for my $item (@items) { 4194 for my $item (@items) {
3922 last unless --$count; 4195 last unless --$count;
3923 4196
3947 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3948 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3949 $label->update; 4222 $label->update;
3950 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
3951 if $diff < 1; 4224 if $diff < 1;
3952 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
3953 } else { 4226 } else {
3954 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
3955 } 4228 }
3956 4229
3957 push @widgets, $label; 4230 push @widgets, $label;
4093} 4366}
4094 4367
4095sub update { 4368sub update {
4096 my ($self) = @_; 4369 my ($self) = @_;
4097 4370
4098 $::WANT_REFRESH++; 4371 $::WANT_REFRESH->start;
4099} 4372}
4100 4373
4101sub add { 4374sub add {
4102 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4103 4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines