… | |
… | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Scalar::Util (); |
6 | use Scalar::Util (); |
7 | use List::Util (); |
7 | use List::Util (); |
|
|
8 | use Event; |
8 | |
9 | |
9 | use CFClient; |
10 | use CFClient; |
10 | use CFClient::Texture; |
11 | use CFClient::Texture; |
11 | |
12 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
… | |
… | |
15 | our $ROOT; |
16 | our $ROOT; |
16 | our $TOOLTIP; |
17 | our $TOOLTIP; |
17 | our $BUTTON_STATE; |
18 | our $BUTTON_STATE; |
18 | |
19 | |
19 | our %WIDGET; # all widgets, weak-referenced |
20 | our %WIDGET; # all widgets, weak-referenced |
|
|
21 | |
|
|
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
|
|
23 | if (!$GRAB) { |
|
|
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
25 | if (length $widget->{tooltip}) { |
|
|
26 | if ($TOOLTIP->{owner} != $widget) { |
|
|
27 | $TOOLTIP->hide; |
|
|
28 | |
|
|
29 | $TOOLTIP->{owner} = $widget; |
|
|
30 | |
|
|
31 | return if $ENV{CFPLUS_DEBUG} & 8; |
|
|
32 | |
|
|
33 | my $tip = $widget->{tooltip}; |
|
|
34 | |
|
|
35 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
36 | |
|
|
37 | $TOOLTIP->set_tooltip_from ($widget); |
|
|
38 | $TOOLTIP->show; |
|
|
39 | } |
|
|
40 | |
|
|
41 | return; |
|
|
42 | } |
|
|
43 | } |
|
|
44 | } |
|
|
45 | |
|
|
46 | $TOOLTIP->hide; |
|
|
47 | delete $TOOLTIP->{owner}; |
|
|
48 | }); |
20 | |
49 | |
21 | sub get_layout { |
50 | sub get_layout { |
22 | my $layout; |
51 | my $layout; |
23 | |
52 | |
24 | for (grep { $_->{name} } values %WIDGET) { |
53 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
39 | my ($layout) = @_; |
68 | my ($layout) = @_; |
40 | |
69 | |
41 | $LAYOUT = $layout; |
70 | $LAYOUT = $layout; |
42 | } |
71 | } |
43 | |
72 | |
44 | sub check_tooltip { |
|
|
45 | return if $ENV{CFPLUS_DEBUG} & 8; |
|
|
46 | |
|
|
47 | if (!$GRAB) { |
|
|
48 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
49 | if (length $widget->{tooltip}) { |
|
|
50 | if ($TOOLTIP->{owner} != $widget) { |
|
|
51 | $TOOLTIP->hide; |
|
|
52 | |
|
|
53 | $TOOLTIP->{owner} = $widget; |
|
|
54 | |
|
|
55 | my $tip = $widget->{tooltip}; |
|
|
56 | |
|
|
57 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
58 | |
|
|
59 | $TOOLTIP->set_tooltip_from ($widget); |
|
|
60 | $TOOLTIP->show; |
|
|
61 | } |
|
|
62 | |
|
|
63 | return; |
|
|
64 | } |
|
|
65 | } |
|
|
66 | } |
|
|
67 | |
|
|
68 | $TOOLTIP->hide; |
|
|
69 | delete $TOOLTIP->{owner}; |
|
|
70 | } |
|
|
71 | |
|
|
72 | # class methods for events |
73 | # class methods for events |
73 | sub feed_sdl_key_down_event { |
74 | sub feed_sdl_key_down_event { |
74 | $FOCUS->emit (key_down => $_[0]) |
75 | $FOCUS->emit (key_down => $_[0]) |
75 | if $FOCUS; |
76 | if $FOCUS; |
76 | } |
77 | } |
… | |
… | |
88 | my $widget = $ROOT->find_widget ($x, $y); |
89 | my $widget = $ROOT->find_widget ($x, $y); |
89 | |
90 | |
90 | $GRAB = $widget; |
91 | $GRAB = $widget; |
91 | $GRAB->update if $GRAB; |
92 | $GRAB->update if $GRAB; |
92 | |
93 | |
93 | check_tooltip; |
94 | $TOOLTIP_WATCHER->cb->(); |
94 | } |
95 | } |
95 | |
96 | |
96 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
97 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
97 | |
98 | |
98 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
99 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
… | |
… | |
113 | if (!$BUTTON_STATE) { |
114 | if (!$BUTTON_STATE) { |
114 | my $grab = $GRAB; undef $GRAB; |
115 | my $grab = $GRAB; undef $GRAB; |
115 | $grab->update if $grab; |
116 | $grab->update if $grab; |
116 | $GRAB->update if $GRAB; |
117 | $GRAB->update if $GRAB; |
117 | |
118 | |
118 | check_tooltip; |
119 | $TOOLTIP_WATCHER->cb->(); |
119 | } |
120 | } |
120 | } |
121 | } |
121 | |
122 | |
122 | sub feed_sdl_motion_event { |
123 | sub feed_sdl_motion_event { |
123 | my ($ev) = @_; |
124 | my ($ev) = @_; |
… | |
… | |
129 | my $hover = $HOVER; $HOVER = $widget; |
130 | my $hover = $HOVER; $HOVER = $widget; |
130 | |
131 | |
131 | $hover->update if $hover && $hover->{can_hover}; |
132 | $hover->update if $hover && $hover->{can_hover}; |
132 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
133 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
133 | |
134 | |
134 | check_tooltip; |
135 | $TOOLTIP_WATCHER->start; |
135 | } |
136 | } |
136 | |
137 | |
137 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
138 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
138 | if $HOVER; |
139 | if $HOVER; |
139 | } |
140 | } |
… | |
… | |
277 | delete $self->{visible}; |
278 | delete $self->{visible}; |
278 | |
279 | |
279 | undef $GRAB if $GRAB == $self; |
280 | undef $GRAB if $GRAB == $self; |
280 | undef $HOVER if $HOVER == $self; |
281 | undef $HOVER if $HOVER == $self; |
281 | |
282 | |
282 | CFClient::UI::check_tooltip |
283 | $CFClient::UI::TOOLTIP_WATCHER->cb->() |
283 | if $TOOLTIP->{owner} == $self; |
284 | if $TOOLTIP->{owner} == $self; |
284 | |
285 | |
285 | $self->focus_out; |
286 | $self->focus_out; |
286 | |
287 | |
287 | $self->emit (visibility_change => 0); |
288 | $self->emit (visibility_change => 0); |
… | |
… | |
399 | |
400 | |
400 | $self->{tooltip} = $tooltip; |
401 | $self->{tooltip} = $tooltip; |
401 | |
402 | |
402 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
403 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
403 | delete $CFClient::UI::TOOLTIP->{owner}; |
404 | delete $CFClient::UI::TOOLTIP->{owner}; |
404 | CFClient::UI::check_tooltip; |
405 | $CFClient::UI::TOOLTIP_WATCHER->cb->(); |
405 | } |
406 | } |
406 | } |
407 | } |
407 | |
408 | |
408 | # translate global coordinates to local coordinate system |
409 | # translate global coordinates to local coordinate system |
409 | sub coord2local { |
410 | sub coord2local { |
… | |
… | |
1542 | |
1543 | |
1543 | sub realloc { |
1544 | sub realloc { |
1544 | my ($self) = @_; |
1545 | my ($self) = @_; |
1545 | |
1546 | |
1546 | delete $self->{ox}; |
1547 | delete $self->{ox}; |
1547 | delete $self->{req_h}; |
|
|
1548 | $self->SUPER::realloc; |
1548 | $self->SUPER::realloc; |
1549 | } |
1549 | } |
1550 | |
1550 | |
1551 | sub set_text { |
1551 | sub set_text { |
1552 | my ($self, $text) = @_; |
1552 | my ($self, $text) = @_; |
… | |
… | |
1554 | return if $self->{text} eq "T$text"; |
1554 | return if $self->{text} eq "T$text"; |
1555 | $self->{text} = "T$text"; |
1555 | $self->{text} = "T$text"; |
1556 | |
1556 | |
1557 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1557 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1558 | $self->{layout}->set_text ($text); |
1558 | $self->{layout}->set_text ($text); |
|
|
1559 | delete $self->{req_h}; |
1559 | |
1560 | |
1560 | $self->realloc; |
1561 | $self->realloc; |
1561 | $self->update; |
1562 | $self->update; |
1562 | } |
1563 | } |
1563 | |
1564 | |
… | |
… | |
1569 | |
1570 | |
1570 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1571 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1571 | |
1572 | |
1572 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1573 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1573 | $self->{layout}->set_markup ($markup); |
1574 | $self->{layout}->set_markup ($markup); |
|
|
1575 | delete $self->{req_h}; |
1574 | |
1576 | |
1575 | $self->realloc; |
1577 | $self->realloc; |
1576 | $self->update; |
1578 | $self->update; |
1577 | } |
1579 | } |
1578 | |
1580 | |
… | |
… | |
1649 | }; |
1651 | }; |
1650 | |
1652 | |
1651 | glEnable GL_TEXTURE_2D; |
1653 | glEnable GL_TEXTURE_2D; |
1652 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1654 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1653 | |
1655 | |
1654 | glColor_premultiply @{$self->{fg}} |
|
|
1655 | if $tex->{format} == GL_ALPHA; |
1656 | if ($tex->{format} == GL_ALPHA) { |
1656 | |
1657 | glColor @{$self->{fg}}; |
|
|
1658 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
|
|
1659 | } else { |
1657 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
1660 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
|
|
1661 | } |
1658 | |
1662 | |
1659 | glDisable GL_TEXTURE_2D; |
1663 | glDisable GL_TEXTURE_2D; |
1660 | } |
1664 | } |
1661 | |
1665 | |
1662 | ############################################################################# |
1666 | ############################################################################# |
… | |
… | |
1694 | $self->{last_activity} = $::NOW; |
1698 | $self->{last_activity} = $::NOW; |
1695 | $self->{text} = $text; |
1699 | $self->{text} = $text; |
1696 | |
1700 | |
1697 | $text =~ s/./*/g if $self->{hidden}; |
1701 | $text =~ s/./*/g if $self->{hidden}; |
1698 | $self->{layout}->set_text ("$text "); |
1702 | $self->{layout}->set_text ("$text "); |
|
|
1703 | delete $self->{req_h}; |
1699 | |
1704 | |
1700 | $self->_emit (changed => $self->{text}); |
1705 | $self->_emit (changed => $self->{text}); |
|
|
1706 | |
|
|
1707 | $self->realloc; |
1701 | $self->update; |
1708 | $self->update; |
1702 | } |
1709 | } |
1703 | |
1710 | |
1704 | sub set_text { |
1711 | sub set_text { |
1705 | my ($self, $text) = @_; |
1712 | my ($self, $text) = @_; |
1706 | |
1713 | |
1707 | $self->{cursor} = length $text; |
1714 | $self->{cursor} = length $text; |
1708 | $self->_set_text ($text); |
1715 | $self->_set_text ($text); |
1709 | |
|
|
1710 | $self->realloc; |
|
|
1711 | } |
1716 | } |
1712 | |
1717 | |
1713 | sub get_text { |
1718 | sub get_text { |
1714 | $_[0]{text} |
1719 | $_[0]{text} |
1715 | } |
1720 | } |