1 | package CFClient::UI; |
1 | package CFClient::UI; |
2 | |
2 | |
|
|
3 | use utf8; |
3 | use strict; |
4 | use strict; |
4 | |
5 | |
5 | use Scalar::Util (); |
6 | use Scalar::Util (); |
6 | use List::Util (); |
7 | use List::Util (); |
7 | |
8 | |
… | |
… | |
23 | |
24 | |
24 | my $tip = $widget->{tooltip}; |
25 | my $tip = $widget->{tooltip}; |
25 | |
26 | |
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
27 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
27 | |
28 | |
28 | $TOOLTIP->set_markup ($widget->{tooltip}); |
29 | $TOOLTIP->set_tooltip_from ($widget); |
29 | |
|
|
30 | $TOOLTIP->show; |
30 | $TOOLTIP->show; |
31 | |
31 | |
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
33 | |
33 | |
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
36 | } |
36 | } |
37 | |
37 | |
38 | $TOOLTIP->move ($x, $y); |
38 | $TOOLTIP->move ($x, $y); |
|
|
39 | $TOOLTIP->check_size; |
|
|
40 | $TOOLTIP->update; |
39 | } |
41 | } |
40 | |
42 | |
41 | return; |
43 | return; |
42 | } |
44 | } |
43 | } |
45 | } |
… | |
… | |
232 | $self->{y} = $y; |
234 | $self->{y} = $y; |
233 | $self->update; |
235 | $self->update; |
234 | } |
236 | } |
235 | |
237 | |
236 | if ($self->{w} != $w || $self->{h} != $h) { |
238 | if ($self->{w} != $w || $self->{h} != $h) { |
237 | $self->{w} = $w; |
239 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; |
238 | $self->{h} = $h; |
|
|
239 | |
|
|
240 | $self->size_allocate ($w, $h); |
|
|
241 | $self->update; |
|
|
242 | $self->emit (size_allocate => $w, $h); |
|
|
243 | } |
240 | } |
244 | } |
241 | } |
245 | |
242 | |
246 | sub size_allocate { |
243 | sub size_allocate { |
247 | # nothing to be done |
244 | # nothing to be done |
… | |
… | |
255 | my ($self) = @_; |
252 | my ($self) = @_; |
256 | |
253 | |
257 | $_->reconfigure |
254 | $_->reconfigure |
258 | for $self->children; |
255 | for $self->children; |
259 | |
256 | |
260 | $self->check_size; |
257 | $self->check_size (1); |
261 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
262 | $self->update; |
258 | $self->update; |
263 | } |
259 | } |
264 | |
260 | |
265 | sub set_max_size { |
261 | sub set_max_size { |
266 | my ($self, $w, $h) = @_; |
262 | my ($self, $w, $h) = @_; |
267 | |
263 | |
268 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
264 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
269 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
265 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
270 | } |
|
|
271 | |
|
|
272 | # return top left coordinates |
|
|
273 | sub _topleft { |
|
|
274 | my ($self, $x, $y) = @_; |
|
|
275 | |
|
|
276 | $self->{parent} |
|
|
277 | or Carp::confess "no parent widget in _topleft\n";#d# |
|
|
278 | |
|
|
279 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
|
|
280 | } |
266 | } |
281 | |
267 | |
282 | # translate global coordinates to local coordinate system |
268 | # translate global coordinates to local coordinate system |
283 | sub coord2local { |
269 | sub coord2local { |
284 | my ($self, $x, $y) = @_; |
270 | my ($self, $x, $y) = @_; |
285 | |
271 | |
286 | my ($X, $Y) = $self->_topleft; |
272 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
287 | ($x - $X, $y - $Y) |
|
|
288 | } |
273 | } |
289 | |
274 | |
290 | # translate local coordinates to global coordinate system |
275 | # translate local coordinates to global coordinate system |
291 | sub coord2global { |
276 | sub coord2global { |
292 | my ($self, $x, $y) = @_; |
277 | my ($self, $x, $y) = @_; |
293 | |
278 | |
294 | my ($X, $Y) = $self->_topleft; |
279 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
295 | ($x + $X, $y + $Y) |
|
|
296 | } |
280 | } |
297 | |
281 | |
298 | sub focus_in { |
282 | sub focus_in { |
299 | my ($self) = @_; |
283 | my ($self) = @_; |
300 | |
284 | |
… | |
… | |
398 | |
382 | |
399 | sub set_parent { |
383 | sub set_parent { |
400 | my ($self, $parent) = @_; |
384 | my ($self, $parent) = @_; |
401 | |
385 | |
402 | Scalar::Util::weaken ($self->{parent} = $parent); |
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
387 | |
|
|
388 | |
|
|
389 | # TODO: req_w _does_change after ->reconfigure |
|
|
390 | $self->check_size |
|
|
391 | unless exists $self->{req_w}; |
403 | } |
392 | } |
404 | |
393 | |
405 | sub check_size { |
394 | sub check_size { |
406 | my ($self) = @_; |
395 | my ($self, $forced) = @_; |
407 | |
396 | |
408 | $self->{parent} |
397 | $self->{force_alloc} = 1 if $forced; |
409 | or return 1; |
398 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
410 | |
|
|
411 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
412 | ? @$self{qw(user_w user_h)} |
|
|
413 | : $self->size_request; |
|
|
414 | |
|
|
415 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
416 | $self->{req_w} = $w; |
|
|
417 | $self->{req_h} = $h; |
|
|
418 | |
|
|
419 | $self->{parent}->check_size |
|
|
420 | or $self->size_allocate ( |
|
|
421 | (List::Util::max $self->{w}, $w), |
|
|
422 | (List::Util::max $self->{h}, $h), |
|
|
423 | ); |
|
|
424 | |
|
|
425 | 1 |
|
|
426 | } else { |
|
|
427 | 0 |
|
|
428 | } |
|
|
429 | } |
399 | } |
430 | |
400 | |
431 | sub update { |
401 | sub update { |
432 | my ($self) = @_; |
402 | my ($self) = @_; |
433 | |
403 | |
… | |
… | |
530 | |
500 | |
531 | $self |
501 | $self |
532 | } |
502 | } |
533 | |
503 | |
534 | sub add { |
504 | sub add { |
535 | my ($self, $child) = @_; |
505 | my ($self, @widgets) = @_; |
536 | |
506 | |
537 | $child->set_parent ($self); |
507 | $_->set_parent ($self) |
|
|
508 | for @widgets; |
538 | |
509 | |
539 | use sort 'stable'; |
510 | use sort 'stable'; |
540 | |
511 | |
541 | $self->{children} = [ |
512 | $self->{children} = [ |
542 | sort { $a->{z} <=> $b->{z} } |
513 | sort { $a->{z} <=> $b->{z} } |
543 | @{$self->{children}}, $child |
514 | @{$self->{children}}, @widgets |
544 | ]; |
515 | ]; |
545 | |
516 | |
546 | $child->check_size; |
517 | $self->check_size (1); |
547 | $self->update; |
518 | $self->update; |
548 | } |
519 | } |
549 | |
520 | |
550 | sub children { |
521 | sub children { |
551 | @{ $_[0]{children} } |
522 | @{ $_[0]{children} } |
… | |
… | |
658 | } |
629 | } |
659 | |
630 | |
660 | sub update { |
631 | sub update { |
661 | my ($self) = @_; |
632 | my ($self) = @_; |
662 | |
633 | |
663 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
634 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
664 | $self->SUPER::update; |
635 | $self->SUPER::update; |
665 | } |
636 | } |
666 | |
637 | |
667 | sub size_allocate { |
638 | sub size_allocate { |
668 | my ($self, $w, $h) = @_; |
639 | my ($self, $w, $h) = @_; |
… | |
… | |
699 | my ($w, $h) = ($self->w, $self->h); |
670 | my ($w, $h) = ($self->w, $self->h); |
700 | |
671 | |
701 | my $tex = $self->{texture} |
672 | my $tex = $self->{texture} |
702 | or return; |
673 | or return; |
703 | |
674 | |
704 | glEnable GL_BLEND; |
|
|
705 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
706 | glEnable GL_TEXTURE_2D; |
675 | glEnable GL_TEXTURE_2D; |
707 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
676 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
708 | glColor 0, 0, 0, 1; |
677 | glColor 0, 0, 0, 1; |
709 | |
678 | |
710 | $tex->draw_quad (0, 0, $w, $h); |
679 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
711 | |
680 | |
712 | glDisable GL_BLEND; |
|
|
713 | glDisable GL_TEXTURE_2D; |
681 | glDisable GL_TEXTURE_2D; |
714 | } |
682 | } |
715 | |
683 | |
716 | ############################################################################# |
684 | ############################################################################# |
717 | |
685 | |
… | |
… | |
741 | $self->{view_y} = int $y; |
709 | $self->{view_y} = int $y; |
742 | |
710 | |
743 | $self->update; |
711 | $self->update; |
744 | } |
712 | } |
745 | |
713 | |
746 | # hmm, this does not work for topleft of $self... but we should not aks for that |
714 | # hmm, this does not work for topleft of $self... but we should not ask for that |
747 | sub _topleft { |
715 | sub coord2local { |
748 | my ($self, $x, $y) = @_; |
716 | my ($self, $x, $y) = @_; |
749 | |
717 | |
750 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
718 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
719 | } |
|
|
720 | |
|
|
721 | sub coord2global { |
|
|
722 | my ($self, $x, $y) = @_; |
|
|
723 | |
|
|
724 | $x = List::Util::min $self->{w}, $x - $self->{view_x}; |
|
|
725 | $y = List::Util::min $self->{h}, $y - $self->{view_y}; |
|
|
726 | |
|
|
727 | $self->SUPER::coord2global ($x, $y) |
751 | } |
728 | } |
752 | |
729 | |
753 | sub find_widget { |
730 | sub find_widget { |
754 | my ($self, $x, $y) = @_; |
731 | my ($self, $x, $y) = @_; |
755 | |
732 | |
… | |
… | |
811 | |
788 | |
812 | our @ISA = CFClient::UI::Bin::; |
789 | our @ISA = CFClient::UI::Bin::; |
813 | |
790 | |
814 | use CFClient::OpenGL; |
791 | use CFClient::OpenGL; |
815 | |
792 | |
816 | sub new { |
|
|
817 | my $class = shift; |
|
|
818 | |
|
|
819 | my $self = $class->SUPER::new ( |
|
|
820 | bg => [1, 1, 1, 1], |
|
|
821 | border_bg => [1, 1, 1, 1], |
|
|
822 | border => 0.8, |
|
|
823 | @_ |
|
|
824 | ); |
|
|
825 | |
|
|
826 | $self |
|
|
827 | } |
|
|
828 | |
|
|
829 | sub _draw { |
|
|
830 | my ($self) = @_; |
|
|
831 | |
|
|
832 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
833 | |
|
|
834 | glEnable GL_BLEND; |
|
|
835 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
836 | glEnable GL_TEXTURE_2D; |
|
|
837 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
838 | |
|
|
839 | # glBegin GL_QUADS; |
|
|
840 | # glColor 0, 0, 0, 0; |
|
|
841 | # glVertex 0 , 0; |
|
|
842 | # glVertex 0 , $h; |
|
|
843 | # glVertex $w, $h; |
|
|
844 | # glVertex $w, 0; |
|
|
845 | # glEnd; |
|
|
846 | |
|
|
847 | |
|
|
848 | $self->child->draw; |
|
|
849 | glDisable GL_BLEND; |
|
|
850 | glDisable GL_TEXTURE_2D; |
|
|
851 | } |
|
|
852 | |
|
|
853 | ############################################################################# |
793 | ############################################################################# |
854 | |
794 | |
855 | package CFClient::UI::FancyFrame; |
795 | package CFClient::UI::FancyFrame; |
856 | |
796 | |
857 | our @ISA = CFClient::UI::Bin::; |
797 | our @ISA = CFClient::UI::Bin::; |
… | |
… | |
971 | my ($self) = @_; |
911 | my ($self) = @_; |
972 | |
912 | |
973 | my ($w, $h ) = ($self->{w}, $self->{h}); |
913 | my ($w, $h ) = ($self->{w}, $self->{h}); |
974 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
914 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
975 | |
915 | |
976 | glEnable GL_BLEND; |
|
|
977 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
978 | glEnable GL_TEXTURE_2D; |
916 | glEnable GL_TEXTURE_2D; |
979 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
917 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
980 | |
918 | |
981 | my $border = $self->border; |
919 | my $border = $self->border; |
982 | |
920 | |
983 | glColor @{ $self->{border_bg} }; |
921 | glColor @{ $self->{border_bg} }; |
984 | $tex[1]->draw_quad (0, 0, $w, $border); |
922 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
985 | $tex[3]->draw_quad (0, $border, $border, $ch); |
923 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
986 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
924 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
987 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
925 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
988 | |
926 | |
989 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
927 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
990 | my $bg = $tex[0]; |
928 | my $bg = $tex[0]; |
991 | |
929 | |
992 | # TODO: repeat texture not scale |
930 | # TODO: repeat texture not scale |
… | |
… | |
996 | glColor @{ $self->{bg} }; |
934 | glColor @{ $self->{bg} }; |
997 | |
935 | |
998 | $bg->{s} = $rep_x; |
936 | $bg->{s} = $rep_x; |
999 | $bg->{t} = $rep_y; |
937 | $bg->{t} = $rep_y; |
1000 | $bg->{wrap_mode} = 1; |
938 | $bg->{wrap_mode} = 1; |
1001 | $bg->draw_quad ($border, $border, $cw, $ch); |
939 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
940 | } |
1002 | |
941 | |
1003 | glDisable GL_TEXTURE_2D; |
942 | glDisable GL_TEXTURE_2D; |
1004 | glDisable GL_BLEND; |
|
|
1005 | } |
|
|
1006 | |
943 | |
1007 | $self->{title}->draw if $self->{title}; |
944 | $self->{title}->draw if $self->{title}; |
1008 | |
945 | |
1009 | $self->child->draw; |
946 | $self->child->draw; |
1010 | } |
947 | } |
… | |
… | |
1237 | } |
1174 | } |
1238 | |
1175 | |
1239 | sub size_allocate { |
1176 | sub size_allocate { |
1240 | my ($self, $w, $h) = @_; |
1177 | my ($self, $w, $h) = @_; |
1241 | |
1178 | |
|
|
1179 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
|
|
1180 | |
1242 | my $children = $self->{children}; |
1181 | my $children = $self->{children}; |
1243 | |
1182 | |
1244 | my @h = map $_->{req_h}, @$children; |
1183 | my @h = map $_->{req_h}, @$children; |
1245 | |
1184 | |
1246 | my $req_h = List::Util::sum @h; |
1185 | my $req_h = List::Util::sum @h; |
… | |
… | |
1285 | my ($class, %arg) = @_; |
1224 | my ($class, %arg) = @_; |
1286 | |
1225 | |
1287 | my $self = $class->SUPER::new ( |
1226 | my $self = $class->SUPER::new ( |
1288 | fg => [1, 1, 1], |
1227 | fg => [1, 1, 1], |
1289 | #font => default_font |
1228 | #font => default_font |
|
|
1229 | #text => initial text |
|
|
1230 | #markup => initial narkup |
|
|
1231 | layout => (new CFClient::Layout), |
1290 | fontsize => 1, |
1232 | fontsize => 1, |
1291 | text => "", |
|
|
1292 | align => -1, |
1233 | align => -1, |
1293 | valign => -1, |
1234 | valign => -1, |
1294 | padding => 2, |
1235 | padding => 2, |
1295 | layout => new CFClient::Layout, |
|
|
1296 | can_events => 0, |
1236 | can_events => 0, |
1297 | %arg |
1237 | %arg |
1298 | ); |
1238 | ); |
1299 | |
1239 | |
1300 | if (exists $self->{template}) { |
1240 | if (exists $self->{template}) { |
1301 | my $layout = new CFClient::Layout; |
1241 | my $layout = new CFClient::Layout; |
1302 | $layout->set_text (delete $self->{template}); |
1242 | $layout->set_text (delete $self->{template}); |
1303 | $self->{template} = $layout; |
1243 | $self->{template} = $layout; |
1304 | } |
1244 | } |
1305 | |
1245 | |
1306 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1246 | if (exists $self->{markup}) { |
1307 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1247 | $self->set_markup (delete $self->{markup}); |
|
|
1248 | } else { |
|
|
1249 | $self->set_text (delete $self->{text}); |
|
|
1250 | } |
1308 | |
1251 | |
1309 | $self |
1252 | $self |
1310 | } |
1253 | } |
1311 | |
1254 | |
1312 | sub escape { |
1255 | sub escape { |
… | |
… | |
1330 | my ($self, $text) = @_; |
1273 | my ($self, $text) = @_; |
1331 | |
1274 | |
1332 | return if $self->{text} eq "T$text"; |
1275 | return if $self->{text} eq "T$text"; |
1333 | $self->{text} = "T$text"; |
1276 | $self->{text} = "T$text"; |
1334 | |
1277 | |
|
|
1278 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1335 | $self->{layout}->set_text ($text); |
1279 | $self->{layout}->set_text ($text); |
1336 | |
1280 | |
1337 | delete $self->{texture}; |
|
|
1338 | $self->update; |
1281 | $self->update; |
1339 | $self->check_size; |
1282 | $self->check_size; |
1340 | } |
1283 | } |
1341 | |
1284 | |
1342 | sub set_markup { |
1285 | sub set_markup { |
1343 | my ($self, $markup) = @_; |
1286 | my ($self, $markup) = @_; |
1344 | |
1287 | |
1345 | return if $self->{text} eq "M$markup"; |
1288 | return if $self->{text} eq "M$markup"; |
1346 | $self->{text} = "M$markup"; |
1289 | $self->{text} = "M$markup"; |
1347 | |
1290 | |
|
|
1291 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
|
|
1292 | |
|
|
1293 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1348 | $self->{layout}->set_markup ($markup); |
1294 | $self->{layout}->set_markup ($markup); |
1349 | |
1295 | |
1350 | delete $self->{texture}; |
|
|
1351 | $self->update; |
1296 | $self->update; |
1352 | $self->check_size; |
1297 | $self->check_size; |
1353 | } |
1298 | } |
1354 | |
1299 | |
1355 | sub size_request { |
1300 | sub size_request { |
… | |
… | |
1395 | |
1340 | |
1396 | sub _draw { |
1341 | sub _draw { |
1397 | my ($self) = @_; |
1342 | my ($self) = @_; |
1398 | |
1343 | |
1399 | my $tex = $self->{texture} ||= do { |
1344 | my $tex = $self->{texture} ||= do { |
|
|
1345 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1400 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1346 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1401 | $self->{layout}->set_width ($self->{w}); |
1347 | $self->{layout}->set_width ($self->{w}); |
1402 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1348 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
|
|
1349 | |
1403 | new_from_layout CFClient::Texture $self->{layout} |
1350 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
|
|
1351 | |
|
|
1352 | $self->{ox} = int $self->{align} < 0 ? $self->{padding} |
|
|
1353 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1354 | : ($self->{w} - $tex->{w}) * 0.5; |
|
|
1355 | |
|
|
1356 | $self->{oy} = int $self->{valign} < 0 ? $self->{padding} |
|
|
1357 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1358 | : ($self->{h} - $tex->{h}) * 0.5; |
|
|
1359 | |
|
|
1360 | $tex |
1404 | }; |
1361 | }; |
1405 | |
1362 | |
1406 | glEnable GL_BLEND; |
|
|
1407 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1408 | glEnable GL_TEXTURE_2D; |
1363 | glEnable GL_TEXTURE_2D; |
1409 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1364 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1410 | |
1365 | |
|
|
1366 | if ($tex->{format} == GL_ALPHA) { |
1411 | glColor @{$self->{fg}}; |
1367 | glColor @{$self->{fg}}; |
1412 | |
|
|
1413 | $self->{ox} = int ( |
|
|
1414 | $self->{align} < 0 ? $self->{padding} |
|
|
1415 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1416 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1417 | ); |
|
|
1418 | |
|
|
1419 | $self->{oy} = int ( |
|
|
1420 | $self->{valign} < 0 ? $self->{padding} |
|
|
1421 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1422 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1423 | ); |
|
|
1424 | |
|
|
1425 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1368 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
|
|
1369 | } else { |
|
|
1370 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
|
|
1371 | } |
1426 | |
1372 | |
1427 | glDisable GL_TEXTURE_2D; |
1373 | glDisable GL_TEXTURE_2D; |
1428 | glDisable GL_BLEND; |
|
|
1429 | } |
1374 | } |
1430 | |
1375 | |
1431 | ############################################################################# |
1376 | ############################################################################# |
1432 | |
1377 | |
1433 | package CFClient::UI::EntryBase; |
1378 | package CFClient::UI::EntryBase; |
… | |
… | |
1468 | $self->{layout}->set_text ("$text "); |
1413 | $self->{layout}->set_text ("$text "); |
1469 | |
1414 | |
1470 | $self->emit (changed => $self->{text}); |
1415 | $self->emit (changed => $self->{text}); |
1471 | } |
1416 | } |
1472 | |
1417 | |
|
|
1418 | sub set_text { |
|
|
1419 | my ($self, $text) = @_; |
|
|
1420 | |
|
|
1421 | $self->{cursor} = length $text; |
|
|
1422 | $self->_set_text ($text); |
|
|
1423 | $self->check_size; |
|
|
1424 | $self->update; |
|
|
1425 | } |
|
|
1426 | |
1473 | sub get_text { |
1427 | sub get_text { |
1474 | $_[0]{text} |
1428 | $_[0]{text} |
1475 | } |
1429 | } |
1476 | |
1430 | |
1477 | sub size_request { |
1431 | sub size_request { |
… | |
… | |
1484 | |
1438 | |
1485 | sub size_allocate { |
1439 | sub size_allocate { |
1486 | my ($self, $w, $h) = @_; |
1440 | my ($self, $w, $h) = @_; |
1487 | |
1441 | |
1488 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1442 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1489 | } |
|
|
1490 | |
|
|
1491 | sub set_text { |
|
|
1492 | my ($self, $text) = @_; |
|
|
1493 | |
|
|
1494 | $self->{cursor} = length $text; |
|
|
1495 | $self->_set_text ($text); |
|
|
1496 | $self->update; |
|
|
1497 | } |
1443 | } |
1498 | |
1444 | |
1499 | sub key_down { |
1445 | sub key_down { |
1500 | my ($self, $ev) = @_; |
1446 | my ($self, $ev) = @_; |
1501 | |
1447 | |
… | |
… | |
1689 | |
1635 | |
1690 | if ($GRAB == $self) { |
1636 | if ($GRAB == $self) { |
1691 | $self->{fg} = $self->{active_fg}; |
1637 | $self->{fg} = $self->{active_fg}; |
1692 | } |
1638 | } |
1693 | |
1639 | |
1694 | glEnable GL_BLEND; |
|
|
1695 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1696 | glEnable GL_TEXTURE_2D; |
1640 | glEnable GL_TEXTURE_2D; |
1697 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1641 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1698 | glColor 0, 0, 0, 1; |
1642 | glColor 0, 0, 0, 1; |
1699 | |
1643 | |
1700 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1644 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1701 | |
1645 | |
1702 | glDisable GL_TEXTURE_2D; |
1646 | glDisable GL_TEXTURE_2D; |
1703 | glDisable GL_BLEND; |
|
|
1704 | |
1647 | |
1705 | $self->SUPER::_draw; |
1648 | $self->SUPER::_draw; |
1706 | } |
1649 | } |
1707 | |
1650 | |
1708 | ############################################################################# |
1651 | ############################################################################# |
… | |
… | |
1755 | |
1698 | |
1756 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1699 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1757 | |
1700 | |
1758 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1701 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1759 | |
1702 | |
1760 | glEnable GL_BLEND; |
1703 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1704 | |
1761 | glEnable GL_TEXTURE_2D; |
1705 | glEnable GL_TEXTURE_2D; |
1762 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1763 | |
|
|
1764 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1765 | |
|
|
1766 | $tex->draw_quad (0, 0, $s, $s); |
1706 | $tex->draw_quad_alpha (0, 0, $s, $s); |
1767 | |
|
|
1768 | glDisable GL_TEXTURE_2D; |
1707 | glDisable GL_TEXTURE_2D; |
1769 | glDisable GL_BLEND; |
|
|
1770 | } |
1708 | } |
1771 | |
1709 | |
1772 | ############################################################################# |
1710 | ############################################################################# |
1773 | |
1711 | |
1774 | package CFClient::UI::Image; |
1712 | package CFClient::UI::Image; |
… | |
… | |
1817 | glTranslate 0, -$self->{w}, 0; |
1755 | glTranslate 0, -$self->{w}, 0; |
1818 | |
1756 | |
1819 | ($w, $h) = ($h, $w); |
1757 | ($w, $h) = ($h, $w); |
1820 | } |
1758 | } |
1821 | |
1759 | |
1822 | glEnable GL_BLEND; |
|
|
1823 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1824 | glEnable GL_TEXTURE_2D; |
1760 | glEnable GL_TEXTURE_2D; |
1825 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1761 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1826 | |
1762 | |
1827 | $tex->draw_quad (0, 0, $w, $h); |
1763 | $tex->draw_quad_alpha (0, 0, $w, $h); |
1828 | |
1764 | |
1829 | glDisable GL_BLEND; |
|
|
1830 | glDisable GL_TEXTURE_2D; |
1765 | glDisable GL_TEXTURE_2D; |
1831 | } |
1766 | } |
1832 | |
1767 | |
1833 | ############################################################################# |
1768 | ############################################################################# |
1834 | |
1769 | |
… | |
… | |
2126 | $page ||= 2; |
2061 | $page ||= 2; |
2127 | |
2062 | |
2128 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2063 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2129 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2064 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2130 | |
2065 | |
2131 | glEnable GL_BLEND; |
|
|
2132 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2133 | glEnable GL_TEXTURE_2D; |
2066 | glEnable GL_TEXTURE_2D; |
2134 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2067 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2135 | |
2068 | |
2136 | # draw background |
2069 | # draw background |
2137 | $tex[1]->draw_quad (0, 0, $w, $h); |
2070 | $tex[1]->draw_quad_alpha (0, 0, $w, $h); |
2138 | |
2071 | |
2139 | # draw handle |
2072 | # draw handle |
2140 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
2073 | $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); |
2141 | |
2074 | |
2142 | glDisable GL_BLEND; |
|
|
2143 | glDisable GL_TEXTURE_2D; |
2075 | glDisable GL_TEXTURE_2D; |
2144 | } |
2076 | } |
2145 | |
2077 | |
2146 | ############################################################################# |
2078 | ############################################################################# |
2147 | |
2079 | |
… | |
… | |
2158 | fontsize => 1, |
2090 | fontsize => 1, |
2159 | can_events => 0, |
2091 | can_events => 0, |
2160 | #font => default_font |
2092 | #font => default_font |
2161 | @_, |
2093 | @_, |
2162 | |
2094 | |
2163 | layout => (new CFClient::Layout), |
2095 | layout => (new CFClient::Layout 1), |
2164 | par => [], |
2096 | par => [], |
2165 | height => 0, |
2097 | height => 0, |
2166 | children => [ |
2098 | children => [ |
2167 | (new CFClient::UI::Empty expand => 1), |
2099 | (new CFClient::UI::Empty expand => 1), |
2168 | (new CFClient::UI::Slider vertical => 1), |
2100 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2186 | |
2118 | |
2187 | my $layout = $self->{layout}; |
2119 | my $layout = $self->{layout}; |
2188 | |
2120 | |
2189 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2121 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2190 | $layout->set_width ($self->{children}[0]{w}); |
2122 | $layout->set_width ($self->{children}[0]{w}); |
2191 | $layout->set_text ($text); |
2123 | $layout->set_markup ($text); |
2192 | |
2124 | |
2193 | ($layout->size)[1] |
2125 | ($layout->size)[1] |
2194 | } |
2126 | } |
2195 | |
2127 | |
2196 | sub reflow { |
2128 | sub reflow { |
… | |
… | |
2234 | |
2166 | |
2235 | return unless $self->{h} > 0; |
2167 | return unless $self->{h} > 0; |
2236 | |
2168 | |
2237 | delete $self->{texture}; |
2169 | delete $self->{texture}; |
2238 | |
2170 | |
2239 | $ROOT->on_refresh ($self, sub { |
2171 | $ROOT->on_post_alloc ($self, sub { |
2240 | if (delete $self->{need_reflow}) { |
2172 | if (delete $self->{need_reflow}) { |
2241 | my $height = 0; |
2173 | my $height = 0; |
2242 | |
2174 | |
2243 | $height += $_->[0] = $self->text_height ($_->[2]) |
2175 | $height += $_->[0] = $self->text_height ($_->[2]) |
2244 | for @{$self->{par}}; |
2176 | for @{$self->{par}}; |
… | |
… | |
2252 | |
2184 | |
2253 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2185 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2254 | glClearColor 0, 0, 0, 0; |
2186 | glClearColor 0, 0, 0, 0; |
2255 | glClear GL_COLOR_BUFFER_BIT; |
2187 | glClear GL_COLOR_BUFFER_BIT; |
2256 | |
2188 | |
2257 | glEnable GL_BLEND; |
|
|
2258 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2259 | glEnable GL_TEXTURE_2D; |
2189 | glEnable GL_TEXTURE_2D; |
2260 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2190 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2261 | |
2191 | |
2262 | my $top = int $self->{children}[1]{range}[0]; |
2192 | my $top = int $self->{children}[1]{range}[0]; |
2263 | |
2193 | |
… | |
… | |
2272 | |
2202 | |
2273 | for my $par (@{$self->{par}}) { |
2203 | for my $par (@{$self->{par}}) { |
2274 | my $h = $par->[0]; |
2204 | my $h = $par->[0]; |
2275 | |
2205 | |
2276 | if ($y0 < $y + $h && $y < $y1) { |
2206 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2207 | $layout->set_foreground (@{ $par->[1] }); |
2277 | $layout->set_text ($par->[2]); |
2208 | $layout->set_markup ($par->[2]); |
2278 | |
2209 | |
2279 | glColor @{ $par->[1] }; |
|
|
2280 | my ($W, $H) = $layout->size; |
2210 | my ($W, $H) = $layout->size; |
2281 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
2211 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2282 | } |
2212 | } |
2283 | |
2213 | |
2284 | $y += $h; |
2214 | $y += $h; |
2285 | } |
2215 | } |
2286 | |
2216 | |
2287 | glDisable GL_TEXTURE_2D; |
2217 | glDisable GL_TEXTURE_2D; |
2288 | glDisable GL_BLEND; |
|
|
2289 | }; |
2218 | }; |
2290 | }); |
2219 | }); |
2291 | } |
2220 | } |
2292 | |
2221 | |
2293 | sub _draw { |
2222 | sub _draw { |
2294 | my ($self) = @_; |
2223 | my ($self) = @_; |
2295 | |
2224 | |
2296 | glEnable GL_BLEND; |
|
|
2297 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2298 | glEnable GL_TEXTURE_2D; |
2225 | glEnable GL_TEXTURE_2D; |
2299 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2226 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2300 | glColor 1, 1, 1, 1; |
2227 | glColor 1, 1, 1, 1; |
2301 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2228 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2302 | glDisable GL_TEXTURE_2D; |
2229 | glDisable GL_TEXTURE_2D; |
2303 | glDisable GL_BLEND; |
|
|
2304 | |
2230 | |
2305 | $self->{children}[1]->draw; |
2231 | $self->{children}[1]->draw; |
2306 | |
2232 | |
2307 | } |
2233 | } |
2308 | |
2234 | |
… | |
… | |
2402 | @_, |
2328 | @_, |
2403 | can_events => 0, |
2329 | can_events => 0, |
2404 | ) |
2330 | ) |
2405 | } |
2331 | } |
2406 | |
2332 | |
2407 | sub set_markup { |
2333 | sub set_tooltip_from { |
2408 | my ($self, $text) = @_; |
2334 | my ($self, $widget) = @_; |
2409 | |
2335 | |
2410 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2336 | $self->add (new CFClient::UI::Label |
2411 | $self->{label}->set_markup ($text); |
2337 | markup => $widget->{tooltip}, |
2412 | $self->add ($self->{label}); |
2338 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
2339 | fontsize => 0.8, |
|
|
2340 | fg => [0, 0, 0, 1], |
|
|
2341 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
|
|
2342 | ); |
2413 | } |
2343 | } |
2414 | |
2344 | |
2415 | sub size_request { |
2345 | sub size_request { |
2416 | my ($self) = @_; |
2346 | my ($self) = @_; |
2417 | |
|
|
2418 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2419 | |
2347 | |
2420 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2348 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2421 | |
2349 | |
2422 | ($w + 4, $h + 4) |
2350 | ($w + 4, $h + 4) |
2423 | } |
2351 | } |
… | |
… | |
2429 | } |
2357 | } |
2430 | |
2358 | |
2431 | sub _draw { |
2359 | sub _draw { |
2432 | my ($self) = @_; |
2360 | my ($self) = @_; |
2433 | |
2361 | |
2434 | glPushMatrix; |
|
|
2435 | glTranslate 0.375, 0.375; |
2362 | glTranslate 0.375, 0.375; |
2436 | |
2363 | |
2437 | my ($w, $h) = @$self{qw(w h)}; |
2364 | my ($w, $h) = @$self{qw(w h)}; |
2438 | |
2365 | |
2439 | glColor 1, 0.8, 0.4; |
2366 | glColor 1, 0.8, 0.4; |
… | |
… | |
2450 | glVertex 0 , $h; |
2377 | glVertex 0 , $h; |
2451 | glVertex $w, $h; |
2378 | glVertex $w, $h; |
2452 | glVertex $w, 0; |
2379 | glVertex $w, 0; |
2453 | glEnd; |
2380 | glEnd; |
2454 | |
2381 | |
2455 | glPopMatrix; |
2382 | glTranslate 2 - 0.375, 2 - 0.375; |
2456 | |
|
|
2457 | glTranslate 2, 2; |
|
|
2458 | $self->SUPER::_draw; |
2383 | $self->SUPER::_draw; |
2459 | } |
2384 | } |
2460 | |
2385 | |
2461 | ############################################################################# |
2386 | ############################################################################# |
2462 | |
2387 | |
… | |
… | |
2485 | return unless $::CONN;#d# manage and cache textures differently |
2410 | return unless $::CONN;#d# manage and cache textures differently |
2486 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2411 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2487 | |
2412 | |
2488 | # TODO animation |
2413 | # TODO animation |
2489 | if ($tex) { |
2414 | if ($tex) { |
2490 | glEnable GL_BLEND; |
|
|
2491 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2492 | glEnable GL_TEXTURE_2D; |
2415 | glEnable GL_TEXTURE_2D; |
2493 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2416 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2494 | glColor 1, 1, 1, 1; |
2417 | glColor 1, 1, 1, 1; |
2495 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2418 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2496 | glDisable GL_TEXTURE_2D; |
2419 | glDisable GL_TEXTURE_2D; |
2497 | glDisable GL_BLEND; |
|
|
2498 | } |
2420 | } |
2499 | } |
2421 | } |
2500 | |
2422 | |
2501 | ############################################################################# |
2423 | ############################################################################# |
2502 | |
2424 | |
… | |
… | |
2507 | sub new { |
2429 | sub new { |
2508 | my $class = shift; |
2430 | my $class = shift; |
2509 | |
2431 | |
2510 | my %args = @_; |
2432 | my %args = @_; |
2511 | |
2433 | |
2512 | my $item = $args{item}; |
2434 | my $item = delete $args{item}; |
2513 | |
2435 | |
2514 | my $desc = $item->{nrof} < 2 |
2436 | my $desc = $item->{nrof} < 2 |
2515 | ? $item->{name} |
2437 | ? $item->{name} |
2516 | : "$item->{nrof} $item->{name_pl}"; |
2438 | : "$item->{nrof} $item->{name_pl}"; |
2517 | |
2439 | |
… | |
… | |
2546 | |
2468 | |
2547 | 1 |
2469 | 1 |
2548 | }, |
2470 | }, |
2549 | %args |
2471 | %args |
2550 | ); |
2472 | ); |
|
|
2473 | |
2551 | $self->add(new CFClient::UI::Face |
2474 | $self->add (new CFClient::UI::Face |
2552 | can_events => 0, |
2475 | can_events => 0, |
2553 | face => $item->{face}, |
2476 | face => $item->{face}, |
2554 | anim => $item->{anim}, |
2477 | anim => $item->{anim}, |
2555 | animspeed => $item->{animspeed}); |
2478 | animspeed => $item->{animspeed}, |
|
|
2479 | ); |
|
|
2480 | |
2556 | $self->add(new CFClient::UI::Label |
2481 | $self->add (new CFClient::UI::Label |
2557 | can_events => 0, |
2482 | can_events => 0, |
2558 | text => $desc); |
2483 | text => $desc, |
|
|
2484 | ); |
2559 | |
2485 | |
2560 | $self |
2486 | $self |
2561 | } |
2487 | } |
2562 | |
2488 | |
2563 | ############################################################################# |
2489 | ############################################################################# |
… | |
… | |
2590 | for my $item (@items) { |
2516 | for my $item (@items) { |
2591 | my $desc = $item->{nrof} < 2 |
2517 | my $desc = $item->{nrof} < 2 |
2592 | ? $item->{name} |
2518 | ? $item->{name} |
2593 | : "$item->{nrof} $item->{name_pl}"; |
2519 | : "$item->{nrof} $item->{name_pl}"; |
2594 | |
2520 | |
2595 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
2521 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2596 | } |
2522 | } |
2597 | |
2523 | |
2598 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2524 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2599 | } |
2525 | } |
2600 | |
2526 | |
… | |
… | |
2675 | } |
2601 | } |
2676 | } |
2602 | } |
2677 | |
2603 | |
2678 | ############################################################################# |
2604 | ############################################################################# |
2679 | |
2605 | |
|
|
2606 | package CFClient::UI::Statusbox; |
|
|
2607 | |
|
|
2608 | our @ISA = CFClient::UI::VBox::; |
|
|
2609 | |
|
|
2610 | sub reorder { |
|
|
2611 | my ($self) = @_; |
|
|
2612 | my $NOW = time; |
|
|
2613 | |
|
|
2614 | while (my ($k, $v) = each %{ $self->{item} }) { |
|
|
2615 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
|
|
2616 | } |
|
|
2617 | |
|
|
2618 | my @widgets; |
|
|
2619 | |
|
|
2620 | my @items = sort { |
|
|
2621 | $a->{pri} <=> $b->{pri} |
|
|
2622 | or $b->{id} <=> $a->{id} |
|
|
2623 | } values %{ $self->{item} }; |
|
|
2624 | |
|
|
2625 | my $count = 10 + 1; |
|
|
2626 | for my $item (@items) { |
|
|
2627 | last unless --$count; |
|
|
2628 | |
|
|
2629 | push @widgets, $item->{label} ||= do { |
|
|
2630 | # TODO: doesn't handle markup well (read as: at all) |
|
|
2631 | my $short = $item->{count} > 1 |
|
|
2632 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2633 | : $item->{text}; |
|
|
2634 | |
|
|
2635 | for ($short) { |
|
|
2636 | s/^\s+//; |
|
|
2637 | s/\012.*/…/s; |
|
|
2638 | my $len = int 40 / $item->{fontsize}; |
|
|
2639 | substr $_, $len, length, "…" if $len < length; |
|
|
2640 | } |
|
|
2641 | |
|
|
2642 | new CFClient::UI::Label |
|
|
2643 | markup => $short, |
|
|
2644 | tooltip => $item->{tooltip}, |
|
|
2645 | tooltip_font => $::FONT_PROP, |
|
|
2646 | tooltip_width => 0.67, |
|
|
2647 | fontsize => $item->{fontsize}, |
|
|
2648 | color => $item->{color}, |
|
|
2649 | can_events => 1, |
|
|
2650 | can_hover => 1 |
|
|
2651 | }; |
|
|
2652 | } |
|
|
2653 | |
|
|
2654 | $self->clear; |
|
|
2655 | $self->SUPER::add (reverse @widgets); |
|
|
2656 | } |
|
|
2657 | |
|
|
2658 | sub add { |
|
|
2659 | my ($self, $text, %arg) = @_; |
|
|
2660 | |
|
|
2661 | $text =~ s/^\s+//; |
|
|
2662 | $text =~ s/\s+$//; |
|
|
2663 | |
|
|
2664 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2665 | |
|
|
2666 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2667 | |
|
|
2668 | if (my $item = $self->{item}{$group}) { |
|
|
2669 | if ($item->{text} eq $text) { |
|
|
2670 | $item->{count}++; |
|
|
2671 | } else { |
|
|
2672 | $item->{count} = 1; |
|
|
2673 | $item->{text} = $item->{tooltip} = $text; |
|
|
2674 | } |
|
|
2675 | $item->{id} = ++$self->{id}; |
|
|
2676 | $item->{timeout} = $timeout; |
|
|
2677 | delete $item->{label}; |
|
|
2678 | } else { |
|
|
2679 | $self->{item}{$group} = { |
|
|
2680 | id => ++$self->{id}, |
|
|
2681 | text => $text, |
|
|
2682 | timeout => $timeout, |
|
|
2683 | tooltip => $text, |
|
|
2684 | fontsize => 0.8, |
|
|
2685 | color => [0.8, 0.8, 0.8, 0.8], |
|
|
2686 | pri => 0, |
|
|
2687 | count => 1, |
|
|
2688 | %arg, |
|
|
2689 | }; |
|
|
2690 | } |
|
|
2691 | |
|
|
2692 | $self->reorder; |
|
|
2693 | } |
|
|
2694 | |
|
|
2695 | ############################################################################# |
|
|
2696 | |
2680 | package CFClient::UI::Root; |
2697 | package CFClient::UI::Root; |
2681 | |
2698 | |
2682 | our @ISA = CFClient::UI::Container::; |
2699 | our @ISA = CFClient::UI::Container::; |
2683 | |
2700 | |
2684 | use CFClient::OpenGL; |
2701 | use CFClient::OpenGL; |
2685 | |
2702 | |
|
|
2703 | sub new { |
|
|
2704 | my $class = shift; |
|
|
2705 | |
|
|
2706 | $class->SUPER::new ( |
|
|
2707 | @_, |
|
|
2708 | ) |
|
|
2709 | } |
|
|
2710 | |
|
|
2711 | sub configure { |
|
|
2712 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2713 | |
|
|
2714 | $self->{w} = $w; |
|
|
2715 | $self->{h} = $h; |
|
|
2716 | } |
|
|
2717 | |
2686 | sub check_size { |
2718 | sub check_size { |
2687 | my ($self) = @_; |
2719 | my ($self) = @_; |
2688 | |
2720 | |
2689 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2721 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2722 | if $self->{w}; |
2690 | } |
2723 | } |
2691 | |
2724 | |
2692 | sub size_request { |
2725 | sub size_request { |
2693 | my ($self) = @_; |
2726 | my ($self) = @_; |
2694 | |
2727 | |
… | |
… | |
2702 | my $old_h = $self->{old_h}; |
2735 | my $old_h = $self->{old_h}; |
2703 | |
2736 | |
2704 | if ($old_w && $old_h) { |
2737 | if ($old_w && $old_h) { |
2705 | for my $child ($self->children) { |
2738 | for my $child ($self->children) { |
2706 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
2739 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
2707 | $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; |
2740 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
2708 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
2741 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
2709 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
2742 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
2710 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
2743 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
2711 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
2744 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
2712 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2745 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2713 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2746 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2714 | } |
2747 | } |
2715 | } |
2748 | } |
2716 | |
2749 | |
2717 | $self->{old_w} = $w; |
|
|
2718 | $self->{old_h} = $h; |
|
|
2719 | } |
|
|
2720 | |
|
|
2721 | sub configure { |
|
|
2722 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2723 | |
|
|
2724 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2725 | |
|
|
2726 | for my $child ($self->children) { |
2750 | for my $child ($self->children) { |
2727 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2751 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2728 | |
2752 | |
2729 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2753 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2730 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2754 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2731 | $child->configure ($X, $Y, $W, $H); |
2755 | $child->configure ($X, $Y, $W, $H); |
2732 | } |
2756 | } |
2733 | } |
|
|
2734 | |
2757 | |
2735 | sub _topleft { |
2758 | $self->{old_w} = $w; |
|
|
2759 | $self->{old_h} = $h; |
|
|
2760 | } |
|
|
2761 | |
|
|
2762 | sub coord2local { |
2736 | my ($self, $x, $y) = @_; |
2763 | my ($self, $x, $y) = @_; |
2737 | |
2764 | |
2738 | ($x, $y) |
2765 | ($x, $y) |
2739 | } |
2766 | } |
2740 | |
2767 | |
|
|
2768 | sub coord2global { |
|
|
2769 | my ($self, $x, $y) = @_; |
|
|
2770 | |
|
|
2771 | ($x, $y) |
|
|
2772 | } |
|
|
2773 | |
2741 | sub update { |
2774 | sub update { |
2742 | my ($self) = @_; |
2775 | my ($self) = @_; |
2743 | |
2776 | |
2744 | $self->check_size; |
2777 | $self->check_size; |
2745 | ::refresh (); |
2778 | $::WANT_REFRESH++; |
2746 | } |
2779 | } |
2747 | |
2780 | |
2748 | sub add { |
2781 | sub add { |
2749 | my ($self, $child) = @_; |
2782 | my ($self, $child) = @_; |
2750 | |
2783 | |
2751 | # integerize window positions |
2784 | # integerise window positions |
2752 | $child->{x} = int $child->{x}; |
2785 | $child->{x} = int $child->{x}; |
2753 | $child->{y} = int $child->{y}; |
2786 | $child->{y} = int $child->{y}; |
2754 | |
2787 | |
2755 | $self->SUPER::add ($child); |
2788 | $self->SUPER::add ($child); |
2756 | } |
2789 | } |
… | |
… | |
2759 | my ($self, $id, $cb) = @_; |
2792 | my ($self, $id, $cb) = @_; |
2760 | |
2793 | |
2761 | $self->{refresh_hook}{$id} = $cb; |
2794 | $self->{refresh_hook}{$id} = $cb; |
2762 | } |
2795 | } |
2763 | |
2796 | |
|
|
2797 | sub on_post_alloc { |
|
|
2798 | my ($self, $id, $cb) = @_; |
|
|
2799 | |
|
|
2800 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2801 | } |
|
|
2802 | |
2764 | sub draw { |
2803 | sub draw { |
2765 | my ($self) = @_; |
2804 | my ($self) = @_; |
2766 | |
2805 | |
2767 | while (my $rcb = delete $self->{refresh_hook}) { |
2806 | while ($self->{refresh_hook}) { |
2768 | $_->() for values %$rcb; |
2807 | $_->() |
|
|
2808 | for values %{delete $self->{refresh_hook}}; |
|
|
2809 | } |
|
|
2810 | |
|
|
2811 | if ($self->{check_size}) { |
|
|
2812 | my @queue = ([], []); |
|
|
2813 | |
|
|
2814 | for (;;) { |
|
|
2815 | if ($self->{check_size}) { |
|
|
2816 | # heuristic: check containers last |
|
|
2817 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2818 | for values %{delete $self->{check_size}} |
|
|
2819 | } |
|
|
2820 | |
|
|
2821 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2822 | |
|
|
2823 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2824 | ? @$widget{qw(user_w user_h)} |
|
|
2825 | : $widget->size_request; |
|
|
2826 | |
|
|
2827 | if (delete $widget->{force_alloc} |
|
|
2828 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
2829 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2830 | |
|
|
2831 | $widget->{req_w} = $w; |
|
|
2832 | $widget->{req_h} = $h; |
|
|
2833 | |
|
|
2834 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
|
|
2835 | |
|
|
2836 | $widget->{parent}->check_size |
|
|
2837 | if $widget->{parent}; |
|
|
2838 | } |
|
|
2839 | } |
|
|
2840 | } |
|
|
2841 | |
|
|
2842 | while ($self->{size_alloc}) { |
|
|
2843 | for (values %{delete $self->{size_alloc}}) { |
|
|
2844 | my ($widget, $w, $h) = @$_; |
|
|
2845 | |
|
|
2846 | $w = 0 if $w < 0; |
|
|
2847 | $h = 0 if $h < 0; |
|
|
2848 | |
|
|
2849 | $widget->{w} = $w; |
|
|
2850 | $widget->{h} = $h; |
|
|
2851 | $widget->size_allocate ($w, $h); |
|
|
2852 | $widget->emit (size_allocate => $w, $h); |
|
|
2853 | } |
|
|
2854 | } |
|
|
2855 | |
|
|
2856 | while ($self->{post_alloc_hook}) { |
|
|
2857 | $_->() |
|
|
2858 | for values %{delete $self->{post_alloc_hook}}; |
2769 | } |
2859 | } |
2770 | |
2860 | |
2771 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2861 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2772 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2862 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2773 | glClear GL_COLOR_BUFFER_BIT; |
2863 | glClear GL_COLOR_BUFFER_BIT; |