… | |
… | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $ROOT; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (exists $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
14 | |
41 | |
15 | # class methods for events |
42 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
43 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
44 | $FOCUS->key_down ($_[0]) if $FOCUS; |
18 | } |
45 | } |
… | |
… | |
28 | if (!$BUTTON_STATE) { |
55 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
56 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
57 | |
31 | $GRAB = $widget; |
58 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
59 | $GRAB->update if $GRAB; |
|
|
60 | |
|
|
61 | check_tooltip; |
33 | } |
62 | } |
34 | |
63 | |
35 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
65 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
… | |
… | |
49 | |
78 | |
50 | if (!$BUTTON_STATE) { |
79 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
80 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
81 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
82 | $GRAB->update if $GRAB; |
|
|
83 | |
|
|
84 | check_tooltip; |
54 | } |
85 | } |
55 | } |
86 | } |
56 | |
87 | |
57 | sub feed_sdl_motion_event { |
88 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
89 | my ($ev) = @_; |
… | |
… | |
63 | if ($widget != $HOVER) { |
94 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
95 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
96 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
97 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
99 | |
|
|
100 | check_tooltip; |
68 | } |
101 | } |
69 | |
102 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
71 | } |
104 | } |
72 | |
105 | |
… | |
… | |
96 | |
129 | |
97 | my $self = bless { |
130 | my $self = bless { |
98 | x => 0, |
131 | x => 0, |
99 | y => 0, |
132 | y => 0, |
100 | z => 0, |
133 | z => 0, |
|
|
134 | can_events => 1, |
101 | @_ |
135 | @_ |
102 | }, $class; |
136 | }, $class; |
103 | |
137 | |
104 | for (keys %$self) { |
138 | for (keys %$self) { |
105 | if (/^connect_(.*)$/) { |
139 | if (/^connect_(.*)$/) { |
… | |
… | |
175 | } |
209 | } |
176 | } |
210 | } |
177 | |
211 | |
178 | sub size_allocate { |
212 | sub size_allocate { |
179 | # nothing to be done |
213 | # nothing to be done |
|
|
214 | } |
|
|
215 | |
|
|
216 | sub set_max_size { |
|
|
217 | my ($self, $w, $h) = @_; |
|
|
218 | |
|
|
219 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
220 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
180 | } |
221 | } |
181 | |
222 | |
182 | # return top left coordinates |
223 | # return top left coordinates |
183 | sub _topleft { |
224 | sub _topleft { |
184 | my ($self, $x, $y) = @_; |
225 | my ($self, $x, $y) = @_; |
… | |
… | |
278 | } |
319 | } |
279 | |
320 | |
280 | sub find_widget { |
321 | sub find_widget { |
281 | my ($self, $x, $y) = @_; |
322 | my ($self, $x, $y) = @_; |
282 | |
323 | |
|
|
324 | return () unless $self->{can_events}; |
|
|
325 | |
283 | return $self |
326 | return $self |
284 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
327 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
285 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
328 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
286 | |
329 | |
287 | () |
330 | () |
… | |
… | |
379 | |
422 | |
380 | package CFClient::UI::Empty; |
423 | package CFClient::UI::Empty; |
381 | |
424 | |
382 | our @ISA = CFClient::UI::Base::; |
425 | our @ISA = CFClient::UI::Base::; |
383 | |
426 | |
|
|
427 | sub new { |
|
|
428 | my ($class, %arg) = @_; |
|
|
429 | $class->SUPER::new (can_events => 0, %arg); |
|
|
430 | } |
|
|
431 | |
384 | sub size_request { |
432 | sub size_request { |
385 | (0, 0) |
433 | (0, 0) |
386 | } |
434 | } |
387 | |
435 | |
388 | sub draw { } |
436 | sub draw { } |
… | |
… | |
396 | sub new { |
444 | sub new { |
397 | my ($class, %arg) = @_; |
445 | my ($class, %arg) = @_; |
398 | |
446 | |
399 | my $children = delete $arg{children} || []; |
447 | my $children = delete $arg{children} || []; |
400 | |
448 | |
401 | my $self = $class->SUPER::new (children => [], %arg); |
449 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
402 | $self->add ($_) for @$children; |
450 | $self->add ($_) for @$children; |
403 | |
451 | |
404 | $self |
452 | $self |
405 | } |
453 | } |
406 | |
454 | |
… | |
… | |
425 | delete $child->{parent}; |
473 | delete $child->{parent}; |
426 | |
474 | |
427 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
475 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
428 | |
476 | |
429 | $self->check_size; |
477 | $self->check_size; |
|
|
478 | $self->update; |
430 | } |
479 | } |
431 | |
480 | |
432 | sub find_widget { |
481 | sub find_widget { |
433 | my ($self, $x, $y) = @_; |
482 | my ($self, $x, $y) = @_; |
434 | |
483 | |
… | |
… | |
660 | |
709 | |
661 | my $self = $class->SUPER::new ( |
710 | my $self = $class->SUPER::new ( |
662 | bg => [1, 1, 1, 1], |
711 | bg => [1, 1, 1, 1], |
663 | border_bg => [1, 1, 1, 1], |
712 | border_bg => [1, 1, 1, 1], |
664 | border => 0.8, |
713 | border => 0.8, |
|
|
714 | can_events => 1, |
665 | @_ |
715 | @_ |
666 | ); |
716 | ); |
667 | |
717 | |
668 | $self->{title} &&= new CFClient::UI::Label |
718 | $self->{title} &&= new CFClient::UI::Label |
669 | align => 0, |
719 | align => 0, |
… | |
… | |
1056 | sub new { |
1106 | sub new { |
1057 | my ($class, %arg) = @_; |
1107 | my ($class, %arg) = @_; |
1058 | |
1108 | |
1059 | my $self = $class->SUPER::new ( |
1109 | my $self = $class->SUPER::new ( |
1060 | fg => [1, 1, 1], |
1110 | fg => [1, 1, 1], |
|
|
1111 | #font => default_font |
1061 | fontsize => 1, |
1112 | fontsize => 1, |
1062 | text => "", |
1113 | text => "", |
1063 | align => -1, |
1114 | align => -1, |
1064 | valign => -1, |
1115 | valign => -1, |
1065 | padding => 2, |
1116 | padding => 2, |
1066 | layout => new CFClient::Layout, |
1117 | layout => new CFClient::Layout, |
|
|
1118 | can_events => 0, |
1067 | %arg |
1119 | %arg |
1068 | ); |
1120 | ); |
1069 | |
1121 | |
1070 | if (exists $self->{template}) { |
1122 | if (exists $self->{template}) { |
1071 | my $layout = new CFClient::Layout; |
1123 | my $layout = new CFClient::Layout; |
… | |
… | |
1110 | } |
1162 | } |
1111 | |
1163 | |
1112 | sub size_request { |
1164 | sub size_request { |
1113 | my ($self) = @_; |
1165 | my ($self) = @_; |
1114 | |
1166 | |
1115 | $self->{layout}->set_width; |
1167 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1168 | $self->{layout}->set_width ($self->{max_w} || -1); |
1116 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1169 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1117 | |
1170 | |
1118 | my ($w, $h) = $self->{layout}->size; |
1171 | my ($w, $h) = $self->{layout}->size; |
1119 | |
1172 | |
1120 | if (exists $self->{template}) { |
1173 | if (exists $self->{template}) { |
|
|
1174 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1121 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1175 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1122 | |
1176 | |
1123 | my ($w2, $h2) = $self->{template}->size; |
1177 | my ($w2, $h2) = $self->{template}->size; |
1124 | |
1178 | |
1125 | $w = List::Util::max $w, $w2; |
1179 | $w = List::Util::max $w, $w2; |
… | |
… | |
1140 | |
1194 | |
1141 | sub set_fontsize { |
1195 | sub set_fontsize { |
1142 | my ($self, $fontsize) = @_; |
1196 | my ($self, $fontsize) = @_; |
1143 | |
1197 | |
1144 | $self->{fontsize} = $fontsize; |
1198 | $self->{fontsize} = $fontsize; |
|
|
1199 | delete $self->{texture}; |
1145 | $self->check_size; |
1200 | $self->check_size; |
|
|
1201 | $self->update; |
1146 | } |
1202 | } |
1147 | |
1203 | |
1148 | sub _draw { |
1204 | sub _draw { |
1149 | my ($self) = @_; |
1205 | my ($self) = @_; |
1150 | |
1206 | |
1151 | my $tex = $self->{texture} ||= do { |
1207 | my $tex = $self->{texture} ||= do { |
|
|
1208 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1152 | $self->{layout}->set_width ($self->{w}); |
1209 | $self->{layout}->set_width ($self->{w}); |
1153 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1210 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1154 | new_from_layout CFClient::Texture $self->{layout} |
1211 | new_from_layout CFClient::Texture $self->{layout} |
1155 | }; |
1212 | }; |
1156 | |
1213 | |
… | |
… | |
1196 | active_bg => [1, 1, 1, 0.5], |
1253 | active_bg => [1, 1, 1, 0.5], |
1197 | active_fg => [0, 0, 0], |
1254 | active_fg => [0, 0, 0], |
1198 | can_hover => 1, |
1255 | can_hover => 1, |
1199 | can_focus => 1, |
1256 | can_focus => 1, |
1200 | valign => 0, |
1257 | valign => 0, |
|
|
1258 | can_events => 1, |
1201 | @_ |
1259 | @_ |
1202 | ) |
1260 | ) |
1203 | } |
1261 | } |
1204 | |
1262 | |
1205 | sub _set_text { |
1263 | sub _set_text { |
… | |
… | |
1390 | bg => [1, 1, 1, 0.2], |
1448 | bg => [1, 1, 1, 0.2], |
1391 | active_fg => [0, 0, 1], |
1449 | active_fg => [0, 0, 1], |
1392 | can_hover => 1, |
1450 | can_hover => 1, |
1393 | align => 0, |
1451 | align => 0, |
1394 | valign => 0, |
1452 | valign => 0, |
|
|
1453 | can_events => 1, |
1395 | @_ |
1454 | @_ |
1396 | ) |
1455 | ) |
1397 | } |
1456 | } |
1398 | |
1457 | |
1399 | sub button_up { |
1458 | sub button_up { |
… | |
… | |
1504 | our %loaded_images; |
1563 | our %loaded_images; |
1505 | |
1564 | |
1506 | sub new { |
1565 | sub new { |
1507 | my $class = shift; |
1566 | my $class = shift; |
1508 | |
1567 | |
1509 | my $self = $class->SUPER::new (@_); |
1568 | my $self = $class->SUPER::new (can_events => 0, @_); |
1510 | |
1569 | |
1511 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
1570 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
1512 | |
1571 | |
1513 | $loaded_images{$self->{image}} ||= |
1572 | $loaded_images{$self->{image}} ||= |
1514 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
1573 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
… | |
… | |
1673 | package CFClient::UI::Gauge; |
1732 | package CFClient::UI::Gauge; |
1674 | |
1733 | |
1675 | our @ISA = CFClient::UI::VBox::; |
1734 | our @ISA = CFClient::UI::VBox::; |
1676 | |
1735 | |
1677 | sub new { |
1736 | sub new { |
1678 | my ($class, %arg) = shift; |
1737 | my ($class, %arg) = @_; |
1679 | |
1738 | |
1680 | my $self = $class->SUPER::new ( |
1739 | my $self = $class->SUPER::new ( |
1681 | @_, |
1740 | tooltip => $arg{type}, |
|
|
1741 | %arg, |
1682 | ); |
1742 | ); |
1683 | |
1743 | |
1684 | $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
1744 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); |
1685 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
1745 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); |
1686 | $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
1746 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); |
1687 | |
1747 | |
1688 | $self |
1748 | $self |
1689 | } |
1749 | } |
1690 | |
1750 | |
1691 | sub set_fontsize { |
1751 | sub set_fontsize { |
… | |
… | |
1858 | sub new { |
1918 | sub new { |
1859 | my $class = shift; |
1919 | my $class = shift; |
1860 | |
1920 | |
1861 | my $self = $class->SUPER::new ( |
1921 | my $self = $class->SUPER::new ( |
1862 | fontsize => 1, |
1922 | fontsize => 1, |
|
|
1923 | #font => default_font |
1863 | @_, |
1924 | @_, |
1864 | |
1925 | |
1865 | layout => (new CFClient::Layout), |
1926 | layout => (new CFClient::Layout), |
1866 | par => [], |
1927 | par => [], |
1867 | height => 0, |
1928 | height => 0, |
… | |
… | |
1907 | sub size_allocate { |
1968 | sub size_allocate { |
1908 | my ($self, $w, $h) = @_; |
1969 | my ($self, $w, $h) = @_; |
1909 | |
1970 | |
1910 | $self->SUPER::size_allocate ($w, $h); |
1971 | $self->SUPER::size_allocate ($w, $h); |
1911 | |
1972 | |
|
|
1973 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1912 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1974 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1913 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1975 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1914 | |
1976 | |
1915 | $self->reflow; |
1977 | $self->reflow; |
1916 | } |
1978 | } |
… | |
… | |
1968 | my $y1 = $top + $self->{h}; |
2030 | my $y1 = $top + $self->{h}; |
1969 | |
2031 | |
1970 | my $y = 0; |
2032 | my $y = 0; |
1971 | |
2033 | |
1972 | my $layout = $self->{layout}; |
2034 | my $layout = $self->{layout}; |
|
|
2035 | |
|
|
2036 | $layout->set_font ($self->{font}) if $self->{font}; |
1973 | |
2037 | |
1974 | for my $par (@{$self->{par}}) { |
2038 | for my $par (@{$self->{par}}) { |
1975 | my $h = $par->[0]; |
2039 | my $h = $par->[0]; |
1976 | |
2040 | |
1977 | if ($y0 < $y + $h && $y < $y1) { |
2041 | if ($y0 < $y + $h && $y < $y1) { |
… | |
… | |
2057 | my $class = shift; |
2121 | my $class = shift; |
2058 | |
2122 | |
2059 | my $self = $class->SUPER::new ( |
2123 | my $self = $class->SUPER::new ( |
2060 | state => 0, |
2124 | state => 0, |
2061 | connect_activate => \&toggle_flopper, |
2125 | connect_activate => \&toggle_flopper, |
|
|
2126 | can_events => 1, |
2062 | @_ |
2127 | @_ |
2063 | ); |
2128 | ); |
2064 | |
2129 | |
2065 | if ($self->{state}) { |
2130 | if ($self->{state}) { |
2066 | $self->{state} = 0; |
2131 | $self->{state} = 0; |
… | |
… | |
2086 | $self->emit (changed => $self->{state}); |
2151 | $self->emit (changed => $self->{state}); |
2087 | } |
2152 | } |
2088 | |
2153 | |
2089 | ############################################################################# |
2154 | ############################################################################# |
2090 | |
2155 | |
|
|
2156 | package CFClient::UI::Tooltip; |
|
|
2157 | |
|
|
2158 | our @ISA = CFClient::UI::Bin::; |
|
|
2159 | |
|
|
2160 | use CFClient::OpenGL; |
|
|
2161 | |
|
|
2162 | sub new { |
|
|
2163 | my $class = shift; |
|
|
2164 | |
|
|
2165 | $class->SUPER::new ( |
|
|
2166 | @_, |
|
|
2167 | can_events => 0, |
|
|
2168 | ) |
|
|
2169 | } |
|
|
2170 | |
|
|
2171 | sub set_text { |
|
|
2172 | my ($self, $text) = @_; |
|
|
2173 | |
|
|
2174 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2175 | $self->{label}->set_text ($text); |
|
|
2176 | $self->add ($self->{label}); |
|
|
2177 | } |
|
|
2178 | |
|
|
2179 | sub size_request { |
|
|
2180 | my ($self) = @_; |
|
|
2181 | |
|
|
2182 | $self->child->set_max_size ($::WIDTH * 0.2); |
|
|
2183 | |
|
|
2184 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2185 | |
|
|
2186 | ($w + 4, $h + 4) |
|
|
2187 | } |
|
|
2188 | |
|
|
2189 | sub _draw { |
|
|
2190 | my ($self) = @_; |
|
|
2191 | |
|
|
2192 | glPushMatrix; |
|
|
2193 | glTranslate 0.375, 0.375; |
|
|
2194 | |
|
|
2195 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2196 | |
|
|
2197 | glColor 1, 0.8, 0.4; |
|
|
2198 | glBegin GL_QUADS; |
|
|
2199 | glVertex 0 , 0; |
|
|
2200 | glVertex 0 , $h; |
|
|
2201 | glVertex $w, $h; |
|
|
2202 | glVertex $w, 0; |
|
|
2203 | glEnd; |
|
|
2204 | |
|
|
2205 | glColor 0, 0, 0; |
|
|
2206 | glBegin GL_LINE_LOOP; |
|
|
2207 | glVertex 0 , 0; |
|
|
2208 | glVertex 0 , $h; |
|
|
2209 | glVertex $w, $h; |
|
|
2210 | glVertex $w, 0; |
|
|
2211 | glEnd; |
|
|
2212 | |
|
|
2213 | glPopMatrix; |
|
|
2214 | |
|
|
2215 | glTranslate 2, 2; |
|
|
2216 | $self->SUPER::_draw; |
|
|
2217 | } |
|
|
2218 | |
|
|
2219 | ############################################################################# |
|
|
2220 | |
2091 | package CFClient::UI::Root; |
2221 | package CFClient::UI::Root; |
2092 | |
2222 | |
2093 | our @ISA = CFClient::UI::Container::; |
2223 | our @ISA = CFClient::UI::Container::; |
2094 | |
2224 | |
2095 | use CFClient::OpenGL; |
2225 | use CFClient::OpenGL; |
… | |
… | |
2170 | ############################################################################# |
2300 | ############################################################################# |
2171 | |
2301 | |
2172 | package CFClient::UI; |
2302 | package CFClient::UI; |
2173 | |
2303 | |
2174 | $ROOT = new CFClient::UI::Root; |
2304 | $ROOT = new CFClient::UI::Root; |
|
|
2305 | $TOOLTIP = new CFClient::UI::Tooltip; |
2175 | |
2306 | |
2176 | 1 |
2307 | 1 |
2177 | |
2308 | |