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.453 by root, Wed Dec 26 20:46:39 2007 UTC vs.
Revision 1.477 by root, Sun Jan 11 23:50:27 2009 UTC

1package dc::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7 7
8use dc; 8use Guard ();
9
10use DC;
9use dc::Pod; 11use DC::Pod;
10use dc::Texture; 12use DC::Texture;
11 13
12our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
13 15
14our $LAYOUT; 16our $LAYOUT;
15our $ROOT; 17our $ROOT;
111 113
112 if ($GRAB) { 114 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 116 # mousewheel
115 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & dc::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
117 119
118 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
119 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
120 122
121 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
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};
211 reconfigure_widgets; 213 reconfigure_widgets;
212} 214}
213 215
214############################################################################# 216#############################################################################
215 217
216package dc::UI::Event; 218package DC::UI::Event;
217 219
218sub xy { 220sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220} 222}
221 223
222############################################################################# 224#############################################################################
223 225
224package dc::UI::Base; 226package DC::UI::Base;
225 227
226use strict; 228use strict;
227 229
228use dc::OpenGL; 230use DC::OpenGL;
229 231
230sub new { 232sub new {
231 my $class = shift; 233 my $class = shift;
232 234
233 my $self = bless { 235 my $self = bless {
238 h => undef, 240 h => undef,
239 can_events => 1, 241 can_events => 1,
240 @_ 242 @_
241 }, $class; 243 }, $class;
242 244
243 dc::weaken ($dc::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
244 246
245 for (keys %$self) { 247 for (keys %$self) {
246 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
247 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
248 } 250 }
249 } 251 }
250 252
251 if (my $layout = $dc::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
252 $self->{x} = $layout->{x} * $dc::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
253 $self->{y} = $layout->{y} * $dc::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
254 $self->{force_w} = $layout->{w} * $dc::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
255 $self->{force_h} = $layout->{h} * $dc::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
256 258
257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
259 261
260 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
278sub show { 280sub show {
279 my ($self) = @_; 281 my ($self) = @_;
280 282
281 return if $self->{parent}; 283 return if $self->{parent};
282 284
283 $dc::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
284} 286}
285 287
286sub set_visible { 288sub set_visible {
287 my ($self) = @_; 289 my ($self) = @_;
288 290
289 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
290 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
309 delete $self->{root}; 314 delete $self->{root};
310 315
311 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
312 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
313 318
314 $dc::UI::TOOLTIP_WATCHER->invoke 319 $DC::UI::TOOLTIP_WATCHER->invoke
315 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
316 321
317 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
318 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
319} 324}
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";
389 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
390 403
391 if ($self->{aspect}) { 404 if ($self->{aspect}) {
392 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
393 406
394 $w = List::Util::min $w, dc::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
395 $h = List::Util::min $h, dc::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
396 409
397 # use alignment to adjust x, y 410 # use alignment to adjust x, y
398 411
399 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
400 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
441 454
442 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
443 456
444 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
445 458
446 if ($dc::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
447 delete $dc::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
448 $dc::UI::TOOLTIP_WATCHER->invoke; 461 $DC::UI::TOOLTIP_WATCHER->invoke;
449 } 462 }
450} 463}
451 464
452# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
453sub coord2local { 466sub coord2local {
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
567# in .xs 580# in .xs
568 581
569sub set_parent { 582sub set_parent {
570 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
571 584
572 dc::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
573 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
574} 587}
575 588
576sub realloc { 589sub realloc {
577 my ($self) = @_; 590 my ($self) = @_;
615} 628}
616 629
617sub DESTROY { 630sub DESTROY {
618 my ($self) = @_; 631 my ($self) = @_;
619 632
620 return if dc::in_destruct; 633 return if DC::in_destruct;
621 634
622 local $@; 635 local $@;
623 eval { $self->destroy }; 636 eval { $self->destroy };
624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
625 638
626 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
627} 640}
628 641
629############################################################################# 642#############################################################################
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 strict;
636use dc::OpenGL; 649use DC::OpenGL;
637 650
638sub new { 651sub new {
639 my $class = shift; 652 my $class = shift;
640 653
641 $class->SUPER::new ( 654 $class->SUPER::new (
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 && $self->{active_bg}
652 ? $self->{active_bg} 672 ? $self->{active_bg}
663 } 683 }
664} 684}
665 685
666############################################################################# 686#############################################################################
667 687
668package dc::UI::Empty; 688package DC::UI::Empty;
669 689
670our @ISA = dc::UI::Base::; 690our @ISA = DC::UI::Base::;
671 691
672sub new { 692sub new {
673 my ($class, %arg) = @_; 693 my ($class, %arg) = @_;
674 $class->SUPER::new (can_events => 0, %arg); 694 $class->SUPER::new (can_events => 0, %arg);
675} 695}
682 702
683sub draw { } 703sub draw { }
684 704
685############################################################################# 705#############################################################################
686 706
687package dc::UI::Container; 707package DC::UI::Container;
688 708
689our @ISA = dc::UI::Base::; 709our @ISA = DC::UI::Base::;
690 710
691sub new { 711sub new {
692 my ($class, %arg) = @_; 712 my ($class, %arg) = @_;
693 713
694 my $children = delete $arg{children}; 714 my $children = delete $arg{children};
788 $_->draw for $self->visible_children; 808 $_->draw for $self->visible_children;
789} 809}
790 810
791############################################################################# 811#############################################################################
792 812
793package dc::UI::Bin; 813package DC::UI::Bin;
794 814
795our @ISA = dc::UI::Container::; 815our @ISA = DC::UI::Container::;
796 816
797sub new { 817sub new {
798 my ($class, %arg) = @_; 818 my ($class, %arg) = @_;
799 819
800 my $child = (delete $arg{child}) || new dc::UI::Empty::; 820 my $child = (delete $arg{child}) || new DC::UI::Empty::;
801 821
802 $class->SUPER::new (children => [$child], %arg) 822 $class->SUPER::new (children => [$child], %arg)
803} 823}
804 824
805sub add { 825sub add {
812sub remove { 832sub remove {
813 my ($self, $widget) = @_; 833 my ($self, $widget) = @_;
814 834
815 $self->SUPER::remove ($widget); 835 $self->SUPER::remove ($widget);
816 836
817 $self->{children} = [new dc::UI::Empty] 837 $self->{children} = [new DC::UI::Empty]
818 unless @{$self->{children}}; 838 unless @{$self->{children}};
819} 839}
820 840
821sub child { $_[0]->{children}[0] } 841sub child { $_[0]->{children}[0] }
822 842
833} 853}
834 854
835############################################################################# 855#############################################################################
836# back-buffered drawing area 856# back-buffered drawing area
837 857
838package dc::UI::Window; 858package DC::UI::Window;
839 859
840our @ISA = dc::UI::Bin::; 860our @ISA = DC::UI::Bin::;
841 861
842use dc::OpenGL; 862use DC::OpenGL;
843 863
844sub new { 864sub new {
845 my ($class, %arg) = @_; 865 my ($class, %arg) = @_;
846 866
847 my $self = $class->SUPER::new (%arg); 867 my $self = $class->SUPER::new (%arg);
869} 889}
870 890
871sub render_child { 891sub render_child {
872 my ($self) = @_; 892 my ($self) = @_;
873 893
874 $self->{texture} = new_from_opengl dc::Texture $self->{w}, $self->{h}, sub { 894 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
875 glClearColor 0, 0, 0, 0; 895 glClearColor 0, 0, 0, 0;
876 glClear GL_COLOR_BUFFER_BIT; 896 glClear GL_COLOR_BUFFER_BIT;
877 897
878 { 898 {
879 package dc::UI::Base; 899 package DC::UI::Base;
880 900
881 local ($draw_x, $draw_y, $draw_w, $draw_h) = 901 local ($draw_x, $draw_y, $draw_w, $draw_h) =
882 (0, 0, $self->{w}, $self->{h}); 902 (0, 0, $self->{w}, $self->{h});
883 903
884 $self->_render; 904 $self->_render;
901 glDisable GL_TEXTURE_2D; 921 glDisable GL_TEXTURE_2D;
902} 922}
903 923
904############################################################################# 924#############################################################################
905 925
906package dc::UI::ViewPort; 926package DC::UI::ViewPort;
907 927
908use List::Util qw(min max); 928use List::Util qw(min max);
909 929
910our @ISA = dc::UI::Window::; 930our @ISA = DC::UI::Window::;
911 931
912sub new { 932sub new {
913 my $class = shift; 933 my $class = shift;
914 934
915 $class->SUPER::new ( 935 $class->SUPER::new (
999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1019 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1020 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1001 ) { 1021 ) {
1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1022 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1003 } else { 1023 } else {
1004 $self->dc::UI::Base::find_widget ($x, $y) 1024 $self->DC::UI::Base::find_widget ($x, $y)
1005 } 1025 }
1006} 1026}
1007 1027
1008sub _render { 1028sub _render {
1009 my ($self) = @_; 1029 my ($self) = @_;
1010 1030
1011 local $dc::UI::Base::draw_x = $dc::UI::Base::draw_x - $self->{view_x}; 1031 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1012 local $dc::UI::Base::draw_y = $dc::UI::Base::draw_y - $self->{view_y}; 1032 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1013 1033
1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1034 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1015 1035
1016 $self->SUPER::_render; 1036 $self->SUPER::_render;
1017} 1037}
1018 1038
1019############################################################################# 1039#############################################################################
1020 1040
1021package dc::UI::ScrolledWindow; 1041package DC::UI::ScrolledWindow;
1022 1042
1023our @ISA = dc::UI::Table::; 1043our @ISA = DC::UI::Table::;
1024 1044
1025sub new { 1045sub new {
1026 my ($class, %arg) = @_; 1046 my ($class, %arg) = @_;
1027 1047
1028 my $child = delete $arg{child}; 1048 my $child = delete $arg{child};
1029 1049
1030 my $self; 1050 my $self;
1031 1051
1032 my $hslider = new dc::UI::Slider 1052 my $hslider = new DC::UI::Slider
1033 c_col => 0, 1053 c_col => 0,
1034 c_row => 1, 1054 c_row => 1,
1035 vertical => 0, 1055 vertical => 0,
1036 range => [0, 0, 1, 0.01], # HACK fix 1056 range => [0, 0, 1, 0.01], # HACK fix
1037 on_changed => sub { 1057 on_changed => sub {
1038 $self->{hpos} = $_[1]; 1058 $self->{hpos} = $_[1];
1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1059 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1060 },
1041 ; 1061 ;
1042 1062
1043 my $vslider = new dc::UI::Slider 1063 my $vslider = new DC::UI::Slider
1044 c_col => 1, 1064 c_col => 1,
1045 c_row => 0, 1065 c_row => 0,
1046 vertical => 1, 1066 vertical => 1,
1047 range => [0, 0, 1, 0.01], # HACK fix 1067 range => [0, 0, 1, 0.01], # HACK fix
1048 on_changed => sub { 1068 on_changed => sub {
1060 col_expand => [1, 0], 1080 col_expand => [1, 0],
1061 row_expand => [1, 0], 1081 row_expand => [1, 0],
1062 %arg, 1082 %arg,
1063 ); 1083 );
1064 1084
1065 $self->{vp} = new dc::UI::ViewPort 1085 $self->{vp} = new DC::UI::ViewPort
1066 c_col => 0, 1086 c_col => 0,
1067 c_row => 0, 1087 c_row => 0,
1068 expand => 1, 1088 expand => 1,
1069 scroll_x => $self->{scroll_x}, 1089 scroll_x => $self->{scroll_x},
1070 scroll_y => $self->{scroll_y}, 1090 scroll_y => $self->{scroll_y},
1196 $self->SUPER::invoke_size_allocate ($w, $h) 1216 $self->SUPER::invoke_size_allocate ($w, $h)
1197} 1217}
1198 1218
1199############################################################################# 1219#############################################################################
1200 1220
1201package dc::UI::Frame; 1221package DC::UI::Frame;
1202 1222
1203our @ISA = dc::UI::Bin::; 1223our @ISA = DC::UI::Bin::;
1204 1224
1205use dc::OpenGL; 1225use DC::OpenGL;
1206 1226
1207sub new { 1227sub new {
1208 my $class = shift; 1228 my $class = shift;
1209 1229
1210 $class->SUPER::new ( 1230 $class->SUPER::new (
1229 $self->SUPER::_draw; 1249 $self->SUPER::_draw;
1230} 1250}
1231 1251
1232############################################################################# 1252#############################################################################
1233 1253
1234package dc::UI::FancyFrame; 1254package DC::UI::FancyFrame;
1235 1255
1236our @ISA = dc::UI::Bin::; 1256our @ISA = DC::UI::Bin::;
1237 1257
1238use dc::OpenGL; 1258use DC::OpenGL;
1239 1259
1240sub new { 1260sub new {
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
1261 1281
1262sub add { 1282sub add {
1263 my ($self, @widgets) = @_; 1283 my ($self, @widgets) = @_;
1264 1284
1265 $self->SUPER::add (@widgets); 1285 $self->SUPER::add (@widgets);
1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label}; 1286 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1267} 1287}
1268 1288
1269sub border { 1289sub border {
1270 int $_[0]{border} * $::FONTSIZE 1290 int $_[0]{border} * $::FONTSIZE
1271} 1291}
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;
1329 } 1349 }
1330} 1350}
1331 1351
1332############################################################################# 1352#############################################################################
1333 1353
1334package dc::UI::Toplevel; 1354package DC::UI::Toplevel;
1335 1355
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
1372 if ($self->{has_close_button}) { 1392 if ($self->{has_close_button}) {
1373 $self->{close_button} = 1393 $self->{close_button} =
1374 new dc::UI::ImageButton 1394 new DC::UI::ImageButton
1375 path => 'x1_close.png', 1395 path => 'x1_close.png',
1376 on_activate => sub { $self->emit ("delete") }; 1396 on_activate => sub { $self->emit ("delete") };
1377 1397
1378 $self->dc::UI::Container::add ($self->{close_button}); 1398 $self->DC::UI::Container::add ($self->{close_button});
1379 } 1399 }
1380 1400
1381 $self 1401 $self
1382} 1402}
1383 1403
1384sub add { 1404sub add {
1385 my ($self, @widgets) = @_; 1405 my ($self, @widgets) = @_;
1386 1406
1387 $self->SUPER::add (@widgets); 1407 $self->SUPER::add (@widgets);
1388 $self->dc::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1408 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1389 $self->dc::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1409 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1390} 1410}
1391 1411
1392sub border { 1412sub border {
1393 int $_[0]{border} * $::FONTSIZE 1413 int $_[0]{border} * $::FONTSIZE
1394} 1414}
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
1579 if $self->{close_button}; 1601 if $self->{close_button};
1580} 1602}
1581 1603
1582############################################################################# 1604#############################################################################
1583 1605
1584package dc::UI::Table; 1606package DC::UI::Table;
1585 1607
1586our @ISA = dc::UI::Container::; 1608our @ISA = DC::UI::Container::;
1587 1609
1588use List::Util qw(max sum); 1610use List::Util qw(max sum);
1589 1611
1590use dc::OpenGL; 1612use DC::OpenGL;
1591 1613
1592sub new { 1614sub new {
1593 my $class = shift; 1615 my $class = shift;
1594 1616
1595 $class->SUPER::new ( 1617 $class->SUPER::new (
1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1661 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640 1662
1641 my $sw = sum @w[$c .. $c + $cs - 1]; 1663 my $sw = sum @w[$c .. $c + $cs - 1];
1642 1664
1643 if ($w > $sw) { 1665 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1666 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 } 1667 }
1646 } 1668 }
1647 1669
1648 # second pass, rows 1670 # second pass, rows
1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1671 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1672 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1651 1673
1652 my $sh = sum @h[$r .. $r + $rs - 1]; 1674 my $sh = sum @h[$r .. $r + $rs - 1];
1653 1675
1654 if ($h > $sh) { 1676 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1677 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1656 } 1678 }
1657 } 1679 }
1658 1680
1659 (\@w, \@h) 1681 (\@w, \@h)
1660} 1682}
1683 @col_expand = (1) x @$ws unless @col_expand; 1705 @col_expand = (1) x @$ws unless @col_expand;
1684 my $col_expand = (sum @col_expand) || 1; 1706 my $col_expand = (sum @col_expand) || 1;
1685 1707
1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1708 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1687 1709
1688 dc::UI::harmonize $ws; 1710 DC::UI::harmonize $ws;
1689 1711
1690 my @row_expand = @{$self->{row_expand}}; 1712 my @row_expand = @{$self->{row_expand}};
1691 @row_expand = (1) x @$ws unless @row_expand; 1713 @row_expand = (1) x @$ws unless @row_expand;
1692 my $row_expand = (sum @row_expand) || 1; 1714 my $row_expand = (sum @row_expand) || 1;
1693 1715
1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1716 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1695 1717
1696 dc::UI::harmonize $hs; 1718 DC::UI::harmonize $hs;
1697 1719
1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1720 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1721 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1700 1722
1701 for my $widget ($self->children) { 1723 for my $widget ($self->children) {
1710 1 1732 1
1711} 1733}
1712 1734
1713############################################################################# 1735#############################################################################
1714 1736
1715package dc::UI::Fixed; 1737package DC::UI::Fixed;
1716 1738
1717use List::Util qw(min max); 1739use List::Util qw(min max);
1718 1740
1719our @ISA = dc::UI::Container::; 1741our @ISA = DC::UI::Container::;
1720 1742
1721sub _scale($$$) { 1743sub _scale($$$) {
1722 my ($rel, $val, $max) = @_; 1744 my ($rel, $val, $max) = @_;
1723 1745
1724 $rel ? $val * $max : $val 1746 $rel ? $val * $max : $val
1776 1 1798 1
1777} 1799}
1778 1800
1779############################################################################# 1801#############################################################################
1780 1802
1781package dc::UI::Box; 1803package DC::UI::Box;
1782 1804
1783our @ISA = dc::UI::Container::; 1805our @ISA = DC::UI::Container::;
1784 1806
1785sub size_request { 1807sub size_request {
1786 my ($self) = @_; 1808 my ($self) = @_;
1787 1809
1788 my @children = $self->visible_children; 1810 my @children = $self->visible_children;
1823 $req[$_] += $space * $children[$_]{expand} 1845 $req[$_] += $space * $children[$_]{expand}
1824 for 0 .. $#children; 1846 for 0 .. $#children;
1825 } 1847 }
1826 } 1848 }
1827 1849
1828 dc::UI::harmonize \@req; 1850 DC::UI::harmonize \@req;
1829 1851
1830 my $pos = 0; 1852 my $pos = 0;
1831 for (0 .. $#children) { 1853 for (0 .. $#children) {
1832 my $alloc = $req[$_]; 1854 my $alloc = $req[$_];
1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1855 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1838 1 1860 1
1839} 1861}
1840 1862
1841############################################################################# 1863#############################################################################
1842 1864
1843package dc::UI::HBox; 1865package DC::UI::HBox;
1844 1866
1845our @ISA = dc::UI::Box::; 1867our @ISA = DC::UI::Box::;
1846 1868
1847sub new { 1869sub new {
1848 my $class = shift; 1870 my $class = shift;
1849 1871
1850 $class->SUPER::new ( 1872 $class->SUPER::new (
1853 ) 1875 )
1854} 1876}
1855 1877
1856############################################################################# 1878#############################################################################
1857 1879
1858package dc::UI::VBox; 1880package DC::UI::VBox;
1859 1881
1860our @ISA = dc::UI::Box::; 1882our @ISA = DC::UI::Box::;
1861 1883
1862sub new { 1884sub new {
1863 my $class = shift; 1885 my $class = shift;
1864 1886
1865 $class->SUPER::new ( 1887 $class->SUPER::new (
1868 ) 1890 )
1869} 1891}
1870 1892
1871############################################################################# 1893#############################################################################
1872 1894
1873package dc::UI::Label; 1895package DC::UI::Label;
1874 1896
1875our @ISA = dc::UI::DrawBG::; 1897our @ISA = DC::UI::DrawBG::;
1876 1898
1877use dc::OpenGL; 1899use DC::OpenGL;
1878 1900
1879sub new { 1901sub new {
1880 my ($class, %arg) = @_; 1902 my ($class, %arg) = @_;
1881 1903
1882 my $self = $class->SUPER::new ( 1904 my $self = $class->SUPER::new (
1887 #text => initial text 1909 #text => initial text
1888 #markup => initial narkup 1910 #markup => initial narkup
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
1902 if (exists $self->{template}) { 1924 if (exists $self->{template}) {
1903 my $layout = new dc::Layout; 1925 my $layout = new DC::Layout;
1904 $layout->set_text (delete $self->{template}); 1926 $layout->set_text (delete $self->{template});
1905 $self->{template} = $layout; 1927 $self->{template} = $layout;
1906 } 1928 }
1907 1929
1908 if (exists $self->{markup}) { 1930 if (exists $self->{markup}) {
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}) {
2059# $self->{list} = dc::OpenGL::glGenList; 2076# $self->{list} = DC::OpenGL::glGenList;
2060# dc::OpenGL::glNewList $self->{list}; 2077# DC::OpenGL::glNewList $self->{list};
2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2078# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# dc::OpenGL::glEndList; 2079# DC::OpenGL::glEndList;
2063# } 2080# }
2064# 2081#
2065# dc::OpenGL::glCallList $self->{list}; 2082# DC::OpenGL::glCallList $self->{list};
2066 2083
2067 $self->{layout}->draw; 2084 $self->{layout}->draw;
2068} 2085}
2069 2086
2070#sub destroy { 2087#sub destroy {
2071# my ($self) = @_; 2088# my ($self) = @_;
2072# 2089#
2073# dc::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2090# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074# 2091#
2075# $self->SUPER::destroy; 2092# $self->SUPER::destroy;
2076#} 2093#}
2077 2094
2078############################################################################# 2095#############################################################################
2079 2096
2080package dc::UI::EntryBase; 2097package DC::UI::EntryBase;
2081 2098
2082our @ISA = dc::UI::Label::; 2099our @ISA = DC::UI::Label::;
2083 2100
2084use dc::OpenGL; 2101use DC::OpenGL;
2085 2102
2086sub new { 2103sub new {
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,
2119 padding_x => 4,
2120 padding_y => 2,
2101 #text => ... 2121 #text => ...
2102 #hidden => "*", 2122 #hidden => "*",
2103 @_ 2123 @_
2104 ) 2124 )
2105} 2125}
2152 2172
2153 my $text = $self->get_text; 2173 my $text = $self->get_text;
2154 2174
2155 $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;
2156 2176
2157 if ($uni == 8) { 2177 if ($sym == DC::SDLK_BACKSPACE) {
2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2178 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2159 } elsif ($uni == 127) { 2179 } elsif ($sym == DC::SDLK_DELETE) {
2160 substr $text, $self->{cursor}, 1, ""; 2180 substr $text, $self->{cursor}, 1, "";
2161 } elsif ($sym == dc::SDLK_LEFT) { 2181 } elsif ($sym == DC::SDLK_LEFT) {
2162 --$self->{cursor} if $self->{cursor}; 2182 --$self->{cursor} if $self->{cursor};
2163 } elsif ($sym == dc::SDLK_RIGHT) { 2183 } elsif ($sym == DC::SDLK_RIGHT) {
2164 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2184 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2165 } elsif ($sym == dc::SDLK_HOME) { 2185 } elsif ($sym == DC::SDLK_HOME) {
2166 # what a hack 2186 # what a hack
2167 $self->{cursor} = 2187 $self->{cursor} =
2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2188 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2169 ? length $1 2189 ? length $1
2170 : 0; 2190 : 0;
2171 } elsif ($sym == dc::SDLK_END) { 2191 } elsif ($sym == DC::SDLK_END) {
2172 # uh, again 2192 # uh, again
2173 $self->{cursor} = 2193 $self->{cursor} =
2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2194 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2175 ? $self->{cursor} + length $1 2195 ? $self->{cursor} + length $1
2176 : length $self->{text}; 2196 : length $self->{text};
2267 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2268 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;
2269 glLineWidth 1; 2289 glLineWidth 1;
2270 2290
2271 } else { 2291 } else {
2272 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2273 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5; 2295 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
2279 } 2299 }
2280} 2300}
2281 2301
2282############################################################################# 2302#############################################################################
2283 2303
2284package dc::UI::Entry; 2304package DC::UI::Entry;
2285 2305
2286our @ISA = dc::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2287 2307
2288use 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
2289 2319
2290sub invoke_key_down { 2320sub invoke_key_down {
2291 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2292 2322
2293 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2299 $self->{history_pointer} = -1; 2329 $self->{history_pointer} = -1;
2300 $self->{history_saveback} = ''; 2330 $self->{history_saveback} = '';
2301 $self->emit (activate => $txt); 2331 $self->emit (activate => $txt);
2302 $self->update; 2332 $self->update;
2303 2333
2304 } elsif ($sym == dc::SDLK_UP) { 2334 } elsif ($sym == DC::SDLK_UP) {
2305 if ($self->{history_pointer} < 0) { 2335 if ($self->{history_pointer} < 0) {
2306 $self->{history_saveback} = $self->get_text; 2336 $self->{history_saveback} = $self->get_text;
2307 } 2337 }
2308 if (@{$self->{history} || []} > 0) { 2338 if (@{$self->{history} || []} > 0) {
2309 $self->{history_pointer}++; 2339 $self->{history_pointer}++;
2311 $self->{history_pointer} = @{$self->{history} || []} - 1; 2341 $self->{history_pointer} = @{$self->{history} || []} - 1;
2312 } 2342 }
2313 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2343 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2314 } 2344 }
2315 2345
2316 } elsif ($sym == dc::SDLK_DOWN) { 2346 } elsif ($sym == DC::SDLK_DOWN) {
2317 $self->{history_pointer}--; 2347 $self->{history_pointer}--;
2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2319 2349
2320 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2321 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2322 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2323 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2324 } 2357 }
2325 2358
2326 } else { 2359 } else {
2327 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2328 } 2361 }
2330 1 2363 1
2331} 2364}
2332 2365
2333############################################################################# 2366#############################################################################
2334 2367
2335package dc::UI::TextEdit; 2368package DC::UI::TextEdit;
2336 2369
2337our @ISA = dc::UI::EntryBase::; 2370our @ISA = DC::UI::EntryBase::;
2338 2371
2339use dc::OpenGL; 2372use DC::OpenGL;
2373
2374sub new {
2375 my $class = shift;
2376
2377 $class->SUPER::new (
2378 padding_y => 4,
2379
2380 @_
2381 )
2382}
2340 2383
2341sub move_cursor_ver { 2384sub move_cursor_ver {
2342 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2343 2386
2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2345 2388
2346 $y += $dy; 2389 $line += $dy;
2347 2390
2348 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2391 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2349 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2350 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2351 $self->update; 2394 $self->update;
2352 return; 2395 return;
2353 } 2396 }
2356sub invoke_key_down { 2399sub invoke_key_down {
2357 my ($self, $ev) = @_; 2400 my ($self, $ev) = @_;
2358 2401
2359 my $sym = $ev->{sym}; 2402 my $sym = $ev->{sym};
2360 2403
2361 if ($sym == dc::SDLK_UP) { 2404 if ($sym == DC::SDLK_UP) {
2362 $self->move_cursor_ver (-1); 2405 $self->move_cursor_ver (-1);
2363 } elsif ($sym == dc::SDLK_DOWN) { 2406 } elsif ($sym == DC::SDLK_DOWN) {
2364 $self->move_cursor_ver (+1); 2407 $self->move_cursor_ver (+1);
2365 } else { 2408 } else {
2366 return $self->SUPER::invoke_key_down ($ev) 2409 return $self->SUPER::invoke_key_down ($ev)
2367 } 2410 }
2368 2411
2369 1 2412 1
2370} 2413}
2371 2414
2372############################################################################# 2415#############################################################################
2373 2416
2374package dc::UI::ButtonBin; 2417package DC::UI::ButtonBin;
2375 2418
2376our @ISA = dc::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2377 2420
2378use dc::OpenGL; 2421use DC::OpenGL;
2379 2422
2380my @tex = 2423my @tex =
2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2383 2426
2384sub new { 2427sub new {
2385 my $class = shift; 2428 my $class = shift;
2386 2429
2387 $class->SUPER::new ( 2430 $class->SUPER::new (
2388 can_hover => 1, 2431 can_hover => 1,
2389 align => 0, 2432 align => 0.5,
2390 valign => 0, 2433 valign => 0.5,
2391 can_events => 1, 2434 can_events => 1,
2392 @_ 2435 @_
2393 ) 2436 )
2394} 2437}
2395 2438
2418 $self->SUPER::_draw; 2461 $self->SUPER::_draw;
2419} 2462}
2420 2463
2421############################################################################# 2464#############################################################################
2422 2465
2423package dc::UI::Button; 2466package DC::UI::Button;
2424 2467
2425our @ISA = dc::UI::Label::; 2468our @ISA = DC::UI::Label::;
2426 2469
2427use dc::OpenGL; 2470use DC::OpenGL;
2428 2471
2429my @tex = 2472my @tex =
2430 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2431 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2432 2475
2433sub new { 2476sub new {
2434 my $class = shift; 2477 my $class = shift;
2435 2478
2436 $class->SUPER::new ( 2479 $class->SUPER::new (
2437 padding_x => 4, 2480 padding_x => 8,
2438 padding_y => 4, 2481 padding_y => 4,
2439 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2440 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2441 can_hover => 1, 2484 can_hover => 1,
2442 align => 0, 2485 align => 0.5,
2443 valign => 0, 2486 valign => 0.5,
2444 can_events => 1, 2487 can_events => 1,
2445 @_ 2488 @_
2446 ) 2489 )
2447} 2490}
2448 2491
2473 $self->SUPER::_draw; 2516 $self->SUPER::_draw;
2474} 2517}
2475 2518
2476############################################################################# 2519#############################################################################
2477 2520
2478package dc::UI::CheckBox; 2521package DC::UI::CheckBox;
2479 2522
2480our @ISA = dc::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2481 2524
2482my @tex = 2525my @tex =
2483 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2484 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2485 2528
2486use dc::OpenGL; 2529use DC::OpenGL;
2487 2530
2488sub new { 2531sub new {
2489 my $class = shift; 2532 my $class = shift;
2490 2533
2491 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2492 padding_x => 2, 2536 padding_x => 2,
2493 padding_y => 2, 2537 padding_y => 2,
2494 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2495 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2496 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2502} 2546}
2503 2547
2504sub size_request { 2548sub size_request {
2505 my ($self) = @_; 2549 my ($self) = @_;
2506 2550
2507 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2508} 2552}
2509 2553
2510sub toggle { 2554sub toggle {
2511 my ($self) = @_; 2555 my ($self) = @_;
2512 2556
2548 glDisable GL_TEXTURE_2D; 2592 glDisable GL_TEXTURE_2D;
2549} 2593}
2550 2594
2551############################################################################# 2595#############################################################################
2552 2596
2553package dc::UI::Image; 2597package DC::UI::Image;
2554 2598
2555our @ISA = dc::UI::Base::; 2599our @ISA = DC::UI::Base::;
2556 2600
2557use dc::OpenGL; 2601use DC::OpenGL;
2558 2602
2559our %texture_cache; 2603our %texture_cache;
2560 2604
2561sub new { 2605sub new {
2562 my $class = shift; 2606 my $class = shift;
2569 2613
2570 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2571 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2572 2616
2573 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2574 new_from_file dc::Texture dc::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2575 2619
2576 dc::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2577 2621
2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2579 2623
2580 $self 2624 $self
2581} 2625}
2582 2626
2583sub STORABLE_freeze { 2627sub STORABLE_freeze {
2584 my ($self, $cloning) = @_; 2628 my ($self, $cloning) = @_;
2585 2629
2586 $self->{path} 2630 $self->{path}
2587 or die "cannot serialise dc::UI::Image on non-loadable images\n"; 2631 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2588 2632
2589 $self->{path} 2633 $self->{path}
2590} 2634}
2591 2635
2592sub STORABLE_attach { 2636sub STORABLE_attach {
2623 glDisable GL_TEXTURE_2D; 2667 glDisable GL_TEXTURE_2D;
2624} 2668}
2625 2669
2626############################################################################# 2670#############################################################################
2627 2671
2628package dc::UI::ImageButton; 2672package DC::UI::ImageButton;
2629 2673
2630our @ISA = dc::UI::Image::; 2674our @ISA = DC::UI::Image::;
2631 2675
2632use dc::OpenGL; 2676use DC::OpenGL;
2633 2677
2634my %textures; 2678my %textures;
2635 2679
2636sub new { 2680sub new {
2637 my $class = shift; 2681 my $class = shift;
2640 padding_x => 4, 2684 padding_x => 4,
2641 padding_y => 4, 2685 padding_y => 4,
2642 fg => [1, 1, 1], 2686 fg => [1, 1, 1],
2643 active_fg => [0, 0, 1], 2687 active_fg => [0, 0, 1],
2644 can_hover => 1, 2688 can_hover => 1,
2645 align => 0, 2689 align => 0.5,
2646 valign => 0, 2690 valign => 0.5,
2647 can_events => 1, 2691 can_events => 1,
2648 @_ 2692 @_
2649 ); 2693 );
2650} 2694}
2651 2695
2665 1 2709 1
2666} 2710}
2667 2711
2668############################################################################# 2712#############################################################################
2669 2713
2670package dc::UI::VGauge; 2714package DC::UI::VGauge;
2671 2715
2672our @ISA = dc::UI::Base::; 2716our @ISA = DC::UI::Base::;
2673 2717
2674use List::Util qw(min max); 2718use List::Util qw(min max);
2675 2719
2676use dc::OpenGL; 2720use DC::OpenGL;
2677 2721
2678my %tex = ( 2722my %tex = (
2679 food => [ 2723 food => [
2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2725 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2682 ], 2726 ],
2683 grace => [ 2727 grace => [
2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2728 map { new_from_resource DC::Texture $_, mipmap => 1 }
2685 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2729 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2686 ], 2730 ],
2687 hp => [ 2731 hp => [
2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2733 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2690 ], 2734 ],
2691 mana => [ 2735 mana => [
2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2736 map { new_from_resource DC::Texture $_, mipmap => 1 }
2693 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2737 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2694 ], 2738 ],
2695); 2739);
2696 2740
2697# eg. VGauge->new (gauge => 'food'), default gauge: food 2741# eg. VGauge->new (gauge => 'food'), default gauge: food
2800 glDisable GL_TEXTURE_2D; 2844 glDisable GL_TEXTURE_2D;
2801} 2845}
2802 2846
2803############################################################################# 2847#############################################################################
2804 2848
2805package dc::UI::Progress; 2849package DC::UI::Progress;
2806 2850
2807our @ISA = dc::UI::Label::; 2851our @ISA = DC::UI::Label::;
2808 2852
2809use dc::OpenGL; 2853use DC::OpenGL;
2810 2854
2811sub new { 2855sub new {
2812 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2813 2857
2814 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2815 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9, 2865 fontsize => 0.9,
2820 valign => 0, 2866 valign => 0.5,
2821 align => 0, 2867 align => 0.5,
2822 can_events => 1, 2868 can_events => 1,
2823 ellipsise => 1, 2869 ellipsise => 1,
2824 label => "%d%%", 2870 label => "%d%%",
2825 %arg, 2871 %arg,
2826 ); 2872 );
2834 my ($self, $label) = @_; 2880 my ($self, $label) = @_;
2835 2881
2836 return if $self->{label} eq $label; 2882 return if $self->{label} eq $label;
2837 $self->{label} = $label; 2883 $self->{label} = $label;
2838 2884
2839 $self->dc::UI::Progress::set_value (0 + delete $self->{value}); 2885 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2840} 2886}
2841 2887
2842sub set_value { 2888sub set_value {
2843 my ($self, $value) = @_; 2889 my ($self, $value) = @_;
2844 2890
2859 my ($self) = @_; 2905 my ($self) = @_;
2860 2906
2861 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2864 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2866 2915
2867 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2869 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2871 } 2920 }
2872 2921
2873 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2875 2928
2876 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2877 2930
2878 { 2931 {
2879 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2881 } 2934 }
2882} 2935}
2883 2936
2884############################################################################# 2937#############################################################################
2885 2938
2886package dc::UI::ExperienceProgress; 2939package DC::UI::ExperienceProgress;
2887 2940
2888our @ISA = dc::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2889 2942
2890sub new { 2943sub new {
2891 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2892 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2893 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2894 tooltip => sub { 2950 tooltip => sub {
2895 my ($self) = @_; 2951 my ($self) = @_;
2896 2952
2897 sprintf "level %d\n%s points\n%s next level\n%s to go", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2954 $tt,
2898 $self->{lvl}, 2955 $self->{lvl},
2899 ::formsep ($self->{exp}), 2956 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}), 2957 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}), 2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2902 }, 2960 },
2903 %arg
2904 ); 2961 );
2905 2962
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN; 2964 if $::CONN;
2908 2965
2916 if $::CONN; 2973 if $::CONN;
2917 2974
2918 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2919} 2976}
2920 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2921sub set_value { 2992sub set_value {
2922 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2923 2994
2924 $self->{lvl} = $lvl; 2995 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2926 2997
2927 my $v = -1;
2928
2929 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2930 my $l0 = $table->[$lvl - 1];
2931 my $l1 = $table->[$lvl];
2932
2933 $self->{nxt} = $l1;
2934
2935 $v = ($exp - $l0) / ($l1 - $l0);
2936 }
2937
2938 $self->SUPER::set_value ($v); 2998 $self->SUPER::set_value ($self->_percent);
2939} 2999}
2940 3000
2941############################################################################# 3001#############################################################################
2942 3002
2943package dc::UI::Gauge; 3003package DC::UI::Gauge;
2944 3004
2945our @ISA = dc::UI::VBox::; 3005our @ISA = DC::UI::VBox::;
2946 3006
2947sub new { 3007sub new {
2948 my ($class, %arg) = @_; 3008 my ($class, %arg) = @_;
2949 3009
2950 my $self = $class->SUPER::new ( 3010 my $self = $class->SUPER::new (
2952 can_hover => 1, 3012 can_hover => 1,
2953 can_events => 1, 3013 can_events => 1,
2954 %arg, 3014 %arg,
2955 ); 3015 );
2956 3016
2957 $self->add ($self->{value} = new dc::UI::Label valign => +1, align => 0, template => "999"); 3017 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2958 $self->add ($self->{gauge} = new dc::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3018 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2959 $self->add ($self->{max} = new dc::UI::Label valign => -1, align => 0, template => "999"); 3019 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2960 3020
2961 $self 3021 $self
2962} 3022}
2963 3023
2964sub set_fontsize { 3024sub set_fontsize {
2985 $self->{value}->set_text ($val); 3045 $self->{value}->set_text ($val);
2986} 3046}
2987 3047
2988############################################################################# 3048#############################################################################
2989 3049
2990package dc::UI::Slider; 3050package DC::UI::Slider;
2991 3051
2992use strict; 3052use strict;
2993 3053
2994use dc::OpenGL; 3054use DC::OpenGL;
2995 3055
2996our @ISA = dc::UI::DrawBG::; 3056our @ISA = DC::UI::DrawBG::;
2997 3057
2998my @tex = 3058my @tex =
2999 map { new_from_file dc::Texture dc::find_rcfile $_ } 3059 map { new_from_resource DC::Texture $_ }
3000 qw(s1_slider.png s1_slider_bg.png); 3060 qw(s1_slider.png s1_slider_bg.png);
3001 3061
3002sub new { 3062sub new {
3003 my $class = shift; 3063 my $class = shift;
3004 3064
3041sub set_value { 3101sub set_value {
3042 my ($self, $value) = @_; 3102 my ($self, $value) = @_;
3043 3103
3044 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3104 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3045 3105
3046 $hi = $lo + 1 if $hi <= $lo; 3106 $hi = $lo if $hi < $lo;
3047 3107
3048 $page = $hi - $lo if $page > $hi - $lo; 3108 $value = $hi - $page if $value > $hi - $page;
3049
3050 $value = $lo if $value < $lo; 3109 $value = $lo if $value < $lo;
3051 $value = $hi - $page if $value > $hi - $page;
3052 3110
3053 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3111 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3054 if $unit; 3112 if $unit;
3055 3113
3056 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3114 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3100sub invoke_mouse_wheel { 3158sub invoke_mouse_wheel {
3101 my ($self, $ev) = @_; 3159 my ($self, $ev) = @_;
3102 3160
3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3161 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3104 3162
3105 my $pagepart = $ev->{mod} & dc::KMOD_SHIFT ? 1 : 0.2; 3163 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3106 3164
3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3165 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3108 3166
3109 1 3167 1
3110} 3168}
3120 my ($self) = @_; 3178 my ($self) = @_;
3121 3179
3122 unless ($self->{knob_w}) { 3180 unless ($self->{knob_w}) {
3123 $self->set_value ($self->{range}[0]); 3181 $self->set_value ($self->{range}[0]);
3124 3182
3125 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3183 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3126 my $range = ($hi - $page - $lo) || 1e-100; 3184 my $range = ($hi - $page - $lo) || 1e-10;
3127 3185
3128 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3186 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3129 3187
3130 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3188 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3131 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3189 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3132 3190
3133 $value = ($value - $lo) / $range; 3191 $value = ($value - $lo) / $range;
3163 glDisable GL_TEXTURE_2D; 3221 glDisable GL_TEXTURE_2D;
3164} 3222}
3165 3223
3166############################################################################# 3224#############################################################################
3167 3225
3168package dc::UI::ValSlider; 3226package DC::UI::ValSlider;
3169 3227
3170our @ISA = dc::UI::HBox::; 3228our @ISA = DC::UI::HBox::;
3171 3229
3172sub new { 3230sub new {
3173 my ($class, %arg) = @_; 3231 my ($class, %arg) = @_;
3174 3232
3175 my $range = delete $arg{range}; 3233 my $range = delete $arg{range};
3176 3234
3177 my $self = $class->SUPER::new ( 3235 my $self = $class->SUPER::new (
3178 slider => (new dc::UI::Slider expand => 1, range => $range), 3236 slider => (new DC::UI::Slider expand => 1, range => $range),
3179 entry => (new dc::UI::Label text => "", template => delete $arg{template}), 3237 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3180 to_value => sub { shift }, 3238 to_value => sub { shift },
3181 from_value => sub { shift }, 3239 from_value => sub { shift },
3182 %arg, 3240 %arg,
3183 ); 3241 );
3184 3242
3204sub set_range { shift->{slider}->set_range (@_) } 3262sub set_range { shift->{slider}->set_range (@_) }
3205sub set_value { shift->{slider}->set_value (@_) } 3263sub set_value { shift->{slider}->set_value (@_) }
3206 3264
3207############################################################################# 3265#############################################################################
3208 3266
3209package dc::UI::TextScroller; 3267package DC::UI::TextScroller;
3210 3268
3211our @ISA = dc::UI::HBox::; 3269our @ISA = DC::UI::HBox::;
3212 3270
3213use dc::OpenGL; 3271use DC::OpenGL;
3214 3272
3215sub new { 3273sub new {
3216 my $class = shift; 3274 my $class = shift;
3217 3275
3218 my $self = $class->SUPER::new ( 3276 my $self = $class->SUPER::new (
3220 can_events => 1, 3278 can_events => 1,
3221 indent => 0, 3279 indent => 0,
3222 #font => default_font 3280 #font => default_font
3223 @_, 3281 @_,
3224 3282
3225 layout => (new dc::Layout), 3283 layout => (new DC::Layout),
3226 par => [], 3284 par => [],
3227 max_par => 0, 3285 max_par => 0,
3228 height => 0, 3286 height => 0,
3229 children => [ 3287 children => [
3230 (new dc::UI::Empty expand => 1), 3288 (new DC::UI::Empty expand => 1),
3231 (new dc::UI::Slider vertical => 1), 3289 (new DC::UI::Slider vertical => 1),
3232 ], 3290 ],
3233 ); 3291 );
3234 3292
3235 $self->{children}[1]->connect (changed => sub { $self->update }); 3293 $self->{children}[1]->connect (changed => sub { $self->update });
3236 3294
3292 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3350 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3293 $layout->set_markup ($para->{markup}); 3351 $layout->set_markup ($para->{markup});
3294 3352
3295 $layout->set_shapes ( 3353 $layout->set_shapes (
3296 map 3354 map
3297 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3355 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3298 @{$para->{widget}} 3356 @{$para->{widget}}
3299 ); 3357 );
3300 3358
3301 $layout 3359 $layout
3302} 3360}
3419 $ROOT->on_post_alloc ($self => sub { 3477 $ROOT->on_post_alloc ($self => sub {
3420 $self->force_uptodate; 3478 $self->force_uptodate;
3421 3479
3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3480 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3423 3481
3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub { 3482 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3425 glClearColor 0, 0, 0, 0; 3483 glClearColor 0, 0, 0, 0;
3426 glClear GL_COLOR_BUFFER_BIT; 3484 glClear GL_COLOR_BUFFER_BIT;
3427 3485
3428 package dc::UI::Base; 3486 package DC::UI::Base;
3429 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3487 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3430 (0, 0, $self->{w}, $self->{h}); 3488 (0, 0, $self->{w}, $self->{h});
3431 3489
3432 my $top = int $self->{children}[1]{range}[0]; 3490 my $top = int $self->{children}[1]{range}[0];
3433 3491
3492 $self->{children}[1]->draw; 3550 $self->{children}[1]->draw;
3493} 3551}
3494 3552
3495############################################################################# 3553#############################################################################
3496 3554
3497package dc::UI::Animator; 3555package DC::UI::Animator;
3498 3556
3499use dc::OpenGL; 3557use DC::OpenGL;
3500 3558
3501our @ISA = dc::UI::Bin::; 3559our @ISA = DC::UI::Bin::;
3502 3560
3503sub moveto { 3561sub moveto {
3504 my ($self, $x, $y) = @_; 3562 my ($self, $x, $y) = @_;
3505 3563
3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3564 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3534 glPopMatrix; 3592 glPopMatrix;
3535} 3593}
3536 3594
3537############################################################################# 3595#############################################################################
3538 3596
3539package dc::UI::Flopper; 3597package DC::UI::Flopper;
3540 3598
3541our @ISA = dc::UI::Button::; 3599our @ISA = DC::UI::Button::;
3542 3600
3543sub new { 3601sub new {
3544 my $class = shift; 3602 my $class = shift;
3545 3603
3546 my $self = $class->SUPER::new ( 3604 my $self = $class->SUPER::new (
3558 $self->{other}->toggle_visibility; 3616 $self->{other}->toggle_visibility;
3559} 3617}
3560 3618
3561############################################################################# 3619#############################################################################
3562 3620
3563package dc::UI::Tooltip; 3621package DC::UI::Tooltip;
3564 3622
3565our @ISA = dc::UI::Bin::; 3623our @ISA = DC::UI::Bin::;
3566 3624
3567use dc::OpenGL; 3625use DC::OpenGL;
3568 3626
3569sub new { 3627sub new {
3570 my $class = shift; 3628 my $class = shift;
3571 3629
3572 $class->SUPER::new ( 3630 $class->SUPER::new (
3579 my ($self, $widget) = @_; 3637 my ($self, $widget) = @_;
3580 3638
3581 my $tip = $widget->{tooltip}; 3639 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip; 3640 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583 3641
3584 $tip = dc::Pod::section_label tooltip => $1 3642 $tip = DC::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/; 3643 if $tip =~ /^#(.*)$/;
3586 3644
3587 if ($ENV{CFPLUS_DEBUG} & 2) { 3645 if ($ENV{CFPLUS_DEBUG} & 2) {
3588 $tip .= "\n\n" . (ref $widget) . "\n" 3646 $tip .= "\n\n" . (ref $widget) . "\n"
3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3647 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3592 } 3650 }
3593 3651
3594 $tip =~ s/^\n+//; 3652 $tip =~ s/^\n+//;
3595 $tip =~ s/\n+$//; 3653 $tip =~ s/\n+$//;
3596 3654
3597 $self->add (new dc::UI::Label 3655 $self->add (new DC::UI::Label
3656 fg => $DC::THEME{tooltip_fg},
3598 markup => $tip, 3657 markup => $tip,
3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3658 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3659 align => 0,
3600 fontsize => 0.8, 3660 fontsize => 0.8,
3601 style => 1, # FLAG_INVERSE 3661 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3602 ellipsise => 0, 3662 ellipsise => 0,
3603 font => ($widget->{tooltip_font} || $::FONT_PROP), 3663 font => ($widget->{tooltip_font} || $::FONT_PROP),
3604 ); 3664 );
3605} 3665}
3606 3666
3643sub _draw { 3703sub _draw {
3644 my ($self) = @_; 3704 my ($self) = @_;
3645 3705
3646 my ($w, $h) = @$self{qw(w h)}; 3706 my ($w, $h) = @$self{qw(w h)};
3647 3707
3648 glColor 1, 0.8, 0.4; 3708 glColor @{ $DC::THEME{tooltip_bg} };
3649 glRect 0, 0, $w, $h; 3709 glRect 0, 0, $w, $h;
3650 3710
3651 glColor 0, 0, 0; 3711 glColor @{ $DC::THEME{tooltip_border} };
3652 glRect_lineloop .5, .5, $w + .5, $h + .5; 3712 glRect_lineloop .5, .5, $w + .5, $h + .5;
3653 3713
3654 glTranslate 2, 2; 3714 glTranslate 2, 2;
3655 3715
3656 $self->SUPER::_draw; 3716 $self->SUPER::_draw;
3657} 3717}
3658 3718
3659############################################################################# 3719#############################################################################
3660 3720
3661package dc::UI::Face; 3721package DC::UI::Face;
3662 3722
3663our @ISA = dc::UI::DrawBG::; 3723our @ISA = DC::UI::DrawBG::;
3664 3724
3665use dc::OpenGL; 3725use DC::OpenGL;
3666 3726
3667sub new { 3727sub new {
3668 my $class = shift; 3728 my $class = shift;
3669 3729
3670 my $self = $class->SUPER::new ( 3730 my $self = $class->SUPER::new (
3673 aspect => 1, 3733 aspect => 1,
3674 can_events => 0, 3734 can_events => 0,
3675 @_, 3735 @_,
3676 ); 3736 );
3677 3737
3678 if ($self->{anim} && $self->{animspeed}) {
3679 dc::weaken (my $widget = $self);
3680
3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3683 return unless $::CONN;
3684
3685 my $w = $widget
3686 or return;
3687
3688 ++$w->{frame};
3689 $w->update_face;
3690
3691 # somehow, $widget can go away
3692 $w->update;
3693 $w->update_timer;
3694 };
3695
3696 $self->update_face;
3697 $self->update_timer; 3738 $self->update_anim;
3698 }
3699 3739
3700 $self 3740 $self
3701} 3741}
3702 3742
3703sub update_timer { 3743sub update_timer {
3726 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3727 } 3767 }
3728 } 3768 }
3729 } 3769 }
3730 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3731 } 3799 }
3732} 3800}
3733 3801
3734sub size_request { 3802sub size_request {
3735 my ($self) = @_; 3803 my ($self) = @_;
3757 return unless $self->{visible}; 3825 return unless $self->{visible};
3758 3826
3759 $self->SUPER::update; 3827 $self->SUPER::update;
3760} 3828}
3761 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3762sub invoke_visibility_change { 3851sub invoke_visibility_change {
3763 my ($self) = @_; 3852 my ($self) = @_;
3764 3853
3765 $self->update_timer; 3854 $self->update_timer;
3766 3855
3790 $self->SUPER::destroy; 3879 $self->SUPER::destroy;
3791} 3880}
3792 3881
3793############################################################################# 3882#############################################################################
3794 3883
3795package dc::UI::Buttonbar; 3884package DC::UI::Buttonbar;
3796 3885
3797our @ISA = dc::UI::HBox::; 3886our @ISA = DC::UI::HBox::;
3798 3887
3799# TODO: should actually wrap buttons and other goodies. 3888# TODO: should actually wrap buttons and other goodies.
3800 3889
3801############################################################################# 3890#############################################################################
3802 3891
3803package dc::UI::Menu; 3892package DC::UI::Menu;
3804 3893
3805our @ISA = dc::UI::Toplevel::; 3894our @ISA = DC::UI::Toplevel::;
3806 3895
3807use dc::OpenGL; 3896use DC::OpenGL;
3808 3897
3809sub new { 3898sub new {
3810 my $class = shift; 3899 my $class = shift;
3811 3900
3812 my $self = $class->SUPER::new ( 3901 my $self = $class->SUPER::new (
3813 items => [], 3902 items => [],
3814 z => 100, 3903 z => 100,
3815 @_, 3904 @_,
3816 ); 3905 );
3817 3906
3818 $self->add ($self->{vbox} = new dc::UI::VBox); 3907 $self->add ($self->{vbox} = new DC::UI::VBox);
3819 3908
3820 for my $item (@{ $self->{items} }) { 3909 for my $item (@{ $self->{items} }) {
3821 my ($widget, $cb, $tooltip) = @$item; 3910 my ($widget, $cb, $tooltip) = @$item;
3822 3911
3823 # handle various types of items, only text for now 3912 # handle various types of items, only text for now
3824 if (!ref $widget) { 3913 if (!ref $widget) {
3825 if ($widget =~ /\t/) { 3914 if ($widget =~ /\t/) {
3826 my ($left, $right) = split /\t/, $widget, 2; 3915 my ($left, $right) = split /\t/, $widget, 2;
3827 3916
3828 $widget = new dc::UI::HBox 3917 $widget = new DC::UI::HBox
3829 can_hover => 1, 3918 can_hover => 1,
3830 can_events => 1, 3919 can_events => 1,
3831 tooltip => $tooltip, 3920 tooltip => $tooltip,
3832 children => [ 3921 children => [
3833 (new dc::UI::Label markup => $left, expand => 1), 3922 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3834 (new dc::UI::Label markup => $right, align => +1), 3923 (new DC::UI::Label markup => $right, align => 1),
3835 ], 3924 ],
3836 ; 3925 ;
3837 3926
3838 } else { 3927 } else {
3839 $widget = new dc::UI::Label 3928 $widget = new DC::UI::Label
3840 can_hover => 1, 3929 can_hover => 1,
3841 can_events => 1, 3930 can_events => 1,
3931 align => 0,
3842 markup => $widget, 3932 markup => $widget,
3843 tooltip => $tooltip; 3933 tooltip => $tooltip;
3844 } 3934 }
3845 } 3935 }
3846 3936
3861 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3862 $GRAB = $self; 3952 $GRAB = $self;
3863 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3864 3954
3865 $self->show; 3955 $self->show;
3866 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3867} 3965}
3868 3966
3869sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3870 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3871 3969
3892 1 3990 1
3893} 3991}
3894 3992
3895############################################################################# 3993#############################################################################
3896 3994
3897package dc::UI::Multiplexer; 3995package DC::UI::Multiplexer;
3898 3996
3899our @ISA = dc::UI::Container::; 3997our @ISA = DC::UI::Container::;
3900 3998
3901sub new { 3999sub new {
3902 my $class = shift; 4000 my $class = shift;
3903 4001
3904 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3905 @_, 4003 @_,
3906 ); 4004 );
3907 4005
3908 $self->{current} = $self->{children}[0] 4006 $self->set_current_page (0);
3909 if @{ $self->{children} };
3910 4007
3911 $self 4008 $self
3912} 4009}
3913 4010
3914sub add { 4011sub add {
3915 my ($self, @widgets) = @_; 4012 my ($self, @widgets) = @_;
3916 4013
3917 $self->SUPER::add (@widgets); 4014 $self->SUPER::add (@widgets);
3918 4015
3919 $self->{current} = $self->{children}[0] 4016 $self->set_current_page (0)
3920 if @{ $self->{children} }; 4017 if @widgets == @{ $self->{children} };
3921} 4018}
3922 4019
3923sub get_current_page { 4020sub get_current_page {
3924 my ($self) = @_; 4021 my ($self) = @_;
3925 4022
3931 4028
3932 my $widget = ref $page_or_widget 4029 my $widget = ref $page_or_widget
3933 ? $page_or_widget 4030 ? $page_or_widget
3934 : $self->{children}[$page_or_widget]; 4031 : $self->{children}[$page_or_widget];
3935 4032
4033 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4034
3936 $self->{current} = $widget; 4035 if (($self->{current} = $widget)) {
4036 $self->{current}->set_visible if $self->{current} && $self->{visible};
3937 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4037 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3938 4038
3939 $self->emit (page_changed => $self->{current}); 4039 $self->emit (page_changed => $self->{current});
4040 }
3940 4041
3941 $self->realloc; 4042 $self->realloc;
3942} 4043}
3943 4044
3944sub visible_children { 4045sub visible_children {
3945 $_[0]{current} 4046 $_[0]{current} || ()
3946} 4047}
3947 4048
3948sub size_request { 4049sub size_request {
3949 my ($self) = @_; 4050 my ($self) = @_;
3950 4051
4052 $self->{current}
3951 $self->{current}->size_request 4053 ? $self->{current}->size_request
4054 : (0, 0)
3952} 4055}
3953 4056
3954sub invoke_size_allocate { 4057sub invoke_size_allocate {
3955 my ($self, $w, $h) = @_; 4058 my ($self, $w, $h) = @_;
3956 4059
3957 $self->{current}->configure (0, 0, $w, $h); 4060 $self->{current}->configure (0, 0, $w, $h)
4061 if $self->{current};
3958 4062
3959 1 4063 1
3960} 4064}
3961 4065
3962sub _draw { 4066sub _draw {
3963 my ($self) = @_; 4067 my ($self) = @_;
3964 4068
3965 $self->{current}->draw; 4069 $self->{current}->draw
4070 if $self->{current};
3966} 4071}
3967 4072
3968############################################################################# 4073#############################################################################
3969 4074
3970package dc::UI::Notebook; 4075package DC::UI::Notebook;
3971 4076
3972use dc::OpenGL; 4077use DC::OpenGL;
3973 4078
3974our @ISA = dc::UI::VBox::; 4079our @ISA = DC::UI::VBox::;
3975 4080
3976sub new { 4081sub new {
3977 my $class = shift; 4082 my $class = shift;
3978 4083
3979 my $self = $class->SUPER::new ( 4084 my $self = $class->SUPER::new (
3980 buttonbar => (new dc::UI::Buttonbar), 4085 buttonbar => (new DC::UI::Buttonbar),
3981 multiplexer => (new dc::UI::Multiplexer expand => 1), 4086 multiplexer => (new DC::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2], 4087 active_outline => [.7, .7, 0.2],
3983 # filter => # will be put between multiplexer and $self 4088 # filter => # will be put between multiplexer and $self
3984 @_, 4089 @_,
3985 ); 4090 );
3986 4091
3995 4100
3996 for my $child (@$widgets) { 4101 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child; 4102 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do { 4103 $child->{c_tab_} ||= do {
3999 my $tab = 4104 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "dc::UI::Base") 4105 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4001 ? $child->{c_tab} 4106 ? $child->{c_tab}
4002 : new dc::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4107 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003 4108
4004 $tab->connect (activate => sub { 4109 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child); 4110 $wself->set_current_page ($child);
4006 }); 4111 });
4007 4112
4037} 4142}
4038 4143
4039sub pages { 4144sub pages {
4040 my ($self) = @_; 4145 my ($self) = @_;
4041 $self->{multiplexer}->children 4146 $self->{multiplexer}->children
4147}
4148
4149sub page_index {
4150 my ($self, $widget) = @_;
4151
4152 my $i = 0;
4153 for ($self->pages) {
4154 if ($_ eq $widget) { return $i };
4155 $i++;
4156 }
4157
4158 undef
4042} 4159}
4043 4160
4044sub add_tab { 4161sub add_tab {
4045 my ($self, $title, $widget, $tooltip) = @_; 4162 my ($self, $title, $widget, $tooltip) = @_;
4046 4163
4083 } 4200 }
4084} 4201}
4085 4202
4086############################################################################# 4203#############################################################################
4087 4204
4088package dc::UI::Selector; 4205package DC::UI::Selector;
4089 4206
4090use utf8; 4207use utf8;
4091 4208
4092our @ISA = dc::UI::Button::; 4209our @ISA = DC::UI::Button::;
4093 4210
4094sub new { 4211sub new {
4095 my $class = shift; 4212 my $class = shift;
4096 4213
4097 my $self = $class->SUPER::new ( 4214 my $self = $class->SUPER::new (
4114 my ($value, $title, $tooltip) = @$_; 4231 my ($value, $title, $tooltip) = @$_;
4115 4232
4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4233 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4117 } 4234 }
4118 4235
4119 dc::UI::Menu->new (items => \@menu_items)->popup ($ev); 4236 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4120} 4237}
4121 4238
4122sub _set_value { 4239sub _set_value {
4123 my ($self, $value) = @_; 4240 my ($self, $value) = @_;
4124 4241
4147 $self->_set_value ($self->{value}); 4264 $self->_set_value ($self->{value});
4148} 4265}
4149 4266
4150############################################################################# 4267#############################################################################
4151 4268
4152package dc::UI::Statusbox; 4269package DC::UI::Statusbox;
4153 4270
4154our @ISA = dc::UI::VBox::; 4271our @ISA = DC::UI::VBox::;
4155 4272
4156sub new { 4273sub new {
4157 my $class = shift; 4274 my $class = shift;
4158 4275
4159 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4160 fontsize => 0.8, 4277 fontsize => 0.8,
4161 @_, 4278 @_,
4162 ); 4279 );
4163 4280
4164 dc::weaken (my $this = $self); 4281 DC::weaken (my $this = $self);
4165 4282
4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4283 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4167 4284
4168 $self 4285 $self
4169} 4286}
4170 4287
4171sub reorder { 4288sub reorder {
4172 my ($self) = @_; 4289 my ($self) = @_;
4173 my $NOW = Time::HiRes::time; 4290 my $NOW = EV::time;
4174 4291
4175 # freeze display when hovering over any label 4292 # freeze display when hovering over any label
4176 return if $dc::UI::TOOLTIP->{owner} 4293 return if $DC::UI::TOOLTIP->{owner}
4177 && grep $dc::UI::TOOLTIP->{owner} == $_->{label}, 4294 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4178 values %{ $self->{item} }; 4295 values %{ $self->{item} };
4179 4296
4180 while (my ($k, $v) = each %{ $self->{item} }) { 4297 while (my ($k, $v) = each %{ $self->{item} }) {
4181 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4298 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4182 } 4299 }
4203 for ($short) { 4320 for ($short) {
4204 s/^\s+//; 4321 s/^\s+//;
4205 s/\s+/ /g; 4322 s/\s+/ /g;
4206 } 4323 }
4207 4324
4208 new dc::UI::Label 4325 new DC::UI::Label
4209 markup => $short, 4326 markup => $short,
4210 tooltip => $item->{tooltip}, 4327 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4328 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4329 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4330 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4331 max_w => $::WIDTH * 0.44,
4332 align => 0,
4215 fg => [@{ $item->{fg} }], 4333 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4334 can_events => 1,
4217 can_hover => 1 4335 can_hover => 1
4218 }; 4336 };
4219 4337
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4345 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4346 }
4229 4347
4230 push @widgets, $label; 4348 push @widgets, $label;
4231 } 4349 }
4350
4351 my $hash = join ",", @widgets;
4352 return if $hash eq $self->{last_widget_hash};
4353 $self->{last_widget_hash} = $hash;
4232 4354
4233 $self->clear; 4355 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4356 $self->SUPER::add (reverse @widgets);
4235} 4357}
4236 4358
4292 $self->SUPER::destroy; 4414 $self->SUPER::destroy;
4293} 4415}
4294 4416
4295############################################################################# 4417#############################################################################
4296 4418
4297package dc::UI::Root; 4419package DC::UI::Root;
4298 4420
4299our @ISA = dc::UI::Container::; 4421our @ISA = DC::UI::Container::;
4300 4422
4301use List::Util qw(min max); 4423use List::Util qw(min max);
4302 4424
4303use dc::OpenGL; 4425use DC::OpenGL;
4304 4426
4305sub new { 4427sub new {
4306 my $class = shift; 4428 my $class = shift;
4307 4429
4308 my $self = $class->SUPER::new ( 4430 my $self = $class->SUPER::new (
4309 visible => 1, 4431 visible => 1,
4310 @_, 4432 @_,
4311 ); 4433 );
4312 4434
4313 dc::weaken ($self->{root} = $self); 4435 DC::weaken ($self->{root} = $self);
4314 4436
4315 $self 4437 $self
4316} 4438}
4317 4439
4318sub size_request { 4440sub size_request {
4443 4565
4444 delete $queue{$widget+0}; 4566 delete $queue{$widget+0};
4445 4567
4446 my ($w, $h) = $widget->size_request; 4568 my ($w, $h) = $widget->size_request;
4447 4569
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4570 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4571 $h += $widget->{padding_y} * 2;
4572
4573 $w = max $widget->{min_w}, $w;
4574 $h = max $widget->{min_h}, $h;
4450 4575
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4576 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4577 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4578
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4579 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4646 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4647 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4648 glLoadIdentity;
4524 4649
4525 { 4650 {
4526 package dc::UI::Base; 4651 package DC::UI::Base;
4527 4652
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4653 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4654 (0, 0, $self->{w}, $self->{h});
4530 4655
4531 $self->_draw; 4656 $self->_draw;
4532 } 4657 }
4533} 4658}
4534 4659
4535############################################################################# 4660#############################################################################
4536 4661
4537package dc::UI; 4662package DC::UI;
4538 4663
4539$ROOT = new dc::UI::Root; 4664$ROOT = new DC::UI::Root;
4540$TOOLTIP = new dc::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4666
45421 46671
4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines