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.483 by root, Wed Jan 4 11:23:23 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines