ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.408 by root, Sun Jul 22 15:26:01 2007 UTC vs.
Revision 1.418 by elmex, Tue Jul 24 18:39:39 2007 UTC

273 $self->emit ("destroy"); 273 $self->emit ("destroy");
274 %$self = (); 274 %$self = ();
275} 275}
276 276
277sub TO_JSON { 277sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 278 { __w_ => $_[0]{s_id} }
279} 279}
280 280
281sub show { 281sub show {
282 my ($self) = @_; 282 my ($self) = @_;
283 283
725 @{$self->{children}}, @widgets 725 @{$self->{children}}, @widgets
726 ]; 726 ];
727 727
728 $self->realloc; 728 $self->realloc;
729 729
730 $self->emit (c_add => \@widgets);
731
730 map $_+0, @widgets 732 map $_+0, @widgets
731} 733}
732 734
733sub children { 735sub children {
734 @{ $_[0]{children} } 736 @{ $_[0]{children} }
735} 737}
736 738
737sub remove { 739sub remove {
738 my ($self, $child) = @_; 740 my ($self, @widgets) = @_;
739 741
742 $self->emit (c_remove => \@widgets);
743
744 for my $child (@widgets) {
740 delete $child->{parent}; 745 delete $child->{parent};
741 $child->hide; 746 $child->hide;
742
743 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 747 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
748 }
744 749
745 $self->realloc; 750 $self->realloc;
746} 751}
747 752
748sub clear { 753sub clear {
776} 781}
777 782
778sub _draw { 783sub _draw {
779 my ($self) = @_; 784 my ($self) = @_;
780 785
781 $_->draw for @{$self->{children}}; 786 $_->draw for $self->visible_children;
782} 787}
783 788
784############################################################################# 789#############################################################################
785 790
786package CFPlus::UI::Bin; 791package CFPlus::UI::Bin;
796} 801}
797 802
798sub add { 803sub add {
799 my ($self, $child) = @_; 804 my ($self, $child) = @_;
800 805
801 $self->SUPER::remove ($_) for @{ $self->{children} }; 806 $self->clear;
802 $self->SUPER::add ($child); 807 $self->SUPER::add ($child);
803} 808}
804 809
805sub remove { 810sub remove {
806 my ($self, $widget) = @_; 811 my ($self, $widget) = @_;
948 $self->{view_x} = $x; 953 $self->{view_x} = $x;
949 $self->{view_y} = $y; 954 $self->{view_y} = $y;
950 955
951 $self->emit (changed => $x, $y); 956 $self->emit (changed => $x, $y);
952 $self->update; 957 $self->update;
958 }
959}
960
961sub set_center {
962 my ($self, $x, $y) = @_;
963
964 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
965}
966
967sub make_visible {
968 my ($self, $x, $y, $border) = @_;
969
970 if ( $x < $self->{view_x} + $self->{w} * $border
971 || $x > $self->{view_x} + $self->{w} * (1 - $border)
972 || $y < $self->{view_y} + $self->{h} * $border
973 || $y > $self->{view_y} + $self->{h} * (1 - $border)
974 ) {
975 $self->set_center ($x, $y);
953 } 976 }
954} 977}
955 978
956# hmm, this does not work for topleft of $self... but we should not ask for that 979# hmm, this does not work for topleft of $self... but we should not ask for that
957sub coord2local { 980sub coord2local {
1070 my ($self, $widget) = @_; 1093 my ($self, $widget) = @_;
1071 1094
1072 $self->{vp}->add ($self->{child} = $widget); 1095 $self->{vp}->add ($self->{child} = $widget);
1073} 1096}
1074 1097
1098sub set_offset { shift->{vp}->set_offset (@_) }
1099sub set_center { shift->{vp}->set_center (@_) }
1100sub make_visible { shift->{vp}->make_visible (@_) }
1101
1075sub update_slider { 1102sub update_slider {
1076 my ($self) = @_; 1103 my ($self) = @_;
1077 1104
1078 my $child = ($self->{vp} or return)->child; 1105 my $child = ($self->{vp} or return)->child;
1079 1106
1686 1713
1687use List::Util qw(min max); 1714use List::Util qw(min max);
1688 1715
1689our @ISA = CFPlus::UI::Container::; 1716our @ISA = CFPlus::UI::Container::;
1690 1717
1691sub add_fixed {
1692 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1693
1694 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1695 $self->SUPER::add ($child);
1696}
1697
1698sub _scale($$$) { 1718sub _scale($$$) {
1699 my ($mode, $val, $max) = @_; 1719 my ($rel, $val, $max) = @_;
1700 1720
1701 $mode eq "abs" ? $val 1721 $rel ? $val * $max : $val
1702 : $mode eq "rel" ? $val * $max
1703 : 0
1704} 1722}
1705 1723
1706sub size_request { 1724sub size_request {
1707 my ($self) = @_; 1725 my ($self) = @_;
1708 1726
1709 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1710 1728
1711 # determine overall size by querying abs widgets 1729 # determine overall size by querying abs widgets
1712 for my $child ($self->visible_children) { 1730 for my $child ($self->visible_children) {
1713 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1731 unless ($child->{c_rel}) {
1732 my $x = $child->{c_x};
1733 my $y = $child->{c_y};
1714 1734
1715 if ($pos eq "abs") {
1716 $w = _scale $size, $w, $child->{req_w};
1717 $h = _scale $size, $h, $child->{req_h};
1718
1719 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1720 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1721 } 1737 }
1722 } 1738 }
1723 1739
1724 my $W = $x2 - $x1; 1740 my $W = $x2 - $x1;
1725 my $H = $y2 - $y1; 1741 my $H = $y2 - $y1;
1726 1742
1727 # now layout remaining widgets 1743 # now layout remaining widgets
1728 for my $child ($self->visible_children) { 1744 for my $child ($self->visible_children) {
1729 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1745 if ($child->{c_rel}) {
1746 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1747 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1730 1748
1731 if ($pos ne "abs") {
1732 $x = _scale $pos, $x, $W;
1733 $y = _scale $pos, $x, $H;
1734 $w = _scale $size, $w, $child->{req_w};
1735 $h = _scale $size, $h, $child->{req_h};
1736
1737 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1738 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1739 } 1751 }
1740 } 1752 }
1741 1753
1742 my $W = $x2 - $x1; 1754 my $W = $x2 - $x1;
1743 my $H = $y2 - $y1; 1755 my $H = $y2 - $y1;
1747 1759
1748sub invoke_size_allocate { 1760sub invoke_size_allocate {
1749 my ($self, $W, $H) = @_; 1761 my ($self, $W, $H) = @_;
1750 1762
1751 for my $child ($self->visible_children) { 1763 for my $child ($self->visible_children) {
1752 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1764 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1765 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1753 1766
1754 $x = _scale $pos, $x, $W; 1767 $x += $child->{c_halign} * $child->{req_w};
1755 $y = _scale $pos, $x, $H; 1768 $y += $child->{c_valign} * $child->{req_h};
1756 $w = _scale $size, $w, $W;
1757 $h = _scale $size, $h, $H;
1758 1769
1759 $child->configure ($x, $y, $w, $h); 1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1760 } 1771 }
1761 1772
1762 1 1773 1
1763} 1774}
1764 1775
1768 1779
1769our @ISA = CFPlus::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1770 1781
1771sub size_request { 1782sub size_request {
1772 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1773 1786
1774 $self->{vertical} 1787 $self->{vertical}
1775 ? ( 1788 ? (
1776 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1777 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1778 ) 1791 )
1779 : ( 1792 : (
1780 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1781 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1782 ) 1795 )
1783} 1796}
1784 1797
1785sub invoke_size_allocate { 1798sub invoke_size_allocate {
1786 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
2061 my $class = shift; 2074 my $class = shift;
2062 2075
2063 $class->SUPER::new ( 2076 $class->SUPER::new (
2064 fg => [1, 1, 1], 2077 fg => [1, 1, 1],
2065 bg => [0, 0, 0, 0.2], 2078 bg => [0, 0, 0, 0.2],
2066 active_bg => [1, 1, 1, 0.5], 2079 active_bg => [0, 0, 1, .2],
2067 active_fg => [0, 0, 0], 2080 active_fg => [1, 1, 1],
2081 active_outline => [1, 1, 0],
2068 can_hover => 1, 2082 can_hover => 1,
2069 can_focus => 1, 2083 can_focus => 1,
2070 valign => 0, 2084 valign => 0,
2071 can_events => 1, 2085 can_events => 1,
2072 ellipsise => 0, 2086 ellipsise => 0,
2229 2243
2230 unless (exists $self->{cur_h}) { 2244 unless (exists $self->{cur_h}) {
2231 my $text = substr $self->{text}, 0, $self->{cursor}; 2245 my $text = substr $self->{text}, 0, $self->{cursor};
2232 utf8::encode $text; 2246 utf8::encode $text;
2233 2247
2234 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2248 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2235 } 2249 }
2236 2250
2251 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2252 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2253 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2254 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2255 glEnd;
2256
2257 glLineWidth 3;
2258 glColor @{$self->{active_outline}};
2259 glTranslate -.375, -.375;
2260 glBegin GL_LINE_LOOP;
2261 glVertex 0, 0;
2262 glVertex $self->{w}, 0;
2263 glVertex $self->{w}, $self->{h};
2264 glVertex 0, $self->{h};
2265 glEnd;
2266 glLineWidth 1;
2241 } 2267 }
2242} 2268}
2243 2269
2244############################################################################# 2270#############################################################################
2245 2271
2331 1 2357 1
2332} 2358}
2333 2359
2334############################################################################# 2360#############################################################################
2335 2361
2362package CFPlus::UI::ButtonBin;
2363
2364our @ISA = CFPlus::UI::Bin::;
2365
2366use CFPlus::OpenGL;
2367
2368my @tex =
2369 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2370 qw(b1_button_inactive.png b1_button_active.png);
2371
2372sub new {
2373 my $class = shift;
2374
2375 $class->SUPER::new (
2376 can_hover => 1,
2377 align => 0,
2378 valign => 0,
2379 can_events => 1,
2380 @_
2381 )
2382}
2383
2384sub invoke_button_up {
2385 my ($self, $ev, $x, $y) = @_;
2386
2387 $self->emit ("activate")
2388 if $x >= 0 && $x < $self->{w}
2389 && $y >= 0 && $y < $self->{h};
2390
2391 1
2392}
2393
2394sub _draw {
2395 my ($self) = @_;
2396
2397 glEnable GL_TEXTURE_2D;
2398 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2399 glColor 0, 0, 0, 1;
2400
2401 my $tex = $tex[$GRAB == $self];
2402 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2403
2404 glDisable GL_TEXTURE_2D;
2405
2406 $self->SUPER::_draw;
2407}
2408
2409#############################################################################
2410
2336package CFPlus::UI::Button; 2411package CFPlus::UI::Button;
2337 2412
2338our @ISA = CFPlus::UI::Label::; 2413our @ISA = CFPlus::UI::Label::;
2339 2414
2340use CFPlus::OpenGL; 2415use CFPlus::OpenGL;
2474sub new { 2549sub new {
2475 my $class = shift; 2550 my $class = shift;
2476 2551
2477 my $self = $class->SUPER::new ( 2552 my $self = $class->SUPER::new (
2478 can_events => 0, 2553 can_events => 0,
2554 scale => 1,
2479 @_, 2555 @_,
2480 ); 2556 );
2481 2557
2482 $self->{path} || $self->{tex} 2558 $self->{path} || $self->{tex}
2483 or Carp::croak "'path' or 'tex' attributes required"; 2559 or Carp::croak "'path' or 'tex' attributes required";
2508} 2584}
2509 2585
2510sub size_request { 2586sub size_request {
2511 my ($self) = @_; 2587 my ($self) = @_;
2512 2588
2513 ($self->{tex}{w}, $self->{tex}{h}) 2589 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2514} 2590}
2515 2591
2516sub _draw { 2592sub _draw {
2517 my ($self) = @_; 2593 my ($self) = @_;
2518 2594
2528 } 2604 }
2529 2605
2530 glEnable GL_TEXTURE_2D; 2606 glEnable GL_TEXTURE_2D;
2531 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2607 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2532 2608
2533 $tex->draw_quad (0, 0, $w, $h); 2609 $tex->draw_quad_alpha (0, 0, $w, $h);
2534 2610
2535 glDisable GL_TEXTURE_2D; 2611 glDisable GL_TEXTURE_2D;
2536} 2612}
2537 2613
2538############################################################################# 2614#############################################################################
2840 2916
2841 $self->SUPER::invoke_button_down ($ev, $x, $y); 2917 $self->SUPER::invoke_button_down ($ev, $x, $y);
2842 2918
2843 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2919 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2844 2920
2845 $self->invoke_mouse_motion ($ev, $x, $y) 2921 $self->invoke_mouse_motion ($ev, $x, $y);
2922
2923 1
2846} 2924}
2847 2925
2848sub invoke_mouse_motion { 2926sub invoke_mouse_motion {
2849 my ($self, $ev, $x, $y) = @_; 2927 my ($self, $ev, $x, $y) = @_;
2850 2928
2870 2948
2871 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 2949 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2872 2950
2873 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 2951 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2874 2952
2875 ! ! $delta 2953 1
2876} 2954}
2877 2955
2878sub update { 2956sub update {
2879 my ($self) = @_; 2957 my ($self) = @_;
2880 2958
3011} 3089}
3012 3090
3013sub size_request { 3091sub size_request {
3014 my ($self) = @_; 3092 my ($self) = @_;
3015 3093
3016 my ($empty, $slider) = @{ $self->{children} }; 3094 my ($empty, $slider) = $self->visible_children;
3017 3095
3018 local $self->{children} = [$empty, $slider]; 3096 local $self->{children} = [$empty, $slider];
3019 $self->SUPER::size_request 3097 $self->SUPER::size_request
3020} 3098}
3021 3099
3607 children => [ 3685 children => [
3608 (new CFPlus::UI::Label markup => $left, expand => 1), 3686 (new CFPlus::UI::Label markup => $left, expand => 1),
3609 (new CFPlus::UI::Label markup => $right, align => +1), 3687 (new CFPlus::UI::Label markup => $right, align => +1),
3610 ], 3688 ],
3611 ; 3689 ;
3612 3690
3613 } else { 3691 } else {
3614 $widget = new CFPlus::UI::Label 3692 $widget = new CFPlus::UI::Label
3615 can_hover => 1, 3693 can_hover => 1,
3616 can_events => 1, 3694 can_events => 1,
3617 markup => $widget, 3695 markup => $widget,
3753 buttonbar => (new CFPlus::UI::Buttonbar), 3831 buttonbar => (new CFPlus::UI::Buttonbar),
3754 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3832 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3755 # filter => # will be put between multiplexer and $self 3833 # filter => # will be put between multiplexer and $self
3756 @_, 3834 @_,
3757 ); 3835 );
3758 3836
3759 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3837 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3760 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3838 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3761 3839
3840 {
3841 Scalar::Util::weaken (my $wself = $self);
3842
3843 $self->{multiplexer}->connect (c_add => sub {
3844 my ($mplex, $widgets) = @_;
3845
3846 for my $child (@$widgets) {
3847 Scalar::Util::weaken $child;
3848 $child->{c_tab_} ||= do {
3849 my $tab =
3850 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3851 ? $child->{c_tab}
3852 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3853
3854 $tab->connect (activate => sub {
3855 $wself->set_current_page ($child);
3856 });
3857
3858 $tab
3859 };
3860
3861 $self->{buttonbar}->add ($child->{c_tab_});
3862 }
3863 });
3864
3865 $self->{multiplexer}->connect (c_remove => sub {
3866 my ($mplex, $widgets) = @_;
3867
3868 for my $child (@$widgets) {
3869 $wself->{buttonbar}->remove ($child->{c_tab_});
3870 }
3871 });
3872 }
3873
3762 $self 3874 $self
3763} 3875}
3764 3876
3765sub add { 3877sub add {
3878 my ($self, @widgets) = @_;
3879
3880 $self->{multiplexer}->add (@widgets)
3881}
3882
3883sub remove {
3884 my ($self, @widgets) = @_;
3885
3886 $self->{multiplexer}->remove (@widgets)
3887}
3888
3889sub pages {
3890 my ($self) = @_;
3891 $self->{multiplexer}->children
3892}
3893
3894sub add_tab {
3766 my ($self, $title, $widget, $tooltip) = @_; 3895 my ($self, $title, $widget, $tooltip) = @_;
3767 3896
3768 CFPlus::weaken $self; 3897 $title = [$title, $tooltip] unless ref $title;
3898 $widget->{c_tab} = $title;
3769 3899
3770 unless (ref $title) {
3771 $title = new CFPlus::UI::Button
3772 markup => $title,
3773 tooltip => $tooltip,
3774 );
3775 }
3776
3777 $title->connect (activate => sub { $self->set_current_page ($widget) });
3778 $self->{buttonbar}->add ($title);
3779
3780 $self->{multiplexer}->add ($widget); 3900 $self->add ($widget);
3781} 3901}
3782 3902
3783sub get_current_page { 3903sub get_current_page {
3784 my ($self) = @_; 3904 my ($self) = @_;
3785 3905

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines