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.484 by root, Sun Jan 8 07:58:39 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
358} 363}
359 364
360sub set_size { 365sub set_size {
361 my ($self, $w, $h) = @_; 366 my ($self, $w, $h) = @_;
362 367
363 $self->{force_w} = $w; 368 $self->{force_w} = List::Util::min $w, ($self->{max_w} || $::WIDTH );
364 $self->{force_h} = $h; 369 $self->{force_h} = List::Util::min $h, ($self->{max_h} || $::HEIGHT);
365 370
366 $self->realloc; 371 $self->realloc;
367} 372}
368 373
369# traverse the widget chain up to find the maximum "physical" size constraints 374# traverse the widget chain up to find the maximum "physical" size constraints
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 );
1461 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1462 1475
1463 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1464 my $border = $self->border; 1477 my $border = $self->border;
1465 1478
1466 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); 1479 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); # left-right
1467 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); 1480 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); # top-down
1468 1481
1469 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1470 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1471 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1472 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1473 1486
1474 my $mx = $x < $border; 1487 my $mx = $x < $border;
1478 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1479 1492
1480 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1481 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1482 1495
1496 $self->set_size (
1483 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1484 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1485 1500
1486 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1487 $self->realloc; 1502 $self->realloc;
1488 }; 1503 };
1489 1504
1490 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1491 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1492 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1493 1508
1494 $self->{motion} = sub { 1509 $self->{motion} = sub {
1495 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1540 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1541 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1542 1557
1543 my $border = $self->border; 1558 my $border = $self->border;
1544 1559
1560 if ($border) {
1545 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1546 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1547 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1548 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1549 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1565 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1550 1566
1551 # move 1567 # move
1552 my $w2 = ($w - $border) * .5; 1568 my $w2 = ($w - $border) * .5;
1553 my $h2 = ($h - $border) * .5; 1569 my $h2 = ($h - $border) * .5;
1554 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1570 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1555 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1571 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1556 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1572 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1557 1573
1558 # resize 1574 # resize
1559 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1560 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1576 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1561 unless $self->{has_close_button}; 1577 unless $self->{has_close_button};
1562 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1578 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1563 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1579 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1580 }
1564 1581
1565 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1566 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1567 1584
1568 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
2090 my $class = shift; 2107 my $class = shift;
2091 2108
2092 $class->SUPER::new ( 2109 $class->SUPER::new (
2093 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2094 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2095 outline => [0.6, 0.3, 0.1], 2112 outline => undef,
2096 active_bg => [0, 0, 1, .2], 2113 active_bg => [0, 0, 1, .2],
2097 active_fg => [1, 1, 1], 2114 active_fg => [1, 1, 1],
2098 active_outline => [1, 1, 0], 2115 active_outline => [1, 1, 0],
2099 can_hover => 1, 2116 can_hover => 1,
2100 can_focus => 1, 2117 can_focus => 1,
2273 glColor @{$self->{active_outline}}; 2290 glColor @{$self->{active_outline}};
2274 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2291 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2275 glLineWidth 1; 2292 glLineWidth 1;
2276 2293
2277 } else { 2294 } else {
2278 glColor @{$self->{outline}}; 2295 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2279 glBegin GL_LINE_STRIP; 2296 glBegin GL_LINE_STRIP;
2280 glVertex .5, $self->{h} * .5; 2297 glVertex .5, $self->{h} * .5;
2281 glVertex .5, $self->{h} - 2.5; 2298 glVertex .5, $self->{h} - 2.5;
2282 glVertex $self->{w} - .5, $self->{h} - 2.5; 2299 glVertex $self->{w} - .5, $self->{h} - 2.5;
2283 glVertex $self->{w} - .5, $self->{h} * .5; 2300 glVertex $self->{w} - .5, $self->{h} * .5;
2516 2533
2517sub new { 2534sub new {
2518 my $class = shift; 2535 my $class = shift;
2519 2536
2520 $class->SUPER::new ( 2537 $class->SUPER::new (
2538 fontsize => 1,
2521 padding_x => 2, 2539 padding_x => 2,
2522 padding_y => 2, 2540 padding_y => 2,
2523 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2524 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2525 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2531} 2549}
2532 2550
2533sub size_request { 2551sub size_request {
2534 my ($self) = @_; 2552 my ($self) = @_;
2535 2553
2536 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2537} 2555}
2538 2556
2539sub toggle { 2557sub toggle {
2540 my ($self) = @_; 2558 my ($self) = @_;
2541 2559
2579 2597
2580############################################################################# 2598#############################################################################
2581 2599
2582package DC::UI::Image; 2600package DC::UI::Image;
2583 2601
2584our @ISA = DC::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2585 2603
2586use DC::OpenGL; 2604use DC::OpenGL;
2587 2605
2588our %texture_cache; 2606our %texture_cache;
2589 2607
2622 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2623 2641
2624 $self->new (path => $path) 2642 $self->new (path => $path)
2625} 2643}
2626 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2627sub size_request { 2652sub size_request {
2628 my ($self) = @_; 2653 my ($self) = @_;
2629 2654
2630 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2655 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2631} 2656}
2632 2657
2633sub _draw { 2658sub _draw {
2634 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2635 2662
2636 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2637 2664
2638 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2639 2666
2657package DC::UI::ImageButton; 2684package DC::UI::ImageButton;
2658 2685
2659our @ISA = DC::UI::Image::; 2686our @ISA = DC::UI::Image::;
2660 2687
2661use DC::OpenGL; 2688use DC::OpenGL;
2662
2663my %textures;
2664 2689
2665sub new { 2690sub new {
2666 my $class = shift; 2691 my $class = shift;
2667 2692
2668 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2839 2864
2840sub new { 2865sub new {
2841 my ($class, %arg) = @_; 2866 my ($class, %arg) = @_;
2842 2867
2843 my $self = $class->SUPER::new ( 2868 my $self = $class->SUPER::new (
2869 padding_x => 2,
2870 padding_y => 2,
2844 fg => [1, 1, 1], 2871 fg => [1, 1, 1],
2845 bg => [0, 0, 1, 0.2], 2872 bg => [0, 0, 1, 0.2],
2846 bar => [0.7, 0.5, 0.1, 0.8], 2873 bar => [0.7, 0.5, 0.1, 0.8],
2847 outline => [0.4, 0.3, 0], 2874 outline => [0.4, 0.3, 0],
2848 fontsize => 0.9, 2875 fontsize => 0.9,
2888 my ($self) = @_; 2915 my ($self) = @_;
2889 2916
2890 glEnable GL_BLEND; 2917 glEnable GL_BLEND;
2891 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2918 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2892 2919
2920 my $px = $self->{padding_x};
2921 my $py = $self->{padding_y};
2922
2893 if ($self->{value} >= 0) { 2923 if ($self->{value} >= 0) {
2894 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2924 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2895 2925
2896 glColor_premultiply @{$self->{bar}}; 2926 glColor_premultiply @{$self->{bar}};
2897 glRect 2, 2, $s, $self->{h} - 2; 2927 glRect $px, $py, $s, $self->{h} - $py;
2898 glColor_premultiply @{$self->{bg}}; 2928 glColor_premultiply @{$self->{bg}};
2899 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2929 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2900 } 2930 }
2901 2931
2902 glColor_premultiply @{$self->{outline}}; 2932 glColor_premultiply @{$self->{outline}};
2933
2934 $px -= .5;
2935 $py -= .5;
2936
2903 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2937 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2904 2938
2905 glDisable GL_BLEND; 2939 glDisable GL_BLEND;
2906 2940
2907 { 2941 {
2908 local $self->{bg}; # do not draw background 2942 local $self->{bg}; # do not draw background
2917our @ISA = DC::UI::Progress::; 2951our @ISA = DC::UI::Progress::;
2918 2952
2919sub new { 2953sub new {
2920 my ($class, %arg) = @_; 2954 my ($class, %arg) = @_;
2921 2955
2956 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2957
2922 my $self = $class->SUPER::new ( 2958 my $self = $class->SUPER::new (
2959 %arg,
2923 tooltip => sub { 2960 tooltip => sub {
2924 my ($self) = @_; 2961 my ($self) = @_;
2925 2962
2926 sprintf "level %d\n%s points\n%s next level\n%s to go", 2963 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2964 $tt,
2927 $self->{lvl}, 2965 $self->{lvl},
2928 ::formsep ($self->{exp}), 2966 ::formsep ($self->{exp}),
2929 ::formsep ($self->{nxt}), 2967 ::formsep ($self->{nxt}),
2930 ::formsep ($self->{nxt} - $self->{exp}), 2968 ::formsep ($self->{nxt} - $self->{exp}),
2969 $self->_percent * 100,
2931 }, 2970 },
2932 %arg
2933 ); 2971 );
2934 2972
2935 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2973 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2936 if $::CONN; 2974 if $::CONN;
2937 2975
2945 if $::CONN; 2983 if $::CONN;
2946 2984
2947 $self->SUPER::DESTROY; 2985 $self->SUPER::DESTROY;
2948} 2986}
2949 2987
2988sub _percent {
2989 my ($self) = @_;
2990
2991 my $table = $::CONN && $::CONN->{exp_table}
2992 or return -1;
2993
2994 my $l0 = $table->[$self->{lvl} - 1];
2995 my $l1 = $table->[$self->{lvl}];
2996
2997 $self->{nxt} = $l1;
2998
2999 ($self->{exp} - $l0) / ($l1 - $l0)
3000}
3001
2950sub set_value { 3002sub set_value {
2951 my ($self, $lvl, $exp) = @_; 3003 my ($self, $lvl, $exp) = @_;
2952 3004
2953 $self->{lvl} = $lvl; 3005 $self->{lvl} = $lvl;
2954 $self->{exp} = $exp; 3006 $self->{exp} = $exp;
2955 3007
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); 3008 $self->SUPER::set_value ($self->_percent);
2968} 3009}
2969 3010
2970############################################################################# 3011#############################################################################
2971 3012
2972package DC::UI::Gauge; 3013package DC::UI::Gauge;
3016 3057
3017############################################################################# 3058#############################################################################
3018 3059
3019package DC::UI::Slider; 3060package DC::UI::Slider;
3020 3061
3021use strict; 3062use common::sense;
3022 3063
3023use DC::OpenGL; 3064use DC::OpenGL;
3024 3065
3025our @ISA = DC::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
3026 3067
3112 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
3113 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3114 3155
3115 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
3116 3157
3117 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3118 3159
3119 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3120 } else { 3161 } else {
3121 return 0; 3162 return 0;
3122 } 3163 }
3248 indent => 0, 3289 indent => 0,
3249 #font => default_font 3290 #font => default_font
3250 @_, 3291 @_,
3251 3292
3252 layout => (new DC::Layout), 3293 layout => (new DC::Layout),
3253 par => [],
3254 max_par => 0, 3294 max_par => 0,
3255 height => 0, 3295 height => 0,
3256 children => [ 3296 children => [
3257 (new DC::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3258 (new DC::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3259 ], 3299 ],
3260 ); 3300 );
3261 3301
3262 $self->{children}[1]->connect (changed => sub { $self->update }); 3302 $self->{children}[1]->connect (changed => sub { $self->update });
3303
3304 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3263 3305
3264 $self 3306 $self
3265} 3307}
3266 3308
3267sub set_fontsize { 3309sub set_fontsize {
3620 3662
3621 $tip =~ s/^\n+//; 3663 $tip =~ s/^\n+//;
3622 $tip =~ s/\n+$//; 3664 $tip =~ s/\n+$//;
3623 3665
3624 $self->add (new DC::UI::Label 3666 $self->add (new DC::UI::Label
3667 fg => $DC::THEME{tooltip_fg},
3625 markup => $tip, 3668 markup => $tip,
3626 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3669 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3627 align => 0, 3670 align => 0,
3628 fontsize => 0.8, 3671 fontsize => 0.8,
3629 style => 1, # FLAG_INVERSE 3672 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3630 ellipsise => 0, 3673 ellipsise => 0,
3631 font => ($widget->{tooltip_font} || $::FONT_PROP), 3674 font => ($widget->{tooltip_font} || $::FONT_PROP),
3632 ); 3675 );
3633} 3676}
3634 3677
3671sub _draw { 3714sub _draw {
3672 my ($self) = @_; 3715 my ($self) = @_;
3673 3716
3674 my ($w, $h) = @$self{qw(w h)}; 3717 my ($w, $h) = @$self{qw(w h)};
3675 3718
3676 glColor 1, 0.8, 0.4; 3719 glColor @{ $DC::THEME{tooltip_bg} };
3677 glRect 0, 0, $w, $h; 3720 glRect 0, 0, $w, $h;
3678 3721
3679 glColor 0, 0, 0; 3722 glColor @{ $DC::THEME{tooltip_border} };
3680 glRect_lineloop .5, .5, $w + .5, $h + .5; 3723 glRect_lineloop .5, .5, $w + .5, $h + .5;
3681 3724
3682 glTranslate 2, 2; 3725 glTranslate 2, 2;
3683 3726
3684 $self->SUPER::_draw; 3727 $self->SUPER::_draw;
3701 aspect => 1, 3744 aspect => 1,
3702 can_events => 0, 3745 can_events => 0,
3703 @_, 3746 @_,
3704 ); 3747 );
3705 3748
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; 3749 $self->update_anim;
3726 }
3727 3750
3728 $self 3751 $self
3729} 3752}
3730 3753
3731sub update_timer { 3754sub update_timer {
3754 $tex->upload (sub { $self->reconfigure }); 3777 $tex->upload (sub { $self->reconfigure });
3755 } 3778 }
3756 } 3779 }
3757 } 3780 }
3758 } 3781 }
3782 }
3783}
3784
3785sub update_anim {
3786 my ($self) = @_;
3787
3788 if ($self->{anim} && $self->{animspeed}) {
3789 DC::weaken (my $widget = $self);
3790
3791 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3792 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3793 return unless $::CONN;
3794
3795 my $w = $widget
3796 or return;
3797
3798 ++$w->{frame};
3799 $w->update_face;
3800
3801 # somehow, $widget can go away
3802 $w->update;
3803 $w->update_timer;
3804 };
3805
3806 $self->update_face;
3807 $self->update_timer;
3808 } else {
3809 delete $self->{timer};
3759 } 3810 }
3760} 3811}
3761 3812
3762sub size_request { 3813sub size_request {
3763 my ($self) = @_; 3814 my ($self) = @_;
3785 return unless $self->{visible}; 3836 return unless $self->{visible};
3786 3837
3787 $self->SUPER::update; 3838 $self->SUPER::update;
3788} 3839}
3789 3840
3841sub set_face {
3842 my ($self, $face) = @_;
3843
3844 $self->{face} = $face;
3845 $self->reconfigure;
3846}
3847
3848sub set_anim {
3849 my ($self, $anim) = @_;
3850
3851 $self->{anim} = $anim;
3852 $self->update_anim;
3853}
3854
3855sub set_animspeed {
3856 my ($self, $animspeed) = @_;
3857
3858 $self->{animspeed} = $animspeed;
3859 $self->update_anim;
3860}
3861
3790sub invoke_visibility_change { 3862sub invoke_visibility_change {
3791 my ($self) = @_; 3863 my ($self) = @_;
3792 3864
3793 $self->update_timer; 3865 $self->update_timer;
3794 3866
3890 # maybe save $GRAB? must be careful about events... 3962 # maybe save $GRAB? must be careful about events...
3891 $GRAB = $self; 3963 $GRAB = $self;
3892 $self->{button} = $ev->{button}; 3964 $self->{button} = $ev->{button};
3893 3965
3894 $self->show; 3966 $self->show;
3895 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3967
3968 my $x = $ev->{x};
3969 my $y = $ev->{y};
3970
3971 $self->{root}->on_post_alloc ($self => sub {
3972 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3973 });
3974
3975 1 # so it can be used inside event handlers
3896} 3976}
3897 3977
3898sub invoke_mouse_motion { 3978sub invoke_mouse_motion {
3899 my ($self, $ev, $x, $y) = @_; 3979 my ($self, $ev, $x, $y) = @_;
3900 3980
3932 4012
3933 my $self = $class->SUPER::new ( 4013 my $self = $class->SUPER::new (
3934 @_, 4014 @_,
3935 ); 4015 );
3936 4016
3937 $self->{current} = $self->{children}[0] 4017 $self->set_current_page (0);
3938 if @{ $self->{children} };
3939 4018
3940 $self 4019 $self
3941} 4020}
3942 4021
3943sub add { 4022sub add {
3944 my ($self, @widgets) = @_; 4023 my ($self, @widgets) = @_;
3945 4024
3946 $self->SUPER::add (@widgets); 4025 $self->SUPER::add (@widgets);
3947 4026
3948 $self->{current} = $self->{children}[0] 4027 $self->set_current_page (0)
3949 if @{ $self->{children} }; 4028 if @widgets == @{ $self->{children} };
3950} 4029}
3951 4030
3952sub get_current_page { 4031sub get_current_page {
3953 my ($self) = @_; 4032 my ($self) = @_;
3954 4033
3960 4039
3961 my $widget = ref $page_or_widget 4040 my $widget = ref $page_or_widget
3962 ? $page_or_widget 4041 ? $page_or_widget
3963 : $self->{children}[$page_or_widget]; 4042 : $self->{children}[$page_or_widget];
3964 4043
4044 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4045
3965 $self->{current} = $widget; 4046 if (($self->{current} = $widget)) {
4047 $self->{current}->set_visible if $self->{current} && $self->{visible};
3966 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4048 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3967 4049
3968 $self->emit (page_changed => $self->{current}); 4050 $self->emit (page_changed => $self->{current});
4051 }
3969 4052
3970 $self->realloc; 4053 $self->realloc;
3971} 4054}
3972 4055
3973sub visible_children { 4056sub visible_children {
3974 $_[0]{current} 4057 $_[0]{current} || ()
3975} 4058}
3976 4059
3977sub size_request { 4060sub size_request {
3978 my ($self) = @_; 4061 my ($self) = @_;
3979 4062
4063 $self->{current}
3980 $self->{current}->size_request 4064 ? $self->{current}->size_request
4065 : (0, 0)
3981} 4066}
3982 4067
3983sub invoke_size_allocate { 4068sub invoke_size_allocate {
3984 my ($self, $w, $h) = @_; 4069 my ($self, $w, $h) = @_;
3985 4070
3986 $self->{current}->configure (0, 0, $w, $h); 4071 $self->{current}->configure (0, 0, $w, $h)
4072 if $self->{current};
3987 4073
3988 1 4074 1
3989} 4075}
3990 4076
3991sub _draw { 4077sub _draw {
3992 my ($self) = @_; 4078 my ($self) = @_;
3993 4079
3994 $self->{current}->draw; 4080 $self->{current}->draw
4081 if $self->{current};
3995} 4082}
3996 4083
3997############################################################################# 4084#############################################################################
3998 4085
3999package DC::UI::Notebook; 4086package DC::UI::Notebook;
4209 $self 4296 $self
4210} 4297}
4211 4298
4212sub reorder { 4299sub reorder {
4213 my ($self) = @_; 4300 my ($self) = @_;
4214 my $NOW = Time::HiRes::time; 4301 my $NOW = AE::time;
4215 4302
4216 # freeze display when hovering over any label 4303 # freeze display when hovering over any label
4217 return if $DC::UI::TOOLTIP->{owner} 4304 return if $DC::UI::TOOLTIP->{owner}
4218 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4305 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4219 values %{ $self->{item} }; 4306 values %{ $self->{item} };
4269 $label->{fg}[3] = $item->{fg}[3] || 1; 4356 $label->{fg}[3] = $item->{fg}[3] || 1;
4270 } 4357 }
4271 4358
4272 push @widgets, $label; 4359 push @widgets, $label;
4273 } 4360 }
4361
4362 my $hash = join ",", @widgets;
4363 return if $hash eq $self->{last_widget_hash};
4364 $self->{last_widget_hash} = $hash;
4274 4365
4275 $self->clear; 4366 $self->clear;
4276 $self->SUPER::add (reverse @widgets); 4367 $self->SUPER::add (reverse @widgets);
4277} 4368}
4278 4369
4314 $ROOT->on_refresh (reorder => sub { 4405 $ROOT->on_refresh (reorder => sub {
4315 $self->reorder; 4406 $self->reorder;
4316 }); 4407 });
4317} 4408}
4318 4409
4410sub clr_group {
4411 my ($self, $group) = @_;
4412
4413 if (delete $self->{item}{$group}) {
4414 $ROOT->on_refresh (reorder => sub {
4415 $self->reorder;
4416 });
4417 }
4418}
4419
4319sub reconfigure { 4420sub reconfigure {
4320 my ($self) = @_; 4421 my ($self) = @_;
4321 4422
4322 delete $_->{label} 4423 delete $_->{label}
4323 for values %{ $self->{item} || {} }; 4424 for values %{ $self->{item} || {} };
4583 4684
4584$ROOT = new DC::UI::Root; 4685$ROOT = new DC::UI::Root;
4585$TOOLTIP = new DC::UI::Tooltip z => 900; 4686$TOOLTIP = new DC::UI::Tooltip z => 900;
4586 4687
45871 46881
4588

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines