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.472 by root, Wed Dec 24 04:09:27 2008 UTC vs.
Revision 1.477 by root, Sun Jan 11 23:50:27 2009 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7
8use Guard ();
7 9
8use DC; 10use DC;
9use DC::Pod; 11use DC::Pod;
10use DC::Texture; 12use DC::Texture;
11 13
189# call when resolution changes etc. 191# call when resolution changes etc.
190sub rescale_widgets { 192sub rescale_widgets {
191 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
192 194
193 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 199
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
530sub connect { 532sub connect {
531 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
532 534
533 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
534 536
535 defined wantarray and DC::guard { 537 defined wantarray and Guard::guard {
536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
537 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
538 } 540 }
539} 541}
540 542
652 $class->SUPER::new ( 654 $class->SUPER::new (
653 #bg => [0, 0, 0, 0.2], 655 #bg => [0, 0, 0, 0.2],
654 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
655 @_ 657 @_
656 ) 658 )
659}
660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
657} 666}
658 667
659sub _draw { 668sub _draw {
660 my ($self) = @_; 669 my ($self) = @_;
661 670
1543 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1544 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1545 1554
1546 my $border = $self->border; 1555 my $border = $self->border;
1547 1556
1557 if ($border) {
1548 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1549 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1550 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1551 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1552 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1553 1563
1554 # move 1564 # move
1555 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1556 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1557 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1558 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1559 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1560 1570
1561 # resize 1571 # resize
1562 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1563 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1564 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1565 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1566 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1567 1578
1568 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1569 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1570 1581
1571 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
2519 2530
2520sub new { 2531sub new {
2521 my $class = shift; 2532 my $class = shift;
2522 2533
2523 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2524 padding_x => 2, 2536 padding_x => 2,
2525 padding_y => 2, 2537 padding_y => 2,
2526 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2527 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2528 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2534} 2546}
2535 2547
2536sub size_request { 2548sub size_request {
2537 my ($self) = @_; 2549 my ($self) = @_;
2538 2550
2539 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2540} 2552}
2541 2553
2542sub toggle { 2554sub toggle {
2543 my ($self) = @_; 2555 my ($self) = @_;
2544 2556
2842 2854
2843sub new { 2855sub new {
2844 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2845 2857
2846 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2847 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2848 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2849 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2850 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2851 fontsize => 0.9, 2865 fontsize => 0.9,
2891 my ($self) = @_; 2905 my ($self) = @_;
2892 2906
2893 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2894 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2895 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2896 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2897 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2898 2915
2899 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2900 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2901 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2902 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2903 } 2920 }
2904 2921
2905 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2906 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2907 2928
2908 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2909 2930
2910 { 2931 {
2911 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2920our @ISA = DC::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2921 2942
2922sub new { 2943sub new {
2923 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2924 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2925 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2926 tooltip => sub { 2950 tooltip => sub {
2927 my ($self) = @_; 2951 my ($self) = @_;
2928 2952
2929 sprintf "level %d\n%s points\n%s next level\n%s to go", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2954 $tt,
2930 $self->{lvl}, 2955 $self->{lvl},
2931 ::formsep ($self->{exp}), 2956 ::formsep ($self->{exp}),
2932 ::formsep ($self->{nxt}), 2957 ::formsep ($self->{nxt}),
2933 ::formsep ($self->{nxt} - $self->{exp}), 2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2934 }, 2960 },
2935 %arg
2936 ); 2961 );
2937 2962
2938 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2939 if $::CONN; 2964 if $::CONN;
2940 2965
2948 if $::CONN; 2973 if $::CONN;
2949 2974
2950 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2951} 2976}
2952 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2953sub set_value { 2992sub set_value {
2954 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2955 2994
2956 $self->{lvl} = $lvl; 2995 $self->{lvl} = $lvl;
2957 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2958 2997
2959 my $v = -1;
2960
2961 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2962 my $l0 = $table->[$lvl - 1];
2963 my $l1 = $table->[$lvl];
2964
2965 $self->{nxt} = $l1;
2966
2967 $v = ($exp - $l0) / ($l1 - $l0);
2968 }
2969
2970 $self->SUPER::set_value ($v); 2998 $self->SUPER::set_value ($self->_percent);
2971} 2999}
2972 3000
2973############################################################################# 3001#############################################################################
2974 3002
2975package DC::UI::Gauge; 3003package DC::UI::Gauge;
3705 aspect => 1, 3733 aspect => 1,
3706 can_events => 0, 3734 can_events => 0,
3707 @_, 3735 @_,
3708 ); 3736 );
3709 3737
3710 if ($self->{anim} && $self->{animspeed}) {
3711 DC::weaken (my $widget = $self);
3712
3713 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3714 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3715 return unless $::CONN;
3716
3717 my $w = $widget
3718 or return;
3719
3720 ++$w->{frame};
3721 $w->update_face;
3722
3723 # somehow, $widget can go away
3724 $w->update;
3725 $w->update_timer;
3726 };
3727
3728 $self->update_face;
3729 $self->update_timer; 3738 $self->update_anim;
3730 }
3731 3739
3732 $self 3740 $self
3733} 3741}
3734 3742
3735sub update_timer { 3743sub update_timer {
3758 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3759 } 3767 }
3760 } 3768 }
3761 } 3769 }
3762 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3763 } 3799 }
3764} 3800}
3765 3801
3766sub size_request { 3802sub size_request {
3767 my ($self) = @_; 3803 my ($self) = @_;
3789 return unless $self->{visible}; 3825 return unless $self->{visible};
3790 3826
3791 $self->SUPER::update; 3827 $self->SUPER::update;
3792} 3828}
3793 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3794sub invoke_visibility_change { 3851sub invoke_visibility_change {
3795 my ($self) = @_; 3852 my ($self) = @_;
3796 3853
3797 $self->update_timer; 3854 $self->update_timer;
3798 3855
3894 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3895 $GRAB = $self; 3952 $GRAB = $self;
3896 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3897 3954
3898 $self->show; 3955 $self->show;
3899 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3900} 3965}
3901 3966
3902sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3903 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3904 3969
4598 4663
4599$ROOT = new DC::UI::Root; 4664$ROOT = new DC::UI::Root;
4600$TOOLTIP = new DC::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4601 4666
46021 46671
4603

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines