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.458 by root, Fri Dec 28 15:05:33 2007 UTC vs.
Revision 1.479 by root, Thu Feb 4 20:01:09 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines