… | |
… | |
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) = @_; |
… | |
… | |
242 | |
266 | |
243 | sub size_allocate { |
267 | sub size_allocate { |
244 | # nothing to be done |
268 | # nothing to be done |
245 | } |
269 | } |
246 | |
270 | |
|
|
271 | sub reconfigure { |
|
|
272 | my ($self) = @_; |
|
|
273 | |
|
|
274 | $self->check_size (1); |
|
|
275 | $self->update; |
|
|
276 | } |
|
|
277 | |
247 | sub children { |
278 | 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 | } |
279 | } |
261 | |
280 | |
262 | sub set_max_size { |
281 | sub set_max_size { |
263 | my ($self, $w, $h) = @_; |
282 | my ($self, $w, $h) = @_; |
264 | |
283 | |
… | |
… | |
384 | sub set_parent { |
403 | sub set_parent { |
385 | my ($self, $parent) = @_; |
404 | my ($self, $parent) = @_; |
386 | |
405 | |
387 | Scalar::Util::weaken ($self->{parent} = $parent); |
406 | Scalar::Util::weaken ($self->{parent} = $parent); |
388 | |
407 | |
|
|
408 | # TODO: req_w _does_change after ->reconfigure |
389 | $self->check_size |
409 | $self->check_size |
390 | unless exists $self->{req_w}; |
410 | unless exists $self->{req_w}; |
391 | } |
411 | } |
392 | |
412 | |
393 | sub check_size { |
413 | sub check_size { |
… | |
… | |
417 | } |
437 | } |
418 | |
438 | |
419 | sub DESTROY { |
439 | sub DESTROY { |
420 | my ($self) = @_; |
440 | my ($self) = @_; |
421 | |
441 | |
|
|
442 | delete $WIDGET{$self+0}; |
422 | #$self->deactivate; |
443 | #$self->deactivate; |
423 | } |
444 | } |
424 | |
445 | |
425 | ############################################################################# |
446 | ############################################################################# |
426 | |
447 | |
… | |
… | |
628 | } |
649 | } |
629 | |
650 | |
630 | sub update { |
651 | sub update { |
631 | my ($self) = @_; |
652 | my ($self) = @_; |
632 | |
653 | |
633 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
654 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
634 | $self->SUPER::update; |
655 | $self->SUPER::update; |
635 | } |
656 | } |
636 | |
657 | |
637 | sub size_allocate { |
658 | sub size_allocate { |
638 | my ($self, $w, $h) = @_; |
659 | my ($self, $w, $h) = @_; |
… | |
… | |
776 | $self->add ($self->{slider}); |
797 | $self->add ($self->{slider}); |
777 | |
798 | |
778 | $self |
799 | $self |
779 | } |
800 | } |
780 | |
801 | |
781 | #TODO# update range on size_allocate depeneing on child |
802 | #TODO# update range on size_allocate depending on child |
782 | # update viewport offset on scroll |
803 | # update viewport offset on scroll |
783 | |
804 | |
784 | ############################################################################# |
805 | ############################################################################# |
785 | |
806 | |
786 | package CFClient::UI::Frame; |
807 | package CFClient::UI::Frame; |
787 | |
808 | |
788 | our @ISA = CFClient::UI::Bin::; |
809 | our @ISA = CFClient::UI::Bin::; |
789 | |
810 | |
790 | use CFClient::OpenGL; |
811 | use CFClient::OpenGL; |
|
|
812 | |
|
|
813 | sub new { |
|
|
814 | my $class = shift; |
|
|
815 | |
|
|
816 | $class->SUPER::new ( |
|
|
817 | bg => undef, |
|
|
818 | @_, |
|
|
819 | ) |
|
|
820 | } |
|
|
821 | |
|
|
822 | sub _draw { |
|
|
823 | my ($self) = @_; |
|
|
824 | |
|
|
825 | if ($self->{bg}) { |
|
|
826 | my ($w, $h) = @$self{qw(w h)}; |
|
|
827 | |
|
|
828 | glEnable GL_BLEND; |
|
|
829 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
830 | glColor @{ $self->{bg} }; |
|
|
831 | |
|
|
832 | glBegin GL_QUADS; |
|
|
833 | glVertex 0 , 0; |
|
|
834 | glVertex 0 , $h; |
|
|
835 | glVertex $w, $h; |
|
|
836 | glVertex $w, 0; |
|
|
837 | glEnd; |
|
|
838 | |
|
|
839 | glDisable GL_BLEND; |
|
|
840 | } |
|
|
841 | |
|
|
842 | $self->SUPER::_draw; |
|
|
843 | } |
791 | |
844 | |
792 | ############################################################################# |
845 | ############################################################################# |
793 | |
846 | |
794 | package CFClient::UI::FancyFrame; |
847 | package CFClient::UI::FancyFrame; |
795 | |
848 | |
… | |
… | |
808 | |
861 | |
809 | my $self = $class->SUPER::new ( |
862 | my $self = $class->SUPER::new ( |
810 | bg => [1, 1, 1, 1], |
863 | bg => [1, 1, 1, 1], |
811 | border_bg => [1, 1, 1, 1], |
864 | border_bg => [1, 1, 1, 1], |
812 | border => 0.6, |
865 | border => 0.6, |
|
|
866 | toplevel => 1, |
813 | can_events => 1, |
867 | can_events => 1, |
814 | @_ |
868 | @_ |
815 | ); |
869 | ); |
816 | |
870 | |
817 | $self->{title} &&= new CFClient::UI::Label |
871 | $self->{title} &&= new CFClient::UI::Label |
… | |
… | |
1448 | my $sym = $ev->{sym}; |
1502 | my $sym = $ev->{sym}; |
1449 | my $uni = $ev->{unicode}; |
1503 | my $uni = $ev->{unicode}; |
1450 | |
1504 | |
1451 | my $text = $self->get_text; |
1505 | my $text = $self->get_text; |
1452 | |
1506 | |
1453 | if ($sym == 8) { |
1507 | if ($uni == 8) { |
1454 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1508 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1455 | } elsif ($sym == 127) { |
1509 | } elsif ($uni == 127) { |
1456 | substr $text, $self->{cursor}, 1, ""; |
1510 | substr $text, $self->{cursor}, 1, ""; |
1457 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1511 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1458 | --$self->{cursor} if $self->{cursor}; |
1512 | --$self->{cursor} if $self->{cursor}; |
1459 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1513 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1460 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1514 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1461 | } elsif ($sym == CFClient::SDLK_HOME) { |
1515 | } elsif ($sym == CFClient::SDLK_HOME) { |
1462 | $self->{cursor} = 0; |
1516 | $self->{cursor} = 0; |
1463 | } elsif ($sym == CFClient::SDLK_END) { |
1517 | } elsif ($sym == CFClient::SDLK_END) { |
1464 | $self->{cursor} = length $text; |
1518 | $self->{cursor} = length $text; |
1465 | } elsif ($sym == 27) { |
1519 | } elsif ($uni == 27) { |
1466 | $self->emit ('escape'); |
1520 | $self->emit ('escape'); |
1467 | } elsif ($uni) { |
1521 | } elsif ($uni) { |
1468 | substr $text, $self->{cursor}++, 0, chr $uni; |
1522 | substr $text, $self->{cursor}++, 0, chr $uni; |
1469 | } |
1523 | } |
1470 | |
1524 | |
… | |
… | |
2165 | |
2219 | |
2166 | return unless $self->{h} > 0; |
2220 | return unless $self->{h} > 0; |
2167 | |
2221 | |
2168 | delete $self->{texture}; |
2222 | delete $self->{texture}; |
2169 | |
2223 | |
2170 | $ROOT->on_refresh ($self, sub { |
2224 | $ROOT->on_post_alloc ($self, sub { |
2171 | if (delete $self->{need_reflow}) { |
2225 | if (delete $self->{need_reflow}) { |
2172 | my $height = 0; |
2226 | my $height = 0; |
2173 | |
2227 | |
2174 | $height += $_->[0] = $self->text_height ($_->[2]) |
2228 | $height += $_->[0] = $self->text_height ($_->[2]) |
2175 | for @{$self->{par}}; |
2229 | for @{$self->{par}}; |
… | |
… | |
2506 | my ($self, $items) = @_; |
2560 | my ($self, $items) = @_; |
2507 | |
2561 | |
2508 | $self->{scrolled}->clear; |
2562 | $self->{scrolled}->clear; |
2509 | return unless $items; |
2563 | return unless $items; |
2510 | |
2564 | |
2511 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
2565 | my @items = sort { |
|
|
2566 | ($a->{type} <=> $b->{type}) |
|
|
2567 | or ($a->{name} cmp $b->{name}) |
|
|
2568 | } @$items; |
2512 | |
2569 | |
2513 | $self->{real_items} = \@items; |
2570 | $self->{real_items} = \@items; |
2514 | |
2571 | |
2515 | for my $item (@items) { |
2572 | for my $item (@items) { |
2516 | my $desc = $item->{nrof} < 2 |
2573 | my $desc = $item->{nrof} < 2 |
2517 | ? $item->{name} |
2574 | ? $item->{name} |
2518 | : "$item->{nrof} $item->{name_pl}"; |
2575 | : "$item->{nrof} $item->{name_pl}"; |
2519 | |
2576 | |
2520 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2577 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2521 | } |
2578 | } |
|
|
2579 | |
|
|
2580 | $self->{scrolled}->add (@items); |
2522 | |
2581 | |
2523 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2582 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2524 | } |
2583 | } |
2525 | |
2584 | |
2526 | sub size_request { |
2585 | sub size_request { |
… | |
… | |
2632 | : $item->{text}; |
2691 | : $item->{text}; |
2633 | |
2692 | |
2634 | for ($short) { |
2693 | for ($short) { |
2635 | s/^\s+//; |
2694 | s/^\s+//; |
2636 | s/\012.*/…/s; |
2695 | s/\012.*/…/s; |
2637 | my $len = int 30 / $item->{fontsize}; |
2696 | my $len = int 40 / $item->{fontsize}; |
2638 | substr $_, $len, length, "…" if $len < length; |
2697 | substr $_, $len, length, "…" if $len < length; |
2639 | } |
2698 | } |
2640 | |
2699 | |
2641 | new CFClient::UI::Label |
2700 | new CFClient::UI::Label |
2642 | markup => $short, |
2701 | markup => $short, |
… | |
… | |
2655 | } |
2714 | } |
2656 | |
2715 | |
2657 | sub add { |
2716 | sub add { |
2658 | my ($self, $text, %arg) = @_; |
2717 | my ($self, $text, %arg) = @_; |
2659 | |
2718 | |
|
|
2719 | $text =~ s/^\s+//; |
|
|
2720 | $text =~ s/\s+$//; |
|
|
2721 | |
2660 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2722 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2661 | |
2723 | |
2662 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2724 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2663 | |
2725 | |
2664 | if (my $item = $self->{item}{$group}) { |
2726 | if (my $item = $self->{item}{$group}) { |
… | |
… | |
2666 | $item->{count}++; |
2728 | $item->{count}++; |
2667 | } else { |
2729 | } else { |
2668 | $item->{count} = 1; |
2730 | $item->{count} = 1; |
2669 | $item->{text} = $item->{tooltip} = $text; |
2731 | $item->{text} = $item->{tooltip} = $text; |
2670 | } |
2732 | } |
|
|
2733 | $item->{id} = ++$self->{id}; |
2671 | $item->{timeout} = $timeout; |
2734 | $item->{timeout} = $timeout; |
2672 | delete $item->{label}; |
2735 | delete $item->{label}; |
2673 | } else { |
2736 | } else { |
2674 | $self->{item}{$group} = { |
2737 | $self->{item}{$group} = { |
2675 | id => ++$self->{id}, |
2738 | id => ++$self->{id}, |
… | |
… | |
2724 | } |
2787 | } |
2725 | |
2788 | |
2726 | sub size_allocate { |
2789 | sub size_allocate { |
2727 | my ($self, $w, $h) = @_; |
2790 | my ($self, $w, $h) = @_; |
2728 | |
2791 | |
2729 | my $old_w = $self->{old_w}; |
2792 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
2730 | my $old_h = $self->{old_h}; |
2793 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
2731 | |
2794 | |
2732 | if ($old_w && $old_h) { |
2795 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2733 | for my $child ($self->children) { |
2796 | 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 | |
2797 | |
2745 | for my $child ($self->children) { |
2798 | for my $child ($self->children) { |
2746 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2799 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2747 | |
2800 | |
2748 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2801 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2749 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2802 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2750 | $child->configure ($X, $Y, $W, $H); |
2803 | $child->configure ($X, $Y, $W, $H); |
2751 | } |
2804 | } |
2752 | |
|
|
2753 | $self->{old_w} = $w; |
|
|
2754 | $self->{old_h} = $h; |
|
|
2755 | } |
2805 | } |
2756 | |
2806 | |
2757 | sub coord2local { |
2807 | sub coord2local { |
2758 | my ($self, $x, $y) = @_; |
2808 | my ($self, $x, $y) = @_; |
2759 | |
2809 | |
… | |
… | |
2772 | $self->check_size; |
2822 | $self->check_size; |
2773 | $::WANT_REFRESH++; |
2823 | $::WANT_REFRESH++; |
2774 | } |
2824 | } |
2775 | |
2825 | |
2776 | sub add { |
2826 | sub add { |
2777 | my ($self, $child) = @_; |
2827 | my ($self, @children) = @_; |
2778 | |
2828 | |
|
|
2829 | for my $child (@children) { |
|
|
2830 | $child->{toplevel} = 1; |
|
|
2831 | |
2779 | # integerise window positions |
2832 | # integerise window positions |
2780 | $child->{x} = int $child->{x}; |
2833 | $child->{x} = int $child->{x}; |
2781 | $child->{y} = int $child->{y}; |
2834 | $child->{y} = int $child->{y}; |
|
|
2835 | } |
2782 | |
2836 | |
2783 | $self->SUPER::add ($child); |
2837 | $self->SUPER::add (@children); |
2784 | } |
2838 | } |
2785 | |
2839 | |
2786 | sub on_refresh { |
2840 | sub on_refresh { |
2787 | my ($self, $id, $cb) = @_; |
2841 | my ($self, $id, $cb) = @_; |
2788 | |
2842 | |
2789 | $self->{refresh_hook}{$id} = $cb; |
2843 | $self->{refresh_hook}{$id} = $cb; |
2790 | } |
2844 | } |
2791 | |
2845 | |
|
|
2846 | sub on_post_alloc { |
|
|
2847 | my ($self, $id, $cb) = @_; |
|
|
2848 | |
|
|
2849 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2850 | } |
|
|
2851 | |
2792 | sub draw { |
2852 | sub draw { |
2793 | my ($self) = @_; |
2853 | my ($self) = @_; |
|
|
2854 | |
|
|
2855 | while ($self->{refresh_hook}) { |
|
|
2856 | $_->() |
|
|
2857 | for values %{delete $self->{refresh_hook}}; |
|
|
2858 | } |
2794 | |
2859 | |
2795 | if ($self->{check_size}) { |
2860 | if ($self->{check_size}) { |
2796 | my @queue = ([], []); |
2861 | my @queue = ([], []); |
2797 | |
2862 | |
2798 | for (;;) { |
2863 | for (;;) { |
… | |
… | |
2835 | $widget->size_allocate ($w, $h); |
2900 | $widget->size_allocate ($w, $h); |
2836 | $widget->emit (size_allocate => $w, $h); |
2901 | $widget->emit (size_allocate => $w, $h); |
2837 | } |
2902 | } |
2838 | } |
2903 | } |
2839 | |
2904 | |
2840 | while ($self->{refresh_hook}) { |
2905 | while ($self->{post_alloc_hook}) { |
2841 | $_->() |
2906 | $_->() |
2842 | for values %{delete $self->{refresh_hook}}; |
2907 | for values %{delete $self->{post_alloc_hook}}; |
2843 | } |
2908 | } |
2844 | |
2909 | |
2845 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2910 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2846 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2911 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2847 | glClear GL_COLOR_BUFFER_BIT; |
2912 | glClear GL_COLOR_BUFFER_BIT; |