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.408 by root, Sun Jul 22 15:26:01 2007 UTC vs.
Revision 1.452 by root, Wed Dec 26 20:09:38 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, 0.03, 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}
341sub hide { 338sub hide {
342 my ($self) = @_; 339 my ($self) = @_;
343 340
344 $self->set_invisible; 341 $self->set_invisible;
345 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 346 $parent->remove ($self);
347 if $self->{parent}; 347 }
348} 348}
349 349
350sub move_abs { 350sub move_abs {
351 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
352 352
362 362
363 $self->{force_w} = $w; 363 $self->{force_w} = $w;
364 $self->{force_h} = $h; 364 $self->{force_h} = $h;
365 365
366 $self->realloc; 366 $self->realloc;
367}
368
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
367} 377}
368 378
369sub size_request { 379sub size_request {
370 require Carp; 380 require Carp;
371 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
433 443
434 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
435 445
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $CFPlus::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
439 } 449 }
440} 450}
441 451
442# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
443sub coord2local { 453sub coord2local {
444 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
445 455
446 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
447 457
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 459}
450 460
451# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
452sub coord2global { 462sub coord2global {
453 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
454 464
455 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
456 466
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 468}
459 469
460sub invoke_focus_in { 470sub invoke_focus_in {
607sub DESTROY { 617sub DESTROY {
608 my ($self) = @_; 618 my ($self) = @_;
609 619
610 return if CFPlus::in_destruct; 620 return if CFPlus::in_destruct;
611 621
622 local $@;
612 eval { $self->destroy }; 623 eval { $self->destroy };
613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614 625
615 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
616} 627}
624use strict; 635use strict;
625use CFPlus::OpenGL; 636use CFPlus::OpenGL;
626 637
627sub new { 638sub new {
628 my $class = shift; 639 my $class = shift;
629
630 # range [value, low, high, page]
631 640
632 $class->SUPER::new ( 641 $class->SUPER::new (
633 #bg => [0, 0, 0, 0.2], 642 #bg => [0, 0, 0, 0.2],
634 #active_bg => [1, 1, 1, 0.5], 643 #active_bg => [1, 1, 1, 0.5],
635 @_ 644 @_
647 my ($w, $h) = @$self{qw(w h)}; 656 my ($w, $h) = @$self{qw(w h)};
648 657
649 glEnable GL_BLEND; 658 glEnable GL_BLEND;
650 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 659 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
651 glColor_premultiply @$color; 660 glColor_premultiply @$color;
652
653 glBegin GL_QUADS;
654 glVertex 0 , 0;
655 glVertex 0 , $h;
656 glVertex $w, $h; 661 glRect 0, 0, $w, $h;
657 glVertex $w, 0;
658 glEnd;
659
660 glDisable GL_BLEND; 662 glDisable GL_BLEND;
661 } 663 }
662} 664}
663 665
664############################################################################# 666#############################################################################
725 @{$self->{children}}, @widgets 727 @{$self->{children}}, @widgets
726 ]; 728 ];
727 729
728 $self->realloc; 730 $self->realloc;
729 731
732 $self->emit (c_add => \@widgets);
733
730 map $_+0, @widgets 734 map $_+0, @widgets
731} 735}
732 736
733sub children { 737sub children {
734 @{ $_[0]{children} } 738 @{ $_[0]{children} }
735} 739}
736 740
737sub remove { 741sub remove {
738 my ($self, $child) = @_; 742 my ($self, @widgets) = @_;
739 743
744 $self->emit (c_remove => \@widgets);
745
746 for my $child (@widgets) {
740 delete $child->{parent}; 747 delete $child->{parent};
741 $child->hide; 748 $child->hide;
742
743 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 749 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
750 }
744 751
745 $self->realloc; 752 $self->realloc;
746} 753}
747 754
748sub clear { 755sub clear {
776} 783}
777 784
778sub _draw { 785sub _draw {
779 my ($self) = @_; 786 my ($self) = @_;
780 787
781 $_->draw for @{$self->{children}}; 788 $_->draw for $self->visible_children;
782} 789}
783 790
784############################################################################# 791#############################################################################
785 792
786package CFPlus::UI::Bin; 793package CFPlus::UI::Bin;
796} 803}
797 804
798sub add { 805sub add {
799 my ($self, $child) = @_; 806 my ($self, $child) = @_;
800 807
801 $self->SUPER::remove ($_) for @{ $self->{children} }; 808 $self->clear;
802 $self->SUPER::add ($child); 809 $self->SUPER::add ($child);
803} 810}
804 811
805sub remove { 812sub remove {
806 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
824 831
825 1 832 1
826} 833}
827 834
828############################################################################# 835#############################################################################
829
830# back-buffered drawing area 836# back-buffered drawing area
831 837
832package CFPlus::UI::Window; 838package CFPlus::UI::Window;
833 839
834our @ISA = CFPlus::UI::Bin::; 840our @ISA = CFPlus::UI::Bin::;
948 $self->{view_x} = $x; 954 $self->{view_x} = $x;
949 $self->{view_y} = $y; 955 $self->{view_y} = $y;
950 956
951 $self->emit (changed => $x, $y); 957 $self->emit (changed => $x, $y);
952 $self->update; 958 $self->update;
959 }
960}
961
962sub set_center {
963 my ($self, $x, $y) = @_;
964
965 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
966}
967
968sub make_visible {
969 my ($self, $x, $y, $border) = @_;
970
971 if ( $x < $self->{view_x} + $self->{w} * $border
972 || $x > $self->{view_x} + $self->{w} * (1 - $border)
973 || $y < $self->{view_y} + $self->{h} * $border
974 || $y > $self->{view_y} + $self->{h} * (1 - $border)
975 ) {
976 $self->set_center ($x, $y);
953 } 977 }
954} 978}
955 979
956# hmm, this does not work for topleft of $self... but we should not ask for that 980# hmm, this does not work for topleft of $self... but we should not ask for that
957sub coord2local { 981sub coord2local {
1004 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1005 1029
1006 my $self; 1030 my $self;
1007 1031
1008 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new CFPlus::UI::Slider
1009 col => 0, 1033 c_col => 0,
1010 row => 1, 1034 c_row => 1,
1011 vertical => 0, 1035 vertical => 0,
1012 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1013 on_changed => sub { 1037 on_changed => sub {
1014 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1015 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1016 }, 1040 },
1017 ; 1041 ;
1018 1042
1019 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new CFPlus::UI::Slider
1020 col => 1, 1044 c_col => 1,
1021 row => 0, 1045 c_row => 0,
1022 vertical => 1, 1046 vertical => 1,
1023 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1048 on_changed => sub {
1025 $self->{vpos} = $_[1]; 1049 $self->{vpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1037 row_expand => [1, 0], 1061 row_expand => [1, 0],
1038 %arg, 1062 %arg,
1039 ); 1063 );
1040 1064
1041 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new CFPlus::UI::ViewPort
1042 col => 0, 1066 c_col => 0,
1043 row => 0, 1067 c_row => 0,
1044 expand => 1, 1068 expand => 1,
1045 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1046 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1047 on_changed => sub { 1071 on_changed => sub {
1048 my ($vp, $x, $y) = @_; 1072 my ($vp, $x, $y) = @_;
1070 my ($self, $widget) = @_; 1094 my ($self, $widget) = @_;
1071 1095
1072 $self->{vp}->add ($self->{child} = $widget); 1096 $self->{vp}->add ($self->{child} = $widget);
1073} 1097}
1074 1098
1099sub set_offset { shift->{vp}->set_offset (@_) }
1100sub set_center { shift->{vp}->set_center (@_) }
1101sub make_visible { shift->{vp}->make_visible (@_) }
1102
1075sub update_slider { 1103sub update_slider {
1076 my ($self) = @_; 1104 my ($self) = @_;
1077 1105
1078 my $child = ($self->{vp} or return)->child; 1106 my $child = ($self->{vp} or return)->child;
1079 1107
1192 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
1193 1221
1194 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
1195 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1196 glColor_premultiply @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
1197
1198 glBegin GL_QUADS;
1199 glVertex 0 , 0;
1200 glVertex 0 , $h;
1201 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
1202 glVertex $w, 0;
1203 glEnd;
1204
1205 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
1206 } 1227 }
1207 1228
1208 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1209} 1230}
1370 1391
1371sub border { 1392sub border {
1372 int $_[0]{border} * $::FONTSIZE 1393 int $_[0]{border} * $::FONTSIZE
1373} 1394}
1374 1395
1396sub get_max_wh {
1397 my ($self) = @_;
1398
1399 return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w};
1401
1402 $self->SUPER::get_max_wh
1403}
1404
1375sub size_request { 1405sub size_request {
1376 my ($self) = @_; 1406 my ($self) = @_;
1377 1407
1378 $self->{title_widget}->size_request 1408 $self->{title_widget}->size_request
1379 if $self->{title_widget}; 1409 if $self->{title_widget};
1571 1601
1572sub add { 1602sub add {
1573 my ($self, @widgets) = @_; 1603 my ($self, @widgets) = @_;
1574 1604
1575 for my $child (@widgets) { 1605 for my $child (@widgets) {
1576 $child->{rowspan} ||= 1; 1606 $child->{c_rowspan} ||= 1;
1577 $child->{colspan} ||= 1; 1607 $child->{c_colspan} ||= 1;
1578 } 1608 }
1579 1609
1580 $self->SUPER::add (@widgets); 1610 $self->SUPER::add (@widgets);
1581} 1611}
1582 1612
1586 my @widgets; 1616 my @widgets;
1587 1617
1588 while (@_) { 1618 while (@_) {
1589 my ($col, $row, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1590 1620
1591 $child->{row} = $row; 1621 $child->{c_row} = $row;
1592 $child->{col} = $col; 1622 $child->{c_col} = $col;
1593 1623
1594 push @widgets, $child; 1624 push @widgets, $child;
1595 } 1625 }
1596 1626
1597 $self->add (@widgets); 1627 $self->add (@widgets);
1603 my (@w, @h); 1633 my (@w, @h);
1604 1634
1605 my @children = $self->children; 1635 my @children = $self->children;
1606 1636
1607 # first pass, columns 1637 # first pass, columns
1608 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1638 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1609 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1610 1640
1611 my $sw = sum @w[$c .. $c + $cs - 1]; 1641 my $sw = sum @w[$c .. $c + $cs - 1];
1612 1642
1613 if ($w > $sw) { 1643 if ($w > $sw) {
1614 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1615 } 1645 }
1616 } 1646 }
1617 1647
1618 # second pass, rows 1648 # second pass, rows
1619 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1620 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1621 1651
1622 my $sh = sum @h[$r .. $r + $rs - 1]; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1623 1653
1624 if ($h > $sh) { 1654 if ($h > $sh) {
1625 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1667 1697
1668 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1669 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1670 1700
1671 for my $widget ($self->children) { 1701 for my $widget ($self->children) {
1672 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1673 1703
1674 $widget->configure ( 1704 $widget->configure (
1675 $x[$c], $y[$r], 1705 $x[$c], $y[$r],
1676 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1677 ); 1707 );
1686 1716
1687use List::Util qw(min max); 1717use List::Util qw(min max);
1688 1718
1689our @ISA = CFPlus::UI::Container::; 1719our @ISA = CFPlus::UI::Container::;
1690 1720
1691sub add_fixed {
1692 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1693
1694 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1695 $self->SUPER::add ($child);
1696}
1697
1698sub _scale($$$) { 1721sub _scale($$$) {
1699 my ($mode, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1700 1723
1701 $mode eq "abs" ? $val 1724 $rel ? $val * $max : $val
1702 : $mode eq "rel" ? $val * $max
1703 : 0
1704} 1725}
1705 1726
1706sub size_request { 1727sub size_request {
1707 my ($self) = @_; 1728 my ($self) = @_;
1708 1729
1709 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1730 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1710 1731
1711 # determine overall size by querying abs widgets 1732 # determine overall size by querying abs widgets
1712 for my $child ($self->visible_children) { 1733 for my $child ($self->visible_children) {
1713 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1734 unless ($child->{c_rel}) {
1735 my $x = $child->{c_x};
1736 my $y = $child->{c_y};
1714 1737
1715 if ($pos eq "abs") {
1716 $w = _scale $size, $w, $child->{req_w};
1717 $h = _scale $size, $h, $child->{req_h};
1718
1719 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1738 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1720 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1739 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1721 } 1740 }
1722 } 1741 }
1723 1742
1724 my $W = $x2 - $x1; 1743 my $W = $x2 - $x1;
1725 my $H = $y2 - $y1; 1744 my $H = $y2 - $y1;
1726 1745
1727 # now layout remaining widgets 1746 # now layout remaining widgets
1728 for my $child ($self->visible_children) { 1747 for my $child ($self->visible_children) {
1729 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1748 if ($child->{c_rel}) {
1749 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1750 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1730 1751
1731 if ($pos ne "abs") {
1732 $x = _scale $pos, $x, $W;
1733 $y = _scale $pos, $x, $H;
1734 $w = _scale $size, $w, $child->{req_w};
1735 $h = _scale $size, $h, $child->{req_h};
1736
1737 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1752 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1738 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1753 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1739 } 1754 }
1740 } 1755 }
1741 1756
1742 my $W = $x2 - $x1; 1757 my $W = $x2 - $x1;
1743 my $H = $y2 - $y1; 1758 my $H = $y2 - $y1;
1747 1762
1748sub invoke_size_allocate { 1763sub invoke_size_allocate {
1749 my ($self, $W, $H) = @_; 1764 my ($self, $W, $H) = @_;
1750 1765
1751 for my $child ($self->visible_children) { 1766 for my $child ($self->visible_children) {
1752 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1767 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1768 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1753 1769
1754 $x = _scale $pos, $x, $W; 1770 $x += $child->{c_halign} * $child->{req_w};
1755 $y = _scale $pos, $x, $H; 1771 $y += $child->{c_valign} * $child->{req_h};
1756 $w = _scale $size, $w, $W;
1757 $h = _scale $size, $h, $H;
1758 1772
1759 $child->configure ($x, $y, $w, $h); 1773 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1760 } 1774 }
1761 1775
1762 1 1776 1
1763} 1777}
1764 1778
1768 1782
1769our @ISA = CFPlus::UI::Container::; 1783our @ISA = CFPlus::UI::Container::;
1770 1784
1771sub size_request { 1785sub size_request {
1772 my ($self) = @_; 1786 my ($self) = @_;
1787
1788 my @children = $self->visible_children;
1773 1789
1774 $self->{vertical} 1790 $self->{vertical}
1775 ? ( 1791 ? (
1776 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1792 (List::Util::max map $_->{req_w}, @children),
1777 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_h}, @children),
1778 ) 1794 )
1779 : ( 1795 : (
1780 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1796 (List::Util::sum map $_->{req_w}, @children),
1781 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1797 (List::Util::max map $_->{req_h}, @children),
1782 ) 1798 )
1783} 1799}
1784 1800
1785sub invoke_size_allocate { 1801sub invoke_size_allocate {
1786 my ($self, $w, $h) = @_; 1802 my ($self, $w, $h) = @_;
1910 1926
1911 delete $self->{ox}; 1927 delete $self->{ox};
1912 $self->SUPER::realloc; 1928 $self->SUPER::realloc;
1913} 1929}
1914 1930
1931sub clear {
1932 my ($self) = @_;
1933
1934 $self->set_text ("");
1935}
1936
1915sub set_text { 1937sub set_text {
1916 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1917 1939
1918 return if $self->{text} eq "T$text"; 1940 return if $self->{text} eq "T$text";
1919 $self->{text} = "T$text"; 1941 $self->{text} = "T$text";
1942 1964
1943sub size_request { 1965sub size_request {
1944 my ($self) = @_; 1966 my ($self) = @_;
1945 1967
1946 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1976
1953 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2025 : ($self->{w} - $size->[0]) * 0.5); 2049 : ($self->{w} - $size->[0]) * 0.5);
2026 2050
2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2029 : ($self->{h} - $size->[1]) * 0.5); 2053 : ($self->{h} - $size->[1]) * 0.5);
2054
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2030 }; 2056 };
2031 2057
2032# unless ($self->{list}) { 2058# unless ($self->{list}) {
2033# $self->{list} = CFPlus::OpenGL::glGenList; 2059# $self->{list} = CFPlus::OpenGL::glGenList;
2034# CFPlus::OpenGL::glNewList $self->{list}; 2060# CFPlus::OpenGL::glNewList $self->{list};
2036# CFPlus::OpenGL::glEndList; 2062# CFPlus::OpenGL::glEndList;
2037# } 2063# }
2038# 2064#
2039# CFPlus::OpenGL::glCallList $self->{list}; 2065# CFPlus::OpenGL::glCallList $self->{list};
2040 2066
2041 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2067 $self->{layout}->draw;
2042} 2068}
2043 2069
2044#sub destroy { 2070#sub destroy {
2045# my ($self) = @_; 2071# my ($self) = @_;
2046# 2072#
2061 my $class = shift; 2087 my $class = shift;
2062 2088
2063 $class->SUPER::new ( 2089 $class->SUPER::new (
2064 fg => [1, 1, 1], 2090 fg => [1, 1, 1],
2065 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1],
2066 active_bg => [1, 1, 1, 0.5], 2093 active_bg => [0, 0, 1, .2],
2067 active_fg => [0, 0, 0], 2094 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0],
2068 can_hover => 1, 2096 can_hover => 1,
2069 can_focus => 1, 2097 can_focus => 1,
2070 valign => 0, 2098 valign => 0,
2071 can_events => 1, 2099 can_events => 1,
2072 ellipsise => 0, 2100 ellipsise => 0,
2212 glColor_premultiply @{$self->{bg}}; 2240 glColor_premultiply @{$self->{bg}};
2213 } 2241 }
2214 2242
2215 glEnable GL_BLEND; 2243 glEnable GL_BLEND;
2216 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2244 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2217 glBegin GL_QUADS;
2218 glVertex 0 , 0;
2219 glVertex 0 , $self->{h};
2220 glVertex $self->{w}, $self->{h}; 2245 glRect 0, 0, $self->{w}, $self->{h};
2221 glVertex $self->{w}, 0;
2222 glEnd;
2223 glDisable GL_BLEND; 2246 glDisable GL_BLEND;
2224 2247
2225 $self->SUPER::_draw; 2248 $self->SUPER::_draw;
2226 2249
2227 #TODO: force update every cursor change :( 2250 #TODO: force update every cursor change :(
2229 2252
2230 unless (exists $self->{cur_h}) { 2253 unless (exists $self->{cur_h}) {
2231 my $text = substr $self->{text}, 0, $self->{cursor}; 2254 my $text = substr $self->{text}, 0, $self->{cursor};
2232 utf8::encode $text; 2255 utf8::encode $text;
2233 2256
2234 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2235 } 2258 }
2236 2259
2260 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2261 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2262 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2263 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2264 glEnd;
2265
2266 glLineWidth 3;
2267 glColor @{$self->{active_outline}};
2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2269 glLineWidth 1;
2270
2271 } else {
2272 glColor @{$self->{outline}};
2273 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5;
2240 glEnd; 2278 glEnd;
2241 } 2279 }
2242} 2280}
2243 2281
2244############################################################################# 2282#############################################################################
2331 1 2369 1
2332} 2370}
2333 2371
2334############################################################################# 2372#############################################################################
2335 2373
2374package CFPlus::UI::ButtonBin;
2375
2376our @ISA = CFPlus::UI::Bin::;
2377
2378use CFPlus::OpenGL;
2379
2380my @tex =
2381 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png);
2383
2384sub new {
2385 my $class = shift;
2386
2387 $class->SUPER::new (
2388 can_hover => 1,
2389 align => 0,
2390 valign => 0,
2391 can_events => 1,
2392 @_
2393 )
2394}
2395
2396sub invoke_button_up {
2397 my ($self, $ev, $x, $y) = @_;
2398
2399 $self->emit ("activate")
2400 if $x >= 0 && $x < $self->{w}
2401 && $y >= 0 && $y < $self->{h};
2402
2403 1
2404}
2405
2406sub _draw {
2407 my ($self) = @_;
2408
2409 glEnable GL_TEXTURE_2D;
2410 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2411 glColor 0, 0, 0, 1;
2412
2413 my $tex = $tex[$GRAB == $self];
2414 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2415
2416 glDisable GL_TEXTURE_2D;
2417
2418 $self->SUPER::_draw;
2419}
2420
2421#############################################################################
2422
2336package CFPlus::UI::Button; 2423package CFPlus::UI::Button;
2337 2424
2338our @ISA = CFPlus::UI::Label::; 2425our @ISA = CFPlus::UI::Label::;
2339 2426
2340use CFPlus::OpenGL; 2427use CFPlus::OpenGL;
2444sub _draw { 2531sub _draw {
2445 my ($self) = @_; 2532 my ($self) = @_;
2446 2533
2447 $self->SUPER::_draw; 2534 $self->SUPER::_draw;
2448 2535
2449 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2536 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2450 2537
2451 my ($w, $h) = @$self{qw(w h)}; 2538 my ($w, $h) = @$self{qw(w h)};
2452 2539
2453 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2540 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2454 2541
2474sub new { 2561sub new {
2475 my $class = shift; 2562 my $class = shift;
2476 2563
2477 my $self = $class->SUPER::new ( 2564 my $self = $class->SUPER::new (
2478 can_events => 0, 2565 can_events => 0,
2566 scale => 1,
2479 @_, 2567 @_,
2480 ); 2568 );
2481 2569
2482 $self->{path} || $self->{tex} 2570 $self->{path} || $self->{tex}
2483 or Carp::croak "'path' or 'tex' attributes required"; 2571 or Carp::croak "'path' or 'tex' attributes required";
2508} 2596}
2509 2597
2510sub size_request { 2598sub size_request {
2511 my ($self) = @_; 2599 my ($self) = @_;
2512 2600
2513 ($self->{tex}{w}, $self->{tex}{h}) 2601 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2514} 2602}
2515 2603
2516sub _draw { 2604sub _draw {
2517 my ($self) = @_; 2605 my ($self) = @_;
2518 2606
2528 } 2616 }
2529 2617
2530 glEnable GL_TEXTURE_2D; 2618 glEnable GL_TEXTURE_2D;
2531 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2619 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2532 2620
2533 $tex->draw_quad (0, 0, $w, $h); 2621 $tex->draw_quad_alpha (0, 0, $w, $h);
2534 2622
2535 glDisable GL_TEXTURE_2D; 2623 glDisable GL_TEXTURE_2D;
2536} 2624}
2537 2625
2538############################################################################# 2626#############################################################################
2557 align => 0, 2645 align => 0,
2558 valign => 0, 2646 valign => 0,
2559 can_events => 1, 2647 can_events => 1,
2560 @_ 2648 @_
2561 ); 2649 );
2650}
2651
2652sub invoke_button_down {
2653 my ($self, $ev, $x, $y) = @_;
2654
2655 1
2562} 2656}
2563 2657
2564sub invoke_button_up { 2658sub invoke_button_up {
2565 my ($self, $ev, $x, $y) = @_; 2659 my ($self, $ev, $x, $y) = @_;
2566 2660
2706 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
2707} 2801}
2708 2802
2709############################################################################# 2803#############################################################################
2710 2804
2805package CFPlus::UI::Progress;
2806
2807our @ISA = CFPlus::UI::Label::;
2808
2809use CFPlus::OpenGL;
2810
2811sub new {
2812 my ($class, %arg) = @_;
2813
2814 my $self = $class->SUPER::new (
2815 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9,
2820 valign => 0,
2821 align => 0,
2822 can_events => 1,
2823 ellipsise => 1,
2824 label => "%d%%",
2825 %arg,
2826 );
2827
2828 $self->set_value ($arg{value} || -1);
2829
2830 $self
2831}
2832
2833sub set_label {
2834 my ($self, $label) = @_;
2835
2836 return if $self->{label} eq $label;
2837 $self->{label} = $label;
2838
2839 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2840}
2841
2842sub set_value {
2843 my ($self, $value) = @_;
2844
2845 if ($self->{value} ne $value) {
2846 $self->{value} = $value;
2847
2848 if ($value < 0) {
2849 $self->set_text ("-");
2850 } else {
2851 $self->set_text (sprintf $self->{label}, $value * 100);
2852 }
2853
2854 $self->update;
2855 }
2856}
2857
2858sub _draw {
2859 my ($self) = @_;
2860
2861 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863
2864 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2866
2867 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2;
2869 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2871 }
2872
2873 glColor_premultiply @{$self->{outline}};
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2875
2876 glDisable GL_BLEND;
2877
2878 {
2879 local $self->{bg}; # do not draw background
2880 $self->SUPER::_draw;
2881 }
2882}
2883
2884#############################################################################
2885
2886package CFPlus::UI::ExperienceProgress;
2887
2888our @ISA = CFPlus::UI::Progress::;
2889
2890sub new {
2891 my ($class, %arg) = @_;
2892
2893 my $self = $class->SUPER::new (
2894 tooltip => sub {
2895 my ($self) = @_;
2896
2897 sprintf "level %d\n%s points\n%s next level\n%s to go",
2898 $self->{lvl},
2899 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}),
2902 },
2903 %arg
2904 );
2905
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN;
2908
2909 $self
2910}
2911
2912sub DESTROY {
2913 my ($self) = @_;
2914
2915 delete $::CONN->{on_exp_update}{$self+0}
2916 if $::CONN;
2917
2918 $self->SUPER::DESTROY;
2919}
2920
2921sub set_value {
2922 my ($self, $lvl, $exp) = @_;
2923
2924 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp;
2926
2927 my $v = -1;
2928
2929 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2930 my $l0 = $table->[$lvl - 1];
2931 my $l1 = $table->[$lvl];
2932
2933 $self->{nxt} = $l1;
2934
2935 $v = ($exp - $l0) / ($l1 - $l0);
2936 }
2937
2938 $self->SUPER::set_value ($v);
2939}
2940
2941#############################################################################
2942
2711package CFPlus::UI::Gauge; 2943package CFPlus::UI::Gauge;
2712 2944
2713our @ISA = CFPlus::UI::VBox::; 2945our @ISA = CFPlus::UI::VBox::;
2714 2946
2715sub new { 2947sub new {
2840 3072
2841 $self->SUPER::invoke_button_down ($ev, $x, $y); 3073 $self->SUPER::invoke_button_down ($ev, $x, $y);
2842 3074
2843 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3075 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2844 3076
2845 $self->invoke_mouse_motion ($ev, $x, $y) 3077 $self->invoke_mouse_motion ($ev, $x, $y);
3078
3079 1
2846} 3080}
2847 3081
2848sub invoke_mouse_motion { 3082sub invoke_mouse_motion {
2849 my ($self, $ev, $x, $y) = @_; 3083 my ($self, $ev, $x, $y) = @_;
2850 3084
2870 3104
2871 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3105 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2872 3106
2873 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2874 3108
2875 ! ! $delta 3109 1
2876} 3110}
2877 3111
2878sub update { 3112sub update {
2879 my ($self) = @_; 3113 my ($self) = @_;
2880 3114
3011} 3245}
3012 3246
3013sub size_request { 3247sub size_request {
3014 my ($self) = @_; 3248 my ($self) = @_;
3015 3249
3016 my ($empty, $slider) = @{ $self->{children} }; 3250 my ($empty, $slider) = $self->visible_children;
3017 3251
3018 local $self->{children} = [$empty, $slider]; 3252 local $self->{children} = [$empty, $slider];
3019 $self->SUPER::size_request 3253 $self->SUPER::size_request
3020} 3254}
3021 3255
3210 3444
3211 if ($y0 < $y + $h && $y < $y1) { 3445 if ($y0 < $y + $h && $y < $y1) {
3212 my $layout = $self->get_layout ($para); 3446 my $layout = $self->get_layout ($para);
3213 3447
3214 $layout->render ($para->{indent}, $y - $y0); 3448 $layout->render ($para->{indent}, $y - $y0);
3449 $layout->draw;
3215 3450
3216 if (my @w = @{ $para->{widget} }) { 3451 if (my @w = @{ $para->{widget} }) {
3217 my @s = $layout->get_shapes; 3452 my @s = $layout->get_shapes;
3218 3453
3219 for (@w) { 3454 for (@w) {
3341} 3576}
3342 3577
3343sub set_tooltip_from { 3578sub set_tooltip_from {
3344 my ($self, $widget) = @_; 3579 my ($self, $widget) = @_;
3345 3580
3346 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3347 if $widget->{tooltip} =~ /^#(.*)$/;
3348
3349 my $tooltip = $widget->{tooltip}; 3581 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583
3584 $tip = CFPlus::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/;
3350 3586
3351 if ($ENV{CFPLUS_DEBUG} & 2) { 3587 if ($ENV{CFPLUS_DEBUG} & 2) {
3352 $tooltip .= "\n\n" . (ref $widget) . "\n" 3588 $tip .= "\n\n" . (ref $widget) . "\n"
3353 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3354 . "req $widget->{req_w} $widget->{req_h}\n" 3590 . "req $widget->{req_w} $widget->{req_h}\n"
3355 . "visible $widget->{visible}"; 3591 . "visible $widget->{visible}";
3356 } 3592 }
3357 3593
3358 $tooltip =~ s/^\n+//; 3594 $tip =~ s/^\n+//;
3359 $tooltip =~ s/\n+$//; 3595 $tip =~ s/\n+$//;
3360 3596
3361 $self->add (new CFPlus::UI::Label 3597 $self->add (new CFPlus::UI::Label
3362 markup => $tooltip, 3598 markup => $tip,
3363 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3364 fontsize => 0.8, 3600 fontsize => 0.8,
3365 style => 1, # FLAG_INVERSE 3601 style => 1, # FLAG_INVERSE
3366 ellipsise => 0, 3602 ellipsise => 0,
3367 font => ($widget->{tooltip_font} || $::FONT_PROP), 3603 font => ($widget->{tooltip_font} || $::FONT_PROP),
3405} 3641}
3406 3642
3407sub _draw { 3643sub _draw {
3408 my ($self) = @_; 3644 my ($self) = @_;
3409 3645
3410 glTranslate 0.375, 0.375;
3411
3412 my ($w, $h) = @$self{qw(w h)}; 3646 my ($w, $h) = @$self{qw(w h)};
3413 3647
3414 glColor 1, 0.8, 0.4; 3648 glColor 1, 0.8, 0.4;
3415 glBegin GL_QUADS; 3649 glRect 0, 0, $w, $h;
3416 glVertex 0 , 0;
3417 glVertex 0 , $h;
3418 glVertex $w, $h;
3419 glVertex $w, 0;
3420 glEnd;
3421 3650
3422 glColor 0, 0, 0; 3651 glColor 0, 0, 0;
3423 glBegin GL_LINE_LOOP; 3652 glRect_lineloop .5, .5, $w + .5, $h + .5;
3424 glVertex 0 , 0;
3425 glVertex 0 , $h;
3426 glVertex $w, $h;
3427 glVertex $w, 0;
3428 glEnd;
3429 3653
3430 glTranslate 2 - 0.375, 2 - 0.375; 3654 glTranslate 2, 2;
3431 3655
3432 $self->SUPER::_draw; 3656 $self->SUPER::_draw;
3433} 3657}
3434 3658
3435############################################################################# 3659#############################################################################
3452 ); 3676 );
3453 3677
3454 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3455 CFPlus::weaken (my $widget = $self); 3679 CFPlus::weaken (my $widget = $self);
3456 3680
3457 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3458 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3459 $self->{timer} = Event->timer (
3460 parked => 1,
3461 cb => sub {
3462 return unless $::CONN && $widget; 3683 return unless $::CONN;
3463 3684
3685 my $w = $widget
3686 or return;
3687
3464 ++$widget->{frame}; 3688 ++$w->{frame};
3465 $widget->update_face; 3689 $w->update_face;
3690
3691 # somehow, $widget can go away
3466 $widget->update; 3692 $w->update;
3467
3468 $widget->update_timer; 3693 $w->update_timer;
3469 },
3470 ); 3694 };
3471 3695
3472 $self->update_face; 3696 $self->update_face;
3473 $self->update_timer; 3697 $self->update_timer;
3474 } 3698 }
3475 3699
3480 my ($self) = @_; 3704 my ($self) = @_;
3481 3705
3482 return unless $self->{timer}; 3706 return unless $self->{timer};
3483 3707
3484 if ($self->{visible}) { 3708 if ($self->{visible}) {
3485 $self->{timer}->at (
3486 $self->{anim_start}
3487 + $self->{animspeed}
3488 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3489 );
3490 $self->{timer}->start; 3709 $self->{timer}->start;
3491 } else { 3710 } else {
3492 $self->{timer}->stop; 3711 $self->{timer}->stop;
3493 } 3712 }
3494} 3713}
3495 3714
3496sub update_face { 3715sub update_face {
3497 my ($self) = @_; 3716 my ($self) = @_;
3498 3717
3499 return unless $::CONN; 3718 if ($::CONN) {
3500
3501 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3719 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3502 if ($anim && @$anim) { 3720 if ($anim && @$anim) {
3503 delete $self->{wait_face};
3504 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3721 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3722 delete $self->{face_change_cb};
3723
3505 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3724 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3725 unless ($tex->{name} || $tex->{loading}) {
3726 $tex->upload (sub { $self->reconfigure });
3727 }
3728 }
3729 }
3506 } 3730 }
3507 } 3731 }
3508} 3732}
3509 3733
3510sub size_request { 3734sub size_request {
3511 my ($self) = @_; 3735 my ($self) = @_;
3512 3736
3513 if ($::CONN) { 3737 if ($::CONN) {
3514 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3738 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3515 if (my $tex = $::CONN->{texture}[$faceid]) { 3739 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3516 $self->{tex} = $tex; 3740 if ($tex->{name}) {
3517 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3741 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3518 } else { 3742 } elsif (!$tex->{loading}) {
3519 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3743 $tex->upload (sub { $self->reconfigure });
3520 $self->realloc;
3521 }); 3744 }
3522 } 3745 }
3746
3747 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3523 } 3748 }
3524 } 3749 }
3525 3750
3526 ($self->{size_w} || 8, $self->{size_h} || 8) 3751 ($self->{size_w} || 8, $self->{size_h} || 8)
3527} 3752}
3607 children => [ 3832 children => [
3608 (new CFPlus::UI::Label markup => $left, expand => 1), 3833 (new CFPlus::UI::Label markup => $left, expand => 1),
3609 (new CFPlus::UI::Label markup => $right, align => +1), 3834 (new CFPlus::UI::Label markup => $right, align => +1),
3610 ], 3835 ],
3611 ; 3836 ;
3612 3837
3613 } else { 3838 } else {
3614 $widget = new CFPlus::UI::Label 3839 $widget = new CFPlus::UI::Label
3615 can_hover => 1, 3840 can_hover => 1,
3616 can_events => 1, 3841 can_events => 1,
3617 markup => $widget, 3842 markup => $widget,
3742 3967
3743############################################################################# 3968#############################################################################
3744 3969
3745package CFPlus::UI::Notebook; 3970package CFPlus::UI::Notebook;
3746 3971
3972use CFPlus::OpenGL;
3973
3747our @ISA = CFPlus::UI::VBox::; 3974our @ISA = CFPlus::UI::VBox::;
3748 3975
3749sub new { 3976sub new {
3750 my $class = shift; 3977 my $class = shift;
3751 3978
3752 my $self = $class->SUPER::new ( 3979 my $self = $class->SUPER::new (
3753 buttonbar => (new CFPlus::UI::Buttonbar), 3980 buttonbar => (new CFPlus::UI::Buttonbar),
3754 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3981 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2],
3755 # filter => # will be put between multiplexer and $self 3983 # filter => # will be put between multiplexer and $self
3756 @_, 3984 @_,
3757 ); 3985 );
3758 3986
3759 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3987 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3760 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3988 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3761 3989
3990 {
3991 Scalar::Util::weaken (my $wself = $self);
3992
3993 $self->{multiplexer}->connect (c_add => sub {
3994 my ($mplex, $widgets) = @_;
3995
3996 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do {
3999 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
4001 ? $child->{c_tab}
4002 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003
4004 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child);
4006 });
4007
4008 $tab
4009 };
4010
4011 $self->{buttonbar}->add ($child->{c_tab_});
4012 }
4013 });
4014
4015 $self->{multiplexer}->connect (c_remove => sub {
4016 my ($mplex, $widgets) = @_;
4017
4018 for my $child (@$widgets) {
4019 $wself->{buttonbar}->remove ($child->{c_tab_});
4020 }
4021 });
4022 }
4023
3762 $self 4024 $self
3763} 4025}
3764 4026
3765sub add { 4027sub add {
4028 my ($self, @widgets) = @_;
4029
4030 $self->{multiplexer}->add (@widgets)
4031}
4032
4033sub remove {
4034 my ($self, @widgets) = @_;
4035
4036 $self->{multiplexer}->remove (@widgets)
4037}
4038
4039sub pages {
4040 my ($self) = @_;
4041 $self->{multiplexer}->children
4042}
4043
4044sub add_tab {
3766 my ($self, $title, $widget, $tooltip) = @_; 4045 my ($self, $title, $widget, $tooltip) = @_;
3767 4046
3768 CFPlus::weaken $self; 4047 $title = [$title, $tooltip] unless ref $title;
4048 $widget->{c_tab} = $title;
3769 4049
3770 unless (ref $title) {
3771 $title = new CFPlus::UI::Button
3772 markup => $title,
3773 tooltip => $tooltip,
3774 );
3775 }
3776
3777 $title->connect (activate => sub { $self->set_current_page ($widget) });
3778 $self->{buttonbar}->add ($title);
3779
3780 $self->{multiplexer}->add ($widget); 4050 $self->add ($widget);
3781} 4051}
3782 4052
3783sub get_current_page { 4053sub get_current_page {
3784 my ($self) = @_; 4054 my ($self) = @_;
3785 4055
3789sub set_current_page { 4059sub set_current_page {
3790 my ($self, $page) = @_; 4060 my ($self, $page) = @_;
3791 4061
3792 $self->{multiplexer}->set_current_page ($page); 4062 $self->{multiplexer}->set_current_page ($page);
3793 $self->emit (page_changed => $self->{multiplexer}{current}); 4063 $self->emit (page_changed => $self->{multiplexer}{current});
4064}
4065
4066sub _draw {
4067 my ($self) = @_;
4068
4069 $self->SUPER::_draw ();
4070
4071 if (my $cur = $self->{multiplexer}{current}) {
4072 if ($cur = $cur->{c_tab_}) {
4073 glTranslate $self->{buttonbar}{x} + $cur->{x},
4074 $self->{buttonbar}{y} + $cur->{y};
4075 glLineWidth 3;
4076 #glEnable GL_BLEND;
4077 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4078 glColor @{$self->{active_outline}};
4079 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4080 glLineWidth 1;
4081 #glDisable GL_BLEND;
4082 }
4083 }
3794} 4084}
3795 4085
3796############################################################################# 4086#############################################################################
3797 4087
3798package CFPlus::UI::Selector; 4088package CFPlus::UI::Selector;
3830} 4120}
3831 4121
3832sub _set_value { 4122sub _set_value {
3833 my ($self, $value) = @_; 4123 my ($self, $value) = @_;
3834 4124
3835 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4125 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4126 $item ||= $self->{options}[0]
3836 or return; 4127 or return;
3837 4128
3838 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
3839 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
3840 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
3841} 4132}
3842 4133
3843sub set_value { 4134sub set_value {
3844 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
3845 4136
3846 return unless $self->{value} ne $value; 4137 return unless $self->{value} ne $value;
3847 4138
3848 $self->_set_value ($value); 4139 $self->_set_value ($value);
3849 $self->emit (changed => $value); 4140 $self->emit (changed => $value);
4141}
4142
4143sub set_options {
4144 my ($self, $options) = @_;
4145
4146 $self->{options} = $options;
4147 $self->_set_value ($self->{value});
3850} 4148}
3851 4149
3852############################################################################# 4150#############################################################################
3853 4151
3854package CFPlus::UI::Statusbox; 4152package CFPlus::UI::Statusbox;
3863 @_, 4161 @_,
3864 ); 4162 );
3865 4163
3866 CFPlus::weaken (my $this = $self); 4164 CFPlus::weaken (my $this = $self);
3867 4165
3868 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3869 4167
3870 $self 4168 $self
3871} 4169}
3872 4170
3873sub reorder { 4171sub reorder {
3881 4179
3882 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
3883 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3884 } 4182 }
3885 4183
4184 $self->{timer}->set (1, 1);
4185
3886 my @widgets; 4186 my @widgets;
3887 4187
3888 my @items = sort { 4188 my @items = sort {
3889 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
3890 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
3891 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
3892
3893 $self->{timer}->interval (1);
3894 4192
3895 my $count = 10 + 1; 4193 my $count = 10 + 1;
3896 for my $item (@items) { 4194 for my $item (@items) {
3897 last unless --$count; 4195 last unless --$count;
3898 4196
3922 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3923 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3924 $label->update; 4222 $label->update;
3925 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
3926 if $diff < 1; 4224 if $diff < 1;
3927 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
3928 } else { 4226 } else {
3929 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
3930 } 4228 }
3931 4229
3932 push @widgets, $label; 4230 push @widgets, $label;
4068} 4366}
4069 4367
4070sub update { 4368sub update {
4071 my ($self) = @_; 4369 my ($self) = @_;
4072 4370
4073 $::WANT_REFRESH++; 4371 $::WANT_REFRESH = 1;
4074} 4372}
4075 4373
4076sub add { 4374sub add {
4077 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4078 4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines