… | |
… | |
944 | |
944 | |
945 | ############################################################################# |
945 | ############################################################################# |
946 | |
946 | |
947 | package CFPlus::UI::ViewPort; |
947 | package CFPlus::UI::ViewPort; |
948 | |
948 | |
|
|
949 | use List::Util qw(min max); |
|
|
950 | |
949 | our @ISA = CFPlus::UI::Window::; |
951 | our @ISA = CFPlus::UI::Window::; |
950 | |
952 | |
951 | sub new { |
953 | sub new { |
952 | my $class = shift; |
954 | my $class = shift; |
953 | |
955 | |
… | |
… | |
984 | } |
986 | } |
985 | |
987 | |
986 | sub set_offset { |
988 | sub set_offset { |
987 | my ($self, $x, $y) = @_; |
989 | my ($self, $x, $y) = @_; |
988 | |
990 | |
989 | $self->{view_x} = int $x; |
991 | $self->{view_x} = max 0, min $self->child->{w} - $self->{w}, int $x; |
990 | $self->{view_y} = int $y; |
992 | $self->{view_y} = max 0, min $self->child->{h} - $self->{h}, int $y; |
991 | |
993 | |
992 | $self->update; |
994 | $self->update; |
993 | } |
995 | } |
994 | |
996 | |
995 | # hmm, this does not work for topleft of $self... but we should not ask for that |
997 | # hmm, this does not work for topleft of $self... but we should not ask for that |
… | |
… | |
1061 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1063 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1062 | }, |
1064 | }, |
1063 | ; |
1065 | ; |
1064 | |
1066 | |
1065 | $self = $class->SUPER::new ( |
1067 | $self = $class->SUPER::new ( |
1066 | vp => (new CFPlus::UI::ViewPort expand => 1), |
1068 | scroll_x => 0, |
|
|
1069 | scroll_y => 1, |
1067 | can_events => 1, |
1070 | can_events => 1, |
1068 | hslider => $hslider, |
1071 | hslider => $hslider, |
1069 | vslider => $vslider, |
1072 | vslider => $vslider, |
1070 | col_expand => [1, 0], |
1073 | col_expand => [1, 0], |
1071 | row_expand => [1, 0], |
1074 | row_expand => [1, 0], |
1072 | %arg, |
1075 | %arg, |
1073 | ); |
1076 | ); |
1074 | |
1077 | |
|
|
1078 | $self->{vp} = new CFPlus::UI::ViewPort |
|
|
1079 | expand => 1, |
|
|
1080 | scroll_x => $self->{scroll_x}, |
|
|
1081 | scroll_y => $self->{scroll_y}, |
|
|
1082 | ; |
|
|
1083 | |
1075 | $self->SUPER::add (0, 0, $self->{vp}); |
1084 | $self->SUPER::add (0, 0, $self->{vp}); |
1076 | $self->SUPER::add (1, 0, $self->{vslider}); |
|
|
1077 | $self->SUPER::add (0, 1, $self->{hslider}); |
|
|
1078 | |
1085 | |
1079 | $self->add ($child) if $child; |
1086 | $self->add ($child) if $child; |
1080 | |
1087 | |
1081 | $self |
1088 | $self |
1082 | } |
1089 | } |
… | |
… | |
1087 | my ($self, $widget) = @_; |
1094 | my ($self, $widget) = @_; |
1088 | |
1095 | |
1089 | $self->{vp}->add ($self->{child} = $widget); |
1096 | $self->{vp}->add ($self->{child} = $widget); |
1090 | } |
1097 | } |
1091 | |
1098 | |
|
|
1099 | sub update_slider { |
|
|
1100 | my ($self) = @_; |
|
|
1101 | |
|
|
1102 | my $child = ($self->{vp} or return)->child; |
|
|
1103 | |
|
|
1104 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
|
|
1105 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
|
|
1106 | |
|
|
1107 | my $visible = $w1 > $w2; |
|
|
1108 | if ($visible != $self->{hslider}{visible}) { |
|
|
1109 | $visible ? $self->SUPER::add (0, 1, $self->{hslider}) |
|
|
1110 | : $self->{hslider}->hide; |
|
|
1111 | } |
|
|
1112 | |
|
|
1113 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
|
|
1114 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
|
|
1115 | |
|
|
1116 | my $visible = $h1 > $h2; |
|
|
1117 | if ($visible != $self->{vslider}{visible}) { |
|
|
1118 | $visible ? $self->SUPER::add (1, 0, $self->{vslider}) |
|
|
1119 | : $self->{vslider}->hide; |
|
|
1120 | } |
|
|
1121 | } |
|
|
1122 | |
|
|
1123 | sub update { |
|
|
1124 | my ($self) = @_; |
|
|
1125 | |
|
|
1126 | $self->SUPER::update; |
|
|
1127 | $self->update_slider; |
|
|
1128 | } |
|
|
1129 | |
1092 | sub invoke_mouse_wheel { |
1130 | sub invoke_mouse_wheel { |
1093 | my ($self, $ev) = @_; |
1131 | my ($self, $ev) = @_; |
1094 | |
1132 | |
1095 | return 0 unless $ev->{dy}; # only vertical movements |
1133 | return 0 unless $ev->{dy}; # only vertical movements for now |
1096 | |
1134 | |
1097 | $self->{vslider}->emit (mouse_wheel => $ev); |
1135 | $self->{vslider}->emit (mouse_wheel => $ev); |
1098 | |
1136 | |
1099 | 1 |
1137 | 1 |
1100 | } |
1138 | } |
1101 | |
1139 | |
1102 | sub update_slider { |
1140 | sub invoke_button_down { |
1103 | my ($self) = @_; |
1141 | my ($self, $ev, $x, $y) = @_; |
1104 | |
1142 | |
1105 | my $child = ($self->{vp} or return)->child; |
1143 | if ($ev->{button} == 2) { |
|
|
1144 | $self->grab_focus; |
|
|
1145 | |
|
|
1146 | $self->{motion} = sub { |
|
|
1147 | my ($ev, $x, $y) = @_; |
1106 | |
1148 | |
1107 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1149 | $self->{vp}->set_offset ( |
1108 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1150 | $self->{vp}{view_x} - $ev->{xrel}, |
1109 | #$self->{hslider}->set_visibility ($w1 != $w2); |
1151 | $self->{vp}{view_y} - $ev->{yrel}, |
|
|
1152 | ); |
|
|
1153 | }; |
1110 | |
1154 | |
1111 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1155 | return 1; |
1112 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1156 | } |
1113 | #$self->{vslider}->set_visibility ($h1 != $h2); |
|
|
1114 | } |
|
|
1115 | |
1157 | |
1116 | sub update { |
1158 | 0 |
1117 | my ($self) = @_; |
1159 | } |
1118 | |
1160 | |
1119 | $self->SUPER::update; |
1161 | sub invoke_button_up { |
1120 | $self->update_slider; |
1162 | my ($self, $ev, $x, $y) = @_; |
|
|
1163 | |
|
|
1164 | if (delete $self->{motion}) { |
|
|
1165 | return 1; |
|
|
1166 | } |
|
|
1167 | |
|
|
1168 | 0 |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | sub invoke_mouse_motion { |
|
|
1172 | my ($self, $ev, $x, $y) = @_; |
|
|
1173 | |
|
|
1174 | if ($self->{motion}) { |
|
|
1175 | $self->{motion}->($ev, $x, $y); |
|
|
1176 | return 1; |
|
|
1177 | } |
|
|
1178 | |
|
|
1179 | 0 |
1121 | } |
1180 | } |
1122 | |
1181 | |
1123 | sub invoke_size_allocate { |
1182 | sub invoke_size_allocate { |
1124 | my ($self, $w, $h) = @_; |
1183 | my ($self, $w, $h) = @_; |
1125 | |
1184 | |
… | |
… | |
1502 | |
1561 | |
1503 | sub new { |
1562 | sub new { |
1504 | my $class = shift; |
1563 | my $class = shift; |
1505 | |
1564 | |
1506 | $class->SUPER::new ( |
1565 | $class->SUPER::new ( |
|
|
1566 | children => [], |
1507 | col_expand => [], |
1567 | col_expand => [], |
1508 | row_expand => [], |
1568 | row_expand => [], |
1509 | @_, |
1569 | @_, |
1510 | ) |
1570 | ) |
1511 | } |
1571 | } |
1512 | |
1572 | |
1513 | sub children { |
1573 | sub children { |
1514 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1574 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1515 | } |
1575 | } |
1516 | |
1576 | |
|
|
1577 | # TODO: store row/col info in child widget and use standard add/del |
1517 | sub add { |
1578 | sub add { |
1518 | my ($self) = shift; |
1579 | my ($self) = shift; |
1519 | |
1580 | |
1520 | while (@_) { |
1581 | while (@_) { |
1521 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1582 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1522 | $child->set_parent ($self); |
1583 | $child->set_parent ($self); |
1523 | $self->{children}[$y][$x] = $child; |
1584 | $self->{children}[$y][$x] = $child; |
1524 | } |
1585 | } |
1525 | |
1586 | |
1526 | $self->{force_realloc} = 1; |
1587 | $self->{force_realloc} = 1; |
1527 | $self->{force_size_alloc} = 1; |
1588 | $self->{force_size_alloc} = 1; |
1528 | $self->realloc; |
1589 | $self->realloc; |
1529 | } |
1590 | } |
1530 | |
1591 | |
1531 | sub remove { |
1592 | sub remove { |
1532 | my ($self, $child) = @_; |
1593 | my ($self, $child) = @_; |
1533 | |
1594 | |
1534 | # TODO: not yet implemented |
1595 | for (@{ $self->{children} }) { |
|
|
1596 | for (@{ $_ || [] }) { |
|
|
1597 | $_ = undef if $_ == $child; |
|
|
1598 | } |
|
|
1599 | } |
1535 | } |
1600 | } |
1536 | |
1601 | |
1537 | # TODO: move to container class maybe? send children a signal on removal? |
1602 | # TODO: move to container class maybe? send children a signal on removal? |
1538 | sub clear { |
1603 | sub clear { |
1539 | my ($self) = @_; |
1604 | my ($self) = @_; |
… | |
… | |
1652 | my ($self) = @_; |
1717 | my ($self) = @_; |
1653 | |
1718 | |
1654 | for (grep $_, @{$self->{children}}) { |
1719 | for (grep $_, @{$self->{children}}) { |
1655 | $_->draw for grep $_, @$_; |
1720 | $_->draw for grep $_, @$_; |
1656 | } |
1721 | } |
|
|
1722 | } |
|
|
1723 | |
|
|
1724 | ############################################################################# |
|
|
1725 | |
|
|
1726 | package CFPlus::UI::Fixed; |
|
|
1727 | |
|
|
1728 | use List::Util qw(min max); |
|
|
1729 | |
|
|
1730 | our @ISA = CFPlus::UI::Container::; |
|
|
1731 | |
|
|
1732 | sub add { |
|
|
1733 | my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_; |
|
|
1734 | |
|
|
1735 | $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h]; |
|
|
1736 | $self->SUPER::add ($child); |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | sub _scale($$$) { |
|
|
1740 | my ($mode, $val, $max) = @_; |
|
|
1741 | |
|
|
1742 | $mode eq "abs" ? $val |
|
|
1743 | : $mode eq "rel" ? $val * $max |
|
|
1744 | : 0 |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | sub size_request { |
|
|
1748 | my ($self) = @_; |
|
|
1749 | |
|
|
1750 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
|
|
1751 | |
|
|
1752 | # determine overall size by querying abs widgets |
|
|
1753 | for my $child ($self->visible_children) { |
|
|
1754 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
|
|
1755 | |
|
|
1756 | if ($pos eq "abs") { |
|
|
1757 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1758 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1759 | |
|
|
1760 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
|
|
1761 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
|
|
1762 | } |
|
|
1763 | } |
|
|
1764 | |
|
|
1765 | my $W = $x2 - $x1; |
|
|
1766 | my $H = $y2 - $y1; |
|
|
1767 | |
|
|
1768 | # now layout remaining widgets |
|
|
1769 | for my $child ($self->visible_children) { |
|
|
1770 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
|
|
1771 | |
|
|
1772 | if ($pos ne "abs") { |
|
|
1773 | $x = _scale $pos, $x, $W; |
|
|
1774 | $y = _scale $pos, $x, $H; |
|
|
1775 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1776 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1777 | |
|
|
1778 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
|
|
1779 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
|
|
1780 | } |
|
|
1781 | } |
|
|
1782 | |
|
|
1783 | my $W = $x2 - $x1; |
|
|
1784 | my $H = $y2 - $y1; |
|
|
1785 | |
|
|
1786 | ($W, $H) |
|
|
1787 | } |
|
|
1788 | |
|
|
1789 | sub invoke_size_allocate { |
|
|
1790 | my ($self, $W, $H) = @_; |
|
|
1791 | |
|
|
1792 | for my $child ($self->visible_children) { |
|
|
1793 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
|
|
1794 | |
|
|
1795 | $x = _scale $pos, $x, $W; |
|
|
1796 | $y = _scale $pos, $x, $H; |
|
|
1797 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1798 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1799 | |
|
|
1800 | $child->configure ($x, $y, $w, $h); |
|
|
1801 | } |
|
|
1802 | |
|
|
1803 | 1 |
1657 | } |
1804 | } |
1658 | |
1805 | |
1659 | ############################################################################# |
1806 | ############################################################################# |
1660 | |
1807 | |
1661 | package CFPlus::UI::Box; |
1808 | package CFPlus::UI::Box; |
… | |
… | |
3321 | |
3468 | |
3322 | sub new { |
3469 | sub new { |
3323 | my $class = shift; |
3470 | my $class = shift; |
3324 | |
3471 | |
3325 | my $self = $class->SUPER::new ( |
3472 | my $self = $class->SUPER::new ( |
|
|
3473 | size_w => 32, |
|
|
3474 | size_h => 8, |
3326 | aspect => 1, |
3475 | aspect => 1, |
3327 | can_events => 0, |
3476 | can_events => 0, |
3328 | @_, |
3477 | @_, |
3329 | ); |
3478 | ); |
3330 | |
3479 | |
… | |
… | |
3334 | $self->{timer} = Event->timer ( |
3483 | $self->{timer} = Event->timer ( |
3335 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3484 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3336 | hard => 1, |
3485 | hard => 1, |
3337 | interval => $self->{animspeed}, |
3486 | interval => $self->{animspeed}, |
3338 | cb => sub { |
3487 | cb => sub { |
3339 | delete $self->{wait_face}; |
3488 | return unless $::CONN; |
|
|
3489 | |
3340 | ++$widget->{frame}; |
3490 | ++$widget->{frame}; |
|
|
3491 | $self->update_face; |
3341 | $widget->update; |
3492 | $self->update; |
3342 | }, |
3493 | }, |
3343 | ); |
3494 | ); |
|
|
3495 | |
|
|
3496 | $self->update_face; |
3344 | } |
3497 | } |
3345 | |
3498 | |
3346 | $self |
3499 | $self |
3347 | } |
3500 | } |
3348 | |
3501 | |
|
|
3502 | sub update_face { |
|
|
3503 | my ($self) = @_; |
|
|
3504 | |
|
|
3505 | return unless $::CONN; |
|
|
3506 | |
|
|
3507 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
|
|
3508 | if ($anim && @$anim) { |
|
|
3509 | delete $self->{wait_face}; |
|
|
3510 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3511 | } |
|
|
3512 | } |
|
|
3513 | } |
|
|
3514 | |
3349 | sub size_request { |
3515 | sub size_request { |
3350 | (32, 8) |
3516 | my ($self) = @_; |
|
|
3517 | |
|
|
3518 | if ($::CONN) { |
|
|
3519 | if (my $faceid = $::CONN->{faceid}[$self->{face}]) { |
|
|
3520 | if (my $tex = $::CONN->{texture}[$faceid]) { |
|
|
3521 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
|
|
3522 | } else { |
|
|
3523 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
|
|
3524 | $self->realloc; |
|
|
3525 | }); |
|
|
3526 | } |
|
|
3527 | } |
|
|
3528 | } |
|
|
3529 | |
|
|
3530 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3351 | } |
3531 | } |
3352 | |
3532 | |
3353 | sub update { |
3533 | sub update { |
3354 | my ($self) = @_; |
3534 | my ($self) = @_; |
3355 | |
3535 | |
… | |
… | |
3363 | |
3543 | |
3364 | return unless $::CONN; |
3544 | return unless $::CONN; |
3365 | |
3545 | |
3366 | $self->SUPER::_draw; |
3546 | $self->SUPER::_draw; |
3367 | |
3547 | |
3368 | my $face; |
|
|
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}] |
3548 | my $faceid = $::CONN->{faceid}[$self->{face}] |
3378 | or return; |
3549 | or return; |
3379 | |
3550 | |
3380 | my $tex = $::CONN->{texture}[$faceid]; |
3551 | my $tex = $::CONN->{texture}[$faceid]; |
3381 | |
3552 | |
3382 | if ($tex) { |
3553 | if ($tex) { |
3383 | glEnable GL_TEXTURE_2D; |
3554 | glEnable GL_TEXTURE_2D; |
3384 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3555 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3385 | glColor 0, 0, 0, 1; |
3556 | glColor 0, 0, 0, 1; |
3386 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3557 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3387 | glDisable GL_TEXTURE_2D; |
3558 | glDisable GL_TEXTURE_2D; |
3388 | } else { |
|
|
3389 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
|
|
3390 | $self->update; |
|
|
3391 | }); |
|
|
3392 | } |
3559 | } |
3393 | } |
3560 | } |
3394 | |
3561 | |
3395 | sub destroy { |
3562 | sub destroy { |
3396 | my ($self) = @_; |
3563 | my ($self) = @_; |