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.468 by root, Wed Sep 3 10:36:25 2008 UTC vs.
Revision 1.474 by root, Sun Jan 11 22:11:23 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};
286sub set_visible { 288sub set_visible {
287 my ($self) = @_; 289 my ($self) = @_;
288 290
289 return if $self->{visible}; 291 return if $self->{visible};
290 292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
295
291 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
293 298
294 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
295 300
296 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
297 302
298 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
299} 304}
300 305
301sub set_invisible { 306sub set_invisible {
302 my ($self) = @_; 307 my ($self) = @_;
303 308
527sub connect { 532sub connect {
528 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
529 534
530 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
531 536
532 defined wantarray and DC::guard { 537 defined wantarray and Guard::guard {
533 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
534 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
535 } 540 }
536} 541}
537 542
1361 my ($class, %arg) = @_; 1366 my ($class, %arg) = @_;
1362 1367
1363 my $self = $class->SUPER::new ( 1368 my $self = $class->SUPER::new (
1364 bg => [1, 1, 1, 1], 1369 bg => [1, 1, 1, 1],
1365 border_bg => [1, 1, 1, 1], 1370 border_bg => [1, 1, 1, 1],
1366 border => 1, 1371 border => 0.8,
1367 can_events => 1, 1372 can_events => 1,
1368 min_w => 64, 1373 min_w => 64,
1369 min_h => 32, 1374 min_h => 32,
1370 %arg, 1375 %arg,
1371 ); 1376 );
1540 glEnable GL_TEXTURE_2D; 1545 glEnable GL_TEXTURE_2D;
1541 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1546 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1542 1547
1543 my $border = $self->border; 1548 my $border = $self->border;
1544 1549
1550 if ($border) {
1545 glColor @{ $self->{border_bg} }; 1551 glColor @{ $self->{border_bg} };
1546 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1552 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1547 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1553 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1548 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1554 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1549 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1555 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1550 1556
1551 # move 1557 # move
1552 my $w2 = ($w - $border) * .5; 1558 my $w2 = ($w - $border) * .5;
1553 my $h2 = ($h - $border) * .5; 1559 my $h2 = ($h - $border) * .5;
1554 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1560 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1555 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1561 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1556 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1562 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1557 1563
1558 # resize 1564 # resize
1559 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1565 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1560 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1566 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1561 unless $self->{has_close_button}; 1567 unless $self->{has_close_button};
1562 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1568 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1563 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1569 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1570 }
1564 1571
1565 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1572 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1566 glColor @{ $self->{bg} }; 1573 glColor @{ $self->{bg} };
1567 1574
1568 # TODO: repeat texture not scale 1575 # TODO: repeat texture not scale
2516 2523
2517sub new { 2524sub new {
2518 my $class = shift; 2525 my $class = shift;
2519 2526
2520 $class->SUPER::new ( 2527 $class->SUPER::new (
2528 fontsize => 1,
2521 padding_x => 2, 2529 padding_x => 2,
2522 padding_y => 2, 2530 padding_y => 2,
2523 fg => [1, 1, 1], 2531 fg => [1, 1, 1],
2524 active_fg => [1, 1, 0], 2532 active_fg => [1, 1, 0],
2525 bg => [0, 0, 0, 0.2], 2533 bg => [0, 0, 0, 0.2],
2531} 2539}
2532 2540
2533sub size_request { 2541sub size_request {
2534 my ($self) = @_; 2542 my ($self) = @_;
2535 2543
2536 (6) x 2 2544 ($self->{fontsize} * $::FONTSIZE) x 2
2537} 2545}
2538 2546
2539sub toggle { 2547sub toggle {
2540 my ($self) = @_; 2548 my ($self) = @_;
2541 2549
2839 2847
2840sub new { 2848sub new {
2841 my ($class, %arg) = @_; 2849 my ($class, %arg) = @_;
2842 2850
2843 my $self = $class->SUPER::new ( 2851 my $self = $class->SUPER::new (
2852 padding_x => 2,
2853 padding_y => 2,
2844 fg => [1, 1, 1], 2854 fg => [1, 1, 1],
2845 bg => [0, 0, 1, 0.2], 2855 bg => [0, 0, 1, 0.2],
2846 bar => [0.7, 0.5, 0.1, 0.8], 2856 bar => [0.7, 0.5, 0.1, 0.8],
2847 outline => [0.4, 0.3, 0], 2857 outline => [0.4, 0.3, 0],
2848 fontsize => 0.9, 2858 fontsize => 0.9,
2888 my ($self) = @_; 2898 my ($self) = @_;
2889 2899
2890 glEnable GL_BLEND; 2900 glEnable GL_BLEND;
2891 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2901 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2892 2902
2903 my $px = $self->{padding_x};
2904 my $py = $self->{padding_y};
2905
2893 if ($self->{value} >= 0) { 2906 if ($self->{value} >= 0) {
2894 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2907 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2895 2908
2896 glColor_premultiply @{$self->{bar}}; 2909 glColor_premultiply @{$self->{bar}};
2897 glRect 2, 2, $s, $self->{h} - 2; 2910 glRect $px, $py, $s, $self->{h} - $py;
2898 glColor_premultiply @{$self->{bg}}; 2911 glColor_premultiply @{$self->{bg}};
2899 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2912 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2900 } 2913 }
2901 2914
2902 glColor_premultiply @{$self->{outline}}; 2915 glColor_premultiply @{$self->{outline}};
2916
2917 $px -= .5;
2918 $py -= .5;
2919
2903 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2920 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2904 2921
2905 glDisable GL_BLEND; 2922 glDisable GL_BLEND;
2906 2923
2907 { 2924 {
2908 local $self->{bg}; # do not draw background 2925 local $self->{bg}; # do not draw background
2917our @ISA = DC::UI::Progress::; 2934our @ISA = DC::UI::Progress::;
2918 2935
2919sub new { 2936sub new {
2920 my ($class, %arg) = @_; 2937 my ($class, %arg) = @_;
2921 2938
2939 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2940
2922 my $self = $class->SUPER::new ( 2941 my $self = $class->SUPER::new (
2942 %arg,
2923 tooltip => sub { 2943 tooltip => sub {
2924 my ($self) = @_; 2944 my ($self) = @_;
2925 2945
2926 sprintf "level %d\n%s points\n%s next level\n%s to go", 2946 sprintf "%slevel %d\n%s points\n%s next level\n%s to go / %d%%",
2947 $tt,
2927 $self->{lvl}, 2948 $self->{lvl},
2928 ::formsep ($self->{exp}), 2949 ::formsep ($self->{exp}),
2929 ::formsep ($self->{nxt}), 2950 ::formsep ($self->{nxt}),
2930 ::formsep ($self->{nxt} - $self->{exp}), 2951 ::formsep ($self->{nxt} - $self->{exp}),
2952 $self->_percent * 100,
2931 }, 2953 },
2932 %arg
2933 ); 2954 );
2934 2955
2935 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2956 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2936 if $::CONN; 2957 if $::CONN;
2937 2958
2945 if $::CONN; 2966 if $::CONN;
2946 2967
2947 $self->SUPER::DESTROY; 2968 $self->SUPER::DESTROY;
2948} 2969}
2949 2970
2971sub _percent {
2972 my ($self) = @_;
2973
2974 my $table = $::CONN && $::CONN->{exp_table}
2975 or return -1;
2976
2977 my $l0 = $table->[$self->{lvl} - 1];
2978 my $l1 = $table->[$self->{lvl}];
2979
2980 $self->{nxt} = $l1;
2981
2982 ($self->{exp} - $l0) / ($l1 - $l0)
2983}
2984
2950sub set_value { 2985sub set_value {
2951 my ($self, $lvl, $exp) = @_; 2986 my ($self, $lvl, $exp) = @_;
2952 2987
2953 $self->{lvl} = $lvl; 2988 $self->{lvl} = $lvl;
2954 $self->{exp} = $exp; 2989 $self->{exp} = $exp;
2955 2990
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); 2991 $self->SUPER::set_value ($self->_percent);
2968} 2992}
2969 2993
2970############################################################################# 2994#############################################################################
2971 2995
2972package DC::UI::Gauge; 2996package DC::UI::Gauge;
3891 # maybe save $GRAB? must be careful about events... 3915 # maybe save $GRAB? must be careful about events...
3892 $GRAB = $self; 3916 $GRAB = $self;
3893 $self->{button} = $ev->{button}; 3917 $self->{button} = $ev->{button};
3894 3918
3895 $self->show; 3919 $self->show;
3896 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3920
3921 my $x = $ev->{x};
3922 my $y = $ev->{y};
3923
3924 $self->{root}->on_post_alloc ($self => sub {
3925 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3926 });
3927
3928 1 # so it can be used inside event handlers
3897} 3929}
3898 3930
3899sub invoke_mouse_motion { 3931sub invoke_mouse_motion {
3900 my ($self, $ev, $x, $y) = @_; 3932 my ($self, $ev, $x, $y) = @_;
3901 3933
3933 3965
3934 my $self = $class->SUPER::new ( 3966 my $self = $class->SUPER::new (
3935 @_, 3967 @_,
3936 ); 3968 );
3937 3969
3938 $self->{current} = $self->{children}[0] 3970 $self->set_current_page (0);
3939 if @{ $self->{children} };
3940 3971
3941 $self 3972 $self
3942} 3973}
3943 3974
3944sub add { 3975sub add {
3945 my ($self, @widgets) = @_; 3976 my ($self, @widgets) = @_;
3946 3977
3947 $self->SUPER::add (@widgets); 3978 $self->SUPER::add (@widgets);
3948 3979
3949 $self->{current} = $self->{children}[0] 3980 $self->set_current_page (0)
3950 if @{ $self->{children} }; 3981 if @widgets == @{ $self->{children} };
3951} 3982}
3952 3983
3953sub get_current_page { 3984sub get_current_page {
3954 my ($self) = @_; 3985 my ($self) = @_;
3955 3986
3961 3992
3962 my $widget = ref $page_or_widget 3993 my $widget = ref $page_or_widget
3963 ? $page_or_widget 3994 ? $page_or_widget
3964 : $self->{children}[$page_or_widget]; 3995 : $self->{children}[$page_or_widget];
3965 3996
3997 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3998
3966 $self->{current} = $widget; 3999 if (($self->{current} = $widget)) {
4000 $self->{current}->set_visible if $self->{current} && $self->{visible};
3967 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4001 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3968 4002
3969 $self->emit (page_changed => $self->{current}); 4003 $self->emit (page_changed => $self->{current});
4004 }
3970 4005
3971 $self->realloc; 4006 $self->realloc;
3972} 4007}
3973 4008
3974sub visible_children { 4009sub visible_children {
3975 $_[0]{current} 4010 $_[0]{current} || ()
3976} 4011}
3977 4012
3978sub size_request { 4013sub size_request {
3979 my ($self) = @_; 4014 my ($self) = @_;
3980 4015
4016 $self->{current}
3981 $self->{current}->size_request 4017 ? $self->{current}->size_request
4018 : (0, 0)
3982} 4019}
3983 4020
3984sub invoke_size_allocate { 4021sub invoke_size_allocate {
3985 my ($self, $w, $h) = @_; 4022 my ($self, $w, $h) = @_;
3986 4023
3987 $self->{current}->configure (0, 0, $w, $h); 4024 $self->{current}->configure (0, 0, $w, $h)
4025 if $self->{current};
3988 4026
3989 1 4027 1
3990} 4028}
3991 4029
3992sub _draw { 4030sub _draw {
3993 my ($self) = @_; 4031 my ($self) = @_;
3994 4032
3995 $self->{current}->draw; 4033 $self->{current}->draw
4034 if $self->{current};
3996} 4035}
3997 4036
3998############################################################################# 4037#############################################################################
3999 4038
4000package DC::UI::Notebook; 4039package DC::UI::Notebook;
4210 $self 4249 $self
4211} 4250}
4212 4251
4213sub reorder { 4252sub reorder {
4214 my ($self) = @_; 4253 my ($self) = @_;
4215 my $NOW = Time::HiRes::time; 4254 my $NOW = EV::time;
4216 4255
4217 # freeze display when hovering over any label 4256 # freeze display when hovering over any label
4218 return if $DC::UI::TOOLTIP->{owner} 4257 return if $DC::UI::TOOLTIP->{owner}
4219 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4258 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4220 values %{ $self->{item} }; 4259 values %{ $self->{item} };
4270 $label->{fg}[3] = $item->{fg}[3] || 1; 4309 $label->{fg}[3] = $item->{fg}[3] || 1;
4271 } 4310 }
4272 4311
4273 push @widgets, $label; 4312 push @widgets, $label;
4274 } 4313 }
4314
4315 my $hash = join ",", @widgets;
4316 return if $hash eq $self->{last_widget_hash};
4317 $self->{last_widget_hash} = $hash;
4275 4318
4276 $self->clear; 4319 $self->clear;
4277 $self->SUPER::add (reverse @widgets); 4320 $self->SUPER::add (reverse @widgets);
4278} 4321}
4279 4322
4584 4627
4585$ROOT = new DC::UI::Root; 4628$ROOT = new DC::UI::Root;
4586$TOOLTIP = new DC::UI::Tooltip z => 900; 4629$TOOLTIP = new DC::UI::Tooltip z => 900;
4587 4630
45881 46311
4589

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines