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.480 by root, Sat Apr 3 02:58:25 2010 UTC

1package DC::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
6
7use Guard ();
7 8
8use DC; 9use DC;
9use DC::Pod; 10use DC::Pod;
10use DC::Texture; 11use DC::Texture;
11 12
189# call when resolution changes etc. 190# call when resolution changes etc.
190sub rescale_widgets { 191sub rescale_widgets {
191 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
192 193
193 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 196 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 197 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 198
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 199 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 200 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
221 222
222############################################################################# 223#############################################################################
223 224
224package DC::UI::Base; 225package DC::UI::Base;
225 226
226use strict; 227use common::sense;
227 228
228use DC::OpenGL; 229use DC::OpenGL;
229 230
230sub new { 231sub new {
231 my $class = shift; 232 my $class = shift;
286sub set_visible { 287sub set_visible {
287 my ($self) = @_; 288 my ($self) = @_;
288 289
289 return if $self->{visible}; 290 return if $self->{visible};
290 291
292 $self->{parent} && $self->{parent}{root}#d#
293 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
294
291 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
293 297
294 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
295 299
296 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
297 301
298 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
299} 303}
300 304
301sub set_invisible { 305sub set_invisible {
302 my ($self) = @_; 306 my ($self) = @_;
303 307
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
630 642
631package DC::UI::DrawBG; 643package DC::UI::DrawBG;
632 644
633our @ISA = DC::UI::Base::; 645our @ISA = DC::UI::Base::;
634 646
635use strict; 647use common::sense;
648
636use DC::OpenGL; 649use DC::OpenGL;
637 650
638sub new { 651sub new {
639 my $class = shift; 652 my $class = shift;
640 653
643 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
644 @_ 657 @_
645 ) 658 )
646} 659}
647 660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
648sub _draw { 668sub _draw {
649 my ($self) = @_; 669 my ($self) = @_;
650 670
651 my $color = $FOCUS == $self && $self->{active_bg} 671 my $color = $FOCUS == $self
652 ? $self->{active_bg} 672 ? $self->{active_bg} || $self->{bg}
653 : $self->{bg}; 673 : $self->{bg};
654 674
655 if ($color && (@$color < 4 || $color->[3])) { 675 if ($color && (@$color < 4 || $color->[3])) {
656 my ($w, $h) = @$self{qw(w h)}; 676 my ($w, $h) = @$self{qw(w h)};
657 677
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 }
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
2562 2594
2563############################################################################# 2595#############################################################################
2564 2596
2565package DC::UI::Image; 2597package DC::UI::Image;
2566 2598
2567our @ISA = DC::UI::Base::; 2599our @ISA = DC::UI::DrawBG::;
2568 2600
2569use DC::OpenGL; 2601use DC::OpenGL;
2570 2602
2571our %texture_cache; 2603our %texture_cache;
2572 2604
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
2605 my ($self, $cloning, $path) = @_; 2637 my ($self, $cloning, $path) = @_;
2606 2638
2607 $self->new (path => $path) 2639 $self->new (path => $path)
2608} 2640}
2609 2641
2642sub set_texture {
2643 my ($self, $tex) = @_;
2644
2645 $self->{tex} = $tex;
2646 $self->update;
2647}
2648
2610sub size_request { 2649sub size_request {
2611 my ($self) = @_; 2650 my ($self) = @_;
2612 2651
2613 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2652 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2614} 2653}
2615 2654
2616sub _draw { 2655sub _draw {
2617 my ($self) = @_; 2656 my ($self) = @_;
2657
2658 $self->SUPER::_draw;
2618 2659
2619 my $tex = $self->{tex}; 2660 my $tex = $self->{tex};
2620 2661
2621 my ($w, $h) = ($self->{w}, $self->{h}); 2662 my ($w, $h) = ($self->{w}, $self->{h});
2622 2663
2640package DC::UI::ImageButton; 2681package DC::UI::ImageButton;
2641 2682
2642our @ISA = DC::UI::Image::; 2683our @ISA = DC::UI::Image::;
2643 2684
2644use DC::OpenGL; 2685use DC::OpenGL;
2645
2646my %textures;
2647 2686
2648sub new { 2687sub new {
2649 my $class = shift; 2688 my $class = shift;
2650 2689
2651 my $self = $class->SUPER::new ( 2690 my $self = $class->SUPER::new (
2652 padding_x => 4, 2691 padding_x => 4,
2653 padding_y => 4, 2692 padding_y => 4,
2654 fg => [1, 1, 1], 2693 fg => [1, 1, 1],
2655 active_fg => [0, 0, 1], 2694 active_fg => [0, 0, 1],
2656 can_hover => 1, 2695 can_hover => 1,
2657 align => 0, 2696 align => 0.5,
2658 valign => 0, 2697 valign => 0.5,
2659 can_events => 1, 2698 can_events => 1,
2660 @_ 2699 @_
2661 ); 2700 );
2662} 2701}
2663 2702
2687 2726
2688use DC::OpenGL; 2727use DC::OpenGL;
2689 2728
2690my %tex = ( 2729my %tex = (
2691 food => [ 2730 food => [
2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2731 map { new_from_resource DC::Texture $_, mipmap => 1 }
2693 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2732 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2694 ], 2733 ],
2695 grace => [ 2734 grace => [
2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2735 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/ 2736 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2698 ], 2737 ],
2699 hp => [ 2738 hp => [
2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2739 map { new_from_resource DC::Texture $_, mipmap => 1 }
2701 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2740 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2702 ], 2741 ],
2703 mana => [ 2742 mana => [
2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2743 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/ 2744 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2706 ], 2745 ],
2707); 2746);
2708 2747
2709# eg. VGauge->new (gauge => 'food'), default gauge: food 2748# eg. VGauge->new (gauge => 'food'), default gauge: food
2822 2861
2823sub new { 2862sub new {
2824 my ($class, %arg) = @_; 2863 my ($class, %arg) = @_;
2825 2864
2826 my $self = $class->SUPER::new ( 2865 my $self = $class->SUPER::new (
2866 padding_x => 2,
2867 padding_y => 2,
2827 fg => [1, 1, 1], 2868 fg => [1, 1, 1],
2828 bg => [0, 0, 1, 0.2], 2869 bg => [0, 0, 1, 0.2],
2829 bar => [0.7, 0.5, 0.1, 0.8], 2870 bar => [0.7, 0.5, 0.1, 0.8],
2830 outline => [0.4, 0.3, 0], 2871 outline => [0.4, 0.3, 0],
2831 fontsize => 0.9, 2872 fontsize => 0.9,
2832 valign => 0, 2873 valign => 0.5,
2833 align => 0, 2874 align => 0.5,
2834 can_events => 1, 2875 can_events => 1,
2835 ellipsise => 1, 2876 ellipsise => 1,
2836 label => "%d%%", 2877 label => "%d%%",
2837 %arg, 2878 %arg,
2838 ); 2879 );
2871 my ($self) = @_; 2912 my ($self) = @_;
2872 2913
2873 glEnable GL_BLEND; 2914 glEnable GL_BLEND;
2874 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2915 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2875 2916
2917 my $px = $self->{padding_x};
2918 my $py = $self->{padding_y};
2919
2876 if ($self->{value} >= 0) { 2920 if ($self->{value} >= 0) {
2877 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2921 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2878 2922
2879 glColor_premultiply @{$self->{bar}}; 2923 glColor_premultiply @{$self->{bar}};
2880 glRect 2, 2, $s, $self->{h} - 2; 2924 glRect $px, $py, $s, $self->{h} - $py;
2881 glColor_premultiply @{$self->{bg}}; 2925 glColor_premultiply @{$self->{bg}};
2882 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2926 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2883 } 2927 }
2884 2928
2885 glColor_premultiply @{$self->{outline}}; 2929 glColor_premultiply @{$self->{outline}};
2930
2931 $px -= .5;
2932 $py -= .5;
2933
2886 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2934 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2887 2935
2888 glDisable GL_BLEND; 2936 glDisable GL_BLEND;
2889 2937
2890 { 2938 {
2891 local $self->{bg}; # do not draw background 2939 local $self->{bg}; # do not draw background
2900our @ISA = DC::UI::Progress::; 2948our @ISA = DC::UI::Progress::;
2901 2949
2902sub new { 2950sub new {
2903 my ($class, %arg) = @_; 2951 my ($class, %arg) = @_;
2904 2952
2953 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2954
2905 my $self = $class->SUPER::new ( 2955 my $self = $class->SUPER::new (
2956 %arg,
2906 tooltip => sub { 2957 tooltip => sub {
2907 my ($self) = @_; 2958 my ($self) = @_;
2908 2959
2909 sprintf "level %d\n%s points\n%s next level\n%s to go", 2960 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2961 $tt,
2910 $self->{lvl}, 2962 $self->{lvl},
2911 ::formsep ($self->{exp}), 2963 ::formsep ($self->{exp}),
2912 ::formsep ($self->{nxt}), 2964 ::formsep ($self->{nxt}),
2913 ::formsep ($self->{nxt} - $self->{exp}), 2965 ::formsep ($self->{nxt} - $self->{exp}),
2966 $self->_percent * 100,
2914 }, 2967 },
2915 %arg
2916 ); 2968 );
2917 2969
2918 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2970 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2919 if $::CONN; 2971 if $::CONN;
2920 2972
2928 if $::CONN; 2980 if $::CONN;
2929 2981
2930 $self->SUPER::DESTROY; 2982 $self->SUPER::DESTROY;
2931} 2983}
2932 2984
2985sub _percent {
2986 my ($self) = @_;
2987
2988 my $table = $::CONN && $::CONN->{exp_table}
2989 or return -1;
2990
2991 my $l0 = $table->[$self->{lvl} - 1];
2992 my $l1 = $table->[$self->{lvl}];
2993
2994 $self->{nxt} = $l1;
2995
2996 ($self->{exp} - $l0) / ($l1 - $l0)
2997}
2998
2933sub set_value { 2999sub set_value {
2934 my ($self, $lvl, $exp) = @_; 3000 my ($self, $lvl, $exp) = @_;
2935 3001
2936 $self->{lvl} = $lvl; 3002 $self->{lvl} = $lvl;
2937 $self->{exp} = $exp; 3003 $self->{exp} = $exp;
2938 3004
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); 3005 $self->SUPER::set_value ($self->_percent);
2951} 3006}
2952 3007
2953############################################################################# 3008#############################################################################
2954 3009
2955package DC::UI::Gauge; 3010package DC::UI::Gauge;
2964 can_hover => 1, 3019 can_hover => 1,
2965 can_events => 1, 3020 can_events => 1,
2966 %arg, 3021 %arg,
2967 ); 3022 );
2968 3023
2969 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999"); 3024 $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); 3025 $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"); 3026 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2972 3027
2973 $self 3028 $self
2974} 3029}
2975 3030
2976sub set_fontsize { 3031sub set_fontsize {
2999 3054
3000############################################################################# 3055#############################################################################
3001 3056
3002package DC::UI::Slider; 3057package DC::UI::Slider;
3003 3058
3004use strict; 3059use common::sense;
3005 3060
3006use DC::OpenGL; 3061use DC::OpenGL;
3007 3062
3008our @ISA = DC::UI::DrawBG::; 3063our @ISA = DC::UI::DrawBG::;
3009 3064
3010my @tex = 3065my @tex =
3011 map { new_from_file DC::Texture DC::find_rcfile $_ } 3066 map { new_from_resource DC::Texture $_ }
3012 qw(s1_slider.png s1_slider_bg.png); 3067 qw(s1_slider.png s1_slider_bg.png);
3013 3068
3014sub new { 3069sub new {
3015 my $class = shift; 3070 my $class = shift;
3016 3071
3053sub set_value { 3108sub set_value {
3054 my ($self, $value) = @_; 3109 my ($self, $value) = @_;
3055 3110
3056 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3111 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3057 3112
3058 $hi = $lo + 1 if $hi <= $lo; 3113 $hi = $lo if $hi < $lo;
3059 3114
3060 $page = $hi - $lo if $page > $hi - $lo; 3115 $value = $hi - $page if $value > $hi - $page;
3061
3062 $value = $lo if $value < $lo; 3116 $value = $lo if $value < $lo;
3063 $value = $hi - $page if $value > $hi - $page;
3064 3117
3065 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3118 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3066 if $unit; 3119 if $unit;
3067 3120
3068 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3121 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3132 my ($self) = @_; 3185 my ($self) = @_;
3133 3186
3134 unless ($self->{knob_w}) { 3187 unless ($self->{knob_w}) {
3135 $self->set_value ($self->{range}[0]); 3188 $self->set_value ($self->{range}[0]);
3136 3189
3137 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3190 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3138 my $range = ($hi - $page - $lo) || 1e-100; 3191 my $range = ($hi - $page - $lo) || 1e-10;
3139 3192
3140 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3193 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3141 3194
3142 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3195 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3143 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3196 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3144 3197
3145 $value = ($value - $lo) / $range; 3198 $value = ($value - $lo) / $range;
3304 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3357 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3305 $layout->set_markup ($para->{markup}); 3358 $layout->set_markup ($para->{markup});
3306 3359
3307 $layout->set_shapes ( 3360 $layout->set_shapes (
3308 map 3361 map
3309 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3362 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3310 @{$para->{widget}} 3363 @{$para->{widget}}
3311 ); 3364 );
3312 3365
3313 $layout 3366 $layout
3314} 3367}
3605 3658
3606 $tip =~ s/^\n+//; 3659 $tip =~ s/^\n+//;
3607 $tip =~ s/\n+$//; 3660 $tip =~ s/\n+$//;
3608 3661
3609 $self->add (new DC::UI::Label 3662 $self->add (new DC::UI::Label
3663 fg => $DC::THEME{tooltip_fg},
3610 markup => $tip, 3664 markup => $tip,
3611 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3665 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3666 align => 0,
3612 fontsize => 0.8, 3667 fontsize => 0.8,
3613 style => 1, # FLAG_INVERSE 3668 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3614 ellipsise => 0, 3669 ellipsise => 0,
3615 font => ($widget->{tooltip_font} || $::FONT_PROP), 3670 font => ($widget->{tooltip_font} || $::FONT_PROP),
3616 ); 3671 );
3617} 3672}
3618 3673
3655sub _draw { 3710sub _draw {
3656 my ($self) = @_; 3711 my ($self) = @_;
3657 3712
3658 my ($w, $h) = @$self{qw(w h)}; 3713 my ($w, $h) = @$self{qw(w h)};
3659 3714
3660 glColor 1, 0.8, 0.4; 3715 glColor @{ $DC::THEME{tooltip_bg} };
3661 glRect 0, 0, $w, $h; 3716 glRect 0, 0, $w, $h;
3662 3717
3663 glColor 0, 0, 0; 3718 glColor @{ $DC::THEME{tooltip_border} };
3664 glRect_lineloop .5, .5, $w + .5, $h + .5; 3719 glRect_lineloop .5, .5, $w + .5, $h + .5;
3665 3720
3666 glTranslate 2, 2; 3721 glTranslate 2, 2;
3667 3722
3668 $self->SUPER::_draw; 3723 $self->SUPER::_draw;
3685 aspect => 1, 3740 aspect => 1,
3686 can_events => 0, 3741 can_events => 0,
3687 @_, 3742 @_,
3688 ); 3743 );
3689 3744
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; 3745 $self->update_anim;
3710 }
3711 3746
3712 $self 3747 $self
3713} 3748}
3714 3749
3715sub update_timer { 3750sub update_timer {
3738 $tex->upload (sub { $self->reconfigure }); 3773 $tex->upload (sub { $self->reconfigure });
3739 } 3774 }
3740 } 3775 }
3741 } 3776 }
3742 } 3777 }
3778 }
3779}
3780
3781sub update_anim {
3782 my ($self) = @_;
3783
3784 if ($self->{anim} && $self->{animspeed}) {
3785 DC::weaken (my $widget = $self);
3786
3787 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3788 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3789 return unless $::CONN;
3790
3791 my $w = $widget
3792 or return;
3793
3794 ++$w->{frame};
3795 $w->update_face;
3796
3797 # somehow, $widget can go away
3798 $w->update;
3799 $w->update_timer;
3800 };
3801
3802 $self->update_face;
3803 $self->update_timer;
3804 } else {
3805 delete $self->{timer};
3743 } 3806 }
3744} 3807}
3745 3808
3746sub size_request { 3809sub size_request {
3747 my ($self) = @_; 3810 my ($self) = @_;
3769 return unless $self->{visible}; 3832 return unless $self->{visible};
3770 3833
3771 $self->SUPER::update; 3834 $self->SUPER::update;
3772} 3835}
3773 3836
3837sub set_face {
3838 my ($self, $face) = @_;
3839
3840 $self->{face} = $face;
3841 $self->reconfigure;
3842}
3843
3844sub set_anim {
3845 my ($self, $anim) = @_;
3846
3847 $self->{anim} = $anim;
3848 $self->update_anim;
3849}
3850
3851sub set_animspeed {
3852 my ($self, $animspeed) = @_;
3853
3854 $self->{animspeed} = $animspeed;
3855 $self->update_anim;
3856}
3857
3774sub invoke_visibility_change { 3858sub invoke_visibility_change {
3775 my ($self) = @_; 3859 my ($self) = @_;
3776 3860
3777 $self->update_timer; 3861 $self->update_timer;
3778 3862
3840 $widget = new DC::UI::HBox 3924 $widget = new DC::UI::HBox
3841 can_hover => 1, 3925 can_hover => 1,
3842 can_events => 1, 3926 can_events => 1,
3843 tooltip => $tooltip, 3927 tooltip => $tooltip,
3844 children => [ 3928 children => [
3845 (new DC::UI::Label markup => $left, expand => 1), 3929 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3846 (new DC::UI::Label markup => $right, align => +1), 3930 (new DC::UI::Label markup => $right, align => 1),
3847 ], 3931 ],
3848 ; 3932 ;
3849 3933
3850 } else { 3934 } else {
3851 $widget = new DC::UI::Label 3935 $widget = new DC::UI::Label
3852 can_hover => 1, 3936 can_hover => 1,
3853 can_events => 1, 3937 can_events => 1,
3938 align => 0,
3854 markup => $widget, 3939 markup => $widget,
3855 tooltip => $tooltip; 3940 tooltip => $tooltip;
3856 } 3941 }
3857 } 3942 }
3858 3943
3873 # maybe save $GRAB? must be careful about events... 3958 # maybe save $GRAB? must be careful about events...
3874 $GRAB = $self; 3959 $GRAB = $self;
3875 $self->{button} = $ev->{button}; 3960 $self->{button} = $ev->{button};
3876 3961
3877 $self->show; 3962 $self->show;
3878 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3963
3964 my $x = $ev->{x};
3965 my $y = $ev->{y};
3966
3967 $self->{root}->on_post_alloc ($self => sub {
3968 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3969 });
3970
3971 1 # so it can be used inside event handlers
3879} 3972}
3880 3973
3881sub invoke_mouse_motion { 3974sub invoke_mouse_motion {
3882 my ($self, $ev, $x, $y) = @_; 3975 my ($self, $ev, $x, $y) = @_;
3883 3976
3915 4008
3916 my $self = $class->SUPER::new ( 4009 my $self = $class->SUPER::new (
3917 @_, 4010 @_,
3918 ); 4011 );
3919 4012
3920 $self->{current} = $self->{children}[0] 4013 $self->set_current_page (0);
3921 if @{ $self->{children} };
3922 4014
3923 $self 4015 $self
3924} 4016}
3925 4017
3926sub add { 4018sub add {
3927 my ($self, @widgets) = @_; 4019 my ($self, @widgets) = @_;
3928 4020
3929 $self->SUPER::add (@widgets); 4021 $self->SUPER::add (@widgets);
3930 4022
3931 $self->{current} = $self->{children}[0] 4023 $self->set_current_page (0)
3932 if @{ $self->{children} }; 4024 if @widgets == @{ $self->{children} };
3933} 4025}
3934 4026
3935sub get_current_page { 4027sub get_current_page {
3936 my ($self) = @_; 4028 my ($self) = @_;
3937 4029
3943 4035
3944 my $widget = ref $page_or_widget 4036 my $widget = ref $page_or_widget
3945 ? $page_or_widget 4037 ? $page_or_widget
3946 : $self->{children}[$page_or_widget]; 4038 : $self->{children}[$page_or_widget];
3947 4039
4040 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4041
3948 $self->{current} = $widget; 4042 if (($self->{current} = $widget)) {
4043 $self->{current}->set_visible if $self->{current} && $self->{visible};
3949 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4044 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3950 4045
3951 $self->emit (page_changed => $self->{current}); 4046 $self->emit (page_changed => $self->{current});
4047 }
3952 4048
3953 $self->realloc; 4049 $self->realloc;
3954} 4050}
3955 4051
3956sub visible_children { 4052sub visible_children {
3957 $_[0]{current} 4053 $_[0]{current} || ()
3958} 4054}
3959 4055
3960sub size_request { 4056sub size_request {
3961 my ($self) = @_; 4057 my ($self) = @_;
3962 4058
4059 $self->{current}
3963 $self->{current}->size_request 4060 ? $self->{current}->size_request
4061 : (0, 0)
3964} 4062}
3965 4063
3966sub invoke_size_allocate { 4064sub invoke_size_allocate {
3967 my ($self, $w, $h) = @_; 4065 my ($self, $w, $h) = @_;
3968 4066
3969 $self->{current}->configure (0, 0, $w, $h); 4067 $self->{current}->configure (0, 0, $w, $h)
4068 if $self->{current};
3970 4069
3971 1 4070 1
3972} 4071}
3973 4072
3974sub _draw { 4073sub _draw {
3975 my ($self) = @_; 4074 my ($self) = @_;
3976 4075
3977 $self->{current}->draw; 4076 $self->{current}->draw
4077 if $self->{current};
3978} 4078}
3979 4079
3980############################################################################# 4080#############################################################################
3981 4081
3982package DC::UI::Notebook; 4082package DC::UI::Notebook;
4049} 4149}
4050 4150
4051sub pages { 4151sub pages {
4052 my ($self) = @_; 4152 my ($self) = @_;
4053 $self->{multiplexer}->children 4153 $self->{multiplexer}->children
4154}
4155
4156sub page_index {
4157 my ($self, $widget) = @_;
4158
4159 my $i = 0;
4160 for ($self->pages) {
4161 if ($_ eq $widget) { return $i };
4162 $i++;
4163 }
4164
4165 undef
4054} 4166}
4055 4167
4056sub add_tab { 4168sub add_tab {
4057 my ($self, $title, $widget, $tooltip) = @_; 4169 my ($self, $title, $widget, $tooltip) = @_;
4058 4170
4180 $self 4292 $self
4181} 4293}
4182 4294
4183sub reorder { 4295sub reorder {
4184 my ($self) = @_; 4296 my ($self) = @_;
4185 my $NOW = Time::HiRes::time; 4297 my $NOW = EV::time;
4186 4298
4187 # freeze display when hovering over any label 4299 # freeze display when hovering over any label
4188 return if $DC::UI::TOOLTIP->{owner} 4300 return if $DC::UI::TOOLTIP->{owner}
4189 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4301 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4190 values %{ $self->{item} }; 4302 values %{ $self->{item} };
4222 tooltip => $item->{tooltip}, 4334 tooltip => $item->{tooltip},
4223 tooltip_font => $::FONT_PROP, 4335 tooltip_font => $::FONT_PROP,
4224 tooltip_width => 0.67, 4336 tooltip_width => 0.67,
4225 fontsize => $item->{fontsize} || $self->{fontsize}, 4337 fontsize => $item->{fontsize} || $self->{fontsize},
4226 max_w => $::WIDTH * 0.44, 4338 max_w => $::WIDTH * 0.44,
4339 align => 0,
4227 fg => [@{ $item->{fg} }], 4340 fg => [@{ $item->{fg} }],
4228 can_events => 1, 4341 can_events => 1,
4229 can_hover => 1 4342 can_hover => 1
4230 }; 4343 };
4231 4344
4239 $label->{fg}[3] = $item->{fg}[3] || 1; 4352 $label->{fg}[3] = $item->{fg}[3] || 1;
4240 } 4353 }
4241 4354
4242 push @widgets, $label; 4355 push @widgets, $label;
4243 } 4356 }
4357
4358 my $hash = join ",", @widgets;
4359 return if $hash eq $self->{last_widget_hash};
4360 $self->{last_widget_hash} = $hash;
4244 4361
4245 $self->clear; 4362 $self->clear;
4246 $self->SUPER::add (reverse @widgets); 4363 $self->SUPER::add (reverse @widgets);
4247} 4364}
4248 4365
4455 4572
4456 delete $queue{$widget+0}; 4573 delete $queue{$widget+0};
4457 4574
4458 my ($w, $h) = $widget->size_request; 4575 my ($w, $h) = $widget->size_request;
4459 4576
4460 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4577 $w += $widget->{padding_x} * 2;
4461 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4578 $h += $widget->{padding_y} * 2;
4579
4580 $w = max $widget->{min_w}, $w;
4581 $h = max $widget->{min_h}, $h;
4462 4582
4463 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4583 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4464 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4584 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4465 4585
4466 $w = $widget->{force_w} if exists $widget->{force_w}; 4586 $w = $widget->{force_w} if exists $widget->{force_w};
4546 4666
4547############################################################################# 4667#############################################################################
4548 4668
4549package DC::UI; 4669package DC::UI;
4550 4670
4551$ROOT = new DC::UI::Root; 4671$ROOT = new DC::UI::Root;
4552$TOOLTIP = new DC::UI::Tooltip z => 900; 4672$TOOLTIP = new DC::UI::Tooltip z => 900;
4553 4673
45541 46741
4555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines