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 | # TODO: req_w _does_change after ->reconfigure |
|
|
389 | $self->check_size |
|
|
390 | unless exists $self->{req_w}; |
403 | } |
391 | } |
404 | |
392 | |
405 | sub check_size { |
393 | sub check_size { |
406 | my ($self) = @_; |
394 | my ($self, $forced) = @_; |
407 | |
395 | |
408 | $self->{parent} |
396 | $self->{force_alloc} = 1 if $forced; |
409 | or return 1; |
397 | $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 | } |
398 | } |
430 | |
399 | |
431 | sub update { |
400 | sub update { |
432 | my ($self) = @_; |
401 | my ($self) = @_; |
433 | |
402 | |
… | |
… | |
530 | |
499 | |
531 | $self |
500 | $self |
532 | } |
501 | } |
533 | |
502 | |
534 | sub add { |
503 | sub add { |
535 | my ($self, $child) = @_; |
504 | my ($self, @widgets) = @_; |
536 | |
505 | |
537 | $child->set_parent ($self); |
506 | $_->set_parent ($self) |
|
|
507 | for @widgets; |
538 | |
508 | |
539 | use sort 'stable'; |
509 | use sort 'stable'; |
540 | |
510 | |
541 | $self->{children} = [ |
511 | $self->{children} = [ |
542 | sort { $a->{z} <=> $b->{z} } |
512 | sort { $a->{z} <=> $b->{z} } |
543 | @{$self->{children}}, $child |
513 | @{$self->{children}}, @widgets |
544 | ]; |
514 | ]; |
545 | |
515 | |
546 | $child->check_size; |
516 | $self->check_size (1); |
547 | $self->update; |
517 | $self->update; |
548 | } |
518 | } |
549 | |
519 | |
550 | sub children { |
520 | sub children { |
551 | @{ $_[0]{children} } |
521 | @{ $_[0]{children} } |
… | |
… | |
658 | } |
628 | } |
659 | |
629 | |
660 | sub update { |
630 | sub update { |
661 | my ($self) = @_; |
631 | my ($self) = @_; |
662 | |
632 | |
663 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
633 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
664 | $self->SUPER::update; |
634 | $self->SUPER::update; |
665 | } |
635 | } |
666 | |
636 | |
667 | sub size_allocate { |
637 | sub size_allocate { |
668 | my ($self, $w, $h) = @_; |
638 | my ($self, $w, $h) = @_; |
… | |
… | |
699 | my ($w, $h) = ($self->w, $self->h); |
669 | my ($w, $h) = ($self->w, $self->h); |
700 | |
670 | |
701 | my $tex = $self->{texture} |
671 | my $tex = $self->{texture} |
702 | or return; |
672 | or return; |
703 | |
673 | |
704 | glEnable GL_BLEND; |
|
|
705 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
706 | glEnable GL_TEXTURE_2D; |
674 | glEnable GL_TEXTURE_2D; |
707 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
675 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
708 | glColor 0, 0, 0, 1; |
676 | glColor 0, 0, 0, 1; |
709 | |
677 | |
710 | $tex->draw_quad (0, 0, $w, $h); |
678 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
711 | |
679 | |
712 | glDisable GL_BLEND; |
|
|
713 | glDisable GL_TEXTURE_2D; |
680 | glDisable GL_TEXTURE_2D; |
714 | } |
681 | } |
715 | |
682 | |
716 | ############################################################################# |
683 | ############################################################################# |
717 | |
684 | |
… | |
… | |
741 | $self->{view_y} = int $y; |
708 | $self->{view_y} = int $y; |
742 | |
709 | |
743 | $self->update; |
710 | $self->update; |
744 | } |
711 | } |
745 | |
712 | |
746 | # hmm, this does not work for topleft of $self... but we should not aks for that |
713 | # hmm, this does not work for topleft of $self... but we should not ask for that |
747 | sub _topleft { |
714 | sub coord2local { |
748 | my ($self, $x, $y) = @_; |
715 | my ($self, $x, $y) = @_; |
749 | |
716 | |
750 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
717 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
718 | } |
|
|
719 | |
|
|
720 | sub coord2global { |
|
|
721 | my ($self, $x, $y) = @_; |
|
|
722 | |
|
|
723 | $x = List::Util::min $self->{w}, $x - $self->{view_x}; |
|
|
724 | $y = List::Util::min $self->{h}, $y - $self->{view_y}; |
|
|
725 | |
|
|
726 | $self->SUPER::coord2global ($x, $y) |
751 | } |
727 | } |
752 | |
728 | |
753 | sub find_widget { |
729 | sub find_widget { |
754 | my ($self, $x, $y) = @_; |
730 | my ($self, $x, $y) = @_; |
755 | |
731 | |
… | |
… | |
800 | $self->add ($self->{slider}); |
776 | $self->add ($self->{slider}); |
801 | |
777 | |
802 | $self |
778 | $self |
803 | } |
779 | } |
804 | |
780 | |
805 | #TODO# update range on size_allocate depeneing on child |
781 | #TODO# update range on size_allocate depending on child |
806 | # update viewport offset on scroll |
782 | # update viewport offset on scroll |
807 | |
783 | |
808 | ############################################################################# |
784 | ############################################################################# |
809 | |
785 | |
810 | package CFClient::UI::Frame; |
786 | package CFClient::UI::Frame; |
… | |
… | |
814 | use CFClient::OpenGL; |
790 | use CFClient::OpenGL; |
815 | |
791 | |
816 | sub new { |
792 | sub new { |
817 | my $class = shift; |
793 | my $class = shift; |
818 | |
794 | |
819 | my $self = $class->SUPER::new ( |
795 | $class->SUPER::new ( |
820 | bg => [1, 1, 1, 1], |
796 | bg => undef, |
821 | border_bg => [1, 1, 1, 1], |
|
|
822 | border => 0.8, |
|
|
823 | @_ |
797 | @_, |
824 | ); |
798 | ) |
825 | |
|
|
826 | $self |
|
|
827 | } |
799 | } |
828 | |
800 | |
829 | sub _draw { |
801 | sub _draw { |
830 | my ($self) = @_; |
802 | my ($self) = @_; |
831 | |
803 | |
832 | my ($w, $h) = ($self->{w}, $self->{h}); |
804 | if ($self->{bg}) { |
|
|
805 | my ($w, $h) = @$self{qw(w h)}; |
833 | |
806 | |
834 | glEnable GL_BLEND; |
807 | glEnable GL_BLEND; |
835 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
808 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
836 | glEnable GL_TEXTURE_2D; |
809 | glColor @{ $self->{bg} }; |
837 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
838 | |
810 | |
839 | # glBegin GL_QUADS; |
811 | glBegin GL_QUADS; |
840 | # glColor 0, 0, 0, 0; |
|
|
841 | # glVertex 0 , 0; |
812 | glVertex 0 , 0; |
842 | # glVertex 0 , $h; |
813 | glVertex 0 , $h; |
843 | # glVertex $w, $h; |
814 | glVertex $w, $h; |
844 | # glVertex $w, 0; |
815 | glVertex $w, 0; |
845 | # glEnd; |
816 | glEnd; |
846 | |
817 | |
847 | |
|
|
848 | $self->child->draw; |
|
|
849 | glDisable GL_BLEND; |
818 | glDisable GL_BLEND; |
850 | glDisable GL_TEXTURE_2D; |
819 | } |
|
|
820 | |
|
|
821 | $self->SUPER::_draw; |
851 | } |
822 | } |
852 | |
823 | |
853 | ############################################################################# |
824 | ############################################################################# |
854 | |
825 | |
855 | package CFClient::UI::FancyFrame; |
826 | package CFClient::UI::FancyFrame; |
… | |
… | |
971 | my ($self) = @_; |
942 | my ($self) = @_; |
972 | |
943 | |
973 | my ($w, $h ) = ($self->{w}, $self->{h}); |
944 | my ($w, $h ) = ($self->{w}, $self->{h}); |
974 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
945 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
975 | |
946 | |
976 | glEnable GL_BLEND; |
|
|
977 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
978 | glEnable GL_TEXTURE_2D; |
947 | glEnable GL_TEXTURE_2D; |
979 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
948 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
980 | |
949 | |
981 | my $border = $self->border; |
950 | my $border = $self->border; |
982 | |
951 | |
983 | glColor @{ $self->{border_bg} }; |
952 | glColor @{ $self->{border_bg} }; |
984 | $tex[1]->draw_quad (0, 0, $w, $border); |
953 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
985 | $tex[3]->draw_quad (0, $border, $border, $ch); |
954 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
986 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
955 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
987 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
956 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
988 | |
957 | |
989 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
958 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
990 | my $bg = $tex[0]; |
959 | my $bg = $tex[0]; |
991 | |
960 | |
992 | # TODO: repeat texture not scale |
961 | # TODO: repeat texture not scale |
… | |
… | |
996 | glColor @{ $self->{bg} }; |
965 | glColor @{ $self->{bg} }; |
997 | |
966 | |
998 | $bg->{s} = $rep_x; |
967 | $bg->{s} = $rep_x; |
999 | $bg->{t} = $rep_y; |
968 | $bg->{t} = $rep_y; |
1000 | $bg->{wrap_mode} = 1; |
969 | $bg->{wrap_mode} = 1; |
1001 | $bg->draw_quad ($border, $border, $cw, $ch); |
970 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
971 | } |
1002 | |
972 | |
1003 | glDisable GL_TEXTURE_2D; |
973 | glDisable GL_TEXTURE_2D; |
1004 | glDisable GL_BLEND; |
|
|
1005 | } |
|
|
1006 | |
974 | |
1007 | $self->{title}->draw if $self->{title}; |
975 | $self->{title}->draw if $self->{title}; |
1008 | |
976 | |
1009 | $self->child->draw; |
977 | $self->child->draw; |
1010 | } |
978 | } |
… | |
… | |
1237 | } |
1205 | } |
1238 | |
1206 | |
1239 | sub size_allocate { |
1207 | sub size_allocate { |
1240 | my ($self, $w, $h) = @_; |
1208 | my ($self, $w, $h) = @_; |
1241 | |
1209 | |
|
|
1210 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
|
|
1211 | |
1242 | my $children = $self->{children}; |
1212 | my $children = $self->{children}; |
1243 | |
1213 | |
1244 | my @h = map $_->{req_h}, @$children; |
1214 | my @h = map $_->{req_h}, @$children; |
1245 | |
1215 | |
1246 | my $req_h = List::Util::sum @h; |
1216 | my $req_h = List::Util::sum @h; |
… | |
… | |
1285 | my ($class, %arg) = @_; |
1255 | my ($class, %arg) = @_; |
1286 | |
1256 | |
1287 | my $self = $class->SUPER::new ( |
1257 | my $self = $class->SUPER::new ( |
1288 | fg => [1, 1, 1], |
1258 | fg => [1, 1, 1], |
1289 | #font => default_font |
1259 | #font => default_font |
|
|
1260 | #text => initial text |
|
|
1261 | #markup => initial narkup |
|
|
1262 | layout => (new CFClient::Layout), |
1290 | fontsize => 1, |
1263 | fontsize => 1, |
1291 | text => "", |
|
|
1292 | align => -1, |
1264 | align => -1, |
1293 | valign => -1, |
1265 | valign => -1, |
1294 | padding => 2, |
1266 | padding => 2, |
1295 | layout => new CFClient::Layout, |
|
|
1296 | can_events => 0, |
1267 | can_events => 0, |
1297 | %arg |
1268 | %arg |
1298 | ); |
1269 | ); |
1299 | |
1270 | |
1300 | if (exists $self->{template}) { |
1271 | if (exists $self->{template}) { |
1301 | my $layout = new CFClient::Layout; |
1272 | my $layout = new CFClient::Layout; |
1302 | $layout->set_text (delete $self->{template}); |
1273 | $layout->set_text (delete $self->{template}); |
1303 | $self->{template} = $layout; |
1274 | $self->{template} = $layout; |
1304 | } |
1275 | } |
1305 | |
1276 | |
1306 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1277 | if (exists $self->{markup}) { |
1307 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1278 | $self->set_markup (delete $self->{markup}); |
|
|
1279 | } else { |
|
|
1280 | $self->set_text (delete $self->{text}); |
|
|
1281 | } |
1308 | |
1282 | |
1309 | $self |
1283 | $self |
1310 | } |
1284 | } |
1311 | |
1285 | |
1312 | sub escape { |
1286 | sub escape { |
… | |
… | |
1330 | my ($self, $text) = @_; |
1304 | my ($self, $text) = @_; |
1331 | |
1305 | |
1332 | return if $self->{text} eq "T$text"; |
1306 | return if $self->{text} eq "T$text"; |
1333 | $self->{text} = "T$text"; |
1307 | $self->{text} = "T$text"; |
1334 | |
1308 | |
|
|
1309 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1335 | $self->{layout}->set_text ($text); |
1310 | $self->{layout}->set_text ($text); |
1336 | |
1311 | |
1337 | delete $self->{texture}; |
|
|
1338 | $self->update; |
1312 | $self->update; |
1339 | $self->check_size; |
1313 | $self->check_size; |
1340 | } |
1314 | } |
1341 | |
1315 | |
1342 | sub set_markup { |
1316 | sub set_markup { |
1343 | my ($self, $markup) = @_; |
1317 | my ($self, $markup) = @_; |
1344 | |
1318 | |
1345 | return if $self->{text} eq "M$markup"; |
1319 | return if $self->{text} eq "M$markup"; |
1346 | $self->{text} = "M$markup"; |
1320 | $self->{text} = "M$markup"; |
1347 | |
1321 | |
|
|
1322 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
|
|
1323 | |
|
|
1324 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1348 | $self->{layout}->set_markup ($markup); |
1325 | $self->{layout}->set_markup ($markup); |
1349 | |
1326 | |
1350 | delete $self->{texture}; |
|
|
1351 | $self->update; |
1327 | $self->update; |
1352 | $self->check_size; |
1328 | $self->check_size; |
1353 | } |
1329 | } |
1354 | |
1330 | |
1355 | sub size_request { |
1331 | sub size_request { |
… | |
… | |
1395 | |
1371 | |
1396 | sub _draw { |
1372 | sub _draw { |
1397 | my ($self) = @_; |
1373 | my ($self) = @_; |
1398 | |
1374 | |
1399 | my $tex = $self->{texture} ||= do { |
1375 | my $tex = $self->{texture} ||= do { |
|
|
1376 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1400 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1377 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1401 | $self->{layout}->set_width ($self->{w}); |
1378 | $self->{layout}->set_width ($self->{w}); |
1402 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1379 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
|
|
1380 | |
1403 | new_from_layout CFClient::Texture $self->{layout} |
1381 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
|
|
1382 | |
|
|
1383 | $self->{ox} = int $self->{align} < 0 ? $self->{padding} |
|
|
1384 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1385 | : ($self->{w} - $tex->{w}) * 0.5; |
|
|
1386 | |
|
|
1387 | $self->{oy} = int $self->{valign} < 0 ? $self->{padding} |
|
|
1388 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1389 | : ($self->{h} - $tex->{h}) * 0.5; |
|
|
1390 | |
|
|
1391 | $tex |
1404 | }; |
1392 | }; |
1405 | |
1393 | |
1406 | glEnable GL_BLEND; |
|
|
1407 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1408 | glEnable GL_TEXTURE_2D; |
1394 | glEnable GL_TEXTURE_2D; |
1409 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1395 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1410 | |
1396 | |
|
|
1397 | if ($tex->{format} == GL_ALPHA) { |
1411 | glColor @{$self->{fg}}; |
1398 | 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}); |
1399 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
|
|
1400 | } else { |
|
|
1401 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
|
|
1402 | } |
1426 | |
1403 | |
1427 | glDisable GL_TEXTURE_2D; |
1404 | glDisable GL_TEXTURE_2D; |
1428 | glDisable GL_BLEND; |
|
|
1429 | } |
1405 | } |
1430 | |
1406 | |
1431 | ############################################################################# |
1407 | ############################################################################# |
1432 | |
1408 | |
1433 | package CFClient::UI::EntryBase; |
1409 | package CFClient::UI::EntryBase; |
… | |
… | |
1468 | $self->{layout}->set_text ("$text "); |
1444 | $self->{layout}->set_text ("$text "); |
1469 | |
1445 | |
1470 | $self->emit (changed => $self->{text}); |
1446 | $self->emit (changed => $self->{text}); |
1471 | } |
1447 | } |
1472 | |
1448 | |
|
|
1449 | sub set_text { |
|
|
1450 | my ($self, $text) = @_; |
|
|
1451 | |
|
|
1452 | $self->{cursor} = length $text; |
|
|
1453 | $self->_set_text ($text); |
|
|
1454 | $self->check_size; |
|
|
1455 | $self->update; |
|
|
1456 | } |
|
|
1457 | |
1473 | sub get_text { |
1458 | sub get_text { |
1474 | $_[0]{text} |
1459 | $_[0]{text} |
1475 | } |
1460 | } |
1476 | |
1461 | |
1477 | sub size_request { |
1462 | sub size_request { |
… | |
… | |
1484 | |
1469 | |
1485 | sub size_allocate { |
1470 | sub size_allocate { |
1486 | my ($self, $w, $h) = @_; |
1471 | my ($self, $w, $h) = @_; |
1487 | |
1472 | |
1488 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1473 | $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 | } |
1474 | } |
1498 | |
1475 | |
1499 | sub key_down { |
1476 | sub key_down { |
1500 | my ($self, $ev) = @_; |
1477 | my ($self, $ev) = @_; |
1501 | |
1478 | |
… | |
… | |
1689 | |
1666 | |
1690 | if ($GRAB == $self) { |
1667 | if ($GRAB == $self) { |
1691 | $self->{fg} = $self->{active_fg}; |
1668 | $self->{fg} = $self->{active_fg}; |
1692 | } |
1669 | } |
1693 | |
1670 | |
1694 | glEnable GL_BLEND; |
|
|
1695 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1696 | glEnable GL_TEXTURE_2D; |
1671 | glEnable GL_TEXTURE_2D; |
1697 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1672 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1698 | glColor 0, 0, 0, 1; |
1673 | glColor 0, 0, 0, 1; |
1699 | |
1674 | |
1700 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1675 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1701 | |
1676 | |
1702 | glDisable GL_TEXTURE_2D; |
1677 | glDisable GL_TEXTURE_2D; |
1703 | glDisable GL_BLEND; |
|
|
1704 | |
1678 | |
1705 | $self->SUPER::_draw; |
1679 | $self->SUPER::_draw; |
1706 | } |
1680 | } |
1707 | |
1681 | |
1708 | ############################################################################# |
1682 | ############################################################################# |
… | |
… | |
1755 | |
1729 | |
1756 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1730 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1757 | |
1731 | |
1758 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1732 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1759 | |
1733 | |
1760 | glEnable GL_BLEND; |
1734 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1735 | |
1761 | glEnable GL_TEXTURE_2D; |
1736 | 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); |
1737 | $tex->draw_quad_alpha (0, 0, $s, $s); |
1767 | |
|
|
1768 | glDisable GL_TEXTURE_2D; |
1738 | glDisable GL_TEXTURE_2D; |
1769 | glDisable GL_BLEND; |
|
|
1770 | } |
1739 | } |
1771 | |
1740 | |
1772 | ############################################################################# |
1741 | ############################################################################# |
1773 | |
1742 | |
1774 | package CFClient::UI::Image; |
1743 | package CFClient::UI::Image; |
… | |
… | |
1817 | glTranslate 0, -$self->{w}, 0; |
1786 | glTranslate 0, -$self->{w}, 0; |
1818 | |
1787 | |
1819 | ($w, $h) = ($h, $w); |
1788 | ($w, $h) = ($h, $w); |
1820 | } |
1789 | } |
1821 | |
1790 | |
1822 | glEnable GL_BLEND; |
|
|
1823 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1824 | glEnable GL_TEXTURE_2D; |
1791 | glEnable GL_TEXTURE_2D; |
1825 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1792 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1826 | |
1793 | |
1827 | $tex->draw_quad (0, 0, $w, $h); |
1794 | $tex->draw_quad_alpha (0, 0, $w, $h); |
1828 | |
1795 | |
1829 | glDisable GL_BLEND; |
|
|
1830 | glDisable GL_TEXTURE_2D; |
1796 | glDisable GL_TEXTURE_2D; |
1831 | } |
1797 | } |
1832 | |
1798 | |
1833 | ############################################################################# |
1799 | ############################################################################# |
1834 | |
1800 | |
… | |
… | |
2126 | $page ||= 2; |
2092 | $page ||= 2; |
2127 | |
2093 | |
2128 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2094 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2129 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2095 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2130 | |
2096 | |
2131 | glEnable GL_BLEND; |
|
|
2132 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2133 | glEnable GL_TEXTURE_2D; |
2097 | glEnable GL_TEXTURE_2D; |
2134 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2098 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2135 | |
2099 | |
2136 | # draw background |
2100 | # draw background |
2137 | $tex[1]->draw_quad (0, 0, $w, $h); |
2101 | $tex[1]->draw_quad_alpha (0, 0, $w, $h); |
2138 | |
2102 | |
2139 | # draw handle |
2103 | # draw handle |
2140 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
2104 | $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); |
2141 | |
2105 | |
2142 | glDisable GL_BLEND; |
|
|
2143 | glDisable GL_TEXTURE_2D; |
2106 | glDisable GL_TEXTURE_2D; |
2144 | } |
2107 | } |
2145 | |
2108 | |
2146 | ############################################################################# |
2109 | ############################################################################# |
2147 | |
2110 | |
… | |
… | |
2158 | fontsize => 1, |
2121 | fontsize => 1, |
2159 | can_events => 0, |
2122 | can_events => 0, |
2160 | #font => default_font |
2123 | #font => default_font |
2161 | @_, |
2124 | @_, |
2162 | |
2125 | |
2163 | layout => (new CFClient::Layout), |
2126 | layout => (new CFClient::Layout 1), |
2164 | par => [], |
2127 | par => [], |
2165 | height => 0, |
2128 | height => 0, |
2166 | children => [ |
2129 | children => [ |
2167 | (new CFClient::UI::Empty expand => 1), |
2130 | (new CFClient::UI::Empty expand => 1), |
2168 | (new CFClient::UI::Slider vertical => 1), |
2131 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2186 | |
2149 | |
2187 | my $layout = $self->{layout}; |
2150 | my $layout = $self->{layout}; |
2188 | |
2151 | |
2189 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2152 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2190 | $layout->set_width ($self->{children}[0]{w}); |
2153 | $layout->set_width ($self->{children}[0]{w}); |
2191 | $layout->set_text ($text); |
2154 | $layout->set_markup ($text); |
2192 | |
2155 | |
2193 | ($layout->size)[1] |
2156 | ($layout->size)[1] |
2194 | } |
2157 | } |
2195 | |
2158 | |
2196 | sub reflow { |
2159 | sub reflow { |
… | |
… | |
2234 | |
2197 | |
2235 | return unless $self->{h} > 0; |
2198 | return unless $self->{h} > 0; |
2236 | |
2199 | |
2237 | delete $self->{texture}; |
2200 | delete $self->{texture}; |
2238 | |
2201 | |
2239 | $ROOT->on_refresh ($self, sub { |
2202 | $ROOT->on_post_alloc ($self, sub { |
2240 | if (delete $self->{need_reflow}) { |
2203 | if (delete $self->{need_reflow}) { |
2241 | my $height = 0; |
2204 | my $height = 0; |
2242 | |
2205 | |
2243 | $height += $_->[0] = $self->text_height ($_->[2]) |
2206 | $height += $_->[0] = $self->text_height ($_->[2]) |
2244 | for @{$self->{par}}; |
2207 | for @{$self->{par}}; |
… | |
… | |
2252 | |
2215 | |
2253 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2216 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2254 | glClearColor 0, 0, 0, 0; |
2217 | glClearColor 0, 0, 0, 0; |
2255 | glClear GL_COLOR_BUFFER_BIT; |
2218 | glClear GL_COLOR_BUFFER_BIT; |
2256 | |
2219 | |
2257 | glEnable GL_BLEND; |
|
|
2258 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2259 | glEnable GL_TEXTURE_2D; |
2220 | glEnable GL_TEXTURE_2D; |
2260 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2221 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2261 | |
2222 | |
2262 | my $top = int $self->{children}[1]{range}[0]; |
2223 | my $top = int $self->{children}[1]{range}[0]; |
2263 | |
2224 | |
… | |
… | |
2272 | |
2233 | |
2273 | for my $par (@{$self->{par}}) { |
2234 | for my $par (@{$self->{par}}) { |
2274 | my $h = $par->[0]; |
2235 | my $h = $par->[0]; |
2275 | |
2236 | |
2276 | if ($y0 < $y + $h && $y < $y1) { |
2237 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2238 | $layout->set_foreground (@{ $par->[1] }); |
2277 | $layout->set_text ($par->[2]); |
2239 | $layout->set_markup ($par->[2]); |
2278 | |
2240 | |
2279 | glColor @{ $par->[1] }; |
|
|
2280 | my ($W, $H) = $layout->size; |
2241 | my ($W, $H) = $layout->size; |
2281 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
2242 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2282 | } |
2243 | } |
2283 | |
2244 | |
2284 | $y += $h; |
2245 | $y += $h; |
2285 | } |
2246 | } |
2286 | |
2247 | |
2287 | glDisable GL_TEXTURE_2D; |
2248 | glDisable GL_TEXTURE_2D; |
2288 | glDisable GL_BLEND; |
|
|
2289 | }; |
2249 | }; |
2290 | }); |
2250 | }); |
2291 | } |
2251 | } |
2292 | |
2252 | |
2293 | sub _draw { |
2253 | sub _draw { |
2294 | my ($self) = @_; |
2254 | my ($self) = @_; |
2295 | |
2255 | |
2296 | glEnable GL_BLEND; |
|
|
2297 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2298 | glEnable GL_TEXTURE_2D; |
2256 | glEnable GL_TEXTURE_2D; |
2299 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2257 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2300 | glColor 1, 1, 1, 1; |
2258 | glColor 1, 1, 1, 1; |
2301 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2259 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2302 | glDisable GL_TEXTURE_2D; |
2260 | glDisable GL_TEXTURE_2D; |
2303 | glDisable GL_BLEND; |
|
|
2304 | |
2261 | |
2305 | $self->{children}[1]->draw; |
2262 | $self->{children}[1]->draw; |
2306 | |
2263 | |
2307 | } |
2264 | } |
2308 | |
2265 | |
… | |
… | |
2402 | @_, |
2359 | @_, |
2403 | can_events => 0, |
2360 | can_events => 0, |
2404 | ) |
2361 | ) |
2405 | } |
2362 | } |
2406 | |
2363 | |
2407 | sub set_markup { |
2364 | sub set_tooltip_from { |
2408 | my ($self, $text) = @_; |
2365 | my ($self, $widget) = @_; |
2409 | |
2366 | |
2410 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2367 | $self->add (new CFClient::UI::Label |
2411 | $self->{label}->set_markup ($text); |
2368 | markup => $widget->{tooltip}, |
2412 | $self->add ($self->{label}); |
2369 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
2370 | fontsize => 0.8, |
|
|
2371 | fg => [0, 0, 0, 1], |
|
|
2372 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
|
|
2373 | ); |
2413 | } |
2374 | } |
2414 | |
2375 | |
2415 | sub size_request { |
2376 | sub size_request { |
2416 | my ($self) = @_; |
2377 | my ($self) = @_; |
2417 | |
|
|
2418 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2419 | |
2378 | |
2420 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2379 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2421 | |
2380 | |
2422 | ($w + 4, $h + 4) |
2381 | ($w + 4, $h + 4) |
2423 | } |
2382 | } |
… | |
… | |
2429 | } |
2388 | } |
2430 | |
2389 | |
2431 | sub _draw { |
2390 | sub _draw { |
2432 | my ($self) = @_; |
2391 | my ($self) = @_; |
2433 | |
2392 | |
2434 | glPushMatrix; |
|
|
2435 | glTranslate 0.375, 0.375; |
2393 | glTranslate 0.375, 0.375; |
2436 | |
2394 | |
2437 | my ($w, $h) = @$self{qw(w h)}; |
2395 | my ($w, $h) = @$self{qw(w h)}; |
2438 | |
2396 | |
2439 | glColor 1, 0.8, 0.4; |
2397 | glColor 1, 0.8, 0.4; |
… | |
… | |
2450 | glVertex 0 , $h; |
2408 | glVertex 0 , $h; |
2451 | glVertex $w, $h; |
2409 | glVertex $w, $h; |
2452 | glVertex $w, 0; |
2410 | glVertex $w, 0; |
2453 | glEnd; |
2411 | glEnd; |
2454 | |
2412 | |
2455 | glPopMatrix; |
2413 | glTranslate 2 - 0.375, 2 - 0.375; |
2456 | |
|
|
2457 | glTranslate 2, 2; |
|
|
2458 | $self->SUPER::_draw; |
2414 | $self->SUPER::_draw; |
2459 | } |
2415 | } |
2460 | |
2416 | |
2461 | ############################################################################# |
2417 | ############################################################################# |
2462 | |
2418 | |
… | |
… | |
2485 | return unless $::CONN;#d# manage and cache textures differently |
2441 | return unless $::CONN;#d# manage and cache textures differently |
2486 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2442 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2487 | |
2443 | |
2488 | # TODO animation |
2444 | # TODO animation |
2489 | if ($tex) { |
2445 | if ($tex) { |
2490 | glEnable GL_BLEND; |
|
|
2491 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2492 | glEnable GL_TEXTURE_2D; |
2446 | glEnable GL_TEXTURE_2D; |
2493 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2494 | glColor 1, 1, 1, 1; |
2448 | glColor 1, 1, 1, 1; |
2495 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2449 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2496 | glDisable GL_TEXTURE_2D; |
2450 | glDisable GL_TEXTURE_2D; |
2497 | glDisable GL_BLEND; |
|
|
2498 | } |
2451 | } |
2499 | } |
2452 | } |
2500 | |
2453 | |
2501 | ############################################################################# |
2454 | ############################################################################# |
2502 | |
2455 | |
… | |
… | |
2507 | sub new { |
2460 | sub new { |
2508 | my $class = shift; |
2461 | my $class = shift; |
2509 | |
2462 | |
2510 | my %args = @_; |
2463 | my %args = @_; |
2511 | |
2464 | |
2512 | my $item = $args{item}; |
2465 | my $item = delete $args{item}; |
2513 | |
2466 | |
2514 | my $desc = $item->{nrof} < 2 |
2467 | my $desc = $item->{nrof} < 2 |
2515 | ? $item->{name} |
2468 | ? $item->{name} |
2516 | : "$item->{nrof} $item->{name_pl}"; |
2469 | : "$item->{nrof} $item->{name_pl}"; |
2517 | |
2470 | |
… | |
… | |
2546 | |
2499 | |
2547 | 1 |
2500 | 1 |
2548 | }, |
2501 | }, |
2549 | %args |
2502 | %args |
2550 | ); |
2503 | ); |
|
|
2504 | |
2551 | $self->add(new CFClient::UI::Face |
2505 | $self->add (new CFClient::UI::Face |
2552 | can_events => 0, |
2506 | can_events => 0, |
2553 | face => $item->{face}, |
2507 | face => $item->{face}, |
2554 | anim => $item->{anim}, |
2508 | anim => $item->{anim}, |
2555 | animspeed => $item->{animspeed}); |
2509 | animspeed => $item->{animspeed}, |
|
|
2510 | ); |
|
|
2511 | |
2556 | $self->add(new CFClient::UI::Label |
2512 | $self->add (new CFClient::UI::Label |
2557 | can_events => 0, |
2513 | can_events => 0, |
2558 | text => $desc); |
2514 | text => $desc, |
|
|
2515 | ); |
2559 | |
2516 | |
2560 | $self |
2517 | $self |
2561 | } |
2518 | } |
2562 | |
2519 | |
2563 | ############################################################################# |
2520 | ############################################################################# |
… | |
… | |
2590 | for my $item (@items) { |
2547 | for my $item (@items) { |
2591 | my $desc = $item->{nrof} < 2 |
2548 | my $desc = $item->{nrof} < 2 |
2592 | ? $item->{name} |
2549 | ? $item->{name} |
2593 | : "$item->{nrof} $item->{name_pl}"; |
2550 | : "$item->{nrof} $item->{name_pl}"; |
2594 | |
2551 | |
2595 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
2552 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2596 | } |
2553 | } |
2597 | |
2554 | |
2598 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2555 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2599 | } |
2556 | } |
2600 | |
2557 | |
… | |
… | |
2675 | } |
2632 | } |
2676 | } |
2633 | } |
2677 | |
2634 | |
2678 | ############################################################################# |
2635 | ############################################################################# |
2679 | |
2636 | |
|
|
2637 | package CFClient::UI::Statusbox; |
|
|
2638 | |
|
|
2639 | our @ISA = CFClient::UI::VBox::; |
|
|
2640 | |
|
|
2641 | sub reorder { |
|
|
2642 | my ($self) = @_; |
|
|
2643 | my $NOW = time; |
|
|
2644 | |
|
|
2645 | while (my ($k, $v) = each %{ $self->{item} }) { |
|
|
2646 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
|
|
2647 | } |
|
|
2648 | |
|
|
2649 | my @widgets; |
|
|
2650 | |
|
|
2651 | my @items = sort { |
|
|
2652 | $a->{pri} <=> $b->{pri} |
|
|
2653 | or $b->{id} <=> $a->{id} |
|
|
2654 | } values %{ $self->{item} }; |
|
|
2655 | |
|
|
2656 | my $count = 10 + 1; |
|
|
2657 | for my $item (@items) { |
|
|
2658 | last unless --$count; |
|
|
2659 | |
|
|
2660 | push @widgets, $item->{label} ||= do { |
|
|
2661 | # TODO: doesn't handle markup well (read as: at all) |
|
|
2662 | my $short = $item->{count} > 1 |
|
|
2663 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2664 | : $item->{text}; |
|
|
2665 | |
|
|
2666 | for ($short) { |
|
|
2667 | s/^\s+//; |
|
|
2668 | s/\012.*/…/s; |
|
|
2669 | my $len = int 40 / $item->{fontsize}; |
|
|
2670 | substr $_, $len, length, "…" if $len < length; |
|
|
2671 | } |
|
|
2672 | |
|
|
2673 | new CFClient::UI::Label |
|
|
2674 | markup => $short, |
|
|
2675 | tooltip => $item->{tooltip}, |
|
|
2676 | tooltip_font => $::FONT_PROP, |
|
|
2677 | tooltip_width => 0.67, |
|
|
2678 | fontsize => $item->{fontsize}, |
|
|
2679 | color => $item->{color}, |
|
|
2680 | can_events => 1, |
|
|
2681 | can_hover => 1 |
|
|
2682 | }; |
|
|
2683 | } |
|
|
2684 | |
|
|
2685 | $self->clear; |
|
|
2686 | $self->SUPER::add (reverse @widgets); |
|
|
2687 | } |
|
|
2688 | |
|
|
2689 | sub add { |
|
|
2690 | my ($self, $text, %arg) = @_; |
|
|
2691 | |
|
|
2692 | $text =~ s/^\s+//; |
|
|
2693 | $text =~ s/\s+$//; |
|
|
2694 | |
|
|
2695 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2696 | |
|
|
2697 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2698 | |
|
|
2699 | if (my $item = $self->{item}{$group}) { |
|
|
2700 | if ($item->{text} eq $text) { |
|
|
2701 | $item->{count}++; |
|
|
2702 | } else { |
|
|
2703 | $item->{count} = 1; |
|
|
2704 | $item->{text} = $item->{tooltip} = $text; |
|
|
2705 | } |
|
|
2706 | $item->{id} = ++$self->{id}; |
|
|
2707 | $item->{timeout} = $timeout; |
|
|
2708 | delete $item->{label}; |
|
|
2709 | } else { |
|
|
2710 | $self->{item}{$group} = { |
|
|
2711 | id => ++$self->{id}, |
|
|
2712 | text => $text, |
|
|
2713 | timeout => $timeout, |
|
|
2714 | tooltip => $text, |
|
|
2715 | fontsize => 0.8, |
|
|
2716 | color => [0.8, 0.8, 0.8, 0.8], |
|
|
2717 | pri => 0, |
|
|
2718 | count => 1, |
|
|
2719 | %arg, |
|
|
2720 | }; |
|
|
2721 | } |
|
|
2722 | |
|
|
2723 | $self->reorder; |
|
|
2724 | } |
|
|
2725 | |
|
|
2726 | ############################################################################# |
|
|
2727 | |
2680 | package CFClient::UI::Root; |
2728 | package CFClient::UI::Root; |
2681 | |
2729 | |
2682 | our @ISA = CFClient::UI::Container::; |
2730 | our @ISA = CFClient::UI::Container::; |
2683 | |
2731 | |
2684 | use CFClient::OpenGL; |
2732 | use CFClient::OpenGL; |
2685 | |
2733 | |
|
|
2734 | sub new { |
|
|
2735 | my $class = shift; |
|
|
2736 | |
|
|
2737 | $class->SUPER::new ( |
|
|
2738 | @_, |
|
|
2739 | ) |
|
|
2740 | } |
|
|
2741 | |
|
|
2742 | sub configure { |
|
|
2743 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2744 | |
|
|
2745 | $self->{w} = $w; |
|
|
2746 | $self->{h} = $h; |
|
|
2747 | } |
|
|
2748 | |
2686 | sub check_size { |
2749 | sub check_size { |
2687 | my ($self) = @_; |
2750 | my ($self) = @_; |
2688 | |
2751 | |
2689 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2752 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2753 | if $self->{w}; |
2690 | } |
2754 | } |
2691 | |
2755 | |
2692 | sub size_request { |
2756 | sub size_request { |
2693 | my ($self) = @_; |
2757 | my ($self) = @_; |
2694 | |
2758 | |
… | |
… | |
2702 | my $old_h = $self->{old_h}; |
2766 | my $old_h = $self->{old_h}; |
2703 | |
2767 | |
2704 | if ($old_w && $old_h) { |
2768 | if ($old_w && $old_h) { |
2705 | for my $child ($self->children) { |
2769 | for my $child ($self->children) { |
2706 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
2770 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
2707 | $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; |
2771 | $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}; |
2772 | $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}; |
2773 | $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; |
2774 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
2711 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
2775 | $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}; |
2776 | $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}; |
2777 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2714 | } |
2778 | } |
2715 | } |
2779 | } |
2716 | |
2780 | |
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) { |
2781 | for my $child ($self->children) { |
2727 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2782 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2728 | |
2783 | |
2729 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2784 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2730 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2785 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2731 | $child->configure ($X, $Y, $W, $H); |
2786 | $child->configure ($X, $Y, $W, $H); |
2732 | } |
2787 | } |
2733 | } |
|
|
2734 | |
2788 | |
2735 | sub _topleft { |
2789 | $self->{old_w} = $w; |
|
|
2790 | $self->{old_h} = $h; |
|
|
2791 | } |
|
|
2792 | |
|
|
2793 | sub coord2local { |
2736 | my ($self, $x, $y) = @_; |
2794 | my ($self, $x, $y) = @_; |
2737 | |
2795 | |
2738 | ($x, $y) |
2796 | ($x, $y) |
2739 | } |
2797 | } |
2740 | |
2798 | |
|
|
2799 | sub coord2global { |
|
|
2800 | my ($self, $x, $y) = @_; |
|
|
2801 | |
|
|
2802 | ($x, $y) |
|
|
2803 | } |
|
|
2804 | |
2741 | sub update { |
2805 | sub update { |
2742 | my ($self) = @_; |
2806 | my ($self) = @_; |
2743 | |
2807 | |
2744 | $self->check_size; |
2808 | $self->check_size; |
2745 | ::refresh (); |
2809 | $::WANT_REFRESH++; |
2746 | } |
2810 | } |
2747 | |
2811 | |
2748 | sub add { |
2812 | sub add { |
2749 | my ($self, $child) = @_; |
2813 | my ($self, $child) = @_; |
2750 | |
2814 | |
2751 | # integerize window positions |
2815 | # integerise window positions |
2752 | $child->{x} = int $child->{x}; |
2816 | $child->{x} = int $child->{x}; |
2753 | $child->{y} = int $child->{y}; |
2817 | $child->{y} = int $child->{y}; |
2754 | |
2818 | |
2755 | $self->SUPER::add ($child); |
2819 | $self->SUPER::add ($child); |
2756 | } |
2820 | } |
… | |
… | |
2759 | my ($self, $id, $cb) = @_; |
2823 | my ($self, $id, $cb) = @_; |
2760 | |
2824 | |
2761 | $self->{refresh_hook}{$id} = $cb; |
2825 | $self->{refresh_hook}{$id} = $cb; |
2762 | } |
2826 | } |
2763 | |
2827 | |
|
|
2828 | sub on_post_alloc { |
|
|
2829 | my ($self, $id, $cb) = @_; |
|
|
2830 | |
|
|
2831 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2832 | } |
|
|
2833 | |
2764 | sub draw { |
2834 | sub draw { |
2765 | my ($self) = @_; |
2835 | my ($self) = @_; |
2766 | |
2836 | |
2767 | while (my $rcb = delete $self->{refresh_hook}) { |
2837 | while ($self->{refresh_hook}) { |
2768 | $_->() for values %$rcb; |
2838 | $_->() |
|
|
2839 | for values %{delete $self->{refresh_hook}}; |
|
|
2840 | } |
|
|
2841 | |
|
|
2842 | if ($self->{check_size}) { |
|
|
2843 | my @queue = ([], []); |
|
|
2844 | |
|
|
2845 | for (;;) { |
|
|
2846 | if ($self->{check_size}) { |
|
|
2847 | # heuristic: check containers last |
|
|
2848 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2849 | for values %{delete $self->{check_size}} |
|
|
2850 | } |
|
|
2851 | |
|
|
2852 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2853 | |
|
|
2854 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2855 | ? @$widget{qw(user_w user_h)} |
|
|
2856 | : $widget->size_request; |
|
|
2857 | |
|
|
2858 | if (delete $widget->{force_alloc} |
|
|
2859 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
2860 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2861 | |
|
|
2862 | $widget->{req_w} = $w; |
|
|
2863 | $widget->{req_h} = $h; |
|
|
2864 | |
|
|
2865 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
|
|
2866 | |
|
|
2867 | $widget->{parent}->check_size |
|
|
2868 | if $widget->{parent}; |
|
|
2869 | } |
|
|
2870 | } |
|
|
2871 | } |
|
|
2872 | |
|
|
2873 | while ($self->{size_alloc}) { |
|
|
2874 | for (values %{delete $self->{size_alloc}}) { |
|
|
2875 | my ($widget, $w, $h) = @$_; |
|
|
2876 | |
|
|
2877 | $w = 0 if $w < 0; |
|
|
2878 | $h = 0 if $h < 0; |
|
|
2879 | |
|
|
2880 | $widget->{w} = $w; |
|
|
2881 | $widget->{h} = $h; |
|
|
2882 | $widget->size_allocate ($w, $h); |
|
|
2883 | $widget->emit (size_allocate => $w, $h); |
|
|
2884 | } |
|
|
2885 | } |
|
|
2886 | |
|
|
2887 | while ($self->{post_alloc_hook}) { |
|
|
2888 | $_->() |
|
|
2889 | for values %{delete $self->{post_alloc_hook}}; |
2769 | } |
2890 | } |
2770 | |
2891 | |
2771 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2892 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2772 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2893 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2773 | glClear GL_COLOR_BUFFER_BIT; |
2894 | glClear GL_COLOR_BUFFER_BIT; |