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.422 by root, Thu Aug 2 14:35:21 2007 UTC vs.
Revision 1.434 by root, Wed Aug 22 21:32:09 2007 UTC

607sub DESTROY { 607sub DESTROY {
608 my ($self) = @_; 608 my ($self) = @_;
609 609
610 return if CFPlus::in_destruct; 610 return if CFPlus::in_destruct;
611 611
612 local $@;
612 eval { $self->destroy }; 613 eval { $self->destroy };
613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 614 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614 615
615 delete $WIDGET{$self+0}; 616 delete $WIDGET{$self+0};
616} 617}
624use strict; 625use strict;
625use CFPlus::OpenGL; 626use CFPlus::OpenGL;
626 627
627sub new { 628sub new {
628 my $class = shift; 629 my $class = shift;
629
630 # range [value, low, high, page]
631 630
632 $class->SUPER::new ( 631 $class->SUPER::new (
633 #bg => [0, 0, 0, 0.2], 632 #bg => [0, 0, 0, 0.2],
634 #active_bg => [1, 1, 1, 0.5], 633 #active_bg => [1, 1, 1, 0.5],
635 @_ 634 @_
647 my ($w, $h) = @$self{qw(w h)}; 646 my ($w, $h) = @$self{qw(w h)};
648 647
649 glEnable GL_BLEND; 648 glEnable GL_BLEND;
650 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 649 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
651 glColor_premultiply @$color; 650 glColor_premultiply @$color;
652
653 glBegin GL_QUADS;
654 glVertex 0 , 0;
655 glVertex 0 , $h;
656 glVertex $w, $h; 651 glRect 0, 0, $w, $h;
657 glVertex $w, 0;
658 glEnd;
659
660 glDisable GL_BLEND; 652 glDisable GL_BLEND;
661 } 653 }
662} 654}
663 655
664############################################################################# 656#############################################################################
1219 my ($w, $h) = @$self{qw(w h)}; 1211 my ($w, $h) = @$self{qw(w h)};
1220 1212
1221 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1214 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1223 glColor_premultiply @{ $self->{bg} }; 1215 glColor_premultiply @{ $self->{bg} };
1224
1225 glBegin GL_QUADS;
1226 glVertex 0 , 0;
1227 glVertex 0 , $h;
1228 glVertex $w, $h; 1216 glRect 0, 0, $w, $h;
1229 glVertex $w, 0;
1230 glEnd;
1231
1232 glDisable GL_BLEND; 1217 glDisable GL_BLEND;
1233 } 1218 }
1234 1219
1235 $self->SUPER::_draw; 1220 $self->SUPER::_draw;
1236} 1221}
2044 : ($self->{w} - $size->[0]) * 0.5); 2029 : ($self->{w} - $size->[0]) * 0.5);
2045 2030
2046 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2031 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2047 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2032 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2048 : ($self->{h} - $size->[1]) * 0.5); 2033 : ($self->{h} - $size->[1]) * 0.5);
2034
2035 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2049 }; 2036 };
2050 2037
2051# unless ($self->{list}) { 2038# unless ($self->{list}) {
2052# $self->{list} = CFPlus::OpenGL::glGenList; 2039# $self->{list} = CFPlus::OpenGL::glGenList;
2053# CFPlus::OpenGL::glNewList $self->{list}; 2040# CFPlus::OpenGL::glNewList $self->{list};
2055# CFPlus::OpenGL::glEndList; 2042# CFPlus::OpenGL::glEndList;
2056# } 2043# }
2057# 2044#
2058# CFPlus::OpenGL::glCallList $self->{list}; 2045# CFPlus::OpenGL::glCallList $self->{list};
2059 2046
2060 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2047 $self->{layout}->draw;
2061} 2048}
2062 2049
2063#sub destroy { 2050#sub destroy {
2064# my ($self) = @_; 2051# my ($self) = @_;
2065# 2052#
2233 glColor_premultiply @{$self->{bg}}; 2220 glColor_premultiply @{$self->{bg}};
2234 } 2221 }
2235 2222
2236 glEnable GL_BLEND; 2223 glEnable GL_BLEND;
2237 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2224 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2238 glBegin GL_QUADS;
2239 glVertex 0 , 0;
2240 glVertex 0 , $self->{h};
2241 glVertex $self->{w}, $self->{h}; 2225 glRect 0, 0, $self->{w}, $self->{h};
2242 glVertex $self->{w}, 0;
2243 glEnd;
2244 glDisable GL_BLEND; 2226 glDisable GL_BLEND;
2245 2227
2246 $self->SUPER::_draw; 2228 $self->SUPER::_draw;
2247 2229
2248 #TODO: force update every cursor change :( 2230 #TODO: force update every cursor change :(
2261 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2243 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2262 glEnd; 2244 glEnd;
2263 2245
2264 glLineWidth 3; 2246 glLineWidth 3;
2265 glColor @{$self->{active_outline}}; 2247 glColor @{$self->{active_outline}};
2266 glBegin GL_LINE_LOOP;
2267 glVertex 0, 0;
2268 glVertex $self->{w} - 1, 0;
2269 glVertex $self->{w} - 1, $self->{h} - 1; 2248 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1;
2270 glVertex 0, $self->{h} - 1;
2271 glEnd;
2272 glLineWidth 1; 2249 glLineWidth 1;
2273 2250
2274 } else { 2251 } else {
2275 glColor @{$self->{outline}}; 2252 glColor @{$self->{outline}};
2276 glTranslate .375, .375; 2253 glTranslate .375, .375;
2649 align => 0, 2626 align => 0,
2650 valign => 0, 2627 valign => 0,
2651 can_events => 1, 2628 can_events => 1,
2652 @_ 2629 @_
2653 ); 2630 );
2631}
2632
2633sub invoke_button_down {
2634 my ($self, $ev, $x, $y) = @_;
2635
2636 1
2654} 2637}
2655 2638
2656sub invoke_button_up { 2639sub invoke_button_up {
2657 my ($self, $ev, $x, $y) = @_; 2640 my ($self, $ev, $x, $y) = @_;
2658 2641
2798 glDisable GL_TEXTURE_2D; 2781 glDisable GL_TEXTURE_2D;
2799} 2782}
2800 2783
2801############################################################################# 2784#############################################################################
2802 2785
2786package CFPlus::UI::Progress;
2787
2788our @ISA = CFPlus::UI::Label::;
2789
2790use CFPlus::OpenGL;
2791
2792sub new {
2793 my ($class, %arg) = @_;
2794
2795 my $self = $class->SUPER::new (
2796 fg => [1, 1, 1],
2797 bg => [0, 0, 1, 0.2],
2798 bar => [0.7, 0.5, 0.1, 0.8],
2799 outline => [0.4, 0.3, 0],
2800 fontsize => 0.9,
2801 valign => 0,
2802 align => 0,
2803 can_events => 1,
2804 ellipsise => 1,
2805 label => "%d%%",
2806 %arg,
2807 );
2808
2809 $self->set_value ($arg{value} || -1);
2810
2811 $self
2812}
2813
2814sub set_label {
2815 my ($self, $label) = @_;
2816
2817 return if $self->{label} eq $label;
2818 $self->{label} = $label;
2819
2820 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2821}
2822
2823sub set_value {
2824 my ($self, $value) = @_;
2825
2826 if ($self->{value} ne $value) {
2827 $self->{value} = $value;
2828
2829 if ($value < 0) {
2830 $self->set_text ("-");
2831 } else {
2832 $self->set_text (sprintf $self->{label}, $value * 100);
2833 }
2834
2835 $self->update;
2836 }
2837}
2838
2839sub _draw {
2840 my ($self) = @_;
2841
2842 glEnable GL_BLEND;
2843 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2844
2845 if ($self->{value} >= 0) {
2846 my $s = 3 + ($self->{w} - 6) * $self->{value};
2847
2848 glColor_premultiply @{$self->{bar}};
2849 glRect 2, 2, $s, $self->{h} - 2;
2850 glColor_premultiply @{$self->{bg}};
2851 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2;
2852 }
2853
2854 glColor_premultiply @{$self->{outline}};
2855 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2;
2856
2857 glDisable GL_BLEND;
2858
2859 {
2860 local $self->{bg}; # do not draw background
2861 $self->SUPER::_draw;
2862 }
2863}
2864
2865#############################################################################
2866
2867package CFPlus::UI::ExperienceProgress;
2868
2869our @ISA = CFPlus::UI::Progress::;
2870
2871sub new {
2872 my ($class, %arg) = @_;
2873
2874 my $self = $class->SUPER::new (
2875 tooltip => sub {
2876 my ($self) = @_;
2877
2878 sprintf "%s points experience (level %d).\n%s points to next level %s.",
2879 ::formsep $self->{exp},
2880 $self->{lvl},
2881 ::formsep $self->{nxt}
2882 },
2883 %arg
2884 );
2885
2886 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2887 if $::CONN;
2888
2889 $self
2890}
2891
2892sub DESTROY {
2893 my ($self) = @_;
2894
2895 delete $::CONN->{on_exp_update}{$self+0}
2896 if $::CONN;
2897
2898 $self->SUPER::DESTROY;
2899}
2900
2901sub set_value {
2902 my ($self, $lvl, $exp) = @_;
2903
2904 $self->{lvl} = $exp;
2905 $self->{exp} = $exp;
2906
2907 my $v = -1;
2908
2909 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2910 my $l0 = $table->[$lvl - 1];
2911 my $l1 = $table->[$lvl];
2912
2913 $self->{nxt} = $l1;
2914
2915 $v = ($exp - $l0) / ($l1 - $l0);
2916 }
2917
2918 $self->SUPER::set_value ($v);
2919}
2920
2921#############################################################################
2922
2803package CFPlus::UI::Gauge; 2923package CFPlus::UI::Gauge;
2804 2924
2805our @ISA = CFPlus::UI::VBox::; 2925our @ISA = CFPlus::UI::VBox::;
2806 2926
2807sub new { 2927sub new {
3304 3424
3305 if ($y0 < $y + $h && $y < $y1) { 3425 if ($y0 < $y + $h && $y < $y1) {
3306 my $layout = $self->get_layout ($para); 3426 my $layout = $self->get_layout ($para);
3307 3427
3308 $layout->render ($para->{indent}, $y - $y0); 3428 $layout->render ($para->{indent}, $y - $y0);
3429 $layout->draw;
3309 3430
3310 if (my @w = @{ $para->{widget} }) { 3431 if (my @w = @{ $para->{widget} }) {
3311 my @s = $layout->get_shapes; 3432 my @s = $layout->get_shapes;
3312 3433
3313 for (@w) { 3434 for (@w) {
3507 3628
3508 glColor 1, 0.8, 0.4; 3629 glColor 1, 0.8, 0.4;
3509 glRect 0, 0, $w, $h; 3630 glRect 0, 0, $w, $h;
3510 3631
3511 glColor 0, 0, 0; 3632 glColor 0, 0, 0;
3512 glBegin GL_LINE_LOOP; 3633 glRect_lineloop 0, 0, $w, $h;
3513 glVertex 0 , 0;
3514 glVertex 0 , $h;
3515 glVertex $w, $h;
3516 glVertex $w, 0;
3517 glEnd;
3518 3634
3519 glTranslate 2 - 0.375, 2 - 0.375; 3635 glTranslate 2 - 0.375, 2 - 0.375;
3520 3636
3521 $self->SUPER::_draw; 3637 $self->SUPER::_draw;
3522} 3638}
3546 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3662 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3547 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3663 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3548 $self->{timer} = Event->timer ( 3664 $self->{timer} = Event->timer (
3549 parked => 1, 3665 parked => 1,
3550 cb => sub { 3666 cb => sub {
3551 return unless $::CONN && $widget; 3667 return unless $::CONN;
3552 3668
3669 my $w = $widget
3670 or return;
3671
3553 ++$widget->{frame}; 3672 ++$w->{frame};
3554 $widget->update_face; 3673 $w->update_face;
3674
3675 # somehow, $widget can go away
3555 $widget->update; 3676 $w->update;
3556
3557 $widget->update_timer; 3677 $w->update_timer;
3558 }, 3678 },
3559 ); 3679 );
3560 3680
3561 $self->update_face; 3681 $self->update_face;
3562 $self->update_timer; 3682 $self->update_timer;
3583} 3703}
3584 3704
3585sub update_face { 3705sub update_face {
3586 my ($self) = @_; 3706 my ($self) = @_;
3587 3707
3588 return unless $::CONN; 3708 if ($::CONN) {
3589
3590 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3709 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3591 if ($anim && @$anim) { 3710 if ($anim && @$anim) {
3592 delete $self->{wait_face};
3593 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3711 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3712 delete $self->{face_change_cb};
3713
3594 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3714 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3715 unless ($tex->{name} || $tex->{loading}) {
3716 $tex->upload (sub { $self->reconfigure });
3717 }
3718 }
3719 }
3595 } 3720 }
3596 } 3721 }
3597} 3722}
3598 3723
3599sub size_request { 3724sub size_request {
3600 my ($self) = @_; 3725 my ($self) = @_;
3601 3726
3602 if ($::CONN) { 3727 if ($::CONN) {
3603 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3728 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3604 if (my $tex = $::CONN->{texture}[$faceid]) { 3729 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3605 $self->{tex} = $tex; 3730 if ($tex->{name}) {
3606 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3731 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3607 } else { 3732 } elsif (!$tex->{loading}) {
3608 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3733 $tex->upload (sub { $self->reconfigure });
3609 $self->realloc;
3610 }); 3734 }
3611 } 3735 }
3736
3737 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3612 } 3738 }
3613 } 3739 }
3614 3740
3615 ($self->{size_w} || 8, $self->{size_h} || 8) 3741 ($self->{size_w} || 8, $self->{size_h} || 8)
3616} 3742}
3831 3957
3832############################################################################# 3958#############################################################################
3833 3959
3834package CFPlus::UI::Notebook; 3960package CFPlus::UI::Notebook;
3835 3961
3962use CFPlus::OpenGL;
3963
3836our @ISA = CFPlus::UI::VBox::; 3964our @ISA = CFPlus::UI::VBox::;
3837 3965
3838sub new { 3966sub new {
3839 my $class = shift; 3967 my $class = shift;
3840 3968
3841 my $self = $class->SUPER::new ( 3969 my $self = $class->SUPER::new (
3842 buttonbar => (new CFPlus::UI::Buttonbar), 3970 buttonbar => (new CFPlus::UI::Buttonbar),
3843 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3971 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3972 active_outline => [1, 1, 0],
3844 # filter => # will be put between multiplexer and $self 3973 # filter => # will be put between multiplexer and $self
3845 @_, 3974 @_,
3846 ); 3975 );
3847 3976
3848 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3977 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3920sub set_current_page { 4049sub set_current_page {
3921 my ($self, $page) = @_; 4050 my ($self, $page) = @_;
3922 4051
3923 $self->{multiplexer}->set_current_page ($page); 4052 $self->{multiplexer}->set_current_page ($page);
3924 $self->emit (page_changed => $self->{multiplexer}{current}); 4053 $self->emit (page_changed => $self->{multiplexer}{current});
4054}
4055
4056sub _draw {
4057 my ($self) = @_;
4058
4059 $self->SUPER::_draw ();
4060
4061 if (my $cur = $self->{multiplexer}{current}) {
4062 if ($cur = $cur->{c_tab_}) {
4063 glTranslate $cur->{x}, $cur->{y};
4064 glLineWidth 3;
4065 glColor @{$self->{active_outline}};
4066 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2;
4067 glLineWidth 1;
4068 }
4069 }
3925} 4070}
3926 4071
3927############################################################################# 4072#############################################################################
3928 4073
3929package CFPlus::UI::Selector; 4074package CFPlus::UI::Selector;
4199} 4344}
4200 4345
4201sub update { 4346sub update {
4202 my ($self) = @_; 4347 my ($self) = @_;
4203 4348
4204 $::WANT_REFRESH++; 4349 $::WANT_REFRESH->start;
4205} 4350}
4206 4351
4207sub add { 4352sub add {
4208 my ($self, @children) = @_; 4353 my ($self, @children) = @_;
4209 4354

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines