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.371 by root, Sun Jul 15 22:07:48 2007 UTC vs.
Revision 1.407 by root, Sat Jul 21 23:31:31 2007 UTC

113 } 113 }
114 114
115 if ($GRAB) { 115 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 116 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 117 # mousewheel
118 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9; 118 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
120
121 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta;
123
120 $GRAB->emit (mouse_wheel => $ev); 124 $GRAB->emit (mouse_wheel => $ev);
121 } else { 125 } else {
122 $GRAB->emit (button_down => $ev) 126 $GRAB->emit (button_down => $ev)
123 } 127 }
124 } 128 }
547 551
548 # parent 552 # parent
549 $self->{parent} && $self->{parent}->emit ($signal, @args) 553 $self->{parent} && $self->{parent}->emit ($signal, @args)
550} 554}
551 555
552sub find_widget { 556#sub find_widget {
553 my ($self, $x, $y) = @_; 557# in .xs
554
555 return () unless $self->{can_events};
556
557 return $self
558 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
559 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
560
561 ()
562}
563 558
564sub set_parent { 559sub set_parent {
565 my ($self, $parent) = @_; 560 my ($self, $parent) = @_;
566 561
567 CFPlus::weaken ($self->{parent} = $parent); 562 CFPlus::weaken ($self->{parent} = $parent);
598 593
599# using global variables seems a bit hacky, but passing through all drawing 594# using global variables seems a bit hacky, but passing through all drawing
600# functions seems pointless. 595# functions seems pointless.
601our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
602 597
603sub draw { 598#sub draw {
604 my ($self) = @_; 599#CFPlus.xs
605
606 return unless $self->{h} && $self->{w};
607
608 # update screen rectangle
609 local $draw_x = $draw_x + $self->{x};
610 local $draw_y = $draw_y + $self->{y};
611
612 # skip widgets that are entirely outside the drawing area
613 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
614 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
615
616 glPushMatrix;
617 glTranslate $self->{x}, $self->{y}, 0;
618
619 if ($self == $HOVER && $self->{can_hover}) {
620 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
621 glEnable GL_BLEND;
622 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
623 glBegin GL_QUADS;
624 glVertex 0 , 0;
625 glVertex $self->{w}, 0;
626 glVertex $self->{w}, $self->{h};
627 glVertex 0 , $self->{h};
628 glEnd;
629 glDisable GL_BLEND;
630 }
631
632 if ($ENV{CFPLUS_DEBUG} & 1) {
633 glPushMatrix;
634 glColor 1, 1, 0, 1;
635 glTranslate 0.375, 0.375;
636 glBegin GL_LINE_LOOP;
637 glVertex 0 , 0;
638 glVertex $self->{w} - 1, 0;
639 glVertex $self->{w} - 1, $self->{h} - 1;
640 glVertex 0 , $self->{h} - 1;
641 glEnd;
642 glPopMatrix;
643 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
644 }
645
646 $self->_draw;
647 glPopMatrix;
648}
649 600
650sub _draw { 601sub _draw {
651 my ($self) = @_; 602 my ($self) = @_;
652 603
653 warn "no draw defined for $self\n"; 604 warn "no draw defined for $self\n";
654} 605}
655 606
656my $cntx;#d#
657sub DESTROY { 607sub DESTROY {
658 my ($self) = @_; 608 my ($self) = @_;
659 609
660 return if CFPlus::in_destruct; 610 return if CFPlus::in_destruct;
661 611
746 can_events => 0, 696 can_events => 0,
747 %arg, 697 %arg,
748 ); 698 );
749 699
750 $self->add (@$children) 700 $self->add (@$children)
751 if $children; 701 if $children && @$children;
752 702
753 $self 703 $self
754} 704}
755 705
756sub realloc { 706sub realloc {
765 my ($self, @widgets) = @_; 715 my ($self, @widgets) = @_;
766 716
767 $_->set_parent ($self) 717 $_->set_parent ($self)
768 for @widgets; 718 for @widgets;
769 719
720 # TODO: only do this in widgets that need it, e.g. root, fixed
770 use sort 'stable'; 721 use sort 'stable';
771 722
772 $self->{children} = [ 723 $self->{children} = [
773 sort { $a->{z} <=> $b->{z} } 724 sort { $a->{z} <=> $b->{z} }
774 @{$self->{children}}, @widgets 725 @{$self->{children}}, @widgets
775 ]; 726 ];
776 727
777 $self->realloc; 728 $self->realloc;
729
730 map $_+0, @widgets
778} 731}
779 732
780sub children { 733sub children {
781 @{ $_[0]{children} } 734 @{ $_[0]{children} }
782} 735}
793} 746}
794 747
795sub clear { 748sub clear {
796 my ($self) = @_; 749 my ($self) = @_;
797 750
798 my $children = delete $self->{children}; 751 my $children = $self->{children};
799 $self->{children} = []; 752 $self->{children} = [];
800 753
801 for (@$children) { 754 for (@$children) {
802 delete $_->{parent}; 755 delete $_->{parent};
803 $_->hide; 756 $_->hide;
943} 896}
944 897
945############################################################################# 898#############################################################################
946 899
947package CFPlus::UI::ViewPort; 900package CFPlus::UI::ViewPort;
901
902use List::Util qw(min max);
948 903
949our @ISA = CFPlus::UI::Window::; 904our @ISA = CFPlus::UI::Window::;
950 905
951sub new { 906sub new {
952 my $class = shift; 907 my $class = shift;
961sub size_request { 916sub size_request {
962 my ($self) = @_; 917 my ($self) = @_;
963 918
964 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 919 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
965 920
966 $w = 10 if $self->{scroll_x}; 921 $w = 1 if $self->{scroll_x};
967 $h = 10 if $self->{scroll_y}; 922 $h = 1 if $self->{scroll_y};
968 923
969 ($w, $h) 924 ($w, $h)
970} 925}
971 926
972sub invoke_size_allocate { 927sub invoke_size_allocate {
984} 939}
985 940
986sub set_offset { 941sub set_offset {
987 my ($self, $x, $y) = @_; 942 my ($self, $x, $y) = @_;
988 943
944 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
945 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
946
947 if ($x != $self->{view_x} or $y != $self->{view_y}) {
989 $self->{view_x} = int $x; 948 $self->{view_x} = $x;
990 $self->{view_y} = int $y; 949 $self->{view_y} = $y;
991 950
951 $self->emit (changed => $x, $y);
992 $self->update; 952 $self->update;
953 }
993} 954}
994 955
995# hmm, this does not work for topleft of $self... but we should not ask for that 956# hmm, this does not work for topleft of $self... but we should not ask for that
996sub coord2local { 957sub coord2local {
997 my ($self, $x, $y) = @_; 958 my ($self, $x, $y) = @_;
1012 my ($self, $x, $y) = @_; 973 my ($self, $x, $y) = @_;
1013 974
1014 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 975 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1015 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 976 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1016 ) { 977 ) {
1017 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 978 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1018 } else { 979 } else {
1019 $self->CFPlus::UI::Base::find_widget ($x, $y) 980 $self->CFPlus::UI::Base::find_widget ($x, $y)
1020 } 981 }
1021} 982}
1022 983
1043 my $child = delete $arg{child}; 1004 my $child = delete $arg{child};
1044 1005
1045 my $self; 1006 my $self;
1046 1007
1047 my $hslider = new CFPlus::UI::Slider 1008 my $hslider = new CFPlus::UI::Slider
1009 col => 0,
1010 row => 1,
1048 vertical => 0, 1011 vertical => 0,
1049 range => [0, 0, 1, 0.01], # HACK fix 1012 range => [0, 0, 1, 0.01], # HACK fix
1050 on_changed => sub { 1013 on_changed => sub {
1051 $self->{hpos} = $_[1]; 1014 $self->{hpos} = $_[1];
1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1015 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1053 }, 1016 },
1054 ; 1017 ;
1055 1018
1056 my $vslider = new CFPlus::UI::Slider 1019 my $vslider = new CFPlus::UI::Slider
1020 col => 1,
1021 row => 0,
1057 vertical => 1, 1022 vertical => 1,
1058 range => [0, 0, 1, 0.01], # HACK fix 1023 range => [0, 0, 1, 0.01], # HACK fix
1059 on_changed => sub { 1024 on_changed => sub {
1060 $self->{vpos} = $_[1]; 1025 $self->{vpos} = $_[1];
1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1062 }, 1027 },
1063 ; 1028 ;
1064 1029
1065 $self = $class->SUPER::new ( 1030 $self = $class->SUPER::new (
1066 vp => (new CFPlus::UI::ViewPort expand => 1), 1031 scroll_x => 0,
1032 scroll_y => 1,
1067 can_events => 1, 1033 can_events => 1,
1068 hslider => $hslider, 1034 hslider => $hslider,
1069 vslider => $vslider, 1035 vslider => $vslider,
1070 col_expand => [1, 0], 1036 col_expand => [1, 0],
1071 row_expand => [1, 0], 1037 row_expand => [1, 0],
1072 %arg, 1038 %arg,
1073 ); 1039 );
1074 1040
1041 $self->{vp} = new CFPlus::UI::ViewPort
1042 col => 0,
1043 row => 0,
1044 expand => 1,
1045 scroll_x => $self->{scroll_x},
1046 scroll_y => $self->{scroll_y},
1047 on_changed => sub {
1048 my ($vp, $x, $y) = @_;
1049
1050 $vp->{parent}{hslider}->set_value ($x);
1051 $vp->{parent}{vslider}->set_value ($y);
1052
1053 0
1054 },
1055 on_size_allocate => sub {
1056 my ($vp, $w, $h) = @_;
1057 $vp->{parent}->update_slider;
1058 0
1059 },
1060 ;
1061
1075 $self->SUPER::add (0, 0, $self->{vp}); 1062 $self->SUPER::add ($self->{vp});
1076 $self->SUPER::add (1, 0, $self->{vslider});
1077 $self->SUPER::add (0, 1, $self->{hslider});
1078 1063
1079 $self->add ($child) if $child; 1064 $self->add ($child) if $child;
1080 1065
1081 $self 1066 $self
1082} 1067}
1083
1084#TODO# update range on size_allocate depending on child
1085 1068
1086sub add { 1069sub add {
1087 my ($self, $widget) = @_; 1070 my ($self, $widget) = @_;
1088 1071
1089 $self->{vp}->add ($self->{child} = $widget); 1072 $self->{vp}->add ($self->{child} = $widget);
1090} 1073}
1091 1074
1075sub update_slider {
1076 my ($self) = @_;
1077
1078 my $child = ($self->{vp} or return)->child;
1079
1080 if ($self->{scroll_x}) {
1081 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1082 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1083
1084 my $visible = $w1 > $w2;
1085 if ($visible != $self->{hslider_visible}) {
1086 $self->{hslider_visible} = $visible;
1087 $visible ? $self->SUPER::add ($self->{hslider})
1088 : $self->SUPER::remove ($self->{hslider});
1089 }
1090 }
1091
1092 if ($self->{scroll_y}) {
1093 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1094 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1095
1096 my $visible = $h1 > $h2;
1097 if ($visible != $self->{vslider_visible}) {
1098 $self->{vslider_visible} = $visible;
1099 $visible ? $self->SUPER::add ($self->{vslider})
1100 : $self->SUPER::remove ($self->{vslider});
1101 }
1102 }
1103}
1104
1105sub start_dragging {
1106 my ($self, $ev) = @_;
1107
1108 $self->grab_focus;
1109
1110 my $ox = $self->{vp}{view_x};
1111 my $oy = $self->{vp}{view_y};
1112
1113 $self->{motion} = sub {
1114 my ($ev, $x, $y) = @_;
1115
1116 $ox -= $ev->{xrel};
1117 $oy -= $ev->{yrel};
1118
1119 $self->{vp}->set_offset ($ox, $oy);
1120 };
1121}
1122
1092sub invoke_mouse_wheel { 1123sub invoke_mouse_wheel {
1093 my ($self, $ev) = @_; 1124 my ($self, $ev) = @_;
1094 1125
1095 return 0 unless $ev->{dy}; # only vertical movements
1096
1097 $self->{vslider}->emit (mouse_wheel => $ev); 1126 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1127 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1098 1128
1099 1 1129 1
1100} 1130}
1101 1131
1102sub update_slider { 1132sub invoke_button_down {
1103 my ($self) = @_; 1133 my ($self, $ev, $x, $y) = @_;
1104 1134
1105 my $child = ($self->{vp} or return)->child; 1135 if ($ev->{button} == 2) {
1136 $self->start_dragging ($ev);
1137 return 1;
1138 }
1106 1139
1107 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1140 0
1108 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1109 #$self->{hslider}->set_visibility ($w1 != $w2);
1110
1111 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1112 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1113 #$self->{vslider}->set_visibility ($h1 != $h2);
1114} 1141}
1115 1142
1116sub update { 1143sub invoke_button_up {
1117 my ($self) = @_; 1144 my ($self, $ev, $x, $y) = @_;
1118 1145
1119 $self->SUPER::update; 1146 if (delete $self->{motion}) {
1120 $self->update_slider; 1147 return 1;
1148 }
1149
1150 0
1151}
1152
1153sub invoke_mouse_motion {
1154 my ($self, $ev, $x, $y) = @_;
1155
1156 if ($self->{motion}) {
1157 $self->{motion}->($ev, $x, $y);
1158 return 1;
1159 }
1160
1161 0
1121} 1162}
1122 1163
1123sub invoke_size_allocate { 1164sub invoke_size_allocate {
1124 my ($self, $w, $h) = @_; 1165 my ($self, $w, $h) = @_;
1125 1166
1281 1322
1282my @border = 1323my @border =
1283 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1324 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1284 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1325 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1285 1326
1327my @icon =
1328 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1329 qw(x1_move.png x1_resize.png);
1330
1286sub new { 1331sub new {
1287 my ($class, %arg) = @_; 1332 my ($class, %arg) = @_;
1288 1333
1289 my $self = $class->SUPER::new ( 1334 my $self = $class->SUPER::new (
1290 bg => [1, 1, 1, 1], 1335 bg => [1, 1, 1, 1],
1458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1503 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1459 1504
1460 my $border = $self->border; 1505 my $border = $self->border;
1461 1506
1462 glColor @{ $self->{border_bg} }; 1507 glColor @{ $self->{border_bg} };
1463 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1508 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1464 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1509 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1465 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1510 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1466 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1511 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1512
1513 # move
1514 my $w2 = ($w - $border) * .5;
1515 my $h2 = ($h - $border) * .5;
1516 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1517 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1518 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1519
1520 # resize
1521 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1522 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1523 unless $self->{has_close_button};
1524 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1525 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1467 1526
1468 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1527 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1469 glColor @{ $self->{bg} }; 1528 glColor @{ $self->{bg} };
1470 1529
1471 # TODO: repeat texture not scale 1530 # TODO: repeat texture not scale
1492 1551
1493############################################################################# 1552#############################################################################
1494 1553
1495package CFPlus::UI::Table; 1554package CFPlus::UI::Table;
1496 1555
1497our @ISA = CFPlus::UI::Base::; 1556our @ISA = CFPlus::UI::Container::;
1498 1557
1499use List::Util qw(max sum); 1558use List::Util qw(max sum);
1500 1559
1501use CFPlus::OpenGL; 1560use CFPlus::OpenGL;
1502 1561
1508 row_expand => [], 1567 row_expand => [],
1509 @_, 1568 @_,
1510 ) 1569 )
1511} 1570}
1512 1571
1513sub children {
1514 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1515}
1516
1517sub add { 1572sub add {
1573 my ($self, @widgets) = @_;
1574
1575 for my $child (@widgets) {
1576 $child->{rowspan} ||= 1;
1577 $child->{colspan} ||= 1;
1578 }
1579
1580 $self->SUPER::add (@widgets);
1581}
1582
1583sub add_at {
1518 my ($self) = shift; 1584 my $self = shift;
1585
1586 my @widgets;
1519 1587
1520 while (@_) { 1588 while (@_) {
1521 my ($x, $y, $child) = splice @_, 0, 3, (); 1589 my ($col, $row, $child) = splice @_, 0, 3, ();
1522 $child->set_parent ($self);
1523 $self->{children}[$y][$x] = $child;
1524 }
1525 1590
1526 $self->{force_realloc} = 1; 1591 $child->{row} = $row;
1527 $self->{force_size_alloc} = 1; 1592 $child->{col} = $col;
1528 $self->realloc;
1529}
1530 1593
1531sub remove { 1594 push @widgets, $child;
1595 }
1596
1597 $self->add (@widgets);
1598}
1599
1600sub get_wh {
1532 my ($self, $child) = @_; 1601 my ($self) = @_;
1533 1602
1534 # TODO: not yet implemented 1603 my (@w, @h);
1535}
1536
1537# TODO: move to container class maybe? send children a signal on removal?
1538sub clear {
1539 my ($self) = @_;
1540 1604
1541 my @children = $self->children; 1605 my @children = $self->children;
1542 delete $self->{children}; 1606
1607 # first pass, columns
1608 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) {
1609 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)};
1610
1611 my $sw = sum @w[$c .. $c + $cs - 1];
1612
1613 if ($w > $sw) {
1614 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1615 }
1543 1616 }
1544 for (@children) {
1545 delete $_->{parent};
1546 $_->hide;
1547 }
1548 1617
1549 $self->realloc; 1618 # second pass, rows
1550} 1619 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) {
1551
1552sub get_wh {
1553 my ($self) = @_;
1554
1555 my (@w, @h);
1556
1557 for my $y (0 .. $#{$self->{children}}) {
1558 my $row = $self->{children}[$y]
1559 or next;
1560
1561 for my $x (0 .. $#$row) {
1562 my $widget = $row->[$x]
1563 or next;
1564 my ($w, $h) = @$widget{qw(req_w req_h)}; 1620 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)};
1565 1621
1566 $w[$x] = max $w[$x], $w; 1622 my $sh = sum @h[$r .. $r + $rs - 1];
1567 $h[$y] = max $h[$y], $h; 1623
1624 if ($h > $sh) {
1625 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1568 } 1626 }
1569 } 1627 }
1570 1628
1571 (\@w, \@h) 1629 (\@w, \@h)
1572} 1630}
1588 my ($ws, $hs) = $self->get_wh; 1646 my ($ws, $hs) = $self->get_wh;
1589 1647
1590 my $req_w = (sum @$ws) || 1; 1648 my $req_w = (sum @$ws) || 1;
1591 my $req_h = (sum @$hs) || 1; 1649 my $req_h = (sum @$hs) || 1;
1592 1650
1593 # TODO: nicer code 1651 # now linearly scale the rows/columns to the allocated size
1594 my @col_expand = @{$self->{col_expand}}; 1652 my @col_expand = @{$self->{col_expand}};
1595 @col_expand = (1) x @$ws unless @col_expand; 1653 @col_expand = (1) x @$ws unless @col_expand;
1596 my $col_expand = (sum @col_expand) || 1; 1654 my $col_expand = (sum @col_expand) || 1;
1597 1655
1598 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1656 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1605 1663
1606 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1664 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1607 1665
1608 CFPlus::UI::harmonize $hs; 1666 CFPlus::UI::harmonize $hs;
1609 1667
1610 my $y; 1668 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1669 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1611 1670
1612 for my $r (0 .. $#{$self->{children}}) { 1671 for my $widget ($self->children) {
1613 my $row = $self->{children}[$r] 1672 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)};
1614 or next;
1615 1673
1616 my $x = 0; 1674 $widget->configure (
1617 my $row_h = $hs->[$r]; 1675 $x[$c], $y[$r],
1676 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1618 1677 );
1619 for my $c (0 .. $#$row) { 1678 }
1620 my $col_w = $ws->[$c];
1621 1679
1622 if (my $widget = $row->[$c]) { 1680 1
1623 $widget->configure ($x, $y, $col_w, $row_h); 1681}
1624 }
1625 1682
1626 $x += $col_w; 1683#############################################################################
1684
1685package CFPlus::UI::Fixed;
1686
1687use List::Util qw(min max);
1688
1689our @ISA = CFPlus::UI::Container::;
1690
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($$$) {
1699 my ($mode, $val, $max) = @_;
1700
1701 $mode eq "abs" ? $val
1702 : $mode eq "rel" ? $val * $max
1703 : 0
1704}
1705
1706sub size_request {
1707 my ($self) = @_;
1708
1709 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1710
1711 # determine overall size by querying abs widgets
1712 for my $child ($self->visible_children) {
1713 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1714
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;
1720 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1627 } 1721 }
1722 }
1628 1723
1629 $y += $row_h; 1724 my $W = $x2 - $x1;
1725 my $H = $y2 - $y1;
1726
1727 # now layout remaining widgets
1728 for my $child ($self->visible_children) {
1729 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1730
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;
1738 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1739 }
1740 }
1741
1742 my $W = $x2 - $x1;
1743 my $H = $y2 - $y1;
1744
1745 ($W, $H)
1746}
1747
1748sub invoke_size_allocate {
1749 my ($self, $W, $H) = @_;
1750
1751 for my $child ($self->visible_children) {
1752 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1753
1754 $x = _scale $pos, $x, $W;
1755 $y = _scale $pos, $x, $H;
1756 $w = _scale $size, $w, $W;
1757 $h = _scale $size, $h, $H;
1758
1759 $child->configure ($x, $y, $w, $h);
1630 } 1760 }
1631 1761
1632 1 1762 1
1633}
1634
1635sub find_widget {
1636 my ($self, $x, $y) = @_;
1637
1638 $x -= $self->{x};
1639 $y -= $self->{y};
1640
1641 my $res;
1642
1643 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1644 $res = $_->find_widget ($x, $y)
1645 and return $res;
1646 }
1647
1648 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1649}
1650
1651sub _draw {
1652 my ($self) = @_;
1653
1654 for (grep $_, @{$self->{children}}) {
1655 $_->draw for grep $_, @$_;
1656 }
1657} 1763}
1658 1764
1659############################################################################# 1765#############################################################################
1660 1766
1661package CFPlus::UI::Box; 1767package CFPlus::UI::Box;
1921 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1922 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1923 : ($self->{h} - $size->[1]) * 0.5); 2029 : ($self->{h} - $size->[1]) * 0.5);
1924 }; 2030 };
1925 2031
1926 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2032# unless ($self->{list}) {
1927 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2033# $self->{list} = CFPlus::OpenGL::glGenList;
2034# CFPlus::OpenGL::glNewList $self->{list};
2035# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2036# CFPlus::OpenGL::glEndList;
2037# }
2038#
2039# CFPlus::OpenGL::glCallList $self->{list};
1928 2040
1929 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2041 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1930} 2042}
2043
2044#sub destroy {
2045# my ($self) = @_;
2046#
2047# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2048#
2049# $self->SUPER::destroy;
2050#}
1931 2051
1932############################################################################# 2052#############################################################################
1933 2053
1934package CFPlus::UI::EntryBase; 2054package CFPlus::UI::EntryBase;
1935 2055
2219 2339
2220use CFPlus::OpenGL; 2340use CFPlus::OpenGL;
2221 2341
2222my @tex = 2342my @tex =
2223 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2343 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2224 qw(b1_button_active.png); 2344 qw(b1_button_inactive.png b1_button_active.png);
2225 2345
2226sub new { 2346sub new {
2227 my $class = shift; 2347 my $class = shift;
2228 2348
2229 $class->SUPER::new ( 2349 $class->SUPER::new (
2230 padding_x => 4, 2350 padding_x => 4,
2231 padding_y => 4, 2351 padding_y => 4,
2232 fg => [1, 1, 1], 2352 fg => [1.0, 1.0, 1.0],
2233 active_fg => [0, 0, 1], 2353 active_fg => [0.8, 0.8, 0.8],
2234 can_hover => 1, 2354 can_hover => 1,
2235 align => 0, 2355 align => 0,
2236 valign => 0, 2356 valign => 0,
2237 can_events => 1, 2357 can_events => 1,
2238 @_ 2358 @_
2256 2376
2257 glEnable GL_TEXTURE_2D; 2377 glEnable GL_TEXTURE_2D;
2258 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2378 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2259 glColor 0, 0, 0, 1; 2379 glColor 0, 0, 0, 1;
2260 2380
2381 my $tex = $tex[$GRAB == $self];
2261 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2382 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2262 2383
2263 glDisable GL_TEXTURE_2D; 2384 glDisable GL_TEXTURE_2D;
2264 2385
2265 $self->SUPER::_draw; 2386 $self->SUPER::_draw;
2266} 2387}
3321 3442
3322sub new { 3443sub new {
3323 my $class = shift; 3444 my $class = shift;
3324 3445
3325 my $self = $class->SUPER::new ( 3446 my $self = $class->SUPER::new (
3447 size_w => 32,
3448 size_h => 8,
3326 aspect => 1, 3449 aspect => 1,
3327 can_events => 0, 3450 can_events => 0,
3328 @_, 3451 @_,
3329 ); 3452 );
3330 3453
3331 if ($self->{anim} && $self->{animspeed}) { 3454 if ($self->{anim} && $self->{animspeed}) {
3332 CFPlus::weaken (my $widget = $self); 3455 CFPlus::weaken (my $widget = $self);
3333 3456
3457 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3458 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3334 $self->{timer} = Event->timer ( 3459 $self->{timer} = Event->timer (
3335 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3336 hard => 1, 3460 parked => 1,
3337 interval => $self->{animspeed},
3338 cb => sub { 3461 cb => sub {
3339 delete $self->{wait_face}; 3462 return unless $::CONN && $widget;
3463
3340 ++$widget->{frame}; 3464 ++$widget->{frame};
3465 $widget->update_face;
3341 $widget->update; 3466 $widget->update;
3467
3468 $widget->update_timer;
3342 }, 3469 },
3343 ); 3470 );
3471
3472 $self->update_face;
3473 $self->update_timer;
3344 } 3474 }
3345 3475
3346 $self 3476 $self
3347} 3477}
3348 3478
3479sub update_timer {
3480 my ($self) = @_;
3481
3482 return unless $self->{timer};
3483
3484 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;
3491 } else {
3492 $self->{timer}->stop;
3493 }
3494}
3495
3496sub update_face {
3497 my ($self) = @_;
3498
3499 return unless $::CONN;
3500
3501 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3502 if ($anim && @$anim) {
3503 delete $self->{wait_face};
3504 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3505 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3506 }
3507 }
3508}
3509
3349sub size_request { 3510sub size_request {
3350 (32, 8) 3511 my ($self) = @_;
3512
3513 if ($::CONN) {
3514 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3515 if (my $tex = $::CONN->{texture}[$faceid]) {
3516 $self->{tex} = $tex;
3517 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3518 } else {
3519 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3520 $self->realloc;
3521 });
3522 }
3523 }
3524 }
3525
3526 ($self->{size_w} || 8, $self->{size_h} || 8)
3351} 3527}
3352 3528
3353sub update { 3529sub update {
3354 my ($self) = @_; 3530 my ($self) = @_;
3355 3531
3356 return unless $self->{visible}; 3532 return unless $self->{visible};
3357 3533
3358 $self->SUPER::update; 3534 $self->SUPER::update;
3359} 3535}
3360 3536
3537sub invoke_visibility_change {
3538 my ($self) = @_;
3539
3540 $self->update_timer;
3541
3542 0
3543}
3544
3361sub _draw { 3545sub _draw {
3362 my ($self) = @_; 3546 my ($self) = @_;
3363 3547
3364 return unless $::CONN;
3365
3366 $self->SUPER::_draw; 3548 $self->SUPER::_draw;
3367 3549
3368 my $face; 3550 if (my $tex = $self->{tex}) {
3369
3370 if ($self->{frame}) {
3371 my $anim = $::CONN->{anim}[$self->{anim}];
3372
3373 $face = $anim->[ $self->{frame} % @$anim ]
3374 if $anim && @$anim;
3375 }
3376
3377 my $faceid = $::CONN->{faceid}[$face || $self->{face}]
3378 or return;
3379
3380 my $tex = $::CONN->{texture}[$faceid];
3381
3382 if ($tex) {
3383 glEnable GL_TEXTURE_2D; 3551 glEnable GL_TEXTURE_2D;
3384 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3385 glColor 0, 0, 0, 1; 3553 glColor 0, 0, 0, 1;
3386 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3554 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3387 glDisable GL_TEXTURE_2D; 3555 glDisable GL_TEXTURE_2D;
3388 } else {
3389 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3390 $self->update;
3391 });
3392 } 3556 }
3393} 3557}
3394 3558
3395sub destroy { 3559sub destroy {
3396 my ($self) = @_; 3560 my ($self) = @_;
3397 3561
3398 $self->{timer}->cancel 3562 (delete $self->{timer})->cancel
3399 if $self->{timer}; 3563 if $self->{timer};
3400 3564
3401 $self->SUPER::destroy; 3565 $self->SUPER::destroy;
3402} 3566}
3403 3567
3947 while ($self->{refresh_hook}) { 4111 while ($self->{refresh_hook}) {
3948 $_->() 4112 $_->()
3949 for values %{delete $self->{refresh_hook}}; 4113 for values %{delete $self->{refresh_hook}};
3950 } 4114 }
3951 4115
3952 if ($self->{realloc}) { 4116 while ($self->{realloc}) {
3953 my %queue; 4117 my %queue;
3954 my @queue; 4118 my @queue;
3955 my $widget; 4119 my $widget;
3956 4120
3957 outer: 4121 outer:
4004 } 4168 }
4005 } 4169 }
4006 4170
4007 delete $self->{realloc}{$widget+0}; 4171 delete $self->{realloc}{$widget+0};
4008 } 4172 }
4009 }
4010 4173
4011 while (my $size_alloc = delete $self->{size_alloc}) { 4174 while (my $size_alloc = delete $self->{size_alloc}) {
4012 my @queue = sort { $b->{visible} <=> $a->{visible} } 4175 my @queue = sort { $a->{visible} <=> $b->{visible} }
4013 values %$size_alloc; 4176 values %$size_alloc;
4014 4177
4015 while () { 4178 while () {
4016 my $widget = pop @queue || last; 4179 my $widget = pop @queue || last;
4017 4180
4018 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4181 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4019 4182
4020 $w = 0 if $w < 0;
4021 $h = 0 if $h < 0;
4022
4023 $w = max $widget->{min_w}, $w; 4183 $w = max $widget->{min_w}, $w;
4024 $h = max $widget->{min_h}, $h; 4184 $h = max $widget->{min_h}, $h;
4025 4185
4026# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4186# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4027# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4187# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4028 4188
4029 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4189 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4030 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4190 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4031 4191
4032 $w = int $w + 0.5; 4192 $w = int $w + 0.5;
4033 $h = int $h + 0.5; 4193 $h = int $h + 0.5;
4034 4194
4035 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4195 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4036 $widget->{old_w} = $widget->{w}; 4196 $widget->{old_w} = $widget->{w};
4037 $widget->{old_h} = $widget->{h}; 4197 $widget->{old_h} = $widget->{h};
4038 4198
4039 $widget->{w} = $w; 4199 $widget->{w} = $w;
4040 $widget->{h} = $h; 4200 $widget->{h} = $h;
4041 4201
4042 $widget->emit (size_allocate => $w, $h); 4202 $widget->emit (size_allocate => $w, $h);
4203 }
4043 } 4204 }
4044 } 4205 }
4045 } 4206 }
4046 4207
4047 while ($self->{post_alloc_hook}) { 4208 while ($self->{post_alloc_hook}) {
4048 $_->() 4209 $_->()
4049 for values %{delete $self->{post_alloc_hook}}; 4210 for values %{delete $self->{post_alloc_hook}};
4050 } 4211 }
4051
4052 4212
4053 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4213 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4054 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4214 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4055 glClear GL_COLOR_BUFFER_BIT; 4215 glClear GL_COLOR_BUFFER_BIT;
4056 4216

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines