… | |
… | |
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}) { |
… | |
… | |
132 | |
134 | |
133 | for (@$vals) { |
135 | for (@$vals) { |
134 | my $i = int $_ + $rem; |
136 | my $i = int $_ + $rem; |
135 | $rem += $_ - $i; |
137 | $rem += $_ - $i; |
136 | $_ = $i; |
138 | $_ = $i; |
|
|
139 | } |
|
|
140 | } |
|
|
141 | |
|
|
142 | # call when resolution changes etc. |
|
|
143 | sub rescale_widgets { |
|
|
144 | my ($sx, $sy) = @_; |
|
|
145 | |
|
|
146 | for my $widget (values %WIDGET) { |
|
|
147 | if ($widget->{toplevel}) { |
|
|
148 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
|
|
149 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
|
|
150 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
|
|
151 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
|
|
152 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
|
|
153 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
|
|
154 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
|
|
155 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
|
|
156 | } |
|
|
157 | |
|
|
158 | $widget->reconfigure; |
137 | } |
159 | } |
138 | } |
160 | } |
139 | |
161 | |
140 | ############################################################################# |
162 | ############################################################################# |
141 | |
163 | |
… | |
… | |
160 | if (/^connect_(.*)$/) { |
182 | if (/^connect_(.*)$/) { |
161 | $self->connect ($1 => delete $self->{$_}); |
183 | $self->connect ($1 => delete $self->{$_}); |
162 | } |
184 | } |
163 | } |
185 | } |
164 | |
186 | |
|
|
187 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
188 | |
165 | $self |
189 | $self |
166 | } |
190 | } |
167 | |
191 | |
168 | sub destroy { |
192 | sub destroy { |
169 | my ($self) = @_; |
193 | my ($self) = @_; |
… | |
… | |
175 | sub show { |
199 | sub show { |
176 | my ($self) = @_; |
200 | my ($self) = @_; |
177 | |
201 | |
178 | return if $self->{parent}; |
202 | return if $self->{parent}; |
179 | |
203 | |
|
|
204 | $self->{toplevel} = 1; |
180 | $CFClient::UI::ROOT->add ($self); |
205 | $CFClient::UI::ROOT->add ($self); |
181 | } |
206 | } |
182 | |
207 | |
183 | sub hide { |
208 | sub hide { |
184 | my ($self) = @_; |
209 | my ($self) = @_; |
… | |
… | |
242 | |
267 | |
243 | sub size_allocate { |
268 | sub size_allocate { |
244 | # nothing to be done |
269 | # nothing to be done |
245 | } |
270 | } |
246 | |
271 | |
247 | sub children { |
|
|
248 | } |
|
|
249 | |
|
|
250 | # call when resolution changes etc. |
|
|
251 | sub reconfigure { |
272 | sub reconfigure { |
252 | my ($self) = @_; |
273 | my ($self) = @_; |
253 | |
|
|
254 | $_->reconfigure |
|
|
255 | for $self->children; |
|
|
256 | |
274 | |
257 | $self->check_size (1); |
275 | $self->check_size (1); |
258 | $self->update; |
276 | $self->update; |
|
|
277 | } |
|
|
278 | |
|
|
279 | sub children { |
259 | } |
280 | } |
260 | |
281 | |
261 | sub set_max_size { |
282 | sub set_max_size { |
262 | my ($self, $w, $h) = @_; |
283 | my ($self, $w, $h) = @_; |
263 | |
284 | |
… | |
… | |
383 | sub set_parent { |
404 | sub set_parent { |
384 | my ($self, $parent) = @_; |
405 | my ($self, $parent) = @_; |
385 | |
406 | |
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
407 | Scalar::Util::weaken ($self->{parent} = $parent); |
387 | |
408 | |
388 | |
|
|
389 | # TODO: req_w _does_change after ->reconfigure |
409 | # TODO: req_w _does_change after ->reconfigure |
390 | $self->check_size |
410 | $self->check_size |
391 | unless exists $self->{req_w}; |
411 | unless exists $self->{req_w}; |
392 | } |
412 | } |
393 | |
413 | |
… | |
… | |
418 | } |
438 | } |
419 | |
439 | |
420 | sub DESTROY { |
440 | sub DESTROY { |
421 | my ($self) = @_; |
441 | my ($self) = @_; |
422 | |
442 | |
|
|
443 | delete $WIDGET{$self+0}; |
423 | #$self->deactivate; |
444 | #$self->deactivate; |
424 | } |
445 | } |
425 | |
446 | |
426 | ############################################################################# |
447 | ############################################################################# |
427 | |
448 | |
… | |
… | |
777 | $self->add ($self->{slider}); |
798 | $self->add ($self->{slider}); |
778 | |
799 | |
779 | $self |
800 | $self |
780 | } |
801 | } |
781 | |
802 | |
782 | #TODO# update range on size_allocate depeneing on child |
803 | #TODO# update range on size_allocate depending on child |
783 | # update viewport offset on scroll |
804 | # update viewport offset on scroll |
784 | |
805 | |
785 | ############################################################################# |
806 | ############################################################################# |
786 | |
807 | |
787 | package CFClient::UI::Frame; |
808 | package CFClient::UI::Frame; |
788 | |
809 | |
789 | our @ISA = CFClient::UI::Bin::; |
810 | our @ISA = CFClient::UI::Bin::; |
790 | |
811 | |
791 | use CFClient::OpenGL; |
812 | use CFClient::OpenGL; |
|
|
813 | |
|
|
814 | sub new { |
|
|
815 | my $class = shift; |
|
|
816 | |
|
|
817 | $class->SUPER::new ( |
|
|
818 | bg => undef, |
|
|
819 | @_, |
|
|
820 | ) |
|
|
821 | } |
|
|
822 | |
|
|
823 | sub _draw { |
|
|
824 | my ($self) = @_; |
|
|
825 | |
|
|
826 | if ($self->{bg}) { |
|
|
827 | my ($w, $h) = @$self{qw(w h)}; |
|
|
828 | |
|
|
829 | glEnable GL_BLEND; |
|
|
830 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
831 | glColor @{ $self->{bg} }; |
|
|
832 | |
|
|
833 | glBegin GL_QUADS; |
|
|
834 | glVertex 0 , 0; |
|
|
835 | glVertex 0 , $h; |
|
|
836 | glVertex $w, $h; |
|
|
837 | glVertex $w, 0; |
|
|
838 | glEnd; |
|
|
839 | |
|
|
840 | glDisable GL_BLEND; |
|
|
841 | } |
|
|
842 | |
|
|
843 | $self->SUPER::_draw; |
|
|
844 | } |
792 | |
845 | |
793 | ############################################################################# |
846 | ############################################################################# |
794 | |
847 | |
795 | package CFClient::UI::FancyFrame; |
848 | package CFClient::UI::FancyFrame; |
796 | |
849 | |
… | |
… | |
809 | |
862 | |
810 | my $self = $class->SUPER::new ( |
863 | my $self = $class->SUPER::new ( |
811 | bg => [1, 1, 1, 1], |
864 | bg => [1, 1, 1, 1], |
812 | border_bg => [1, 1, 1, 1], |
865 | border_bg => [1, 1, 1, 1], |
813 | border => 0.6, |
866 | border => 0.6, |
|
|
867 | toplevel => 1, |
814 | can_events => 1, |
868 | can_events => 1, |
815 | @_ |
869 | @_ |
816 | ); |
870 | ); |
817 | |
871 | |
818 | $self->{title} &&= new CFClient::UI::Label |
872 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
1449 | my $sym = $ev->{sym}; |
1503 | my $sym = $ev->{sym}; |
1450 | my $uni = $ev->{unicode}; |
1504 | my $uni = $ev->{unicode}; |
1451 | |
1505 | |
1452 | my $text = $self->get_text; |
1506 | my $text = $self->get_text; |
1453 | |
1507 | |
1454 | if ($sym == 8) { |
1508 | if ($uni == 8) { |
1455 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1509 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1456 | } elsif ($sym == 127) { |
1510 | } elsif ($uni == 127) { |
1457 | substr $text, $self->{cursor}, 1, ""; |
1511 | substr $text, $self->{cursor}, 1, ""; |
1458 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1512 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1459 | --$self->{cursor} if $self->{cursor}; |
1513 | --$self->{cursor} if $self->{cursor}; |
1460 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1514 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1461 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1515 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1462 | } elsif ($sym == CFClient::SDLK_HOME) { |
1516 | } elsif ($sym == CFClient::SDLK_HOME) { |
1463 | $self->{cursor} = 0; |
1517 | $self->{cursor} = 0; |
1464 | } elsif ($sym == CFClient::SDLK_END) { |
1518 | } elsif ($sym == CFClient::SDLK_END) { |
1465 | $self->{cursor} = length $text; |
1519 | $self->{cursor} = length $text; |
1466 | } elsif ($sym == 27) { |
1520 | } elsif ($uni == 27) { |
1467 | $self->emit ('escape'); |
1521 | $self->emit ('escape'); |
1468 | } elsif ($uni) { |
1522 | } elsif ($uni) { |
1469 | substr $text, $self->{cursor}++, 0, chr $uni; |
1523 | substr $text, $self->{cursor}++, 0, chr $uni; |
1470 | } |
1524 | } |
1471 | |
1525 | |
… | |
… | |
2507 | my ($self, $items) = @_; |
2561 | my ($self, $items) = @_; |
2508 | |
2562 | |
2509 | $self->{scrolled}->clear; |
2563 | $self->{scrolled}->clear; |
2510 | return unless $items; |
2564 | return unless $items; |
2511 | |
2565 | |
2512 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
2566 | my @items = sort { |
|
|
2567 | ($a->{type} <=> $b->{type}) |
|
|
2568 | or ($a->{name} cmp $b->{name}) |
|
|
2569 | } @$items; |
2513 | |
2570 | |
2514 | $self->{real_items} = \@items; |
2571 | $self->{real_items} = \@items; |
2515 | |
2572 | |
2516 | for my $item (@items) { |
2573 | for my $item (@items) { |
2517 | my $desc = $item->{nrof} < 2 |
2574 | my $desc = $item->{nrof} < 2 |
2518 | ? $item->{name} |
2575 | ? $item->{name} |
2519 | : "$item->{nrof} $item->{name_pl}"; |
2576 | : "$item->{nrof} $item->{name_pl}"; |
2520 | |
2577 | |
2521 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2578 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2522 | } |
2579 | } |
|
|
2580 | |
|
|
2581 | $self->{scrolled}->add (@items); |
2523 | |
2582 | |
2524 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2583 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2525 | } |
2584 | } |
2526 | |
2585 | |
2527 | sub size_request { |
2586 | sub size_request { |
… | |
… | |
2729 | } |
2788 | } |
2730 | |
2789 | |
2731 | sub size_allocate { |
2790 | sub size_allocate { |
2732 | my ($self, $w, $h) = @_; |
2791 | my ($self, $w, $h) = @_; |
2733 | |
2792 | |
2734 | my $old_w = $self->{old_w}; |
2793 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2735 | my $old_h = $self->{old_h}; |
2794 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2736 | |
2795 | |
2737 | if ($old_w && $old_h) { |
2796 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2738 | for my $child ($self->children) { |
2797 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
2739 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2740 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2741 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2742 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2743 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2744 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2745 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2746 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2747 | } |
|
|
2748 | } |
|
|
2749 | |
2798 | |
2750 | for my $child ($self->children) { |
2799 | for my $child ($self->children) { |
2751 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2800 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2752 | |
2801 | |
2753 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2802 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2754 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2803 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2755 | $child->configure ($X, $Y, $W, $H); |
2804 | $child->configure ($X, $Y, $W, $H); |
2756 | } |
2805 | } |
2757 | |
|
|
2758 | $self->{old_w} = $w; |
|
|
2759 | $self->{old_h} = $h; |
|
|
2760 | } |
2806 | } |
2761 | |
2807 | |
2762 | sub coord2local { |
2808 | sub coord2local { |
2763 | my ($self, $x, $y) = @_; |
2809 | my ($self, $x, $y) = @_; |
2764 | |
2810 | |