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.421 by root, Wed Aug 1 14:27:21 2007 UTC vs.
Revision 1.431 by root, Tue Aug 21 02:41:52 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}
1923 1908
1924 delete $self->{ox}; 1909 delete $self->{ox};
1925 $self->SUPER::realloc; 1910 $self->SUPER::realloc;
1926} 1911}
1927 1912
1913sub clear {
1914 my ($self) = @_;
1915
1916 $self->set_text ("");
1917}
1918
1928sub set_text { 1919sub set_text {
1929 my ($self, $text) = @_; 1920 my ($self, $text) = @_;
1930 1921
1931 return if $self->{text} eq "T$text"; 1922 return if $self->{text} eq "T$text";
1932 $self->{text} = "T$text"; 1923 $self->{text} = "T$text";
2038 : ($self->{w} - $size->[0]) * 0.5); 2029 : ($self->{w} - $size->[0]) * 0.5);
2039 2030
2040 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2031 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2041 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2032 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2042 : ($self->{h} - $size->[1]) * 0.5); 2033 : ($self->{h} - $size->[1]) * 0.5);
2034
2035 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2043 }; 2036 };
2044 2037
2045# unless ($self->{list}) { 2038# unless ($self->{list}) {
2046# $self->{list} = CFPlus::OpenGL::glGenList; 2039# $self->{list} = CFPlus::OpenGL::glGenList;
2047# CFPlus::OpenGL::glNewList $self->{list}; 2040# CFPlus::OpenGL::glNewList $self->{list};
2049# CFPlus::OpenGL::glEndList; 2042# CFPlus::OpenGL::glEndList;
2050# } 2043# }
2051# 2044#
2052# CFPlus::OpenGL::glCallList $self->{list}; 2045# CFPlus::OpenGL::glCallList $self->{list};
2053 2046
2054 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2047 $self->{layout}->draw;
2055} 2048}
2056 2049
2057#sub destroy { 2050#sub destroy {
2058# my ($self) = @_; 2051# my ($self) = @_;
2059# 2052#
2227 glColor_premultiply @{$self->{bg}}; 2220 glColor_premultiply @{$self->{bg}};
2228 } 2221 }
2229 2222
2230 glEnable GL_BLEND; 2223 glEnable GL_BLEND;
2231 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2224 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2232 glBegin GL_QUADS;
2233 glVertex 0 , 0;
2234 glVertex 0 , $self->{h};
2235 glVertex $self->{w}, $self->{h}; 2225 glRect 0, 0, $self->{w}, $self->{h};
2236 glVertex $self->{w}, 0;
2237 glEnd;
2238 glDisable GL_BLEND; 2226 glDisable GL_BLEND;
2239 2227
2240 $self->SUPER::_draw; 2228 $self->SUPER::_draw;
2241 2229
2242 #TODO: force update every cursor change :( 2230 #TODO: force update every cursor change :(
2255 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};
2256 glEnd; 2244 glEnd;
2257 2245
2258 glLineWidth 3; 2246 glLineWidth 3;
2259 glColor @{$self->{active_outline}}; 2247 glColor @{$self->{active_outline}};
2260 glBegin GL_LINE_LOOP;
2261 glVertex 0, 0;
2262 glVertex $self->{w} - 1, 0;
2263 glVertex $self->{w} - 1, $self->{h} - 1; 2248 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1;
2264 glVertex 0, $self->{h} - 1;
2265 glEnd;
2266 glLineWidth 1; 2249 glLineWidth 1;
2267 2250
2268 } else { 2251 } else {
2269 glColor @{$self->{outline}}; 2252 glColor @{$self->{outline}};
2270 glTranslate .375, .375; 2253 glTranslate .375, .375;
2643 align => 0, 2626 align => 0,
2644 valign => 0, 2627 valign => 0,
2645 can_events => 1, 2628 can_events => 1,
2646 @_ 2629 @_
2647 ); 2630 );
2631}
2632
2633sub invoke_button_down {
2634 my ($self, $ev, $x, $y) = @_;
2635
2636 1
2648} 2637}
2649 2638
2650sub invoke_button_up { 2639sub invoke_button_up {
2651 my ($self, $ev, $x, $y) = @_; 2640 my ($self, $ev, $x, $y) = @_;
2652 2641
2792 glDisable GL_TEXTURE_2D; 2781 glDisable GL_TEXTURE_2D;
2793} 2782}
2794 2783
2795############################################################################# 2784#############################################################################
2796 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 %arg,
2806 );
2807
2808 $self->set_value ($arg{value} || -1);
2809
2810 $self
2811}
2812
2813sub set_value {
2814 my ($self, $value) = @_;
2815
2816 if ($self->{value} != $value) {
2817 $self->{value} = $value;
2818
2819 if ($value < 0) {
2820 $self->set_text ("-");
2821 } else {
2822 $self->set_text (sprintf "%d%%", $value * 100);
2823 }
2824 $self->update;
2825 }
2826}
2827
2828sub _draw {
2829 my ($self) = @_;
2830
2831 glEnable GL_BLEND;
2832 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2833
2834 if ($self->{value} >= 0) {
2835 my $s = 3 + ($self->{w} - 6) * $self->{value};
2836
2837 glColor_premultiply @{$self->{bar}};
2838 glRect 2, 2, $s, $self->{h} - 2;
2839 glColor_premultiply @{$self->{bg}};
2840 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2;
2841 }
2842
2843 glColor_premultiply @{$self->{outline}};
2844 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2;
2845
2846 glDisable GL_BLEND;
2847
2848 {
2849 local $self->{bg}; # do not draw background
2850 $self->SUPER::_draw;
2851 }
2852}
2853
2854#############################################################################
2855
2856package CFPlus::UI::ExperienceProgress;
2857
2858our @ISA = CFPlus::UI::Progress::;
2859
2860sub new {
2861 my ($class, %arg) = @_;
2862
2863 my $self = $class->SUPER::new (
2864 %arg
2865 );
2866
2867 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2868 if $::CONN;
2869
2870 $self
2871}
2872
2873sub DESTROY {
2874 my ($self) = @_;
2875
2876 delete $::CONN->{on_exp_update}{$self+0}
2877 if $::CONN;
2878
2879 $self->SUPER::DESTROY;
2880}
2881
2882sub set_value {
2883 my ($self, $lvl, $exp) = @_;
2884
2885 my $v = -1;
2886
2887 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2888 my $l0 = $table->[$lvl - 1];
2889 my $l1 = $table->[$lvl];
2890
2891 $v = ($exp - $l0) / ($l1 - $l0);
2892 }
2893
2894 $self->SUPER::set_value ($v);
2895}
2896
2897#############################################################################
2898
2797package CFPlus::UI::Gauge; 2899package CFPlus::UI::Gauge;
2798 2900
2799our @ISA = CFPlus::UI::VBox::; 2901our @ISA = CFPlus::UI::VBox::;
2800 2902
2801sub new { 2903sub new {
3298 3400
3299 if ($y0 < $y + $h && $y < $y1) { 3401 if ($y0 < $y + $h && $y < $y1) {
3300 my $layout = $self->get_layout ($para); 3402 my $layout = $self->get_layout ($para);
3301 3403
3302 $layout->render ($para->{indent}, $y - $y0); 3404 $layout->render ($para->{indent}, $y - $y0);
3405 $layout->draw;
3303 3406
3304 if (my @w = @{ $para->{widget} }) { 3407 if (my @w = @{ $para->{widget} }) {
3305 my @s = $layout->get_shapes; 3408 my @s = $layout->get_shapes;
3306 3409
3307 for (@w) { 3410 for (@w) {
3501 3604
3502 glColor 1, 0.8, 0.4; 3605 glColor 1, 0.8, 0.4;
3503 glRect 0, 0, $w, $h; 3606 glRect 0, 0, $w, $h;
3504 3607
3505 glColor 0, 0, 0; 3608 glColor 0, 0, 0;
3506 glBegin GL_LINE_LOOP; 3609 glRect_lineloop 0, 0, $w, $h;
3507 glVertex 0 , 0;
3508 glVertex 0 , $h;
3509 glVertex $w, $h;
3510 glVertex $w, 0;
3511 glEnd;
3512 3610
3513 glTranslate 2 - 0.375, 2 - 0.375; 3611 glTranslate 2 - 0.375, 2 - 0.375;
3514 3612
3515 $self->SUPER::_draw; 3613 $self->SUPER::_draw;
3516} 3614}
3540 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3638 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3541 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3639 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3542 $self->{timer} = Event->timer ( 3640 $self->{timer} = Event->timer (
3543 parked => 1, 3641 parked => 1,
3544 cb => sub { 3642 cb => sub {
3545 return unless $::CONN && $widget; 3643 return unless $::CONN;
3546 3644
3645 my $w = $widget
3646 or return;
3647
3547 ++$widget->{frame}; 3648 ++$w->{frame};
3548 $widget->update_face; 3649 $w->update_face;
3650
3651 # somehow, $widget can go away
3549 $widget->update; 3652 $w->update;
3550
3551 $widget->update_timer; 3653 $w->update_timer;
3552 }, 3654 },
3553 ); 3655 );
3554 3656
3555 $self->update_face; 3657 $self->update_face;
3556 $self->update_timer; 3658 $self->update_timer;
3577} 3679}
3578 3680
3579sub update_face { 3681sub update_face {
3580 my ($self) = @_; 3682 my ($self) = @_;
3581 3683
3582 return unless $::CONN; 3684 if ($::CONN) {
3583
3584 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3685 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3585 if ($anim && @$anim) { 3686 if ($anim && @$anim) {
3586 delete $self->{wait_face};
3587 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3687 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3688 delete $self->{face_change_cb};
3689
3588 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3690 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3691 unless ($tex->{name} || $tex->{loading}) {
3692 $tex->upload (sub { $self->reconfigure });
3693 }
3694 }
3695 }
3589 } 3696 }
3590 } 3697 }
3591} 3698}
3592 3699
3593sub size_request { 3700sub size_request {
3594 my ($self) = @_; 3701 my ($self) = @_;
3595 3702
3596 if ($::CONN) { 3703 if ($::CONN) {
3597 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3704 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3598 if (my $tex = $::CONN->{texture}[$faceid]) { 3705 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3599 $self->{tex} = $tex; 3706 if ($tex->{name}) {
3600 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3707 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3601 } else { 3708 } elsif (!$tex->{loading}) {
3602 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3709 $tex->upload (sub { $self->reconfigure });
3603 $self->realloc;
3604 }); 3710 }
3605 } 3711 }
3712
3713 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3606 } 3714 }
3607 } 3715 }
3608 3716
3609 ($self->{size_w} || 8, $self->{size_h} || 8) 3717 ($self->{size_w} || 8, $self->{size_h} || 8)
3610} 3718}
4193} 4301}
4194 4302
4195sub update { 4303sub update {
4196 my ($self) = @_; 4304 my ($self) = @_;
4197 4305
4198 $::WANT_REFRESH++; 4306 $::WANT_REFRESH->start;
4199} 4307}
4200 4308
4201sub add { 4309sub add {
4202 my ($self, @children) = @_; 4310 my ($self, @children) = @_;
4203 4311

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines