… | |
… | |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | |
12 | |
13 | our $ROOT; |
13 | our $ROOT; |
14 | our $TOOLTIP; |
14 | our $TOOLTIP; |
15 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
|
|
16 | |
|
|
17 | our %WIDGET; # all widgets, weak-referenced |
16 | |
18 | |
17 | sub check_tooltip { |
19 | sub check_tooltip { |
18 | if (!$GRAB) { |
20 | if (!$GRAB) { |
19 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
21 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
20 | if (length $widget->{tooltip}) { |
22 | if (length $widget->{tooltip}) { |
… | |
… | |
24 | |
26 | |
25 | my $tip = $widget->{tooltip}; |
27 | my $tip = $widget->{tooltip}; |
26 | |
28 | |
27 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
29 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
28 | |
30 | |
29 | $TOOLTIP->set_markup ($widget->{tooltip}); |
31 | $TOOLTIP->set_tooltip_from ($widget); |
30 | |
|
|
31 | $TOOLTIP->show; |
32 | $TOOLTIP->show; |
32 | |
33 | |
33 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
34 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
34 | |
35 | |
35 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
|
|
36 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
36 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) |
37 | } |
37 | if $x + $TOOLTIP->{w} > $::WIDTH; |
38 | |
38 | |
39 | $TOOLTIP->move ($x, $y); |
39 | $TOOLTIP->move ($x, $y); |
|
|
40 | $TOOLTIP->check_size; |
|
|
41 | $TOOLTIP->update; |
40 | } |
42 | } |
41 | |
43 | |
42 | return; |
44 | return; |
43 | } |
45 | } |
44 | } |
46 | } |
… | |
… | |
131 | |
133 | |
132 | for (@$vals) { |
134 | for (@$vals) { |
133 | my $i = int $_ + $rem; |
135 | my $i = int $_ + $rem; |
134 | $rem += $_ - $i; |
136 | $rem += $_ - $i; |
135 | $_ = $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; |
136 | } |
158 | } |
137 | } |
159 | } |
138 | |
160 | |
139 | ############################################################################# |
161 | ############################################################################# |
140 | |
162 | |
… | |
… | |
159 | if (/^connect_(.*)$/) { |
181 | if (/^connect_(.*)$/) { |
160 | $self->connect ($1 => delete $self->{$_}); |
182 | $self->connect ($1 => delete $self->{$_}); |
161 | } |
183 | } |
162 | } |
184 | } |
163 | |
185 | |
|
|
186 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
187 | |
164 | $self |
188 | $self |
165 | } |
189 | } |
166 | |
190 | |
167 | sub destroy { |
191 | sub destroy { |
168 | my ($self) = @_; |
192 | my ($self) = @_; |
… | |
… | |
241 | |
265 | |
242 | sub size_allocate { |
266 | sub size_allocate { |
243 | # nothing to be done |
267 | # nothing to be done |
244 | } |
268 | } |
245 | |
269 | |
|
|
270 | sub reconfigure { |
|
|
271 | my ($self) = @_; |
|
|
272 | |
|
|
273 | $self->check_size (1); |
|
|
274 | $self->update; |
|
|
275 | } |
|
|
276 | |
246 | sub children { |
277 | sub children { |
247 | } |
|
|
248 | |
|
|
249 | # call when resolution changes etc. |
|
|
250 | sub reconfigure { |
|
|
251 | my ($self) = @_; |
|
|
252 | |
|
|
253 | $_->reconfigure |
|
|
254 | for $self->children; |
|
|
255 | |
|
|
256 | $self->check_size; |
|
|
257 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}]; |
|
|
258 | $self->update; |
|
|
259 | } |
278 | } |
260 | |
279 | |
261 | sub set_max_size { |
280 | sub set_max_size { |
262 | my ($self, $w, $h) = @_; |
281 | my ($self, $w, $h) = @_; |
263 | |
282 | |
… | |
… | |
356 | glVertex $self->{w}, 0; |
375 | glVertex $self->{w}, 0; |
357 | glVertex $self->{w}, $self->{h}; |
376 | glVertex $self->{w}, $self->{h}; |
358 | glVertex 0 , $self->{h}; |
377 | glVertex 0 , $self->{h}; |
359 | glEnd; |
378 | glEnd; |
360 | glPopMatrix; |
379 | glPopMatrix; |
361 | CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
380 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
362 | } |
381 | } |
363 | } |
382 | } |
364 | |
383 | |
365 | sub _draw { |
384 | sub _draw { |
366 | my ($self) = @_; |
385 | my ($self) = @_; |
… | |
… | |
383 | sub set_parent { |
402 | sub set_parent { |
384 | my ($self, $parent) = @_; |
403 | my ($self, $parent) = @_; |
385 | |
404 | |
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
405 | Scalar::Util::weaken ($self->{parent} = $parent); |
387 | |
406 | |
|
|
407 | # TODO: req_w _does_change after ->reconfigure |
388 | $self->check_size |
408 | $self->check_size |
389 | unless exists $self->{req_w}; |
409 | unless exists $self->{req_w}; |
390 | } |
410 | } |
391 | |
411 | |
392 | sub check_size { |
412 | sub check_size { |
393 | my ($self) = @_; |
413 | my ($self, $forced) = @_; |
394 | |
414 | |
|
|
415 | $self->{force_alloc} = 1 if $forced; |
395 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
416 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
396 | } |
417 | } |
397 | |
418 | |
398 | sub update { |
419 | sub update { |
399 | my ($self) = @_; |
420 | my ($self) = @_; |
… | |
… | |
415 | } |
436 | } |
416 | |
437 | |
417 | sub DESTROY { |
438 | sub DESTROY { |
418 | my ($self) = @_; |
439 | my ($self) = @_; |
419 | |
440 | |
|
|
441 | delete $WIDGET{$self+0}; |
420 | #$self->deactivate; |
442 | #$self->deactivate; |
421 | } |
443 | } |
422 | |
444 | |
423 | ############################################################################# |
445 | ############################################################################# |
424 | |
446 | |
… | |
… | |
509 | $self->{children} = [ |
531 | $self->{children} = [ |
510 | sort { $a->{z} <=> $b->{z} } |
532 | sort { $a->{z} <=> $b->{z} } |
511 | @{$self->{children}}, @widgets |
533 | @{$self->{children}}, @widgets |
512 | ]; |
534 | ]; |
513 | |
535 | |
514 | $self->check_size; |
536 | $self->check_size (1); |
515 | $self->update; |
537 | $self->update; |
516 | } |
538 | } |
517 | |
539 | |
518 | sub children { |
540 | sub children { |
519 | @{ $_[0]{children} } |
541 | @{ $_[0]{children} } |
… | |
… | |
626 | } |
648 | } |
627 | |
649 | |
628 | sub update { |
650 | sub update { |
629 | my ($self) = @_; |
651 | my ($self) = @_; |
630 | |
652 | |
631 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
653 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
632 | $self->SUPER::update; |
654 | $self->SUPER::update; |
633 | } |
655 | } |
634 | |
656 | |
635 | sub size_allocate { |
657 | sub size_allocate { |
636 | my ($self, $w, $h) = @_; |
658 | my ($self, $w, $h) = @_; |
… | |
… | |
774 | $self->add ($self->{slider}); |
796 | $self->add ($self->{slider}); |
775 | |
797 | |
776 | $self |
798 | $self |
777 | } |
799 | } |
778 | |
800 | |
779 | #TODO# update range on size_allocate depeneing on child |
801 | #TODO# update range on size_allocate depending on child |
780 | # update viewport offset on scroll |
802 | # update viewport offset on scroll |
781 | |
803 | |
782 | ############################################################################# |
804 | ############################################################################# |
783 | |
805 | |
784 | package CFClient::UI::Frame; |
806 | package CFClient::UI::Frame; |
… | |
… | |
788 | use CFClient::OpenGL; |
810 | use CFClient::OpenGL; |
789 | |
811 | |
790 | sub new { |
812 | sub new { |
791 | my $class = shift; |
813 | my $class = shift; |
792 | |
814 | |
793 | my $self = $class->SUPER::new ( |
815 | $class->SUPER::new ( |
794 | bg => [1, 1, 1, 1], |
816 | bg => undef, |
795 | border_bg => [1, 1, 1, 1], |
|
|
796 | border => 0.8, |
|
|
797 | @_ |
817 | @_, |
798 | ); |
818 | ) |
799 | |
|
|
800 | $self |
|
|
801 | } |
819 | } |
802 | |
820 | |
803 | sub _draw { |
821 | sub _draw { |
804 | my ($self) = @_; |
822 | my ($self) = @_; |
805 | |
823 | |
806 | my ($w, $h) = ($self->{w}, $self->{h}); |
824 | if ($self->{bg}) { |
|
|
825 | my ($w, $h) = @$self{qw(w h)}; |
807 | |
826 | |
808 | glEnable GL_BLEND; |
827 | glEnable GL_BLEND; |
809 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
828 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
810 | glEnable GL_TEXTURE_2D; |
829 | glColor @{ $self->{bg} }; |
811 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
812 | |
830 | |
813 | # glBegin GL_QUADS; |
831 | glBegin GL_QUADS; |
814 | # glColor 0, 0, 0, 0; |
|
|
815 | # glVertex 0 , 0; |
832 | glVertex 0 , 0; |
816 | # glVertex 0 , $h; |
833 | glVertex 0 , $h; |
817 | # glVertex $w, $h; |
834 | glVertex $w, $h; |
818 | # glVertex $w, 0; |
835 | glVertex $w, 0; |
819 | # glEnd; |
836 | glEnd; |
820 | |
837 | |
821 | |
|
|
822 | $self->child->draw; |
|
|
823 | glDisable GL_BLEND; |
838 | glDisable GL_BLEND; |
824 | glDisable GL_TEXTURE_2D; |
839 | } |
|
|
840 | |
|
|
841 | $self->SUPER::_draw; |
825 | } |
842 | } |
826 | |
843 | |
827 | ############################################################################# |
844 | ############################################################################# |
828 | |
845 | |
829 | package CFClient::UI::FancyFrame; |
846 | package CFClient::UI::FancyFrame; |
… | |
… | |
843 | |
860 | |
844 | my $self = $class->SUPER::new ( |
861 | my $self = $class->SUPER::new ( |
845 | bg => [1, 1, 1, 1], |
862 | bg => [1, 1, 1, 1], |
846 | border_bg => [1, 1, 1, 1], |
863 | border_bg => [1, 1, 1, 1], |
847 | border => 0.6, |
864 | border => 0.6, |
|
|
865 | toplevel => 1, |
848 | can_events => 1, |
866 | can_events => 1, |
849 | @_ |
867 | @_ |
850 | ); |
868 | ); |
851 | |
869 | |
852 | $self->{title} &&= new CFClient::UI::Label |
870 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
945 | my ($self) = @_; |
963 | my ($self) = @_; |
946 | |
964 | |
947 | my ($w, $h ) = ($self->{w}, $self->{h}); |
965 | my ($w, $h ) = ($self->{w}, $self->{h}); |
948 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
966 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
949 | |
967 | |
950 | glEnable GL_BLEND; |
|
|
951 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
952 | glEnable GL_TEXTURE_2D; |
968 | glEnable GL_TEXTURE_2D; |
953 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
969 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
954 | |
970 | |
955 | my $border = $self->border; |
971 | my $border = $self->border; |
956 | |
972 | |
957 | glColor @{ $self->{border_bg} }; |
973 | glColor @{ $self->{border_bg} }; |
958 | $tex[1]->draw_quad (0, 0, $w, $border); |
974 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
959 | $tex[3]->draw_quad (0, $border, $border, $ch); |
975 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
960 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
976 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
961 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
977 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
962 | |
978 | |
963 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
979 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
964 | my $bg = $tex[0]; |
980 | my $bg = $tex[0]; |
965 | |
981 | |
966 | # TODO: repeat texture not scale |
982 | # TODO: repeat texture not scale |
… | |
… | |
970 | glColor @{ $self->{bg} }; |
986 | glColor @{ $self->{bg} }; |
971 | |
987 | |
972 | $bg->{s} = $rep_x; |
988 | $bg->{s} = $rep_x; |
973 | $bg->{t} = $rep_y; |
989 | $bg->{t} = $rep_y; |
974 | $bg->{wrap_mode} = 1; |
990 | $bg->{wrap_mode} = 1; |
975 | $bg->draw_quad ($border, $border, $cw, $ch); |
991 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
992 | } |
976 | |
993 | |
977 | glDisable GL_TEXTURE_2D; |
994 | glDisable GL_TEXTURE_2D; |
978 | glDisable GL_BLEND; |
|
|
979 | } |
|
|
980 | |
995 | |
981 | $self->{title}->draw if $self->{title}; |
996 | $self->{title}->draw if $self->{title}; |
982 | |
997 | |
983 | $self->child->draw; |
998 | $self->child->draw; |
984 | } |
999 | } |
… | |
… | |
1486 | my $sym = $ev->{sym}; |
1501 | my $sym = $ev->{sym}; |
1487 | my $uni = $ev->{unicode}; |
1502 | my $uni = $ev->{unicode}; |
1488 | |
1503 | |
1489 | my $text = $self->get_text; |
1504 | my $text = $self->get_text; |
1490 | |
1505 | |
1491 | if ($sym == 8) { |
1506 | if ($uni == 8) { |
1492 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1507 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1493 | } elsif ($sym == 127) { |
1508 | } elsif ($uni == 127) { |
1494 | substr $text, $self->{cursor}, 1, ""; |
1509 | substr $text, $self->{cursor}, 1, ""; |
1495 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1510 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1496 | --$self->{cursor} if $self->{cursor}; |
1511 | --$self->{cursor} if $self->{cursor}; |
1497 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1512 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1498 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1513 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1499 | } elsif ($sym == CFClient::SDLK_HOME) { |
1514 | } elsif ($sym == CFClient::SDLK_HOME) { |
1500 | $self->{cursor} = 0; |
1515 | $self->{cursor} = 0; |
1501 | } elsif ($sym == CFClient::SDLK_END) { |
1516 | } elsif ($sym == CFClient::SDLK_END) { |
1502 | $self->{cursor} = length $text; |
1517 | $self->{cursor} = length $text; |
1503 | } elsif ($sym == 27) { |
1518 | } elsif ($uni == 27) { |
1504 | $self->emit ('escape'); |
1519 | $self->emit ('escape'); |
1505 | } elsif ($uni) { |
1520 | } elsif ($uni) { |
1506 | substr $text, $self->{cursor}++, 0, chr $uni; |
1521 | substr $text, $self->{cursor}++, 0, chr $uni; |
1507 | } |
1522 | } |
1508 | |
1523 | |
… | |
… | |
1672 | |
1687 | |
1673 | if ($GRAB == $self) { |
1688 | if ($GRAB == $self) { |
1674 | $self->{fg} = $self->{active_fg}; |
1689 | $self->{fg} = $self->{active_fg}; |
1675 | } |
1690 | } |
1676 | |
1691 | |
1677 | glEnable GL_BLEND; |
|
|
1678 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1679 | glEnable GL_TEXTURE_2D; |
1692 | glEnable GL_TEXTURE_2D; |
1680 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1693 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1681 | glColor 0, 0, 0, 1; |
1694 | glColor 0, 0, 0, 1; |
1682 | |
1695 | |
1683 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1696 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1684 | |
1697 | |
1685 | glDisable GL_TEXTURE_2D; |
1698 | glDisable GL_TEXTURE_2D; |
1686 | glDisable GL_BLEND; |
|
|
1687 | |
1699 | |
1688 | $self->SUPER::_draw; |
1700 | $self->SUPER::_draw; |
1689 | } |
1701 | } |
1690 | |
1702 | |
1691 | ############################################################################# |
1703 | ############################################################################# |
… | |
… | |
1738 | |
1750 | |
1739 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1751 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1740 | |
1752 | |
1741 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1753 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1742 | |
1754 | |
1743 | glEnable GL_BLEND; |
1755 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1756 | |
1744 | glEnable GL_TEXTURE_2D; |
1757 | glEnable GL_TEXTURE_2D; |
1745 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1746 | |
|
|
1747 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1748 | |
|
|
1749 | $tex->draw_quad (0, 0, $s, $s); |
1758 | $tex->draw_quad_alpha (0, 0, $s, $s); |
1750 | |
|
|
1751 | glDisable GL_TEXTURE_2D; |
1759 | glDisable GL_TEXTURE_2D; |
1752 | glDisable GL_BLEND; |
|
|
1753 | } |
1760 | } |
1754 | |
1761 | |
1755 | ############################################################################# |
1762 | ############################################################################# |
1756 | |
1763 | |
1757 | package CFClient::UI::Image; |
1764 | package CFClient::UI::Image; |
… | |
… | |
1800 | glTranslate 0, -$self->{w}, 0; |
1807 | glTranslate 0, -$self->{w}, 0; |
1801 | |
1808 | |
1802 | ($w, $h) = ($h, $w); |
1809 | ($w, $h) = ($h, $w); |
1803 | } |
1810 | } |
1804 | |
1811 | |
1805 | glEnable GL_BLEND; |
|
|
1806 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1807 | glEnable GL_TEXTURE_2D; |
1812 | glEnable GL_TEXTURE_2D; |
1808 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1813 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1809 | |
1814 | |
1810 | $tex->draw_quad (0, 0, $w, $h); |
1815 | $tex->draw_quad_alpha (0, 0, $w, $h); |
1811 | |
1816 | |
1812 | glDisable GL_BLEND; |
|
|
1813 | glDisable GL_TEXTURE_2D; |
1817 | glDisable GL_TEXTURE_2D; |
1814 | } |
1818 | } |
1815 | |
1819 | |
1816 | ############################################################################# |
1820 | ############################################################################# |
1817 | |
1821 | |
… | |
… | |
2006 | qw(s1_slider.png s1_slider_bg.png); |
2010 | qw(s1_slider.png s1_slider_bg.png); |
2007 | |
2011 | |
2008 | sub new { |
2012 | sub new { |
2009 | my $class = shift; |
2013 | my $class = shift; |
2010 | |
2014 | |
2011 | # range [value, low, high, page] |
2015 | # range [value, low, high, page, unit] |
2012 | |
2016 | |
2013 | # TODO: 0-width page |
2017 | # TODO: 0-width page |
2014 | # TODO: req_w/h are wrong with vertical |
2018 | # TODO: req_w/h are wrong with vertical |
2015 | # TODO: calculations are off |
2019 | # TODO: calculations are off |
2016 | my $self = $class->SUPER::new ( |
2020 | my $self = $class->SUPER::new ( |
… | |
… | |
2019 | range => [0, 0, 100, 10], |
2023 | range => [0, 0, 100, 10], |
2020 | req_w => $::WIDTH / 80, |
2024 | req_w => $::WIDTH / 80, |
2021 | req_h => $::WIDTH / 80, |
2025 | req_h => $::WIDTH / 80, |
2022 | vertical => 0, |
2026 | vertical => 0, |
2023 | can_hover => 1, |
2027 | can_hover => 1, |
2024 | inner_pad => 5, |
2028 | inner_pad => .05, |
2025 | @_ |
2029 | @_ |
2026 | ); |
2030 | ); |
2027 | |
2031 | |
|
|
2032 | $self->set_value ($self->{range}[0]); |
|
|
2033 | $self->update; |
|
|
2034 | |
2028 | $self |
2035 | $self |
|
|
2036 | } |
|
|
2037 | |
|
|
2038 | sub set_value { |
|
|
2039 | my ($self, $value) = @_; |
|
|
2040 | |
|
|
2041 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
|
|
2042 | |
|
|
2043 | $hi = $lo + 1 if $hi <= $lo; |
|
|
2044 | |
|
|
2045 | $value = $lo if $value < $lo; |
|
|
2046 | $value = $hi if $value > $hi; |
|
|
2047 | |
|
|
2048 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
|
|
2049 | if $unit; |
|
|
2050 | |
|
|
2051 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2052 | |
|
|
2053 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
|
|
2054 | |
|
|
2055 | if ($value != $old_value) { |
|
|
2056 | $self->emit (changed => $value); |
|
|
2057 | $self->update; |
|
|
2058 | } |
2029 | } |
2059 | } |
2030 | |
2060 | |
2031 | sub size_request { |
2061 | sub size_request { |
2032 | my ($self) = @_; |
2062 | my ($self) = @_; |
2033 | |
2063 | |
… | |
… | |
2046 | |
2076 | |
2047 | sub mouse_motion { |
2077 | sub mouse_motion { |
2048 | my ($self, $ev, $x, $y) = @_; |
2078 | my ($self, $ev, $x, $y) = @_; |
2049 | |
2079 | |
2050 | if ($GRAB == $self) { |
2080 | if ($GRAB == $self) { |
|
|
2081 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
2082 | |
|
|
2083 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2084 | |
|
|
2085 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
|
|
2086 | |
|
|
2087 | $self->set_value ($x * ($hi - $lo) + $lo); |
|
|
2088 | } |
|
|
2089 | } |
|
|
2090 | |
|
|
2091 | sub update { |
|
|
2092 | my ($self) = @_; |
|
|
2093 | |
|
|
2094 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
|
|
2095 | $self->set_value ($self->{range}[0]); |
|
|
2096 | |
2051 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2097 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2052 | |
2098 | |
2053 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2099 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2054 | |
2100 | |
2055 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
2101 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2056 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2057 | |
2102 | |
2058 | $x -= $inner_pad_px; # substract the padding |
2103 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2059 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
2104 | $value = $self->{scale} * ($value - $lo); |
2060 | $x = $lo if $x < $lo; |
|
|
2061 | $x = $hi - $page if $x > $hi - $page; |
|
|
2062 | $self->{range}[0] = $x; |
|
|
2063 | |
2105 | |
2064 | $self->emit (changed => $x); |
2106 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
2065 | $self->update; |
2107 | |
|
|
2108 | $value = 0 if $value < 0; |
|
|
2109 | $page = 1 - $value if $value + $page > 1; |
|
|
2110 | |
|
|
2111 | $self->{knob_x} = $value; |
|
|
2112 | $self->{knob_w} = $page; |
2066 | } |
2113 | }); |
2067 | } |
|
|
2068 | |
2114 | |
2069 | # the inner_* stuff is for generating a padding for the slider handle, |
2115 | $self->SUPER::update; |
2070 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
2071 | # correct propably, but it does the job for now |
|
|
2072 | sub _calc_inner_pad_px { |
|
|
2073 | my ($self, $w) = @_; |
|
|
2074 | ($w / 100) * $self->{inner_pad} # % to pixels |
|
|
2075 | } |
2116 | } |
2076 | |
2117 | |
2077 | sub _draw { |
2118 | sub _draw { |
2078 | my ($self) = @_; |
2119 | my ($self) = @_; |
2079 | |
2120 | |
2080 | $self->SUPER::_draw (); |
2121 | $self->SUPER::_draw (); |
2081 | |
2122 | |
2082 | my ($w, $h) = @$self{qw(w h)}; |
2123 | glScale $self->{w}, $self->{h}; |
2083 | |
2124 | |
2084 | if ($self->{vertical}) { |
2125 | if ($self->{vertical}) { |
2085 | # draw a vertical slider like a rotated horizontal slider |
2126 | # draw a vertical slider like a rotated horizontal slider |
2086 | |
2127 | |
2087 | glRotate 90, 0, 0, 1; |
2128 | glRotate 90, 0, 0, 1; |
2088 | glTranslate 0, -$self->{w}, 0; |
2129 | glTranslate 0, 1, 0; |
2089 | |
|
|
2090 | ($w, $h) = ($h, $w); |
|
|
2091 | } |
2130 | } |
2092 | |
2131 | |
2093 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2132 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2094 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2133 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2095 | |
2134 | |
2096 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2097 | |
|
|
2098 | $hi = $value + 1 if $lo == $hi; |
|
|
2099 | |
|
|
2100 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
2101 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2102 | |
|
|
2103 | $page = int $page * $inner_w / ($hi - $lo); |
|
|
2104 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
|
|
2105 | |
|
|
2106 | $w -= $page; |
|
|
2107 | $page &= ~1; |
|
|
2108 | glTranslate $page * 0.5, 0, 0; |
|
|
2109 | $page ||= 2; |
|
|
2110 | |
|
|
2111 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
|
|
2112 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
|
|
2113 | |
|
|
2114 | glEnable GL_BLEND; |
|
|
2115 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2116 | glEnable GL_TEXTURE_2D; |
2135 | glEnable GL_TEXTURE_2D; |
2117 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2136 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2118 | |
2137 | |
2119 | # draw background |
2138 | # draw background |
2120 | $tex[1]->draw_quad (0, 0, $w, $h); |
2139 | $tex[1]->draw_quad_alpha (0, 0, 1, 1); |
2121 | |
2140 | |
2122 | # draw handle |
2141 | # draw handle |
2123 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
2142 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2124 | |
2143 | |
2125 | glDisable GL_BLEND; |
|
|
2126 | glDisable GL_TEXTURE_2D; |
2144 | glDisable GL_TEXTURE_2D; |
2127 | } |
2145 | } |
2128 | |
2146 | |
2129 | ############################################################################# |
2147 | ############################################################################# |
2130 | |
2148 | |
… | |
… | |
2141 | fontsize => 1, |
2159 | fontsize => 1, |
2142 | can_events => 0, |
2160 | can_events => 0, |
2143 | #font => default_font |
2161 | #font => default_font |
2144 | @_, |
2162 | @_, |
2145 | |
2163 | |
2146 | layout => (new CFClient::Layout), |
2164 | layout => (new CFClient::Layout 1), |
2147 | par => [], |
2165 | par => [], |
2148 | height => 0, |
2166 | height => 0, |
2149 | children => [ |
2167 | children => [ |
2150 | (new CFClient::UI::Empty expand => 1), |
2168 | (new CFClient::UI::Empty expand => 1), |
2151 | (new CFClient::UI::Slider vertical => 1), |
2169 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2169 | |
2187 | |
2170 | my $layout = $self->{layout}; |
2188 | my $layout = $self->{layout}; |
2171 | |
2189 | |
2172 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2190 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2173 | $layout->set_width ($self->{children}[0]{w}); |
2191 | $layout->set_width ($self->{children}[0]{w}); |
2174 | $layout->set_text ($text); |
2192 | $layout->set_markup ($text); |
2175 | |
2193 | |
2176 | ($layout->size)[1] |
2194 | ($layout->size)[1] |
2177 | } |
2195 | } |
2178 | |
2196 | |
2179 | sub reflow { |
2197 | sub reflow { |
… | |
… | |
2217 | |
2235 | |
2218 | return unless $self->{h} > 0; |
2236 | return unless $self->{h} > 0; |
2219 | |
2237 | |
2220 | delete $self->{texture}; |
2238 | delete $self->{texture}; |
2221 | |
2239 | |
2222 | $ROOT->on_refresh ($self, sub { |
2240 | $ROOT->on_post_alloc ($self, sub { |
2223 | if (delete $self->{need_reflow}) { |
2241 | if (delete $self->{need_reflow}) { |
2224 | my $height = 0; |
2242 | my $height = 0; |
2225 | |
2243 | |
2226 | $height += $_->[0] = $self->text_height ($_->[2]) |
2244 | $height += $_->[0] = $self->text_height ($_->[2]) |
2227 | for @{$self->{par}}; |
2245 | for @{$self->{par}}; |
… | |
… | |
2235 | |
2253 | |
2236 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2254 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2237 | glClearColor 0, 0, 0, 0; |
2255 | glClearColor 0, 0, 0, 0; |
2238 | glClear GL_COLOR_BUFFER_BIT; |
2256 | glClear GL_COLOR_BUFFER_BIT; |
2239 | |
2257 | |
2240 | glEnable GL_BLEND; |
|
|
2241 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2242 | glEnable GL_TEXTURE_2D; |
2258 | glEnable GL_TEXTURE_2D; |
2243 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2259 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2244 | |
2260 | |
2245 | my $top = int $self->{children}[1]{range}[0]; |
2261 | my $top = int $self->{children}[1]{range}[0]; |
2246 | |
2262 | |
… | |
… | |
2255 | |
2271 | |
2256 | for my $par (@{$self->{par}}) { |
2272 | for my $par (@{$self->{par}}) { |
2257 | my $h = $par->[0]; |
2273 | my $h = $par->[0]; |
2258 | |
2274 | |
2259 | if ($y0 < $y + $h && $y < $y1) { |
2275 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2276 | $layout->set_foreground (@{ $par->[1] }); |
2260 | $layout->set_text ($par->[2]); |
2277 | $layout->set_markup ($par->[2]); |
2261 | |
2278 | |
2262 | glColor @{ $par->[1] }; |
|
|
2263 | my ($W, $H) = $layout->size; |
2279 | my ($W, $H) = $layout->size; |
2264 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
2280 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2265 | } |
2281 | } |
2266 | |
2282 | |
2267 | $y += $h; |
2283 | $y += $h; |
2268 | } |
2284 | } |
2269 | |
2285 | |
2270 | glDisable GL_TEXTURE_2D; |
2286 | glDisable GL_TEXTURE_2D; |
2271 | glDisable GL_BLEND; |
|
|
2272 | }; |
2287 | }; |
2273 | }); |
2288 | }); |
2274 | } |
2289 | } |
2275 | |
2290 | |
2276 | sub _draw { |
2291 | sub _draw { |
2277 | my ($self) = @_; |
2292 | my ($self) = @_; |
2278 | |
2293 | |
2279 | glEnable GL_BLEND; |
|
|
2280 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2281 | glEnable GL_TEXTURE_2D; |
2294 | glEnable GL_TEXTURE_2D; |
2282 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2295 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2283 | glColor 1, 1, 1, 1; |
2296 | glColor 1, 1, 1, 1; |
2284 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2297 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2285 | glDisable GL_TEXTURE_2D; |
2298 | glDisable GL_TEXTURE_2D; |
2286 | glDisable GL_BLEND; |
|
|
2287 | |
2299 | |
2288 | $self->{children}[1]->draw; |
2300 | $self->{children}[1]->draw; |
2289 | |
2301 | |
2290 | } |
2302 | } |
2291 | |
2303 | |
… | |
… | |
2385 | @_, |
2397 | @_, |
2386 | can_events => 0, |
2398 | can_events => 0, |
2387 | ) |
2399 | ) |
2388 | } |
2400 | } |
2389 | |
2401 | |
2390 | sub set_markup { |
2402 | sub set_tooltip_from { |
2391 | my ($self, $text) = @_; |
2403 | my ($self, $widget) = @_; |
2392 | |
2404 | |
2393 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2405 | $self->add (new CFClient::UI::Label |
2394 | $self->{label}->set_markup ($text); |
2406 | markup => $widget->{tooltip}, |
2395 | $self->add ($self->{label}); |
2407 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
2408 | fontsize => 0.8, |
|
|
2409 | fg => [0, 0, 0, 1], |
|
|
2410 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
|
|
2411 | ); |
2396 | } |
2412 | } |
2397 | |
2413 | |
2398 | sub size_request { |
2414 | sub size_request { |
2399 | my ($self) = @_; |
2415 | my ($self) = @_; |
2400 | |
|
|
2401 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2402 | |
2416 | |
2403 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2417 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2404 | |
2418 | |
2405 | ($w + 4, $h + 4) |
2419 | ($w + 4, $h + 4) |
2406 | } |
2420 | } |
… | |
… | |
2412 | } |
2426 | } |
2413 | |
2427 | |
2414 | sub _draw { |
2428 | sub _draw { |
2415 | my ($self) = @_; |
2429 | my ($self) = @_; |
2416 | |
2430 | |
2417 | glPushMatrix; |
|
|
2418 | glTranslate 0.375, 0.375; |
2431 | glTranslate 0.375, 0.375; |
2419 | |
2432 | |
2420 | my ($w, $h) = @$self{qw(w h)}; |
2433 | my ($w, $h) = @$self{qw(w h)}; |
2421 | |
2434 | |
2422 | glColor 1, 0.8, 0.4; |
2435 | glColor 1, 0.8, 0.4; |
… | |
… | |
2433 | glVertex 0 , $h; |
2446 | glVertex 0 , $h; |
2434 | glVertex $w, $h; |
2447 | glVertex $w, $h; |
2435 | glVertex $w, 0; |
2448 | glVertex $w, 0; |
2436 | glEnd; |
2449 | glEnd; |
2437 | |
2450 | |
2438 | glPopMatrix; |
2451 | glTranslate 2 - 0.375, 2 - 0.375; |
2439 | |
|
|
2440 | glTranslate 2, 2; |
|
|
2441 | $self->SUPER::_draw; |
2452 | $self->SUPER::_draw; |
2442 | } |
2453 | } |
2443 | |
2454 | |
2444 | ############################################################################# |
2455 | ############################################################################# |
2445 | |
2456 | |
… | |
… | |
2468 | return unless $::CONN;#d# manage and cache textures differently |
2479 | return unless $::CONN;#d# manage and cache textures differently |
2469 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2480 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2470 | |
2481 | |
2471 | # TODO animation |
2482 | # TODO animation |
2472 | if ($tex) { |
2483 | if ($tex) { |
2473 | glEnable GL_BLEND; |
|
|
2474 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2475 | glEnable GL_TEXTURE_2D; |
2484 | glEnable GL_TEXTURE_2D; |
2476 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2485 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2477 | glColor 1, 1, 1, 1; |
2486 | glColor 1, 1, 1, 1; |
2478 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2487 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2479 | glDisable GL_TEXTURE_2D; |
2488 | glDisable GL_TEXTURE_2D; |
2480 | glDisable GL_BLEND; |
|
|
2481 | } |
2489 | } |
2482 | } |
2490 | } |
2483 | |
2491 | |
2484 | ############################################################################# |
2492 | ############################################################################# |
2485 | |
2493 | |
… | |
… | |
2496 | |
2504 | |
2497 | my $desc = $item->{nrof} < 2 |
2505 | my $desc = $item->{nrof} < 2 |
2498 | ? $item->{name} |
2506 | ? $item->{name} |
2499 | : "$item->{nrof} $item->{name_pl}"; |
2507 | : "$item->{nrof} $item->{name_pl}"; |
2500 | |
2508 | |
|
|
2509 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2510 | and $desc .= " (open)"; |
|
|
2511 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2512 | and $desc .= " (applied)"; |
|
|
2513 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2514 | and $desc .= " (unpaid)"; |
|
|
2515 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2516 | and $desc .= " (magic)"; |
|
|
2517 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2518 | and $desc .= " (cursed)"; |
|
|
2519 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2520 | and $desc .= " (damned)"; |
|
|
2521 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2522 | and $desc .= " *"; |
2501 | |
2523 | |
2502 | my $self = $class->SUPER::new ( |
2524 | my $self = $class->SUPER::new ( |
2503 | can_hover => 1, |
2525 | can_hover => 1, |
2504 | can_events => 1, |
2526 | can_events => 1, |
2505 | tooltip => (CFClient::UI::Label->escape ($desc) |
2527 | tooltip => (CFClient::UI::Label->escape ($desc) |
… | |
… | |
2511 | if ($ev->{button} == 1) { |
2533 | if ($ev->{button} == 1) { |
2512 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2534 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2513 | } elsif ($ev->{button} == 2) { |
2535 | } elsif ($ev->{button} == 2) { |
2514 | $::CONN->send ("apply $item->{tag}"); |
2536 | $::CONN->send ("apply $item->{tag}"); |
2515 | } elsif ($ev->{button} == 3) { |
2537 | } elsif ($ev->{button} == 3) { |
2516 | CFClient::UI::Menu->new ( |
2538 | my @menu_items = ( |
2517 | items => [ |
|
|
2518 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
2539 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2540 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2541 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2542 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
2519 | [ |
2543 | [ |
2520 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2544 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2521 | sub { $::CONN->send ("lock $item->{tag}") }, |
2545 | sub { $::CONN->send ("lock ". () ."$item->{tag}") }, |
2522 | ], |
|
|
2523 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2524 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2525 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2526 | ], |
2546 | ], |
2527 | )->popup ($ev); |
2547 | ); |
|
|
2548 | |
|
|
2549 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
2528 | } |
2550 | } |
2529 | |
2551 | |
2530 | 1 |
2552 | 1 |
2531 | }, |
2553 | }, |
2532 | %args |
2554 | %args |
… | |
… | |
2568 | my ($self, $items) = @_; |
2590 | my ($self, $items) = @_; |
2569 | |
2591 | |
2570 | $self->{scrolled}->clear; |
2592 | $self->{scrolled}->clear; |
2571 | return unless $items; |
2593 | return unless $items; |
2572 | |
2594 | |
2573 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
2595 | my @items = sort { |
|
|
2596 | ($a->{type} <=> $b->{type}) |
|
|
2597 | or ($a->{name} cmp $b->{name}) |
|
|
2598 | } @$items; |
2574 | |
2599 | |
2575 | $self->{real_items} = \@items; |
2600 | $self->{real_items} = \@items; |
2576 | |
2601 | |
2577 | for my $item (@items) { |
2602 | for my $item (@items) { |
2578 | my $desc = $item->{nrof} < 2 |
2603 | my $desc = $item->{nrof} < 2 |
2579 | ? $item->{name} |
2604 | ? $item->{name} |
2580 | : "$item->{nrof} $item->{name_pl}"; |
2605 | : "$item->{nrof} $item->{name_pl}"; |
2581 | |
2606 | |
2582 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2607 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2583 | } |
2608 | } |
|
|
2609 | |
|
|
2610 | $self->{scrolled}->add (@items); |
2584 | |
2611 | |
2585 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2612 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2586 | } |
2613 | } |
2587 | |
2614 | |
2588 | sub size_request { |
2615 | sub size_request { |
… | |
… | |
2675 | while (my ($k, $v) = each %{ $self->{item} }) { |
2702 | while (my ($k, $v) = each %{ $self->{item} }) { |
2676 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
2703 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
2677 | } |
2704 | } |
2678 | |
2705 | |
2679 | my @widgets; |
2706 | my @widgets; |
2680 | my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; |
2707 | |
|
|
2708 | my @items = sort { |
|
|
2709 | $a->{pri} <=> $b->{pri} |
|
|
2710 | or $b->{id} <=> $a->{id} |
|
|
2711 | } values %{ $self->{item} }; |
|
|
2712 | |
2681 | my $count = 10 + 1; |
2713 | my $count = 10 + 1; |
2682 | for my $item (@items) { |
2714 | for my $item (@items) { |
2683 | last unless --$count; |
2715 | last unless --$count; |
2684 | |
2716 | |
2685 | push @widgets, $item->{label} ||= do { |
2717 | push @widgets, $item->{label} ||= do { |
2686 | # TODO: doesn't handle markup well (read as: at all) |
2718 | # TODO: doesn't handle markup well (read as: at all) |
2687 | my $short = delete $item->{text}; |
2719 | my $short = $item->{count} > 1 |
|
|
2720 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2721 | : $item->{text}; |
|
|
2722 | |
2688 | for ($short) { |
2723 | for ($short) { |
2689 | s/^\s+//; |
2724 | s/^\s+//; |
2690 | s/\012.*//s; |
2725 | s/\s+/ /g; |
2691 | my $len = int 30 / $item->{fontsize}; |
2726 | my $len = int 40 / $item->{fontsize}; |
2692 | substr $_, $len, length, "…" if $len < length; |
2727 | substr $_, $len, length, "…" if $len < length; |
2693 | } |
2728 | } |
2694 | |
2729 | |
2695 | new CFClient::UI::Label |
2730 | new CFClient::UI::Label |
2696 | markup => $short, |
2731 | markup => $short, |
2697 | tooltip => delete $item->{tooltip}, |
2732 | tooltip => $item->{tooltip}, |
|
|
2733 | tooltip_font => $::FONT_PROP, |
|
|
2734 | tooltip_width => 0.67, |
2698 | fontsize => delete $item->{fontsize}, |
2735 | fontsize => $item->{fontsize}, |
2699 | color => delete $item->{color}, |
2736 | fg => $item->{fg}, |
2700 | can_events => 1, |
2737 | can_events => 1, |
2701 | can_hover => 1, |
2738 | can_hover => 1 |
2702 | }; |
2739 | }; |
2703 | } |
2740 | } |
2704 | |
2741 | |
2705 | $self->clear; |
2742 | $self->clear; |
2706 | $self->SUPER::add (@widgets); |
2743 | $self->SUPER::add (reverse @widgets); |
2707 | } |
2744 | } |
2708 | |
2745 | |
2709 | sub add { |
2746 | sub add { |
2710 | my ($self, $text, %arg) = @_; |
2747 | my ($self, $text, %arg) = @_; |
2711 | |
2748 | |
2712 | my $item = { |
2749 | $text =~ s/^\s+//; |
2713 | time => time, |
2750 | $text =~ s/\s+$//; |
|
|
2751 | |
|
|
2752 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2753 | |
|
|
2754 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2755 | |
|
|
2756 | if (my $item = $self->{item}{$group}) { |
|
|
2757 | if ($item->{text} eq $text) { |
|
|
2758 | $item->{count}++; |
|
|
2759 | } else { |
|
|
2760 | $item->{count} = 1; |
|
|
2761 | $item->{text} = $item->{tooltip} = $text; |
|
|
2762 | } |
|
|
2763 | $item->{id} = ++$self->{id}; |
|
|
2764 | $item->{timeout} = $timeout; |
|
|
2765 | delete $item->{label}; |
|
|
2766 | } else { |
|
|
2767 | $self->{item}{$group} = { |
|
|
2768 | id => ++$self->{id}, |
2714 | text => $text, |
2769 | text => $text, |
2715 | timeout => 60, |
2770 | timeout => $timeout, |
2716 | tooltip => $text, |
2771 | tooltip => $text, |
2717 | fontsize => 0.8, |
2772 | fontsize => 0.8, |
2718 | color => [0.8, 0.8, 0.8, 0.8], |
2773 | fg => [0.8, 0.8, 0.8, 0.8], |
|
|
2774 | pri => 0, |
|
|
2775 | count => 1, |
2719 | %arg, |
2776 | %arg, |
|
|
2777 | }; |
2720 | }; |
2778 | } |
2721 | |
|
|
2722 | $item->{timeout} += time; |
|
|
2723 | $item->{group} ||= $item+0; |
|
|
2724 | |
|
|
2725 | $item = $self->{item}{$item->{group}} ||= $item; |
|
|
2726 | |
2779 | |
2727 | $self->reorder; |
2780 | $self->reorder; |
2728 | } |
2781 | } |
2729 | |
2782 | |
2730 | ############################################################################# |
2783 | ############################################################################# |
… | |
… | |
2764 | } |
2817 | } |
2765 | |
2818 | |
2766 | sub size_allocate { |
2819 | sub size_allocate { |
2767 | my ($self, $w, $h) = @_; |
2820 | my ($self, $w, $h) = @_; |
2768 | |
2821 | |
2769 | my $old_w = $self->{old_w}; |
2822 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2770 | my $old_h = $self->{old_h}; |
2823 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2771 | |
2824 | |
2772 | if ($old_w && $old_h) { |
2825 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2773 | for my $child ($self->children) { |
2826 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
2774 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2775 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2776 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2777 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2778 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2779 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2780 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2781 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2782 | } |
|
|
2783 | } |
|
|
2784 | |
2827 | |
2785 | for my $child ($self->children) { |
2828 | for my $child ($self->children) { |
2786 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2829 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2787 | |
2830 | |
|
|
2831 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
|
|
2832 | if exists $child->{req_x}; |
|
|
2833 | |
|
|
2834 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
|
|
2835 | if exists $child->{req_y}; |
|
|
2836 | |
2788 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2837 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
2789 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2838 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
2839 | |
2790 | $child->configure ($X, $Y, $W, $H); |
2840 | $child->configure ($X, $Y, $W, $H); |
2791 | } |
2841 | } |
2792 | |
|
|
2793 | $self->{old_w} = $w; |
|
|
2794 | $self->{old_h} = $h; |
|
|
2795 | } |
2842 | } |
2796 | |
2843 | |
2797 | sub coord2local { |
2844 | sub coord2local { |
2798 | my ($self, $x, $y) = @_; |
2845 | my ($self, $x, $y) = @_; |
2799 | |
2846 | |
… | |
… | |
2812 | $self->check_size; |
2859 | $self->check_size; |
2813 | $::WANT_REFRESH++; |
2860 | $::WANT_REFRESH++; |
2814 | } |
2861 | } |
2815 | |
2862 | |
2816 | sub add { |
2863 | sub add { |
2817 | my ($self, $child) = @_; |
2864 | my ($self, @children) = @_; |
2818 | |
2865 | |
|
|
2866 | for my $child (@children) { |
|
|
2867 | $child->{toplevel} = 1; |
|
|
2868 | |
2819 | # integerise window positions |
2869 | # integerise window positions |
2820 | $child->{x} = int $child->{x}; |
2870 | $child->{x} = int $child->{x}; |
2821 | $child->{y} = int $child->{y}; |
2871 | $child->{y} = int $child->{y}; |
|
|
2872 | } |
2822 | |
2873 | |
2823 | $self->SUPER::add ($child); |
2874 | $self->SUPER::add (@children); |
2824 | } |
2875 | } |
2825 | |
2876 | |
2826 | sub on_refresh { |
2877 | sub on_refresh { |
2827 | my ($self, $id, $cb) = @_; |
2878 | my ($self, $id, $cb) = @_; |
2828 | |
2879 | |
2829 | $self->{refresh_hook}{$id} = $cb; |
2880 | $self->{refresh_hook}{$id} = $cb; |
2830 | } |
2881 | } |
2831 | |
2882 | |
|
|
2883 | sub on_post_alloc { |
|
|
2884 | my ($self, $id, $cb) = @_; |
|
|
2885 | |
|
|
2886 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2887 | } |
|
|
2888 | |
2832 | sub draw { |
2889 | sub draw { |
2833 | my ($self) = @_; |
2890 | my ($self) = @_; |
|
|
2891 | |
|
|
2892 | while ($self->{refresh_hook}) { |
|
|
2893 | $_->() |
|
|
2894 | for values %{delete $self->{refresh_hook}}; |
|
|
2895 | } |
2834 | |
2896 | |
2835 | if ($self->{check_size}) { |
2897 | if ($self->{check_size}) { |
2836 | my @queue = ([], []); |
2898 | my @queue = ([], []); |
2837 | |
2899 | |
2838 | for (;;) { |
2900 | for (;;) { |
… | |
… | |
2846 | |
2908 | |
2847 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
2909 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
2848 | ? @$widget{qw(user_w user_h)} |
2910 | ? @$widget{qw(user_w user_h)} |
2849 | : $widget->size_request; |
2911 | : $widget->size_request; |
2850 | |
2912 | |
|
|
2913 | if (delete $widget->{force_alloc} |
2851 | if ($w != $widget->{req_w} || $h != $widget->{req_h}) { |
2914 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
2852 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
2915 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
2853 | |
2916 | |
2854 | $widget->{req_w} = $w; |
2917 | $widget->{req_w} = $w; |
2855 | $widget->{req_h} = $h; |
2918 | $widget->{req_h} = $h; |
2856 | |
2919 | |
2857 | $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; |
2920 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
2858 | |
2921 | |
2859 | $widget->{parent}->check_size |
2922 | $widget->{parent}->check_size |
2860 | if $widget->{parent}; |
2923 | if $widget->{parent}; |
2861 | } |
2924 | } |
2862 | } |
2925 | } |
… | |
… | |
2874 | $widget->size_allocate ($w, $h); |
2937 | $widget->size_allocate ($w, $h); |
2875 | $widget->emit (size_allocate => $w, $h); |
2938 | $widget->emit (size_allocate => $w, $h); |
2876 | } |
2939 | } |
2877 | } |
2940 | } |
2878 | |
2941 | |
2879 | while ($self->{refresh_hook}) { |
2942 | while ($self->{post_alloc_hook}) { |
2880 | $_->() |
2943 | $_->() |
2881 | for values %{delete $self->{refresh_hook}}; |
2944 | for values %{delete $self->{post_alloc_hook}}; |
2882 | } |
2945 | } |
2883 | |
2946 | |
2884 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2947 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2885 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2948 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2886 | glClear GL_COLOR_BUFFER_BIT; |
2949 | glClear GL_COLOR_BUFFER_BIT; |