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->_check_size; |
396 | $self->{force_alloc} = 1 if $forced; |
409 | return; |
|
|
410 | my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {}; |
|
|
411 | |
|
|
412 | $check_size->{$self} = $self; |
|
|
413 | |
|
|
414 | $CFClient::UI::ROOT->on_refresh (_check_size => sub { |
|
|
415 | while (%$check_size) { |
|
|
416 | my @widgets = values %$check_size; |
|
|
417 | $_->_check_size |
|
|
418 | for @widgets; |
|
|
419 | } |
|
|
420 | }); |
|
|
421 | } |
|
|
422 | |
|
|
423 | sub _check_size { |
|
|
424 | my ($self) = @_; |
|
|
425 | |
|
|
426 | delete $CFClient::UI::ROOT->{_check_size}{$self}; |
397 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
427 | |
|
|
428 | $self->{parent} |
|
|
429 | or return 1; |
|
|
430 | |
|
|
431 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
432 | ? @$self{qw(user_w user_h)} |
|
|
433 | : $self->size_request; |
|
|
434 | |
|
|
435 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
436 | $self->{req_w} = $w; |
|
|
437 | $self->{req_h} = $h; |
|
|
438 | |
|
|
439 | $self->{parent}->_check_size |
|
|
440 | or $self->size_allocate ( |
|
|
441 | (List::Util::max $self->{w}, $w), |
|
|
442 | (List::Util::max $self->{h}, $h), |
|
|
443 | ); |
|
|
444 | |
|
|
445 | 1 |
|
|
446 | } else { |
|
|
447 | 0 |
|
|
448 | } |
|
|
449 | } |
398 | } |
450 | |
399 | |
451 | sub update { |
400 | sub update { |
452 | my ($self) = @_; |
401 | my ($self) = @_; |
453 | |
402 | |
454 | my $update = $CFClient::UI::ROOT->{_update} ||= {}; |
403 | $self->{parent}->update |
455 | |
404 | if $self->{parent}; |
456 | $update->{$self} = $self; |
|
|
457 | |
|
|
458 | $CFClient::UI::ROOT->on_refresh (_update => sub { |
|
|
459 | while (%$update) { |
|
|
460 | my @widgets = values %$update; |
|
|
461 | %$update = (); |
|
|
462 | |
|
|
463 | $_->{parent} && $_->{parent}->update |
|
|
464 | for @widgets; |
|
|
465 | } |
|
|
466 | }); |
|
|
467 | } |
405 | } |
468 | |
406 | |
469 | sub connect { |
407 | sub connect { |
470 | my ($self, $signal, $cb) = @_; |
408 | my ($self, $signal, $cb) = @_; |
471 | |
409 | |
… | |
… | |
573 | $self->{children} = [ |
511 | $self->{children} = [ |
574 | sort { $a->{z} <=> $b->{z} } |
512 | sort { $a->{z} <=> $b->{z} } |
575 | @{$self->{children}}, @widgets |
513 | @{$self->{children}}, @widgets |
576 | ]; |
514 | ]; |
577 | |
515 | |
578 | $_->check_size |
516 | $self->check_size (1); |
579 | for @widgets; |
|
|
580 | |
|
|
581 | $self->update; |
517 | $self->update; |
582 | } |
518 | } |
583 | |
519 | |
584 | sub children { |
520 | sub children { |
585 | @{ $_[0]{children} } |
521 | @{ $_[0]{children} } |
… | |
… | |
692 | } |
628 | } |
693 | |
629 | |
694 | sub update { |
630 | sub update { |
695 | my ($self) = @_; |
631 | my ($self) = @_; |
696 | |
632 | |
697 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
633 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
698 | $self->SUPER::update; |
634 | $self->SUPER::update; |
699 | } |
635 | } |
700 | |
636 | |
701 | sub size_allocate { |
637 | sub size_allocate { |
702 | my ($self, $w, $h) = @_; |
638 | my ($self, $w, $h) = @_; |
… | |
… | |
733 | my ($w, $h) = ($self->w, $self->h); |
669 | my ($w, $h) = ($self->w, $self->h); |
734 | |
670 | |
735 | my $tex = $self->{texture} |
671 | my $tex = $self->{texture} |
736 | or return; |
672 | or return; |
737 | |
673 | |
738 | glEnable GL_BLEND; |
|
|
739 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
740 | glEnable GL_TEXTURE_2D; |
674 | glEnable GL_TEXTURE_2D; |
741 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
675 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
742 | glColor 0, 0, 0, 1; |
676 | glColor 0, 0, 0, 1; |
743 | |
677 | |
744 | $tex->draw_quad (0, 0, $w, $h); |
678 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
745 | |
679 | |
746 | glDisable GL_BLEND; |
|
|
747 | glDisable GL_TEXTURE_2D; |
680 | glDisable GL_TEXTURE_2D; |
748 | } |
681 | } |
749 | |
682 | |
750 | ############################################################################# |
683 | ############################################################################# |
751 | |
684 | |
… | |
… | |
775 | $self->{view_y} = int $y; |
708 | $self->{view_y} = int $y; |
776 | |
709 | |
777 | $self->update; |
710 | $self->update; |
778 | } |
711 | } |
779 | |
712 | |
780 | # 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 |
781 | sub _topleft { |
714 | sub coord2local { |
782 | my ($self, $x, $y) = @_; |
715 | my ($self, $x, $y) = @_; |
783 | |
716 | |
784 | $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) |
785 | } |
727 | } |
786 | |
728 | |
787 | sub find_widget { |
729 | sub find_widget { |
788 | my ($self, $x, $y) = @_; |
730 | my ($self, $x, $y) = @_; |
789 | |
731 | |
… | |
… | |
834 | $self->add ($self->{slider}); |
776 | $self->add ($self->{slider}); |
835 | |
777 | |
836 | $self |
778 | $self |
837 | } |
779 | } |
838 | |
780 | |
839 | #TODO# update range on size_allocate depeneing on child |
781 | #TODO# update range on size_allocate depending on child |
840 | # update viewport offset on scroll |
782 | # update viewport offset on scroll |
841 | |
783 | |
842 | ############################################################################# |
784 | ############################################################################# |
843 | |
785 | |
844 | package CFClient::UI::Frame; |
786 | package CFClient::UI::Frame; |
… | |
… | |
848 | use CFClient::OpenGL; |
790 | use CFClient::OpenGL; |
849 | |
791 | |
850 | sub new { |
792 | sub new { |
851 | my $class = shift; |
793 | my $class = shift; |
852 | |
794 | |
853 | my $self = $class->SUPER::new ( |
795 | $class->SUPER::new ( |
854 | bg => [1, 1, 1, 1], |
796 | bg => undef, |
855 | border_bg => [1, 1, 1, 1], |
|
|
856 | border => 0.8, |
|
|
857 | @_ |
797 | @_, |
858 | ); |
798 | ) |
859 | |
|
|
860 | $self |
|
|
861 | } |
799 | } |
862 | |
800 | |
863 | sub _draw { |
801 | sub _draw { |
864 | my ($self) = @_; |
802 | my ($self) = @_; |
865 | |
803 | |
866 | my ($w, $h) = ($self->{w}, $self->{h}); |
804 | if ($self->{bg}) { |
|
|
805 | my ($w, $h) = @$self{qw(w h)}; |
867 | |
806 | |
868 | glEnable GL_BLEND; |
807 | glEnable GL_BLEND; |
869 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
808 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
870 | glEnable GL_TEXTURE_2D; |
809 | glColor @{ $self->{bg} }; |
871 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
872 | |
810 | |
873 | # glBegin GL_QUADS; |
811 | glBegin GL_QUADS; |
874 | # glColor 0, 0, 0, 0; |
|
|
875 | # glVertex 0 , 0; |
812 | glVertex 0 , 0; |
876 | # glVertex 0 , $h; |
813 | glVertex 0 , $h; |
877 | # glVertex $w, $h; |
814 | glVertex $w, $h; |
878 | # glVertex $w, 0; |
815 | glVertex $w, 0; |
879 | # glEnd; |
816 | glEnd; |
880 | |
817 | |
881 | |
|
|
882 | $self->child->draw; |
|
|
883 | glDisable GL_BLEND; |
818 | glDisable GL_BLEND; |
884 | glDisable GL_TEXTURE_2D; |
819 | } |
|
|
820 | |
|
|
821 | $self->SUPER::_draw; |
885 | } |
822 | } |
886 | |
823 | |
887 | ############################################################################# |
824 | ############################################################################# |
888 | |
825 | |
889 | package CFClient::UI::FancyFrame; |
826 | package CFClient::UI::FancyFrame; |
… | |
… | |
1005 | my ($self) = @_; |
942 | my ($self) = @_; |
1006 | |
943 | |
1007 | my ($w, $h ) = ($self->{w}, $self->{h}); |
944 | my ($w, $h ) = ($self->{w}, $self->{h}); |
1008 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
945 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
1009 | |
946 | |
1010 | glEnable GL_BLEND; |
|
|
1011 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1012 | glEnable GL_TEXTURE_2D; |
947 | glEnable GL_TEXTURE_2D; |
1013 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
948 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1014 | |
949 | |
1015 | my $border = $self->border; |
950 | my $border = $self->border; |
1016 | |
951 | |
1017 | glColor @{ $self->{border_bg} }; |
952 | glColor @{ $self->{border_bg} }; |
1018 | $tex[1]->draw_quad (0, 0, $w, $border); |
953 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
1019 | $tex[3]->draw_quad (0, $border, $border, $ch); |
954 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
1020 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
955 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1021 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
956 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
1022 | |
957 | |
1023 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
958 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1024 | my $bg = $tex[0]; |
959 | my $bg = $tex[0]; |
1025 | |
960 | |
1026 | # TODO: repeat texture not scale |
961 | # TODO: repeat texture not scale |
… | |
… | |
1030 | glColor @{ $self->{bg} }; |
965 | glColor @{ $self->{bg} }; |
1031 | |
966 | |
1032 | $bg->{s} = $rep_x; |
967 | $bg->{s} = $rep_x; |
1033 | $bg->{t} = $rep_y; |
968 | $bg->{t} = $rep_y; |
1034 | $bg->{wrap_mode} = 1; |
969 | $bg->{wrap_mode} = 1; |
1035 | $bg->draw_quad ($border, $border, $cw, $ch); |
970 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
971 | } |
1036 | |
972 | |
1037 | glDisable GL_TEXTURE_2D; |
973 | glDisable GL_TEXTURE_2D; |
1038 | glDisable GL_BLEND; |
|
|
1039 | } |
|
|
1040 | |
974 | |
1041 | $self->{title}->draw if $self->{title}; |
975 | $self->{title}->draw if $self->{title}; |
1042 | |
976 | |
1043 | $self->child->draw; |
977 | $self->child->draw; |
1044 | } |
978 | } |
… | |
… | |
1271 | } |
1205 | } |
1272 | |
1206 | |
1273 | sub size_allocate { |
1207 | sub size_allocate { |
1274 | my ($self, $w, $h) = @_; |
1208 | my ($self, $w, $h) = @_; |
1275 | |
1209 | |
|
|
1210 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
|
|
1211 | |
1276 | my $children = $self->{children}; |
1212 | my $children = $self->{children}; |
1277 | |
1213 | |
1278 | my @h = map $_->{req_h}, @$children; |
1214 | my @h = map $_->{req_h}, @$children; |
1279 | |
1215 | |
1280 | my $req_h = List::Util::sum @h; |
1216 | my $req_h = List::Util::sum @h; |
… | |
… | |
1319 | my ($class, %arg) = @_; |
1255 | my ($class, %arg) = @_; |
1320 | |
1256 | |
1321 | my $self = $class->SUPER::new ( |
1257 | my $self = $class->SUPER::new ( |
1322 | fg => [1, 1, 1], |
1258 | fg => [1, 1, 1], |
1323 | #font => default_font |
1259 | #font => default_font |
|
|
1260 | #text => initial text |
|
|
1261 | #markup => initial narkup |
|
|
1262 | layout => (new CFClient::Layout), |
1324 | fontsize => 1, |
1263 | fontsize => 1, |
1325 | text => "", |
|
|
1326 | align => -1, |
1264 | align => -1, |
1327 | valign => -1, |
1265 | valign => -1, |
1328 | padding => 2, |
1266 | padding => 2, |
1329 | layout => new CFClient::Layout, |
|
|
1330 | can_events => 0, |
1267 | can_events => 0, |
1331 | %arg |
1268 | %arg |
1332 | ); |
1269 | ); |
1333 | |
1270 | |
1334 | if (exists $self->{template}) { |
1271 | if (exists $self->{template}) { |
1335 | my $layout = new CFClient::Layout; |
1272 | my $layout = new CFClient::Layout; |
1336 | $layout->set_text (delete $self->{template}); |
1273 | $layout->set_text (delete $self->{template}); |
1337 | $self->{template} = $layout; |
1274 | $self->{template} = $layout; |
1338 | } |
1275 | } |
1339 | |
1276 | |
1340 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1277 | if (exists $self->{markup}) { |
1341 | $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 | } |
1342 | |
1282 | |
1343 | $self |
1283 | $self |
1344 | } |
1284 | } |
1345 | |
1285 | |
1346 | sub escape { |
1286 | sub escape { |
… | |
… | |
1364 | my ($self, $text) = @_; |
1304 | my ($self, $text) = @_; |
1365 | |
1305 | |
1366 | return if $self->{text} eq "T$text"; |
1306 | return if $self->{text} eq "T$text"; |
1367 | $self->{text} = "T$text"; |
1307 | $self->{text} = "T$text"; |
1368 | |
1308 | |
|
|
1309 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1369 | $self->{layout}->set_text ($text); |
1310 | $self->{layout}->set_text ($text); |
1370 | |
1311 | |
1371 | delete $self->{texture}; |
|
|
1372 | $self->update; |
1312 | $self->update; |
1373 | $self->check_size; |
1313 | $self->check_size; |
1374 | } |
1314 | } |
1375 | |
1315 | |
1376 | sub set_markup { |
1316 | sub set_markup { |
1377 | my ($self, $markup) = @_; |
1317 | my ($self, $markup) = @_; |
1378 | |
1318 | |
1379 | return if $self->{text} eq "M$markup"; |
1319 | return if $self->{text} eq "M$markup"; |
1380 | $self->{text} = "M$markup"; |
1320 | $self->{text} = "M$markup"; |
1381 | |
1321 | |
|
|
1322 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
|
|
1323 | |
|
|
1324 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1382 | $self->{layout}->set_markup ($markup); |
1325 | $self->{layout}->set_markup ($markup); |
1383 | |
1326 | |
1384 | delete $self->{texture}; |
|
|
1385 | $self->update; |
1327 | $self->update; |
1386 | $self->check_size; |
1328 | $self->check_size; |
1387 | } |
1329 | } |
1388 | |
1330 | |
1389 | sub size_request { |
1331 | sub size_request { |
… | |
… | |
1429 | |
1371 | |
1430 | sub _draw { |
1372 | sub _draw { |
1431 | my ($self) = @_; |
1373 | my ($self) = @_; |
1432 | |
1374 | |
1433 | my $tex = $self->{texture} ||= do { |
1375 | my $tex = $self->{texture} ||= do { |
|
|
1376 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1434 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1377 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1435 | $self->{layout}->set_width ($self->{w}); |
1378 | $self->{layout}->set_width ($self->{w}); |
1436 | $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 | |
1437 | 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 |
1438 | }; |
1392 | }; |
1439 | |
1393 | |
1440 | glEnable GL_BLEND; |
|
|
1441 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1442 | glEnable GL_TEXTURE_2D; |
1394 | glEnable GL_TEXTURE_2D; |
1443 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1395 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1444 | |
1396 | |
|
|
1397 | if ($tex->{format} == GL_ALPHA) { |
1445 | glColor @{$self->{fg}}; |
1398 | glColor @{$self->{fg}}; |
1446 | |
|
|
1447 | $self->{ox} = int ( |
|
|
1448 | $self->{align} < 0 ? $self->{padding} |
|
|
1449 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1450 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1451 | ); |
|
|
1452 | |
|
|
1453 | $self->{oy} = int ( |
|
|
1454 | $self->{valign} < 0 ? $self->{padding} |
|
|
1455 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1456 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1457 | ); |
|
|
1458 | |
|
|
1459 | $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 | } |
1460 | |
1403 | |
1461 | glDisable GL_TEXTURE_2D; |
1404 | glDisable GL_TEXTURE_2D; |
1462 | glDisable GL_BLEND; |
|
|
1463 | } |
1405 | } |
1464 | |
1406 | |
1465 | ############################################################################# |
1407 | ############################################################################# |
1466 | |
1408 | |
1467 | package CFClient::UI::EntryBase; |
1409 | package CFClient::UI::EntryBase; |
… | |
… | |
1502 | $self->{layout}->set_text ("$text "); |
1444 | $self->{layout}->set_text ("$text "); |
1503 | |
1445 | |
1504 | $self->emit (changed => $self->{text}); |
1446 | $self->emit (changed => $self->{text}); |
1505 | } |
1447 | } |
1506 | |
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 | |
1507 | sub get_text { |
1458 | sub get_text { |
1508 | $_[0]{text} |
1459 | $_[0]{text} |
1509 | } |
1460 | } |
1510 | |
1461 | |
1511 | sub size_request { |
1462 | sub size_request { |
… | |
… | |
1518 | |
1469 | |
1519 | sub size_allocate { |
1470 | sub size_allocate { |
1520 | my ($self, $w, $h) = @_; |
1471 | my ($self, $w, $h) = @_; |
1521 | |
1472 | |
1522 | $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 |
1523 | } |
|
|
1524 | |
|
|
1525 | sub set_text { |
|
|
1526 | my ($self, $text) = @_; |
|
|
1527 | |
|
|
1528 | $self->{cursor} = length $text; |
|
|
1529 | $self->_set_text ($text); |
|
|
1530 | $self->update; |
|
|
1531 | } |
1474 | } |
1532 | |
1475 | |
1533 | sub key_down { |
1476 | sub key_down { |
1534 | my ($self, $ev) = @_; |
1477 | my ($self, $ev) = @_; |
1535 | |
1478 | |
… | |
… | |
1537 | my $sym = $ev->{sym}; |
1480 | my $sym = $ev->{sym}; |
1538 | my $uni = $ev->{unicode}; |
1481 | my $uni = $ev->{unicode}; |
1539 | |
1482 | |
1540 | my $text = $self->get_text; |
1483 | my $text = $self->get_text; |
1541 | |
1484 | |
1542 | if ($sym == 8) { |
1485 | if ($uni == 8) { |
1543 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1486 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1544 | } elsif ($sym == 127) { |
1487 | } elsif ($uni == 127) { |
1545 | substr $text, $self->{cursor}, 1, ""; |
1488 | substr $text, $self->{cursor}, 1, ""; |
1546 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1489 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1547 | --$self->{cursor} if $self->{cursor}; |
1490 | --$self->{cursor} if $self->{cursor}; |
1548 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1491 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1549 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1492 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1550 | } elsif ($sym == CFClient::SDLK_HOME) { |
1493 | } elsif ($sym == CFClient::SDLK_HOME) { |
1551 | $self->{cursor} = 0; |
1494 | $self->{cursor} = 0; |
1552 | } elsif ($sym == CFClient::SDLK_END) { |
1495 | } elsif ($sym == CFClient::SDLK_END) { |
1553 | $self->{cursor} = length $text; |
1496 | $self->{cursor} = length $text; |
1554 | } elsif ($sym == 27) { |
1497 | } elsif ($uni == 27) { |
1555 | $self->emit ('escape'); |
1498 | $self->emit ('escape'); |
1556 | } elsif ($uni) { |
1499 | } elsif ($uni) { |
1557 | substr $text, $self->{cursor}++, 0, chr $uni; |
1500 | substr $text, $self->{cursor}++, 0, chr $uni; |
1558 | } |
1501 | } |
1559 | |
1502 | |
… | |
… | |
1723 | |
1666 | |
1724 | if ($GRAB == $self) { |
1667 | if ($GRAB == $self) { |
1725 | $self->{fg} = $self->{active_fg}; |
1668 | $self->{fg} = $self->{active_fg}; |
1726 | } |
1669 | } |
1727 | |
1670 | |
1728 | glEnable GL_BLEND; |
|
|
1729 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1730 | glEnable GL_TEXTURE_2D; |
1671 | glEnable GL_TEXTURE_2D; |
1731 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1672 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1732 | glColor 0, 0, 0, 1; |
1673 | glColor 0, 0, 0, 1; |
1733 | |
1674 | |
1734 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1675 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1735 | |
1676 | |
1736 | glDisable GL_TEXTURE_2D; |
1677 | glDisable GL_TEXTURE_2D; |
1737 | glDisable GL_BLEND; |
|
|
1738 | |
1678 | |
1739 | $self->SUPER::_draw; |
1679 | $self->SUPER::_draw; |
1740 | } |
1680 | } |
1741 | |
1681 | |
1742 | ############################################################################# |
1682 | ############################################################################# |
… | |
… | |
1789 | |
1729 | |
1790 | 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; |
1791 | |
1731 | |
1792 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1732 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1793 | |
1733 | |
1794 | glEnable GL_BLEND; |
1734 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1735 | |
1795 | glEnable GL_TEXTURE_2D; |
1736 | glEnable GL_TEXTURE_2D; |
1796 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1797 | |
|
|
1798 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1799 | |
|
|
1800 | $tex->draw_quad (0, 0, $s, $s); |
1737 | $tex->draw_quad_alpha (0, 0, $s, $s); |
1801 | |
|
|
1802 | glDisable GL_TEXTURE_2D; |
1738 | glDisable GL_TEXTURE_2D; |
1803 | glDisable GL_BLEND; |
|
|
1804 | } |
1739 | } |
1805 | |
1740 | |
1806 | ############################################################################# |
1741 | ############################################################################# |
1807 | |
1742 | |
1808 | package CFClient::UI::Image; |
1743 | package CFClient::UI::Image; |
… | |
… | |
1851 | glTranslate 0, -$self->{w}, 0; |
1786 | glTranslate 0, -$self->{w}, 0; |
1852 | |
1787 | |
1853 | ($w, $h) = ($h, $w); |
1788 | ($w, $h) = ($h, $w); |
1854 | } |
1789 | } |
1855 | |
1790 | |
1856 | glEnable GL_BLEND; |
|
|
1857 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1858 | glEnable GL_TEXTURE_2D; |
1791 | glEnable GL_TEXTURE_2D; |
1859 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1792 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1860 | |
1793 | |
1861 | $tex->draw_quad (0, 0, $w, $h); |
1794 | $tex->draw_quad_alpha (0, 0, $w, $h); |
1862 | |
1795 | |
1863 | glDisable GL_BLEND; |
|
|
1864 | glDisable GL_TEXTURE_2D; |
1796 | glDisable GL_TEXTURE_2D; |
1865 | } |
1797 | } |
1866 | |
1798 | |
1867 | ############################################################################# |
1799 | ############################################################################# |
1868 | |
1800 | |
… | |
… | |
2160 | $page ||= 2; |
2092 | $page ||= 2; |
2161 | |
2093 | |
2162 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2094 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2163 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2095 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2164 | |
2096 | |
2165 | glEnable GL_BLEND; |
|
|
2166 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2167 | glEnable GL_TEXTURE_2D; |
2097 | glEnable GL_TEXTURE_2D; |
2168 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2098 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2169 | |
2099 | |
2170 | # draw background |
2100 | # draw background |
2171 | $tex[1]->draw_quad (0, 0, $w, $h); |
2101 | $tex[1]->draw_quad_alpha (0, 0, $w, $h); |
2172 | |
2102 | |
2173 | # draw handle |
2103 | # draw handle |
2174 | $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); |
2175 | |
2105 | |
2176 | glDisable GL_BLEND; |
|
|
2177 | glDisable GL_TEXTURE_2D; |
2106 | glDisable GL_TEXTURE_2D; |
2178 | } |
2107 | } |
2179 | |
2108 | |
2180 | ############################################################################# |
2109 | ############################################################################# |
2181 | |
2110 | |
… | |
… | |
2192 | fontsize => 1, |
2121 | fontsize => 1, |
2193 | can_events => 0, |
2122 | can_events => 0, |
2194 | #font => default_font |
2123 | #font => default_font |
2195 | @_, |
2124 | @_, |
2196 | |
2125 | |
2197 | layout => (new CFClient::Layout), |
2126 | layout => (new CFClient::Layout 1), |
2198 | par => [], |
2127 | par => [], |
2199 | height => 0, |
2128 | height => 0, |
2200 | children => [ |
2129 | children => [ |
2201 | (new CFClient::UI::Empty expand => 1), |
2130 | (new CFClient::UI::Empty expand => 1), |
2202 | (new CFClient::UI::Slider vertical => 1), |
2131 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2220 | |
2149 | |
2221 | my $layout = $self->{layout}; |
2150 | my $layout = $self->{layout}; |
2222 | |
2151 | |
2223 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2152 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2224 | $layout->set_width ($self->{children}[0]{w}); |
2153 | $layout->set_width ($self->{children}[0]{w}); |
2225 | $layout->set_text ($text); |
2154 | $layout->set_markup ($text); |
2226 | |
2155 | |
2227 | ($layout->size)[1] |
2156 | ($layout->size)[1] |
2228 | } |
2157 | } |
2229 | |
2158 | |
2230 | sub reflow { |
2159 | sub reflow { |
… | |
… | |
2268 | |
2197 | |
2269 | return unless $self->{h} > 0; |
2198 | return unless $self->{h} > 0; |
2270 | |
2199 | |
2271 | delete $self->{texture}; |
2200 | delete $self->{texture}; |
2272 | |
2201 | |
2273 | $ROOT->on_refresh ($self, sub { |
2202 | $ROOT->on_post_alloc ($self, sub { |
2274 | if (delete $self->{need_reflow}) { |
2203 | if (delete $self->{need_reflow}) { |
2275 | my $height = 0; |
2204 | my $height = 0; |
2276 | |
2205 | |
2277 | $height += $_->[0] = $self->text_height ($_->[2]) |
2206 | $height += $_->[0] = $self->text_height ($_->[2]) |
2278 | for @{$self->{par}}; |
2207 | for @{$self->{par}}; |
… | |
… | |
2286 | |
2215 | |
2287 | $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 { |
2288 | glClearColor 0, 0, 0, 0; |
2217 | glClearColor 0, 0, 0, 0; |
2289 | glClear GL_COLOR_BUFFER_BIT; |
2218 | glClear GL_COLOR_BUFFER_BIT; |
2290 | |
2219 | |
2291 | glEnable GL_BLEND; |
|
|
2292 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2293 | glEnable GL_TEXTURE_2D; |
2220 | glEnable GL_TEXTURE_2D; |
2294 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2221 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2295 | |
2222 | |
2296 | my $top = int $self->{children}[1]{range}[0]; |
2223 | my $top = int $self->{children}[1]{range}[0]; |
2297 | |
2224 | |
… | |
… | |
2306 | |
2233 | |
2307 | for my $par (@{$self->{par}}) { |
2234 | for my $par (@{$self->{par}}) { |
2308 | my $h = $par->[0]; |
2235 | my $h = $par->[0]; |
2309 | |
2236 | |
2310 | if ($y0 < $y + $h && $y < $y1) { |
2237 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2238 | $layout->set_foreground (@{ $par->[1] }); |
2311 | $layout->set_text ($par->[2]); |
2239 | $layout->set_markup ($par->[2]); |
2312 | |
2240 | |
2313 | glColor @{ $par->[1] }; |
|
|
2314 | my ($W, $H) = $layout->size; |
2241 | my ($W, $H) = $layout->size; |
2315 | 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); |
2316 | } |
2243 | } |
2317 | |
2244 | |
2318 | $y += $h; |
2245 | $y += $h; |
2319 | } |
2246 | } |
2320 | |
2247 | |
2321 | glDisable GL_TEXTURE_2D; |
2248 | glDisable GL_TEXTURE_2D; |
2322 | glDisable GL_BLEND; |
|
|
2323 | }; |
2249 | }; |
2324 | }); |
2250 | }); |
2325 | } |
2251 | } |
2326 | |
2252 | |
2327 | sub _draw { |
2253 | sub _draw { |
2328 | my ($self) = @_; |
2254 | my ($self) = @_; |
2329 | |
2255 | |
2330 | glEnable GL_BLEND; |
|
|
2331 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2332 | glEnable GL_TEXTURE_2D; |
2256 | glEnable GL_TEXTURE_2D; |
2333 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2257 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2334 | glColor 1, 1, 1, 1; |
2258 | glColor 1, 1, 1, 1; |
2335 | $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}); |
2336 | glDisable GL_TEXTURE_2D; |
2260 | glDisable GL_TEXTURE_2D; |
2337 | glDisable GL_BLEND; |
|
|
2338 | |
2261 | |
2339 | $self->{children}[1]->draw; |
2262 | $self->{children}[1]->draw; |
2340 | |
2263 | |
2341 | } |
2264 | } |
2342 | |
2265 | |
… | |
… | |
2436 | @_, |
2359 | @_, |
2437 | can_events => 0, |
2360 | can_events => 0, |
2438 | ) |
2361 | ) |
2439 | } |
2362 | } |
2440 | |
2363 | |
2441 | sub set_markup { |
2364 | sub set_tooltip_from { |
2442 | my ($self, $text) = @_; |
2365 | my ($self, $widget) = @_; |
2443 | |
2366 | |
2444 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2367 | $self->add (new CFClient::UI::Label |
2445 | $self->{label}->set_markup ($text); |
2368 | markup => $widget->{tooltip}, |
2446 | $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 | ); |
2447 | } |
2374 | } |
2448 | |
2375 | |
2449 | sub size_request { |
2376 | sub size_request { |
2450 | my ($self) = @_; |
2377 | my ($self) = @_; |
2451 | |
|
|
2452 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2453 | |
2378 | |
2454 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2379 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2455 | |
2380 | |
2456 | ($w + 4, $h + 4) |
2381 | ($w + 4, $h + 4) |
2457 | } |
2382 | } |
… | |
… | |
2463 | } |
2388 | } |
2464 | |
2389 | |
2465 | sub _draw { |
2390 | sub _draw { |
2466 | my ($self) = @_; |
2391 | my ($self) = @_; |
2467 | |
2392 | |
2468 | glPushMatrix; |
|
|
2469 | glTranslate 0.375, 0.375; |
2393 | glTranslate 0.375, 0.375; |
2470 | |
2394 | |
2471 | my ($w, $h) = @$self{qw(w h)}; |
2395 | my ($w, $h) = @$self{qw(w h)}; |
2472 | |
2396 | |
2473 | glColor 1, 0.8, 0.4; |
2397 | glColor 1, 0.8, 0.4; |
… | |
… | |
2484 | glVertex 0 , $h; |
2408 | glVertex 0 , $h; |
2485 | glVertex $w, $h; |
2409 | glVertex $w, $h; |
2486 | glVertex $w, 0; |
2410 | glVertex $w, 0; |
2487 | glEnd; |
2411 | glEnd; |
2488 | |
2412 | |
2489 | glPopMatrix; |
2413 | glTranslate 2 - 0.375, 2 - 0.375; |
2490 | |
|
|
2491 | glTranslate 2, 2; |
|
|
2492 | $self->SUPER::_draw; |
2414 | $self->SUPER::_draw; |
2493 | } |
2415 | } |
2494 | |
2416 | |
2495 | ############################################################################# |
2417 | ############################################################################# |
2496 | |
2418 | |
… | |
… | |
2519 | return unless $::CONN;#d# manage and cache textures differently |
2441 | return unless $::CONN;#d# manage and cache textures differently |
2520 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2442 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2521 | |
2443 | |
2522 | # TODO animation |
2444 | # TODO animation |
2523 | if ($tex) { |
2445 | if ($tex) { |
2524 | glEnable GL_BLEND; |
|
|
2525 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2526 | glEnable GL_TEXTURE_2D; |
2446 | glEnable GL_TEXTURE_2D; |
2527 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2528 | glColor 1, 1, 1, 1; |
2448 | glColor 1, 1, 1, 1; |
2529 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2449 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2530 | glDisable GL_TEXTURE_2D; |
2450 | glDisable GL_TEXTURE_2D; |
2531 | glDisable GL_BLEND; |
|
|
2532 | } |
2451 | } |
2533 | } |
2452 | } |
2534 | |
2453 | |
2535 | ############################################################################# |
2454 | ############################################################################# |
2536 | |
2455 | |
… | |
… | |
2541 | sub new { |
2460 | sub new { |
2542 | my $class = shift; |
2461 | my $class = shift; |
2543 | |
2462 | |
2544 | my %args = @_; |
2463 | my %args = @_; |
2545 | |
2464 | |
2546 | my $item = $args{item}; |
2465 | my $item = delete $args{item}; |
2547 | |
2466 | |
2548 | my $desc = $item->{nrof} < 2 |
2467 | my $desc = $item->{nrof} < 2 |
2549 | ? $item->{name} |
2468 | ? $item->{name} |
2550 | : "$item->{nrof} $item->{name_pl}"; |
2469 | : "$item->{nrof} $item->{name_pl}"; |
2551 | |
2470 | |
… | |
… | |
2580 | |
2499 | |
2581 | 1 |
2500 | 1 |
2582 | }, |
2501 | }, |
2583 | %args |
2502 | %args |
2584 | ); |
2503 | ); |
|
|
2504 | |
2585 | $self->add(new CFClient::UI::Face |
2505 | $self->add (new CFClient::UI::Face |
2586 | can_events => 0, |
2506 | can_events => 0, |
2587 | face => $item->{face}, |
2507 | face => $item->{face}, |
2588 | anim => $item->{anim}, |
2508 | anim => $item->{anim}, |
2589 | animspeed => $item->{animspeed}); |
2509 | animspeed => $item->{animspeed}, |
|
|
2510 | ); |
|
|
2511 | |
2590 | $self->add(new CFClient::UI::Label |
2512 | $self->add (new CFClient::UI::Label |
2591 | can_events => 0, |
2513 | can_events => 0, |
2592 | text => $desc); |
2514 | text => $desc, |
|
|
2515 | ); |
2593 | |
2516 | |
2594 | $self |
2517 | $self |
2595 | } |
2518 | } |
2596 | |
2519 | |
2597 | ############################################################################# |
2520 | ############################################################################# |
… | |
… | |
2615 | my ($self, $items) = @_; |
2538 | my ($self, $items) = @_; |
2616 | |
2539 | |
2617 | $self->{scrolled}->clear; |
2540 | $self->{scrolled}->clear; |
2618 | return unless $items; |
2541 | return unless $items; |
2619 | |
2542 | |
2620 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
2543 | my @items = sort { |
|
|
2544 | ($a->{type} <=> $b->{type}) |
|
|
2545 | or ($a->{name} cmp $b->{name}) |
|
|
2546 | } @$items; |
2621 | |
2547 | |
2622 | $self->{real_items} = \@items; |
2548 | $self->{real_items} = \@items; |
2623 | |
2549 | |
2624 | for my $item (@items) { |
2550 | for my $item (@items) { |
2625 | my $desc = $item->{nrof} < 2 |
2551 | my $desc = $item->{nrof} < 2 |
2626 | ? $item->{name} |
2552 | ? $item->{name} |
2627 | : "$item->{nrof} $item->{name_pl}"; |
2553 | : "$item->{nrof} $item->{name_pl}"; |
2628 | |
2554 | |
2629 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
2555 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2630 | } |
2556 | } |
|
|
2557 | |
|
|
2558 | $self->{scrolled}->add (@items); |
2631 | |
2559 | |
2632 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2560 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2633 | } |
2561 | } |
2634 | |
2562 | |
2635 | sub size_request { |
2563 | sub size_request { |
… | |
… | |
2709 | } |
2637 | } |
2710 | } |
2638 | } |
2711 | |
2639 | |
2712 | ############################################################################# |
2640 | ############################################################################# |
2713 | |
2641 | |
|
|
2642 | package CFClient::UI::Statusbox; |
|
|
2643 | |
|
|
2644 | our @ISA = CFClient::UI::VBox::; |
|
|
2645 | |
|
|
2646 | sub reorder { |
|
|
2647 | my ($self) = @_; |
|
|
2648 | my $NOW = time; |
|
|
2649 | |
|
|
2650 | while (my ($k, $v) = each %{ $self->{item} }) { |
|
|
2651 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
|
|
2652 | } |
|
|
2653 | |
|
|
2654 | my @widgets; |
|
|
2655 | |
|
|
2656 | my @items = sort { |
|
|
2657 | $a->{pri} <=> $b->{pri} |
|
|
2658 | or $b->{id} <=> $a->{id} |
|
|
2659 | } values %{ $self->{item} }; |
|
|
2660 | |
|
|
2661 | my $count = 10 + 1; |
|
|
2662 | for my $item (@items) { |
|
|
2663 | last unless --$count; |
|
|
2664 | |
|
|
2665 | push @widgets, $item->{label} ||= do { |
|
|
2666 | # TODO: doesn't handle markup well (read as: at all) |
|
|
2667 | my $short = $item->{count} > 1 |
|
|
2668 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2669 | : $item->{text}; |
|
|
2670 | |
|
|
2671 | for ($short) { |
|
|
2672 | s/^\s+//; |
|
|
2673 | s/\012.*/…/s; |
|
|
2674 | my $len = int 40 / $item->{fontsize}; |
|
|
2675 | substr $_, $len, length, "…" if $len < length; |
|
|
2676 | } |
|
|
2677 | |
|
|
2678 | new CFClient::UI::Label |
|
|
2679 | markup => $short, |
|
|
2680 | tooltip => $item->{tooltip}, |
|
|
2681 | tooltip_font => $::FONT_PROP, |
|
|
2682 | tooltip_width => 0.67, |
|
|
2683 | fontsize => $item->{fontsize}, |
|
|
2684 | color => $item->{color}, |
|
|
2685 | can_events => 1, |
|
|
2686 | can_hover => 1 |
|
|
2687 | }; |
|
|
2688 | } |
|
|
2689 | |
|
|
2690 | $self->clear; |
|
|
2691 | $self->SUPER::add (reverse @widgets); |
|
|
2692 | } |
|
|
2693 | |
|
|
2694 | sub add { |
|
|
2695 | my ($self, $text, %arg) = @_; |
|
|
2696 | |
|
|
2697 | $text =~ s/^\s+//; |
|
|
2698 | $text =~ s/\s+$//; |
|
|
2699 | |
|
|
2700 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2701 | |
|
|
2702 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2703 | |
|
|
2704 | if (my $item = $self->{item}{$group}) { |
|
|
2705 | if ($item->{text} eq $text) { |
|
|
2706 | $item->{count}++; |
|
|
2707 | } else { |
|
|
2708 | $item->{count} = 1; |
|
|
2709 | $item->{text} = $item->{tooltip} = $text; |
|
|
2710 | } |
|
|
2711 | $item->{id} = ++$self->{id}; |
|
|
2712 | $item->{timeout} = $timeout; |
|
|
2713 | delete $item->{label}; |
|
|
2714 | } else { |
|
|
2715 | $self->{item}{$group} = { |
|
|
2716 | id => ++$self->{id}, |
|
|
2717 | text => $text, |
|
|
2718 | timeout => $timeout, |
|
|
2719 | tooltip => $text, |
|
|
2720 | fontsize => 0.8, |
|
|
2721 | color => [0.8, 0.8, 0.8, 0.8], |
|
|
2722 | pri => 0, |
|
|
2723 | count => 1, |
|
|
2724 | %arg, |
|
|
2725 | }; |
|
|
2726 | } |
|
|
2727 | |
|
|
2728 | $self->reorder; |
|
|
2729 | } |
|
|
2730 | |
|
|
2731 | ############################################################################# |
|
|
2732 | |
2714 | package CFClient::UI::Root; |
2733 | package CFClient::UI::Root; |
2715 | |
2734 | |
2716 | our @ISA = CFClient::UI::Container::; |
2735 | our @ISA = CFClient::UI::Container::; |
2717 | |
2736 | |
2718 | use CFClient::OpenGL; |
2737 | use CFClient::OpenGL; |
2719 | |
2738 | |
|
|
2739 | sub new { |
|
|
2740 | my $class = shift; |
|
|
2741 | |
|
|
2742 | $class->SUPER::new ( |
|
|
2743 | @_, |
|
|
2744 | ) |
|
|
2745 | } |
|
|
2746 | |
|
|
2747 | sub configure { |
|
|
2748 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2749 | |
|
|
2750 | $self->{w} = $w; |
|
|
2751 | $self->{h} = $h; |
|
|
2752 | } |
|
|
2753 | |
2720 | sub check_size { |
2754 | sub check_size { |
2721 | my ($self) = @_; |
2755 | my ($self) = @_; |
2722 | |
2756 | |
2723 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2757 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2758 | if $self->{w}; |
2724 | } |
2759 | } |
2725 | |
2760 | |
2726 | sub size_request { |
2761 | sub size_request { |
2727 | my ($self) = @_; |
2762 | my ($self) = @_; |
2728 | |
2763 | |
… | |
… | |
2746 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2781 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2747 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2782 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2748 | } |
2783 | } |
2749 | } |
2784 | } |
2750 | |
2785 | |
2751 | $self->{old_w} = $w; |
|
|
2752 | $self->{old_h} = $h; |
|
|
2753 | } |
|
|
2754 | |
|
|
2755 | sub configure { |
|
|
2756 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2757 | |
|
|
2758 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2759 | |
|
|
2760 | for my $child ($self->children) { |
2786 | for my $child ($self->children) { |
2761 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2787 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2762 | |
2788 | |
2763 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2789 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2764 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2790 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2765 | $child->configure ($X, $Y, $W, $H); |
2791 | $child->configure ($X, $Y, $W, $H); |
2766 | } |
2792 | } |
2767 | } |
|
|
2768 | |
2793 | |
2769 | sub _topleft { |
2794 | $self->{old_w} = $w; |
|
|
2795 | $self->{old_h} = $h; |
|
|
2796 | } |
|
|
2797 | |
|
|
2798 | sub coord2local { |
2770 | my ($self, $x, $y) = @_; |
2799 | my ($self, $x, $y) = @_; |
2771 | |
2800 | |
2772 | ($x, $y) |
2801 | ($x, $y) |
2773 | } |
2802 | } |
2774 | |
2803 | |
|
|
2804 | sub coord2global { |
|
|
2805 | my ($self, $x, $y) = @_; |
|
|
2806 | |
|
|
2807 | ($x, $y) |
|
|
2808 | } |
|
|
2809 | |
2775 | sub update { |
2810 | sub update { |
2776 | my ($self) = @_; |
2811 | my ($self) = @_; |
2777 | |
2812 | |
2778 | $self->check_size; |
2813 | $self->check_size; |
2779 | ::refresh (); |
2814 | $::WANT_REFRESH++; |
2780 | } |
2815 | } |
2781 | |
2816 | |
2782 | sub add { |
2817 | sub add { |
2783 | my ($self, $child) = @_; |
2818 | my ($self, $child) = @_; |
2784 | |
2819 | |
2785 | # integerize window positions |
2820 | # integerise window positions |
2786 | $child->{x} = int $child->{x}; |
2821 | $child->{x} = int $child->{x}; |
2787 | $child->{y} = int $child->{y}; |
2822 | $child->{y} = int $child->{y}; |
2788 | |
2823 | |
2789 | $self->SUPER::add ($child); |
2824 | $self->SUPER::add ($child); |
2790 | } |
2825 | } |
… | |
… | |
2793 | my ($self, $id, $cb) = @_; |
2828 | my ($self, $id, $cb) = @_; |
2794 | |
2829 | |
2795 | $self->{refresh_hook}{$id} = $cb; |
2830 | $self->{refresh_hook}{$id} = $cb; |
2796 | } |
2831 | } |
2797 | |
2832 | |
|
|
2833 | sub on_post_alloc { |
|
|
2834 | my ($self, $id, $cb) = @_; |
|
|
2835 | |
|
|
2836 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2837 | } |
|
|
2838 | |
2798 | sub draw { |
2839 | sub draw { |
2799 | my ($self) = @_; |
2840 | my ($self) = @_; |
2800 | |
2841 | |
2801 | while (my $rcb = delete $self->{refresh_hook}) { |
2842 | while ($self->{refresh_hook}) { |
2802 | $_->() for values %$rcb; |
2843 | $_->() |
|
|
2844 | for values %{delete $self->{refresh_hook}}; |
|
|
2845 | } |
|
|
2846 | |
|
|
2847 | if ($self->{check_size}) { |
|
|
2848 | my @queue = ([], []); |
|
|
2849 | |
|
|
2850 | for (;;) { |
|
|
2851 | if ($self->{check_size}) { |
|
|
2852 | # heuristic: check containers last |
|
|
2853 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2854 | for values %{delete $self->{check_size}} |
|
|
2855 | } |
|
|
2856 | |
|
|
2857 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2858 | |
|
|
2859 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2860 | ? @$widget{qw(user_w user_h)} |
|
|
2861 | : $widget->size_request; |
|
|
2862 | |
|
|
2863 | if (delete $widget->{force_alloc} |
|
|
2864 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
2865 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2866 | |
|
|
2867 | $widget->{req_w} = $w; |
|
|
2868 | $widget->{req_h} = $h; |
|
|
2869 | |
|
|
2870 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
|
|
2871 | |
|
|
2872 | $widget->{parent}->check_size |
|
|
2873 | if $widget->{parent}; |
|
|
2874 | } |
|
|
2875 | } |
|
|
2876 | } |
|
|
2877 | |
|
|
2878 | while ($self->{size_alloc}) { |
|
|
2879 | for (values %{delete $self->{size_alloc}}) { |
|
|
2880 | my ($widget, $w, $h) = @$_; |
|
|
2881 | |
|
|
2882 | $w = 0 if $w < 0; |
|
|
2883 | $h = 0 if $h < 0; |
|
|
2884 | |
|
|
2885 | $widget->{w} = $w; |
|
|
2886 | $widget->{h} = $h; |
|
|
2887 | $widget->size_allocate ($w, $h); |
|
|
2888 | $widget->emit (size_allocate => $w, $h); |
|
|
2889 | } |
|
|
2890 | } |
|
|
2891 | |
|
|
2892 | while ($self->{post_alloc_hook}) { |
|
|
2893 | $_->() |
|
|
2894 | for values %{delete $self->{post_alloc_hook}}; |
2803 | } |
2895 | } |
2804 | |
2896 | |
2805 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2897 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2806 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2898 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2807 | glClear GL_COLOR_BUFFER_BIT; |
2899 | glClear GL_COLOR_BUFFER_BIT; |