… | |
… | |
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 | |
247 | sub children { |
|
|
248 | } |
|
|
249 | |
|
|
250 | # call when resolution changes etc. |
|
|
251 | sub reconfigure { |
270 | sub reconfigure { |
252 | my ($self) = @_; |
271 | my ($self) = @_; |
253 | |
|
|
254 | $_->reconfigure |
|
|
255 | for $self->children; |
|
|
256 | |
272 | |
257 | $self->check_size (1); |
273 | $self->check_size (1); |
258 | $self->update; |
274 | $self->update; |
|
|
275 | } |
|
|
276 | |
|
|
277 | sub children { |
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 | |
… | |
… | |
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 | |
388 | |
|
|
389 | # TODO: req_w _does_change after ->reconfigure |
407 | # TODO: req_w _does_change after ->reconfigure |
390 | $self->check_size |
408 | $self->check_size |
391 | unless exists $self->{req_w}; |
409 | unless exists $self->{req_w}; |
392 | } |
410 | } |
393 | |
411 | |
… | |
… | |
418 | } |
436 | } |
419 | |
437 | |
420 | sub DESTROY { |
438 | sub DESTROY { |
421 | my ($self) = @_; |
439 | my ($self) = @_; |
422 | |
440 | |
|
|
441 | delete $WIDGET{$self+0}; |
423 | #$self->deactivate; |
442 | #$self->deactivate; |
424 | } |
443 | } |
425 | |
444 | |
426 | ############################################################################# |
445 | ############################################################################# |
427 | |
446 | |
… | |
… | |
777 | $self->add ($self->{slider}); |
796 | $self->add ($self->{slider}); |
778 | |
797 | |
779 | $self |
798 | $self |
780 | } |
799 | } |
781 | |
800 | |
782 | #TODO# update range on size_allocate depeneing on child |
801 | #TODO# update range on size_allocate depending on child |
783 | # update viewport offset on scroll |
802 | # update viewport offset on scroll |
784 | |
803 | |
785 | ############################################################################# |
804 | ############################################################################# |
786 | |
805 | |
787 | package CFClient::UI::Frame; |
806 | package CFClient::UI::Frame; |
788 | |
807 | |
789 | our @ISA = CFClient::UI::Bin::; |
808 | our @ISA = CFClient::UI::Bin::; |
790 | |
809 | |
791 | 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 | } |
792 | |
843 | |
793 | ############################################################################# |
844 | ############################################################################# |
794 | |
845 | |
795 | package CFClient::UI::FancyFrame; |
846 | package CFClient::UI::FancyFrame; |
796 | |
847 | |
… | |
… | |
809 | |
860 | |
810 | my $self = $class->SUPER::new ( |
861 | my $self = $class->SUPER::new ( |
811 | bg => [1, 1, 1, 1], |
862 | bg => [1, 1, 1, 1], |
812 | border_bg => [1, 1, 1, 1], |
863 | border_bg => [1, 1, 1, 1], |
813 | border => 0.6, |
864 | border => 0.6, |
|
|
865 | toplevel => 1, |
814 | can_events => 1, |
866 | can_events => 1, |
815 | @_ |
867 | @_ |
816 | ); |
868 | ); |
817 | |
869 | |
818 | $self->{title} &&= new CFClient::UI::Label |
870 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
1449 | my $sym = $ev->{sym}; |
1501 | my $sym = $ev->{sym}; |
1450 | my $uni = $ev->{unicode}; |
1502 | my $uni = $ev->{unicode}; |
1451 | |
1503 | |
1452 | my $text = $self->get_text; |
1504 | my $text = $self->get_text; |
1453 | |
1505 | |
1454 | if ($sym == 8) { |
1506 | if ($uni == 8) { |
1455 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1507 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1456 | } elsif ($sym == 127) { |
1508 | } elsif ($uni == 127) { |
1457 | substr $text, $self->{cursor}, 1, ""; |
1509 | substr $text, $self->{cursor}, 1, ""; |
1458 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1510 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1459 | --$self->{cursor} if $self->{cursor}; |
1511 | --$self->{cursor} if $self->{cursor}; |
1460 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1512 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1461 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1513 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1462 | } elsif ($sym == CFClient::SDLK_HOME) { |
1514 | } elsif ($sym == CFClient::SDLK_HOME) { |
1463 | $self->{cursor} = 0; |
1515 | $self->{cursor} = 0; |
1464 | } elsif ($sym == CFClient::SDLK_END) { |
1516 | } elsif ($sym == CFClient::SDLK_END) { |
1465 | $self->{cursor} = length $text; |
1517 | $self->{cursor} = length $text; |
1466 | } elsif ($sym == 27) { |
1518 | } elsif ($uni == 27) { |
1467 | $self->emit ('escape'); |
1519 | $self->emit ('escape'); |
1468 | } elsif ($uni) { |
1520 | } elsif ($uni) { |
1469 | substr $text, $self->{cursor}++, 0, chr $uni; |
1521 | substr $text, $self->{cursor}++, 0, chr $uni; |
1470 | } |
1522 | } |
1471 | |
1523 | |
… | |
… | |
2507 | my ($self, $items) = @_; |
2559 | my ($self, $items) = @_; |
2508 | |
2560 | |
2509 | $self->{scrolled}->clear; |
2561 | $self->{scrolled}->clear; |
2510 | return unless $items; |
2562 | return unless $items; |
2511 | |
2563 | |
2512 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
2564 | my @items = sort { |
|
|
2565 | ($a->{type} <=> $b->{type}) |
|
|
2566 | or ($a->{name} cmp $b->{name}) |
|
|
2567 | } @$items; |
2513 | |
2568 | |
2514 | $self->{real_items} = \@items; |
2569 | $self->{real_items} = \@items; |
2515 | |
2570 | |
2516 | for my $item (@items) { |
2571 | for my $item (@items) { |
2517 | my $desc = $item->{nrof} < 2 |
2572 | my $desc = $item->{nrof} < 2 |
2518 | ? $item->{name} |
2573 | ? $item->{name} |
2519 | : "$item->{nrof} $item->{name_pl}"; |
2574 | : "$item->{nrof} $item->{name_pl}"; |
2520 | |
2575 | |
2521 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2576 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2522 | } |
2577 | } |
|
|
2578 | |
|
|
2579 | $self->{scrolled}->add (@items); |
2523 | |
2580 | |
2524 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2581 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2525 | } |
2582 | } |
2526 | |
2583 | |
2527 | sub size_request { |
2584 | sub size_request { |
… | |
… | |
2729 | } |
2786 | } |
2730 | |
2787 | |
2731 | sub size_allocate { |
2788 | sub size_allocate { |
2732 | my ($self, $w, $h) = @_; |
2789 | my ($self, $w, $h) = @_; |
2733 | |
2790 | |
2734 | my $old_w = $self->{old_w}; |
2791 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2735 | my $old_h = $self->{old_h}; |
2792 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2736 | |
2793 | |
2737 | if ($old_w && $old_h) { |
2794 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2738 | for my $child ($self->children) { |
2795 | 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 | |
2796 | |
2750 | for my $child ($self->children) { |
2797 | for my $child ($self->children) { |
2751 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2798 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2752 | |
2799 | |
2753 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2800 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2754 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2801 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2755 | $child->configure ($X, $Y, $W, $H); |
2802 | $child->configure ($X, $Y, $W, $H); |
2756 | } |
2803 | } |
2757 | |
|
|
2758 | $self->{old_w} = $w; |
|
|
2759 | $self->{old_h} = $h; |
|
|
2760 | } |
2804 | } |
2761 | |
2805 | |
2762 | sub coord2local { |
2806 | sub coord2local { |
2763 | my ($self, $x, $y) = @_; |
2807 | my ($self, $x, $y) = @_; |
2764 | |
2808 | |
… | |
… | |
2777 | $self->check_size; |
2821 | $self->check_size; |
2778 | $::WANT_REFRESH++; |
2822 | $::WANT_REFRESH++; |
2779 | } |
2823 | } |
2780 | |
2824 | |
2781 | sub add { |
2825 | sub add { |
2782 | my ($self, $child) = @_; |
2826 | my ($self, @children) = @_; |
2783 | |
2827 | |
|
|
2828 | for my $child (@children) { |
|
|
2829 | $child->{toplevel} = 1; |
|
|
2830 | |
2784 | # integerise window positions |
2831 | # integerise window positions |
2785 | $child->{x} = int $child->{x}; |
2832 | $child->{x} = int $child->{x}; |
2786 | $child->{y} = int $child->{y}; |
2833 | $child->{y} = int $child->{y}; |
|
|
2834 | } |
2787 | |
2835 | |
2788 | $self->SUPER::add ($child); |
2836 | $self->SUPER::add (@children); |
2789 | } |
2837 | } |
2790 | |
2838 | |
2791 | sub on_refresh { |
2839 | sub on_refresh { |
2792 | my ($self, $id, $cb) = @_; |
2840 | my ($self, $id, $cb) = @_; |
2793 | |
2841 | |