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.467 by root, Wed Sep 3 06:07:39 2008 UTC vs.
Revision 1.483 by root, Wed Jan 4 11:23:23 2012 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 AnyEvent ();
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};
221 223
222############################################################################# 224#############################################################################
223 225
224package DC::UI::Base; 226package DC::UI::Base;
225 227
226use strict; 228use common::sense;
227 229
228use DC::OpenGL; 230use DC::OpenGL;
229 231
230sub new { 232sub new {
231 my $class = shift; 233 my $class = shift;
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
638 643
639package DC::UI::DrawBG; 644package DC::UI::DrawBG;
640 645
641our @ISA = DC::UI::Base::; 646our @ISA = DC::UI::Base::;
642 647
643use strict; 648use common::sense;
649
644use DC::OpenGL; 650use DC::OpenGL;
645 651
646sub new { 652sub new {
647 my $class = shift; 653 my $class = shift;
648 654
651 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
652 @_ 658 @_
653 ) 659 )
654} 660}
655 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
656sub _draw { 669sub _draw {
657 my ($self) = @_; 670 my ($self) = @_;
658 671
659 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
660 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
661 : $self->{bg}; 674 : $self->{bg};
662 675
663 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
664 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
665 678
1256 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1257 } 1270 }
1258 1271
1259 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1260 # label => "", 1273 # label => "",
1261 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1262 border => 0.8, 1275 border => 0.8,
1263 style => 'single', 1276 style => 'single',
1264 %arg, 1277 %arg,
1265 ); 1278 );
1266 1279
1319 my $border = $self->border; 1332 my $border = $self->border;
1320 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1321 1334
1322 $child->draw; 1335 $child->draw;
1323 1336
1324 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1325 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1326 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1327 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1328 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1329 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1342 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1361 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1362 1375
1363 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1364 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1365 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1366 border => 1, 1379 border => 0.8,
1367 can_events => 1, 1380 can_events => 1,
1368 min_w => 64, 1381 min_w => 64,
1369 min_h => 32, 1382 min_h => 32,
1370 %arg, 1383 %arg,
1371 ); 1384 );
1540 glEnable GL_TEXTURE_2D; 1553 glEnable GL_TEXTURE_2D;
1541 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1554 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1542 1555
1543 my $border = $self->border; 1556 my $border = $self->border;
1544 1557
1558 if ($border) {
1545 glColor @{ $self->{border_bg} }; 1559 glColor @{ $self->{border_bg} };
1546 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1560 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1547 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1561 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1548 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1562 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1549 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1563 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1550 1564
1551 # move 1565 # move
1552 my $w2 = ($w - $border) * .5; 1566 my $w2 = ($w - $border) * .5;
1553 my $h2 = ($h - $border) * .5; 1567 my $h2 = ($h - $border) * .5;
1554 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1555 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1556 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1570 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1557 1571
1558 # resize 1572 # resize
1559 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1573 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1560 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1574 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1561 unless $self->{has_close_button}; 1575 unless $self->{has_close_button};
1562 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1563 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1577 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1578 }
1564 1579
1565 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1580 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1566 glColor @{ $self->{bg} }; 1581 glColor @{ $self->{bg} };
1567 1582
1568 # TODO: repeat texture not scale 1583 # TODO: repeat texture not scale
2090 my $class = shift; 2105 my $class = shift;
2091 2106
2092 $class->SUPER::new ( 2107 $class->SUPER::new (
2093 fg => [1, 1, 1], 2108 fg => [1, 1, 1],
2094 bg => [0, 0, 0, 0.2], 2109 bg => [0, 0, 0, 0.2],
2095 outline => [0.6, 0.3, 0.1], 2110 outline => undef,
2096 active_bg => [0, 0, 1, .2], 2111 active_bg => [0, 0, 1, .2],
2097 active_fg => [1, 1, 1], 2112 active_fg => [1, 1, 1],
2098 active_outline => [1, 1, 0], 2113 active_outline => [1, 1, 0],
2099 can_hover => 1, 2114 can_hover => 1,
2100 can_focus => 1, 2115 can_focus => 1,
2273 glColor @{$self->{active_outline}}; 2288 glColor @{$self->{active_outline}};
2274 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2289 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2275 glLineWidth 1; 2290 glLineWidth 1;
2276 2291
2277 } else { 2292 } else {
2278 glColor @{$self->{outline}}; 2293 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2279 glBegin GL_LINE_STRIP; 2294 glBegin GL_LINE_STRIP;
2280 glVertex .5, $self->{h} * .5; 2295 glVertex .5, $self->{h} * .5;
2281 glVertex .5, $self->{h} - 2.5; 2296 glVertex .5, $self->{h} - 2.5;
2282 glVertex $self->{w} - .5, $self->{h} - 2.5; 2297 glVertex $self->{w} - .5, $self->{h} - 2.5;
2283 glVertex $self->{w} - .5, $self->{h} * .5; 2298 glVertex $self->{w} - .5, $self->{h} * .5;
2516 2531
2517sub new { 2532sub new {
2518 my $class = shift; 2533 my $class = shift;
2519 2534
2520 $class->SUPER::new ( 2535 $class->SUPER::new (
2536 fontsize => 1,
2521 padding_x => 2, 2537 padding_x => 2,
2522 padding_y => 2, 2538 padding_y => 2,
2523 fg => [1, 1, 1], 2539 fg => [1, 1, 1],
2524 active_fg => [1, 1, 0], 2540 active_fg => [1, 1, 0],
2525 bg => [0, 0, 0, 0.2], 2541 bg => [0, 0, 0, 0.2],
2531} 2547}
2532 2548
2533sub size_request { 2549sub size_request {
2534 my ($self) = @_; 2550 my ($self) = @_;
2535 2551
2536 (6) x 2 2552 ($self->{fontsize} * $::FONTSIZE) x 2
2537} 2553}
2538 2554
2539sub toggle { 2555sub toggle {
2540 my ($self) = @_; 2556 my ($self) = @_;
2541 2557
2579 2595
2580############################################################################# 2596#############################################################################
2581 2597
2582package DC::UI::Image; 2598package DC::UI::Image;
2583 2599
2584our @ISA = DC::UI::Base::; 2600our @ISA = DC::UI::DrawBG::;
2585 2601
2586use DC::OpenGL; 2602use DC::OpenGL;
2587 2603
2588our %texture_cache; 2604our %texture_cache;
2589 2605
2622 my ($self, $cloning, $path) = @_; 2638 my ($self, $cloning, $path) = @_;
2623 2639
2624 $self->new (path => $path) 2640 $self->new (path => $path)
2625} 2641}
2626 2642
2643sub set_texture {
2644 my ($self, $tex) = @_;
2645
2646 $self->{tex} = $tex;
2647 $self->update;
2648}
2649
2627sub size_request { 2650sub size_request {
2628 my ($self) = @_; 2651 my ($self) = @_;
2629 2652
2630 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2653 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2631} 2654}
2632 2655
2633sub _draw { 2656sub _draw {
2634 my ($self) = @_; 2657 my ($self) = @_;
2658
2659 $self->SUPER::_draw;
2635 2660
2636 my $tex = $self->{tex}; 2661 my $tex = $self->{tex};
2637 2662
2638 my ($w, $h) = ($self->{w}, $self->{h}); 2663 my ($w, $h) = ($self->{w}, $self->{h});
2639 2664
2657package DC::UI::ImageButton; 2682package DC::UI::ImageButton;
2658 2683
2659our @ISA = DC::UI::Image::; 2684our @ISA = DC::UI::Image::;
2660 2685
2661use DC::OpenGL; 2686use DC::OpenGL;
2662
2663my %textures;
2664 2687
2665sub new { 2688sub new {
2666 my $class = shift; 2689 my $class = shift;
2667 2690
2668 my $self = $class->SUPER::new ( 2691 my $self = $class->SUPER::new (
2839 2862
2840sub new { 2863sub new {
2841 my ($class, %arg) = @_; 2864 my ($class, %arg) = @_;
2842 2865
2843 my $self = $class->SUPER::new ( 2866 my $self = $class->SUPER::new (
2867 padding_x => 2,
2868 padding_y => 2,
2844 fg => [1, 1, 1], 2869 fg => [1, 1, 1],
2845 bg => [0, 0, 1, 0.2], 2870 bg => [0, 0, 1, 0.2],
2846 bar => [0.7, 0.5, 0.1, 0.8], 2871 bar => [0.7, 0.5, 0.1, 0.8],
2847 outline => [0.4, 0.3, 0], 2872 outline => [0.4, 0.3, 0],
2848 fontsize => 0.9, 2873 fontsize => 0.9,
2888 my ($self) = @_; 2913 my ($self) = @_;
2889 2914
2890 glEnable GL_BLEND; 2915 glEnable GL_BLEND;
2891 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2916 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2892 2917
2918 my $px = $self->{padding_x};
2919 my $py = $self->{padding_y};
2920
2893 if ($self->{value} >= 0) { 2921 if ($self->{value} >= 0) {
2894 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2922 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2895 2923
2896 glColor_premultiply @{$self->{bar}}; 2924 glColor_premultiply @{$self->{bar}};
2897 glRect 2, 2, $s, $self->{h} - 2; 2925 glRect $px, $py, $s, $self->{h} - $py;
2898 glColor_premultiply @{$self->{bg}}; 2926 glColor_premultiply @{$self->{bg}};
2899 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2927 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2900 } 2928 }
2901 2929
2902 glColor_premultiply @{$self->{outline}}; 2930 glColor_premultiply @{$self->{outline}};
2931
2932 $px -= .5;
2933 $py -= .5;
2934
2903 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2935 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2904 2936
2905 glDisable GL_BLEND; 2937 glDisable GL_BLEND;
2906 2938
2907 { 2939 {
2908 local $self->{bg}; # do not draw background 2940 local $self->{bg}; # do not draw background
2917our @ISA = DC::UI::Progress::; 2949our @ISA = DC::UI::Progress::;
2918 2950
2919sub new { 2951sub new {
2920 my ($class, %arg) = @_; 2952 my ($class, %arg) = @_;
2921 2953
2954 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2955
2922 my $self = $class->SUPER::new ( 2956 my $self = $class->SUPER::new (
2957 %arg,
2923 tooltip => sub { 2958 tooltip => sub {
2924 my ($self) = @_; 2959 my ($self) = @_;
2925 2960
2926 sprintf "level %d\n%s points\n%s next level\n%s to go", 2961 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2962 $tt,
2927 $self->{lvl}, 2963 $self->{lvl},
2928 ::formsep ($self->{exp}), 2964 ::formsep ($self->{exp}),
2929 ::formsep ($self->{nxt}), 2965 ::formsep ($self->{nxt}),
2930 ::formsep ($self->{nxt} - $self->{exp}), 2966 ::formsep ($self->{nxt} - $self->{exp}),
2967 $self->_percent * 100,
2931 }, 2968 },
2932 %arg
2933 ); 2969 );
2934 2970
2935 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2971 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2936 if $::CONN; 2972 if $::CONN;
2937 2973
2945 if $::CONN; 2981 if $::CONN;
2946 2982
2947 $self->SUPER::DESTROY; 2983 $self->SUPER::DESTROY;
2948} 2984}
2949 2985
2986sub _percent {
2987 my ($self) = @_;
2988
2989 my $table = $::CONN && $::CONN->{exp_table}
2990 or return -1;
2991
2992 my $l0 = $table->[$self->{lvl} - 1];
2993 my $l1 = $table->[$self->{lvl}];
2994
2995 $self->{nxt} = $l1;
2996
2997 ($self->{exp} - $l0) / ($l1 - $l0)
2998}
2999
2950sub set_value { 3000sub set_value {
2951 my ($self, $lvl, $exp) = @_; 3001 my ($self, $lvl, $exp) = @_;
2952 3002
2953 $self->{lvl} = $lvl; 3003 $self->{lvl} = $lvl;
2954 $self->{exp} = $exp; 3004 $self->{exp} = $exp;
2955 3005
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); 3006 $self->SUPER::set_value ($self->_percent);
2968} 3007}
2969 3008
2970############################################################################# 3009#############################################################################
2971 3010
2972package DC::UI::Gauge; 3011package DC::UI::Gauge;
3016 3055
3017############################################################################# 3056#############################################################################
3018 3057
3019package DC::UI::Slider; 3058package DC::UI::Slider;
3020 3059
3021use strict; 3060use common::sense;
3022 3061
3023use DC::OpenGL; 3062use DC::OpenGL;
3024 3063
3025our @ISA = DC::UI::DrawBG::; 3064our @ISA = DC::UI::DrawBG::;
3026 3065
3112 if ($GRAB == $self) { 3151 if ($GRAB == $self) {
3113 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3152 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3114 3153
3115 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3154 my (undef, $lo, $hi, $page) = @{$self->{range}};
3116 3155
3117 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3156 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3118 3157
3119 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3158 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3120 } else { 3159 } else {
3121 return 0; 3160 return 0;
3122 } 3161 }
3248 indent => 0, 3287 indent => 0,
3249 #font => default_font 3288 #font => default_font
3250 @_, 3289 @_,
3251 3290
3252 layout => (new DC::Layout), 3291 layout => (new DC::Layout),
3253 par => [],
3254 max_par => 0, 3292 max_par => 0,
3255 height => 0, 3293 height => 0,
3256 children => [ 3294 children => [
3257 (new DC::UI::Empty expand => 1), 3295 (new DC::UI::Empty expand => 1),
3258 (new DC::UI::Slider vertical => 1), 3296 (new DC::UI::Slider vertical => 1),
3259 ], 3297 ],
3260 ); 3298 );
3261 3299
3262 $self->{children}[1]->connect (changed => sub { $self->update }); 3300 $self->{children}[1]->connect (changed => sub { $self->update });
3301
3302 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3263 3303
3264 $self 3304 $self
3265} 3305}
3266 3306
3267sub set_fontsize { 3307sub set_fontsize {
3620 3660
3621 $tip =~ s/^\n+//; 3661 $tip =~ s/^\n+//;
3622 $tip =~ s/\n+$//; 3662 $tip =~ s/\n+$//;
3623 3663
3624 $self->add (new DC::UI::Label 3664 $self->add (new DC::UI::Label
3665 fg => $DC::THEME{tooltip_fg},
3625 markup => $tip, 3666 markup => $tip,
3626 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3667 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3627 align => 0, 3668 align => 0,
3628 fontsize => 0.8, 3669 fontsize => 0.8,
3629 style => 1, # FLAG_INVERSE 3670 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3630 ellipsise => 0, 3671 ellipsise => 0,
3631 font => ($widget->{tooltip_font} || $::FONT_PROP), 3672 font => ($widget->{tooltip_font} || $::FONT_PROP),
3632 ); 3673 );
3633} 3674}
3634 3675
3671sub _draw { 3712sub _draw {
3672 my ($self) = @_; 3713 my ($self) = @_;
3673 3714
3674 my ($w, $h) = @$self{qw(w h)}; 3715 my ($w, $h) = @$self{qw(w h)};
3675 3716
3676 glColor 1, 0.8, 0.4; 3717 glColor @{ $DC::THEME{tooltip_bg} };
3677 glRect 0, 0, $w, $h; 3718 glRect 0, 0, $w, $h;
3678 3719
3679 glColor 0, 0, 0; 3720 glColor @{ $DC::THEME{tooltip_border} };
3680 glRect_lineloop .5, .5, $w + .5, $h + .5; 3721 glRect_lineloop .5, .5, $w + .5, $h + .5;
3681 3722
3682 glTranslate 2, 2; 3723 glTranslate 2, 2;
3683 3724
3684 $self->SUPER::_draw; 3725 $self->SUPER::_draw;
3701 aspect => 1, 3742 aspect => 1,
3702 can_events => 0, 3743 can_events => 0,
3703 @_, 3744 @_,
3704 ); 3745 );
3705 3746
3706 if ($self->{anim} && $self->{animspeed}) {
3707 DC::weaken (my $widget = $self);
3708
3709 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3710 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3711 return unless $::CONN;
3712
3713 my $w = $widget
3714 or return;
3715
3716 ++$w->{frame};
3717 $w->update_face;
3718
3719 # somehow, $widget can go away
3720 $w->update;
3721 $w->update_timer;
3722 };
3723
3724 $self->update_face;
3725 $self->update_timer; 3747 $self->update_anim;
3726 }
3727 3748
3728 $self 3749 $self
3729} 3750}
3730 3751
3731sub update_timer { 3752sub update_timer {
3754 $tex->upload (sub { $self->reconfigure }); 3775 $tex->upload (sub { $self->reconfigure });
3755 } 3776 }
3756 } 3777 }
3757 } 3778 }
3758 } 3779 }
3780 }
3781}
3782
3783sub update_anim {
3784 my ($self) = @_;
3785
3786 if ($self->{anim} && $self->{animspeed}) {
3787 DC::weaken (my $widget = $self);
3788
3789 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3790 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3791 return unless $::CONN;
3792
3793 my $w = $widget
3794 or return;
3795
3796 ++$w->{frame};
3797 $w->update_face;
3798
3799 # somehow, $widget can go away
3800 $w->update;
3801 $w->update_timer;
3802 };
3803
3804 $self->update_face;
3805 $self->update_timer;
3806 } else {
3807 delete $self->{timer};
3759 } 3808 }
3760} 3809}
3761 3810
3762sub size_request { 3811sub size_request {
3763 my ($self) = @_; 3812 my ($self) = @_;
3785 return unless $self->{visible}; 3834 return unless $self->{visible};
3786 3835
3787 $self->SUPER::update; 3836 $self->SUPER::update;
3788} 3837}
3789 3838
3839sub set_face {
3840 my ($self, $face) = @_;
3841
3842 $self->{face} = $face;
3843 $self->reconfigure;
3844}
3845
3846sub set_anim {
3847 my ($self, $anim) = @_;
3848
3849 $self->{anim} = $anim;
3850 $self->update_anim;
3851}
3852
3853sub set_animspeed {
3854 my ($self, $animspeed) = @_;
3855
3856 $self->{animspeed} = $animspeed;
3857 $self->update_anim;
3858}
3859
3790sub invoke_visibility_change { 3860sub invoke_visibility_change {
3791 my ($self) = @_; 3861 my ($self) = @_;
3792 3862
3793 $self->update_timer; 3863 $self->update_timer;
3794 3864
3890 # maybe save $GRAB? must be careful about events... 3960 # maybe save $GRAB? must be careful about events...
3891 $GRAB = $self; 3961 $GRAB = $self;
3892 $self->{button} = $ev->{button}; 3962 $self->{button} = $ev->{button};
3893 3963
3894 $self->show; 3964 $self->show;
3895 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3965
3966 my $x = $ev->{x};
3967 my $y = $ev->{y};
3968
3969 $self->{root}->on_post_alloc ($self => sub {
3970 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3971 });
3972
3973 1 # so it can be used inside event handlers
3896} 3974}
3897 3975
3898sub invoke_mouse_motion { 3976sub invoke_mouse_motion {
3899 my ($self, $ev, $x, $y) = @_; 3977 my ($self, $ev, $x, $y) = @_;
3900 3978
3932 4010
3933 my $self = $class->SUPER::new ( 4011 my $self = $class->SUPER::new (
3934 @_, 4012 @_,
3935 ); 4013 );
3936 4014
3937 $self->{current} = $self->{children}[0] 4015 $self->set_current_page (0);
3938 if @{ $self->{children} };
3939 4016
3940 $self 4017 $self
3941} 4018}
3942 4019
3943sub add { 4020sub add {
3944 my ($self, @widgets) = @_; 4021 my ($self, @widgets) = @_;
3945 4022
3946 $self->SUPER::add (@widgets); 4023 $self->SUPER::add (@widgets);
3947 4024
3948 $self->{current} = $self->{children}[0] 4025 $self->set_current_page (0)
3949 if @{ $self->{children} }; 4026 if @widgets == @{ $self->{children} };
3950} 4027}
3951 4028
3952sub get_current_page { 4029sub get_current_page {
3953 my ($self) = @_; 4030 my ($self) = @_;
3954 4031
3960 4037
3961 my $widget = ref $page_or_widget 4038 my $widget = ref $page_or_widget
3962 ? $page_or_widget 4039 ? $page_or_widget
3963 : $self->{children}[$page_or_widget]; 4040 : $self->{children}[$page_or_widget];
3964 4041
4042 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4043
3965 $self->{current} = $widget; 4044 if (($self->{current} = $widget)) {
4045 $self->{current}->set_visible if $self->{current} && $self->{visible};
3966 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4046 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3967 4047
3968 $self->emit (page_changed => $self->{current}); 4048 $self->emit (page_changed => $self->{current});
4049 }
3969 4050
3970 $self->realloc; 4051 $self->realloc;
3971} 4052}
3972 4053
3973sub visible_children { 4054sub visible_children {
3974 $_[0]{current} 4055 $_[0]{current} || ()
3975} 4056}
3976 4057
3977sub size_request { 4058sub size_request {
3978 my ($self) = @_; 4059 my ($self) = @_;
3979 4060
4061 $self->{current}
3980 $self->{current}->size_request 4062 ? $self->{current}->size_request
4063 : (0, 0)
3981} 4064}
3982 4065
3983sub invoke_size_allocate { 4066sub invoke_size_allocate {
3984 my ($self, $w, $h) = @_; 4067 my ($self, $w, $h) = @_;
3985 4068
3986 $self->{current}->configure (0, 0, $w, $h); 4069 $self->{current}->configure (0, 0, $w, $h)
4070 if $self->{current};
3987 4071
3988 1 4072 1
3989} 4073}
3990 4074
3991sub _draw { 4075sub _draw {
3992 my ($self) = @_; 4076 my ($self) = @_;
3993 4077
3994 $self->{current}->draw; 4078 $self->{current}->draw
4079 if $self->{current};
3995} 4080}
3996 4081
3997############################################################################# 4082#############################################################################
3998 4083
3999package DC::UI::Notebook; 4084package DC::UI::Notebook;
4209 $self 4294 $self
4210} 4295}
4211 4296
4212sub reorder { 4297sub reorder {
4213 my ($self) = @_; 4298 my ($self) = @_;
4214 my $NOW = Time::HiRes::time; 4299 my $NOW = AE::time;
4215 4300
4216 # freeze display when hovering over any label 4301 # freeze display when hovering over any label
4217 return if $DC::UI::TOOLTIP->{owner} 4302 return if $DC::UI::TOOLTIP->{owner}
4218 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4303 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4219 values %{ $self->{item} }; 4304 values %{ $self->{item} };
4269 $label->{fg}[3] = $item->{fg}[3] || 1; 4354 $label->{fg}[3] = $item->{fg}[3] || 1;
4270 } 4355 }
4271 4356
4272 push @widgets, $label; 4357 push @widgets, $label;
4273 } 4358 }
4359
4360 my $hash = join ",", @widgets;
4361 return if $hash eq $self->{last_widget_hash};
4362 $self->{last_widget_hash} = $hash;
4274 4363
4275 $self->clear; 4364 $self->clear;
4276 $self->SUPER::add (reverse @widgets); 4365 $self->SUPER::add (reverse @widgets);
4277} 4366}
4278 4367
4314 $ROOT->on_refresh (reorder => sub { 4403 $ROOT->on_refresh (reorder => sub {
4315 $self->reorder; 4404 $self->reorder;
4316 }); 4405 });
4317} 4406}
4318 4407
4408sub clr_group {
4409 my ($self, $group) = @_;
4410
4411 if (delete $self->{item}{$group}) {
4412 $ROOT->on_refresh (reorder => sub {
4413 $self->reorder;
4414 });
4415 }
4416}
4417
4319sub reconfigure { 4418sub reconfigure {
4320 my ($self) = @_; 4419 my ($self) = @_;
4321 4420
4322 delete $_->{label} 4421 delete $_->{label}
4323 for values %{ $self->{item} || {} }; 4422 for values %{ $self->{item} || {} };
4583 4682
4584$ROOT = new DC::UI::Root; 4683$ROOT = new DC::UI::Root;
4585$TOOLTIP = new DC::UI::Tooltip z => 900; 4684$TOOLTIP = new DC::UI::Tooltip z => 900;
4586 4685
45871 46861
4588

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines