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.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
296 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
297 301
298 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
299} 303}
300 304
301sub set_invisible { 305sub set_invisible {
302 my ($self) = @_; 306 my ($self) = @_;
303 307
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
1256 fontsize => ($arg{border} || 0.8) * 0.75; 1268 fontsize => ($arg{border} || 0.8) * 0.75;
1257 } 1269 }
1258 1270
1259 my $self = $class->SUPER::new ( 1271 my $self = $class->SUPER::new (
1260 # label => "", 1272 # label => "",
1261 fg => [0.6, 0.3, 0.1], 1273 fg => undef,
1262 border => 0.8, 1274 border => 0.8,
1263 style => 'single', 1275 style => 'single',
1264 %arg, 1276 %arg,
1265 ); 1277 );
1266 1278
1319 my $border = $self->border; 1331 my $border = $self->border;
1320 my ($w, $h) = ($self->{w}, $self->{h}); 1332 my ($w, $h) = ($self->{w}, $self->{h});
1321 1333
1322 $child->draw; 1334 $child->draw;
1323 1335
1324 glColor @{$self->{fg}}; 1336 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1325 glBegin GL_LINE_STRIP; 1337 glBegin GL_LINE_STRIP;
1326 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1338 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1327 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1328 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1340 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; 1341 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1361 my ($class, %arg) = @_; 1373 my ($class, %arg) = @_;
1362 1374
1363 my $self = $class->SUPER::new ( 1375 my $self = $class->SUPER::new (
1364 bg => [1, 1, 1, 1], 1376 bg => [1, 1, 1, 1],
1365 border_bg => [1, 1, 1, 1], 1377 border_bg => [1, 1, 1, 1],
1366 border => 1, 1378 border => 0.8,
1367 can_events => 1, 1379 can_events => 1,
1368 min_w => 64, 1380 min_w => 64,
1369 min_h => 32, 1381 min_h => 32,
1370 %arg, 1382 %arg,
1371 ); 1383 );
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
2090 my $class = shift; 2104 my $class = shift;
2091 2105
2092 $class->SUPER::new ( 2106 $class->SUPER::new (
2093 fg => [1, 1, 1], 2107 fg => [1, 1, 1],
2094 bg => [0, 0, 0, 0.2], 2108 bg => [0, 0, 0, 0.2],
2095 outline => [0.6, 0.3, 0.1], 2109 outline => undef,
2096 active_bg => [0, 0, 1, .2], 2110 active_bg => [0, 0, 1, .2],
2097 active_fg => [1, 1, 1], 2111 active_fg => [1, 1, 1],
2098 active_outline => [1, 1, 0], 2112 active_outline => [1, 1, 0],
2099 can_hover => 1, 2113 can_hover => 1,
2100 can_focus => 1, 2114 can_focus => 1,
2273 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2274 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2288 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2275 glLineWidth 1; 2289 glLineWidth 1;
2276 2290
2277 } else { 2291 } else {
2278 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2279 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2280 glVertex .5, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2281 glVertex .5, $self->{h} - 2.5; 2295 glVertex .5, $self->{h} - 2.5;
2282 glVertex $self->{w} - .5, $self->{h} - 2.5; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2283 glVertex $self->{w} - .5, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
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 }
3620 3658
3621 $tip =~ s/^\n+//; 3659 $tip =~ s/^\n+//;
3622 $tip =~ s/\n+$//; 3660 $tip =~ s/\n+$//;
3623 3661
3624 $self->add (new DC::UI::Label 3662 $self->add (new DC::UI::Label
3663 fg => $DC::THEME{tooltip_fg},
3625 markup => $tip, 3664 markup => $tip,
3626 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3665 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3627 align => 0, 3666 align => 0,
3628 fontsize => 0.8, 3667 fontsize => 0.8,
3629 style => 1, # FLAG_INVERSE 3668 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3630 ellipsise => 0, 3669 ellipsise => 0,
3631 font => ($widget->{tooltip_font} || $::FONT_PROP), 3670 font => ($widget->{tooltip_font} || $::FONT_PROP),
3632 ); 3671 );
3633} 3672}
3634 3673
3671sub _draw { 3710sub _draw {
3672 my ($self) = @_; 3711 my ($self) = @_;
3673 3712
3674 my ($w, $h) = @$self{qw(w h)}; 3713 my ($w, $h) = @$self{qw(w h)};
3675 3714
3676 glColor 1, 0.8, 0.4; 3715 glColor @{ $DC::THEME{tooltip_bg} };
3677 glRect 0, 0, $w, $h; 3716 glRect 0, 0, $w, $h;
3678 3717
3679 glColor 0, 0, 0; 3718 glColor @{ $DC::THEME{tooltip_border} };
3680 glRect_lineloop .5, .5, $w + .5, $h + .5; 3719 glRect_lineloop .5, .5, $w + .5, $h + .5;
3681 3720
3682 glTranslate 2, 2; 3721 glTranslate 2, 2;
3683 3722
3684 $self->SUPER::_draw; 3723 $self->SUPER::_draw;
3701 aspect => 1, 3740 aspect => 1,
3702 can_events => 0, 3741 can_events => 0,
3703 @_, 3742 @_,
3704 ); 3743 );
3705 3744
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; 3745 $self->update_anim;
3726 }
3727 3746
3728 $self 3747 $self
3729} 3748}
3730 3749
3731sub update_timer { 3750sub update_timer {
3754 $tex->upload (sub { $self->reconfigure }); 3773 $tex->upload (sub { $self->reconfigure });
3755 } 3774 }
3756 } 3775 }
3757 } 3776 }
3758 } 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};
3759 } 3806 }
3760} 3807}
3761 3808
3762sub size_request { 3809sub size_request {
3763 my ($self) = @_; 3810 my ($self) = @_;
3785 return unless $self->{visible}; 3832 return unless $self->{visible};
3786 3833
3787 $self->SUPER::update; 3834 $self->SUPER::update;
3788} 3835}
3789 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
3790sub invoke_visibility_change { 3858sub invoke_visibility_change {
3791 my ($self) = @_; 3859 my ($self) = @_;
3792 3860
3793 $self->update_timer; 3861 $self->update_timer;
3794 3862
3890 # maybe save $GRAB? must be careful about events... 3958 # maybe save $GRAB? must be careful about events...
3891 $GRAB = $self; 3959 $GRAB = $self;
3892 $self->{button} = $ev->{button}; 3960 $self->{button} = $ev->{button};
3893 3961
3894 $self->show; 3962 $self->show;
3895 $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
3896} 3972}
3897 3973
3898sub invoke_mouse_motion { 3974sub invoke_mouse_motion {
3899 my ($self, $ev, $x, $y) = @_; 3975 my ($self, $ev, $x, $y) = @_;
3900 3976
3932 4008
3933 my $self = $class->SUPER::new ( 4009 my $self = $class->SUPER::new (
3934 @_, 4010 @_,
3935 ); 4011 );
3936 4012
3937 $self->{current} = $self->{children}[0] 4013 $self->set_current_page (0);
3938 if @{ $self->{children} };
3939 4014
3940 $self 4015 $self
3941} 4016}
3942 4017
3943sub add { 4018sub add {
3944 my ($self, @widgets) = @_; 4019 my ($self, @widgets) = @_;
3945 4020
3946 $self->SUPER::add (@widgets); 4021 $self->SUPER::add (@widgets);
3947 4022
3948 $self->{current} = $self->{children}[0] 4023 $self->set_current_page (0)
3949 if @{ $self->{children} }; 4024 if @widgets == @{ $self->{children} };
3950} 4025}
3951 4026
3952sub get_current_page { 4027sub get_current_page {
3953 my ($self) = @_; 4028 my ($self) = @_;
3954 4029
3960 4035
3961 my $widget = ref $page_or_widget 4036 my $widget = ref $page_or_widget
3962 ? $page_or_widget 4037 ? $page_or_widget
3963 : $self->{children}[$page_or_widget]; 4038 : $self->{children}[$page_or_widget];
3964 4039
4040 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4041
3965 $self->{current} = $widget; 4042 if (($self->{current} = $widget)) {
4043 $self->{current}->set_visible if $self->{current} && $self->{visible};
3966 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4044 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3967 4045
3968 $self->emit (page_changed => $self->{current}); 4046 $self->emit (page_changed => $self->{current});
4047 }
3969 4048
3970 $self->realloc; 4049 $self->realloc;
3971} 4050}
3972 4051
3973sub visible_children { 4052sub visible_children {
3974 $_[0]{current} 4053 $_[0]{current} || ()
3975} 4054}
3976 4055
3977sub size_request { 4056sub size_request {
3978 my ($self) = @_; 4057 my ($self) = @_;
3979 4058
4059 $self->{current}
3980 $self->{current}->size_request 4060 ? $self->{current}->size_request
4061 : (0, 0)
3981} 4062}
3982 4063
3983sub invoke_size_allocate { 4064sub invoke_size_allocate {
3984 my ($self, $w, $h) = @_; 4065 my ($self, $w, $h) = @_;
3985 4066
3986 $self->{current}->configure (0, 0, $w, $h); 4067 $self->{current}->configure (0, 0, $w, $h)
4068 if $self->{current};
3987 4069
3988 1 4070 1
3989} 4071}
3990 4072
3991sub _draw { 4073sub _draw {
3992 my ($self) = @_; 4074 my ($self) = @_;
3993 4075
3994 $self->{current}->draw; 4076 $self->{current}->draw
4077 if $self->{current};
3995} 4078}
3996 4079
3997############################################################################# 4080#############################################################################
3998 4081
3999package DC::UI::Notebook; 4082package DC::UI::Notebook;
4209 $self 4292 $self
4210} 4293}
4211 4294
4212sub reorder { 4295sub reorder {
4213 my ($self) = @_; 4296 my ($self) = @_;
4214 my $NOW = Time::HiRes::time; 4297 my $NOW = EV::time;
4215 4298
4216 # freeze display when hovering over any label 4299 # freeze display when hovering over any label
4217 return if $DC::UI::TOOLTIP->{owner} 4300 return if $DC::UI::TOOLTIP->{owner}
4218 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4301 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4219 values %{ $self->{item} }; 4302 values %{ $self->{item} };
4269 $label->{fg}[3] = $item->{fg}[3] || 1; 4352 $label->{fg}[3] = $item->{fg}[3] || 1;
4270 } 4353 }
4271 4354
4272 push @widgets, $label; 4355 push @widgets, $label;
4273 } 4356 }
4357
4358 my $hash = join ",", @widgets;
4359 return if $hash eq $self->{last_widget_hash};
4360 $self->{last_widget_hash} = $hash;
4274 4361
4275 $self->clear; 4362 $self->clear;
4276 $self->SUPER::add (reverse @widgets); 4363 $self->SUPER::add (reverse @widgets);
4277} 4364}
4278 4365
4583 4670
4584$ROOT = new DC::UI::Root; 4671$ROOT = new DC::UI::Root;
4585$TOOLTIP = new DC::UI::Tooltip z => 900; 4672$TOOLTIP = new DC::UI::Tooltip z => 900;
4586 4673
45871 46741
4588

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines