… | |
… | |
335 | sub size_request { |
335 | sub size_request { |
336 | require Carp; |
336 | require Carp; |
337 | Carp::confess "size_request is abstract"; |
337 | Carp::confess "size_request is abstract"; |
338 | } |
338 | } |
339 | |
339 | |
|
|
340 | sub baseline_shift { |
|
|
341 | 0 |
|
|
342 | } |
|
|
343 | |
340 | sub configure { |
344 | sub configure { |
341 | my ($self, $x, $y, $w, $h) = @_; |
345 | my ($self, $x, $y, $w, $h) = @_; |
342 | |
346 | |
343 | if ($self->{aspect}) { |
347 | if ($self->{aspect}) { |
344 | my ($ow, $oh) = ($w, $h); |
348 | my ($ow, $oh) = ($w, $h); |
… | |
… | |
419 | my ($self) = @_; |
423 | my ($self) = @_; |
420 | |
424 | |
421 | return if $FOCUS == $self; |
425 | return if $FOCUS == $self; |
422 | return unless $self->{can_focus}; |
426 | return unless $self->{can_focus}; |
423 | |
427 | |
424 | my $focus = $FOCUS; $FOCUS = $self; |
428 | $FOCUS = $self; |
425 | |
429 | |
426 | $focus->update if $focus; |
430 | $self->update; |
427 | $FOCUS->update; |
|
|
428 | |
431 | |
429 | 0 |
432 | 0 |
430 | } |
433 | } |
431 | |
434 | |
432 | sub invoke_focus_out { |
435 | sub invoke_focus_out { |
433 | my ($self) = @_; |
436 | my ($self) = @_; |
434 | |
437 | |
435 | return unless $FOCUS == $self; |
438 | return unless $FOCUS == $self; |
436 | |
439 | |
437 | my $focus = $FOCUS; undef $FOCUS; |
440 | undef $FOCUS; |
438 | |
441 | |
439 | $focus->update if $focus; #? |
442 | $self->update; |
440 | |
443 | |
441 | $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus |
444 | $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus |
442 | unless $FOCUS; |
445 | unless $FOCUS; |
443 | |
446 | |
444 | 0 |
447 | 0 |
445 | } |
448 | } |
446 | |
449 | |
447 | sub grab_focus { |
450 | sub grab_focus { |
448 | my ($self) = @_; |
451 | my ($self) = @_; |
449 | |
452 | |
|
|
453 | $FOCUS->emit ("focus_out") if $FOCUS; |
450 | $self->emit ("focus_in"); |
454 | $self->emit ("focus_in"); |
451 | } |
455 | } |
452 | |
456 | |
453 | sub invoke_mouse_motion { 1 } |
457 | sub invoke_mouse_motion { 1 } |
454 | sub invoke_button_up { 1 } |
458 | sub invoke_button_up { 1 } |
… | |
… | |
535 | return unless $self->{h} && $self->{w}; |
539 | return unless $self->{h} && $self->{w}; |
536 | |
540 | |
537 | # update screen rectangle |
541 | # update screen rectangle |
538 | local $draw_x = $draw_x + $self->{x}; |
542 | local $draw_x = $draw_x + $self->{x}; |
539 | local $draw_y = $draw_y + $self->{y}; |
543 | local $draw_y = $draw_y + $self->{y}; |
540 | local $draw_w = $draw_x + $self->{w}; |
|
|
541 | local $draw_h = $draw_y + $self->{h}; |
|
|
542 | |
544 | |
543 | # skip widgets that are entirely outside the drawing area |
545 | # skip widgets that are entirely outside the drawing area |
544 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
546 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
545 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
547 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
546 | |
548 | |
… | |
… | |
585 | } |
587 | } |
586 | |
588 | |
587 | sub DESTROY { |
589 | sub DESTROY { |
588 | my ($self) = @_; |
590 | my ($self) = @_; |
589 | |
591 | |
|
|
592 | return if CFClient::in_destruct; |
|
|
593 | |
590 | delete $WIDGET{$self+0}; |
594 | delete $WIDGET{$self+0}; |
591 | |
595 | |
592 | eval { $self->destroy }; |
596 | eval { $self->destroy }; |
593 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
597 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
594 | } |
598 | } |
… | |
… | |
984 | $self->add ($child) if $child; |
988 | $self->add ($child) if $child; |
985 | |
989 | |
986 | $self |
990 | $self |
987 | } |
991 | } |
988 | |
992 | |
|
|
993 | #TODO# update range on size_allocate depending on child |
|
|
994 | |
989 | sub add { |
995 | sub add { |
990 | my ($self, $widget) = @_; |
996 | my ($self, $widget) = @_; |
991 | |
997 | |
992 | $self->{vp}->add ($self->{child} = $widget); |
998 | $self->{vp}->add ($self->{child} = $widget); |
993 | } |
999 | } |
994 | |
1000 | |
|
|
1001 | sub update_slider { |
|
|
1002 | my ($self) = @_; |
|
|
1003 | |
|
|
1004 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); |
|
|
1005 | } |
|
|
1006 | |
995 | sub update { |
1007 | sub update { |
996 | my ($self) = @_; |
1008 | my ($self) = @_; |
997 | |
1009 | |
998 | $self->SUPER::update; |
1010 | $self->SUPER::update; |
999 | |
1011 | |
1000 | # todo: overwrite size_allocate of child |
1012 | $self->update_slider; |
1001 | my $child = $self->{vp}->child; |
|
|
1002 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
|
|
1003 | } |
1013 | } |
1004 | |
1014 | |
1005 | sub invoke_size_allocate { |
1015 | sub invoke_size_allocate { |
1006 | my ($self, $w, $h) = @_; |
1016 | my ($self, $w, $h) = @_; |
1007 | |
1017 | |
1008 | my $child = $self->{vp}->child; |
1018 | $self->update_slider; |
1009 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
|
|
1010 | |
1019 | |
1011 | $self->SUPER::invoke_size_allocate ($w, $h) |
1020 | $self->SUPER::invoke_size_allocate ($w, $h) |
1012 | } |
1021 | } |
1013 | |
|
|
1014 | #TODO# update range on size_allocate depending on child |
|
|
1015 | # update viewport offset on scroll |
|
|
1016 | |
1022 | |
1017 | ############################################################################# |
1023 | ############################################################################# |
1018 | |
1024 | |
1019 | package CFClient::UI::Frame; |
1025 | package CFClient::UI::Frame; |
1020 | |
1026 | |
… | |
… | |
1076 | my $self = $class->SUPER::new ( |
1082 | my $self = $class->SUPER::new ( |
1077 | bg => [1, 1, 1, 1], |
1083 | bg => [1, 1, 1, 1], |
1078 | border_bg => [1, 1, 1, 1], |
1084 | border_bg => [1, 1, 1, 1], |
1079 | border => 0.6, |
1085 | border => 0.6, |
1080 | can_events => 1, |
1086 | can_events => 1, |
1081 | min_w => 16, |
1087 | min_w => 64, |
1082 | min_h => 16, |
1088 | min_h => 32, |
1083 | %arg, |
1089 | %arg, |
1084 | ); |
1090 | ); |
1085 | |
1091 | |
1086 | $self->{title_widget} = new CFClient::UI::Label |
1092 | $self->{title_widget} = new CFClient::UI::Label |
1087 | align => 0, |
1093 | align => 0, |
… | |
… | |
1091 | if exists $self->{title}; |
1097 | if exists $self->{title}; |
1092 | |
1098 | |
1093 | if ($self->{has_close_button}) { |
1099 | if ($self->{has_close_button}) { |
1094 | $self->{close_button} = |
1100 | $self->{close_button} = |
1095 | new CFClient::UI::ImageButton |
1101 | new CFClient::UI::ImageButton |
1096 | image => 'x1_close.png', |
1102 | path => 'x1_close.png', |
1097 | on_activate => sub { $self->hide }; |
1103 | on_activate => sub { $self->emit ("delete") }; |
1098 | |
1104 | |
1099 | $self->CFClient::UI::Container::add ($self->{close_button}); |
1105 | $self->CFClient::UI::Container::add ($self->{close_button}); |
1100 | } |
1106 | } |
1101 | |
1107 | |
1102 | $self |
1108 | $self |
… | |
… | |
1148 | $self->child->configure ($border, $border, $w, $h); |
1154 | $self->child->configure ($border, $border, $w, $h); |
1149 | |
1155 | |
1150 | $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border) |
1156 | $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border) |
1151 | if $self->{close_button}; |
1157 | if $self->{close_button}; |
1152 | |
1158 | |
|
|
1159 | 1 |
|
|
1160 | } |
|
|
1161 | |
|
|
1162 | sub invoke_delete { |
|
|
1163 | my ($self) = @_; |
|
|
1164 | |
|
|
1165 | $self->hide; |
|
|
1166 | |
1153 | 1 |
1167 | 1 |
1154 | } |
1168 | } |
1155 | |
1169 | |
1156 | sub invoke_button_down { |
1170 | sub invoke_button_down { |
1157 | my ($self, $ev, $x, $y) = @_; |
1171 | my ($self, $ev, $x, $y) = @_; |
… | |
… | |
1438 | |
1452 | |
1439 | sub invoke_size_allocate { |
1453 | sub invoke_size_allocate { |
1440 | my ($self, $w, $h) = @_; |
1454 | my ($self, $w, $h) = @_; |
1441 | |
1455 | |
1442 | my $space = $self->{vertical} ? $h : $w; |
1456 | my $space = $self->{vertical} ? $h : $w; |
1443 | my $children = $self->{children}; |
1457 | my @children = $self->visible_children; |
1444 | |
1458 | |
1445 | my @req; |
1459 | my @req; |
1446 | |
1460 | |
1447 | if ($self->{homogeneous}) { |
1461 | if ($self->{homogeneous}) { |
1448 | @req = ($space / (@$children || 1)) x @$children; |
1462 | @req = ($space / (@children || 1)) x @children; |
1449 | } else { |
1463 | } else { |
1450 | @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; |
1464 | @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children; |
1451 | my $req = List::Util::sum @req; |
1465 | my $req = List::Util::sum @req; |
1452 | |
1466 | |
1453 | if ($req > $space) { |
1467 | if ($req > $space) { |
1454 | # ah well, not enough space |
1468 | # ah well, not enough space |
1455 | $_ *= $space / $req for @req; |
1469 | $_ *= $space / $req for @req; |
1456 | } else { |
1470 | } else { |
1457 | my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; |
1471 | my $expand = (List::Util::sum map $_->{expand}, @children) || 1; |
1458 | |
1472 | |
1459 | $space = ($space - $req) / $expand; # remaining space to give away |
1473 | $space = ($space - $req) / $expand; # remaining space to give away |
1460 | |
1474 | |
1461 | $req[$_] += $space * $children->[$_]{expand} |
1475 | $req[$_] += $space * $children[$_]{expand} |
1462 | for 0 .. $#$children; |
1476 | for 0 .. $#children; |
1463 | } |
1477 | } |
1464 | } |
1478 | } |
1465 | |
1479 | |
1466 | CFClient::UI::harmonize \@req; |
1480 | CFClient::UI::harmonize \@req; |
1467 | |
1481 | |
1468 | my $pos = 0; |
1482 | my $pos = 0; |
1469 | for (0 .. $#$children) { |
1483 | for (0 .. $#children) { |
1470 | my $alloc = $req[$_]; |
1484 | my $alloc = $req[$_]; |
1471 | $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1485 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1472 | |
1486 | |
1473 | $pos += $alloc; |
1487 | $pos += $alloc; |
1474 | } |
1488 | } |
1475 | |
1489 | |
1476 | 1 |
1490 | 1 |
… | |
… | |
1579 | my ($self, $text) = @_; |
1593 | my ($self, $text) = @_; |
1580 | |
1594 | |
1581 | return if $self->{text} eq "T$text"; |
1595 | return if $self->{text} eq "T$text"; |
1582 | $self->{text} = "T$text"; |
1596 | $self->{text} = "T$text"; |
1583 | |
1597 | |
1584 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
|
|
1585 | $self->{layout}->set_text ($text); |
1598 | $self->{layout}->set_text ($text); |
1586 | |
1599 | |
1587 | delete $self->{size_req}; |
1600 | delete $self->{size_req}; |
1588 | $self->realloc; |
1601 | $self->realloc; |
1589 | $self->update; |
1602 | $self->update; |
… | |
… | |
1595 | return if $self->{text} eq "M$markup"; |
1608 | return if $self->{text} eq "M$markup"; |
1596 | $self->{text} = "M$markup"; |
1609 | $self->{text} = "M$markup"; |
1597 | |
1610 | |
1598 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1611 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1599 | |
1612 | |
1600 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
|
|
1601 | $self->{layout}->set_markup ($markup); |
1613 | $self->{layout}->set_markup ($markup); |
1602 | |
1614 | |
1603 | delete $self->{size_req}; |
1615 | delete $self->{size_req}; |
1604 | $self->realloc; |
1616 | $self->realloc; |
1605 | $self->update; |
1617 | $self->update; |
… | |
… | |
1617 | |
1629 | |
1618 | my ($w, $h) = $self->{layout}->size; |
1630 | my ($w, $h) = $self->{layout}->size; |
1619 | |
1631 | |
1620 | if (exists $self->{template}) { |
1632 | if (exists $self->{template}) { |
1621 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1633 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1634 | $self->{template}->set_width ($self->{max_w} || -1); |
1622 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1635 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1623 | |
1636 | |
1624 | my ($w2, $h2) = $self->{template}->size; |
1637 | my ($w2, $h2) = $self->{template}->size; |
1625 | |
1638 | |
1626 | $w = List::Util::max $w, $w2; |
1639 | $w = List::Util::max $w, $w2; |
… | |
… | |
1631 | }; |
1644 | }; |
1632 | |
1645 | |
1633 | @{ $self->{size_req} } |
1646 | @{ $self->{size_req} } |
1634 | } |
1647 | } |
1635 | |
1648 | |
|
|
1649 | sub baseline_shift { |
|
|
1650 | $_[0]{layout}->descent |
|
|
1651 | } |
|
|
1652 | |
1636 | sub invoke_size_allocate { |
1653 | sub invoke_size_allocate { |
1637 | my ($self, $w, $h) = @_; |
1654 | my ($self, $w, $h) = @_; |
1638 | |
1655 | |
1639 | delete $self->{ox}; |
1656 | delete $self->{ox}; |
1640 | |
1657 | |
… | |
… | |
1646 | |
1663 | |
1647 | sub set_fontsize { |
1664 | sub set_fontsize { |
1648 | my ($self, $fontsize) = @_; |
1665 | my ($self, $fontsize) = @_; |
1649 | |
1666 | |
1650 | $self->{fontsize} = $fontsize; |
1667 | $self->{fontsize} = $fontsize; |
|
|
1668 | delete $self->{size_req}; |
1651 | delete $self->{texture}; |
1669 | delete $self->{texture}; |
1652 | |
1670 | |
1653 | $self->realloc; |
1671 | $self->realloc; |
1654 | } |
1672 | } |
1655 | |
1673 | |
1656 | sub reconfigure { |
1674 | sub reconfigure { |
1657 | my ($self) = @_; |
1675 | my ($self) = @_; |
1658 | |
1676 | |
1659 | delete $self->{size_req}; |
1677 | delete $self->{size_req}; |
|
|
1678 | delete $self->{texture}; |
1660 | |
1679 | |
1661 | $self->SUPER::reconfigure; |
1680 | $self->SUPER::reconfigure; |
1662 | } |
1681 | } |
1663 | |
1682 | |
1664 | sub _draw { |
1683 | sub _draw { |
1665 | my ($self) = @_; |
1684 | my ($self) = @_; |
1666 | |
1685 | |
1667 | $self->SUPER::_draw; # draw background, if applicable |
1686 | $self->SUPER::_draw; # draw background, if applicable |
1668 | |
1687 | |
1669 | my $tex = $self->{texture} ||= do { |
1688 | my $size = $self->{texture} ||= do { |
1670 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1689 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1671 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1690 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1672 | $self->{layout}->set_width ($self->{w}); |
1691 | $self->{layout}->set_width ($self->{w}); |
1673 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1692 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1674 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1693 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1675 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1694 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1676 | |
1695 | |
1677 | new_from_layout CFClient::Texture $self->{layout} |
1696 | [$self->{layout}->size] |
1678 | }; |
1697 | }; |
1679 | |
1698 | |
1680 | unless (exists $self->{ox}) { |
1699 | unless (exists $self->{ox}) { |
1681 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1700 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1682 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} |
1701 | : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} |
1683 | : ($self->{w} - $tex->{w}) * 0.5); |
1702 | : ($self->{w} - $size->[0]) * 0.5); |
1684 | |
1703 | |
1685 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1704 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1686 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1705 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
1687 | : ($self->{h} - $tex->{h}) * 0.5); |
1706 | : ($self->{h} - $size->[1]) * 0.5); |
1688 | }; |
1707 | }; |
1689 | |
1708 | |
1690 | glEnable GL_TEXTURE_2D; |
|
|
1691 | |
|
|
1692 | my $w = List::Util::min $self->{w} + 4, $tex->{w}; |
1709 | my $w = List::Util::min $self->{w} + 4, $size->[0]; |
1693 | my $h = List::Util::min $self->{h} + 2, $tex->{h}; |
1710 | my $h = List::Util::min $self->{h} + 2, $size->[1]; |
1694 | |
1711 | |
1695 | if ($tex->{format} == GL_ALPHA) { |
1712 | $self->{layout}->render ($self->{ox}, $self->{oy}); |
1696 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1697 | glColor @{$self->{fg}}; |
|
|
1698 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h); |
|
|
1699 | } else { |
|
|
1700 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1701 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h); |
|
|
1702 | } |
|
|
1703 | |
|
|
1704 | glDisable GL_TEXTURE_2D; |
|
|
1705 | } |
1713 | } |
1706 | |
1714 | |
1707 | ############################################################################# |
1715 | ############################################################################# |
1708 | |
1716 | |
1709 | package CFClient::UI::EntryBase; |
1717 | package CFClient::UI::EntryBase; |
… | |
… | |
1820 | |
1828 | |
1821 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1829 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1822 | |
1830 | |
1823 | # byte-index to char-index |
1831 | # byte-index to char-index |
1824 | my $text = $self->{text}; |
1832 | my $text = $self->{text}; |
1825 | utf8::encode $text; |
1833 | utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; |
1826 | $self->{cursor} = length substr $text, 0, $idx; |
1834 | $self->{cursor} = length $text; |
1827 | |
1835 | |
1828 | $self->_set_text ($self->{text}); |
1836 | $self->_set_text ($self->{text}); |
1829 | $self->update; |
1837 | $self->update; |
1830 | |
1838 | |
1831 | 1 |
1839 | 1 |
… | |
… | |
2051 | my ($self) = @_; |
2059 | my ($self) = @_; |
2052 | |
2060 | |
2053 | (6) x 2 |
2061 | (6) x 2 |
2054 | } |
2062 | } |
2055 | |
2063 | |
|
|
2064 | sub toggle { |
|
|
2065 | my ($self) = @_; |
|
|
2066 | |
|
|
2067 | $self->{state} = !$self->{state}; |
|
|
2068 | $self->emit (changed => $self->{state}); |
|
|
2069 | $self->update; |
|
|
2070 | } |
|
|
2071 | |
2056 | sub invoke_button_down { |
2072 | sub invoke_button_down { |
2057 | my ($self, $ev, $x, $y) = @_; |
2073 | my ($self, $ev, $x, $y) = @_; |
2058 | |
2074 | |
2059 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
2075 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
2060 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
2076 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
2061 | $self->{state} = !$self->{state}; |
2077 | $self->toggle; |
2062 | $self->emit (changed => $self->{state}); |
|
|
2063 | } else { |
2078 | } else { |
2064 | return 0 |
2079 | return 0 |
2065 | } |
2080 | } |
2066 | |
2081 | |
2067 | 1 |
2082 | 1 |
… | |
… | |
2092 | package CFClient::UI::Image; |
2107 | package CFClient::UI::Image; |
2093 | |
2108 | |
2094 | our @ISA = CFClient::UI::Base::; |
2109 | our @ISA = CFClient::UI::Base::; |
2095 | |
2110 | |
2096 | use CFClient::OpenGL; |
2111 | use CFClient::OpenGL; |
2097 | use Carp qw/confess/; |
|
|
2098 | |
2112 | |
2099 | our %loaded_images; |
2113 | our %texture_cache; |
2100 | |
2114 | |
2101 | sub new { |
2115 | sub new { |
2102 | my $class = shift; |
2116 | my $class = shift; |
2103 | |
2117 | |
2104 | my $self = $class->SUPER::new (can_events => 0, @_); |
2118 | my $self = $class->SUPER::new ( |
|
|
2119 | can_events => 0, |
|
|
2120 | @_, |
|
|
2121 | ); |
2105 | |
2122 | |
2106 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
2123 | $self->{path} || $self->{tex} |
|
|
2124 | or Carp::croak "'path' or 'tex' attributes required"; |
2107 | |
2125 | |
2108 | $loaded_images{$self->{image}} ||= |
2126 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2109 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
2127 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; |
2110 | |
2128 | |
2111 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
2129 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2112 | |
2130 | |
2113 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
2131 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2114 | |
|
|
2115 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
2116 | |
2132 | |
2117 | $self |
2133 | $self |
2118 | } |
2134 | } |
2119 | |
2135 | |
2120 | sub size_request { |
2136 | sub size_request { |
2121 | my ($self) = @_; |
2137 | my ($self) = @_; |
2122 | |
2138 | |
2123 | ($self->{tex}->{w}, $self->{tex}->{h}) |
2139 | ($self->{tex}{w}, $self->{tex}{h}) |
2124 | } |
2140 | } |
2125 | |
2141 | |
2126 | sub _draw { |
2142 | sub _draw { |
2127 | my ($self) = @_; |
2143 | my ($self) = @_; |
2128 | |
2144 | |
… | |
… | |
2237 | my $ycut1 = max 0, min 1, $ycut; |
2253 | my $ycut1 = max 0, min 1, $ycut; |
2238 | my $ycut2 = max 0, min 1, $ycut - 1; |
2254 | my $ycut2 = max 0, min 1, $ycut - 1; |
2239 | |
2255 | |
2240 | my $h1 = $self->{h} * (1 - $ycut1); |
2256 | my $h1 = $self->{h} * (1 - $ycut1); |
2241 | my $h2 = $self->{h} * (1 - $ycut2); |
2257 | my $h2 = $self->{h} * (1 - $ycut2); |
|
|
2258 | my $h3 = $self->{h}; |
|
|
2259 | |
|
|
2260 | $_ = $_ * (284-4)/288 + 4/288 for ($h1, $h2, $h3); |
2242 | |
2261 | |
2243 | glEnable GL_BLEND; |
2262 | glEnable GL_BLEND; |
2244 | glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
2263 | glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
2245 | GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2264 | GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2246 | glEnable GL_TEXTURE_2D; |
2265 | glEnable GL_TEXTURE_2D; |
… | |
… | |
2265 | |
2284 | |
2266 | if ($t3) { |
2285 | if ($t3) { |
2267 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
2286 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
2268 | glBegin GL_QUADS; |
2287 | glBegin GL_QUADS; |
2269 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
2288 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
2270 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
2289 | glTexCoord 0 , $t3->{t}; glVertex 0 , $h3; |
2271 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
2290 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3; |
2272 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
2291 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
2273 | glEnd; |
2292 | glEnd; |
2274 | } |
2293 | } |
2275 | |
2294 | |
2276 | glDisable GL_BLEND; |
2295 | glDisable GL_BLEND; |
… | |
… | |
2545 | can_events => 0, |
2564 | can_events => 0, |
2546 | indent => 0, |
2565 | indent => 0, |
2547 | #font => default_font |
2566 | #font => default_font |
2548 | @_, |
2567 | @_, |
2549 | |
2568 | |
2550 | layout => (new CFClient::Layout 1), |
2569 | layout => (new CFClient::Layout), |
2551 | par => [], |
2570 | par => [], |
2552 | height => 0, |
2571 | height => 0, |
2553 | children => [ |
2572 | children => [ |
2554 | (new CFClient::UI::Empty expand => 1), |
2573 | (new CFClient::UI::Empty expand => 1), |
2555 | (new CFClient::UI::Slider vertical => 1), |
2574 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2566 | |
2585 | |
2567 | $self->{fontsize} = $fontsize; |
2586 | $self->{fontsize} = $fontsize; |
2568 | $self->reflow; |
2587 | $self->reflow; |
2569 | } |
2588 | } |
2570 | |
2589 | |
|
|
2590 | sub size_request { |
|
|
2591 | my ($self) = @_; |
|
|
2592 | |
|
|
2593 | my ($empty, $slider) = @{ $self->{children} }; |
|
|
2594 | |
|
|
2595 | local $self->{children} = [$empty, $slider]; |
|
|
2596 | $self->SUPER::size_request |
|
|
2597 | } |
|
|
2598 | |
2571 | sub invoke_size_allocate { |
2599 | sub invoke_size_allocate { |
2572 | my ($self, $w, $h) = @_; |
2600 | my ($self, $w, $h) = @_; |
2573 | |
2601 | |
|
|
2602 | my ($empty, $slider, @other) = @{ $self->{children} }; |
|
|
2603 | $_->configure (@$_{qw(x y req_w req_h)}) for @other; |
|
|
2604 | |
2574 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2605 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2575 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2606 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2576 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2607 | $self->{layout}->set_width ($empty->{w}); |
2577 | $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
2608 | $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
2578 | |
2609 | |
2579 | $self->reflow; |
2610 | $self->reflow; |
2580 | |
2611 | |
|
|
2612 | local $self->{children} = [$empty, $slider]; |
2581 | $self->SUPER::invoke_size_allocate ($w, $h) |
2613 | $self->SUPER::invoke_size_allocate ($w, $h) |
2582 | } |
2614 | } |
2583 | |
2615 | |
2584 | sub text_size { |
2616 | sub get_layout { |
2585 | my ($self, $text, $indent) = @_; |
2617 | my ($self, $para) = @_; |
2586 | |
2618 | |
2587 | my $layout = $self->{layout}; |
2619 | my $layout = $self->{layout}; |
2588 | |
2620 | |
|
|
2621 | $layout->set_font ($self->{font}) if $self->{font}; |
|
|
2622 | $layout->set_foreground (@{$para->{fg}}); |
2589 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2623 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2590 | $layout->set_width ($self->{children}[0]{w} - $indent); |
2624 | $layout->set_width ($self->{children}[0]{w} - $para->{indent}); |
2591 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
2625 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
2592 | $layout->set_markup ($text); |
2626 | $layout->set_markup ($para->{markup}); |
|
|
2627 | |
|
|
2628 | $layout->set_shapes ( |
|
|
2629 | map |
|
|
2630 | +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), |
|
|
2631 | @{$para->{widget}} |
2593 | |
2632 | ); |
|
|
2633 | |
2594 | $layout->size |
2634 | $layout |
2595 | } |
2635 | } |
2596 | |
2636 | |
2597 | sub reflow { |
2637 | sub reflow { |
2598 | my ($self) = @_; |
2638 | my ($self) = @_; |
2599 | |
2639 | |
… | |
… | |
2608 | $self->{children}[1]->set_value ($offset); |
2648 | $self->{children}[1]->set_value ($offset); |
2609 | } |
2649 | } |
2610 | |
2650 | |
2611 | sub clear { |
2651 | sub clear { |
2612 | my ($self) = @_; |
2652 | my ($self) = @_; |
|
|
2653 | |
|
|
2654 | my (undef, undef, @other) = @{ $self->{children} }; |
|
|
2655 | $self->remove ($_) for @other; |
2613 | |
2656 | |
2614 | $self->{par} = []; |
2657 | $self->{par} = []; |
2615 | $self->{height} = 0; |
2658 | $self->{height} = 0; |
2616 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
2659 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
2617 | } |
2660 | } |
2618 | |
2661 | |
2619 | sub add_paragraph { |
2662 | sub add_paragraph { |
2620 | my ($self, $color, $text, $indent) = @_; |
2663 | my ($self, $color, $para, $indent) = @_; |
2621 | |
2664 | |
2622 | for my $line (split /\n/, $text) { |
2665 | my ($text, @w) = ref $para ? @$para : $para; |
2623 | my ($w, $h) = $self->text_size ($line); |
2666 | |
2624 | $self->{height} += $h; |
2667 | $para = { |
2625 | push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; |
2668 | w => 1e10, |
|
|
2669 | wrapped => 1, |
|
|
2670 | fg => $color, |
|
|
2671 | indent => $indent, |
|
|
2672 | markup => $text, |
|
|
2673 | widget => \@w, |
2626 | } |
2674 | }; |
2627 | |
2675 | |
2628 | $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); |
2676 | $self->add (@w) if @w; |
|
|
2677 | push @{$self->{par}}, $para; |
|
|
2678 | |
|
|
2679 | $self->{need_reflow}++; |
|
|
2680 | $self->update; |
|
|
2681 | } |
|
|
2682 | |
|
|
2683 | sub scroll_to_bottom { |
|
|
2684 | my ($self) = @_; |
|
|
2685 | |
|
|
2686 | $self->{scroll_to_bottom} = 1; |
|
|
2687 | $self->update; |
2629 | } |
2688 | } |
2630 | |
2689 | |
2631 | sub update { |
2690 | sub update { |
2632 | my ($self) = @_; |
2691 | my ($self) = @_; |
2633 | |
2692 | |
… | |
… | |
2641 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
2700 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
2642 | |
2701 | |
2643 | if (delete $self->{need_reflow}) { |
2702 | if (delete $self->{need_reflow}) { |
2644 | my $height = 0; |
2703 | my $height = 0; |
2645 | |
2704 | |
2646 | my $layout = $self->{layout}; |
|
|
2647 | |
|
|
2648 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2649 | |
|
|
2650 | for (@{$self->{par}}) { |
2705 | for my $para (@{$self->{par}}) { |
2651 | if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support |
2706 | if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { |
2652 | $layout->set_width ($W - $_->[3]); |
2707 | my $layout = $self->get_layout ($para); |
2653 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
|
|
2654 | $layout->set_markup ($_->[4]); |
|
|
2655 | my ($w, $h) = $layout->size; |
2708 | my ($w, $h) = $layout->size; |
2656 | $_->[0] = $w + $_->[3]; |
2709 | |
2657 | $_->[1] = $h; |
2710 | $para->{w} = $w + $para->{indent}; |
|
|
2711 | $para->{h} = $h; |
|
|
2712 | $para->{wrapped} = $layout->has_wrapped; |
2658 | } |
2713 | } |
2659 | |
2714 | |
2660 | $height += $_->[1]; |
2715 | $height += $para->{h}; |
2661 | } |
2716 | } |
2662 | |
2717 | |
2663 | $self->{height} = $height; |
2718 | $self->{height} = $height; |
2664 | |
2719 | |
2665 | $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); |
2720 | $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); |
2666 | |
2721 | |
2667 | delete $self->{texture}; |
2722 | delete $self->{texture}; |
|
|
2723 | } |
|
|
2724 | |
|
|
2725 | if (delete $self->{scroll_to_bottom}) { |
|
|
2726 | $self->{children}[1]->set_value (1e10); |
2668 | } |
2727 | } |
2669 | |
2728 | |
2670 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2729 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2671 | glClearColor 0, 0, 0, 0; |
2730 | glClearColor 0, 0, 0, 0; |
2672 | glClear GL_COLOR_BUFFER_BIT; |
2731 | glClear GL_COLOR_BUFFER_BIT; |
… | |
… | |
2676 | my $y0 = $top; |
2735 | my $y0 = $top; |
2677 | my $y1 = $top + $H; |
2736 | my $y1 = $top + $H; |
2678 | |
2737 | |
2679 | my $y = 0; |
2738 | my $y = 0; |
2680 | |
2739 | |
2681 | my $layout = $self->{layout}; |
|
|
2682 | |
|
|
2683 | $layout->set_font ($self->{font}) if $self->{font}; |
|
|
2684 | |
|
|
2685 | glEnable GL_BLEND; |
|
|
2686 | #TODO# not correct in windows where rgba is forced off |
|
|
2687 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2688 | |
|
|
2689 | for my $par (@{$self->{par}}) { |
2740 | for my $para (@{$self->{par}}) { |
2690 | my $h = $par->[1]; |
2741 | my $h = $para->{h}; |
2691 | |
2742 | |
2692 | if ($y0 < $y + $h && $y < $y1) { |
2743 | if ($y0 < $y + $h && $y < $y1) { |
2693 | $layout->set_foreground (@{ $par->[2] }); |
|
|
2694 | $layout->set_width ($W - $par->[3]); |
|
|
2695 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
|
|
2696 | $layout->set_markup ($par->[4]); |
|
|
2697 | |
2744 | |
2698 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
2745 | my $layout = $self->get_layout ($para); |
2699 | |
2746 | |
2700 | glRasterPos $par->[3], $y - $y0; |
2747 | $layout->render ($para->{indent}, $y - $y0); |
2701 | glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; |
2748 | |
|
|
2749 | if (my @w = @{ $para->{widget} }) { |
|
|
2750 | my @s = $layout->get_shapes; |
|
|
2751 | |
|
|
2752 | for (@w) { |
|
|
2753 | my ($dx, $dy) = splice @s, 0, 2, (); |
|
|
2754 | |
|
|
2755 | $_->{x} = $dx + $para->{indent}; |
|
|
2756 | $_->{y} = $dy + $y - $y0; |
|
|
2757 | |
|
|
2758 | $_->draw; |
|
|
2759 | } |
|
|
2760 | } |
2702 | } |
2761 | } |
2703 | |
2762 | |
2704 | $y += $h; |
2763 | $y += $h; |
2705 | } |
2764 | } |
2706 | |
|
|
2707 | glDisable GL_BLEND; |
|
|
2708 | }; |
2765 | }; |
2709 | }); |
2766 | }); |
|
|
2767 | } |
|
|
2768 | |
|
|
2769 | sub reconfigure { |
|
|
2770 | my ($self) = @_; |
|
|
2771 | |
|
|
2772 | $self->SUPER::reconfigure; |
|
|
2773 | |
|
|
2774 | $_->{w} = 1e10 for @{ $self->{par} }; |
|
|
2775 | $self->reflow; |
2710 | } |
2776 | } |
2711 | |
2777 | |
2712 | sub _draw { |
2778 | sub _draw { |
2713 | my ($self) = @_; |
2779 | my ($self) = @_; |
2714 | |
2780 | |
… | |
… | |
2717 | glColor 0, 0, 0, 1; |
2783 | glColor 0, 0, 0, 1; |
2718 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2784 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2719 | glDisable GL_TEXTURE_2D; |
2785 | glDisable GL_TEXTURE_2D; |
2720 | |
2786 | |
2721 | $self->{children}[1]->draw; |
2787 | $self->{children}[1]->draw; |
2722 | |
|
|
2723 | } |
2788 | } |
2724 | |
2789 | |
2725 | ############################################################################# |
2790 | ############################################################################# |
2726 | |
2791 | |
2727 | package CFClient::UI::Animator; |
2792 | package CFClient::UI::Animator; |
… | |
… | |
3000 | for my $item (@{ $self->{items} }) { |
3065 | for my $item (@{ $self->{items} }) { |
3001 | my ($widget, $cb, $tooltip) = @$item; |
3066 | my ($widget, $cb, $tooltip) = @$item; |
3002 | |
3067 | |
3003 | # handle various types of items, only text for now |
3068 | # handle various types of items, only text for now |
3004 | if (!ref $widget) { |
3069 | if (!ref $widget) { |
|
|
3070 | if ($widget =~ /\t/) { |
|
|
3071 | my ($left, $right) = split /\t/, $widget, 2; |
|
|
3072 | |
|
|
3073 | $widget = new CFClient::UI::HBox |
|
|
3074 | can_hover => 1, |
|
|
3075 | can_events => 1, |
|
|
3076 | tooltip => $tooltip, |
|
|
3077 | children => [ |
|
|
3078 | (new CFClient::UI::Label markup => $left, expand => 1), |
|
|
3079 | (new CFClient::UI::Label markup => $right, align => +1), |
|
|
3080 | ], |
|
|
3081 | ; |
|
|
3082 | |
|
|
3083 | } else { |
3005 | $widget = new CFClient::UI::Label |
3084 | $widget = new CFClient::UI::Label |
3006 | can_hover => 1, |
3085 | can_hover => 1, |
3007 | can_events => 1, |
3086 | can_events => 1, |
3008 | markup => $widget, |
3087 | markup => $widget, |
3009 | tooltip => $tooltip |
3088 | tooltip => $tooltip; |
|
|
3089 | } |
3010 | } |
3090 | } |
3011 | |
3091 | |
3012 | $self->{item}{$widget} = $item; |
3092 | $self->{item}{$widget} = $item; |
3013 | |
3093 | |
3014 | $self->{vbox}->add ($widget); |
3094 | $self->{vbox}->add ($widget); |
… | |
… | |
3081 | |
3161 | |
3082 | $self->SUPER::add (@widgets); |
3162 | $self->SUPER::add (@widgets); |
3083 | |
3163 | |
3084 | $self->{current} = $self->{children}[0] |
3164 | $self->{current} = $self->{children}[0] |
3085 | if @{ $self->{children} }; |
3165 | if @{ $self->{children} }; |
|
|
3166 | } |
|
|
3167 | |
|
|
3168 | sub get_current_page { |
|
|
3169 | my ($self) = @_; |
|
|
3170 | |
|
|
3171 | $self->{current} |
3086 | } |
3172 | } |
3087 | |
3173 | |
3088 | sub set_current_page { |
3174 | sub set_current_page { |
3089 | my ($self, $page_or_widget) = @_; |
3175 | my ($self, $page_or_widget) = @_; |
3090 | |
3176 | |
… | |
… | |
3156 | tooltip => $tooltip, |
3242 | tooltip => $tooltip, |
3157 | on_activate => sub { $self->set_current_page ($widget) }, |
3243 | on_activate => sub { $self->set_current_page ($widget) }, |
3158 | ); |
3244 | ); |
3159 | |
3245 | |
3160 | $self->{multiplexer}->add ($widget); |
3246 | $self->{multiplexer}->add ($widget); |
|
|
3247 | } |
|
|
3248 | |
|
|
3249 | sub get_current_page { |
|
|
3250 | my ($self) = @_; |
|
|
3251 | |
|
|
3252 | $self->{multiplexer}->get_current_page |
3161 | } |
3253 | } |
3162 | |
3254 | |
3163 | sub set_current_page { |
3255 | sub set_current_page { |
3164 | my ($self, $page) = @_; |
3256 | my ($self, $page) = @_; |
3165 | |
3257 | |
… | |
… | |
3343 | count => 1, |
3435 | count => 1, |
3344 | %arg, |
3436 | %arg, |
3345 | }; |
3437 | }; |
3346 | } |
3438 | } |
3347 | |
3439 | |
|
|
3440 | $ROOT->on_refresh (reorder => sub { |
3348 | $self->reorder; |
3441 | $self->reorder; |
|
|
3442 | }); |
3349 | } |
3443 | } |
3350 | |
3444 | |
3351 | sub reconfigure { |
3445 | sub reconfigure { |
3352 | my ($self) = @_; |
3446 | my ($self) = @_; |
3353 | |
3447 | |
… | |
… | |
3368 | |
3462 | |
3369 | ############################################################################# |
3463 | ############################################################################# |
3370 | |
3464 | |
3371 | package CFClient::UI::Inventory; |
3465 | package CFClient::UI::Inventory; |
3372 | |
3466 | |
3373 | our @ISA = CFClient::UI::ScrolledWindow::; |
3467 | our @ISA = CFClient::UI::Table::; |
3374 | |
3468 | |
3375 | sub new { |
3469 | sub new { |
3376 | my $class = shift; |
3470 | my $class = shift; |
3377 | |
3471 | |
3378 | my $self = $class->SUPER::new ( |
3472 | my $self = $class->SUPER::new ( |
3379 | child => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
3473 | col_expand => [0, 1, 0], |
3380 | @_, |
3474 | @_, |
3381 | ); |
3475 | ); |
3382 | |
3476 | |
3383 | $self |
3477 | $self |
3384 | } |
3478 | } |
3385 | |
3479 | |
3386 | sub set_items { |
3480 | sub set_items { |
3387 | my ($self, $items) = @_; |
3481 | my ($self, $items) = @_; |
3388 | |
3482 | |
3389 | $self->{child}->clear; |
3483 | $self->clear; |
3390 | return unless $items; |
3484 | return unless $items; |
3391 | |
3485 | |
3392 | my @items = sort { |
3486 | my @items = sort { |
3393 | ($a->{type} <=> $b->{type}) |
3487 | ($a->{type} <=> $b->{type}) |
3394 | or ($a->{name} cmp $b->{name}) |
3488 | or ($a->{name} cmp $b->{name}) |
3395 | } @$items; |
3489 | } values %$items; |
3396 | |
3490 | |
3397 | $self->{real_items} = \@items; |
3491 | $self->{real_items} = \@items; |
3398 | |
3492 | |
3399 | my $row = 0; |
3493 | my $row = 0; |
3400 | for my $item (@items) { |
3494 | for my $item (@items) { |
3401 | CFClient::Item::update_widgets $item; |
3495 | CFClient::Item::update_widgets $item; |
3402 | |
3496 | |
3403 | $self->{child}->add (0, $row, $item->{face_widget}); |
3497 | $self->add (0, $row, $item->{face_widget}); |
3404 | $self->{child}->add (1, $row, $item->{desc_widget}); |
3498 | $self->add (1, $row, $item->{desc_widget}); |
3405 | $self->{child}->add (2, $row, $item->{weight_widget}); |
3499 | $self->add (2, $row, $item->{weight_widget}); |
3406 | |
3500 | |
3407 | $row++; |
3501 | $row++; |
3408 | } |
|
|
3409 | } |
|
|
3410 | |
|
|
3411 | ############################################################################# |
|
|
3412 | |
|
|
3413 | package CFClient::UI::BindEditor; |
|
|
3414 | |
|
|
3415 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
3416 | |
|
|
3417 | sub new { |
|
|
3418 | my $class = shift; |
|
|
3419 | |
|
|
3420 | my $self = $class->SUPER::new (binding => [], commands => [], @_); |
|
|
3421 | |
|
|
3422 | $self->add (my $vb = new CFClient::UI::VBox); |
|
|
3423 | |
|
|
3424 | |
|
|
3425 | $vb->add ($self->{rec_btn} = new CFClient::UI::Button |
|
|
3426 | text => "start recording", |
|
|
3427 | tooltip => "Start/Stops recording of actions." |
|
|
3428 | ."All subsequent actions after the recording started will be captured." |
|
|
3429 | ."The actions are displayed after the record was stopped." |
|
|
3430 | ."To bind the action you have to click on the 'Bind' button", |
|
|
3431 | on_activate => sub { |
|
|
3432 | unless ($self->{recording}) { |
|
|
3433 | $self->start; |
|
|
3434 | } else { |
|
|
3435 | $self->stop; |
|
|
3436 | } |
|
|
3437 | }); |
|
|
3438 | |
|
|
3439 | $vb->add (new CFClient::UI::Label text => "Actions:"); |
|
|
3440 | $vb->add ($self->{cmdbox} = new CFClient::UI::VBox); |
|
|
3441 | |
|
|
3442 | $vb->add (new CFClient::UI::Label text => "Bound to: "); |
|
|
3443 | $vb->add (my $hb = new CFClient::UI::HBox); |
|
|
3444 | $hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1); |
|
|
3445 | $hb->add (new CFClient::UI::Button |
|
|
3446 | text => "bind", |
|
|
3447 | tooltip => "This opens a query where you have to press the key combination to bind the recorded actions", |
|
|
3448 | on_activate => sub { |
|
|
3449 | $self->ask_for_bind; |
|
|
3450 | }); |
|
|
3451 | |
|
|
3452 | $vb->add (my $hb = new CFClient::UI::HBox); |
|
|
3453 | $hb->add (new CFClient::UI::Button |
|
|
3454 | text => "ok", |
|
|
3455 | expand => 1, |
|
|
3456 | tooltip => "This closes the binding editor and saves the binding", |
|
|
3457 | on_activate => sub { |
|
|
3458 | $self->hide; |
|
|
3459 | $self->commit; |
|
|
3460 | }); |
|
|
3461 | |
|
|
3462 | $hb->add (new CFClient::UI::Button |
|
|
3463 | text => "cancel", |
|
|
3464 | expand => 1, |
|
|
3465 | tooltip => "This closes the binding editor without saving", |
|
|
3466 | on_activate => sub { |
|
|
3467 | $self->hide; |
|
|
3468 | $self->{binding_cancel}->() |
|
|
3469 | if $self->{binding_cancel}; |
|
|
3470 | }); |
|
|
3471 | |
|
|
3472 | $self->update_binding_widgets; |
|
|
3473 | |
|
|
3474 | $self |
|
|
3475 | } |
|
|
3476 | |
|
|
3477 | sub cfg_bind { |
|
|
3478 | my ($self, $mod, $sym, $cmds) = @_; |
|
|
3479 | $::CFG->{profile}{default}{bindings}{$mod}{$sym} = $cmds; |
|
|
3480 | ::update_bindings (); |
|
|
3481 | } |
|
|
3482 | |
|
|
3483 | sub cfg_unbind { |
|
|
3484 | my ($self, $mod, $sym, $cmds) = @_; |
|
|
3485 | delete $::CFG->{profile}{default}{bindings}{$mod}{$sym}; |
|
|
3486 | ::update_bindings (); |
|
|
3487 | } |
|
|
3488 | |
|
|
3489 | sub commit { |
|
|
3490 | my ($self) = @_; |
|
|
3491 | my ($mod, $sym, $cmds) = $self->get_binding; |
|
|
3492 | if ($sym != 0 && @$cmds > 0) { |
|
|
3493 | $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym) |
|
|
3494 | ."'. Don't forget 'Save Config'!"); |
|
|
3495 | $self->{binding_change}->($mod, $sym, $cmds) |
|
|
3496 | if $self->{binding_change}; |
|
|
3497 | } else { |
|
|
3498 | $::STATUSBOX->add ("No action bound, no key or action specified!"); |
|
|
3499 | $self->{binding_cancel}->() |
|
|
3500 | if $self->{binding_cancel}; |
|
|
3501 | } |
|
|
3502 | } |
|
|
3503 | |
|
|
3504 | sub start { |
|
|
3505 | my ($self) = @_; |
|
|
3506 | |
|
|
3507 | $self->{rec_btn}->set_text ("stop recording"); |
|
|
3508 | $self->{recording} = 1; |
|
|
3509 | $self->clear_command_list; |
|
|
3510 | $::CONN->start_record if $::CONN; |
|
|
3511 | } |
|
|
3512 | |
|
|
3513 | sub stop { |
|
|
3514 | my ($self) = @_; |
|
|
3515 | |
|
|
3516 | $self->{rec_btn}->set_text ("start recording"); |
|
|
3517 | $self->{recording} = 0; |
|
|
3518 | |
|
|
3519 | my $rec; |
|
|
3520 | $rec = $::CONN->stop_record if $::CONN; |
|
|
3521 | return unless ref $rec eq 'ARRAY'; |
|
|
3522 | $self->set_command_list ($rec); |
|
|
3523 | } |
|
|
3524 | |
|
|
3525 | |
|
|
3526 | sub ask_for_bind_and_commit { |
|
|
3527 | my ($self) = @_; |
|
|
3528 | $self->ask_for_bind (1); |
|
|
3529 | } |
|
|
3530 | |
|
|
3531 | sub ask_for_bind { |
|
|
3532 | my ($self, $commit, $end_cb) = @_; |
|
|
3533 | |
|
|
3534 | CFClient::Binder::open_binding_dialog (sub { |
|
|
3535 | my ($mod, $sym) = @_; |
|
|
3536 | $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? |
|
|
3537 | $self->update_binding_widgets; |
|
|
3538 | $self->commit if $commit; |
|
|
3539 | $end_cb->() if $end_cb; |
|
|
3540 | }); |
|
|
3541 | } |
|
|
3542 | |
|
|
3543 | # $mod and $sym are the modifiers and key symbol |
|
|
3544 | # $cmds is a array ref of strings (the commands) |
|
|
3545 | # $cb is the callback that is executed on OK |
|
|
3546 | # $ccb is the callback that is executed on CANCEL and |
|
|
3547 | # when the binding was unsuccessful on OK |
|
|
3548 | sub set_binding { |
|
|
3549 | my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_; |
|
|
3550 | |
|
|
3551 | $self->clear_command_list; |
|
|
3552 | $self->{recording} = 0; |
|
|
3553 | $self->{rec_btn}->set_text ("start recording"); |
|
|
3554 | |
|
|
3555 | $self->{binding} = [$mod, $sym]; |
|
|
3556 | $self->{commands} = $cmds; |
|
|
3557 | |
|
|
3558 | $self->{binding_change} = $cb; |
|
|
3559 | $self->{binding_cancel} = $ccb; |
|
|
3560 | |
|
|
3561 | $self->update_binding_widgets; |
|
|
3562 | } |
|
|
3563 | |
|
|
3564 | # this is a shortcut method that asks for a binding |
|
|
3565 | # and then just binds it. |
|
|
3566 | sub do_quick_binding { |
|
|
3567 | my ($self, $cmds, $end_cb) = @_; |
|
|
3568 | $self->set_binding (undef, undef, $cmds, sub { $self->cfg_bind (@_) }); |
|
|
3569 | $self->ask_for_bind (1, $end_cb); |
|
|
3570 | } |
|
|
3571 | |
|
|
3572 | sub update_binding_widgets { |
|
|
3573 | my ($self) = @_; |
|
|
3574 | my ($mod, $sym, $cmds) = $self->get_binding; |
|
|
3575 | $self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym)); |
|
|
3576 | $self->set_command_list ($cmds); |
|
|
3577 | } |
|
|
3578 | |
|
|
3579 | sub get_binding { |
|
|
3580 | my ($self) = @_; |
|
|
3581 | return ( |
|
|
3582 | $self->{binding}->[0], |
|
|
3583 | $self->{binding}->[1], |
|
|
3584 | [ grep { defined $_ } @{$self->{commands}} ] |
|
|
3585 | ); |
|
|
3586 | } |
|
|
3587 | |
|
|
3588 | sub clear_command_list { |
|
|
3589 | my ($self) = @_; |
|
|
3590 | $self->{cmdbox}->clear (); |
|
|
3591 | } |
|
|
3592 | |
|
|
3593 | sub set_command_list { |
|
|
3594 | my ($self, $cmds) = @_; |
|
|
3595 | |
|
|
3596 | $self->{cmdbox}->clear (); |
|
|
3597 | $self->{commands} = $cmds; |
|
|
3598 | |
|
|
3599 | my $idx = 0; |
|
|
3600 | |
|
|
3601 | for (@$cmds) { |
|
|
3602 | $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox); |
|
|
3603 | |
|
|
3604 | my $i = $idx; |
|
|
3605 | $hb->add (new CFClient::UI::Label text => $_); |
|
|
3606 | $hb->add (new CFClient::UI::Button |
|
|
3607 | text => "delete", |
|
|
3608 | tooltip => "Deletes the action from the record", |
|
|
3609 | on_activate => sub { |
|
|
3610 | $self->{cmdbox}->remove ($hb); |
|
|
3611 | $cmds->[$i] = undef; |
|
|
3612 | }); |
|
|
3613 | |
|
|
3614 | |
|
|
3615 | $idx++ |
|
|
3616 | } |
3502 | } |
3617 | } |
3503 | } |
3618 | |
3504 | |
3619 | ############################################################################# |
3505 | ############################################################################# |
3620 | |
3506 | |
… | |
… | |
3674 | } elsif ($ev->{button} == 2) { |
3560 | } elsif ($ev->{button} == 2) { |
3675 | $::CONN->user_send ("invoke $spell->{name}"); |
3561 | $::CONN->user_send ("invoke $spell->{name}"); |
3676 | } elsif ($ev->{button} == 3) { |
3562 | } elsif ($ev->{button} == 3) { |
3677 | (new CFClient::UI::Menu |
3563 | (new CFClient::UI::Menu |
3678 | items => [ |
3564 | items => [ |
3679 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
3565 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
3680 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
3566 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
3681 | ], |
3567 | ], |
3682 | )->popup ($ev); |
3568 | )->popup ($ev); |
3683 | } else { |
3569 | } else { |
3684 | return 0; |
3570 | return 0; |
… | |
… | |
3930 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3816 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3931 | |
3817 | |
3932 | $w = 0 if $w < 0; |
3818 | $w = 0 if $w < 0; |
3933 | $h = 0 if $h < 0; |
3819 | $h = 0 if $h < 0; |
3934 | |
3820 | |
|
|
3821 | $w = max $widget->{min_w}, $w; |
|
|
3822 | $h = max $widget->{min_h}, $h; |
|
|
3823 | |
|
|
3824 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
|
|
3825 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
|
|
3826 | |
3935 | $w = int $w + 0.5; |
3827 | $w = int $w + 0.5; |
3936 | $h = int $h + 0.5; |
3828 | $h = int $h + 0.5; |
3937 | |
3829 | |
3938 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3830 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3939 | $widget->{old_w} = $widget->{w}; |
3831 | $widget->{old_w} = $widget->{w}; |