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 | |
… | |
… | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
12 | |
12 | our $ROOT; |
13 | our $ROOT; |
13 | our $TOOLTIP; |
14 | our $TOOLTIP; |
14 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
|
|
16 | |
|
|
17 | our %WIDGET; # all widgets, weak-referenced |
15 | |
18 | |
16 | sub check_tooltip { |
19 | sub check_tooltip { |
17 | if (!$GRAB) { |
20 | if (!$GRAB) { |
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
21 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
19 | if (length $widget->{tooltip}) { |
22 | if (length $widget->{tooltip}) { |
… | |
… | |
23 | |
26 | |
24 | my $tip = $widget->{tooltip}; |
27 | my $tip = $widget->{tooltip}; |
25 | |
28 | |
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
29 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
27 | |
30 | |
28 | $TOOLTIP->set_markup ($widget->{tooltip}); |
31 | $TOOLTIP->set_tooltip_from ($widget); |
29 | |
|
|
30 | $TOOLTIP->show; |
32 | $TOOLTIP->show; |
31 | |
33 | |
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
34 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
33 | |
35 | |
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
|
|
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
36 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) |
36 | } |
37 | if $x + $TOOLTIP->{w} > $::WIDTH; |
37 | |
38 | |
38 | $TOOLTIP->move ($x, $y); |
39 | $TOOLTIP->move ($x, $y); |
|
|
40 | $TOOLTIP->check_size; |
|
|
41 | $TOOLTIP->update; |
39 | } |
42 | } |
40 | |
43 | |
41 | return; |
44 | return; |
42 | } |
45 | } |
43 | } |
46 | } |
… | |
… | |
130 | |
133 | |
131 | for (@$vals) { |
134 | for (@$vals) { |
132 | my $i = int $_ + $rem; |
135 | my $i = int $_ + $rem; |
133 | $rem += $_ - $i; |
136 | $rem += $_ - $i; |
134 | $_ = $i; |
137 | $_ = $i; |
|
|
138 | } |
|
|
139 | } |
|
|
140 | |
|
|
141 | # call when resolution changes etc. |
|
|
142 | sub rescale_widgets { |
|
|
143 | my ($sx, $sy) = @_; |
|
|
144 | |
|
|
145 | for my $widget (values %WIDGET) { |
|
|
146 | if ($widget->{toplevel}) { |
|
|
147 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
|
|
148 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
|
|
149 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
|
|
150 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
|
|
151 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
|
|
152 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
|
|
153 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
|
|
154 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
|
|
155 | } |
|
|
156 | |
|
|
157 | $widget->reconfigure; |
135 | } |
158 | } |
136 | } |
159 | } |
137 | |
160 | |
138 | ############################################################################# |
161 | ############################################################################# |
139 | |
162 | |
… | |
… | |
158 | if (/^connect_(.*)$/) { |
181 | if (/^connect_(.*)$/) { |
159 | $self->connect ($1 => delete $self->{$_}); |
182 | $self->connect ($1 => delete $self->{$_}); |
160 | } |
183 | } |
161 | } |
184 | } |
162 | |
185 | |
|
|
186 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
187 | |
163 | $self |
188 | $self |
164 | } |
189 | } |
165 | |
190 | |
166 | sub destroy { |
191 | sub destroy { |
167 | my ($self) = @_; |
192 | my ($self) = @_; |
… | |
… | |
240 | |
265 | |
241 | sub size_allocate { |
266 | sub size_allocate { |
242 | # nothing to be done |
267 | # nothing to be done |
243 | } |
268 | } |
244 | |
269 | |
|
|
270 | sub reconfigure { |
|
|
271 | my ($self) = @_; |
|
|
272 | |
|
|
273 | $self->check_size (1); |
|
|
274 | $self->update; |
|
|
275 | } |
|
|
276 | |
245 | sub children { |
277 | sub children { |
246 | } |
|
|
247 | |
|
|
248 | # call when resolution changes etc. |
|
|
249 | sub reconfigure { |
|
|
250 | my ($self) = @_; |
|
|
251 | |
|
|
252 | $_->reconfigure |
|
|
253 | for $self->children; |
|
|
254 | |
|
|
255 | $self->check_size; |
|
|
256 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
257 | $self->update; |
|
|
258 | } |
278 | } |
259 | |
279 | |
260 | sub set_max_size { |
280 | sub set_max_size { |
261 | my ($self, $w, $h) = @_; |
281 | my ($self, $w, $h) = @_; |
262 | |
282 | |
263 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
283 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
264 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
284 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub set_tooltip { |
|
|
288 | my ($self, $tooltip) = @_; |
|
|
289 | |
|
|
290 | $self->{tooltip} = $tooltip; |
|
|
291 | |
|
|
292 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
|
|
293 | delete $CFClient::UI::TOOLTIP->{owner}; |
|
|
294 | CFClient::UI::check_tooltip; |
|
|
295 | } |
265 | } |
296 | } |
266 | |
297 | |
267 | # translate global coordinates to local coordinate system |
298 | # translate global coordinates to local coordinate system |
268 | sub coord2local { |
299 | sub coord2local { |
269 | my ($self, $x, $y) = @_; |
300 | my ($self, $x, $y) = @_; |
… | |
… | |
355 | glVertex $self->{w}, 0; |
386 | glVertex $self->{w}, 0; |
356 | glVertex $self->{w}, $self->{h}; |
387 | glVertex $self->{w}, $self->{h}; |
357 | glVertex 0 , $self->{h}; |
388 | glVertex 0 , $self->{h}; |
358 | glEnd; |
389 | glEnd; |
359 | glPopMatrix; |
390 | glPopMatrix; |
360 | CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
391 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
361 | } |
392 | } |
362 | } |
393 | } |
363 | |
394 | |
364 | sub _draw { |
395 | sub _draw { |
365 | my ($self) = @_; |
396 | my ($self) = @_; |
… | |
… | |
380 | } |
411 | } |
381 | |
412 | |
382 | sub set_parent { |
413 | sub set_parent { |
383 | my ($self, $parent) = @_; |
414 | my ($self, $parent) = @_; |
384 | |
415 | |
385 | $self->{_tree_depth} = $parent->{_tree_depth} + 1; |
|
|
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
416 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
417 | |
|
|
418 | # TODO: req_w _does_change after ->reconfigure |
|
|
419 | $self->check_size |
|
|
420 | unless exists $self->{req_w}; |
387 | } |
421 | } |
388 | |
422 | |
389 | sub check_size { |
423 | sub check_size { |
390 | my ($self) = @_; |
424 | my ($self, $forced) = @_; |
391 | |
425 | |
|
|
426 | $self->{force_alloc} = 1 if $forced; |
392 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
427 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
393 | } |
428 | } |
394 | |
429 | |
395 | sub update { |
430 | sub update { |
396 | my ($self) = @_; |
431 | my ($self) = @_; |
… | |
… | |
412 | } |
447 | } |
413 | |
448 | |
414 | sub DESTROY { |
449 | sub DESTROY { |
415 | my ($self) = @_; |
450 | my ($self) = @_; |
416 | |
451 | |
|
|
452 | delete $WIDGET{$self+0}; |
417 | #$self->deactivate; |
453 | #$self->deactivate; |
418 | } |
454 | } |
419 | |
455 | |
420 | ############################################################################# |
456 | ############################################################################# |
421 | |
457 | |
… | |
… | |
430 | my $class = shift; |
466 | my $class = shift; |
431 | |
467 | |
432 | # range [value, low, high, page] |
468 | # range [value, low, high, page] |
433 | |
469 | |
434 | $class->SUPER::new ( |
470 | $class->SUPER::new ( |
435 | bg => [0, 0, 0, 0.2], |
471 | #bg => [0, 0, 0, 0.2], |
436 | active_bg => [1, 1, 1, 0.5], |
472 | #active_bg => [1, 1, 1, 0.5], |
437 | @_ |
473 | @_ |
438 | ) |
474 | ) |
439 | } |
475 | } |
440 | |
476 | |
441 | sub _draw { |
477 | sub _draw { |
442 | my ($self) = @_; |
478 | my ($self) = @_; |
443 | |
479 | |
|
|
480 | my $color = $FOCUS == $self && $self->{active_bg} |
|
|
481 | ? $self->{active_bg} |
|
|
482 | : $self->{bg}; |
|
|
483 | |
|
|
484 | if ($color && (@$color < 4 || $color->[3])) { |
444 | my ($w, $h) = @$self{qw(w h)}; |
485 | my ($w, $h) = @$self{qw(w h)}; |
445 | |
486 | |
446 | glEnable GL_BLEND; |
487 | glEnable GL_BLEND; |
447 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
488 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
448 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
489 | glColor @$color; |
449 | |
490 | |
450 | glBegin GL_QUADS; |
491 | glBegin GL_QUADS; |
451 | glVertex 0 , 0; |
492 | glVertex 0 , 0; |
452 | glVertex 0 , $h; |
493 | glVertex 0 , $h; |
453 | glVertex $w, $h; |
494 | glVertex $w, $h; |
454 | glVertex $w, 0; |
495 | glVertex $w, 0; |
455 | glEnd; |
496 | glEnd; |
456 | |
497 | |
457 | glDisable GL_BLEND; |
498 | glDisable GL_BLEND; |
|
|
499 | } |
458 | } |
500 | } |
459 | |
501 | |
460 | ############################################################################# |
502 | ############################################################################# |
461 | |
503 | |
462 | package CFClient::UI::Empty; |
504 | package CFClient::UI::Empty; |
… | |
… | |
506 | $self->{children} = [ |
548 | $self->{children} = [ |
507 | sort { $a->{z} <=> $b->{z} } |
549 | sort { $a->{z} <=> $b->{z} } |
508 | @{$self->{children}}, @widgets |
550 | @{$self->{children}}, @widgets |
509 | ]; |
551 | ]; |
510 | |
552 | |
511 | $_->check_size |
553 | $self->check_size (1); |
512 | for @widgets; |
|
|
513 | |
|
|
514 | $self->update; |
554 | $self->update; |
515 | } |
555 | } |
516 | |
556 | |
517 | sub children { |
557 | sub children { |
518 | @{ $_[0]{children} } |
558 | @{ $_[0]{children} } |
… | |
… | |
625 | } |
665 | } |
626 | |
666 | |
627 | sub update { |
667 | sub update { |
628 | my ($self) = @_; |
668 | my ($self) = @_; |
629 | |
669 | |
630 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
670 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
631 | $self->SUPER::update; |
671 | $self->SUPER::update; |
632 | } |
672 | } |
633 | |
673 | |
634 | sub size_allocate { |
674 | sub size_allocate { |
635 | my ($self, $w, $h) = @_; |
675 | my ($self, $w, $h) = @_; |
… | |
… | |
666 | my ($w, $h) = ($self->w, $self->h); |
706 | my ($w, $h) = ($self->w, $self->h); |
667 | |
707 | |
668 | my $tex = $self->{texture} |
708 | my $tex = $self->{texture} |
669 | or return; |
709 | or return; |
670 | |
710 | |
671 | glEnable GL_BLEND; |
|
|
672 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
673 | glEnable GL_TEXTURE_2D; |
711 | glEnable GL_TEXTURE_2D; |
674 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
712 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
675 | glColor 0, 0, 0, 1; |
713 | glColor 0, 0, 0, 1; |
676 | |
714 | |
677 | $tex->draw_quad (0, 0, $w, $h); |
715 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
678 | |
716 | |
679 | glDisable GL_BLEND; |
|
|
680 | glDisable GL_TEXTURE_2D; |
717 | glDisable GL_TEXTURE_2D; |
681 | } |
718 | } |
682 | |
719 | |
683 | ############################################################################# |
720 | ############################################################################# |
684 | |
721 | |
… | |
… | |
776 | $self->add ($self->{slider}); |
813 | $self->add ($self->{slider}); |
777 | |
814 | |
778 | $self |
815 | $self |
779 | } |
816 | } |
780 | |
817 | |
781 | #TODO# update range on size_allocate depeneing on child |
818 | #TODO# update range on size_allocate depending on child |
782 | # update viewport offset on scroll |
819 | # update viewport offset on scroll |
783 | |
820 | |
784 | ############################################################################# |
821 | ############################################################################# |
785 | |
822 | |
786 | package CFClient::UI::Frame; |
823 | package CFClient::UI::Frame; |
… | |
… | |
790 | use CFClient::OpenGL; |
827 | use CFClient::OpenGL; |
791 | |
828 | |
792 | sub new { |
829 | sub new { |
793 | my $class = shift; |
830 | my $class = shift; |
794 | |
831 | |
795 | my $self = $class->SUPER::new ( |
832 | $class->SUPER::new ( |
796 | bg => [1, 1, 1, 1], |
833 | bg => undef, |
797 | border_bg => [1, 1, 1, 1], |
|
|
798 | border => 0.8, |
|
|
799 | @_ |
834 | @_, |
800 | ); |
835 | ) |
801 | |
|
|
802 | $self |
|
|
803 | } |
836 | } |
804 | |
837 | |
805 | sub _draw { |
838 | sub _draw { |
806 | my ($self) = @_; |
839 | my ($self) = @_; |
807 | |
840 | |
808 | my ($w, $h) = ($self->{w}, $self->{h}); |
841 | if ($self->{bg}) { |
|
|
842 | my ($w, $h) = @$self{qw(w h)}; |
809 | |
843 | |
810 | glEnable GL_BLEND; |
844 | glEnable GL_BLEND; |
811 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
845 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
812 | glEnable GL_TEXTURE_2D; |
846 | glColor @{ $self->{bg} }; |
813 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
814 | |
847 | |
815 | # glBegin GL_QUADS; |
848 | glBegin GL_QUADS; |
816 | # glColor 0, 0, 0, 0; |
|
|
817 | # glVertex 0 , 0; |
849 | glVertex 0 , 0; |
818 | # glVertex 0 , $h; |
850 | glVertex 0 , $h; |
819 | # glVertex $w, $h; |
851 | glVertex $w, $h; |
820 | # glVertex $w, 0; |
852 | glVertex $w, 0; |
821 | # glEnd; |
853 | glEnd; |
822 | |
854 | |
823 | |
|
|
824 | $self->child->draw; |
|
|
825 | glDisable GL_BLEND; |
855 | glDisable GL_BLEND; |
826 | glDisable GL_TEXTURE_2D; |
856 | } |
|
|
857 | |
|
|
858 | $self->SUPER::_draw; |
827 | } |
859 | } |
828 | |
860 | |
829 | ############################################################################# |
861 | ############################################################################# |
830 | |
862 | |
831 | package CFClient::UI::FancyFrame; |
863 | package CFClient::UI::FancyFrame; |
… | |
… | |
845 | |
877 | |
846 | my $self = $class->SUPER::new ( |
878 | my $self = $class->SUPER::new ( |
847 | bg => [1, 1, 1, 1], |
879 | bg => [1, 1, 1, 1], |
848 | border_bg => [1, 1, 1, 1], |
880 | border_bg => [1, 1, 1, 1], |
849 | border => 0.6, |
881 | border => 0.6, |
|
|
882 | toplevel => 1, |
850 | can_events => 1, |
883 | can_events => 1, |
851 | @_ |
884 | @_ |
852 | ); |
885 | ); |
853 | |
886 | |
854 | $self->{title} &&= new CFClient::UI::Label |
887 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
879 | my ($self, $w, $h) = @_; |
912 | my ($self, $w, $h) = @_; |
880 | |
913 | |
881 | $h -= List::Util::max 0, $self->border * 2; |
914 | $h -= List::Util::max 0, $self->border * 2; |
882 | $w -= List::Util::max 0, $self->border * 2; |
915 | $w -= List::Util::max 0, $self->border * 2; |
883 | |
916 | |
884 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
917 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
885 | if $self->{title}; |
918 | if $self->{title}; |
886 | |
919 | |
887 | $self->child->configure ($self->border, $self->border, $w, $h); |
920 | $self->child->configure ($self->border, $self->border, $w, $h); |
888 | } |
921 | } |
889 | |
922 | |
… | |
… | |
947 | my ($self) = @_; |
980 | my ($self) = @_; |
948 | |
981 | |
949 | my ($w, $h ) = ($self->{w}, $self->{h}); |
982 | my ($w, $h ) = ($self->{w}, $self->{h}); |
950 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
983 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
951 | |
984 | |
952 | glEnable GL_BLEND; |
|
|
953 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
954 | glEnable GL_TEXTURE_2D; |
985 | glEnable GL_TEXTURE_2D; |
955 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
986 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
956 | |
987 | |
957 | my $border = $self->border; |
988 | my $border = $self->border; |
958 | |
989 | |
959 | glColor @{ $self->{border_bg} }; |
990 | glColor @{ $self->{border_bg} }; |
960 | $tex[1]->draw_quad (0, 0, $w, $border); |
991 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
961 | $tex[3]->draw_quad (0, $border, $border, $ch); |
992 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
962 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
993 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
963 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
994 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
964 | |
995 | |
965 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
996 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
966 | my $bg = $tex[0]; |
997 | my $bg = $tex[0]; |
967 | |
998 | |
968 | # TODO: repeat texture not scale |
999 | # TODO: repeat texture not scale |
… | |
… | |
972 | glColor @{ $self->{bg} }; |
1003 | glColor @{ $self->{bg} }; |
973 | |
1004 | |
974 | $bg->{s} = $rep_x; |
1005 | $bg->{s} = $rep_x; |
975 | $bg->{t} = $rep_y; |
1006 | $bg->{t} = $rep_y; |
976 | $bg->{wrap_mode} = 1; |
1007 | $bg->{wrap_mode} = 1; |
977 | $bg->draw_quad ($border, $border, $cw, $ch); |
1008 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
1009 | } |
978 | |
1010 | |
979 | glDisable GL_TEXTURE_2D; |
1011 | glDisable GL_TEXTURE_2D; |
980 | glDisable GL_BLEND; |
|
|
981 | } |
|
|
982 | |
1012 | |
983 | $self->{title}->draw if $self->{title}; |
1013 | $self->{title}->draw if $self->{title}; |
984 | |
1014 | |
985 | $self->child->draw; |
1015 | $self->child->draw; |
986 | } |
1016 | } |
… | |
… | |
1213 | } |
1243 | } |
1214 | |
1244 | |
1215 | sub size_allocate { |
1245 | sub size_allocate { |
1216 | my ($self, $w, $h) = @_; |
1246 | my ($self, $w, $h) = @_; |
1217 | |
1247 | |
|
|
1248 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
|
|
1249 | |
1218 | my $children = $self->{children}; |
1250 | my $children = $self->{children}; |
1219 | |
1251 | |
1220 | my @h = map $_->{req_h}, @$children; |
1252 | my @h = map $_->{req_h}, @$children; |
1221 | |
1253 | |
1222 | my $req_h = List::Util::sum @h; |
1254 | my $req_h = List::Util::sum @h; |
… | |
… | |
1251 | |
1283 | |
1252 | ############################################################################# |
1284 | ############################################################################# |
1253 | |
1285 | |
1254 | package CFClient::UI::Label; |
1286 | package CFClient::UI::Label; |
1255 | |
1287 | |
1256 | our @ISA = CFClient::UI::Base::; |
1288 | our @ISA = CFClient::UI::DrawBG::; |
1257 | |
1289 | |
1258 | use CFClient::OpenGL; |
1290 | use CFClient::OpenGL; |
1259 | |
1291 | |
1260 | sub new { |
1292 | sub new { |
1261 | my ($class, %arg) = @_; |
1293 | my ($class, %arg) = @_; |
1262 | |
1294 | |
1263 | my $self = $class->SUPER::new ( |
1295 | my $self = $class->SUPER::new ( |
1264 | fg => [1, 1, 1], |
1296 | fg => [1, 1, 1], |
|
|
1297 | #bg => none |
|
|
1298 | #active_bg => none |
1265 | #font => default_font |
1299 | #font => default_font |
|
|
1300 | #text => initial text |
|
|
1301 | #markup => initial narkup |
|
|
1302 | layout => (new CFClient::Layout), |
1266 | fontsize => 1, |
1303 | fontsize => 1, |
1267 | text => "", |
|
|
1268 | align => -1, |
1304 | align => -1, |
1269 | valign => -1, |
1305 | valign => -1, |
1270 | padding => 2, |
1306 | padding => 2, |
1271 | layout => new CFClient::Layout, |
|
|
1272 | can_events => 0, |
1307 | can_events => 0, |
1273 | %arg |
1308 | %arg |
1274 | ); |
1309 | ); |
1275 | |
1310 | |
1276 | if (exists $self->{template}) { |
1311 | if (exists $self->{template}) { |
1277 | my $layout = new CFClient::Layout; |
1312 | my $layout = new CFClient::Layout; |
1278 | $layout->set_text (delete $self->{template}); |
1313 | $layout->set_text (delete $self->{template}); |
1279 | $self->{template} = $layout; |
1314 | $self->{template} = $layout; |
1280 | } |
1315 | } |
1281 | |
1316 | |
1282 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1317 | if (exists $self->{markup}) { |
1283 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1318 | $self->set_markup (delete $self->{markup}); |
|
|
1319 | } else { |
|
|
1320 | $self->set_text (delete $self->{text}); |
|
|
1321 | } |
1284 | |
1322 | |
1285 | $self |
1323 | $self |
1286 | } |
1324 | } |
1287 | |
1325 | |
1288 | sub escape { |
1326 | sub escape($) { |
1289 | local $_ = $_[1]; |
1327 | local $_ = $_[0]; |
1290 | |
1328 | |
1291 | s/&/&/g; |
1329 | s/&/&/g; |
1292 | s/>/>/g; |
1330 | s/>/>/g; |
1293 | s/</</g; |
1331 | s/</</g; |
1294 | |
1332 | |
1295 | $_[1] |
1333 | $_ |
1296 | } |
1334 | } |
1297 | |
1335 | |
1298 | sub update { |
1336 | sub update { |
1299 | my ($self) = @_; |
1337 | my ($self) = @_; |
1300 | |
1338 | |
… | |
… | |
1306 | my ($self, $text) = @_; |
1344 | my ($self, $text) = @_; |
1307 | |
1345 | |
1308 | return if $self->{text} eq "T$text"; |
1346 | return if $self->{text} eq "T$text"; |
1309 | $self->{text} = "T$text"; |
1347 | $self->{text} = "T$text"; |
1310 | |
1348 | |
|
|
1349 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1311 | $self->{layout}->set_text ($text); |
1350 | $self->{layout}->set_text ($text); |
1312 | |
1351 | |
1313 | $self->update; |
1352 | $self->update; |
1314 | $self->check_size; |
1353 | $self->check_size; |
1315 | } |
1354 | } |
… | |
… | |
1318 | my ($self, $markup) = @_; |
1357 | my ($self, $markup) = @_; |
1319 | |
1358 | |
1320 | return if $self->{text} eq "M$markup"; |
1359 | return if $self->{text} eq "M$markup"; |
1321 | $self->{text} = "M$markup"; |
1360 | $self->{text} = "M$markup"; |
1322 | |
1361 | |
|
|
1362 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
|
|
1363 | |
|
|
1364 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1323 | $self->{layout}->set_markup ($markup); |
1365 | $self->{layout}->set_markup ($markup); |
1324 | |
1366 | |
1325 | $self->update; |
1367 | $self->update; |
1326 | $self->check_size; |
1368 | $self->check_size; |
1327 | } |
1369 | } |
… | |
… | |
1368 | } |
1410 | } |
1369 | |
1411 | |
1370 | sub _draw { |
1412 | sub _draw { |
1371 | my ($self) = @_; |
1413 | my ($self) = @_; |
1372 | |
1414 | |
|
|
1415 | $self->SUPER::_draw; # draw background, if applicable |
|
|
1416 | |
1373 | my $tex = $self->{texture} ||= do { |
1417 | my $tex = $self->{texture} ||= do { |
|
|
1418 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1374 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1419 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1375 | $self->{layout}->set_width ($self->{w}); |
1420 | $self->{layout}->set_width ($self->{w}); |
1376 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1421 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
|
|
1422 | |
1377 | new_from_layout CFClient::Texture $self->{layout} |
1423 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
|
|
1424 | |
|
|
1425 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
|
|
1426 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1427 | : ($self->{w} - $tex->{w}) * 0.5); |
|
|
1428 | |
|
|
1429 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
|
|
1430 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1431 | : ($self->{h} - $tex->{h}) * 0.5); |
|
|
1432 | |
|
|
1433 | $tex |
1378 | }; |
1434 | }; |
1379 | |
1435 | |
1380 | glEnable GL_BLEND; |
|
|
1381 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1382 | glEnable GL_TEXTURE_2D; |
1436 | glEnable GL_TEXTURE_2D; |
1383 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1437 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1384 | |
1438 | |
|
|
1439 | if ($tex->{format} == GL_ALPHA) { |
1385 | glColor @{$self->{fg}}; |
1440 | glColor @{$self->{fg}}; |
1386 | |
|
|
1387 | $self->{ox} = int ( |
|
|
1388 | $self->{align} < 0 ? $self->{padding} |
|
|
1389 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1390 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1391 | ); |
|
|
1392 | |
|
|
1393 | $self->{oy} = int ( |
|
|
1394 | $self->{valign} < 0 ? $self->{padding} |
|
|
1395 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1396 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1397 | ); |
|
|
1398 | |
|
|
1399 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1441 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
|
|
1442 | } else { |
|
|
1443 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
|
|
1444 | } |
1400 | |
1445 | |
1401 | glDisable GL_TEXTURE_2D; |
1446 | glDisable GL_TEXTURE_2D; |
1402 | glDisable GL_BLEND; |
|
|
1403 | } |
1447 | } |
1404 | |
1448 | |
1405 | ############################################################################# |
1449 | ############################################################################# |
1406 | |
1450 | |
1407 | package CFClient::UI::EntryBase; |
1451 | package CFClient::UI::EntryBase; |
… | |
… | |
1442 | $self->{layout}->set_text ("$text "); |
1486 | $self->{layout}->set_text ("$text "); |
1443 | |
1487 | |
1444 | $self->emit (changed => $self->{text}); |
1488 | $self->emit (changed => $self->{text}); |
1445 | } |
1489 | } |
1446 | |
1490 | |
1447 | sub get_text { |
|
|
1448 | $_[0]{text} |
|
|
1449 | } |
|
|
1450 | |
|
|
1451 | sub size_request { |
|
|
1452 | my ($self) = @_; |
|
|
1453 | |
|
|
1454 | my ($w, $h) = $self->SUPER::size_request; |
|
|
1455 | |
|
|
1456 | ($w + 1, $h) # add 1 for cursor |
|
|
1457 | } |
|
|
1458 | |
|
|
1459 | sub size_allocate { |
|
|
1460 | my ($self, $w, $h) = @_; |
|
|
1461 | |
|
|
1462 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
|
|
1463 | } |
|
|
1464 | |
|
|
1465 | sub set_text { |
1491 | sub set_text { |
1466 | my ($self, $text) = @_; |
1492 | my ($self, $text) = @_; |
1467 | |
1493 | |
1468 | $self->{cursor} = length $text; |
1494 | $self->{cursor} = length $text; |
1469 | $self->_set_text ($text); |
1495 | $self->_set_text ($text); |
1470 | $self->update; |
1496 | $self->update; |
|
|
1497 | $self->check_size; |
|
|
1498 | } |
|
|
1499 | |
|
|
1500 | sub get_text { |
|
|
1501 | $_[0]{text} |
|
|
1502 | } |
|
|
1503 | |
|
|
1504 | sub size_request { |
|
|
1505 | my ($self) = @_; |
|
|
1506 | |
|
|
1507 | my ($w, $h) = $self->SUPER::size_request; |
|
|
1508 | |
|
|
1509 | ($w + 1, $h) # add 1 for cursor |
|
|
1510 | } |
|
|
1511 | |
|
|
1512 | sub size_allocate { |
|
|
1513 | my ($self, $w, $h) = @_; |
|
|
1514 | |
|
|
1515 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1471 | } |
1516 | } |
1472 | |
1517 | |
1473 | sub key_down { |
1518 | sub key_down { |
1474 | my ($self, $ev) = @_; |
1519 | my ($self, $ev) = @_; |
1475 | |
1520 | |
… | |
… | |
1477 | my $sym = $ev->{sym}; |
1522 | my $sym = $ev->{sym}; |
1478 | my $uni = $ev->{unicode}; |
1523 | my $uni = $ev->{unicode}; |
1479 | |
1524 | |
1480 | my $text = $self->get_text; |
1525 | my $text = $self->get_text; |
1481 | |
1526 | |
1482 | if ($sym == 8) { |
1527 | if ($uni == 8) { |
1483 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1528 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1484 | } elsif ($sym == 127) { |
1529 | } elsif ($uni == 127) { |
1485 | substr $text, $self->{cursor}, 1, ""; |
1530 | substr $text, $self->{cursor}, 1, ""; |
1486 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1531 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1487 | --$self->{cursor} if $self->{cursor}; |
1532 | --$self->{cursor} if $self->{cursor}; |
1488 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1533 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1489 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1534 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1490 | } elsif ($sym == CFClient::SDLK_HOME) { |
1535 | } elsif ($sym == CFClient::SDLK_HOME) { |
1491 | $self->{cursor} = 0; |
1536 | $self->{cursor} = 0; |
1492 | } elsif ($sym == CFClient::SDLK_END) { |
1537 | } elsif ($sym == CFClient::SDLK_END) { |
1493 | $self->{cursor} = length $text; |
1538 | $self->{cursor} = length $text; |
1494 | } elsif ($sym == 27) { |
1539 | } elsif ($uni == 27) { |
1495 | $self->emit ('escape'); |
1540 | $self->emit ('escape'); |
1496 | } elsif ($uni) { |
1541 | } elsif ($uni) { |
1497 | substr $text, $self->{cursor}++, 0, chr $uni; |
1542 | substr $text, $self->{cursor}++, 0, chr $uni; |
1498 | } |
1543 | } |
1499 | |
1544 | |
1500 | $self->_set_text ($text); |
1545 | $self->_set_text ($text); |
1501 | $self->update; |
1546 | $self->update; |
|
|
1547 | $self->check_size; |
1502 | } |
1548 | } |
1503 | |
1549 | |
1504 | sub focus_in { |
1550 | sub focus_in { |
1505 | my ($self) = @_; |
1551 | my ($self) = @_; |
1506 | |
1552 | |
… | |
… | |
1635 | my $class = shift; |
1681 | my $class = shift; |
1636 | |
1682 | |
1637 | $class->SUPER::new ( |
1683 | $class->SUPER::new ( |
1638 | padding => 4, |
1684 | padding => 4, |
1639 | fg => [1, 1, 1], |
1685 | fg => [1, 1, 1], |
1640 | bg => [1, 1, 1, 0.2], |
|
|
1641 | active_fg => [0, 0, 1], |
1686 | active_fg => [0, 0, 1], |
1642 | can_hover => 1, |
1687 | can_hover => 1, |
1643 | align => 0, |
1688 | align => 0, |
1644 | valign => 0, |
1689 | valign => 0, |
1645 | can_events => 1, |
1690 | can_events => 1, |
… | |
… | |
1663 | |
1708 | |
1664 | if ($GRAB == $self) { |
1709 | if ($GRAB == $self) { |
1665 | $self->{fg} = $self->{active_fg}; |
1710 | $self->{fg} = $self->{active_fg}; |
1666 | } |
1711 | } |
1667 | |
1712 | |
1668 | glEnable GL_BLEND; |
|
|
1669 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1670 | glEnable GL_TEXTURE_2D; |
1713 | glEnable GL_TEXTURE_2D; |
1671 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1714 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1672 | glColor 0, 0, 0, 1; |
1715 | glColor 0, 0, 0, 1; |
1673 | |
1716 | |
1674 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1717 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1675 | |
1718 | |
1676 | glDisable GL_TEXTURE_2D; |
1719 | glDisable GL_TEXTURE_2D; |
1677 | glDisable GL_BLEND; |
|
|
1678 | |
1720 | |
1679 | $self->SUPER::_draw; |
1721 | $self->SUPER::_draw; |
1680 | } |
1722 | } |
1681 | |
1723 | |
1682 | ############################################################################# |
1724 | ############################################################################# |
… | |
… | |
1696 | |
1738 | |
1697 | $class->SUPER::new ( |
1739 | $class->SUPER::new ( |
1698 | padding => 2, |
1740 | padding => 2, |
1699 | fg => [1, 1, 1], |
1741 | fg => [1, 1, 1], |
1700 | active_fg => [1, 1, 0], |
1742 | active_fg => [1, 1, 0], |
|
|
1743 | bg => [0, 0, 0, 0.2], |
|
|
1744 | active_bg => [1, 1, 1, 0.5], |
1701 | state => 0, |
1745 | state => 0, |
1702 | can_hover => 1, |
1746 | can_hover => 1, |
1703 | @_ |
1747 | @_ |
1704 | ) |
1748 | ) |
1705 | } |
1749 | } |
… | |
… | |
1729 | |
1773 | |
1730 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1774 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1731 | |
1775 | |
1732 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1776 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1733 | |
1777 | |
1734 | glEnable GL_BLEND; |
1778 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1779 | |
1735 | glEnable GL_TEXTURE_2D; |
1780 | glEnable GL_TEXTURE_2D; |
1736 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1737 | |
|
|
1738 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1739 | |
|
|
1740 | $tex->draw_quad (0, 0, $s, $s); |
1781 | $tex->draw_quad_alpha (0, 0, $s, $s); |
1741 | |
|
|
1742 | glDisable GL_TEXTURE_2D; |
1782 | glDisable GL_TEXTURE_2D; |
1743 | glDisable GL_BLEND; |
|
|
1744 | } |
1783 | } |
1745 | |
1784 | |
1746 | ############################################################################# |
1785 | ############################################################################# |
1747 | |
1786 | |
1748 | package CFClient::UI::Image; |
1787 | package CFClient::UI::Image; |
… | |
… | |
1791 | glTranslate 0, -$self->{w}, 0; |
1830 | glTranslate 0, -$self->{w}, 0; |
1792 | |
1831 | |
1793 | ($w, $h) = ($h, $w); |
1832 | ($w, $h) = ($h, $w); |
1794 | } |
1833 | } |
1795 | |
1834 | |
1796 | glEnable GL_BLEND; |
|
|
1797 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1798 | glEnable GL_TEXTURE_2D; |
1835 | glEnable GL_TEXTURE_2D; |
1799 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1836 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1800 | |
1837 | |
1801 | $tex->draw_quad (0, 0, $w, $h); |
1838 | $tex->draw_quad_alpha (0, 0, $w, $h); |
1802 | |
1839 | |
1803 | glDisable GL_BLEND; |
|
|
1804 | glDisable GL_TEXTURE_2D; |
1840 | glDisable GL_TEXTURE_2D; |
1805 | } |
1841 | } |
1806 | |
1842 | |
1807 | ############################################################################# |
1843 | ############################################################################# |
1808 | |
1844 | |
… | |
… | |
1997 | qw(s1_slider.png s1_slider_bg.png); |
2033 | qw(s1_slider.png s1_slider_bg.png); |
1998 | |
2034 | |
1999 | sub new { |
2035 | sub new { |
2000 | my $class = shift; |
2036 | my $class = shift; |
2001 | |
2037 | |
2002 | # range [value, low, high, page] |
2038 | # range [value, low, high, page, unit] |
2003 | |
2039 | |
2004 | # TODO: 0-width page |
2040 | # TODO: 0-width page |
2005 | # TODO: req_w/h are wrong with vertical |
2041 | # TODO: req_w/h are wrong with vertical |
2006 | # TODO: calculations are off |
2042 | # TODO: calculations are off |
2007 | my $self = $class->SUPER::new ( |
2043 | my $self = $class->SUPER::new ( |
2008 | fg => [1, 1, 1], |
2044 | fg => [1, 1, 1], |
2009 | active_fg => [0, 0, 0], |
2045 | active_fg => [0, 0, 0], |
|
|
2046 | bg => [0, 0, 0, 0.2], |
|
|
2047 | active_bg => [1, 1, 1, 0.5], |
2010 | range => [0, 0, 100, 10], |
2048 | range => [0, 0, 100, 10], |
2011 | req_w => $::WIDTH / 80, |
2049 | req_w => $::WIDTH / 80, |
2012 | req_h => $::WIDTH / 80, |
2050 | req_h => $::WIDTH / 80, |
2013 | vertical => 0, |
2051 | vertical => 0, |
2014 | can_hover => 1, |
2052 | can_hover => 1, |
2015 | inner_pad => 5, |
2053 | inner_pad => .05, |
2016 | @_ |
2054 | @_ |
2017 | ); |
2055 | ); |
2018 | |
2056 | |
|
|
2057 | $self->set_value ($self->{range}[0]); |
|
|
2058 | $self->update; |
|
|
2059 | |
2019 | $self |
2060 | $self |
|
|
2061 | } |
|
|
2062 | |
|
|
2063 | sub set_value { |
|
|
2064 | my ($self, $value) = @_; |
|
|
2065 | |
|
|
2066 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
|
|
2067 | |
|
|
2068 | $hi = $lo + 1 if $hi <= $lo; |
|
|
2069 | |
|
|
2070 | $value = $lo if $value < $lo; |
|
|
2071 | $value = $hi if $value > $hi; |
|
|
2072 | |
|
|
2073 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
|
|
2074 | if $unit; |
|
|
2075 | |
|
|
2076 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2077 | |
|
|
2078 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
|
|
2079 | |
|
|
2080 | if ($value != $old_value) { |
|
|
2081 | $self->emit (changed => $value); |
|
|
2082 | $self->update; |
|
|
2083 | } |
2020 | } |
2084 | } |
2021 | |
2085 | |
2022 | sub size_request { |
2086 | sub size_request { |
2023 | my ($self) = @_; |
2087 | my ($self) = @_; |
2024 | |
2088 | |
… | |
… | |
2037 | |
2101 | |
2038 | sub mouse_motion { |
2102 | sub mouse_motion { |
2039 | my ($self, $ev, $x, $y) = @_; |
2103 | my ($self, $ev, $x, $y) = @_; |
2040 | |
2104 | |
2041 | if ($GRAB == $self) { |
2105 | if ($GRAB == $self) { |
|
|
2106 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
2107 | |
|
|
2108 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2109 | |
|
|
2110 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
|
|
2111 | |
|
|
2112 | $self->set_value ($x * ($hi - $lo) + $lo); |
|
|
2113 | } |
|
|
2114 | } |
|
|
2115 | |
|
|
2116 | sub update { |
|
|
2117 | my ($self) = @_; |
|
|
2118 | |
|
|
2119 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
|
|
2120 | $self->set_value ($self->{range}[0]); |
|
|
2121 | |
2042 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2122 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2043 | |
2123 | |
2044 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2124 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2045 | |
2125 | |
2046 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
2126 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2047 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2048 | |
2127 | |
2049 | $x -= $inner_pad_px; # substract the padding |
2128 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2050 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
2129 | $value = $self->{scale} * ($value - $lo); |
2051 | $x = $lo if $x < $lo; |
|
|
2052 | $x = $hi - $page if $x > $hi - $page; |
|
|
2053 | $self->{range}[0] = $x; |
|
|
2054 | |
2130 | |
2055 | $self->emit (changed => $x); |
2131 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
2056 | $self->update; |
2132 | |
|
|
2133 | $value = 0 if $value < 0; |
|
|
2134 | $page = 1 - $value if $value + $page > 1; |
|
|
2135 | |
|
|
2136 | $self->{knob_x} = $value; |
|
|
2137 | $self->{knob_w} = $page; |
2057 | } |
2138 | }); |
2058 | } |
|
|
2059 | |
2139 | |
2060 | # the inner_* stuff is for generating a padding for the slider handle, |
2140 | $self->SUPER::update; |
2061 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
2062 | # correct propably, but it does the job for now |
|
|
2063 | sub _calc_inner_pad_px { |
|
|
2064 | my ($self, $w) = @_; |
|
|
2065 | ($w / 100) * $self->{inner_pad} # % to pixels |
|
|
2066 | } |
2141 | } |
2067 | |
2142 | |
2068 | sub _draw { |
2143 | sub _draw { |
2069 | my ($self) = @_; |
2144 | my ($self) = @_; |
2070 | |
2145 | |
2071 | $self->SUPER::_draw (); |
2146 | $self->SUPER::_draw (); |
2072 | |
2147 | |
2073 | my ($w, $h) = @$self{qw(w h)}; |
2148 | glScale $self->{w}, $self->{h}; |
2074 | |
2149 | |
2075 | if ($self->{vertical}) { |
2150 | if ($self->{vertical}) { |
2076 | # draw a vertical slider like a rotated horizontal slider |
2151 | # draw a vertical slider like a rotated horizontal slider |
2077 | |
2152 | |
2078 | glRotate 90, 0, 0, 1; |
2153 | glRotate 90, 0, 0, 1; |
2079 | glTranslate 0, -$self->{w}, 0; |
2154 | glTranslate 0, 1, 0; |
2080 | |
|
|
2081 | ($w, $h) = ($h, $w); |
|
|
2082 | } |
2155 | } |
2083 | |
2156 | |
2084 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2157 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2085 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2158 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2086 | |
2159 | |
2087 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2088 | |
|
|
2089 | $hi = $value + 1 if $lo == $hi; |
|
|
2090 | |
|
|
2091 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
2092 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2093 | |
|
|
2094 | $page = int $page * $inner_w / ($hi - $lo); |
|
|
2095 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
|
|
2096 | |
|
|
2097 | $w -= $page; |
|
|
2098 | $page &= ~1; |
|
|
2099 | glTranslate $page * 0.5, 0, 0; |
|
|
2100 | $page ||= 2; |
|
|
2101 | |
|
|
2102 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
|
|
2103 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
|
|
2104 | |
|
|
2105 | glEnable GL_BLEND; |
|
|
2106 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2107 | glEnable GL_TEXTURE_2D; |
2160 | glEnable GL_TEXTURE_2D; |
2108 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2161 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2109 | |
2162 | |
2110 | # draw background |
2163 | # draw background |
2111 | $tex[1]->draw_quad (0, 0, $w, $h); |
2164 | $tex[1]->draw_quad_alpha (0, 0, 1, 1); |
2112 | |
2165 | |
2113 | # draw handle |
2166 | # draw handle |
2114 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
2167 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2115 | |
2168 | |
2116 | glDisable GL_BLEND; |
|
|
2117 | glDisable GL_TEXTURE_2D; |
2169 | glDisable GL_TEXTURE_2D; |
2118 | } |
2170 | } |
2119 | |
2171 | |
2120 | ############################################################################# |
2172 | ############################################################################# |
2121 | |
2173 | |
… | |
… | |
2132 | fontsize => 1, |
2184 | fontsize => 1, |
2133 | can_events => 0, |
2185 | can_events => 0, |
2134 | #font => default_font |
2186 | #font => default_font |
2135 | @_, |
2187 | @_, |
2136 | |
2188 | |
2137 | layout => (new CFClient::Layout), |
2189 | layout => (new CFClient::Layout 1), |
2138 | par => [], |
2190 | par => [], |
2139 | height => 0, |
2191 | height => 0, |
2140 | children => [ |
2192 | children => [ |
2141 | (new CFClient::UI::Empty expand => 1), |
2193 | (new CFClient::UI::Empty expand => 1), |
2142 | (new CFClient::UI::Slider vertical => 1), |
2194 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2160 | |
2212 | |
2161 | my $layout = $self->{layout}; |
2213 | my $layout = $self->{layout}; |
2162 | |
2214 | |
2163 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2215 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2164 | $layout->set_width ($self->{children}[0]{w}); |
2216 | $layout->set_width ($self->{children}[0]{w}); |
2165 | $layout->set_text ($text); |
2217 | $layout->set_markup ($text); |
2166 | |
2218 | |
2167 | ($layout->size)[1] |
2219 | ($layout->size)[1] |
2168 | } |
2220 | } |
2169 | |
2221 | |
2170 | sub reflow { |
2222 | sub reflow { |
… | |
… | |
2208 | |
2260 | |
2209 | return unless $self->{h} > 0; |
2261 | return unless $self->{h} > 0; |
2210 | |
2262 | |
2211 | delete $self->{texture}; |
2263 | delete $self->{texture}; |
2212 | |
2264 | |
2213 | $ROOT->on_refresh ($self, sub { |
2265 | $ROOT->on_post_alloc ($self, sub { |
2214 | if (delete $self->{need_reflow}) { |
2266 | if (delete $self->{need_reflow}) { |
2215 | my $height = 0; |
2267 | my $height = 0; |
2216 | |
2268 | |
2217 | $height += $_->[0] = $self->text_height ($_->[2]) |
2269 | $height += $_->[0] = $self->text_height ($_->[2]) |
2218 | for @{$self->{par}}; |
2270 | for @{$self->{par}}; |
… | |
… | |
2226 | |
2278 | |
2227 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2279 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2228 | glClearColor 0, 0, 0, 0; |
2280 | glClearColor 0, 0, 0, 0; |
2229 | glClear GL_COLOR_BUFFER_BIT; |
2281 | glClear GL_COLOR_BUFFER_BIT; |
2230 | |
2282 | |
2231 | glEnable GL_BLEND; |
|
|
2232 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2233 | glEnable GL_TEXTURE_2D; |
2283 | glEnable GL_TEXTURE_2D; |
2234 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2284 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2235 | |
2285 | |
2236 | my $top = int $self->{children}[1]{range}[0]; |
2286 | my $top = int $self->{children}[1]{range}[0]; |
2237 | |
2287 | |
… | |
… | |
2246 | |
2296 | |
2247 | for my $par (@{$self->{par}}) { |
2297 | for my $par (@{$self->{par}}) { |
2248 | my $h = $par->[0]; |
2298 | my $h = $par->[0]; |
2249 | |
2299 | |
2250 | if ($y0 < $y + $h && $y < $y1) { |
2300 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2301 | $layout->set_foreground (@{ $par->[1] }); |
2251 | $layout->set_text ($par->[2]); |
2302 | $layout->set_markup ($par->[2]); |
2252 | |
2303 | |
2253 | glColor @{ $par->[1] }; |
|
|
2254 | my ($W, $H) = $layout->size; |
2304 | my ($W, $H) = $layout->size; |
2255 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
2305 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2256 | } |
2306 | } |
2257 | |
2307 | |
2258 | $y += $h; |
2308 | $y += $h; |
2259 | } |
2309 | } |
2260 | |
2310 | |
2261 | glDisable GL_TEXTURE_2D; |
2311 | glDisable GL_TEXTURE_2D; |
2262 | glDisable GL_BLEND; |
|
|
2263 | }; |
2312 | }; |
2264 | }); |
2313 | }); |
2265 | } |
2314 | } |
2266 | |
2315 | |
2267 | sub _draw { |
2316 | sub _draw { |
2268 | my ($self) = @_; |
2317 | my ($self) = @_; |
2269 | |
2318 | |
2270 | glEnable GL_BLEND; |
|
|
2271 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2272 | glEnable GL_TEXTURE_2D; |
2319 | glEnable GL_TEXTURE_2D; |
2273 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2320 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2274 | glColor 1, 1, 1, 1; |
2321 | glColor 1, 1, 1, 1; |
2275 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2322 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2276 | glDisable GL_TEXTURE_2D; |
2323 | glDisable GL_TEXTURE_2D; |
2277 | glDisable GL_BLEND; |
|
|
2278 | |
2324 | |
2279 | $self->{children}[1]->draw; |
2325 | $self->{children}[1]->draw; |
2280 | |
2326 | |
2281 | } |
2327 | } |
2282 | |
2328 | |
… | |
… | |
2376 | @_, |
2422 | @_, |
2377 | can_events => 0, |
2423 | can_events => 0, |
2378 | ) |
2424 | ) |
2379 | } |
2425 | } |
2380 | |
2426 | |
2381 | sub set_markup { |
2427 | sub set_tooltip_from { |
2382 | my ($self, $text) = @_; |
2428 | my ($self, $widget) = @_; |
2383 | |
2429 | |
2384 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2430 | $self->add (new CFClient::UI::Label |
2385 | $self->{label}->set_markup ($text); |
2431 | markup => $widget->{tooltip}, |
2386 | $self->add ($self->{label}); |
2432 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
2433 | fontsize => 0.8, |
|
|
2434 | fg => [0, 0, 0, 1], |
|
|
2435 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
|
|
2436 | ); |
2387 | } |
2437 | } |
2388 | |
2438 | |
2389 | sub size_request { |
2439 | sub size_request { |
2390 | my ($self) = @_; |
2440 | my ($self) = @_; |
2391 | |
|
|
2392 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2393 | |
2441 | |
2394 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2442 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2395 | |
2443 | |
2396 | ($w + 4, $h + 4) |
2444 | ($w + 4, $h + 4) |
2397 | } |
2445 | } |
… | |
… | |
2403 | } |
2451 | } |
2404 | |
2452 | |
2405 | sub _draw { |
2453 | sub _draw { |
2406 | my ($self) = @_; |
2454 | my ($self) = @_; |
2407 | |
2455 | |
2408 | glPushMatrix; |
|
|
2409 | glTranslate 0.375, 0.375; |
2456 | glTranslate 0.375, 0.375; |
2410 | |
2457 | |
2411 | my ($w, $h) = @$self{qw(w h)}; |
2458 | my ($w, $h) = @$self{qw(w h)}; |
2412 | |
2459 | |
2413 | glColor 1, 0.8, 0.4; |
2460 | glColor 1, 0.8, 0.4; |
… | |
… | |
2424 | glVertex 0 , $h; |
2471 | glVertex 0 , $h; |
2425 | glVertex $w, $h; |
2472 | glVertex $w, $h; |
2426 | glVertex $w, 0; |
2473 | glVertex $w, 0; |
2427 | glEnd; |
2474 | glEnd; |
2428 | |
2475 | |
2429 | glPopMatrix; |
2476 | glTranslate 2 - 0.375, 2 - 0.375; |
2430 | |
|
|
2431 | glTranslate 2, 2; |
|
|
2432 | $self->SUPER::_draw; |
2477 | $self->SUPER::_draw; |
2433 | } |
2478 | } |
2434 | |
2479 | |
2435 | ############################################################################# |
2480 | ############################################################################# |
2436 | |
2481 | |
… | |
… | |
2459 | return unless $::CONN;#d# manage and cache textures differently |
2504 | return unless $::CONN;#d# manage and cache textures differently |
2460 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2505 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2461 | |
2506 | |
2462 | # TODO animation |
2507 | # TODO animation |
2463 | if ($tex) { |
2508 | if ($tex) { |
2464 | glEnable GL_BLEND; |
|
|
2465 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2466 | glEnable GL_TEXTURE_2D; |
2509 | glEnable GL_TEXTURE_2D; |
2467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2510 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2468 | glColor 1, 1, 1, 1; |
2511 | glColor 1, 1, 1, 1; |
2469 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2512 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2470 | glDisable GL_TEXTURE_2D; |
2513 | glDisable GL_TEXTURE_2D; |
2471 | glDisable GL_BLEND; |
|
|
2472 | } |
2514 | } |
2473 | } |
2515 | } |
2474 | |
2516 | |
2475 | ############################################################################# |
2517 | ############################################################################# |
2476 | |
2518 | |
… | |
… | |
2481 | sub new { |
2523 | sub new { |
2482 | my $class = shift; |
2524 | my $class = shift; |
2483 | |
2525 | |
2484 | my %args = @_; |
2526 | my %args = @_; |
2485 | |
2527 | |
2486 | my $item = $args{item}; |
2528 | my $item = delete $args{item}; |
2487 | |
2529 | |
2488 | my $desc = $item->{nrof} < 2 |
2530 | my $desc = $item->{nrof} < 2 |
2489 | ? $item->{name} |
2531 | ? $item->{name} |
2490 | : "$item->{nrof} $item->{name_pl}"; |
2532 | : "$item->{nrof} $item->{name_pl}"; |
2491 | |
2533 | |
|
|
2534 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2535 | and $desc .= " (open)"; |
|
|
2536 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2537 | and $desc .= " (applied)"; |
|
|
2538 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2539 | and $desc .= " (unpaid)"; |
|
|
2540 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2541 | and $desc .= " (magic)"; |
|
|
2542 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2543 | and $desc .= " (cursed)"; |
|
|
2544 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2545 | and $desc .= " (damned)"; |
|
|
2546 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2547 | and $desc .= " *"; |
2492 | |
2548 | |
2493 | my $self = $class->SUPER::new ( |
2549 | my $self = $class->SUPER::new ( |
2494 | can_hover => 1, |
2550 | can_hover => 1, |
2495 | can_events => 1, |
2551 | can_events => 1, |
2496 | tooltip => (CFClient::UI::Label->escape ($desc) |
2552 | tooltip => ((CFClient::UI::Label::escape $desc) |
2497 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
2553 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
2498 | connect_button_down => sub { |
2554 | connect_button_down => sub { |
2499 | my ($self, $ev, $x, $y) = @_; |
2555 | my ($self, $ev, $x, $y) = @_; |
2500 | |
2556 | |
2501 | # todo: maybe put examine on 1? but should just be a tooltip :( |
2557 | # todo: maybe put examine on 1? but should just be a tooltip :( |
2502 | if ($ev->{button} == 1) { |
2558 | if ($ev->{button} == 1) { |
|
|
2559 | my $targ = $::CONN->{player}{tag}; |
|
|
2560 | |
|
|
2561 | if ($item->{container} == $::CONN->{player}{tag}) { |
|
|
2562 | $targ = $main::OPENCONT; |
|
|
2563 | } |
|
|
2564 | |
2503 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2565 | $::CONN->send ("move $targ $item->{tag} 0"); |
2504 | } elsif ($ev->{button} == 2) { |
2566 | } elsif ($ev->{button} == 2) { |
2505 | $::CONN->send ("apply $item->{tag}"); |
2567 | $::CONN->send ("apply $item->{tag}"); |
2506 | } elsif ($ev->{button} == 3) { |
2568 | } elsif ($ev->{button} == 3) { |
2507 | CFClient::UI::Menu->new ( |
2569 | my @menu_items = ( |
2508 | items => [ |
|
|
2509 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
2570 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2571 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2572 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2573 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
2510 | [ |
2574 | [ |
2511 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2575 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2512 | sub { $::CONN->send ("lock $item->{tag}") }, |
2576 | sub { $::CONN->send ("lock $item->{tag}") }, |
2513 | ], |
|
|
2514 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2515 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2516 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2517 | ], |
2577 | ], |
2518 | )->popup ($ev); |
2578 | ); |
|
|
2579 | |
|
|
2580 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
2519 | } |
2581 | } |
2520 | |
2582 | |
2521 | 1 |
2583 | 1 |
2522 | }, |
2584 | }, |
2523 | %args |
2585 | %args |
2524 | ); |
2586 | ); |
|
|
2587 | |
2525 | $self->add(new CFClient::UI::Face |
2588 | $self->add (new CFClient::UI::Face |
2526 | can_events => 0, |
2589 | can_events => 0, |
2527 | face => $item->{face}, |
2590 | face => $item->{face}, |
2528 | anim => $item->{anim}, |
2591 | anim => $item->{anim}, |
2529 | animspeed => $item->{animspeed}); |
2592 | animspeed => $item->{animspeed}, |
|
|
2593 | ); |
|
|
2594 | |
2530 | $self->add(new CFClient::UI::Label |
2595 | $self->add (new CFClient::UI::Label |
2531 | can_events => 0, |
2596 | can_events => 0, |
2532 | text => $desc); |
2597 | text => $desc, |
|
|
2598 | ); |
2533 | |
2599 | |
2534 | $self |
2600 | $self |
2535 | } |
2601 | } |
2536 | |
2602 | |
2537 | ############################################################################# |
2603 | ############################################################################# |
… | |
… | |
2555 | my ($self, $items) = @_; |
2621 | my ($self, $items) = @_; |
2556 | |
2622 | |
2557 | $self->{scrolled}->clear; |
2623 | $self->{scrolled}->clear; |
2558 | return unless $items; |
2624 | return unless $items; |
2559 | |
2625 | |
2560 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
2626 | my @items = sort { |
|
|
2627 | ($a->{type} <=> $b->{type}) |
|
|
2628 | or ($a->{name} cmp $b->{name}) |
|
|
2629 | } @$items; |
2561 | |
2630 | |
2562 | $self->{real_items} = \@items; |
2631 | $self->{real_items} = \@items; |
2563 | |
2632 | |
2564 | for my $item (@items) { |
2633 | for my $item (@items) { |
2565 | my $desc = $item->{nrof} < 2 |
2634 | my $desc = $item->{nrof} < 2 |
2566 | ? $item->{name} |
2635 | ? $item->{name} |
2567 | : "$item->{nrof} $item->{name_pl}"; |
2636 | : "$item->{nrof} $item->{name_pl}"; |
2568 | |
2637 | |
2569 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
2638 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2570 | } |
2639 | } |
|
|
2640 | |
|
|
2641 | $self->{scrolled}->add (@items); |
2571 | |
2642 | |
2572 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2643 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2573 | } |
2644 | } |
2574 | |
2645 | |
2575 | sub size_request { |
2646 | sub size_request { |
… | |
… | |
2649 | } |
2720 | } |
2650 | } |
2721 | } |
2651 | |
2722 | |
2652 | ############################################################################# |
2723 | ############################################################################# |
2653 | |
2724 | |
|
|
2725 | package CFClient::UI::Statusbox; |
|
|
2726 | |
|
|
2727 | our @ISA = CFClient::UI::VBox::; |
|
|
2728 | |
|
|
2729 | sub new { |
|
|
2730 | my $class = shift; |
|
|
2731 | |
|
|
2732 | $class->SUPER::new ( |
|
|
2733 | fontsize => 0.8, |
|
|
2734 | @_, |
|
|
2735 | ) |
|
|
2736 | } |
|
|
2737 | |
|
|
2738 | sub reorder { |
|
|
2739 | my ($self) = @_; |
|
|
2740 | my $NOW = time; |
|
|
2741 | |
|
|
2742 | while (my ($k, $v) = each %{ $self->{item} }) { |
|
|
2743 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
|
|
2744 | } |
|
|
2745 | |
|
|
2746 | my @widgets; |
|
|
2747 | |
|
|
2748 | my @items = sort { |
|
|
2749 | $a->{pri} <=> $b->{pri} |
|
|
2750 | or $b->{id} <=> $a->{id} |
|
|
2751 | } values %{ $self->{item} }; |
|
|
2752 | |
|
|
2753 | my $count = 10 + 1; |
|
|
2754 | for my $item (@items) { |
|
|
2755 | last unless --$count; |
|
|
2756 | |
|
|
2757 | push @widgets, $item->{label} ||= do { |
|
|
2758 | # TODO: doesn't handle markup well (read as: at all) |
|
|
2759 | my $short = $item->{count} > 1 |
|
|
2760 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2761 | : $item->{text}; |
|
|
2762 | |
|
|
2763 | my $fontsize = $item->{fontsize} || $self->{fontsize}; |
|
|
2764 | |
|
|
2765 | for ($short) { |
|
|
2766 | s/^\s+//; |
|
|
2767 | s/\s+/ /g; |
|
|
2768 | my $len = int 40 / $fontsize; |
|
|
2769 | substr $_, $len, length, "…" if $len < length; |
|
|
2770 | } |
|
|
2771 | |
|
|
2772 | new CFClient::UI::Label |
|
|
2773 | markup => $short, |
|
|
2774 | tooltip => $item->{tooltip}, |
|
|
2775 | tooltip_font => $::FONT_PROP, |
|
|
2776 | tooltip_width => 0.67, |
|
|
2777 | fontsize => $fontsize, |
|
|
2778 | fg => $item->{fg}, |
|
|
2779 | can_events => 1, |
|
|
2780 | can_hover => 1 |
|
|
2781 | }; |
|
|
2782 | } |
|
|
2783 | |
|
|
2784 | $self->clear; |
|
|
2785 | $self->SUPER::add (reverse @widgets); |
|
|
2786 | } |
|
|
2787 | |
|
|
2788 | sub add { |
|
|
2789 | my ($self, $text, %arg) = @_; |
|
|
2790 | |
|
|
2791 | $text =~ s/^\s+//; |
|
|
2792 | $text =~ s/\s+$//; |
|
|
2793 | |
|
|
2794 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2795 | |
|
|
2796 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2797 | |
|
|
2798 | if (my $item = $self->{item}{$group}) { |
|
|
2799 | if ($item->{text} eq $text) { |
|
|
2800 | $item->{count}++; |
|
|
2801 | } else { |
|
|
2802 | $item->{count} = 1; |
|
|
2803 | $item->{text} = $item->{tooltip} = $text; |
|
|
2804 | } |
|
|
2805 | $item->{id} = ++$self->{id}; |
|
|
2806 | $item->{timeout} = $timeout; |
|
|
2807 | delete $item->{label}; |
|
|
2808 | } else { |
|
|
2809 | $self->{item}{$group} = { |
|
|
2810 | id => ++$self->{id}, |
|
|
2811 | text => $text, |
|
|
2812 | timeout => $timeout, |
|
|
2813 | tooltip => $text, |
|
|
2814 | fg => [0.8, 0.8, 0.8, 0.8], |
|
|
2815 | pri => 0, |
|
|
2816 | count => 1, |
|
|
2817 | %arg, |
|
|
2818 | }; |
|
|
2819 | } |
|
|
2820 | |
|
|
2821 | $self->reorder; |
|
|
2822 | } |
|
|
2823 | |
|
|
2824 | ############################################################################# |
|
|
2825 | |
2654 | package CFClient::UI::Root; |
2826 | package CFClient::UI::Root; |
2655 | |
2827 | |
2656 | our @ISA = CFClient::UI::Container::; |
2828 | our @ISA = CFClient::UI::Container::; |
2657 | |
2829 | |
2658 | use CFClient::OpenGL; |
2830 | use CFClient::OpenGL; |
… | |
… | |
2686 | } |
2858 | } |
2687 | |
2859 | |
2688 | sub size_allocate { |
2860 | sub size_allocate { |
2689 | my ($self, $w, $h) = @_; |
2861 | my ($self, $w, $h) = @_; |
2690 | |
2862 | |
2691 | my $old_w = $self->{old_w}; |
2863 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2692 | my $old_h = $self->{old_h}; |
2864 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2693 | |
2865 | |
2694 | if ($old_w && $old_h) { |
2866 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2695 | for my $child ($self->children) { |
2867 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
2696 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2697 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2698 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2699 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2700 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2701 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2702 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2703 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2704 | } |
|
|
2705 | } |
|
|
2706 | |
2868 | |
2707 | for my $child ($self->children) { |
2869 | for my $child ($self->children) { |
2708 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2870 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2709 | |
2871 | |
|
|
2872 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
|
|
2873 | if exists $child->{req_x}; |
|
|
2874 | |
|
|
2875 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
|
|
2876 | if exists $child->{req_y}; |
|
|
2877 | |
2710 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2878 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
2711 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2879 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
2880 | |
2712 | $child->configure ($X, $Y, $W, $H); |
2881 | $child->configure ($X, $Y, $W, $H); |
2713 | } |
2882 | } |
2714 | |
|
|
2715 | $self->{old_w} = $w; |
|
|
2716 | $self->{old_h} = $h; |
|
|
2717 | } |
2883 | } |
2718 | |
2884 | |
2719 | sub coord2local { |
2885 | sub coord2local { |
2720 | my ($self, $x, $y) = @_; |
2886 | my ($self, $x, $y) = @_; |
2721 | |
2887 | |
… | |
… | |
2734 | $self->check_size; |
2900 | $self->check_size; |
2735 | $::WANT_REFRESH++; |
2901 | $::WANT_REFRESH++; |
2736 | } |
2902 | } |
2737 | |
2903 | |
2738 | sub add { |
2904 | sub add { |
2739 | my ($self, $child) = @_; |
2905 | my ($self, @children) = @_; |
2740 | |
2906 | |
|
|
2907 | for my $child (@children) { |
|
|
2908 | $child->{toplevel} = 1; |
|
|
2909 | |
2741 | # integerise window positions |
2910 | # integerise window positions |
2742 | $child->{x} = int $child->{x}; |
2911 | $child->{x} = int $child->{x}; |
2743 | $child->{y} = int $child->{y}; |
2912 | $child->{y} = int $child->{y}; |
|
|
2913 | } |
2744 | |
2914 | |
2745 | $self->SUPER::add ($child); |
2915 | $self->SUPER::add (@children); |
2746 | } |
2916 | } |
2747 | |
2917 | |
2748 | sub on_refresh { |
2918 | sub on_refresh { |
2749 | my ($self, $id, $cb) = @_; |
2919 | my ($self, $id, $cb) = @_; |
2750 | |
2920 | |
2751 | $self->{refresh_hook}{$id} = $cb; |
2921 | $self->{refresh_hook}{$id} = $cb; |
2752 | } |
2922 | } |
2753 | |
2923 | |
|
|
2924 | sub on_post_alloc { |
|
|
2925 | my ($self, $id, $cb) = @_; |
|
|
2926 | |
|
|
2927 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2928 | } |
|
|
2929 | |
2754 | sub draw { |
2930 | sub draw { |
2755 | my ($self) = @_; |
2931 | my ($self) = @_; |
|
|
2932 | |
|
|
2933 | while ($self->{refresh_hook}) { |
|
|
2934 | $_->() |
|
|
2935 | for values %{delete $self->{refresh_hook}}; |
|
|
2936 | } |
2756 | |
2937 | |
2757 | if ($self->{check_size}) { |
2938 | if ($self->{check_size}) { |
2758 | my @queue = ([], []); |
2939 | my @queue = ([], []); |
2759 | |
2940 | |
2760 | for (;;) { |
2941 | for (;;) { |
… | |
… | |
2768 | |
2949 | |
2769 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
2950 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
2770 | ? @$widget{qw(user_w user_h)} |
2951 | ? @$widget{qw(user_w user_h)} |
2771 | : $widget->size_request; |
2952 | : $widget->size_request; |
2772 | |
2953 | |
|
|
2954 | if (delete $widget->{force_alloc} |
2773 | if ($w != $widget->{req_w} || $h != $widget->{req_h}) { |
2955 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
2956 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2957 | |
2774 | $widget->{req_w} = $w; |
2958 | $widget->{req_w} = $w; |
2775 | $widget->{req_h} = $h; |
2959 | $widget->{req_h} = $h; |
2776 | |
2960 | |
2777 | $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; |
2961 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
2778 | |
2962 | |
2779 | $widget->{parent}->check_size |
2963 | $widget->{parent}->check_size |
2780 | if $widget->{parent}; |
2964 | if $widget->{parent}; |
2781 | } |
2965 | } |
2782 | } |
2966 | } |
2783 | } |
2967 | } |
2784 | |
2968 | |
2785 | while ($self->{size_alloc}) { |
2969 | while ($self->{size_alloc}) { |
2786 | for (values %{delete $self->{size_alloc}}) { |
2970 | for (values %{delete $self->{size_alloc}}) { |
2787 | my ($widget, $w, $h) = @$_; |
2971 | my ($widget, $w, $h) = @$_; |
|
|
2972 | |
|
|
2973 | $w = 0 if $w < 0; |
|
|
2974 | $h = 0 if $h < 0; |
2788 | |
2975 | |
2789 | $widget->{w} = $w; |
2976 | $widget->{w} = $w; |
2790 | $widget->{h} = $h; |
2977 | $widget->{h} = $h; |
2791 | $widget->size_allocate ($w, $h); |
2978 | $widget->size_allocate ($w, $h); |
2792 | $widget->emit (size_allocate => $w, $h); |
2979 | $widget->emit (size_allocate => $w, $h); |
2793 | } |
2980 | } |
2794 | } |
2981 | } |
2795 | |
2982 | |
2796 | while ($self->{refresh_hook}) { |
2983 | while ($self->{post_alloc_hook}) { |
2797 | $_->() |
2984 | $_->() |
2798 | for values %{delete $self->{refresh_hook}}; |
2985 | for values %{delete $self->{post_alloc_hook}}; |
2799 | } |
2986 | } |
2800 | |
2987 | |
2801 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2988 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2802 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2989 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2803 | glClear GL_COLOR_BUFFER_BIT; |
2990 | glClear GL_COLOR_BUFFER_BIT; |