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.462 by root, Thu Mar 20 22:28:33 2008 UTC vs.
Revision 1.488 by root, Fri Nov 23 12:39:05 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
50sub get_layout { 52sub get_layout {
51 my $layout; 53 my $layout;
52 54
53 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
54 my $win = $layout->{$_->{name}} = { }; 56 my $win = $layout->{$_->{name}} = { };
55 57
56 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/; 58 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
57 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/; 59 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
58 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w}; 60 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
59 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h}; 61 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
60 62
69 71
70 $LAYOUT = $layout; 72 $LAYOUT = $layout;
71} 73}
72 74
73# class methods for events 75# class methods for events
74sub feed_sdl_key_down_event { 76sub feed_sdl_key_down_event {
75 $FOCUS->emit (key_down => $_[0]) 77 $FOCUS->emit (key_down => $_[0])
76 if $FOCUS; 78 if $FOCUS;
77} 79}
78 80
79sub feed_sdl_key_up_event { 81sub feed_sdl_key_up_event {
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
370sub get_max_wh { 375#sub get_max_wh {
371 my ($self) = @_; 376# my ($self) = @_;
372 377#
373 my ($w, $h) = @$self{qw(max_w max_h)}; 378# my ($w, $h) = @$self{qw(max_w max_h)};
374 379#
375 if ($w <= 0 || $h <= 0) { 380# if ($w <= 0 || $h <= 0) {
376 my ($mw, $mh) = $self->{parent} 381# my ($mw, $mh) = $self->{parent}
377 ? $self->{parent}->get_max_wh 382# ? $self->{parent}->get_max_wh
378 : ($::WIDTH, $::HEIGHT); 383# : ($::WIDTH, $::HEIGHT);
379 384#
380 $w = $mw if $w <= 0; 385# $w = $mw if $w <= 0;
381 $h = $mh if $h <= 0; 386# $h = $mh if $h <= 0;
382 } 387# }
383 388#
384 ($w, $h) 389# ($w, $h)
385} 390#}
386 391
387sub size_request { 392sub size_request {
388 require Carp; 393 require Carp;
389 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
390} 395}
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
1143 1156
1144 $self->grab_focus; 1157 $self->grab_focus;
1145 1158
1146 my $ox = $self->{vp}{view_x}; 1159 my $ox = $self->{vp}{view_x};
1147 my $oy = $self->{vp}{view_y}; 1160 my $oy = $self->{vp}{view_y};
1148 1161
1149 $self->{motion} = sub { 1162 $self->{motion} = sub {
1150 my ($ev, $x, $y) = @_; 1163 my ($ev, $x, $y) = @_;
1151 1164
1152 $ox -= $ev->{xrel}; 1165 $ox -= $ev->{xrel};
1153 $oy -= $ev->{yrel}; 1166 $oy -= $ev->{yrel};
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;
1343 1356
1344our @ISA = DC::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1345 1358
1346use DC::OpenGL; 1359use DC::OpenGL;
1347 1360
1348my $bg = 1361my $bg =
1349 new_from_file DC::Texture DC::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1350 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1351 1364
1352my @border = 1365my @border =
1353 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1354 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1367 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1355 1368
1356my @icon = 1369my @icon =
1357 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 1370 map { new_from_resource DC::Texture $_, mipmap => 1 }
1358 qw(x1_move.png x1_resize.png); 1371 qw(x1_move.png x1_resize.png);
1359 1372
1360sub new { 1373sub new {
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 => 0.6, 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 );
1399 1412
1400sub border { 1413sub border {
1401 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1402} 1415}
1403 1416
1404sub get_max_wh { 1417#sub get_max_wh {
1405 my ($self) = @_; 1418# my ($self) = @_;
1406 1419#
1407 return ($self->{w}, $self->{h}) 1420# return ($self->{w}, $self->{h})
1408 if $self->{visible} && $self->{w}; 1421# if $self->{visible} && $self->{w};
1409 1422#
1410 $self->SUPER::get_max_wh 1423# $self->SUPER::get_max_wh
1411} 1424#}
1412 1425
1413sub size_request { 1426sub size_request {
1414 my ($self) = @_; 1427 my ($self) = @_;
1415 1428
1416 $self->{title_widget}->size_request 1429 $self->{title_widget}->size_request
1451 1464
1452sub invoke_delete { 1465sub invoke_delete {
1453 my ($self) = @_; 1466 my ($self) = @_;
1454 1467
1455 $self->hide; 1468 $self->hide;
1456 1469
1457 1 1470 1
1458} 1471}
1459 1472
1460sub invoke_button_down { 1473sub invoke_button_down {
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
1823 if ($req > $space) { 1840 if ($req > $space) {
1824 # ah well, not enough space 1841 # ah well, not enough space
1825 $_ *= $space / $req for @req; 1842 $_ *= $space / $req for @req;
1826 } else { 1843 } else {
1827 my $expand = (List::Util::sum map $_->{expand}, @children) || 1; 1844 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1828 1845
1829 $space = ($space - $req) / $expand; # remaining space to give away 1846 $space = ($space - $req) / $expand; # remaining space to give away
1830 1847
1831 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1832 for 0 .. $#children; 1849 for 0 .. $#children;
1833 } 1850 }
1972 1989
1973sub size_request { 1990sub size_request {
1974 my ($self) = @_; 1991 my ($self) = @_;
1975 1992
1976 $self->{size_req} ||= do { 1993 $self->{size_req} ||= do {
1977 my ($max_w, $max_h) = $self->get_max_wh; 1994 my $max_w = $self->{w} || $self->{max_w} || 0x0fffffff; # actually 2**31-1 but allow for overflow inside pango
1978 1995
1979 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
1980 $self->{layout}->set_width ($max_w); 1997 $self->{layout}->set_width ($max_w);
1981 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
1982 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2062# $self->{list} = DC::OpenGL::glGenList; 2079# $self->{list} = DC::OpenGL::glGenList;
2063# DC::OpenGL::glNewList $self->{list}; 2080# DC::OpenGL::glNewList $self->{list};
2064# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2081# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2065# DC::OpenGL::glEndList; 2082# DC::OpenGL::glEndList;
2066# } 2083# }
2067# 2084#
2068# DC::OpenGL::glCallList $self->{list}; 2085# DC::OpenGL::glCallList $self->{list};
2069 2086
2070 $self->{layout}->draw; 2087 $self->{layout}->draw;
2071} 2088}
2072 2089
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,
2158 2175
2159 my $text = $self->get_text; 2176 my $text = $self->get_text;
2160 2177
2161 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2178 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2162 2179
2163 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2164 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2165 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2166 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2167 } elsif ($sym == DC::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2168 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2169 } elsif ($sym == DC::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2170 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2221 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; 2238 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2222 $self->{cursor} = length $text; 2239 $self->{cursor} = length $text;
2223 2240
2224 $self->_set_text ($self->{text}); 2241 $self->_set_text ($self->{text});
2225 $self->update; 2242 $self->update;
2226 2243
2227 1 2244 1
2228} 2245}
2229 2246
2230sub invoke_mouse_motion { 2247sub invoke_mouse_motion {
2231 my ($self, $ev, $x, $y) = @_; 2248 my ($self, $ev, $x, $y) = @_;
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;
2405our @ISA = DC::UI::Bin::; 2422our @ISA = DC::UI::Bin::;
2406 2423
2407use DC::OpenGL; 2424use DC::OpenGL;
2408 2425
2409my @tex = 2426my @tex =
2410 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2411 qw(b1_button_inactive.png b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2412 2429
2413sub new { 2430sub new {
2414 my $class = shift; 2431 my $class = shift;
2415 2432
2454our @ISA = DC::UI::Label::; 2471our @ISA = DC::UI::Label::;
2455 2472
2456use DC::OpenGL; 2473use DC::OpenGL;
2457 2474
2458my @tex = 2475my @tex =
2459 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2460 qw(b1_button_inactive.png b1_button_active.png); 2477 qw(b1_button_inactive.png b1_button_active.png);
2461 2478
2462sub new { 2479sub new {
2463 my $class = shift; 2480 my $class = shift;
2464 2481
2507package DC::UI::CheckBox; 2524package DC::UI::CheckBox;
2508 2525
2509our @ISA = DC::UI::DrawBG::; 2526our @ISA = DC::UI::DrawBG::;
2510 2527
2511my @tex = 2528my @tex =
2512 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2529 map { new_from_resource DC::Texture $_, mipmap => 1 }
2513 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2530 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2514 2531
2515use DC::OpenGL; 2532use DC::OpenGL;
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
2598 2616
2599 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2600 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2601 2619
2602 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2603 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2604 2622
2605 DC::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2606 2624
2607 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2608 2626
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 (
2704 2729
2705use DC::OpenGL; 2730use DC::OpenGL;
2706 2731
2707my %tex = ( 2732my %tex = (
2708 food => [ 2733 food => [
2709 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2710 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2711 ], 2736 ],
2712 grace => [ 2737 grace => [
2713 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2714 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2739 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2715 ], 2740 ],
2716 hp => [ 2741 hp => [
2717 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2718 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2719 ], 2744 ],
2720 mana => [ 2745 mana => [
2721 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2722 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2747 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2723 ], 2748 ],
2724); 2749);
2725 2750
2726# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
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
3027my @tex = 3068my @tex =
3028 map { new_from_file DC::Texture DC::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
3029 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
3030 3071
3031sub new { 3072sub new {
3032 my $class = shift; 3073 my $class = shift;
3033 3074
3070sub set_value { 3111sub set_value {
3071 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
3072 3113
3073 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3074 3115
3075 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
3076 3117
3077 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
3078
3079 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
3080 $value = $hi - $page if $value > $hi - $page;
3081 3120
3082 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3083 if $unit; 3122 if $unit;
3084 3123
3085 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3100 my ($self, $ev, $x, $y) = @_; 3139 my ($self, $ev, $x, $y) = @_;
3101 3140
3102 $self->SUPER::invoke_button_down ($ev, $x, $y); 3141 $self->SUPER::invoke_button_down ($ev, $x, $y);
3103 3142
3104 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3143 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
3105 3144
3106 $self->invoke_mouse_motion ($ev, $x, $y); 3145 $self->invoke_mouse_motion ($ev, $x, $y);
3107 3146
3108 1 3147 1
3109} 3148}
3110 3149
3114 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
3115 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3116 3155
3117 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
3118 3157
3119 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3120 3159
3121 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3122 } else { 3161 } else {
3123 return 0; 3162 return 0;
3124 } 3163 }
3149 my ($self) = @_; 3188 my ($self) = @_;
3150 3189
3151 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
3152 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
3153 3192
3154 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3155 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
3156 3195
3157 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3196 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3158 3197
3159 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3198 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3160 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3161 3200
3162 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
3170 3209
3171 glScale $self->{w}, $self->{h}; 3210 glScale $self->{w}, $self->{h};
3172 3211
3173 if ($self->{vertical}) { 3212 if ($self->{vertical}) {
3174 # draw a vertical slider like a rotated horizontal slider 3213 # draw a vertical slider like a rotated horizontal slider
3175 3214
3176 glTranslate 1, 0, 0; 3215 glTranslate 1, 0, 0;
3177 glRotate 90, 0, 0, 1; 3216 glRotate 90, 0, 0, 1;
3178 } 3217 }
3179 3218
3180 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3219 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
3248 fontsize => 1, 3287 fontsize => 1,
3249 can_events => 1, 3288 can_events => 1,
3250 indent => 0, 3289 indent => 0,
3251 #font => default_font 3290 #font => default_font
3252 @_, 3291 @_,
3253 3292
3254 layout => (new DC::Layout), 3293 layout => (new DC::Layout),
3255 par => [],
3256 max_par => 0, 3294 max_par => 0,
3257 height => 0, 3295 height => 0,
3258 children => [ 3296 children => [
3259 (new DC::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3260 (new DC::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3261 ], 3299 ],
3262 ); 3300 );
3263 3301
3264 $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} };
3265 3305
3266 $self 3306 $self
3267} 3307}
3268 3308
3269sub set_fontsize { 3309sub set_fontsize {
3533 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3534 3574
3535 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3536 $self->{speed} = 0.001; 3576 $self->{speed} = 0.001;
3537 $self->{time} = 1; 3577 $self->{time} = 1;
3538 3578
3539 ::animation_start $self; 3579 ::animation_start $self;
3540} 3580}
3541 3581
3542sub animate { 3582sub animate {
3543 my ($self, $interval) = @_; 3583 my ($self, $interval) = @_;
3547 $self->{time} = 0; 3587 $self->{time} = 0;
3548 ::animation_stop $self; 3588 ::animation_stop $self;
3549 } 3589 }
3550 3590
3551 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; 3591 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3552 3592
3553 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); 3593 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3554 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); 3594 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3555} 3595}
3556 3596
3557sub _draw { 3597sub _draw {
3602 @_, 3642 @_,
3603 can_events => 0, 3643 can_events => 0,
3604 ) 3644 )
3605} 3645}
3606 3646
3647# expand, as good as possible
3648sub _expand_doclets {
3649 my ($tip) = @_;
3650
3651 $tip =~ s{#\(([^)]+)\)}{
3652 if ($::CONN) {
3653 exists $::CONN->{doclet}{$1}
3654 ? $::CONN->{doclet}{$1}
3655 : "(waiting for server to show full text)"
3656 } else {
3657 "(unable to show full text without server connection)"
3658 }
3659 }ge;
3660
3661 $tip =~ s/^\n+//;
3662 $tip =~ s/\n+$//;
3663
3664 $tip
3665}
3666
3667# expands a tooltip, potentially multiple times remotely
3668# and returns a guard. clals the clalback each time the text changes.
3669sub expand_tooltip {
3670 my ($tip, $cb) = @_;
3671
3672 # first expand #name tooltips from local pod
3673 $tip = DC::Pod::section_label tooltip => $1
3674 if $tip =~ /^#([^(].*)$/;
3675
3676 my $active; # true if any remote requests outstanding
3677
3678 if ($::CONN && $::CONN->{addme_success}) {
3679 # now find all doclet references
3680 for my $doclet ($tip =~ /#\(([^)]+)\)/g) {
3681 unless (exists $::CONN->{doclet}{$doclet}) {
3682 # need to ask the server
3683 # we don't try to avoid duplicate requests
3684
3685 $active = 1;
3686 $::CONN->send_exti_req (doclet => (split /\//, $doclet, 2), sub {
3687 $::CONN->{doclet}{$doclet} = DC::sanitise_cfxml $_[0];
3688 $cb->(_expand_doclets $tip) if $active;
3689 });
3690 }
3691 }
3692 }
3693
3694 $cb->(_expand_doclets $tip);
3695
3696 $active and Guard::guard { undef $active }
3697}
3698
3607sub set_tooltip_from { 3699sub set_tooltip_from {
3608 my ($self, $widget) = @_; 3700 my ($self, $widget) = @_;
3609 3701
3610 my $tip = $widget->{tooltip}; 3702 my $tip = $widget->{tooltip};
3611 $tip = $tip->($widget) if "CODE" eq ref $tip; 3703 $tip = $tip->($widget) if "CODE" eq ref $tip;
3612
3613 $tip = DC::Pod::section_label tooltip => $1
3614 if $tip =~ /^#(.*)$/;
3615
3616 if ($ENV{CFPLUS_DEBUG} & 2) {
3617 $tip .= "\n\n" . (ref $widget) . "\n"
3618 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3619 . "req $widget->{req_w} $widget->{req_h}\n"
3620 . "visible $widget->{visible}";
3621 }
3622
3623 $tip =~ s/^\n+//;
3624 $tip =~ s/\n+$//;
3625 3704
3626 $self->add (new DC::UI::Label 3705 $self->add (new DC::UI::Label
3627 markup => $tip, 3706 fg => $DC::THEME{tooltip_fg},
3628 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3707 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3629 align => 0, 3708 align => 0,
3630 fontsize => 0.8, 3709 fontsize => 0.8,
3631 style => 1, # FLAG_INVERSE 3710 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3632 ellipsise => 0, 3711 ellipsise => 0,
3633 font => ($widget->{tooltip_font} || $::FONT_PROP), 3712 font => ($widget->{tooltip_font} || $::FONT_PROP),
3634 ); 3713 );
3714
3715 $self->{tooltip_expand} = expand_tooltip $tip, sub {
3716 my ($tip) = @_;
3717
3718 if ($ENV{CFPLUS_DEBUG} & 2) {
3719 $tip .= "\n\n" . (ref $widget) . "\n"
3720 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3721 . "req $widget->{req_w} $widget->{req_h}\n"
3722 . "visible $widget->{visible}";
3723 }
3724
3725 $self->{children}[0]->set_markup ($tip);
3726 };
3635} 3727}
3636 3728
3637sub size_request { 3729sub size_request {
3638 my ($self) = @_; 3730 my ($self) = @_;
3639 3731
3673sub _draw { 3765sub _draw {
3674 my ($self) = @_; 3766 my ($self) = @_;
3675 3767
3676 my ($w, $h) = @$self{qw(w h)}; 3768 my ($w, $h) = @$self{qw(w h)};
3677 3769
3678 glColor 1, 0.8, 0.4; 3770 glColor @{ $DC::THEME{tooltip_bg} };
3679 glRect 0, 0, $w, $h; 3771 glRect 0, 0, $w, $h;
3680 3772
3681 glColor 0, 0, 0; 3773 glColor @{ $DC::THEME{tooltip_border} };
3682 glRect_lineloop .5, .5, $w + .5, $h + .5; 3774 glRect_lineloop .5, .5, $w + .5, $h + .5;
3683 3775
3684 glTranslate 2, 2; 3776 glTranslate 2, 2;
3685 3777
3686 $self->SUPER::_draw; 3778 $self->SUPER::_draw;
3687} 3779}
3688 3780
3703 aspect => 1, 3795 aspect => 1,
3704 can_events => 0, 3796 can_events => 0,
3705 @_, 3797 @_,
3706 ); 3798 );
3707 3799
3708 if ($self->{anim} && $self->{animspeed}) {
3709 DC::weaken (my $widget = $self);
3710
3711 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3712 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3713 return unless $::CONN;
3714
3715 my $w = $widget
3716 or return;
3717
3718 ++$w->{frame};
3719 $w->update_face;
3720
3721 # somehow, $widget can go away
3722 $w->update;
3723 $w->update_timer;
3724 };
3725
3726 $self->update_face;
3727 $self->update_timer; 3800 $self->update_anim;
3728 } 3801
3729
3730 $self 3802 $self
3731} 3803}
3732 3804
3733sub update_timer { 3805sub update_timer {
3734 my ($self) = @_; 3806 my ($self) = @_;
3756 $tex->upload (sub { $self->reconfigure }); 3828 $tex->upload (sub { $self->reconfigure });
3757 } 3829 }
3758 } 3830 }
3759 } 3831 }
3760 } 3832 }
3833 }
3834}
3835
3836sub update_anim {
3837 my ($self) = @_;
3838
3839 if ($self->{anim} && $self->{animspeed}) {
3840 DC::weaken (my $widget = $self);
3841
3842 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3843 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3844 return unless $::CONN;
3845
3846 my $w = $widget
3847 or return;
3848
3849 ++$w->{frame};
3850 $w->update_face;
3851
3852 # somehow, $widget can go away
3853 $w->update;
3854 $w->update_timer;
3855 };
3856
3857 $self->update_face;
3858 $self->update_timer;
3859 } else {
3860 delete $self->{timer};
3761 } 3861 }
3762} 3862}
3763 3863
3764sub size_request { 3864sub size_request {
3765 my ($self) = @_; 3865 my ($self) = @_;
3787 return unless $self->{visible}; 3887 return unless $self->{visible};
3788 3888
3789 $self->SUPER::update; 3889 $self->SUPER::update;
3790} 3890}
3791 3891
3892sub set_face {
3893 my ($self, $face) = @_;
3894
3895 $self->{face} = $face;
3896 $self->reconfigure;
3897}
3898
3899sub set_anim {
3900 my ($self, $anim) = @_;
3901
3902 $self->{anim} = $anim;
3903 $self->update_anim;
3904}
3905
3906sub set_animspeed {
3907 my ($self, $animspeed) = @_;
3908
3909 $self->{animspeed} = $animspeed;
3910 $self->update_anim;
3911}
3912
3792sub invoke_visibility_change { 3913sub invoke_visibility_change {
3793 my ($self) = @_; 3914 my ($self) = @_;
3794 3915
3795 $self->update_timer; 3916 $self->update_timer;
3796 3917
3858 $widget = new DC::UI::HBox 3979 $widget = new DC::UI::HBox
3859 can_hover => 1, 3980 can_hover => 1,
3860 can_events => 1, 3981 can_events => 1,
3861 tooltip => $tooltip, 3982 tooltip => $tooltip,
3862 children => [ 3983 children => [
3863 (new DC::UI::Label markup => $left, expand => 1), 3984 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3864 (new DC::UI::Label markup => $right, align => 1), 3985 (new DC::UI::Label markup => $right, align => 1),
3865 ], 3986 ],
3866 ; 3987 ;
3867 3988
3868 } else { 3989 } else {
3892 # maybe save $GRAB? must be careful about events... 4013 # maybe save $GRAB? must be careful about events...
3893 $GRAB = $self; 4014 $GRAB = $self;
3894 $self->{button} = $ev->{button}; 4015 $self->{button} = $ev->{button};
3895 4016
3896 $self->show; 4017 $self->show;
3897 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 4018
4019 my $x = $ev->{x};
4020 my $y = $ev->{y};
4021
4022 $self->{root}->on_post_alloc ($self => sub {
4023 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
4024 });
4025
4026 1 # so it can be used inside event handlers
3898} 4027}
3899 4028
3900sub invoke_mouse_motion { 4029sub invoke_mouse_motion {
3901 my ($self, $ev, $x, $y) = @_; 4030 my ($self, $ev, $x, $y) = @_;
3902 4031
3934 4063
3935 my $self = $class->SUPER::new ( 4064 my $self = $class->SUPER::new (
3936 @_, 4065 @_,
3937 ); 4066 );
3938 4067
3939 $self->{current} = $self->{children}[0] 4068 $self->set_current_page (0);
3940 if @{ $self->{children} };
3941 4069
3942 $self 4070 $self
3943} 4071}
3944 4072
3945sub add { 4073sub add {
3946 my ($self, @widgets) = @_; 4074 my ($self, @widgets) = @_;
3947 4075
3948 $self->SUPER::add (@widgets); 4076 $self->SUPER::add (@widgets);
3949 4077
3950 $self->{current} = $self->{children}[0] 4078 $self->set_current_page (0)
3951 if @{ $self->{children} }; 4079 if @widgets == @{ $self->{children} };
3952} 4080}
3953 4081
3954sub get_current_page { 4082sub get_current_page {
3955 my ($self) = @_; 4083 my ($self) = @_;
3956 4084
3962 4090
3963 my $widget = ref $page_or_widget 4091 my $widget = ref $page_or_widget
3964 ? $page_or_widget 4092 ? $page_or_widget
3965 : $self->{children}[$page_or_widget]; 4093 : $self->{children}[$page_or_widget];
3966 4094
4095 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4096
3967 $self->{current} = $widget; 4097 if (($self->{current} = $widget)) {
4098 $self->{current}->set_visible if $self->{current} && $self->{visible};
3968 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4099 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3969 4100
3970 $self->emit (page_changed => $self->{current}); 4101 $self->emit (page_changed => $self->{current});
4102 }
3971 4103
3972 $self->realloc; 4104 $self->realloc;
3973} 4105}
3974 4106
3975sub visible_children { 4107sub visible_children {
3976 $_[0]{current} 4108 $_[0]{current} || ()
3977} 4109}
3978 4110
3979sub size_request { 4111sub size_request {
3980 my ($self) = @_; 4112 my ($self) = @_;
3981 4113
4114 $self->{current}
3982 $self->{current}->size_request 4115 ? $self->{current}->size_request
4116 : (0, 0)
3983} 4117}
3984 4118
3985sub invoke_size_allocate { 4119sub invoke_size_allocate {
3986 my ($self, $w, $h) = @_; 4120 my ($self, $w, $h) = @_;
3987 4121
3988 $self->{current}->configure (0, 0, $w, $h); 4122 $self->{current}->configure (0, 0, $w, $h)
4123 if $self->{current};
3989 4124
3990 1 4125 1
3991} 4126}
3992 4127
3993sub _draw { 4128sub _draw {
3994 my ($self) = @_; 4129 my ($self) = @_;
3995 4130
3996 $self->{current}->draw; 4131 $self->{current}->draw
4132 if $self->{current};
3997} 4133}
3998 4134
3999############################################################################# 4135#############################################################################
4000 4136
4001package DC::UI::Notebook; 4137package DC::UI::Notebook;
4211 $self 4347 $self
4212} 4348}
4213 4349
4214sub reorder { 4350sub reorder {
4215 my ($self) = @_; 4351 my ($self) = @_;
4216 my $NOW = Time::HiRes::time; 4352 my $NOW = AE::time;
4217 4353
4218 # freeze display when hovering over any label 4354 # freeze display when hovering over any label
4219 return if $DC::UI::TOOLTIP->{owner} 4355 return if $DC::UI::TOOLTIP->{owner}
4220 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4356 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4221 values %{ $self->{item} }; 4357 values %{ $self->{item} };
4271 $label->{fg}[3] = $item->{fg}[3] || 1; 4407 $label->{fg}[3] = $item->{fg}[3] || 1;
4272 } 4408 }
4273 4409
4274 push @widgets, $label; 4410 push @widgets, $label;
4275 } 4411 }
4412
4413 my $hash = join ",", @widgets;
4414 return if $hash eq $self->{last_widget_hash};
4415 $self->{last_widget_hash} = $hash;
4276 4416
4277 $self->clear; 4417 $self->clear;
4278 $self->SUPER::add (reverse @widgets); 4418 $self->SUPER::add (reverse @widgets);
4279} 4419}
4280 4420
4316 $ROOT->on_refresh (reorder => sub { 4456 $ROOT->on_refresh (reorder => sub {
4317 $self->reorder; 4457 $self->reorder;
4318 }); 4458 });
4319} 4459}
4320 4460
4461sub clr_group {
4462 my ($self, $group) = @_;
4463
4464 if (delete $self->{item}{$group}) {
4465 $ROOT->on_refresh (reorder => sub {
4466 $self->reorder;
4467 });
4468 }
4469}
4470
4321sub reconfigure { 4471sub reconfigure {
4322 my ($self) = @_; 4472 my ($self) = @_;
4323 4473
4324 delete $_->{label} 4474 delete $_->{label}
4325 for values %{ $self->{item} || {} }; 4475 for values %{ $self->{item} || {} };
4479 while () { 4629 while () {
4480 @queue or last outer; 4630 @queue or last outer;
4481 4631
4482 $widget = pop @{ $queue[-1] || [] } 4632 $widget = pop @{ $queue[-1] || [] }
4483 and last; 4633 and last;
4484 4634
4485 pop @queue; 4635 pop @queue;
4486 } 4636 }
4487 4637
4488 delete $queue{$widget+0}; 4638 delete $queue{$widget+0};
4489 4639
4585 4735
4586$ROOT = new DC::UI::Root; 4736$ROOT = new DC::UI::Root;
4587$TOOLTIP = new DC::UI::Tooltip z => 900; 4737$TOOLTIP = new DC::UI::Tooltip z => 900;
4588 4738
45891 47391
4590

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines