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.367 by root, Fri Apr 27 00:59:14 2007 UTC vs.
Revision 1.408 by root, Sun Jul 22 15:26:01 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 }
268 $self->hide; 272 $self->hide;
269 $self->emit ("destroy"); 273 $self->emit ("destroy");
270 %$self = (); 274 %$self = ();
271} 275}
272 276
277sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} }
279}
280
273sub show { 281sub show {
274 my ($self) = @_; 282 my ($self) = @_;
275 283
276 return if $self->{parent}; 284 return if $self->{parent};
277 285
316sub set_visibility { 324sub set_visibility {
317 my ($self, $visible) = @_; 325 my ($self, $visible) = @_;
318 326
319 return if $self->{visible} == $visible; 327 return if $self->{visible} == $visible;
320 328
321 $visible ? $self->hide 329 $visible ? $self->show
322 : $self->show; 330 : $self->hide;
323} 331}
324 332
325sub toggle_visibility { 333sub toggle_visibility {
326 my ($self) = @_; 334 my ($self) = @_;
327 335
543 551
544 # parent 552 # parent
545 $self->{parent} && $self->{parent}->emit ($signal, @args) 553 $self->{parent} && $self->{parent}->emit ($signal, @args)
546} 554}
547 555
548sub find_widget { 556#sub find_widget {
549 my ($self, $x, $y) = @_; 557# in .xs
550
551 return () unless $self->{can_events};
552
553 return $self
554 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
555 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
556
557 ()
558}
559 558
560sub set_parent { 559sub set_parent {
561 my ($self, $parent) = @_; 560 my ($self, $parent) = @_;
562 561
563 CFPlus::weaken ($self->{parent} = $parent); 562 CFPlus::weaken ($self->{parent} = $parent);
594 593
595# 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
596# functions seems pointless. 595# functions seems pointless.
597our ($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
598 597
599sub draw { 598#sub draw {
600 my ($self) = @_; 599#CFPlus.xs
601
602 return unless $self->{h} && $self->{w};
603
604 # update screen rectangle
605 local $draw_x = $draw_x + $self->{x};
606 local $draw_y = $draw_y + $self->{y};
607
608 # skip widgets that are entirely outside the drawing area
609 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
610 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
611
612 glPushMatrix;
613 glTranslate $self->{x}, $self->{y}, 0;
614
615 if ($self == $HOVER && $self->{can_hover}) {
616 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
617 glEnable GL_BLEND;
618 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
619 glBegin GL_QUADS;
620 glVertex 0 , 0;
621 glVertex $self->{w}, 0;
622 glVertex $self->{w}, $self->{h};
623 glVertex 0 , $self->{h};
624 glEnd;
625 glDisable GL_BLEND;
626 }
627
628 if ($ENV{CFPLUS_DEBUG} & 1) {
629 glPushMatrix;
630 glColor 1, 1, 0, 1;
631 glTranslate 0.375, 0.375;
632 glBegin GL_LINE_LOOP;
633 glVertex 0 , 0;
634 glVertex $self->{w} - 1, 0;
635 glVertex $self->{w} - 1, $self->{h} - 1;
636 glVertex 0 , $self->{h} - 1;
637 glEnd;
638 glPopMatrix;
639 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
640 }
641
642 $self->_draw;
643 glPopMatrix;
644}
645 600
646sub _draw { 601sub _draw {
647 my ($self) = @_; 602 my ($self) = @_;
648 603
649 warn "no draw defined for $self\n"; 604 warn "no draw defined for $self\n";
650} 605}
651 606
652my $cntx;#d#
653sub DESTROY { 607sub DESTROY {
654 my ($self) = @_; 608 my ($self) = @_;
655 609
656 return if CFPlus::in_destruct; 610 return if CFPlus::in_destruct;
657 611
742 can_events => 0, 696 can_events => 0,
743 %arg, 697 %arg,
744 ); 698 );
745 699
746 $self->add (@$children) 700 $self->add (@$children)
747 if $children; 701 if $children && @$children;
748 702
749 $self 703 $self
750} 704}
751 705
752sub realloc { 706sub realloc {
761 my ($self, @widgets) = @_; 715 my ($self, @widgets) = @_;
762 716
763 $_->set_parent ($self) 717 $_->set_parent ($self)
764 for @widgets; 718 for @widgets;
765 719
720 # TODO: only do this in widgets that need it, e.g. root, fixed
766 use sort 'stable'; 721 use sort 'stable';
767 722
768 $self->{children} = [ 723 $self->{children} = [
769 sort { $a->{z} <=> $b->{z} } 724 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 725 @{$self->{children}}, @widgets
771 ]; 726 ];
772 727
773 $self->realloc; 728 $self->realloc;
729
730 map $_+0, @widgets
774} 731}
775 732
776sub children { 733sub children {
777 @{ $_[0]{children} } 734 @{ $_[0]{children} }
778} 735}
789} 746}
790 747
791sub clear { 748sub clear {
792 my ($self) = @_; 749 my ($self) = @_;
793 750
794 my $children = delete $self->{children}; 751 my $children = $self->{children};
795 $self->{children} = []; 752 $self->{children} = [];
796 753
797 for (@$children) { 754 for (@$children) {
798 delete $_->{parent}; 755 delete $_->{parent};
799 $_->hide; 756 $_->hide;
939} 896}
940 897
941############################################################################# 898#############################################################################
942 899
943package CFPlus::UI::ViewPort; 900package CFPlus::UI::ViewPort;
901
902use List::Util qw(min max);
944 903
945our @ISA = CFPlus::UI::Window::; 904our @ISA = CFPlus::UI::Window::;
946 905
947sub new { 906sub new {
948 my $class = shift; 907 my $class = shift;
957sub size_request { 916sub size_request {
958 my ($self) = @_; 917 my ($self) = @_;
959 918
960 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 919 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
961 920
962 $w = 10 if $self->{scroll_x}; 921 $w = 1 if $self->{scroll_x};
963 $h = 10 if $self->{scroll_y}; 922 $h = 1 if $self->{scroll_y};
964 923
965 ($w, $h) 924 ($w, $h)
966} 925}
967 926
968sub invoke_size_allocate { 927sub invoke_size_allocate {
980} 939}
981 940
982sub set_offset { 941sub set_offset {
983 my ($self, $x, $y) = @_; 942 my ($self, $x, $y) = @_;
984 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}) {
985 $self->{view_x} = int $x; 948 $self->{view_x} = $x;
986 $self->{view_y} = int $y; 949 $self->{view_y} = $y;
987 950
951 $self->emit (changed => $x, $y);
988 $self->update; 952 $self->update;
953 }
989} 954}
990 955
991# 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
992sub coord2local { 957sub coord2local {
993 my ($self, $x, $y) = @_; 958 my ($self, $x, $y) = @_;
1008 my ($self, $x, $y) = @_; 973 my ($self, $x, $y) = @_;
1009 974
1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 975 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 976 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1012 ) { 977 ) {
1013 $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})
1014 } else { 979 } else {
1015 $self->CFPlus::UI::Base::find_widget ($x, $y) 980 $self->CFPlus::UI::Base::find_widget ($x, $y)
1016 } 981 }
1017} 982}
1018 983
1029 994
1030############################################################################# 995#############################################################################
1031 996
1032package CFPlus::UI::ScrolledWindow; 997package CFPlus::UI::ScrolledWindow;
1033 998
1034our @ISA = CFPlus::UI::HBox::; 999our @ISA = CFPlus::UI::Table::;
1035 1000
1036sub new { 1001sub new {
1037 my ($class, %arg) = @_; 1002 my ($class, %arg) = @_;
1038 1003
1039 my $child = delete $arg{child}; 1004 my $child = delete $arg{child};
1040 1005
1041 my $self; 1006 my $self;
1042 1007
1043 my $slider = new CFPlus::UI::Slider 1008 my $hslider = new CFPlus::UI::Slider
1009 col => 0,
1010 row => 1,
1011 vertical => 0,
1012 range => [0, 0, 1, 0.01], # HACK fix
1013 on_changed => sub {
1014 $self->{hpos} = $_[1];
1015 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1016 },
1017 ;
1018
1019 my $vslider = new CFPlus::UI::Slider
1020 col => 1,
1021 row => 0,
1044 vertical => 1, 1022 vertical => 1,
1045 range => [0, 0, 1, 0.01], # HACK fix 1023 range => [0, 0, 1, 0.01], # HACK fix
1046 on_changed => sub { 1024 on_changed => sub {
1047 $self->{vp}->set_offset (0, $_[1]); 1025 $self->{vpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1048 }, 1027 },
1049 ; 1028 ;
1050 1029
1051 $self = $class->SUPER::new ( 1030 $self = $class->SUPER::new (
1052 vp => (new CFPlus::UI::ViewPort expand => 1), 1031 scroll_x => 0,
1032 scroll_y => 1,
1053 can_events => 1, 1033 can_events => 1,
1054 slider => $slider, 1034 hslider => $hslider,
1035 vslider => $vslider,
1036 col_expand => [1, 0],
1037 row_expand => [1, 0],
1055 %arg, 1038 %arg,
1056 ); 1039 );
1057 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
1058 $self->SUPER::add ($self->{vp}, $self->{slider}); 1062 $self->SUPER::add ($self->{vp});
1063
1059 $self->add ($child) if $child; 1064 $self->add ($child) if $child;
1060 1065
1061 $self 1066 $self
1062} 1067}
1063
1064#TODO# update range on size_allocate depending on child
1065 1068
1066sub add { 1069sub add {
1067 my ($self, $widget) = @_; 1070 my ($self, $widget) = @_;
1068 1071
1069 $self->{vp}->add ($self->{child} = $widget); 1072 $self->{vp}->add ($self->{child} = $widget);
1070} 1073}
1071 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
1072sub invoke_mouse_wheel { 1123sub invoke_mouse_wheel {
1073 my ($self, $ev) = @_; 1124 my ($self, $ev) = @_;
1074 1125
1075 return 0 unless $ev->{dy}; # only vertical movements
1076
1077 $self->{slider}->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};
1078 1128
1079 1 1129 1
1080} 1130}
1081 1131
1082sub update_slider { 1132sub invoke_button_down {
1083 my ($self) = @_; 1133 my ($self, $ev, $x, $y) = @_;
1084 1134
1085 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1135 if ($ev->{button} == 2) {
1086} 1136 $self->start_dragging ($ev);
1137 return 1;
1138 }
1087 1139
1088sub update { 1140 0
1089 my ($self) = @_; 1141}
1090 1142
1091 $self->SUPER::update; 1143sub invoke_button_up {
1144 my ($self, $ev, $x, $y) = @_;
1092 1145
1093 $self->update_slider; 1146 if (delete $self->{motion}) {
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
1094} 1162}
1095 1163
1096sub invoke_size_allocate { 1164sub invoke_size_allocate {
1097 my ($self, $w, $h) = @_; 1165 my ($self, $w, $h) = @_;
1098 1166
1099 $self->update_slider; 1167 $self->update_slider;
1100
1101 $self->SUPER::invoke_size_allocate ($w, $h) 1168 $self->SUPER::invoke_size_allocate ($w, $h)
1102} 1169}
1103 1170
1104############################################################################# 1171#############################################################################
1105 1172
1255 1322
1256my @border = 1323my @border =
1257 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1324 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1258 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);
1259 1326
1327my @icon =
1328 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1329 qw(x1_move.png x1_resize.png);
1330
1260sub new { 1331sub new {
1261 my ($class, %arg) = @_; 1332 my ($class, %arg) = @_;
1262 1333
1263 my $self = $class->SUPER::new ( 1334 my $self = $class->SUPER::new (
1264 bg => [1, 1, 1, 1], 1335 bg => [1, 1, 1, 1],
1432 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1503 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1433 1504
1434 my $border = $self->border; 1505 my $border = $self->border;
1435 1506
1436 glColor @{ $self->{border_bg} }; 1507 glColor @{ $self->{border_bg} };
1437 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1508 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1438 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1509 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1439 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1510 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1440 $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);
1441 1526
1442 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1527 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1443 glColor @{ $self->{bg} }; 1528 glColor @{ $self->{bg} };
1444 1529
1445 # TODO: repeat texture not scale 1530 # TODO: repeat texture not scale
1466 1551
1467############################################################################# 1552#############################################################################
1468 1553
1469package CFPlus::UI::Table; 1554package CFPlus::UI::Table;
1470 1555
1471our @ISA = CFPlus::UI::Base::; 1556our @ISA = CFPlus::UI::Container::;
1472 1557
1473use List::Util qw(max sum); 1558use List::Util qw(max sum);
1474 1559
1475use CFPlus::OpenGL; 1560use CFPlus::OpenGL;
1476 1561
1477sub new { 1562sub new {
1478 my $class = shift; 1563 my $class = shift;
1479 1564
1480 $class->SUPER::new ( 1565 $class->SUPER::new (
1481 col_expand => [], 1566 col_expand => [],
1567 row_expand => [],
1482 @_, 1568 @_,
1483 ) 1569 )
1484} 1570}
1485 1571
1486sub children {
1487 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1488}
1489
1490sub 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 {
1491 my ($self) = shift; 1584 my $self = shift;
1585
1586 my @widgets;
1492 1587
1493 while (@_) { 1588 while (@_) {
1494 my ($x, $y, $child) = splice @_, 0, 3, (); 1589 my ($col, $row, $child) = splice @_, 0, 3, ();
1495 $child->set_parent ($self);
1496 $self->{children}[$y][$x] = $child;
1497 }
1498 1590
1499 $self->{force_realloc} = 1; 1591 $child->{row} = $row;
1500 $self->{force_size_alloc} = 1; 1592 $child->{col} = $col;
1501 $self->realloc;
1502}
1503 1593
1504sub remove { 1594 push @widgets, $child;
1595 }
1596
1597 $self->add (@widgets);
1598}
1599
1600sub get_wh {
1505 my ($self, $child) = @_; 1601 my ($self) = @_;
1506 1602
1507 # TODO: not yet implemented 1603 my (@w, @h);
1508}
1509
1510# TODO: move to container class maybe? send children a signal on removal?
1511sub clear {
1512 my ($self) = @_;
1513 1604
1514 my @children = $self->children; 1605 my @children = $self->children;
1515 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 }
1516 1616 }
1517 for (@children) {
1518 delete $_->{parent};
1519 $_->hide;
1520 }
1521 1617
1522 $self->realloc; 1618 # second pass, rows
1523} 1619 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) {
1524
1525sub get_wh {
1526 my ($self) = @_;
1527
1528 my (@w, @h);
1529
1530 for my $y (0 .. $#{$self->{children}}) {
1531 my $row = $self->{children}[$y]
1532 or next;
1533
1534 for my $x (0 .. $#$row) {
1535 my $widget = $row->[$x]
1536 or next;
1537 my ($w, $h) = @$widget{qw(req_w req_h)}; 1620 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)};
1538 1621
1539 $w[$x] = max $w[$x], $w; 1622 my $sh = sum @h[$r .. $r + $rs - 1];
1540 $h[$y] = max $h[$y], $h; 1623
1624 if ($h > $sh) {
1625 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1541 } 1626 }
1542 } 1627 }
1543 1628
1544 (\@w, \@h) 1629 (\@w, \@h)
1545} 1630}
1561 my ($ws, $hs) = $self->get_wh; 1646 my ($ws, $hs) = $self->get_wh;
1562 1647
1563 my $req_w = (sum @$ws) || 1; 1648 my $req_w = (sum @$ws) || 1;
1564 my $req_h = (sum @$hs) || 1; 1649 my $req_h = (sum @$hs) || 1;
1565 1650
1566 # TODO: nicer code && do row_expand 1651 # now linearly scale the rows/columns to the allocated size
1567 my @col_expand = @{$self->{col_expand}}; 1652 my @col_expand = @{$self->{col_expand}};
1568 @col_expand = (1) x @$ws unless @col_expand; 1653 @col_expand = (1) x @$ws unless @col_expand;
1569 my $col_expand = (sum @col_expand) || 1; 1654 my $col_expand = (sum @col_expand) || 1;
1570 1655
1571 # linearly scale sizes
1572 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1656 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1573 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1574 1657
1575 CFPlus::UI::harmonize $ws; 1658 CFPlus::UI::harmonize $ws;
1659
1660 my @row_expand = @{$self->{row_expand}};
1661 @row_expand = (1) x @$ws unless @row_expand;
1662 my $row_expand = (sum @row_expand) || 1;
1663
1664 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1665
1576 CFPlus::UI::harmonize $hs; 1666 CFPlus::UI::harmonize $hs;
1577 1667
1578 my $y; 1668 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1669 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1579 1670
1580 for my $r (0 .. $#{$self->{children}}) { 1671 for my $widget ($self->children) {
1581 my $row = $self->{children}[$r] 1672 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)};
1582 or next;
1583 1673
1584 my $x = 0; 1674 $widget->configure (
1585 my $row_h = $hs->[$r]; 1675 $x[$c], $y[$r],
1676 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1586 1677 );
1587 for my $c (0 .. $#$row) { 1678 }
1588 my $col_w = $ws->[$c];
1589 1679
1590 if (my $widget = $row->[$c]) { 1680 1
1591 $widget->configure ($x, $y, $col_w, $row_h); 1681}
1592 }
1593 1682
1594 $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;
1595 } 1721 }
1722 }
1596 1723
1597 $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);
1598 } 1760 }
1599 1761
1600 1 1762 1
1601}
1602
1603sub find_widget {
1604 my ($self, $x, $y) = @_;
1605
1606 $x -= $self->{x};
1607 $y -= $self->{y};
1608
1609 my $res;
1610
1611 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1612 $res = $_->find_widget ($x, $y)
1613 and return $res;
1614 }
1615
1616 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1617}
1618
1619sub _draw {
1620 my ($self) = @_;
1621
1622 for (grep $_, @{$self->{children}}) {
1623 $_->draw for grep $_, @$_;
1624 }
1625} 1763}
1626 1764
1627############################################################################# 1765#############################################################################
1628 1766
1629package CFPlus::UI::Box; 1767package CFPlus::UI::Box;
1889 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1890 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1891 : ($self->{h} - $size->[1]) * 0.5); 2029 : ($self->{h} - $size->[1]) * 0.5);
1892 }; 2030 };
1893 2031
1894 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2032# unless ($self->{list}) {
1895 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};
1896 2040
1897 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2041 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1898} 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#}
1899 2051
1900############################################################################# 2052#############################################################################
1901 2053
1902package CFPlus::UI::EntryBase; 2054package CFPlus::UI::EntryBase;
1903 2055
2187 2339
2188use CFPlus::OpenGL; 2340use CFPlus::OpenGL;
2189 2341
2190my @tex = 2342my @tex =
2191 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2343 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2192 qw(b1_button_active.png); 2344 qw(b1_button_inactive.png b1_button_active.png);
2193 2345
2194sub new { 2346sub new {
2195 my $class = shift; 2347 my $class = shift;
2196 2348
2197 $class->SUPER::new ( 2349 $class->SUPER::new (
2198 padding_x => 4, 2350 padding_x => 4,
2199 padding_y => 4, 2351 padding_y => 4,
2200 fg => [1, 1, 1], 2352 fg => [1.0, 1.0, 1.0],
2201 active_fg => [0, 0, 1], 2353 active_fg => [0.8, 0.8, 0.8],
2202 can_hover => 1, 2354 can_hover => 1,
2203 align => 0, 2355 align => 0,
2204 valign => 0, 2356 valign => 0,
2205 can_events => 1, 2357 can_events => 1,
2206 @_ 2358 @_
2224 2376
2225 glEnable GL_TEXTURE_2D; 2377 glEnable GL_TEXTURE_2D;
2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2378 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2227 glColor 0, 0, 0, 1; 2379 glColor 0, 0, 0, 1;
2228 2380
2381 my $tex = $tex[$GRAB == $self];
2229 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2382 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2230 2383
2231 glDisable GL_TEXTURE_2D; 2384 glDisable GL_TEXTURE_2D;
2232 2385
2233 $self->SUPER::_draw; 2386 $self->SUPER::_draw;
2234} 2387}
3289 3442
3290sub new { 3443sub new {
3291 my $class = shift; 3444 my $class = shift;
3292 3445
3293 my $self = $class->SUPER::new ( 3446 my $self = $class->SUPER::new (
3447 size_w => 32,
3448 size_h => 8,
3294 aspect => 1, 3449 aspect => 1,
3295 can_events => 0, 3450 can_events => 0,
3296 @_, 3451 @_,
3297 ); 3452 );
3298 3453
3299 if ($self->{anim} && $self->{animspeed}) { 3454 if ($self->{anim} && $self->{animspeed}) {
3300 CFPlus::weaken (my $widget = $self); 3455 CFPlus::weaken (my $widget = $self);
3301 3456
3457 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3458 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3302 $self->{timer} = Event->timer ( 3459 $self->{timer} = Event->timer (
3303 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3304 hard => 1, 3460 parked => 1,
3305 interval => $self->{animspeed},
3306 cb => sub { 3461 cb => sub {
3462 return unless $::CONN && $widget;
3463
3307 ++$widget->{frame}; 3464 ++$widget->{frame};
3465 $widget->update_face;
3308 $widget->update; 3466 $widget->update;
3467
3468 $widget->update_timer;
3309 }, 3469 },
3310 ); 3470 );
3471
3472 $self->update_face;
3473 $self->update_timer;
3311 } 3474 }
3312 3475
3313 $self 3476 $self
3314} 3477}
3315 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
3316sub size_request { 3510sub size_request {
3317 (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)
3318} 3527}
3319 3528
3320sub update { 3529sub update {
3321 my ($self) = @_; 3530 my ($self) = @_;
3322 3531
3323 return unless $self->{visible}; 3532 return unless $self->{visible};
3324 3533
3325 $self->SUPER::update; 3534 $self->SUPER::update;
3326} 3535}
3327 3536
3537sub invoke_visibility_change {
3538 my ($self) = @_;
3539
3540 $self->update_timer;
3541
3542 0
3543}
3544
3328sub _draw { 3545sub _draw {
3329 my ($self) = @_; 3546 my ($self) = @_;
3330 3547
3331 return unless $::CONN;
3332
3333 $self->SUPER::_draw; 3548 $self->SUPER::_draw;
3334 3549
3335 my $face; 3550 if (my $tex = $self->{tex}) {
3336
3337 if ($self->{frame}) {
3338 my $anim = $::CONN->{anim}[$self->{anim}];
3339
3340 $face = $anim->[ $self->{frame} % @$anim ]
3341 if $anim && @$anim;
3342 }
3343
3344 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3345
3346 if ($tex) {
3347 glEnable GL_TEXTURE_2D; 3551 glEnable GL_TEXTURE_2D;
3348 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3349 glColor 0, 0, 0, 1; 3553 glColor 0, 0, 0, 1;
3350 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3554 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3351 glDisable GL_TEXTURE_2D; 3555 glDisable GL_TEXTURE_2D;
3353} 3557}
3354 3558
3355sub destroy { 3559sub destroy {
3356 my ($self) = @_; 3560 my ($self) = @_;
3357 3561
3358 $self->{timer}->cancel 3562 (delete $self->{timer})->cancel
3359 if $self->{timer}; 3563 if $self->{timer};
3360 3564
3361 $self->SUPER::destroy; 3565 $self->SUPER::destroy;
3362} 3566}
3363 3567
3561sub add { 3765sub add {
3562 my ($self, $title, $widget, $tooltip) = @_; 3766 my ($self, $title, $widget, $tooltip) = @_;
3563 3767
3564 CFPlus::weaken $self; 3768 CFPlus::weaken $self;
3565 3769
3566 $self->{buttonbar}->add (new CFPlus::UI::Button 3770 unless (ref $title) {
3771 $title = new CFPlus::UI::Button
3567 markup => $title, 3772 markup => $title,
3568 tooltip => $tooltip, 3773 tooltip => $tooltip,
3774 );
3775 }
3776
3569 on_activate => sub { $self->set_current_page ($widget) }, 3777 $title->connect (activate => sub { $self->set_current_page ($widget) });
3570 ); 3778 $self->{buttonbar}->add ($title);
3571 3779
3572 $self->{multiplexer}->add ($widget); 3780 $self->{multiplexer}->add ($widget);
3573} 3781}
3574 3782
3575sub get_current_page { 3783sub get_current_page {
3907 while ($self->{refresh_hook}) { 4115 while ($self->{refresh_hook}) {
3908 $_->() 4116 $_->()
3909 for values %{delete $self->{refresh_hook}}; 4117 for values %{delete $self->{refresh_hook}};
3910 } 4118 }
3911 4119
3912 if ($self->{realloc}) { 4120 while ($self->{realloc}) {
3913 my %queue; 4121 my %queue;
3914 my @queue; 4122 my @queue;
3915 my $widget; 4123 my $widget;
3916 4124
3917 outer: 4125 outer:
3964 } 4172 }
3965 } 4173 }
3966 4174
3967 delete $self->{realloc}{$widget+0}; 4175 delete $self->{realloc}{$widget+0};
3968 } 4176 }
3969 }
3970 4177
3971 while (my $size_alloc = delete $self->{size_alloc}) { 4178 while (my $size_alloc = delete $self->{size_alloc}) {
3972 my @queue = sort { $b->{visible} <=> $a->{visible} } 4179 my @queue = sort { $a->{visible} <=> $b->{visible} }
3973 values %$size_alloc; 4180 values %$size_alloc;
3974 4181
3975 while () { 4182 while () {
3976 my $widget = pop @queue || last; 4183 my $widget = pop @queue || last;
3977 4184
3978 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4185 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3979 4186
3980 $w = 0 if $w < 0;
3981 $h = 0 if $h < 0;
3982
3983 $w = max $widget->{min_w}, $w; 4187 $w = max $widget->{min_w}, $w;
3984 $h = max $widget->{min_h}, $h; 4188 $h = max $widget->{min_h}, $h;
3985 4189
3986# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4190# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
3987# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4191# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
3988 4192
3989 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4193 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3990 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4194 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3991 4195
3992 $w = int $w + 0.5; 4196 $w = int $w + 0.5;
3993 $h = int $h + 0.5; 4197 $h = int $h + 0.5;
3994 4198
3995 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4199 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3996 $widget->{old_w} = $widget->{w}; 4200 $widget->{old_w} = $widget->{w};
3997 $widget->{old_h} = $widget->{h}; 4201 $widget->{old_h} = $widget->{h};
3998 4202
3999 $widget->{w} = $w; 4203 $widget->{w} = $w;
4000 $widget->{h} = $h; 4204 $widget->{h} = $h;
4001 4205
4002 $widget->emit (size_allocate => $w, $h); 4206 $widget->emit (size_allocate => $w, $h);
4207 }
4003 } 4208 }
4004 } 4209 }
4005 } 4210 }
4006 4211
4007 while ($self->{post_alloc_hook}) { 4212 while ($self->{post_alloc_hook}) {
4008 $_->() 4213 $_->()
4009 for values %{delete $self->{post_alloc_hook}}; 4214 for values %{delete $self->{post_alloc_hook}};
4010 } 4215 }
4011
4012 4216
4013 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4217 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4014 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4218 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4015 glClear GL_COLOR_BUFFER_BIT; 4219 glClear GL_COLOR_BUFFER_BIT;
4016 4220

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines