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.361 by root, Sat Dec 9 02:21:25 2006 UTC vs.
Revision 1.387 by root, Fri Jul 20 18:19:45 2007 UTC

1package CFPlus::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util ();
7use List::Util (); 6use List::Util ();
8use Event; 7use Event;
9 8
10use CFPlus; 9use CFPlus;
11use CFPlus::Pod; 10use CFPlus::Pod;
23our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
24 if (!$GRAB) { 23 if (!$GRAB) {
25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
26 if (length $widget->{tooltip}) { 25 if (length $widget->{tooltip}) {
27 if ($TOOLTIP->{owner} != $widget) { 26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
28 $TOOLTIP->hide; 28 $TOOLTIP->hide;
29 29
30 $TOOLTIP->{owner} = $widget; 30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
31 32
32 return if $ENV{CFPLUS_DEBUG} & 8; 33 return if $ENV{CFPLUS_DEBUG} & 8;
33 34
34 my $tip = $widget->{tooltip}; 35 my $tip = $widget->{tooltip};
35 36
43 } 44 }
44 } 45 }
45 } 46 }
46 47
47 $TOOLTIP->hide; 48 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
48 delete $TOOLTIP->{owner}; 50 delete $TOOLTIP->{owner};
49}); 51});
50 52
51sub get_layout { 53sub get_layout {
52 my $layout; 54 my $layout;
235 h => undef, 237 h => undef,
236 can_events => 1, 238 can_events => 1,
237 @_ 239 @_
238 }, $class; 240 }, $class;
239 241
240 Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
241 243
242 for (keys %$self) { 244 for (keys %$self) {
243 if (/^on_(.*)$/) { 245 if (/^on_(.*)$/) {
244 $self->connect ($1 => delete $self->{$_}); 246 $self->connect ($1 => delete $self->{$_});
245 } 247 }
266 $self->hide; 268 $self->hide;
267 $self->emit ("destroy"); 269 $self->emit ("destroy");
268 %$self = (); 270 %$self = ();
269} 271}
270 272
273sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} }
275}
276
271sub show { 277sub show {
272 my ($self) = @_; 278 my ($self) = @_;
273 279
274 return if $self->{parent}; 280 return if $self->{parent};
275 281
314sub set_visibility { 320sub set_visibility {
315 my ($self, $visible) = @_; 321 my ($self, $visible) = @_;
316 322
317 return if $self->{visible} == $visible; 323 return if $self->{visible} == $visible;
318 324
319 $visible ? $self->hide 325 $visible ? $self->show
320 : $self->show; 326 : $self->hide;
321} 327}
322 328
323sub toggle_visibility { 329sub toggle_visibility {
324 my ($self) = @_; 330 my ($self) = @_;
325 331
530 ? $args[0]->xy ($self) 536 ? $args[0]->xy ($self)
531 : (); 537 : ();
532 538
533 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 539 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
534 540
535 #d##TODO# stop propagating at first true, do not use sum 541 for my $cb (
536 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 542 @{$self->{signal_cb}{$signal} || []}, # before
537 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 543 ($self->can ("invoke_$signal") || sub { 1 }), # closure
544 ) {
545 return $cb->($self, @args, @append) || next;
546 }
547
548 # parent
538 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 549 $self->{parent} && $self->{parent}->emit ($signal, @args)
539} 550}
540 551
541sub find_widget { 552#sub find_widget {
542 my ($self, $x, $y) = @_; 553# in .xs
543
544 return () unless $self->{can_events};
545
546 return $self
547 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
548 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
549
550 ()
551}
552 554
553sub set_parent { 555sub set_parent {
554 my ($self, $parent) = @_; 556 my ($self, $parent) = @_;
555 557
556 Scalar::Util::weaken ($self->{parent} = $parent); 558 CFPlus::weaken ($self->{parent} = $parent);
557 $self->set_visible if $parent->{visible}; 559 $self->set_visible if $parent->{visible};
558} 560}
559 561
560sub realloc { 562sub realloc {
561 my ($self) = @_; 563 my ($self) = @_;
640 my ($self) = @_; 642 my ($self) = @_;
641 643
642 warn "no draw defined for $self\n"; 644 warn "no draw defined for $self\n";
643} 645}
644 646
647my $cntx;#d#
645sub DESTROY { 648sub DESTROY {
646 my ($self) = @_; 649 my ($self) = @_;
647 650
648 return if CFPlus::in_destruct; 651 return if CFPlus::in_destruct;
649 652
932 935
933############################################################################# 936#############################################################################
934 937
935package CFPlus::UI::ViewPort; 938package CFPlus::UI::ViewPort;
936 939
940use List::Util qw(min max);
941
937our @ISA = CFPlus::UI::Window::; 942our @ISA = CFPlus::UI::Window::;
938 943
939sub new { 944sub new {
940 my $class = shift; 945 my $class = shift;
941 946
972} 977}
973 978
974sub set_offset { 979sub set_offset {
975 my ($self, $x, $y) = @_; 980 my ($self, $x, $y) = @_;
976 981
982 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
983 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
984
985 if ($x != $self->{view_x} or $y != $self->{view_y}) {
977 $self->{view_x} = int $x; 986 $self->{view_x} = $x;
978 $self->{view_y} = int $y; 987 $self->{view_y} = $y;
979 988
989 $self->emit (changed => $x, $y);
980 $self->update; 990 $self->update;
991 }
981} 992}
982 993
983# hmm, this does not work for topleft of $self... but we should not ask for that 994# hmm, this does not work for topleft of $self... but we should not ask for that
984sub coord2local { 995sub coord2local {
985 my ($self, $x, $y) = @_; 996 my ($self, $x, $y) = @_;
1000 my ($self, $x, $y) = @_; 1011 my ($self, $x, $y) = @_;
1001 1012
1002 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1013 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1003 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1014 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1004 ) { 1015 ) {
1005 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1016 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1006 } else { 1017 } else {
1007 $self->CFPlus::UI::Base::find_widget ($x, $y) 1018 $self->CFPlus::UI::Base::find_widget ($x, $y)
1008 } 1019 }
1009} 1020}
1010 1021
1021 1032
1022############################################################################# 1033#############################################################################
1023 1034
1024package CFPlus::UI::ScrolledWindow; 1035package CFPlus::UI::ScrolledWindow;
1025 1036
1026our @ISA = CFPlus::UI::HBox::; 1037our @ISA = CFPlus::UI::Table::;
1027 1038
1028sub new { 1039sub new {
1029 my ($class, %arg) = @_; 1040 my ($class, %arg) = @_;
1030 1041
1031 my $child = delete $arg{child}; 1042 my $child = delete $arg{child};
1032 1043
1033 my $self; 1044 my $self;
1034 1045
1035 my $slider = new CFPlus::UI::Slider 1046 my $hslider = new CFPlus::UI::Slider
1047 vertical => 0,
1048 range => [0, 0, 1, 0.01], # HACK fix
1049 on_changed => sub {
1050 $self->{hpos} = $_[1];
1051 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1052 },
1053 ;
1054
1055 my $vslider = new CFPlus::UI::Slider
1036 vertical => 1, 1056 vertical => 1,
1037 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1038 on_changed => sub { 1058 on_changed => sub {
1039 $self->{vp}->set_offset (0, $_[1]); 1059 $self->{vpos} = $_[1];
1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1061 },
1041 ; 1062 ;
1042 1063
1043 $self = $class->SUPER::new ( 1064 $self = $class->SUPER::new (
1044 vp => (new CFPlus::UI::ViewPort expand => 1), 1065 scroll_x => 0,
1066 scroll_y => 1,
1045 can_events => 1, 1067 can_events => 1,
1046 slider => $slider, 1068 hslider => $hslider,
1069 vslider => $vslider,
1070 col_expand => [1, 0],
1071 row_expand => [1, 0],
1047 %arg, 1072 %arg,
1048 ); 1073 );
1049 1074
1050 $self->SUPER::add ($self->{vp}, $self->{slider}); 1075 $self->{vp} = new CFPlus::UI::ViewPort
1076 expand => 1,
1077 scroll_x => $self->{scroll_x},
1078 scroll_y => $self->{scroll_y},
1079 on_changed => sub {
1080 my ($vp, $x, $y) = @_;
1081
1082 $vp->{parent}{hslider}->set_value ($x);
1083 $vp->{parent}{vslider}->set_value ($y);
1084
1085 0
1086 },
1087 ;
1088
1089 $self->SUPER::add_at (0, 0, $self->{vp});
1090
1051 $self->add ($child) if $child; 1091 $self->add ($child) if $child;
1052 1092
1053 $self 1093 $self
1054} 1094}
1055 1095
1059 my ($self, $widget) = @_; 1099 my ($self, $widget) = @_;
1060 1100
1061 $self->{vp}->add ($self->{child} = $widget); 1101 $self->{vp}->add ($self->{child} = $widget);
1062} 1102}
1063 1103
1104sub update_slider {
1105 my ($self) = @_;
1106
1107 my $child = ($self->{vp} or return)->child;
1108
1109 my ($w1, $w2) = ($child->{w}, $self->{vp}{w});
1110 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1111
1112 my $visible = $w1 > $w2;
1113 if ($visible != $self->{hslider}{visible}) {
1114 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider})
1115 : $self->{hslider}->hide;
1116 }
1117
1118 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1119 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1120
1121 my $visible = $h1 > $h2;
1122 if ($visible != $self->{vslider}{visible}) {
1123 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider})
1124 : $self->{vslider}->hide;
1125 }
1126}
1127
1128sub update {
1129 my ($self) = @_;
1130
1131 $self->update_slider;
1132 $self->SUPER::update;
1133}
1134
1064sub invoke_mouse_wheel { 1135sub invoke_mouse_wheel {
1065 my ($self, $ev) = @_; 1136 my ($self, $ev) = @_;
1066 1137
1067 return 0 unless $ev->{dy}; # only vertical movements 1138 return 0 unless $ev->{dy}; # only vertical movements for now
1068 1139
1069 $self->{slider}->emit (mouse_wheel => $ev); 1140 $self->{vslider}->emit (mouse_wheel => $ev);
1070 1141
1071 1 1142 1
1072} 1143}
1073 1144
1074sub update_slider { 1145sub invoke_button_down {
1075 my ($self) = @_; 1146 my ($self, $ev, $x, $y) = @_;
1076 1147
1077 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1148 if ($ev->{button} == 2) {
1078} 1149 $self->grab_focus;
1079 1150
1080sub update { 1151 my $ox = $self->{vp}{view_x} + $ev->{x};
1081 my ($self) = @_; 1152 my $oy = $self->{vp}{view_y} + $ev->{y};
1153
1154 $self->{motion} = sub {
1155 my ($ev, $x, $y) = @_;
1082 1156
1083 $self->SUPER::update; 1157 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1158 $self->update;
1159 };
1084 1160
1085 $self->update_slider; 1161 return 1;
1162 }
1163
1164 0
1165}
1166
1167sub invoke_button_up {
1168 my ($self, $ev, $x, $y) = @_;
1169
1170 if (delete $self->{motion}) {
1171 return 1;
1172 }
1173
1174 0
1175}
1176
1177sub invoke_mouse_motion {
1178 my ($self, $ev, $x, $y) = @_;
1179
1180 if ($self->{motion}) {
1181 $self->{motion}->($ev, $x, $y);
1182 return 1;
1183 }
1184
1185 0
1086} 1186}
1087 1187
1088sub invoke_size_allocate { 1188sub invoke_size_allocate {
1089 my ($self, $w, $h) = @_; 1189 my ($self, $w, $h) = @_;
1090 1190
1091 $self->update_slider; 1191 $self->update_slider;
1092
1093 $self->SUPER::invoke_size_allocate ($w, $h) 1192 $self->SUPER::invoke_size_allocate ($w, $h)
1094} 1193}
1095 1194
1096############################################################################# 1195#############################################################################
1097 1196
1247 1346
1248my @border = 1347my @border =
1249 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1250 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1349 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1251 1350
1351my @icon =
1352 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1353 qw(x1_move.png x1_resize.png);
1354
1252sub new { 1355sub new {
1253 my ($class, %arg) = @_; 1356 my ($class, %arg) = @_;
1254 1357
1255 my $self = $class->SUPER::new ( 1358 my $self = $class->SUPER::new (
1256 bg => [1, 1, 1, 1], 1359 bg => [1, 1, 1, 1],
1424 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1527 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1425 1528
1426 my $border = $self->border; 1529 my $border = $self->border;
1427 1530
1428 glColor @{ $self->{border_bg} }; 1531 glColor @{ $self->{border_bg} };
1429 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1532 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1430 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1533 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1431 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1534 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1432 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1535 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1536
1537 # move
1538 my $w2 = ($w - $border) * .5;
1539 my $h2 = ($h - $border) * .5;
1540 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1541 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1542 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1543
1544 # resize
1545 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1546 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1547 unless $self->{has_close_button};
1548 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1433 1550
1434 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1551 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1435 glColor @{ $self->{bg} }; 1552 glColor @{ $self->{bg} };
1436 1553
1437 # TODO: repeat texture not scale 1554 # TODO: repeat texture not scale
1468 1585
1469sub new { 1586sub new {
1470 my $class = shift; 1587 my $class = shift;
1471 1588
1472 $class->SUPER::new ( 1589 $class->SUPER::new (
1590 children => [],
1473 col_expand => [], 1591 col_expand => [],
1592 row_expand => [],
1474 @_, 1593 @_,
1475 ) 1594 )
1476} 1595}
1477 1596
1478sub children { 1597sub children {
1479 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1598 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1480} 1599}
1481 1600
1601# TODO: store row/col info in child widget and use standard add/del
1482sub add { 1602sub add {
1483 my ($self) = shift; 1603 my $self = shift;
1604
1605 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d#
1606 $self->add_at (@_);
1607}
1608
1609sub add_at {
1610 my $self = shift;
1484 1611
1485 while (@_) { 1612 while (@_) {
1486 my ($x, $y, $child) = splice @_, 0, 3, (); 1613 my ($col, $row, $child) = splice @_, 0, 3, ();
1614
1487 $child->set_parent ($self); 1615 $child->set_parent ($self);
1488 $self->{children}[$y][$x] = $child; 1616 $self->{children}[$row][$col] = $child;
1489 } 1617 }
1490 1618
1491 $self->{force_realloc} = 1; 1619 $self->{force_realloc} = 1;
1492 $self->{force_size_alloc} = 1; 1620 $self->{force_size_alloc} = 1;
1493 $self->realloc; 1621 $self->realloc;
1494} 1622}
1495 1623
1496sub remove { 1624sub remove {
1497 my ($self, $child) = @_; 1625 my ($self, $child) = @_;
1498 1626
1499 # TODO: not yet implemented 1627 for (@{ $self->{children} }) {
1628 for (@{ $_ || [] }) {
1629 $_ = undef if $_ == $child;
1630 }
1631 }
1500} 1632}
1501 1633
1502# TODO: move to container class maybe? send children a signal on removal? 1634# TODO: move to container class maybe? send children a signal on removal?
1503sub clear { 1635sub clear {
1504 my ($self) = @_; 1636 my ($self) = @_;
1553 my ($ws, $hs) = $self->get_wh; 1685 my ($ws, $hs) = $self->get_wh;
1554 1686
1555 my $req_w = (sum @$ws) || 1; 1687 my $req_w = (sum @$ws) || 1;
1556 my $req_h = (sum @$hs) || 1; 1688 my $req_h = (sum @$hs) || 1;
1557 1689
1558 # TODO: nicer code && do row_expand 1690 # TODO: nicer code
1559 my @col_expand = @{$self->{col_expand}}; 1691 my @col_expand = @{$self->{col_expand}};
1560 @col_expand = (1) x @$ws unless @col_expand; 1692 @col_expand = (1) x @$ws unless @col_expand;
1561 my $col_expand = (sum @col_expand) || 1; 1693 my $col_expand = (sum @col_expand) || 1;
1562 1694
1563 # linearly scale sizes
1564 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1695 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1565 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1566 1696
1567 CFPlus::UI::harmonize $ws; 1697 CFPlus::UI::harmonize $ws;
1698
1699 my @row_expand = @{$self->{row_expand}};
1700 @row_expand = (1) x @$ws unless @row_expand;
1701 my $row_expand = (sum @row_expand) || 1;
1702
1703 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1704
1568 CFPlus::UI::harmonize $hs; 1705 CFPlus::UI::harmonize $hs;
1569 1706
1570 my $y; 1707 my $y;
1571 1708
1572 for my $r (0 .. $#{$self->{children}}) { 1709 for my $r (0 .. $#{$self->{children}}) {
1612 my ($self) = @_; 1749 my ($self) = @_;
1613 1750
1614 for (grep $_, @{$self->{children}}) { 1751 for (grep $_, @{$self->{children}}) {
1615 $_->draw for grep $_, @$_; 1752 $_->draw for grep $_, @$_;
1616 } 1753 }
1754}
1755
1756#############################################################################
1757
1758package CFPlus::UI::Fixed;
1759
1760use List::Util qw(min max);
1761
1762our @ISA = CFPlus::UI::Container::;
1763
1764sub add {
1765 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1766
1767 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1768 $self->SUPER::add ($child);
1769}
1770
1771sub _scale($$$) {
1772 my ($mode, $val, $max) = @_;
1773
1774 $mode eq "abs" ? $val
1775 : $mode eq "rel" ? $val * $max
1776 : 0
1777}
1778
1779sub size_request {
1780 my ($self) = @_;
1781
1782 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1783
1784 # determine overall size by querying abs widgets
1785 for my $child ($self->visible_children) {
1786 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1787
1788 if ($pos eq "abs") {
1789 $w = _scale $size, $w, $child->{req_w};
1790 $h = _scale $size, $h, $child->{req_h};
1791
1792 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1793 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1794 }
1795 }
1796
1797 my $W = $x2 - $x1;
1798 my $H = $y2 - $y1;
1799
1800 # now layout remaining widgets
1801 for my $child ($self->visible_children) {
1802 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1803
1804 if ($pos ne "abs") {
1805 $x = _scale $pos, $x, $W;
1806 $y = _scale $pos, $x, $H;
1807 $w = _scale $size, $w, $child->{req_w};
1808 $h = _scale $size, $h, $child->{req_h};
1809
1810 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1811 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1812 }
1813 }
1814
1815 my $W = $x2 - $x1;
1816 my $H = $y2 - $y1;
1817
1818 ($W, $H)
1819}
1820
1821sub invoke_size_allocate {
1822 my ($self, $W, $H) = @_;
1823
1824 for my $child ($self->visible_children) {
1825 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1826
1827 $x = _scale $pos, $x, $W;
1828 $y = _scale $pos, $x, $H;
1829 $w = _scale $size, $w, $child->{req_w};
1830 $h = _scale $size, $h, $child->{req_h};
1831
1832 $child->configure ($x, $y, $w, $h);
1833 }
1834
1835 1
1617} 1836}
1618 1837
1619############################################################################# 1838#############################################################################
1620 1839
1621package CFPlus::UI::Box; 1840package CFPlus::UI::Box;
1962 my $sym = $ev->{sym}; 2181 my $sym = $ev->{sym};
1963 my $uni = $ev->{unicode}; 2182 my $uni = $ev->{unicode};
1964 2183
1965 my $text = $self->get_text; 2184 my $text = $self->get_text;
1966 2185
2186 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2187
1967 if ($uni == 8) { 2188 if ($uni == 8) {
1968 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2189 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1969 } elsif ($uni == 127) { 2190 } elsif ($uni == 127) {
1970 substr $text, $self->{cursor}, 1, ""; 2191 substr $text, $self->{cursor}, 1, "";
1971 } elsif ($sym == CFPlus::SDLK_LEFT) { 2192 } elsif ($sym == CFPlus::SDLK_LEFT) {
1972 --$self->{cursor} if $self->{cursor}; 2193 --$self->{cursor} if $self->{cursor};
1973 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2194 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1974 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2195 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1975 } elsif ($sym == CFPlus::SDLK_HOME) { 2196 } elsif ($sym == CFPlus::SDLK_HOME) {
2197 # what a hack
1976 $self->{cursor} = 0; 2198 $self->{cursor} =
2199 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2200 ? length $1
2201 : 0;
1977 } elsif ($sym == CFPlus::SDLK_END) { 2202 } elsif ($sym == CFPlus::SDLK_END) {
2203 # uh, again
2204 $self->{cursor} =
2205 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
1978 $self->{cursor} = length $text; 2206 ? $self->{cursor} + length $1
2207 : length $self->{text};
1979 } elsif ($uni == 21) { # ctrl-u 2208 } elsif ($uni == 21) { # ctrl-u
1980 $text = ""; 2209 $text = "";
1981 $self->{cursor} = 0; 2210 $self->{cursor} = 0;
1982 } elsif ($uni == 27) { 2211 } elsif ($uni == 27) {
1983 $self->emit ('escape'); 2212 $self->emit ('escape');
1984 } elsif ($uni >= 0x20 || $uni == 0x0d) { 2213 } elsif ($uni == 0x0d) {
2214 substr $text, $self->{cursor}++, 0, "\012";
2215 } elsif ($uni >= 0x20) {
1985 substr $text, $self->{cursor}++, 0, chr $uni; 2216 substr $text, $self->{cursor}++, 0, chr $uni;
1986 } else { 2217 } else {
1987 return 0; 2218 return 0;
1988 } 2219 }
1989 2220
2167 2398
2168use CFPlus::OpenGL; 2399use CFPlus::OpenGL;
2169 2400
2170my @tex = 2401my @tex =
2171 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2402 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2172 qw(b1_button_active.png); 2403 qw(b1_button_inactive.png b1_button_active.png);
2173 2404
2174sub new { 2405sub new {
2175 my $class = shift; 2406 my $class = shift;
2176 2407
2177 $class->SUPER::new ( 2408 $class->SUPER::new (
2178 padding_x => 4, 2409 padding_x => 4,
2179 padding_y => 4, 2410 padding_y => 4,
2180 fg => [1, 1, 1], 2411 fg => [1.0, 1.0, 1.0],
2181 active_fg => [0, 0, 1], 2412 active_fg => [0.8, 0.8, 0.8],
2182 can_hover => 1, 2413 can_hover => 1,
2183 align => 0, 2414 align => 0,
2184 valign => 0, 2415 valign => 0,
2185 can_events => 1, 2416 can_events => 1,
2186 @_ 2417 @_
2204 2435
2205 glEnable GL_TEXTURE_2D; 2436 glEnable GL_TEXTURE_2D;
2206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2437 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2207 glColor 0, 0, 0, 1; 2438 glColor 0, 0, 0, 1;
2208 2439
2440 my $tex = $tex[$GRAB == $self];
2209 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2441 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2210 2442
2211 glDisable GL_TEXTURE_2D; 2443 glDisable GL_TEXTURE_2D;
2212 2444
2213 $self->SUPER::_draw; 2445 $self->SUPER::_draw;
2214} 2446}
2310 or Carp::croak "'path' or 'tex' attributes required"; 2542 or Carp::croak "'path' or 'tex' attributes required";
2311 2543
2312 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2544 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2313 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2545 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2314 2546
2315 Scalar::Util::weaken $texture_cache{$self->{path}}; 2547 CFPlus::weaken $texture_cache{$self->{path}};
2316 2548
2317 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2549 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2318 2550
2319 $self 2551 $self
2320} 2552}
2693sub invoke_mouse_wheel { 2925sub invoke_mouse_wheel {
2694 my ($self, $ev) = @_; 2926 my ($self, $ev) = @_;
2695 2927
2696 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2928 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2697 2929
2930 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2931
2698 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 2932 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2699 2933
2700 ! ! $delta 2934 ! ! $delta
2701} 2935}
2702 2936
2703sub update { 2937sub update {
2813 #font => default_font 3047 #font => default_font
2814 @_, 3048 @_,
2815 3049
2816 layout => (new CFPlus::Layout), 3050 layout => (new CFPlus::Layout),
2817 par => [], 3051 par => [],
3052 max_par => 0,
2818 height => 0, 3053 height => 0,
2819 children => [ 3054 children => [
2820 (new CFPlus::UI::Empty expand => 1), 3055 (new CFPlus::UI::Empty expand => 1),
2821 (new CFPlus::UI::Slider vertical => 1), 3056 (new CFPlus::UI::Slider vertical => 1),
2822 ], 3057 ],
2945 wrapped => 1, 3180 wrapped => 1,
2946 }; 3181 };
2947 3182
2948 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3183 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2949 push @{$self->{par}}, $para; 3184 push @{$self->{par}}, $para;
3185 }
3186
3187 if (my $max = $self->{max_par}) {
3188 shift @{$self->{par}} while @{$self->{par}} > $max;
2950 } 3189 }
2951 3190
2952 $self->{need_reflow}++; 3191 $self->{need_reflow}++;
2953 $self->update; 3192 $self->update;
2954} 3193}
3262 3501
3263sub new { 3502sub new {
3264 my $class = shift; 3503 my $class = shift;
3265 3504
3266 my $self = $class->SUPER::new ( 3505 my $self = $class->SUPER::new (
3506 size_w => 32,
3507 size_h => 8,
3267 aspect => 1, 3508 aspect => 1,
3268 can_events => 0, 3509 can_events => 0,
3269 @_, 3510 @_,
3270 ); 3511 );
3271 3512
3272 if ($self->{anim} && $self->{animspeed}) { 3513 if ($self->{anim} && $self->{animspeed}) {
3273 Scalar::Util::weaken (my $widget = $self); 3514 CFPlus::weaken (my $widget = $self);
3274 3515
3516 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3517 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3275 $self->{timer} = Event->timer ( 3518 $self->{timer} = Event->timer (
3276 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3277 hard => 1, 3519 parked => 1,
3278 interval => $self->{animspeed},
3279 cb => sub { 3520 cb => sub {
3521 return unless $::CONN && $widget;
3522
3280 ++$widget->{frame}; 3523 ++$widget->{frame};
3524 $widget->update_face;
3281 $widget->update; 3525 $widget->update;
3526
3527 $widget->update_timer;
3282 }, 3528 },
3283 ); 3529 );
3530
3531 $self->update_face;
3532 $self->update_timer;
3284 } 3533 }
3285 3534
3286 $self 3535 $self
3287} 3536}
3288 3537
3538sub update_timer {
3539 my ($self) = @_;
3540
3541 return unless $self->{timer};
3542
3543 if ($self->{visible}) {
3544 $self->{timer}->at (
3545 $self->{anim_start}
3546 + $self->{animspeed}
3547 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3548 );
3549 $self->{timer}->start;
3550 } else {
3551 $self->{timer}->stop;
3552 }
3553}
3554
3555sub update_face {
3556 my ($self) = @_;
3557
3558 return unless $::CONN;
3559
3560 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3561 if ($anim && @$anim) {
3562 delete $self->{wait_face};
3563 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3564 }
3565 }
3566}
3567
3289sub size_request { 3568sub size_request {
3290 (32, 8) 3569 my ($self) = @_;
3570
3571 if ($::CONN) {
3572 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3573 if (my $tex = $::CONN->{texture}[$faceid]) {
3574 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3575 } else {
3576 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3577 $self->realloc;
3578 });
3579 }
3580 }
3581 }
3582
3583 ($self->{size_w} || 8, $self->{size_h} || 8)
3291} 3584}
3292 3585
3293sub update { 3586sub update {
3294 my ($self) = @_; 3587 my ($self) = @_;
3295 3588
3296 return unless $self->{visible}; 3589 return unless $self->{visible};
3297 3590
3298 $self->SUPER::update; 3591 $self->SUPER::update;
3299} 3592}
3300 3593
3594sub invoke_visibility_change {
3595 my ($self) = @_;
3596
3597 $self->update_timer;
3598
3599 0
3600}
3601
3301sub _draw { 3602sub _draw {
3302 my ($self) = @_; 3603 my ($self) = @_;
3303 3604
3304 return unless $::CONN; 3605 return unless $::CONN;
3305 3606
3306 $self->SUPER::_draw; 3607 $self->SUPER::_draw;
3307 3608
3308 my $face;
3309
3310 if ($self->{frame}) {
3311 my $anim = $::CONN->{anim}[$self->{anim}]; 3609 my $faceid = $::CONN->{faceid}[$self->{face}]
3312 3610 or return;
3313 $face = $anim->[ $self->{frame} % @$anim ]
3314 if $anim && @$anim;
3315 }
3316 3611
3317 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 3612 my $tex = $::CONN->{texture}[$faceid];
3318 3613
3319 if ($tex) { 3614 if ($tex) {
3320 glEnable GL_TEXTURE_2D; 3615 glEnable GL_TEXTURE_2D;
3321 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3322 glColor 0, 0, 0, 1; 3617 glColor 0, 0, 0, 1;
3326} 3621}
3327 3622
3328sub destroy { 3623sub destroy {
3329 my ($self) = @_; 3624 my ($self) = @_;
3330 3625
3331 $self->{timer}->cancel 3626 (delete $self->{timer})->cancel
3332 if $self->{timer}; 3627 if $self->{timer};
3333 3628
3334 $self->SUPER::destroy; 3629 $self->SUPER::destroy;
3335} 3630}
3336 3631
3532} 3827}
3533 3828
3534sub add { 3829sub add {
3535 my ($self, $title, $widget, $tooltip) = @_; 3830 my ($self, $title, $widget, $tooltip) = @_;
3536 3831
3537 Scalar::Util::weaken $self; 3832 CFPlus::weaken $self;
3538 3833
3539 $self->{buttonbar}->add (new CFPlus::UI::Button 3834 $self->{buttonbar}->add (new CFPlus::UI::Button
3540 markup => $title, 3835 markup => $title,
3541 tooltip => $tooltip, 3836 tooltip => $tooltip,
3542 on_activate => sub { $self->set_current_page ($widget) }, 3837 on_activate => sub { $self->set_current_page ($widget) },
3626 my $self = $class->SUPER::new ( 3921 my $self = $class->SUPER::new (
3627 fontsize => 0.8, 3922 fontsize => 0.8,
3628 @_, 3923 @_,
3629 ); 3924 );
3630 3925
3631 Scalar::Util::weaken (my $this = $self); 3926 CFPlus::weaken (my $this = $self);
3632 3927
3633 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3928 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3634 3929
3635 $self 3930 $self
3636} 3931}
3775 my $self = $class->SUPER::new ( 4070 my $self = $class->SUPER::new (
3776 visible => 1, 4071 visible => 1,
3777 @_, 4072 @_,
3778 ); 4073 );
3779 4074
3780 Scalar::Util::weaken ($self->{root} = $self); 4075 CFPlus::weaken ($self->{root} = $self);
3781 4076
3782 $self 4077 $self
3783} 4078}
3784 4079
3785sub size_request { 4080sub size_request {
3980 while ($self->{post_alloc_hook}) { 4275 while ($self->{post_alloc_hook}) {
3981 $_->() 4276 $_->()
3982 for values %{delete $self->{post_alloc_hook}}; 4277 for values %{delete $self->{post_alloc_hook}};
3983 } 4278 }
3984 4279
3985
3986 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4280 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3987 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4281 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3988 glClear GL_COLOR_BUFFER_BIT; 4282 glClear GL_COLOR_BUFFER_BIT;
3989 4283
3990 glMatrixMode GL_PROJECTION; 4284 glMatrixMode GL_PROJECTION;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines