… | |
… | |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | |
12 | |
13 | our $ROOT; |
13 | our $ROOT; |
14 | our $TOOLTIP; |
14 | our $TOOLTIP; |
15 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
|
|
16 | |
|
|
17 | our %WIDGET; # all widgets, weak-referenced |
16 | |
18 | |
17 | sub check_tooltip { |
19 | sub check_tooltip { |
18 | if (!$GRAB) { |
20 | if (!$GRAB) { |
19 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
21 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
20 | if (length $widget->{tooltip}) { |
22 | if (length $widget->{tooltip}) { |
… | |
… | |
29 | $TOOLTIP->set_tooltip_from ($widget); |
31 | $TOOLTIP->set_tooltip_from ($widget); |
30 | $TOOLTIP->show; |
32 | $TOOLTIP->show; |
31 | |
33 | |
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
34 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
33 | |
35 | |
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
|
|
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
36 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) |
36 | } |
37 | if $x + $TOOLTIP->{w} > $::WIDTH; |
37 | |
38 | |
38 | $TOOLTIP->move ($x, $y); |
39 | $TOOLTIP->move ($x, $y); |
39 | $TOOLTIP->check_size; |
40 | $TOOLTIP->check_size; |
40 | $TOOLTIP->update; |
41 | $TOOLTIP->update; |
41 | } |
42 | } |
… | |
… | |
132 | |
133 | |
133 | for (@$vals) { |
134 | for (@$vals) { |
134 | my $i = int $_ + $rem; |
135 | my $i = int $_ + $rem; |
135 | $rem += $_ - $i; |
136 | $rem += $_ - $i; |
136 | $_ = $i; |
137 | $_ = $i; |
|
|
138 | } |
|
|
139 | } |
|
|
140 | |
|
|
141 | # call when resolution changes etc. |
|
|
142 | sub rescale_widgets { |
|
|
143 | my ($sx, $sy) = @_; |
|
|
144 | |
|
|
145 | for my $widget (values %WIDGET) { |
|
|
146 | if ($widget->{toplevel}) { |
|
|
147 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
|
|
148 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
|
|
149 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
|
|
150 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
|
|
151 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
|
|
152 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
|
|
153 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
|
|
154 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
|
|
155 | } |
|
|
156 | |
|
|
157 | $widget->reconfigure; |
137 | } |
158 | } |
138 | } |
159 | } |
139 | |
160 | |
140 | ############################################################################# |
161 | ############################################################################# |
141 | |
162 | |
… | |
… | |
160 | if (/^connect_(.*)$/) { |
181 | if (/^connect_(.*)$/) { |
161 | $self->connect ($1 => delete $self->{$_}); |
182 | $self->connect ($1 => delete $self->{$_}); |
162 | } |
183 | } |
163 | } |
184 | } |
164 | |
185 | |
|
|
186 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
187 | |
165 | $self |
188 | $self |
166 | } |
189 | } |
167 | |
190 | |
168 | sub destroy { |
191 | sub destroy { |
169 | my ($self) = @_; |
192 | my ($self) = @_; |
… | |
… | |
242 | |
265 | |
243 | sub size_allocate { |
266 | sub size_allocate { |
244 | # nothing to be done |
267 | # nothing to be done |
245 | } |
268 | } |
246 | |
269 | |
247 | sub children { |
|
|
248 | } |
|
|
249 | |
|
|
250 | # call when resolution changes etc. |
|
|
251 | sub reconfigure { |
270 | sub reconfigure { |
252 | my ($self) = @_; |
271 | my ($self) = @_; |
253 | |
|
|
254 | $_->reconfigure |
|
|
255 | for $self->children; |
|
|
256 | |
272 | |
257 | $self->check_size (1); |
273 | $self->check_size (1); |
258 | $self->update; |
274 | $self->update; |
259 | } |
275 | } |
260 | |
276 | |
|
|
277 | sub children { |
|
|
278 | } |
|
|
279 | |
261 | sub set_max_size { |
280 | sub set_max_size { |
262 | my ($self, $w, $h) = @_; |
281 | my ($self, $w, $h) = @_; |
263 | |
282 | |
264 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
283 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
265 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
284 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub set_tooltip { |
|
|
288 | my ($self, $tooltip) = @_; |
|
|
289 | |
|
|
290 | $self->{tooltip} = $tooltip; |
|
|
291 | |
|
|
292 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
|
|
293 | delete $CFClient::UI::TOOLTIP->{owner}; |
|
|
294 | CFClient::UI::check_tooltip; |
|
|
295 | } |
266 | } |
296 | } |
267 | |
297 | |
268 | # translate global coordinates to local coordinate system |
298 | # translate global coordinates to local coordinate system |
269 | sub coord2local { |
299 | sub coord2local { |
270 | my ($self, $x, $y) = @_; |
300 | my ($self, $x, $y) = @_; |
… | |
… | |
356 | glVertex $self->{w}, 0; |
386 | glVertex $self->{w}, 0; |
357 | glVertex $self->{w}, $self->{h}; |
387 | glVertex $self->{w}, $self->{h}; |
358 | glVertex 0 , $self->{h}; |
388 | glVertex 0 , $self->{h}; |
359 | glEnd; |
389 | glEnd; |
360 | glPopMatrix; |
390 | glPopMatrix; |
361 | CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
391 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
362 | } |
392 | } |
363 | } |
393 | } |
364 | |
394 | |
365 | sub _draw { |
395 | sub _draw { |
366 | my ($self) = @_; |
396 | my ($self) = @_; |
… | |
… | |
417 | } |
447 | } |
418 | |
448 | |
419 | sub DESTROY { |
449 | sub DESTROY { |
420 | my ($self) = @_; |
450 | my ($self) = @_; |
421 | |
451 | |
|
|
452 | delete $WIDGET{$self+0}; |
422 | #$self->deactivate; |
453 | #$self->deactivate; |
423 | } |
454 | } |
424 | |
455 | |
425 | ############################################################################# |
456 | ############################################################################# |
426 | |
457 | |
… | |
… | |
435 | my $class = shift; |
466 | my $class = shift; |
436 | |
467 | |
437 | # range [value, low, high, page] |
468 | # range [value, low, high, page] |
438 | |
469 | |
439 | $class->SUPER::new ( |
470 | $class->SUPER::new ( |
440 | bg => [0, 0, 0, 0.2], |
471 | #bg => [0, 0, 0, 0.2], |
441 | active_bg => [1, 1, 1, 0.5], |
472 | #active_bg => [1, 1, 1, 0.5], |
442 | @_ |
473 | @_ |
443 | ) |
474 | ) |
444 | } |
475 | } |
445 | |
476 | |
446 | sub _draw { |
477 | sub _draw { |
447 | my ($self) = @_; |
478 | my ($self) = @_; |
448 | |
479 | |
|
|
480 | my $color = $FOCUS == $self && $self->{active_bg} |
|
|
481 | ? $self->{active_bg} |
|
|
482 | : $self->{bg}; |
|
|
483 | |
|
|
484 | if ($color && (@$color < 4 || $color->[3])) { |
449 | my ($w, $h) = @$self{qw(w h)}; |
485 | my ($w, $h) = @$self{qw(w h)}; |
450 | |
486 | |
451 | glEnable GL_BLEND; |
487 | glEnable GL_BLEND; |
452 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
488 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
453 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
489 | glColor @$color; |
454 | |
490 | |
455 | glBegin GL_QUADS; |
491 | glBegin GL_QUADS; |
456 | glVertex 0 , 0; |
492 | glVertex 0 , 0; |
457 | glVertex 0 , $h; |
493 | glVertex 0 , $h; |
458 | glVertex $w, $h; |
494 | glVertex $w, $h; |
459 | glVertex $w, 0; |
495 | glVertex $w, 0; |
460 | glEnd; |
496 | glEnd; |
461 | |
497 | |
462 | glDisable GL_BLEND; |
498 | glDisable GL_BLEND; |
|
|
499 | } |
463 | } |
500 | } |
464 | |
501 | |
465 | ############################################################################# |
502 | ############################################################################# |
466 | |
503 | |
467 | package CFClient::UI::Empty; |
504 | package CFClient::UI::Empty; |
… | |
… | |
840 | |
877 | |
841 | my $self = $class->SUPER::new ( |
878 | my $self = $class->SUPER::new ( |
842 | bg => [1, 1, 1, 1], |
879 | bg => [1, 1, 1, 1], |
843 | border_bg => [1, 1, 1, 1], |
880 | border_bg => [1, 1, 1, 1], |
844 | border => 0.6, |
881 | border => 0.6, |
|
|
882 | toplevel => 1, |
845 | can_events => 1, |
883 | can_events => 1, |
846 | @_ |
884 | @_ |
847 | ); |
885 | ); |
848 | |
886 | |
849 | $self->{title} &&= new CFClient::UI::Label |
887 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
874 | my ($self, $w, $h) = @_; |
912 | my ($self, $w, $h) = @_; |
875 | |
913 | |
876 | $h -= List::Util::max 0, $self->border * 2; |
914 | $h -= List::Util::max 0, $self->border * 2; |
877 | $w -= List::Util::max 0, $self->border * 2; |
915 | $w -= List::Util::max 0, $self->border * 2; |
878 | |
916 | |
879 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
917 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
880 | if $self->{title}; |
918 | if $self->{title}; |
881 | |
919 | |
882 | $self->child->configure ($self->border, $self->border, $w, $h); |
920 | $self->child->configure ($self->border, $self->border, $w, $h); |
883 | } |
921 | } |
884 | |
922 | |
… | |
… | |
1245 | |
1283 | |
1246 | ############################################################################# |
1284 | ############################################################################# |
1247 | |
1285 | |
1248 | package CFClient::UI::Label; |
1286 | package CFClient::UI::Label; |
1249 | |
1287 | |
1250 | our @ISA = CFClient::UI::Base::; |
1288 | our @ISA = CFClient::UI::DrawBG::; |
1251 | |
1289 | |
1252 | use CFClient::OpenGL; |
1290 | use CFClient::OpenGL; |
1253 | |
1291 | |
1254 | sub new { |
1292 | sub new { |
1255 | my ($class, %arg) = @_; |
1293 | my ($class, %arg) = @_; |
1256 | |
1294 | |
1257 | my $self = $class->SUPER::new ( |
1295 | my $self = $class->SUPER::new ( |
1258 | fg => [1, 1, 1], |
1296 | fg => [1, 1, 1], |
|
|
1297 | #bg => none |
|
|
1298 | #active_bg => none |
1259 | #font => default_font |
1299 | #font => default_font |
1260 | #text => initial text |
1300 | #text => initial text |
1261 | #markup => initial narkup |
1301 | #markup => initial narkup |
1262 | layout => (new CFClient::Layout), |
1302 | layout => (new CFClient::Layout), |
1263 | fontsize => 1, |
1303 | fontsize => 1, |
… | |
… | |
1281 | } |
1321 | } |
1282 | |
1322 | |
1283 | $self |
1323 | $self |
1284 | } |
1324 | } |
1285 | |
1325 | |
1286 | sub escape { |
1326 | sub escape($) { |
1287 | local $_ = $_[1]; |
1327 | local $_ = $_[0]; |
1288 | |
1328 | |
1289 | s/&/&/g; |
1329 | s/&/&/g; |
1290 | s/>/>/g; |
1330 | s/>/>/g; |
1291 | s/</</g; |
1331 | s/</</g; |
1292 | |
1332 | |
1293 | $_[1] |
1333 | $_ |
1294 | } |
1334 | } |
1295 | |
1335 | |
1296 | sub update { |
1336 | sub update { |
1297 | my ($self) = @_; |
1337 | my ($self) = @_; |
1298 | |
1338 | |
… | |
… | |
1369 | $self->check_size; |
1409 | $self->check_size; |
1370 | } |
1410 | } |
1371 | |
1411 | |
1372 | sub _draw { |
1412 | sub _draw { |
1373 | my ($self) = @_; |
1413 | my ($self) = @_; |
|
|
1414 | |
|
|
1415 | $self->SUPER::_draw; # draw background, if applicable |
1374 | |
1416 | |
1375 | my $tex = $self->{texture} ||= do { |
1417 | my $tex = $self->{texture} ||= do { |
1376 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1418 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1377 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1419 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1378 | $self->{layout}->set_width ($self->{w}); |
1420 | $self->{layout}->set_width ($self->{w}); |
1379 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1421 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1380 | |
1422 | |
1381 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1423 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1382 | |
1424 | |
1383 | $self->{ox} = int $self->{align} < 0 ? $self->{padding} |
1425 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
1384 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1426 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1385 | : ($self->{w} - $tex->{w}) * 0.5; |
1427 | : ($self->{w} - $tex->{w}) * 0.5); |
1386 | |
1428 | |
1387 | $self->{oy} = int $self->{valign} < 0 ? $self->{padding} |
1429 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
1388 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1430 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1389 | : ($self->{h} - $tex->{h}) * 0.5; |
1431 | : ($self->{h} - $tex->{h}) * 0.5); |
1390 | |
1432 | |
1391 | $tex |
1433 | $tex |
1392 | }; |
1434 | }; |
1393 | |
1435 | |
1394 | glEnable GL_TEXTURE_2D; |
1436 | glEnable GL_TEXTURE_2D; |
… | |
… | |
1449 | sub set_text { |
1491 | sub set_text { |
1450 | my ($self, $text) = @_; |
1492 | my ($self, $text) = @_; |
1451 | |
1493 | |
1452 | $self->{cursor} = length $text; |
1494 | $self->{cursor} = length $text; |
1453 | $self->_set_text ($text); |
1495 | $self->_set_text ($text); |
|
|
1496 | $self->update; |
1454 | $self->check_size; |
1497 | $self->check_size; |
1455 | $self->update; |
|
|
1456 | } |
1498 | } |
1457 | |
1499 | |
1458 | sub get_text { |
1500 | sub get_text { |
1459 | $_[0]{text} |
1501 | $_[0]{text} |
1460 | } |
1502 | } |
… | |
… | |
1500 | substr $text, $self->{cursor}++, 0, chr $uni; |
1542 | substr $text, $self->{cursor}++, 0, chr $uni; |
1501 | } |
1543 | } |
1502 | |
1544 | |
1503 | $self->_set_text ($text); |
1545 | $self->_set_text ($text); |
1504 | $self->update; |
1546 | $self->update; |
|
|
1547 | $self->check_size; |
1505 | } |
1548 | } |
1506 | |
1549 | |
1507 | sub focus_in { |
1550 | sub focus_in { |
1508 | my ($self) = @_; |
1551 | my ($self) = @_; |
1509 | |
1552 | |
… | |
… | |
1638 | my $class = shift; |
1681 | my $class = shift; |
1639 | |
1682 | |
1640 | $class->SUPER::new ( |
1683 | $class->SUPER::new ( |
1641 | padding => 4, |
1684 | padding => 4, |
1642 | fg => [1, 1, 1], |
1685 | fg => [1, 1, 1], |
1643 | bg => [1, 1, 1, 0.2], |
|
|
1644 | active_fg => [0, 0, 1], |
1686 | active_fg => [0, 0, 1], |
1645 | can_hover => 1, |
1687 | can_hover => 1, |
1646 | align => 0, |
1688 | align => 0, |
1647 | valign => 0, |
1689 | valign => 0, |
1648 | can_events => 1, |
1690 | can_events => 1, |
… | |
… | |
1696 | |
1738 | |
1697 | $class->SUPER::new ( |
1739 | $class->SUPER::new ( |
1698 | padding => 2, |
1740 | padding => 2, |
1699 | fg => [1, 1, 1], |
1741 | fg => [1, 1, 1], |
1700 | active_fg => [1, 1, 0], |
1742 | active_fg => [1, 1, 0], |
|
|
1743 | bg => [0, 0, 0, 0.2], |
|
|
1744 | active_bg => [1, 1, 1, 0.5], |
1701 | state => 0, |
1745 | state => 0, |
1702 | can_hover => 1, |
1746 | can_hover => 1, |
1703 | @_ |
1747 | @_ |
1704 | ) |
1748 | ) |
1705 | } |
1749 | } |
… | |
… | |
1989 | qw(s1_slider.png s1_slider_bg.png); |
2033 | qw(s1_slider.png s1_slider_bg.png); |
1990 | |
2034 | |
1991 | sub new { |
2035 | sub new { |
1992 | my $class = shift; |
2036 | my $class = shift; |
1993 | |
2037 | |
1994 | # range [value, low, high, page] |
2038 | # range [value, low, high, page, unit] |
1995 | |
2039 | |
1996 | # TODO: 0-width page |
2040 | # TODO: 0-width page |
1997 | # TODO: req_w/h are wrong with vertical |
2041 | # TODO: req_w/h are wrong with vertical |
1998 | # TODO: calculations are off |
2042 | # TODO: calculations are off |
1999 | my $self = $class->SUPER::new ( |
2043 | my $self = $class->SUPER::new ( |
2000 | fg => [1, 1, 1], |
2044 | fg => [1, 1, 1], |
2001 | active_fg => [0, 0, 0], |
2045 | active_fg => [0, 0, 0], |
|
|
2046 | bg => [0, 0, 0, 0.2], |
|
|
2047 | active_bg => [1, 1, 1, 0.5], |
2002 | range => [0, 0, 100, 10], |
2048 | range => [0, 0, 100, 10], |
2003 | req_w => $::WIDTH / 80, |
2049 | req_w => $::WIDTH / 80, |
2004 | req_h => $::WIDTH / 80, |
2050 | req_h => $::WIDTH / 80, |
2005 | vertical => 0, |
2051 | vertical => 0, |
2006 | can_hover => 1, |
2052 | can_hover => 1, |
2007 | inner_pad => 5, |
2053 | inner_pad => .05, |
2008 | @_ |
2054 | @_ |
2009 | ); |
2055 | ); |
2010 | |
2056 | |
|
|
2057 | $self->set_value ($self->{range}[0]); |
|
|
2058 | $self->update; |
|
|
2059 | |
2011 | $self |
2060 | $self |
|
|
2061 | } |
|
|
2062 | |
|
|
2063 | sub set_value { |
|
|
2064 | my ($self, $value) = @_; |
|
|
2065 | |
|
|
2066 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
|
|
2067 | |
|
|
2068 | $hi = $lo + 1 if $hi <= $lo; |
|
|
2069 | |
|
|
2070 | $value = $lo if $value < $lo; |
|
|
2071 | $value = $hi if $value > $hi; |
|
|
2072 | |
|
|
2073 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
|
|
2074 | if $unit; |
|
|
2075 | |
|
|
2076 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2077 | |
|
|
2078 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
|
|
2079 | |
|
|
2080 | if ($value != $old_value) { |
|
|
2081 | $self->emit (changed => $value); |
|
|
2082 | $self->update; |
|
|
2083 | } |
2012 | } |
2084 | } |
2013 | |
2085 | |
2014 | sub size_request { |
2086 | sub size_request { |
2015 | my ($self) = @_; |
2087 | my ($self) = @_; |
2016 | |
2088 | |
… | |
… | |
2029 | |
2101 | |
2030 | sub mouse_motion { |
2102 | sub mouse_motion { |
2031 | my ($self, $ev, $x, $y) = @_; |
2103 | my ($self, $ev, $x, $y) = @_; |
2032 | |
2104 | |
2033 | if ($GRAB == $self) { |
2105 | if ($GRAB == $self) { |
|
|
2106 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
2107 | |
|
|
2108 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2109 | |
|
|
2110 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
|
|
2111 | |
|
|
2112 | $self->set_value ($x * ($hi - $lo) + $lo); |
|
|
2113 | } |
|
|
2114 | } |
|
|
2115 | |
|
|
2116 | sub update { |
|
|
2117 | my ($self) = @_; |
|
|
2118 | |
|
|
2119 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
|
|
2120 | $self->set_value ($self->{range}[0]); |
|
|
2121 | |
2034 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2122 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2035 | |
2123 | |
2036 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2124 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2037 | |
2125 | |
2038 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
2126 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2039 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2040 | |
2127 | |
2041 | $x -= $inner_pad_px; # substract the padding |
2128 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2042 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
2129 | $value = $self->{scale} * ($value - $lo); |
2043 | $x = $lo if $x < $lo; |
|
|
2044 | $x = $hi - $page if $x > $hi - $page; |
|
|
2045 | $self->{range}[0] = $x; |
|
|
2046 | |
2130 | |
2047 | $self->emit (changed => $x); |
2131 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
2048 | $self->update; |
2132 | |
|
|
2133 | $value = 0 if $value < 0; |
|
|
2134 | $page = 1 - $value if $value + $page > 1; |
|
|
2135 | |
|
|
2136 | $self->{knob_x} = $value; |
|
|
2137 | $self->{knob_w} = $page; |
2049 | } |
2138 | }); |
2050 | } |
|
|
2051 | |
2139 | |
2052 | # the inner_* stuff is for generating a padding for the slider handle, |
2140 | $self->SUPER::update; |
2053 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
2054 | # correct propably, but it does the job for now |
|
|
2055 | sub _calc_inner_pad_px { |
|
|
2056 | my ($self, $w) = @_; |
|
|
2057 | ($w / 100) * $self->{inner_pad} # % to pixels |
|
|
2058 | } |
2141 | } |
2059 | |
2142 | |
2060 | sub _draw { |
2143 | sub _draw { |
2061 | my ($self) = @_; |
2144 | my ($self) = @_; |
2062 | |
2145 | |
2063 | $self->SUPER::_draw (); |
2146 | $self->SUPER::_draw (); |
2064 | |
2147 | |
2065 | my ($w, $h) = @$self{qw(w h)}; |
2148 | glScale $self->{w}, $self->{h}; |
2066 | |
2149 | |
2067 | if ($self->{vertical}) { |
2150 | if ($self->{vertical}) { |
2068 | # draw a vertical slider like a rotated horizontal slider |
2151 | # draw a vertical slider like a rotated horizontal slider |
2069 | |
2152 | |
2070 | glRotate 90, 0, 0, 1; |
2153 | glRotate 90, 0, 0, 1; |
2071 | glTranslate 0, -$self->{w}, 0; |
2154 | glTranslate 0, 1, 0; |
2072 | |
|
|
2073 | ($w, $h) = ($h, $w); |
|
|
2074 | } |
2155 | } |
2075 | |
2156 | |
2076 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2157 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2077 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2158 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2078 | |
2159 | |
2079 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2080 | |
|
|
2081 | $hi = $value + 1 if $lo == $hi; |
|
|
2082 | |
|
|
2083 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
2084 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2085 | |
|
|
2086 | $page = int $page * $inner_w / ($hi - $lo); |
|
|
2087 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
|
|
2088 | |
|
|
2089 | $w -= $page; |
|
|
2090 | $page &= ~1; |
|
|
2091 | glTranslate $page * 0.5, 0, 0; |
|
|
2092 | $page ||= 2; |
|
|
2093 | |
|
|
2094 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
|
|
2095 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
|
|
2096 | |
|
|
2097 | glEnable GL_TEXTURE_2D; |
2160 | glEnable GL_TEXTURE_2D; |
2098 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2161 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2099 | |
2162 | |
2100 | # draw background |
2163 | # draw background |
2101 | $tex[1]->draw_quad_alpha (0, 0, $w, $h); |
2164 | $tex[1]->draw_quad_alpha (0, 0, 1, 1); |
2102 | |
2165 | |
2103 | # draw handle |
2166 | # draw handle |
2104 | $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); |
2167 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2105 | |
2168 | |
2106 | glDisable GL_TEXTURE_2D; |
2169 | glDisable GL_TEXTURE_2D; |
2107 | } |
2170 | } |
2108 | |
2171 | |
2109 | ############################################################################# |
2172 | ############################################################################# |
… | |
… | |
2466 | |
2529 | |
2467 | my $desc = $item->{nrof} < 2 |
2530 | my $desc = $item->{nrof} < 2 |
2468 | ? $item->{name} |
2531 | ? $item->{name} |
2469 | : "$item->{nrof} $item->{name_pl}"; |
2532 | : "$item->{nrof} $item->{name_pl}"; |
2470 | |
2533 | |
|
|
2534 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2535 | and $desc .= " (open)"; |
|
|
2536 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2537 | and $desc .= " (applied)"; |
|
|
2538 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2539 | and $desc .= " (unpaid)"; |
|
|
2540 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2541 | and $desc .= " (magic)"; |
|
|
2542 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2543 | and $desc .= " (cursed)"; |
|
|
2544 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2545 | and $desc .= " (damned)"; |
|
|
2546 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2547 | and $desc .= " *"; |
2471 | |
2548 | |
2472 | my $self = $class->SUPER::new ( |
2549 | my $self = $class->SUPER::new ( |
2473 | can_hover => 1, |
2550 | can_hover => 1, |
2474 | can_events => 1, |
2551 | can_events => 1, |
2475 | tooltip => (CFClient::UI::Label->escape ($desc) |
2552 | tooltip => ((CFClient::UI::Label::escape $desc) |
2476 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
2553 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
2477 | connect_button_down => sub { |
2554 | connect_button_down => sub { |
2478 | my ($self, $ev, $x, $y) = @_; |
2555 | my ($self, $ev, $x, $y) = @_; |
2479 | |
2556 | |
2480 | # todo: maybe put examine on 1? but should just be a tooltip :( |
2557 | # todo: maybe put examine on 1? but should just be a tooltip :( |
2481 | if ($ev->{button} == 1) { |
2558 | if ($ev->{button} == 1) { |
2482 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2559 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2483 | } elsif ($ev->{button} == 2) { |
2560 | } elsif ($ev->{button} == 2) { |
2484 | $::CONN->send ("apply $item->{tag}"); |
2561 | $::CONN->send ("apply $item->{tag}"); |
2485 | } elsif ($ev->{button} == 3) { |
2562 | } elsif ($ev->{button} == 3) { |
2486 | CFClient::UI::Menu->new ( |
2563 | my @menu_items = ( |
2487 | items => [ |
|
|
2488 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
2564 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2565 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2566 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2567 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
2489 | [ |
2568 | [ |
2490 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2569 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2491 | sub { $::CONN->send ("lock $item->{tag}") }, |
2570 | sub { $::CONN->send ("lock ". () ."$item->{tag}") }, |
2492 | ], |
|
|
2493 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2494 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2495 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2496 | ], |
2571 | ], |
2497 | )->popup ($ev); |
2572 | ); |
|
|
2573 | |
|
|
2574 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
2498 | } |
2575 | } |
2499 | |
2576 | |
2500 | 1 |
2577 | 1 |
2501 | }, |
2578 | }, |
2502 | %args |
2579 | %args |
… | |
… | |
2641 | |
2718 | |
2642 | package CFClient::UI::Statusbox; |
2719 | package CFClient::UI::Statusbox; |
2643 | |
2720 | |
2644 | our @ISA = CFClient::UI::VBox::; |
2721 | our @ISA = CFClient::UI::VBox::; |
2645 | |
2722 | |
|
|
2723 | sub new { |
|
|
2724 | my $class = shift; |
|
|
2725 | |
|
|
2726 | $class->SUPER::new ( |
|
|
2727 | fontsize => 0.8, |
|
|
2728 | @_, |
|
|
2729 | ) |
|
|
2730 | } |
|
|
2731 | |
2646 | sub reorder { |
2732 | sub reorder { |
2647 | my ($self) = @_; |
2733 | my ($self) = @_; |
2648 | my $NOW = time; |
2734 | my $NOW = time; |
2649 | |
2735 | |
2650 | while (my ($k, $v) = each %{ $self->{item} }) { |
2736 | while (my ($k, $v) = each %{ $self->{item} }) { |
… | |
… | |
2666 | # TODO: doesn't handle markup well (read as: at all) |
2752 | # TODO: doesn't handle markup well (read as: at all) |
2667 | my $short = $item->{count} > 1 |
2753 | my $short = $item->{count} > 1 |
2668 | ? "<b>$item->{count} ×</b> $item->{text}" |
2754 | ? "<b>$item->{count} ×</b> $item->{text}" |
2669 | : $item->{text}; |
2755 | : $item->{text}; |
2670 | |
2756 | |
|
|
2757 | my $fontsize = $item->{fontsize} || $self->{fontsize}; |
|
|
2758 | |
2671 | for ($short) { |
2759 | for ($short) { |
2672 | s/^\s+//; |
2760 | s/^\s+//; |
2673 | s/\012.*/…/s; |
2761 | s/\s+/ /g; |
2674 | my $len = int 40 / $item->{fontsize}; |
2762 | my $len = int 40 / $fontsize; |
2675 | substr $_, $len, length, "…" if $len < length; |
2763 | substr $_, $len, length, "…" if $len < length; |
2676 | } |
2764 | } |
2677 | |
2765 | |
2678 | new CFClient::UI::Label |
2766 | new CFClient::UI::Label |
2679 | markup => $short, |
2767 | markup => $short, |
2680 | tooltip => $item->{tooltip}, |
2768 | tooltip => $item->{tooltip}, |
2681 | tooltip_font => $::FONT_PROP, |
2769 | tooltip_font => $::FONT_PROP, |
2682 | tooltip_width => 0.67, |
2770 | tooltip_width => 0.67, |
2683 | fontsize => $item->{fontsize}, |
2771 | fontsize => $fontsize, |
2684 | color => $item->{color}, |
2772 | fg => $item->{fg}, |
2685 | can_events => 1, |
2773 | can_events => 1, |
2686 | can_hover => 1 |
2774 | can_hover => 1 |
2687 | }; |
2775 | }; |
2688 | } |
2776 | } |
2689 | |
2777 | |
… | |
… | |
2715 | $self->{item}{$group} = { |
2803 | $self->{item}{$group} = { |
2716 | id => ++$self->{id}, |
2804 | id => ++$self->{id}, |
2717 | text => $text, |
2805 | text => $text, |
2718 | timeout => $timeout, |
2806 | timeout => $timeout, |
2719 | tooltip => $text, |
2807 | tooltip => $text, |
2720 | fontsize => 0.8, |
|
|
2721 | color => [0.8, 0.8, 0.8, 0.8], |
2808 | fg => [0.8, 0.8, 0.8, 0.8], |
2722 | pri => 0, |
2809 | pri => 0, |
2723 | count => 1, |
2810 | count => 1, |
2724 | %arg, |
2811 | %arg, |
2725 | }; |
2812 | }; |
2726 | } |
2813 | } |
… | |
… | |
2765 | } |
2852 | } |
2766 | |
2853 | |
2767 | sub size_allocate { |
2854 | sub size_allocate { |
2768 | my ($self, $w, $h) = @_; |
2855 | my ($self, $w, $h) = @_; |
2769 | |
2856 | |
2770 | my $old_w = $self->{old_w}; |
2857 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2771 | my $old_h = $self->{old_h}; |
2858 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2772 | |
2859 | |
2773 | if ($old_w && $old_h) { |
2860 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2774 | for my $child ($self->children) { |
2861 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
2775 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2776 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2777 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2778 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2779 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2780 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2781 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2782 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2783 | } |
|
|
2784 | } |
|
|
2785 | |
2862 | |
2786 | for my $child ($self->children) { |
2863 | for my $child ($self->children) { |
2787 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2864 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2788 | |
2865 | |
|
|
2866 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
|
|
2867 | if exists $child->{req_x}; |
|
|
2868 | |
|
|
2869 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
|
|
2870 | if exists $child->{req_y}; |
|
|
2871 | |
2789 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2872 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
2790 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2873 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
2874 | |
2791 | $child->configure ($X, $Y, $W, $H); |
2875 | $child->configure ($X, $Y, $W, $H); |
2792 | } |
2876 | } |
2793 | |
|
|
2794 | $self->{old_w} = $w; |
|
|
2795 | $self->{old_h} = $h; |
|
|
2796 | } |
2877 | } |
2797 | |
2878 | |
2798 | sub coord2local { |
2879 | sub coord2local { |
2799 | my ($self, $x, $y) = @_; |
2880 | my ($self, $x, $y) = @_; |
2800 | |
2881 | |
… | |
… | |
2813 | $self->check_size; |
2894 | $self->check_size; |
2814 | $::WANT_REFRESH++; |
2895 | $::WANT_REFRESH++; |
2815 | } |
2896 | } |
2816 | |
2897 | |
2817 | sub add { |
2898 | sub add { |
2818 | my ($self, $child) = @_; |
2899 | my ($self, @children) = @_; |
2819 | |
2900 | |
|
|
2901 | for my $child (@children) { |
|
|
2902 | $child->{toplevel} = 1; |
|
|
2903 | |
2820 | # integerise window positions |
2904 | # integerise window positions |
2821 | $child->{x} = int $child->{x}; |
2905 | $child->{x} = int $child->{x}; |
2822 | $child->{y} = int $child->{y}; |
2906 | $child->{y} = int $child->{y}; |
|
|
2907 | } |
2823 | |
2908 | |
2824 | $self->SUPER::add ($child); |
2909 | $self->SUPER::add (@children); |
2825 | } |
2910 | } |
2826 | |
2911 | |
2827 | sub on_refresh { |
2912 | sub on_refresh { |
2828 | my ($self, $id, $cb) = @_; |
2913 | my ($self, $id, $cb) = @_; |
2829 | |
2914 | |