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.471 by elmex, Sun Dec 7 21:26:16 2008 UTC vs.
Revision 1.481 by root, Tue Oct 12 05:11:38 2010 UTC

1package DC::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
6
7use Guard ();
7 8
8use DC; 9use DC;
9use DC::Pod; 10use DC::Pod;
10use DC::Texture; 11use DC::Texture;
11 12
189# call when resolution changes etc. 190# call when resolution changes etc.
190sub rescale_widgets { 191sub rescale_widgets {
191 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
192 193
193 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 196 $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.]+$/; 197 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 198
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 199 $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}; 200 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
221 222
222############################################################################# 223#############################################################################
223 224
224package DC::UI::Base; 225package DC::UI::Base;
225 226
226use strict; 227use common::sense;
227 228
228use DC::OpenGL; 229use DC::OpenGL;
229 230
230sub new { 231sub new {
231 my $class = shift; 232 my $class = shift;
286sub set_visible { 287sub set_visible {
287 my ($self) = @_; 288 my ($self) = @_;
288 289
289 return if $self->{visible}; 290 return if $self->{visible};
290 291
292 $self->{parent} && $self->{parent}{root}#d#
293 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
294
291 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
293 297
294 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
295 299
527sub connect { 531sub connect {
528 my ($self, $signal, $cb) = @_; 532 my ($self, $signal, $cb) = @_;
529 533
530 push @{ $self->{signal_cb}{$signal} }, $cb; 534 push @{ $self->{signal_cb}{$signal} }, $cb;
531 535
532 defined wantarray and DC::guard { 536 defined wantarray and Guard::guard {
533 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 537 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
534 @{ $self->{signal_cb}{$signal} }; 538 @{ $self->{signal_cb}{$signal} };
535 } 539 }
536} 540}
537 541
638 642
639package DC::UI::DrawBG; 643package DC::UI::DrawBG;
640 644
641our @ISA = DC::UI::Base::; 645our @ISA = DC::UI::Base::;
642 646
643use strict; 647use common::sense;
648
644use DC::OpenGL; 649use DC::OpenGL;
645 650
646sub new { 651sub new {
647 my $class = shift; 652 my $class = shift;
648 653
651 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
652 @_ 657 @_
653 ) 658 )
654} 659}
655 660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
656sub _draw { 668sub _draw {
657 my ($self) = @_; 669 my ($self) = @_;
658 670
659 my $color = $FOCUS == $self && $self->{active_bg} 671 my $color = $FOCUS == $self
660 ? $self->{active_bg} 672 ? $self->{active_bg} || $self->{bg}
661 : $self->{bg}; 673 : $self->{bg};
662 674
663 if ($color && (@$color < 4 || $color->[3])) { 675 if ($color && (@$color < 4 || $color->[3])) {
664 my ($w, $h) = @$self{qw(w h)}; 676 my ($w, $h) = @$self{qw(w h)};
665 677
1540 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1541 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1542 1554
1543 my $border = $self->border; 1555 my $border = $self->border;
1544 1556
1557 if ($border) {
1545 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1546 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1547 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1548 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1549 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1550 1563
1551 # move 1564 # move
1552 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1553 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1554 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1555 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1556 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1557 1570
1558 # resize 1571 # resize
1559 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1560 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1561 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1562 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1563 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1564 1578
1565 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1566 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1567 1581
1568 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
2516 2530
2517sub new { 2531sub new {
2518 my $class = shift; 2532 my $class = shift;
2519 2533
2520 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2521 padding_x => 2, 2536 padding_x => 2,
2522 padding_y => 2, 2537 padding_y => 2,
2523 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2524 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2525 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2531} 2546}
2532 2547
2533sub size_request { 2548sub size_request {
2534 my ($self) = @_; 2549 my ($self) = @_;
2535 2550
2536 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2537} 2552}
2538 2553
2539sub toggle { 2554sub toggle {
2540 my ($self) = @_; 2555 my ($self) = @_;
2541 2556
2579 2594
2580############################################################################# 2595#############################################################################
2581 2596
2582package DC::UI::Image; 2597package DC::UI::Image;
2583 2598
2584our @ISA = DC::UI::Base::; 2599our @ISA = DC::UI::DrawBG::;
2585 2600
2586use DC::OpenGL; 2601use DC::OpenGL;
2587 2602
2588our %texture_cache; 2603our %texture_cache;
2589 2604
2622 my ($self, $cloning, $path) = @_; 2637 my ($self, $cloning, $path) = @_;
2623 2638
2624 $self->new (path => $path) 2639 $self->new (path => $path)
2625} 2640}
2626 2641
2642sub set_texture {
2643 my ($self, $tex) = @_;
2644
2645 $self->{tex} = $tex;
2646 $self->update;
2647}
2648
2627sub size_request { 2649sub size_request {
2628 my ($self) = @_; 2650 my ($self) = @_;
2629 2651
2630 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2652 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2631} 2653}
2632 2654
2633sub _draw { 2655sub _draw {
2634 my ($self) = @_; 2656 my ($self) = @_;
2657
2658 $self->SUPER::_draw;
2635 2659
2636 my $tex = $self->{tex}; 2660 my $tex = $self->{tex};
2637 2661
2638 my ($w, $h) = ($self->{w}, $self->{h}); 2662 my ($w, $h) = ($self->{w}, $self->{h});
2639 2663
2657package DC::UI::ImageButton; 2681package DC::UI::ImageButton;
2658 2682
2659our @ISA = DC::UI::Image::; 2683our @ISA = DC::UI::Image::;
2660 2684
2661use DC::OpenGL; 2685use DC::OpenGL;
2662
2663my %textures;
2664 2686
2665sub new { 2687sub new {
2666 my $class = shift; 2688 my $class = shift;
2667 2689
2668 my $self = $class->SUPER::new ( 2690 my $self = $class->SUPER::new (
2839 2861
2840sub new { 2862sub new {
2841 my ($class, %arg) = @_; 2863 my ($class, %arg) = @_;
2842 2864
2843 my $self = $class->SUPER::new ( 2865 my $self = $class->SUPER::new (
2866 padding_x => 2,
2867 padding_y => 2,
2844 fg => [1, 1, 1], 2868 fg => [1, 1, 1],
2845 bg => [0, 0, 1, 0.2], 2869 bg => [0, 0, 1, 0.2],
2846 bar => [0.7, 0.5, 0.1, 0.8], 2870 bar => [0.7, 0.5, 0.1, 0.8],
2847 outline => [0.4, 0.3, 0], 2871 outline => [0.4, 0.3, 0],
2848 fontsize => 0.9, 2872 fontsize => 0.9,
2888 my ($self) = @_; 2912 my ($self) = @_;
2889 2913
2890 glEnable GL_BLEND; 2914 glEnable GL_BLEND;
2891 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2915 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2892 2916
2917 my $px = $self->{padding_x};
2918 my $py = $self->{padding_y};
2919
2893 if ($self->{value} >= 0) { 2920 if ($self->{value} >= 0) {
2894 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2921 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2895 2922
2896 glColor_premultiply @{$self->{bar}}; 2923 glColor_premultiply @{$self->{bar}};
2897 glRect 2, 2, $s, $self->{h} - 2; 2924 glRect $px, $py, $s, $self->{h} - $py;
2898 glColor_premultiply @{$self->{bg}}; 2925 glColor_premultiply @{$self->{bg}};
2899 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2926 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2900 } 2927 }
2901 2928
2902 glColor_premultiply @{$self->{outline}}; 2929 glColor_premultiply @{$self->{outline}};
2930
2931 $px -= .5;
2932 $py -= .5;
2933
2903 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2934 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2904 2935
2905 glDisable GL_BLEND; 2936 glDisable GL_BLEND;
2906 2937
2907 { 2938 {
2908 local $self->{bg}; # do not draw background 2939 local $self->{bg}; # do not draw background
2917our @ISA = DC::UI::Progress::; 2948our @ISA = DC::UI::Progress::;
2918 2949
2919sub new { 2950sub new {
2920 my ($class, %arg) = @_; 2951 my ($class, %arg) = @_;
2921 2952
2953 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2954
2922 my $self = $class->SUPER::new ( 2955 my $self = $class->SUPER::new (
2956 %arg,
2923 tooltip => sub { 2957 tooltip => sub {
2924 my ($self) = @_; 2958 my ($self) = @_;
2925 2959
2926 sprintf "level %d\n%s points\n%s next level\n%s to go", 2960 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2961 $tt,
2927 $self->{lvl}, 2962 $self->{lvl},
2928 ::formsep ($self->{exp}), 2963 ::formsep ($self->{exp}),
2929 ::formsep ($self->{nxt}), 2964 ::formsep ($self->{nxt}),
2930 ::formsep ($self->{nxt} - $self->{exp}), 2965 ::formsep ($self->{nxt} - $self->{exp}),
2966 $self->_percent * 100,
2931 }, 2967 },
2932 %arg
2933 ); 2968 );
2934 2969
2935 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2970 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2936 if $::CONN; 2971 if $::CONN;
2937 2972
2945 if $::CONN; 2980 if $::CONN;
2946 2981
2947 $self->SUPER::DESTROY; 2982 $self->SUPER::DESTROY;
2948} 2983}
2949 2984
2985sub _percent {
2986 my ($self) = @_;
2987
2988 my $table = $::CONN && $::CONN->{exp_table}
2989 or return -1;
2990
2991 my $l0 = $table->[$self->{lvl} - 1];
2992 my $l1 = $table->[$self->{lvl}];
2993
2994 $self->{nxt} = $l1;
2995
2996 ($self->{exp} - $l0) / ($l1 - $l0)
2997}
2998
2950sub set_value { 2999sub set_value {
2951 my ($self, $lvl, $exp) = @_; 3000 my ($self, $lvl, $exp) = @_;
2952 3001
2953 $self->{lvl} = $lvl; 3002 $self->{lvl} = $lvl;
2954 $self->{exp} = $exp; 3003 $self->{exp} = $exp;
2955 3004
2956 my $v = -1;
2957
2958 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2959 my $l0 = $table->[$lvl - 1];
2960 my $l1 = $table->[$lvl];
2961
2962 $self->{nxt} = $l1;
2963
2964 $v = ($exp - $l0) / ($l1 - $l0);
2965 }
2966
2967 $self->SUPER::set_value ($v); 3005 $self->SUPER::set_value ($self->_percent);
2968} 3006}
2969 3007
2970############################################################################# 3008#############################################################################
2971 3009
2972package DC::UI::Gauge; 3010package DC::UI::Gauge;
3016 3054
3017############################################################################# 3055#############################################################################
3018 3056
3019package DC::UI::Slider; 3057package DC::UI::Slider;
3020 3058
3021use strict; 3059use common::sense;
3022 3060
3023use DC::OpenGL; 3061use DC::OpenGL;
3024 3062
3025our @ISA = DC::UI::DrawBG::; 3063our @ISA = DC::UI::DrawBG::;
3026 3064
3112 if ($GRAB == $self) { 3150 if ($GRAB == $self) {
3113 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3151 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3114 3152
3115 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3153 my (undef, $lo, $hi, $page) = @{$self->{range}};
3116 3154
3117 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3155 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3118 3156
3119 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3157 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3120 } else { 3158 } else {
3121 return 0; 3159 return 0;
3122 } 3160 }
3702 aspect => 1, 3740 aspect => 1,
3703 can_events => 0, 3741 can_events => 0,
3704 @_, 3742 @_,
3705 ); 3743 );
3706 3744
3707 if ($self->{anim} && $self->{animspeed}) {
3708 DC::weaken (my $widget = $self);
3709
3710 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3711 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3712 return unless $::CONN;
3713
3714 my $w = $widget
3715 or return;
3716
3717 ++$w->{frame};
3718 $w->update_face;
3719
3720 # somehow, $widget can go away
3721 $w->update;
3722 $w->update_timer;
3723 };
3724
3725 $self->update_face;
3726 $self->update_timer; 3745 $self->update_anim;
3727 }
3728 3746
3729 $self 3747 $self
3730} 3748}
3731 3749
3732sub update_timer { 3750sub update_timer {
3755 $tex->upload (sub { $self->reconfigure }); 3773 $tex->upload (sub { $self->reconfigure });
3756 } 3774 }
3757 } 3775 }
3758 } 3776 }
3759 } 3777 }
3778 }
3779}
3780
3781sub update_anim {
3782 my ($self) = @_;
3783
3784 if ($self->{anim} && $self->{animspeed}) {
3785 DC::weaken (my $widget = $self);
3786
3787 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3788 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3789 return unless $::CONN;
3790
3791 my $w = $widget
3792 or return;
3793
3794 ++$w->{frame};
3795 $w->update_face;
3796
3797 # somehow, $widget can go away
3798 $w->update;
3799 $w->update_timer;
3800 };
3801
3802 $self->update_face;
3803 $self->update_timer;
3804 } else {
3805 delete $self->{timer};
3760 } 3806 }
3761} 3807}
3762 3808
3763sub size_request { 3809sub size_request {
3764 my ($self) = @_; 3810 my ($self) = @_;
3786 return unless $self->{visible}; 3832 return unless $self->{visible};
3787 3833
3788 $self->SUPER::update; 3834 $self->SUPER::update;
3789} 3835}
3790 3836
3837sub set_face {
3838 my ($self, $face) = @_;
3839
3840 $self->{face} = $face;
3841 $self->reconfigure;
3842}
3843
3844sub set_anim {
3845 my ($self, $anim) = @_;
3846
3847 $self->{anim} = $anim;
3848 $self->update_anim;
3849}
3850
3851sub set_animspeed {
3852 my ($self, $animspeed) = @_;
3853
3854 $self->{animspeed} = $animspeed;
3855 $self->update_anim;
3856}
3857
3791sub invoke_visibility_change { 3858sub invoke_visibility_change {
3792 my ($self) = @_; 3859 my ($self) = @_;
3793 3860
3794 $self->update_timer; 3861 $self->update_timer;
3795 3862
3891 # maybe save $GRAB? must be careful about events... 3958 # maybe save $GRAB? must be careful about events...
3892 $GRAB = $self; 3959 $GRAB = $self;
3893 $self->{button} = $ev->{button}; 3960 $self->{button} = $ev->{button};
3894 3961
3895 $self->show; 3962 $self->show;
3896 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3963
3964 my $x = $ev->{x};
3965 my $y = $ev->{y};
3966
3967 $self->{root}->on_post_alloc ($self => sub {
3968 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3969 });
3970
3971 1 # so it can be used inside event handlers
3897} 3972}
3898 3973
3899sub invoke_mouse_motion { 3974sub invoke_mouse_motion {
3900 my ($self, $ev, $x, $y) = @_; 3975 my ($self, $ev, $x, $y) = @_;
3901 3976
4595 4670
4596$ROOT = new DC::UI::Root; 4671$ROOT = new DC::UI::Root;
4597$TOOLTIP = new DC::UI::Tooltip z => 900; 4672$TOOLTIP = new DC::UI::Tooltip z => 900;
4598 4673
45991 46741
4600

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines