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.457 by root, Fri Dec 28 15:05:20 2007 UTC vs.
Revision 1.481 by root, Tue Oct 12 05:11:38 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 }
2352 2383
2353sub move_cursor_ver { 2384sub move_cursor_ver {
2354 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2355 2386
2356 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2357 warn "cursor $self->{cursor} => $x $line\n";#d#
2358 2388
2359 $line += $dy; 2389 $line += $dy;
2360 2390
2361 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) { 2391 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2362 warn "index $x $line => $index\n";#d#
2363 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2364 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2365 $self->update; 2394 $self->update;
2366 return; 2395 return;
2367 } 2396 }
2390our @ISA = DC::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2391 2420
2392use DC::OpenGL; 2421use DC::OpenGL;
2393 2422
2394my @tex = 2423my @tex =
2395 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2396 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2397 2426
2398sub new { 2427sub new {
2399 my $class = shift; 2428 my $class = shift;
2400 2429
2401 $class->SUPER::new ( 2430 $class->SUPER::new (
2402 can_hover => 1, 2431 can_hover => 1,
2403 align => 0, 2432 align => 0.5,
2404 valign => 0, 2433 valign => 0.5,
2405 can_events => 1, 2434 can_events => 1,
2406 @_ 2435 @_
2407 ) 2436 )
2408} 2437}
2409 2438
2439our @ISA = DC::UI::Label::; 2468our @ISA = DC::UI::Label::;
2440 2469
2441use DC::OpenGL; 2470use DC::OpenGL;
2442 2471
2443my @tex = 2472my @tex =
2444 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2445 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2446 2475
2447sub new { 2476sub new {
2448 my $class = shift; 2477 my $class = shift;
2449 2478
2451 padding_x => 8, 2480 padding_x => 8,
2452 padding_y => 4, 2481 padding_y => 4,
2453 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2454 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2455 can_hover => 1, 2484 can_hover => 1,
2456 align => 0, 2485 align => 0.5,
2457 valign => 0, 2486 valign => 0.5,
2458 can_events => 1, 2487 can_events => 1,
2459 @_ 2488 @_
2460 ) 2489 )
2461} 2490}
2462 2491
2492package DC::UI::CheckBox; 2521package DC::UI::CheckBox;
2493 2522
2494our @ISA = DC::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2495 2524
2496my @tex = 2525my @tex =
2497 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2498 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2499 2528
2500use DC::OpenGL; 2529use DC::OpenGL;
2501 2530
2502sub new { 2531sub new {
2503 my $class = shift; 2532 my $class = shift;
2504 2533
2505 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2506 padding_x => 2, 2536 padding_x => 2,
2507 padding_y => 2, 2537 padding_y => 2,
2508 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2509 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2510 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2516} 2546}
2517 2547
2518sub size_request { 2548sub size_request {
2519 my ($self) = @_; 2549 my ($self) = @_;
2520 2550
2521 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2522} 2552}
2523 2553
2524sub toggle { 2554sub toggle {
2525 my ($self) = @_; 2555 my ($self) = @_;
2526 2556
2564 2594
2565############################################################################# 2595#############################################################################
2566 2596
2567package DC::UI::Image; 2597package DC::UI::Image;
2568 2598
2569our @ISA = DC::UI::Base::; 2599our @ISA = DC::UI::DrawBG::;
2570 2600
2571use DC::OpenGL; 2601use DC::OpenGL;
2572 2602
2573our %texture_cache; 2603our %texture_cache;
2574 2604
2583 2613
2584 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2585 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2586 2616
2587 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2588 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2589 2619
2590 DC::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2591 2621
2592 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2593 2623
2607 my ($self, $cloning, $path) = @_; 2637 my ($self, $cloning, $path) = @_;
2608 2638
2609 $self->new (path => $path) 2639 $self->new (path => $path)
2610} 2640}
2611 2641
2642sub set_texture {
2643 my ($self, $tex) = @_;
2644
2645 $self->{tex} = $tex;
2646 $self->update;
2647}
2648
2612sub size_request { 2649sub size_request {
2613 my ($self) = @_; 2650 my ($self) = @_;
2614 2651
2615 (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})
2616} 2653}
2617 2654
2618sub _draw { 2655sub _draw {
2619 my ($self) = @_; 2656 my ($self) = @_;
2657
2658 $self->SUPER::_draw;
2620 2659
2621 my $tex = $self->{tex}; 2660 my $tex = $self->{tex};
2622 2661
2623 my ($w, $h) = ($self->{w}, $self->{h}); 2662 my ($w, $h) = ($self->{w}, $self->{h});
2624 2663
2642package DC::UI::ImageButton; 2681package DC::UI::ImageButton;
2643 2682
2644our @ISA = DC::UI::Image::; 2683our @ISA = DC::UI::Image::;
2645 2684
2646use DC::OpenGL; 2685use DC::OpenGL;
2647
2648my %textures;
2649 2686
2650sub new { 2687sub new {
2651 my $class = shift; 2688 my $class = shift;
2652 2689
2653 my $self = $class->SUPER::new ( 2690 my $self = $class->SUPER::new (
2654 padding_x => 4, 2691 padding_x => 4,
2655 padding_y => 4, 2692 padding_y => 4,
2656 fg => [1, 1, 1], 2693 fg => [1, 1, 1],
2657 active_fg => [0, 0, 1], 2694 active_fg => [0, 0, 1],
2658 can_hover => 1, 2695 can_hover => 1,
2659 align => 0, 2696 align => 0.5,
2660 valign => 0, 2697 valign => 0.5,
2661 can_events => 1, 2698 can_events => 1,
2662 @_ 2699 @_
2663 ); 2700 );
2664} 2701}
2665 2702
2689 2726
2690use DC::OpenGL; 2727use DC::OpenGL;
2691 2728
2692my %tex = ( 2729my %tex = (
2693 food => [ 2730 food => [
2694 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2731 map { new_from_resource DC::Texture $_, mipmap => 1 }
2695 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2732 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2696 ], 2733 ],
2697 grace => [ 2734 grace => [
2698 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2735 map { new_from_resource DC::Texture $_, mipmap => 1 }
2699 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/
2700 ], 2737 ],
2701 hp => [ 2738 hp => [
2702 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2739 map { new_from_resource DC::Texture $_, mipmap => 1 }
2703 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2740 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2704 ], 2741 ],
2705 mana => [ 2742 mana => [
2706 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2743 map { new_from_resource DC::Texture $_, mipmap => 1 }
2707 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/
2708 ], 2745 ],
2709); 2746);
2710 2747
2711# eg. VGauge->new (gauge => 'food'), default gauge: food 2748# eg. VGauge->new (gauge => 'food'), default gauge: food
2824 2861
2825sub new { 2862sub new {
2826 my ($class, %arg) = @_; 2863 my ($class, %arg) = @_;
2827 2864
2828 my $self = $class->SUPER::new ( 2865 my $self = $class->SUPER::new (
2866 padding_x => 2,
2867 padding_y => 2,
2829 fg => [1, 1, 1], 2868 fg => [1, 1, 1],
2830 bg => [0, 0, 1, 0.2], 2869 bg => [0, 0, 1, 0.2],
2831 bar => [0.7, 0.5, 0.1, 0.8], 2870 bar => [0.7, 0.5, 0.1, 0.8],
2832 outline => [0.4, 0.3, 0], 2871 outline => [0.4, 0.3, 0],
2833 fontsize => 0.9, 2872 fontsize => 0.9,
2834 valign => 0, 2873 valign => 0.5,
2835 align => 0, 2874 align => 0.5,
2836 can_events => 1, 2875 can_events => 1,
2837 ellipsise => 1, 2876 ellipsise => 1,
2838 label => "%d%%", 2877 label => "%d%%",
2839 %arg, 2878 %arg,
2840 ); 2879 );
2873 my ($self) = @_; 2912 my ($self) = @_;
2874 2913
2875 glEnable GL_BLEND; 2914 glEnable GL_BLEND;
2876 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2915 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2877 2916
2917 my $px = $self->{padding_x};
2918 my $py = $self->{padding_y};
2919
2878 if ($self->{value} >= 0) { 2920 if ($self->{value} >= 0) {
2879 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2921 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2880 2922
2881 glColor_premultiply @{$self->{bar}}; 2923 glColor_premultiply @{$self->{bar}};
2882 glRect 2, 2, $s, $self->{h} - 2; 2924 glRect $px, $py, $s, $self->{h} - $py;
2883 glColor_premultiply @{$self->{bg}}; 2925 glColor_premultiply @{$self->{bg}};
2884 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2926 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2885 } 2927 }
2886 2928
2887 glColor_premultiply @{$self->{outline}}; 2929 glColor_premultiply @{$self->{outline}};
2930
2931 $px -= .5;
2932 $py -= .5;
2933
2888 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;
2889 2935
2890 glDisable GL_BLEND; 2936 glDisable GL_BLEND;
2891 2937
2892 { 2938 {
2893 local $self->{bg}; # do not draw background 2939 local $self->{bg}; # do not draw background
2902our @ISA = DC::UI::Progress::; 2948our @ISA = DC::UI::Progress::;
2903 2949
2904sub new { 2950sub new {
2905 my ($class, %arg) = @_; 2951 my ($class, %arg) = @_;
2906 2952
2953 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2954
2907 my $self = $class->SUPER::new ( 2955 my $self = $class->SUPER::new (
2956 %arg,
2908 tooltip => sub { 2957 tooltip => sub {
2909 my ($self) = @_; 2958 my ($self) = @_;
2910 2959
2911 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,
2912 $self->{lvl}, 2962 $self->{lvl},
2913 ::formsep ($self->{exp}), 2963 ::formsep ($self->{exp}),
2914 ::formsep ($self->{nxt}), 2964 ::formsep ($self->{nxt}),
2915 ::formsep ($self->{nxt} - $self->{exp}), 2965 ::formsep ($self->{nxt} - $self->{exp}),
2966 $self->_percent * 100,
2916 }, 2967 },
2917 %arg
2918 ); 2968 );
2919 2969
2920 $::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}) }
2921 if $::CONN; 2971 if $::CONN;
2922 2972
2930 if $::CONN; 2980 if $::CONN;
2931 2981
2932 $self->SUPER::DESTROY; 2982 $self->SUPER::DESTROY;
2933} 2983}
2934 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
2935sub set_value { 2999sub set_value {
2936 my ($self, $lvl, $exp) = @_; 3000 my ($self, $lvl, $exp) = @_;
2937 3001
2938 $self->{lvl} = $lvl; 3002 $self->{lvl} = $lvl;
2939 $self->{exp} = $exp; 3003 $self->{exp} = $exp;
2940 3004
2941 my $v = -1;
2942
2943 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2944 my $l0 = $table->[$lvl - 1];
2945 my $l1 = $table->[$lvl];
2946
2947 $self->{nxt} = $l1;
2948
2949 $v = ($exp - $l0) / ($l1 - $l0);
2950 }
2951
2952 $self->SUPER::set_value ($v); 3005 $self->SUPER::set_value ($self->_percent);
2953} 3006}
2954 3007
2955############################################################################# 3008#############################################################################
2956 3009
2957package DC::UI::Gauge; 3010package DC::UI::Gauge;
2966 can_hover => 1, 3019 can_hover => 1,
2967 can_events => 1, 3020 can_events => 1,
2968 %arg, 3021 %arg,
2969 ); 3022 );
2970 3023
2971 $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");
2972 $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);
2973 $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");
2974 3027
2975 $self 3028 $self
2976} 3029}
2977 3030
2978sub set_fontsize { 3031sub set_fontsize {
3001 3054
3002############################################################################# 3055#############################################################################
3003 3056
3004package DC::UI::Slider; 3057package DC::UI::Slider;
3005 3058
3006use strict; 3059use common::sense;
3007 3060
3008use DC::OpenGL; 3061use DC::OpenGL;
3009 3062
3010our @ISA = DC::UI::DrawBG::; 3063our @ISA = DC::UI::DrawBG::;
3011 3064
3012my @tex = 3065my @tex =
3013 map { new_from_file DC::Texture DC::find_rcfile $_ } 3066 map { new_from_resource DC::Texture $_ }
3014 qw(s1_slider.png s1_slider_bg.png); 3067 qw(s1_slider.png s1_slider_bg.png);
3015 3068
3016sub new { 3069sub new {
3017 my $class = shift; 3070 my $class = shift;
3018 3071
3055sub set_value { 3108sub set_value {
3056 my ($self, $value) = @_; 3109 my ($self, $value) = @_;
3057 3110
3058 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3111 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3059 3112
3060 $hi = $lo + 1 if $hi <= $lo; 3113 $hi = $lo if $hi < $lo;
3061 3114
3062 $page = $hi - $lo if $page > $hi - $lo; 3115 $value = $hi - $page if $value > $hi - $page;
3063
3064 $value = $lo if $value < $lo; 3116 $value = $lo if $value < $lo;
3065 $value = $hi - $page if $value > $hi - $page;
3066 3117
3067 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3118 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3068 if $unit; 3119 if $unit;
3069 3120
3070 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3121 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3099 if ($GRAB == $self) { 3150 if ($GRAB == $self) {
3100 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3151 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3101 3152
3102 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3153 my (undef, $lo, $hi, $page) = @{$self->{range}};
3103 3154
3104 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3155 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3105 3156
3106 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3157 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3107 } else { 3158 } else {
3108 return 0; 3159 return 0;
3109 } 3160 }
3134 my ($self) = @_; 3185 my ($self) = @_;
3135 3186
3136 unless ($self->{knob_w}) { 3187 unless ($self->{knob_w}) {
3137 $self->set_value ($self->{range}[0]); 3188 $self->set_value ($self->{range}[0]);
3138 3189
3139 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3190 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3140 my $range = ($hi - $page - $lo) || 1e-100; 3191 my $range = ($hi - $page - $lo) || 1e-10;
3141 3192
3142 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};
3143 3194
3144 $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;
3145 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3196 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3146 3197
3147 $value = ($value - $lo) / $range; 3198 $value = ($value - $lo) / $range;
3306 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3357 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3307 $layout->set_markup ($para->{markup}); 3358 $layout->set_markup ($para->{markup});
3308 3359
3309 $layout->set_shapes ( 3360 $layout->set_shapes (
3310 map 3361 map
3311 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3362 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3312 @{$para->{widget}} 3363 @{$para->{widget}}
3313 ); 3364 );
3314 3365
3315 $layout 3366 $layout
3316} 3367}
3607 3658
3608 $tip =~ s/^\n+//; 3659 $tip =~ s/^\n+//;
3609 $tip =~ s/\n+$//; 3660 $tip =~ s/\n+$//;
3610 3661
3611 $self->add (new DC::UI::Label 3662 $self->add (new DC::UI::Label
3663 fg => $DC::THEME{tooltip_fg},
3612 markup => $tip, 3664 markup => $tip,
3613 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3665 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3666 align => 0,
3614 fontsize => 0.8, 3667 fontsize => 0.8,
3615 style => 1, # FLAG_INVERSE 3668 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3616 ellipsise => 0, 3669 ellipsise => 0,
3617 font => ($widget->{tooltip_font} || $::FONT_PROP), 3670 font => ($widget->{tooltip_font} || $::FONT_PROP),
3618 ); 3671 );
3619} 3672}
3620 3673
3657sub _draw { 3710sub _draw {
3658 my ($self) = @_; 3711 my ($self) = @_;
3659 3712
3660 my ($w, $h) = @$self{qw(w h)}; 3713 my ($w, $h) = @$self{qw(w h)};
3661 3714
3662 glColor 1, 0.8, 0.4; 3715 glColor @{ $DC::THEME{tooltip_bg} };
3663 glRect 0, 0, $w, $h; 3716 glRect 0, 0, $w, $h;
3664 3717
3665 glColor 0, 0, 0; 3718 glColor @{ $DC::THEME{tooltip_border} };
3666 glRect_lineloop .5, .5, $w + .5, $h + .5; 3719 glRect_lineloop .5, .5, $w + .5, $h + .5;
3667 3720
3668 glTranslate 2, 2; 3721 glTranslate 2, 2;
3669 3722
3670 $self->SUPER::_draw; 3723 $self->SUPER::_draw;
3687 aspect => 1, 3740 aspect => 1,
3688 can_events => 0, 3741 can_events => 0,
3689 @_, 3742 @_,
3690 ); 3743 );
3691 3744
3692 if ($self->{anim} && $self->{animspeed}) {
3693 DC::weaken (my $widget = $self);
3694
3695 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3696 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3697 return unless $::CONN;
3698
3699 my $w = $widget
3700 or return;
3701
3702 ++$w->{frame};
3703 $w->update_face;
3704
3705 # somehow, $widget can go away
3706 $w->update;
3707 $w->update_timer;
3708 };
3709
3710 $self->update_face;
3711 $self->update_timer; 3745 $self->update_anim;
3712 }
3713 3746
3714 $self 3747 $self
3715} 3748}
3716 3749
3717sub update_timer { 3750sub update_timer {
3740 $tex->upload (sub { $self->reconfigure }); 3773 $tex->upload (sub { $self->reconfigure });
3741 } 3774 }
3742 } 3775 }
3743 } 3776 }
3744 } 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};
3745 } 3806 }
3746} 3807}
3747 3808
3748sub size_request { 3809sub size_request {
3749 my ($self) = @_; 3810 my ($self) = @_;
3771 return unless $self->{visible}; 3832 return unless $self->{visible};
3772 3833
3773 $self->SUPER::update; 3834 $self->SUPER::update;
3774} 3835}
3775 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
3776sub invoke_visibility_change { 3858sub invoke_visibility_change {
3777 my ($self) = @_; 3859 my ($self) = @_;
3778 3860
3779 $self->update_timer; 3861 $self->update_timer;
3780 3862
3842 $widget = new DC::UI::HBox 3924 $widget = new DC::UI::HBox
3843 can_hover => 1, 3925 can_hover => 1,
3844 can_events => 1, 3926 can_events => 1,
3845 tooltip => $tooltip, 3927 tooltip => $tooltip,
3846 children => [ 3928 children => [
3847 (new DC::UI::Label markup => $left, expand => 1), 3929 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3848 (new DC::UI::Label markup => $right, align => +1), 3930 (new DC::UI::Label markup => $right, align => 1),
3849 ], 3931 ],
3850 ; 3932 ;
3851 3933
3852 } else { 3934 } else {
3853 $widget = new DC::UI::Label 3935 $widget = new DC::UI::Label
3854 can_hover => 1, 3936 can_hover => 1,
3855 can_events => 1, 3937 can_events => 1,
3938 align => 0,
3856 markup => $widget, 3939 markup => $widget,
3857 tooltip => $tooltip; 3940 tooltip => $tooltip;
3858 } 3941 }
3859 } 3942 }
3860 3943
3875 # maybe save $GRAB? must be careful about events... 3958 # maybe save $GRAB? must be careful about events...
3876 $GRAB = $self; 3959 $GRAB = $self;
3877 $self->{button} = $ev->{button}; 3960 $self->{button} = $ev->{button};
3878 3961
3879 $self->show; 3962 $self->show;
3880 $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
3881} 3972}
3882 3973
3883sub invoke_mouse_motion { 3974sub invoke_mouse_motion {
3884 my ($self, $ev, $x, $y) = @_; 3975 my ($self, $ev, $x, $y) = @_;
3885 3976
3917 4008
3918 my $self = $class->SUPER::new ( 4009 my $self = $class->SUPER::new (
3919 @_, 4010 @_,
3920 ); 4011 );
3921 4012
3922 $self->{current} = $self->{children}[0] 4013 $self->set_current_page (0);
3923 if @{ $self->{children} };
3924 4014
3925 $self 4015 $self
3926} 4016}
3927 4017
3928sub add { 4018sub add {
3929 my ($self, @widgets) = @_; 4019 my ($self, @widgets) = @_;
3930 4020
3931 $self->SUPER::add (@widgets); 4021 $self->SUPER::add (@widgets);
3932 4022
3933 $self->{current} = $self->{children}[0] 4023 $self->set_current_page (0)
3934 if @{ $self->{children} }; 4024 if @widgets == @{ $self->{children} };
3935} 4025}
3936 4026
3937sub get_current_page { 4027sub get_current_page {
3938 my ($self) = @_; 4028 my ($self) = @_;
3939 4029
3945 4035
3946 my $widget = ref $page_or_widget 4036 my $widget = ref $page_or_widget
3947 ? $page_or_widget 4037 ? $page_or_widget
3948 : $self->{children}[$page_or_widget]; 4038 : $self->{children}[$page_or_widget];
3949 4039
4040 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4041
3950 $self->{current} = $widget; 4042 if (($self->{current} = $widget)) {
4043 $self->{current}->set_visible if $self->{current} && $self->{visible};
3951 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4044 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3952 4045
3953 $self->emit (page_changed => $self->{current}); 4046 $self->emit (page_changed => $self->{current});
4047 }
3954 4048
3955 $self->realloc; 4049 $self->realloc;
3956} 4050}
3957 4051
3958sub visible_children { 4052sub visible_children {
3959 $_[0]{current} 4053 $_[0]{current} || ()
3960} 4054}
3961 4055
3962sub size_request { 4056sub size_request {
3963 my ($self) = @_; 4057 my ($self) = @_;
3964 4058
4059 $self->{current}
3965 $self->{current}->size_request 4060 ? $self->{current}->size_request
4061 : (0, 0)
3966} 4062}
3967 4063
3968sub invoke_size_allocate { 4064sub invoke_size_allocate {
3969 my ($self, $w, $h) = @_; 4065 my ($self, $w, $h) = @_;
3970 4066
3971 $self->{current}->configure (0, 0, $w, $h); 4067 $self->{current}->configure (0, 0, $w, $h)
4068 if $self->{current};
3972 4069
3973 1 4070 1
3974} 4071}
3975 4072
3976sub _draw { 4073sub _draw {
3977 my ($self) = @_; 4074 my ($self) = @_;
3978 4075
3979 $self->{current}->draw; 4076 $self->{current}->draw
4077 if $self->{current};
3980} 4078}
3981 4079
3982############################################################################# 4080#############################################################################
3983 4081
3984package DC::UI::Notebook; 4082package DC::UI::Notebook;
4051} 4149}
4052 4150
4053sub pages { 4151sub pages {
4054 my ($self) = @_; 4152 my ($self) = @_;
4055 $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
4056} 4166}
4057 4167
4058sub add_tab { 4168sub add_tab {
4059 my ($self, $title, $widget, $tooltip) = @_; 4169 my ($self, $title, $widget, $tooltip) = @_;
4060 4170
4182 $self 4292 $self
4183} 4293}
4184 4294
4185sub reorder { 4295sub reorder {
4186 my ($self) = @_; 4296 my ($self) = @_;
4187 my $NOW = Time::HiRes::time; 4297 my $NOW = EV::time;
4188 4298
4189 # freeze display when hovering over any label 4299 # freeze display when hovering over any label
4190 return if $DC::UI::TOOLTIP->{owner} 4300 return if $DC::UI::TOOLTIP->{owner}
4191 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4301 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4192 values %{ $self->{item} }; 4302 values %{ $self->{item} };
4224 tooltip => $item->{tooltip}, 4334 tooltip => $item->{tooltip},
4225 tooltip_font => $::FONT_PROP, 4335 tooltip_font => $::FONT_PROP,
4226 tooltip_width => 0.67, 4336 tooltip_width => 0.67,
4227 fontsize => $item->{fontsize} || $self->{fontsize}, 4337 fontsize => $item->{fontsize} || $self->{fontsize},
4228 max_w => $::WIDTH * 0.44, 4338 max_w => $::WIDTH * 0.44,
4339 align => 0,
4229 fg => [@{ $item->{fg} }], 4340 fg => [@{ $item->{fg} }],
4230 can_events => 1, 4341 can_events => 1,
4231 can_hover => 1 4342 can_hover => 1
4232 }; 4343 };
4233 4344
4241 $label->{fg}[3] = $item->{fg}[3] || 1; 4352 $label->{fg}[3] = $item->{fg}[3] || 1;
4242 } 4353 }
4243 4354
4244 push @widgets, $label; 4355 push @widgets, $label;
4245 } 4356 }
4357
4358 my $hash = join ",", @widgets;
4359 return if $hash eq $self->{last_widget_hash};
4360 $self->{last_widget_hash} = $hash;
4246 4361
4247 $self->clear; 4362 $self->clear;
4248 $self->SUPER::add (reverse @widgets); 4363 $self->SUPER::add (reverse @widgets);
4249} 4364}
4250 4365
4457 4572
4458 delete $queue{$widget+0}; 4573 delete $queue{$widget+0};
4459 4574
4460 my ($w, $h) = $widget->size_request; 4575 my ($w, $h) = $widget->size_request;
4461 4576
4462 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4577 $w += $widget->{padding_x} * 2;
4463 $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;
4464 4582
4465 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4583 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4466 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4584 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4467 4585
4468 $w = $widget->{force_w} if exists $widget->{force_w}; 4586 $w = $widget->{force_w} if exists $widget->{force_w};
4548 4666
4549############################################################################# 4667#############################################################################
4550 4668
4551package DC::UI; 4669package DC::UI;
4552 4670
4553$ROOT = new DC::UI::Root; 4671$ROOT = new DC::UI::Root;
4554$TOOLTIP = new DC::UI::Tooltip z => 900; 4672$TOOLTIP = new DC::UI::Tooltip z => 900;
4555 4673
45561 46741
4557

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines