… | |
… | |
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}) { |
… | |
… | |
29 | $TOOLTIP->set_tooltip_from ($widget); |
31 | $TOOLTIP->set_tooltip_from ($widget); |
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); |
39 | $TOOLTIP->check_size; |
40 | $TOOLTIP->check_size; |
40 | $TOOLTIP->update; |
41 | $TOOLTIP->update; |
41 | } |
42 | } |
… | |
… | |
132 | |
133 | |
133 | for (@$vals) { |
134 | for (@$vals) { |
134 | my $i = int $_ + $rem; |
135 | my $i = int $_ + $rem; |
135 | $rem += $_ - $i; |
136 | $rem += $_ - $i; |
136 | $_ = $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; |
137 | } |
158 | } |
138 | } |
159 | } |
139 | |
160 | |
140 | ############################################################################# |
161 | ############################################################################# |
141 | |
162 | |
… | |
… | |
160 | if (/^connect_(.*)$/) { |
181 | if (/^connect_(.*)$/) { |
161 | $self->connect ($1 => delete $self->{$_}); |
182 | $self->connect ($1 => delete $self->{$_}); |
162 | } |
183 | } |
163 | } |
184 | } |
164 | |
185 | |
|
|
186 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
187 | |
165 | $self |
188 | $self |
166 | } |
189 | } |
167 | |
190 | |
168 | sub destroy { |
191 | sub destroy { |
169 | my ($self) = @_; |
192 | my ($self) = @_; |
… | |
… | |
242 | |
265 | |
243 | sub size_allocate { |
266 | sub size_allocate { |
244 | # nothing to be done |
267 | # nothing to be done |
245 | } |
268 | } |
246 | |
269 | |
|
|
270 | sub reconfigure { |
|
|
271 | my ($self) = @_; |
|
|
272 | |
|
|
273 | $self->check_size (1); |
|
|
274 | $self->update; |
|
|
275 | } |
|
|
276 | |
247 | sub children { |
277 | sub children { |
248 | } |
|
|
249 | |
|
|
250 | # call when resolution changes etc. |
|
|
251 | sub reconfigure { |
|
|
252 | my ($self) = @_; |
|
|
253 | |
|
|
254 | $_->reconfigure |
|
|
255 | for $self->children; |
|
|
256 | |
|
|
257 | $self->check_size; |
|
|
258 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}]; |
|
|
259 | $self->update; |
|
|
260 | } |
278 | } |
261 | |
279 | |
262 | sub set_max_size { |
280 | sub set_max_size { |
263 | my ($self, $w, $h) = @_; |
281 | my ($self, $w, $h) = @_; |
264 | |
282 | |
… | |
… | |
357 | glVertex $self->{w}, 0; |
375 | glVertex $self->{w}, 0; |
358 | glVertex $self->{w}, $self->{h}; |
376 | glVertex $self->{w}, $self->{h}; |
359 | glVertex 0 , $self->{h}; |
377 | glVertex 0 , $self->{h}; |
360 | glEnd; |
378 | glEnd; |
361 | glPopMatrix; |
379 | glPopMatrix; |
362 | 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; |
363 | } |
381 | } |
364 | } |
382 | } |
365 | |
383 | |
366 | sub _draw { |
384 | sub _draw { |
367 | my ($self) = @_; |
385 | my ($self) = @_; |
… | |
… | |
384 | sub set_parent { |
402 | sub set_parent { |
385 | my ($self, $parent) = @_; |
403 | my ($self, $parent) = @_; |
386 | |
404 | |
387 | Scalar::Util::weaken ($self->{parent} = $parent); |
405 | Scalar::Util::weaken ($self->{parent} = $parent); |
388 | |
406 | |
|
|
407 | # TODO: req_w _does_change after ->reconfigure |
389 | $self->check_size |
408 | $self->check_size |
390 | unless exists $self->{req_w}; |
409 | unless exists $self->{req_w}; |
391 | } |
410 | } |
392 | |
411 | |
393 | sub check_size { |
412 | sub check_size { |
… | |
… | |
417 | } |
436 | } |
418 | |
437 | |
419 | sub DESTROY { |
438 | sub DESTROY { |
420 | my ($self) = @_; |
439 | my ($self) = @_; |
421 | |
440 | |
|
|
441 | delete $WIDGET{$self+0}; |
422 | #$self->deactivate; |
442 | #$self->deactivate; |
423 | } |
443 | } |
424 | |
444 | |
425 | ############################################################################# |
445 | ############################################################################# |
426 | |
446 | |
… | |
… | |
628 | } |
648 | } |
629 | |
649 | |
630 | sub update { |
650 | sub update { |
631 | my ($self) = @_; |
651 | my ($self) = @_; |
632 | |
652 | |
633 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
653 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
634 | $self->SUPER::update; |
654 | $self->SUPER::update; |
635 | } |
655 | } |
636 | |
656 | |
637 | sub size_allocate { |
657 | sub size_allocate { |
638 | my ($self, $w, $h) = @_; |
658 | my ($self, $w, $h) = @_; |
… | |
… | |
776 | $self->add ($self->{slider}); |
796 | $self->add ($self->{slider}); |
777 | |
797 | |
778 | $self |
798 | $self |
779 | } |
799 | } |
780 | |
800 | |
781 | #TODO# update range on size_allocate depeneing on child |
801 | #TODO# update range on size_allocate depending on child |
782 | # update viewport offset on scroll |
802 | # update viewport offset on scroll |
783 | |
803 | |
784 | ############################################################################# |
804 | ############################################################################# |
785 | |
805 | |
786 | package CFClient::UI::Frame; |
806 | package CFClient::UI::Frame; |
787 | |
807 | |
788 | our @ISA = CFClient::UI::Bin::; |
808 | our @ISA = CFClient::UI::Bin::; |
789 | |
809 | |
790 | use CFClient::OpenGL; |
810 | use CFClient::OpenGL; |
|
|
811 | |
|
|
812 | sub new { |
|
|
813 | my $class = shift; |
|
|
814 | |
|
|
815 | $class->SUPER::new ( |
|
|
816 | bg => undef, |
|
|
817 | @_, |
|
|
818 | ) |
|
|
819 | } |
|
|
820 | |
|
|
821 | sub _draw { |
|
|
822 | my ($self) = @_; |
|
|
823 | |
|
|
824 | if ($self->{bg}) { |
|
|
825 | my ($w, $h) = @$self{qw(w h)}; |
|
|
826 | |
|
|
827 | glEnable GL_BLEND; |
|
|
828 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
829 | glColor @{ $self->{bg} }; |
|
|
830 | |
|
|
831 | glBegin GL_QUADS; |
|
|
832 | glVertex 0 , 0; |
|
|
833 | glVertex 0 , $h; |
|
|
834 | glVertex $w, $h; |
|
|
835 | glVertex $w, 0; |
|
|
836 | glEnd; |
|
|
837 | |
|
|
838 | glDisable GL_BLEND; |
|
|
839 | } |
|
|
840 | |
|
|
841 | $self->SUPER::_draw; |
|
|
842 | } |
791 | |
843 | |
792 | ############################################################################# |
844 | ############################################################################# |
793 | |
845 | |
794 | package CFClient::UI::FancyFrame; |
846 | package CFClient::UI::FancyFrame; |
795 | |
847 | |
… | |
… | |
808 | |
860 | |
809 | my $self = $class->SUPER::new ( |
861 | my $self = $class->SUPER::new ( |
810 | bg => [1, 1, 1, 1], |
862 | bg => [1, 1, 1, 1], |
811 | border_bg => [1, 1, 1, 1], |
863 | border_bg => [1, 1, 1, 1], |
812 | border => 0.6, |
864 | border => 0.6, |
|
|
865 | toplevel => 1, |
813 | can_events => 1, |
866 | can_events => 1, |
814 | @_ |
867 | @_ |
815 | ); |
868 | ); |
816 | |
869 | |
817 | $self->{title} &&= new CFClient::UI::Label |
870 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
1448 | my $sym = $ev->{sym}; |
1501 | my $sym = $ev->{sym}; |
1449 | my $uni = $ev->{unicode}; |
1502 | my $uni = $ev->{unicode}; |
1450 | |
1503 | |
1451 | my $text = $self->get_text; |
1504 | my $text = $self->get_text; |
1452 | |
1505 | |
1453 | if ($sym == 8) { |
1506 | if ($uni == 8) { |
1454 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1507 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1455 | } elsif ($sym == 127) { |
1508 | } elsif ($uni == 127) { |
1456 | substr $text, $self->{cursor}, 1, ""; |
1509 | substr $text, $self->{cursor}, 1, ""; |
1457 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1510 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1458 | --$self->{cursor} if $self->{cursor}; |
1511 | --$self->{cursor} if $self->{cursor}; |
1459 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1512 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1460 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1513 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1461 | } elsif ($sym == CFClient::SDLK_HOME) { |
1514 | } elsif ($sym == CFClient::SDLK_HOME) { |
1462 | $self->{cursor} = 0; |
1515 | $self->{cursor} = 0; |
1463 | } elsif ($sym == CFClient::SDLK_END) { |
1516 | } elsif ($sym == CFClient::SDLK_END) { |
1464 | $self->{cursor} = length $text; |
1517 | $self->{cursor} = length $text; |
1465 | } elsif ($sym == 27) { |
1518 | } elsif ($uni == 27) { |
1466 | $self->emit ('escape'); |
1519 | $self->emit ('escape'); |
1467 | } elsif ($uni) { |
1520 | } elsif ($uni) { |
1468 | substr $text, $self->{cursor}++, 0, chr $uni; |
1521 | substr $text, $self->{cursor}++, 0, chr $uni; |
1469 | } |
1522 | } |
1470 | |
1523 | |
… | |
… | |
1957 | qw(s1_slider.png s1_slider_bg.png); |
2010 | qw(s1_slider.png s1_slider_bg.png); |
1958 | |
2011 | |
1959 | sub new { |
2012 | sub new { |
1960 | my $class = shift; |
2013 | my $class = shift; |
1961 | |
2014 | |
1962 | # range [value, low, high, page] |
2015 | # range [value, low, high, page, unit] |
1963 | |
2016 | |
1964 | # TODO: 0-width page |
2017 | # TODO: 0-width page |
1965 | # TODO: req_w/h are wrong with vertical |
2018 | # TODO: req_w/h are wrong with vertical |
1966 | # TODO: calculations are off |
2019 | # TODO: calculations are off |
1967 | my $self = $class->SUPER::new ( |
2020 | my $self = $class->SUPER::new ( |
… | |
… | |
1970 | range => [0, 0, 100, 10], |
2023 | range => [0, 0, 100, 10], |
1971 | req_w => $::WIDTH / 80, |
2024 | req_w => $::WIDTH / 80, |
1972 | req_h => $::WIDTH / 80, |
2025 | req_h => $::WIDTH / 80, |
1973 | vertical => 0, |
2026 | vertical => 0, |
1974 | can_hover => 1, |
2027 | can_hover => 1, |
1975 | inner_pad => 5, |
2028 | inner_pad => .05, |
1976 | @_ |
2029 | @_ |
1977 | ); |
2030 | ); |
1978 | |
2031 | |
|
|
2032 | $self->set_value ($self->{range}[0]); |
|
|
2033 | $self->update; |
|
|
2034 | |
1979 | $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 | } |
1980 | } |
2059 | } |
1981 | |
2060 | |
1982 | sub size_request { |
2061 | sub size_request { |
1983 | my ($self) = @_; |
2062 | my ($self) = @_; |
1984 | |
2063 | |
… | |
… | |
1997 | |
2076 | |
1998 | sub mouse_motion { |
2077 | sub mouse_motion { |
1999 | my ($self, $ev, $x, $y) = @_; |
2078 | my ($self, $ev, $x, $y) = @_; |
2000 | |
2079 | |
2001 | 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 | |
2002 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2097 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2003 | |
2098 | |
2004 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2099 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2005 | |
2100 | |
2006 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
2101 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2007 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2008 | |
2102 | |
2009 | $x -= $inner_pad_px; # substract the padding |
2103 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2010 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
2104 | $value = $self->{scale} * ($value - $lo); |
2011 | $x = $lo if $x < $lo; |
|
|
2012 | $x = $hi - $page if $x > $hi - $page; |
|
|
2013 | $self->{range}[0] = $x; |
|
|
2014 | |
2105 | |
2015 | $self->emit (changed => $x); |
2106 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
2016 | $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; |
2017 | } |
2113 | }); |
2018 | } |
|
|
2019 | |
2114 | |
2020 | # the inner_* stuff is for generating a padding for the slider handle, |
2115 | $self->SUPER::update; |
2021 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
2022 | # correct propably, but it does the job for now |
|
|
2023 | sub _calc_inner_pad_px { |
|
|
2024 | my ($self, $w) = @_; |
|
|
2025 | ($w / 100) * $self->{inner_pad} # % to pixels |
|
|
2026 | } |
2116 | } |
2027 | |
2117 | |
2028 | sub _draw { |
2118 | sub _draw { |
2029 | my ($self) = @_; |
2119 | my ($self) = @_; |
2030 | |
2120 | |
2031 | $self->SUPER::_draw (); |
2121 | $self->SUPER::_draw (); |
2032 | |
2122 | |
2033 | my ($w, $h) = @$self{qw(w h)}; |
2123 | glScale $self->{w}, $self->{h}; |
2034 | |
2124 | |
2035 | if ($self->{vertical}) { |
2125 | if ($self->{vertical}) { |
2036 | # draw a vertical slider like a rotated horizontal slider |
2126 | # draw a vertical slider like a rotated horizontal slider |
2037 | |
2127 | |
2038 | glRotate 90, 0, 0, 1; |
2128 | glRotate 90, 0, 0, 1; |
2039 | glTranslate 0, -$self->{w}, 0; |
2129 | glTranslate 0, 1, 0; |
2040 | |
|
|
2041 | ($w, $h) = ($h, $w); |
|
|
2042 | } |
2130 | } |
2043 | |
2131 | |
2044 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2132 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2045 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2133 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2046 | |
2134 | |
2047 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2048 | |
|
|
2049 | $hi = $value + 1 if $lo == $hi; |
|
|
2050 | |
|
|
2051 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
2052 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
2053 | |
|
|
2054 | $page = int $page * $inner_w / ($hi - $lo); |
|
|
2055 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
|
|
2056 | |
|
|
2057 | $w -= $page; |
|
|
2058 | $page &= ~1; |
|
|
2059 | glTranslate $page * 0.5, 0, 0; |
|
|
2060 | $page ||= 2; |
|
|
2061 | |
|
|
2062 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
|
|
2063 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
|
|
2064 | |
|
|
2065 | glEnable GL_TEXTURE_2D; |
2135 | glEnable GL_TEXTURE_2D; |
2066 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2136 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2067 | |
2137 | |
2068 | # draw background |
2138 | # draw background |
2069 | $tex[1]->draw_quad_alpha (0, 0, $w, $h); |
2139 | $tex[1]->draw_quad_alpha (0, 0, 1, 1); |
2070 | |
2140 | |
2071 | # draw handle |
2141 | # draw handle |
2072 | $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); |
2142 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2073 | |
2143 | |
2074 | glDisable GL_TEXTURE_2D; |
2144 | glDisable GL_TEXTURE_2D; |
2075 | } |
2145 | } |
2076 | |
2146 | |
2077 | ############################################################################# |
2147 | ############################################################################# |
… | |
… | |
2165 | |
2235 | |
2166 | return unless $self->{h} > 0; |
2236 | return unless $self->{h} > 0; |
2167 | |
2237 | |
2168 | delete $self->{texture}; |
2238 | delete $self->{texture}; |
2169 | |
2239 | |
2170 | $ROOT->on_refresh ($self, sub { |
2240 | $ROOT->on_post_alloc ($self, sub { |
2171 | if (delete $self->{need_reflow}) { |
2241 | if (delete $self->{need_reflow}) { |
2172 | my $height = 0; |
2242 | my $height = 0; |
2173 | |
2243 | |
2174 | $height += $_->[0] = $self->text_height ($_->[2]) |
2244 | $height += $_->[0] = $self->text_height ($_->[2]) |
2175 | for @{$self->{par}}; |
2245 | for @{$self->{par}}; |
… | |
… | |
2434 | |
2504 | |
2435 | my $desc = $item->{nrof} < 2 |
2505 | my $desc = $item->{nrof} < 2 |
2436 | ? $item->{name} |
2506 | ? $item->{name} |
2437 | : "$item->{nrof} $item->{name_pl}"; |
2507 | : "$item->{nrof} $item->{name_pl}"; |
2438 | |
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 .= " *"; |
2439 | |
2523 | |
2440 | my $self = $class->SUPER::new ( |
2524 | my $self = $class->SUPER::new ( |
2441 | can_hover => 1, |
2525 | can_hover => 1, |
2442 | can_events => 1, |
2526 | can_events => 1, |
2443 | tooltip => (CFClient::UI::Label->escape ($desc) |
2527 | tooltip => (CFClient::UI::Label->escape ($desc) |
… | |
… | |
2449 | if ($ev->{button} == 1) { |
2533 | if ($ev->{button} == 1) { |
2450 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2534 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
2451 | } elsif ($ev->{button} == 2) { |
2535 | } elsif ($ev->{button} == 2) { |
2452 | $::CONN->send ("apply $item->{tag}"); |
2536 | $::CONN->send ("apply $item->{tag}"); |
2453 | } elsif ($ev->{button} == 3) { |
2537 | } elsif ($ev->{button} == 3) { |
2454 | CFClient::UI::Menu->new ( |
2538 | my @menu_items = ( |
2455 | items => [ |
|
|
2456 | ["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") }], |
2457 | [ |
2543 | [ |
2458 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2544 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
2459 | sub { $::CONN->send ("lock $item->{tag}") }, |
2545 | sub { $::CONN->send ("lock ". () ."$item->{tag}") }, |
2460 | ], |
|
|
2461 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2462 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2463 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2464 | ], |
2546 | ], |
2465 | )->popup ($ev); |
2547 | ); |
|
|
2548 | |
|
|
2549 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
2466 | } |
2550 | } |
2467 | |
2551 | |
2468 | 1 |
2552 | 1 |
2469 | }, |
2553 | }, |
2470 | %args |
2554 | %args |
… | |
… | |
2506 | my ($self, $items) = @_; |
2590 | my ($self, $items) = @_; |
2507 | |
2591 | |
2508 | $self->{scrolled}->clear; |
2592 | $self->{scrolled}->clear; |
2509 | return unless $items; |
2593 | return unless $items; |
2510 | |
2594 | |
2511 | 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; |
2512 | |
2599 | |
2513 | $self->{real_items} = \@items; |
2600 | $self->{real_items} = \@items; |
2514 | |
2601 | |
2515 | for my $item (@items) { |
2602 | for my $item (@items) { |
2516 | my $desc = $item->{nrof} < 2 |
2603 | my $desc = $item->{nrof} < 2 |
2517 | ? $item->{name} |
2604 | ? $item->{name} |
2518 | : "$item->{nrof} $item->{name_pl}"; |
2605 | : "$item->{nrof} $item->{name_pl}"; |
2519 | |
2606 | |
2520 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2607 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2521 | } |
2608 | } |
|
|
2609 | |
|
|
2610 | $self->{scrolled}->add (@items); |
2522 | |
2611 | |
2523 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2612 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2524 | } |
2613 | } |
2525 | |
2614 | |
2526 | sub size_request { |
2615 | sub size_request { |
… | |
… | |
2631 | ? "<b>$item->{count} ×</b> $item->{text}" |
2720 | ? "<b>$item->{count} ×</b> $item->{text}" |
2632 | : $item->{text}; |
2721 | : $item->{text}; |
2633 | |
2722 | |
2634 | for ($short) { |
2723 | for ($short) { |
2635 | s/^\s+//; |
2724 | s/^\s+//; |
2636 | s/\012.*/…/s; |
2725 | s/\s+/ /g; |
2637 | my $len = int 30 / $item->{fontsize}; |
2726 | my $len = int 40 / $item->{fontsize}; |
2638 | substr $_, $len, length, "…" if $len < length; |
2727 | substr $_, $len, length, "…" if $len < length; |
2639 | } |
2728 | } |
2640 | |
2729 | |
2641 | new CFClient::UI::Label |
2730 | new CFClient::UI::Label |
2642 | markup => $short, |
2731 | markup => $short, |
2643 | tooltip => $item->{tooltip}, |
2732 | tooltip => $item->{tooltip}, |
2644 | tooltip_font => $::FONT_PROP, |
2733 | tooltip_font => $::FONT_PROP, |
2645 | tooltip_width => 0.67, |
2734 | tooltip_width => 0.67, |
2646 | fontsize => $item->{fontsize}, |
2735 | fontsize => $item->{fontsize}, |
2647 | color => $item->{color}, |
2736 | fg => $item->{fg}, |
2648 | can_events => 1, |
2737 | can_events => 1, |
2649 | can_hover => 1 |
2738 | can_hover => 1 |
2650 | }; |
2739 | }; |
2651 | } |
2740 | } |
2652 | |
2741 | |
… | |
… | |
2654 | $self->SUPER::add (reverse @widgets); |
2743 | $self->SUPER::add (reverse @widgets); |
2655 | } |
2744 | } |
2656 | |
2745 | |
2657 | sub add { |
2746 | sub add { |
2658 | my ($self, $text, %arg) = @_; |
2747 | my ($self, $text, %arg) = @_; |
|
|
2748 | |
|
|
2749 | $text =~ s/^\s+//; |
|
|
2750 | $text =~ s/\s+$//; |
2659 | |
2751 | |
2660 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2752 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2661 | |
2753 | |
2662 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2754 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2663 | |
2755 | |
… | |
… | |
2666 | $item->{count}++; |
2758 | $item->{count}++; |
2667 | } else { |
2759 | } else { |
2668 | $item->{count} = 1; |
2760 | $item->{count} = 1; |
2669 | $item->{text} = $item->{tooltip} = $text; |
2761 | $item->{text} = $item->{tooltip} = $text; |
2670 | } |
2762 | } |
|
|
2763 | $item->{id} = ++$self->{id}; |
2671 | $item->{timeout} = $timeout; |
2764 | $item->{timeout} = $timeout; |
2672 | delete $item->{label}; |
2765 | delete $item->{label}; |
2673 | } else { |
2766 | } else { |
2674 | $self->{item}{$group} = { |
2767 | $self->{item}{$group} = { |
2675 | id => ++$self->{id}, |
2768 | id => ++$self->{id}, |
2676 | text => $text, |
2769 | text => $text, |
2677 | timeout => $timeout, |
2770 | timeout => $timeout, |
2678 | tooltip => $text, |
2771 | tooltip => $text, |
2679 | fontsize => 0.8, |
2772 | fontsize => 0.8, |
2680 | color => [0.8, 0.8, 0.8, 0.8], |
2773 | fg => [0.8, 0.8, 0.8, 0.8], |
2681 | pri => 0, |
2774 | pri => 0, |
2682 | count => 1, |
2775 | count => 1, |
2683 | %arg, |
2776 | %arg, |
2684 | }; |
2777 | }; |
2685 | } |
2778 | } |
… | |
… | |
2724 | } |
2817 | } |
2725 | |
2818 | |
2726 | sub size_allocate { |
2819 | sub size_allocate { |
2727 | my ($self, $w, $h) = @_; |
2820 | my ($self, $w, $h) = @_; |
2728 | |
2821 | |
2729 | my $old_w = $self->{old_w}; |
2822 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2730 | my $old_h = $self->{old_h}; |
2823 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2731 | |
2824 | |
2732 | if ($old_w && $old_h) { |
2825 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2733 | for my $child ($self->children) { |
2826 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
2734 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2735 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2736 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2737 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2738 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2739 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2740 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2741 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2742 | } |
|
|
2743 | } |
|
|
2744 | |
2827 | |
2745 | for my $child ($self->children) { |
2828 | for my $child ($self->children) { |
2746 | 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)}; |
2747 | |
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 | |
2748 | $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; |
2749 | $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 | |
2750 | $child->configure ($X, $Y, $W, $H); |
2840 | $child->configure ($X, $Y, $W, $H); |
2751 | } |
2841 | } |
2752 | |
|
|
2753 | $self->{old_w} = $w; |
|
|
2754 | $self->{old_h} = $h; |
|
|
2755 | } |
2842 | } |
2756 | |
2843 | |
2757 | sub coord2local { |
2844 | sub coord2local { |
2758 | my ($self, $x, $y) = @_; |
2845 | my ($self, $x, $y) = @_; |
2759 | |
2846 | |
… | |
… | |
2772 | $self->check_size; |
2859 | $self->check_size; |
2773 | $::WANT_REFRESH++; |
2860 | $::WANT_REFRESH++; |
2774 | } |
2861 | } |
2775 | |
2862 | |
2776 | sub add { |
2863 | sub add { |
2777 | my ($self, $child) = @_; |
2864 | my ($self, @children) = @_; |
2778 | |
2865 | |
|
|
2866 | for my $child (@children) { |
|
|
2867 | $child->{toplevel} = 1; |
|
|
2868 | |
2779 | # integerise window positions |
2869 | # integerise window positions |
2780 | $child->{x} = int $child->{x}; |
2870 | $child->{x} = int $child->{x}; |
2781 | $child->{y} = int $child->{y}; |
2871 | $child->{y} = int $child->{y}; |
|
|
2872 | } |
2782 | |
2873 | |
2783 | $self->SUPER::add ($child); |
2874 | $self->SUPER::add (@children); |
2784 | } |
2875 | } |
2785 | |
2876 | |
2786 | sub on_refresh { |
2877 | sub on_refresh { |
2787 | my ($self, $id, $cb) = @_; |
2878 | my ($self, $id, $cb) = @_; |
2788 | |
2879 | |
2789 | $self->{refresh_hook}{$id} = $cb; |
2880 | $self->{refresh_hook}{$id} = $cb; |
2790 | } |
2881 | } |
2791 | |
2882 | |
|
|
2883 | sub on_post_alloc { |
|
|
2884 | my ($self, $id, $cb) = @_; |
|
|
2885 | |
|
|
2886 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2887 | } |
|
|
2888 | |
2792 | sub draw { |
2889 | sub draw { |
2793 | my ($self) = @_; |
2890 | my ($self) = @_; |
|
|
2891 | |
|
|
2892 | while ($self->{refresh_hook}) { |
|
|
2893 | $_->() |
|
|
2894 | for values %{delete $self->{refresh_hook}}; |
|
|
2895 | } |
2794 | |
2896 | |
2795 | if ($self->{check_size}) { |
2897 | if ($self->{check_size}) { |
2796 | my @queue = ([], []); |
2898 | my @queue = ([], []); |
2797 | |
2899 | |
2798 | for (;;) { |
2900 | for (;;) { |
… | |
… | |
2835 | $widget->size_allocate ($w, $h); |
2937 | $widget->size_allocate ($w, $h); |
2836 | $widget->emit (size_allocate => $w, $h); |
2938 | $widget->emit (size_allocate => $w, $h); |
2837 | } |
2939 | } |
2838 | } |
2940 | } |
2839 | |
2941 | |
2840 | while ($self->{refresh_hook}) { |
2942 | while ($self->{post_alloc_hook}) { |
2841 | $_->() |
2943 | $_->() |
2842 | for values %{delete $self->{refresh_hook}}; |
2944 | for values %{delete $self->{post_alloc_hook}}; |
2843 | } |
2945 | } |
2844 | |
2946 | |
2845 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2947 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2846 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2948 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2847 | glClear GL_COLOR_BUFFER_BIT; |
2949 | glClear GL_COLOR_BUFFER_BIT; |