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.456 by root, Fri Dec 28 11:44:02 2007 UTC vs.
Revision 1.477 by root, Sun Jan 11 23:50:27 2009 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7
8use Guard ();
7 9
8use DC; 10use DC;
9use DC::Pod; 11use DC::Pod;
10use DC::Texture; 12use DC::Texture;
11 13
189# call when resolution changes etc. 191# call when resolution changes etc.
190sub rescale_widgets { 192sub rescale_widgets {
191 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
192 194
193 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 199
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
286sub set_visible { 288sub set_visible {
287 my ($self) = @_; 289 my ($self) = @_;
288 290
289 return if $self->{visible}; 291 return if $self->{visible};
290 292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
295
291 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
293 298
294 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
295 300
296 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
297 302
298 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
299} 304}
300 305
301sub set_invisible { 306sub set_invisible {
302 my ($self) = @_; 307 my ($self) = @_;
303 308
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 { 375sub get_max_wh {
371 my ($self) = @_; 376 my ($self) = @_;
372 377
378 my ($w, $h) = @$self{qw(max_w max_h)};
379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
373 return $self->{parent}->get_max_wh 382 ? $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT) 383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
377} 390}
378 391
379sub size_request { 392sub size_request {
380 require Carp; 393 require Carp;
381 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
519sub connect { 532sub connect {
520 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
521 534
522 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
523 536
524 defined wantarray and DC::guard { 537 defined wantarray and Guard::guard {
525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
526 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
527 } 540 }
528} 541}
529 542
641 $class->SUPER::new ( 654 $class->SUPER::new (
642 #bg => [0, 0, 0, 0.2], 655 #bg => [0, 0, 0, 0.2],
643 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
644 @_ 657 @_
645 ) 658 )
659}
660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
646} 666}
647 667
648sub _draw { 668sub _draw {
649 my ($self) = @_; 669 my ($self) = @_;
650 670
1241 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1242 1262
1243 if ((exists $arg{label}) && !ref $arg{label}) { 1263 if ((exists $arg{label}) && !ref $arg{label}) {
1244 $arg{label} = new DC::UI::Label 1264 $arg{label} = new DC::UI::Label
1245 align => 1, 1265 align => 1,
1246 valign => 0, 1266 valign => 0.5,
1247 text => $arg{label}, 1267 text => $arg{label},
1248 fontsize => ($arg{border} || 0.8) * 0.75; 1268 fontsize => ($arg{border} || 0.8) * 0.75;
1249 } 1269 }
1250 1270
1251 my $self = $class->SUPER::new ( 1271 my $self = $class->SUPER::new (
1252 # label => "", 1272 # label => "",
1253 fg => [0.6, 0.3, 0.1], 1273 fg => undef,
1254 border => 0.8, 1274 border => 0.8,
1255 style => 'single', 1275 style => 'single',
1256 %arg, 1276 %arg,
1257 ); 1277 );
1258 1278
1311 my $border = $self->border; 1331 my $border = $self->border;
1312 my ($w, $h) = ($self->{w}, $self->{h}); 1332 my ($w, $h) = ($self->{w}, $self->{h});
1313 1333
1314 $child->draw; 1334 $child->draw;
1315 1335
1316 glColor @{$self->{fg}}; 1336 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1317 glBegin GL_LINE_STRIP; 1337 glBegin GL_LINE_STRIP;
1318 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1338 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1320 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;
1321 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;
1336our @ISA = DC::UI::Bin::; 1356our @ISA = DC::UI::Bin::;
1337 1357
1338use DC::OpenGL; 1358use DC::OpenGL;
1339 1359
1340my $bg = 1360my $bg =
1341 new_from_file DC::Texture DC::find_rcfile "d1_bg.png", 1361 new_from_resource DC::Texture "d1_bg.png",
1342 mipmap => 1, wrap => 1; 1362 mipmap => 1, wrap => 1;
1343 1363
1344my @border = 1364my @border =
1345 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 1365 map { new_from_resource DC::Texture $_, mipmap => 1 }
1346 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1366 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1347 1367
1348my @icon = 1368my @icon =
1349 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 1369 map { new_from_resource DC::Texture $_, mipmap => 1 }
1350 qw(x1_move.png x1_resize.png); 1370 qw(x1_move.png x1_resize.png);
1351 1371
1352sub new { 1372sub new {
1353 my ($class, %arg) = @_; 1373 my ($class, %arg) = @_;
1354 1374
1355 my $self = $class->SUPER::new ( 1375 my $self = $class->SUPER::new (
1356 bg => [1, 1, 1, 1], 1376 bg => [1, 1, 1, 1],
1357 border_bg => [1, 1, 1, 1], 1377 border_bg => [1, 1, 1, 1],
1358 border => 0.6, 1378 border => 0.8,
1359 can_events => 1, 1379 can_events => 1,
1360 min_w => 64, 1380 min_w => 64,
1361 min_h => 32, 1381 min_h => 32,
1362 %arg, 1382 %arg,
1363 ); 1383 );
1364 1384
1365 $self->{title_widget} = new DC::UI::Label 1385 $self->{title_widget} = new DC::UI::Label
1366 align => 0, 1386 align => 0.5,
1367 valign => 1, 1387 valign => 1,
1368 text => $self->{title}, 1388 text => $self->{title},
1369 fontsize => $self->{border}, 1389 fontsize => $self->{border},
1370 if exists $self->{title}; 1390 if exists $self->{title};
1371 1391
1532 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1534 1554
1535 my $border = $self->border; 1555 my $border = $self->border;
1536 1556
1557 if ($border) {
1537 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1541 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1542 1563
1543 # move 1564 # move
1544 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1545 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1546 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1547 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1548 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1549 1570
1550 # resize 1571 # resize
1551 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1553 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1554 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1555 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1556 1578
1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1558 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1559 1581
1560 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
1889 #max_w => maximum pixel width 1911 #max_w => maximum pixel width
1890 #style => 0, # render flags 1912 #style => 0, # render flags
1891 ellipsise => 3, # end 1913 ellipsise => 3, # end
1892 layout => (new DC::Layout), 1914 layout => (new DC::Layout),
1893 fontsize => 1, 1915 fontsize => 1,
1894 align => -1, 1916 align => 0.5,
1895 valign => -1, 1917 valign => 0.5,
1896 padding_x => 2, 1918 padding_x => 4,
1897 padding_y => 2, 1919 padding_y => 2,
1898 can_events => 0, 1920 can_events => 0,
1899 %arg 1921 %arg
1900 ); 1922 );
1901 1923
1967 1989
1968 $self->{size_req} ||= do { 1990 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh; 1991 my ($max_w, $max_h) = $self->get_max_wh;
1970 1992
1971 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1993 $self->{layout}->set_font ($self->{font}) if $self->{font};
1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1994 $self->{layout}->set_width ($max_w);
1973 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1995 $self->{layout}->set_ellipsise ($self->{ellipsise});
1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1996 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1976 1998
1977 my ($w, $h) = $self->{layout}->size; 1999 my ($w, $h) = $self->{layout}->size;
1978 2000
1979 if (exists $self->{template}) { 2001 if (exists $self->{template}) {
1980 $self->{template}->set_font ($self->{font}) if $self->{font}; 2002 $self->{template}->set_font ($self->{font}) if $self->{font};
1981 $self->{template}->set_width ($self->{max_w} || -1); 2003 $self->{template}->set_width ($max_w);
1982 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2004 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1983 2005
1984 my ($w2, $h2) = $self->{template}->size; 2006 my ($w2, $h2) = $self->{template}->size;
1985 2007
1986 $w = List::Util::max $w, $w2; 2008 $w = List::Util::max $w, $w2;
2042 2064
2043 [$self->{layout}->size] 2065 [$self->{layout}->size]
2044 }; 2066 };
2045 2067
2046 unless (exists $self->{ox}) { 2068 unless (exists $self->{ox}) {
2047 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2069 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2048 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2070 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2049 : ($self->{w} - $size->[0]) * 0.5);
2050
2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2053 : ($self->{h} - $size->[1]) * 0.5);
2054 2071
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2056 }; 2073 };
2057 2074
2058# unless ($self->{list}) { 2075# unless ($self->{list}) {
2087 my $class = shift; 2104 my $class = shift;
2088 2105
2089 $class->SUPER::new ( 2106 $class->SUPER::new (
2090 fg => [1, 1, 1], 2107 fg => [1, 1, 1],
2091 bg => [0, 0, 0, 0.2], 2108 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1], 2109 outline => undef,
2093 active_bg => [0, 0, 1, .2], 2110 active_bg => [0, 0, 1, .2],
2094 active_fg => [1, 1, 1], 2111 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0], 2112 active_outline => [1, 1, 0],
2096 can_hover => 1, 2113 can_hover => 1,
2097 can_focus => 1, 2114 can_focus => 1,
2115 align => 0,
2098 valign => 0, 2116 valign => 0.5,
2099 can_events => 1, 2117 can_events => 1,
2100 ellipsise => 0, 2118 ellipsise => 0,
2101 padding_x => 4, 2119 padding_x => 4,
2102 padding_y => 2, 2120 padding_y => 2,
2103 #text => ... 2121 #text => ...
2154 2172
2155 my $text = $self->get_text; 2173 my $text = $self->get_text;
2156 2174
2157 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2175 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2158 2176
2159 if ($uni == 8) { 2177 if ($sym == DC::SDLK_BACKSPACE) {
2160 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2178 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2161 } elsif ($uni == 127) { 2179 } elsif ($sym == DC::SDLK_DELETE) {
2162 substr $text, $self->{cursor}, 1, ""; 2180 substr $text, $self->{cursor}, 1, "";
2163 } elsif ($sym == DC::SDLK_LEFT) { 2181 } elsif ($sym == DC::SDLK_LEFT) {
2164 --$self->{cursor} if $self->{cursor}; 2182 --$self->{cursor} if $self->{cursor};
2165 } elsif ($sym == DC::SDLK_RIGHT) { 2183 } elsif ($sym == DC::SDLK_RIGHT) {
2166 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2184 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2269 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2270 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;
2271 glLineWidth 1; 2289 glLineWidth 1;
2272 2290
2273 } else { 2291 } else {
2274 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2275 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2276 glVertex .5, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2277 glVertex .5, $self->{h} - 2.5; 2295 glVertex .5, $self->{h} - 2.5;
2278 glVertex $self->{w} - .5, $self->{h} - 2.5; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2279 glVertex $self->{w} - .5, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
2286package DC::UI::Entry; 2304package DC::UI::Entry;
2287 2305
2288our @ISA = DC::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2289 2307
2290use DC::OpenGL; 2308use DC::OpenGL;
2309
2310sub new {
2311 my $class = shift;
2312
2313 $class->SUPER::new (
2314 history_pointer => -1,
2315 @_
2316 )
2317}
2318
2291 2319
2292sub invoke_key_down { 2320sub invoke_key_down {
2293 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2294 2322
2295 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2320 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2321 2349
2322 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2323 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2324 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2325 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2326 } 2357 }
2327 2358
2328 } else { 2359 } else {
2329 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2330 } 2361 }
2351} 2382}
2352 2383
2353sub move_cursor_ver { 2384sub move_cursor_ver {
2354 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2355 2386
2356 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2357 2388
2358 $y += $dy; 2389 $line += $dy;
2359 2390
2360 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2391 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2361 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2362 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2363 $self->update; 2394 $self->update;
2364 return; 2395 return;
2365 } 2396 }
2388our @ISA = DC::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2389 2420
2390use DC::OpenGL; 2421use DC::OpenGL;
2391 2422
2392my @tex = 2423my @tex =
2393 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2394 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2395 2426
2396sub new { 2427sub new {
2397 my $class = shift; 2428 my $class = shift;
2398 2429
2399 $class->SUPER::new ( 2430 $class->SUPER::new (
2400 can_hover => 1, 2431 can_hover => 1,
2401 align => 0, 2432 align => 0.5,
2402 valign => 0, 2433 valign => 0.5,
2403 can_events => 1, 2434 can_events => 1,
2404 @_ 2435 @_
2405 ) 2436 )
2406} 2437}
2407 2438
2437our @ISA = DC::UI::Label::; 2468our @ISA = DC::UI::Label::;
2438 2469
2439use DC::OpenGL; 2470use DC::OpenGL;
2440 2471
2441my @tex = 2472my @tex =
2442 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2443 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2444 2475
2445sub new { 2476sub new {
2446 my $class = shift; 2477 my $class = shift;
2447 2478
2449 padding_x => 8, 2480 padding_x => 8,
2450 padding_y => 4, 2481 padding_y => 4,
2451 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2452 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2453 can_hover => 1, 2484 can_hover => 1,
2454 align => 0, 2485 align => 0.5,
2455 valign => 0, 2486 valign => 0.5,
2456 can_events => 1, 2487 can_events => 1,
2457 @_ 2488 @_
2458 ) 2489 )
2459} 2490}
2460 2491
2490package DC::UI::CheckBox; 2521package DC::UI::CheckBox;
2491 2522
2492our @ISA = DC::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2493 2524
2494my @tex = 2525my @tex =
2495 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2496 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2497 2528
2498use DC::OpenGL; 2529use DC::OpenGL;
2499 2530
2500sub new { 2531sub new {
2501 my $class = shift; 2532 my $class = shift;
2502 2533
2503 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2504 padding_x => 2, 2536 padding_x => 2,
2505 padding_y => 2, 2537 padding_y => 2,
2506 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2507 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2508 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2514} 2546}
2515 2547
2516sub size_request { 2548sub size_request {
2517 my ($self) = @_; 2549 my ($self) = @_;
2518 2550
2519 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2520} 2552}
2521 2553
2522sub toggle { 2554sub toggle {
2523 my ($self) = @_; 2555 my ($self) = @_;
2524 2556
2581 2613
2582 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2583 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2584 2616
2585 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2586 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2587 2619
2588 DC::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2589 2621
2590 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2591 2623
2652 padding_x => 4, 2684 padding_x => 4,
2653 padding_y => 4, 2685 padding_y => 4,
2654 fg => [1, 1, 1], 2686 fg => [1, 1, 1],
2655 active_fg => [0, 0, 1], 2687 active_fg => [0, 0, 1],
2656 can_hover => 1, 2688 can_hover => 1,
2657 align => 0, 2689 align => 0.5,
2658 valign => 0, 2690 valign => 0.5,
2659 can_events => 1, 2691 can_events => 1,
2660 @_ 2692 @_
2661 ); 2693 );
2662} 2694}
2663 2695
2687 2719
2688use DC::OpenGL; 2720use DC::OpenGL;
2689 2721
2690my %tex = ( 2722my %tex = (
2691 food => [ 2723 food => [
2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2693 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2725 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2694 ], 2726 ],
2695 grace => [ 2727 grace => [
2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2728 map { new_from_resource DC::Texture $_, mipmap => 1 }
2697 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2729 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2698 ], 2730 ],
2699 hp => [ 2731 hp => [
2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2701 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2733 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2702 ], 2734 ],
2703 mana => [ 2735 mana => [
2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2736 map { new_from_resource DC::Texture $_, mipmap => 1 }
2705 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2737 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2706 ], 2738 ],
2707); 2739);
2708 2740
2709# eg. VGauge->new (gauge => 'food'), default gauge: food 2741# eg. VGauge->new (gauge => 'food'), default gauge: food
2822 2854
2823sub new { 2855sub new {
2824 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2825 2857
2826 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2827 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2828 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2829 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2830 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2831 fontsize => 0.9, 2865 fontsize => 0.9,
2832 valign => 0, 2866 valign => 0.5,
2833 align => 0, 2867 align => 0.5,
2834 can_events => 1, 2868 can_events => 1,
2835 ellipsise => 1, 2869 ellipsise => 1,
2836 label => "%d%%", 2870 label => "%d%%",
2837 %arg, 2871 %arg,
2838 ); 2872 );
2871 my ($self) = @_; 2905 my ($self) = @_;
2872 2906
2873 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2874 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2875 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2876 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2877 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2878 2915
2879 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2880 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2881 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2882 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2883 } 2920 }
2884 2921
2885 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2886 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2887 2928
2888 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2889 2930
2890 { 2931 {
2891 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2900our @ISA = DC::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2901 2942
2902sub new { 2943sub new {
2903 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2904 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2905 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2906 tooltip => sub { 2950 tooltip => sub {
2907 my ($self) = @_; 2951 my ($self) = @_;
2908 2952
2909 sprintf "level %d\n%s points\n%s next level\n%s to go", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2954 $tt,
2910 $self->{lvl}, 2955 $self->{lvl},
2911 ::formsep ($self->{exp}), 2956 ::formsep ($self->{exp}),
2912 ::formsep ($self->{nxt}), 2957 ::formsep ($self->{nxt}),
2913 ::formsep ($self->{nxt} - $self->{exp}), 2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2914 }, 2960 },
2915 %arg
2916 ); 2961 );
2917 2962
2918 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2919 if $::CONN; 2964 if $::CONN;
2920 2965
2928 if $::CONN; 2973 if $::CONN;
2929 2974
2930 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2931} 2976}
2932 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2933sub set_value { 2992sub set_value {
2934 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2935 2994
2936 $self->{lvl} = $lvl; 2995 $self->{lvl} = $lvl;
2937 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2938 2997
2939 my $v = -1;
2940
2941 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2942 my $l0 = $table->[$lvl - 1];
2943 my $l1 = $table->[$lvl];
2944
2945 $self->{nxt} = $l1;
2946
2947 $v = ($exp - $l0) / ($l1 - $l0);
2948 }
2949
2950 $self->SUPER::set_value ($v); 2998 $self->SUPER::set_value ($self->_percent);
2951} 2999}
2952 3000
2953############################################################################# 3001#############################################################################
2954 3002
2955package DC::UI::Gauge; 3003package DC::UI::Gauge;
2964 can_hover => 1, 3012 can_hover => 1,
2965 can_events => 1, 3013 can_events => 1,
2966 %arg, 3014 %arg,
2967 ); 3015 );
2968 3016
2969 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999"); 3017 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2970 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3018 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2971 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999"); 3019 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2972 3020
2973 $self 3021 $self
2974} 3022}
2975 3023
2976sub set_fontsize { 3024sub set_fontsize {
3006use DC::OpenGL; 3054use DC::OpenGL;
3007 3055
3008our @ISA = DC::UI::DrawBG::; 3056our @ISA = DC::UI::DrawBG::;
3009 3057
3010my @tex = 3058my @tex =
3011 map { new_from_file DC::Texture DC::find_rcfile $_ } 3059 map { new_from_resource DC::Texture $_ }
3012 qw(s1_slider.png s1_slider_bg.png); 3060 qw(s1_slider.png s1_slider_bg.png);
3013 3061
3014sub new { 3062sub new {
3015 my $class = shift; 3063 my $class = shift;
3016 3064
3053sub set_value { 3101sub set_value {
3054 my ($self, $value) = @_; 3102 my ($self, $value) = @_;
3055 3103
3056 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3104 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3057 3105
3058 $hi = $lo + 1 if $hi <= $lo; 3106 $hi = $lo if $hi < $lo;
3059 3107
3060 $page = $hi - $lo if $page > $hi - $lo; 3108 $value = $hi - $page if $value > $hi - $page;
3061
3062 $value = $lo if $value < $lo; 3109 $value = $lo if $value < $lo;
3063 $value = $hi - $page if $value > $hi - $page;
3064 3110
3065 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3111 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3066 if $unit; 3112 if $unit;
3067 3113
3068 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3114 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3132 my ($self) = @_; 3178 my ($self) = @_;
3133 3179
3134 unless ($self->{knob_w}) { 3180 unless ($self->{knob_w}) {
3135 $self->set_value ($self->{range}[0]); 3181 $self->set_value ($self->{range}[0]);
3136 3182
3137 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3183 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3138 my $range = ($hi - $page - $lo) || 1e-100; 3184 my $range = ($hi - $page - $lo) || 1e-10;
3139 3185
3140 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3186 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3141 3187
3142 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3188 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3143 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3189 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3144 3190
3145 $value = ($value - $lo) / $range; 3191 $value = ($value - $lo) / $range;
3304 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3350 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3305 $layout->set_markup ($para->{markup}); 3351 $layout->set_markup ($para->{markup});
3306 3352
3307 $layout->set_shapes ( 3353 $layout->set_shapes (
3308 map 3354 map
3309 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3355 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3310 @{$para->{widget}} 3356 @{$para->{widget}}
3311 ); 3357 );
3312 3358
3313 $layout 3359 $layout
3314} 3360}
3605 3651
3606 $tip =~ s/^\n+//; 3652 $tip =~ s/^\n+//;
3607 $tip =~ s/\n+$//; 3653 $tip =~ s/\n+$//;
3608 3654
3609 $self->add (new DC::UI::Label 3655 $self->add (new DC::UI::Label
3656 fg => $DC::THEME{tooltip_fg},
3610 markup => $tip, 3657 markup => $tip,
3611 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3658 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3659 align => 0,
3612 fontsize => 0.8, 3660 fontsize => 0.8,
3613 style => 1, # FLAG_INVERSE 3661 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3614 ellipsise => 0, 3662 ellipsise => 0,
3615 font => ($widget->{tooltip_font} || $::FONT_PROP), 3663 font => ($widget->{tooltip_font} || $::FONT_PROP),
3616 ); 3664 );
3617} 3665}
3618 3666
3655sub _draw { 3703sub _draw {
3656 my ($self) = @_; 3704 my ($self) = @_;
3657 3705
3658 my ($w, $h) = @$self{qw(w h)}; 3706 my ($w, $h) = @$self{qw(w h)};
3659 3707
3660 glColor 1, 0.8, 0.4; 3708 glColor @{ $DC::THEME{tooltip_bg} };
3661 glRect 0, 0, $w, $h; 3709 glRect 0, 0, $w, $h;
3662 3710
3663 glColor 0, 0, 0; 3711 glColor @{ $DC::THEME{tooltip_border} };
3664 glRect_lineloop .5, .5, $w + .5, $h + .5; 3712 glRect_lineloop .5, .5, $w + .5, $h + .5;
3665 3713
3666 glTranslate 2, 2; 3714 glTranslate 2, 2;
3667 3715
3668 $self->SUPER::_draw; 3716 $self->SUPER::_draw;
3685 aspect => 1, 3733 aspect => 1,
3686 can_events => 0, 3734 can_events => 0,
3687 @_, 3735 @_,
3688 ); 3736 );
3689 3737
3690 if ($self->{anim} && $self->{animspeed}) {
3691 DC::weaken (my $widget = $self);
3692
3693 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3694 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3695 return unless $::CONN;
3696
3697 my $w = $widget
3698 or return;
3699
3700 ++$w->{frame};
3701 $w->update_face;
3702
3703 # somehow, $widget can go away
3704 $w->update;
3705 $w->update_timer;
3706 };
3707
3708 $self->update_face;
3709 $self->update_timer; 3738 $self->update_anim;
3710 }
3711 3739
3712 $self 3740 $self
3713} 3741}
3714 3742
3715sub update_timer { 3743sub update_timer {
3738 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3739 } 3767 }
3740 } 3768 }
3741 } 3769 }
3742 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3743 } 3799 }
3744} 3800}
3745 3801
3746sub size_request { 3802sub size_request {
3747 my ($self) = @_; 3803 my ($self) = @_;
3769 return unless $self->{visible}; 3825 return unless $self->{visible};
3770 3826
3771 $self->SUPER::update; 3827 $self->SUPER::update;
3772} 3828}
3773 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3774sub invoke_visibility_change { 3851sub invoke_visibility_change {
3775 my ($self) = @_; 3852 my ($self) = @_;
3776 3853
3777 $self->update_timer; 3854 $self->update_timer;
3778 3855
3840 $widget = new DC::UI::HBox 3917 $widget = new DC::UI::HBox
3841 can_hover => 1, 3918 can_hover => 1,
3842 can_events => 1, 3919 can_events => 1,
3843 tooltip => $tooltip, 3920 tooltip => $tooltip,
3844 children => [ 3921 children => [
3845 (new DC::UI::Label markup => $left, expand => 1), 3922 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3846 (new DC::UI::Label markup => $right, align => +1), 3923 (new DC::UI::Label markup => $right, align => 1),
3847 ], 3924 ],
3848 ; 3925 ;
3849 3926
3850 } else { 3927 } else {
3851 $widget = new DC::UI::Label 3928 $widget = new DC::UI::Label
3852 can_hover => 1, 3929 can_hover => 1,
3853 can_events => 1, 3930 can_events => 1,
3931 align => 0,
3854 markup => $widget, 3932 markup => $widget,
3855 tooltip => $tooltip; 3933 tooltip => $tooltip;
3856 } 3934 }
3857 } 3935 }
3858 3936
3873 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3874 $GRAB = $self; 3952 $GRAB = $self;
3875 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3876 3954
3877 $self->show; 3955 $self->show;
3878 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3879} 3965}
3880 3966
3881sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3882 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3883 3969
3915 4001
3916 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3917 @_, 4003 @_,
3918 ); 4004 );
3919 4005
3920 $self->{current} = $self->{children}[0] 4006 $self->set_current_page (0);
3921 if @{ $self->{children} };
3922 4007
3923 $self 4008 $self
3924} 4009}
3925 4010
3926sub add { 4011sub add {
3927 my ($self, @widgets) = @_; 4012 my ($self, @widgets) = @_;
3928 4013
3929 $self->SUPER::add (@widgets); 4014 $self->SUPER::add (@widgets);
3930 4015
3931 $self->{current} = $self->{children}[0] 4016 $self->set_current_page (0)
3932 if @{ $self->{children} }; 4017 if @widgets == @{ $self->{children} };
3933} 4018}
3934 4019
3935sub get_current_page { 4020sub get_current_page {
3936 my ($self) = @_; 4021 my ($self) = @_;
3937 4022
3943 4028
3944 my $widget = ref $page_or_widget 4029 my $widget = ref $page_or_widget
3945 ? $page_or_widget 4030 ? $page_or_widget
3946 : $self->{children}[$page_or_widget]; 4031 : $self->{children}[$page_or_widget];
3947 4032
4033 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4034
3948 $self->{current} = $widget; 4035 if (($self->{current} = $widget)) {
4036 $self->{current}->set_visible if $self->{current} && $self->{visible};
3949 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4037 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3950 4038
3951 $self->emit (page_changed => $self->{current}); 4039 $self->emit (page_changed => $self->{current});
4040 }
3952 4041
3953 $self->realloc; 4042 $self->realloc;
3954} 4043}
3955 4044
3956sub visible_children { 4045sub visible_children {
3957 $_[0]{current} 4046 $_[0]{current} || ()
3958} 4047}
3959 4048
3960sub size_request { 4049sub size_request {
3961 my ($self) = @_; 4050 my ($self) = @_;
3962 4051
4052 $self->{current}
3963 $self->{current}->size_request 4053 ? $self->{current}->size_request
4054 : (0, 0)
3964} 4055}
3965 4056
3966sub invoke_size_allocate { 4057sub invoke_size_allocate {
3967 my ($self, $w, $h) = @_; 4058 my ($self, $w, $h) = @_;
3968 4059
3969 $self->{current}->configure (0, 0, $w, $h); 4060 $self->{current}->configure (0, 0, $w, $h)
4061 if $self->{current};
3970 4062
3971 1 4063 1
3972} 4064}
3973 4065
3974sub _draw { 4066sub _draw {
3975 my ($self) = @_; 4067 my ($self) = @_;
3976 4068
3977 $self->{current}->draw; 4069 $self->{current}->draw
4070 if $self->{current};
3978} 4071}
3979 4072
3980############################################################################# 4073#############################################################################
3981 4074
3982package DC::UI::Notebook; 4075package DC::UI::Notebook;
4049} 4142}
4050 4143
4051sub pages { 4144sub pages {
4052 my ($self) = @_; 4145 my ($self) = @_;
4053 $self->{multiplexer}->children 4146 $self->{multiplexer}->children
4147}
4148
4149sub page_index {
4150 my ($self, $widget) = @_;
4151
4152 my $i = 0;
4153 for ($self->pages) {
4154 if ($_ eq $widget) { return $i };
4155 $i++;
4156 }
4157
4158 undef
4054} 4159}
4055 4160
4056sub add_tab { 4161sub add_tab {
4057 my ($self, $title, $widget, $tooltip) = @_; 4162 my ($self, $title, $widget, $tooltip) = @_;
4058 4163
4180 $self 4285 $self
4181} 4286}
4182 4287
4183sub reorder { 4288sub reorder {
4184 my ($self) = @_; 4289 my ($self) = @_;
4185 my $NOW = Time::HiRes::time; 4290 my $NOW = EV::time;
4186 4291
4187 # freeze display when hovering over any label 4292 # freeze display when hovering over any label
4188 return if $DC::UI::TOOLTIP->{owner} 4293 return if $DC::UI::TOOLTIP->{owner}
4189 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4294 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4190 values %{ $self->{item} }; 4295 values %{ $self->{item} };
4222 tooltip => $item->{tooltip}, 4327 tooltip => $item->{tooltip},
4223 tooltip_font => $::FONT_PROP, 4328 tooltip_font => $::FONT_PROP,
4224 tooltip_width => 0.67, 4329 tooltip_width => 0.67,
4225 fontsize => $item->{fontsize} || $self->{fontsize}, 4330 fontsize => $item->{fontsize} || $self->{fontsize},
4226 max_w => $::WIDTH * 0.44, 4331 max_w => $::WIDTH * 0.44,
4332 align => 0,
4227 fg => [@{ $item->{fg} }], 4333 fg => [@{ $item->{fg} }],
4228 can_events => 1, 4334 can_events => 1,
4229 can_hover => 1 4335 can_hover => 1
4230 }; 4336 };
4231 4337
4239 $label->{fg}[3] = $item->{fg}[3] || 1; 4345 $label->{fg}[3] = $item->{fg}[3] || 1;
4240 } 4346 }
4241 4347
4242 push @widgets, $label; 4348 push @widgets, $label;
4243 } 4349 }
4350
4351 my $hash = join ",", @widgets;
4352 return if $hash eq $self->{last_widget_hash};
4353 $self->{last_widget_hash} = $hash;
4244 4354
4245 $self->clear; 4355 $self->clear;
4246 $self->SUPER::add (reverse @widgets); 4356 $self->SUPER::add (reverse @widgets);
4247} 4357}
4248 4358
4455 4565
4456 delete $queue{$widget+0}; 4566 delete $queue{$widget+0};
4457 4567
4458 my ($w, $h) = $widget->size_request; 4568 my ($w, $h) = $widget->size_request;
4459 4569
4460 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4570 $w += $widget->{padding_x} * 2;
4461 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4571 $h += $widget->{padding_y} * 2;
4572
4573 $w = max $widget->{min_w}, $w;
4574 $h = max $widget->{min_h}, $h;
4462 4575
4463 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4576 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4464 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4577 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4465 4578
4466 $w = $widget->{force_w} if exists $widget->{force_w}; 4579 $w = $widget->{force_w} if exists $widget->{force_w};
4546 4659
4547############################################################################# 4660#############################################################################
4548 4661
4549package DC::UI; 4662package DC::UI;
4550 4663
4551$ROOT = new DC::UI::Root; 4664$ROOT = new DC::UI::Root;
4552$TOOLTIP = new DC::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4553 4666
45541 46671
4555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines