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.472 by root, Wed Dec 24 04:09:27 2008 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 DC;
9use dc::Pod; 9use DC::Pod;
10use dc::Texture; 10use DC::Texture;
11 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13 13
14our $LAYOUT; 14our $LAYOUT;
15our $ROOT; 15our $ROOT;
111 111
112 if ($GRAB) { 112 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 114 # mousewheel
115 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & dc::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
117 117
118 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
119 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
120 120
121 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
211 reconfigure_widgets; 211 reconfigure_widgets;
212} 212}
213 213
214############################################################################# 214#############################################################################
215 215
216package dc::UI::Event; 216package DC::UI::Event;
217 217
218sub xy { 218sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220} 220}
221 221
222############################################################################# 222#############################################################################
223 223
224package dc::UI::Base; 224package DC::UI::Base;
225 225
226use strict; 226use strict;
227 227
228use dc::OpenGL; 228use DC::OpenGL;
229 229
230sub new { 230sub new {
231 my $class = shift; 231 my $class = shift;
232 232
233 my $self = bless { 233 my $self = bless {
238 h => undef, 238 h => undef,
239 can_events => 1, 239 can_events => 1,
240 @_ 240 @_
241 }, $class; 241 }, $class;
242 242
243 dc::weaken ($dc::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
244 244
245 for (keys %$self) { 245 for (keys %$self) {
246 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
247 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
248 } 248 }
249 } 249 }
250 250
251 if (my $layout = $dc::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
252 $self->{x} = $layout->{x} * $dc::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $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}; 253 $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}; 254 $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}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
256 256
257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
259 259
260 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
278sub show { 278sub show {
279 my ($self) = @_; 279 my ($self) = @_;
280 280
281 return if $self->{parent}; 281 return if $self->{parent};
282 282
283 $dc::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
284} 284}
285 285
286sub set_visible { 286sub set_visible {
287 my ($self) = @_; 287 my ($self) = @_;
288 288
289 return if $self->{visible}; 289 return if $self->{visible};
290
291 $self->{parent} && $self->{parent}{root}#d#
292 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
290 293
291 $self->{root} = $self->{parent}{root}; 294 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 295 $self->{visible} = $self->{parent}{visible} + 1;
293 296
294 $self->emit (visibility_change => 1); 297 $self->emit (visibility_change => 1);
295 298
296 $self->realloc if !exists $self->{req_w}; 299 $self->realloc if !exists $self->{req_w};
297 300
298 $_->set_visible for $self->children; 301 $_->set_visible for $self->visible_children;
299} 302}
300 303
301sub set_invisible { 304sub set_invisible {
302 my ($self) = @_; 305 my ($self) = @_;
303 306
309 delete $self->{root}; 312 delete $self->{root};
310 313
311 undef $GRAB if $GRAB == $self; 314 undef $GRAB if $GRAB == $self;
312 undef $HOVER if $HOVER == $self; 315 undef $HOVER if $HOVER == $self;
313 316
314 $dc::UI::TOOLTIP_WATCHER->invoke 317 $DC::UI::TOOLTIP_WATCHER->invoke
315 if $TOOLTIP->{owner} == $self; 318 if $TOOLTIP->{owner} == $self;
316 319
317 $self->emit ("focus_out"); 320 $self->emit ("focus_out");
318 $self->emit (visibility_change => 0); 321 $self->emit (visibility_change => 0);
319} 322}
368 371
369# traverse the widget chain up to find the maximum "physical" size constraints 372# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh { 373sub get_max_wh {
371 my ($self) = @_; 374 my ($self) = @_;
372 375
376 my ($w, $h) = @$self{qw(max_w max_h)};
377
378 if ($w <= 0 || $h <= 0) {
379 my ($mw, $mh) = $self->{parent}
373 return $self->{parent}->get_max_wh 380 ? $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT) 381 : ($::WIDTH, $::HEIGHT);
382
383 $w = $mw if $w <= 0;
384 $h = $mh if $h <= 0;
385 }
386
387 ($w, $h)
377} 388}
378 389
379sub size_request { 390sub size_request {
380 require Carp; 391 require Carp;
381 Carp::confess "size_request is abstract"; 392 Carp::confess "size_request is abstract";
389 my ($self, $x, $y, $w, $h) = @_; 400 my ($self, $x, $y, $w, $h) = @_;
390 401
391 if ($self->{aspect}) { 402 if ($self->{aspect}) {
392 my ($ow, $oh) = ($w, $h); 403 my ($ow, $oh) = ($w, $h);
393 404
394 $w = List::Util::min $w, dc::ceil $h * $self->{aspect}; 405 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
395 $h = List::Util::min $h, dc::ceil $w / $self->{aspect}; 406 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
396 407
397 # use alignment to adjust x, y 408 # use alignment to adjust x, y
398 409
399 $x += int 0.5 * ($ow - $w); 410 $x += int 0.5 * ($ow - $w);
400 $y += int 0.5 * ($oh - $h); 411 $y += int 0.5 * ($oh - $h);
441 452
442 return if $self->{tooltip} eq $tooltip; 453 return if $self->{tooltip} eq $tooltip;
443 454
444 $self->{tooltip} = $tooltip; 455 $self->{tooltip} = $tooltip;
445 456
446 if ($dc::UI::TOOLTIP->{owner} == $self) { 457 if ($DC::UI::TOOLTIP->{owner} == $self) {
447 delete $dc::UI::TOOLTIP->{owner}; 458 delete $DC::UI::TOOLTIP->{owner};
448 $dc::UI::TOOLTIP_WATCHER->invoke; 459 $DC::UI::TOOLTIP_WATCHER->invoke;
449 } 460 }
450} 461}
451 462
452# translate global coordinates to local coordinate system 463# translate global coordinates to local coordinate system
453sub coord2local { 464sub coord2local {
519sub connect { 530sub connect {
520 my ($self, $signal, $cb) = @_; 531 my ($self, $signal, $cb) = @_;
521 532
522 push @{ $self->{signal_cb}{$signal} }, $cb; 533 push @{ $self->{signal_cb}{$signal} }, $cb;
523 534
524 defined wantarray and dc::guard { 535 defined wantarray and DC::guard {
525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
526 @{ $self->{signal_cb}{$signal} }; 537 @{ $self->{signal_cb}{$signal} };
527 } 538 }
528} 539}
529 540
567# in .xs 578# in .xs
568 579
569sub set_parent { 580sub set_parent {
570 my ($self, $parent) = @_; 581 my ($self, $parent) = @_;
571 582
572 dc::weaken ($self->{parent} = $parent); 583 DC::weaken ($self->{parent} = $parent);
573 $self->set_visible if $parent->{visible}; 584 $self->set_visible if $parent->{visible};
574} 585}
575 586
576sub realloc { 587sub realloc {
577 my ($self) = @_; 588 my ($self) = @_;
615} 626}
616 627
617sub DESTROY { 628sub DESTROY {
618 my ($self) = @_; 629 my ($self) = @_;
619 630
620 return if dc::in_destruct; 631 return if DC::in_destruct;
621 632
622 local $@; 633 local $@;
623 eval { $self->destroy }; 634 eval { $self->destroy };
624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 635 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
625 636
626 delete $WIDGET{$self+0}; 637 delete $WIDGET{$self+0};
627} 638}
628 639
629############################################################################# 640#############################################################################
630 641
631package dc::UI::DrawBG; 642package DC::UI::DrawBG;
632 643
633our @ISA = dc::UI::Base::; 644our @ISA = DC::UI::Base::;
634 645
635use strict; 646use strict;
636use dc::OpenGL; 647use DC::OpenGL;
637 648
638sub new { 649sub new {
639 my $class = shift; 650 my $class = shift;
640 651
641 $class->SUPER::new ( 652 $class->SUPER::new (
663 } 674 }
664} 675}
665 676
666############################################################################# 677#############################################################################
667 678
668package dc::UI::Empty; 679package DC::UI::Empty;
669 680
670our @ISA = dc::UI::Base::; 681our @ISA = DC::UI::Base::;
671 682
672sub new { 683sub new {
673 my ($class, %arg) = @_; 684 my ($class, %arg) = @_;
674 $class->SUPER::new (can_events => 0, %arg); 685 $class->SUPER::new (can_events => 0, %arg);
675} 686}
682 693
683sub draw { } 694sub draw { }
684 695
685############################################################################# 696#############################################################################
686 697
687package dc::UI::Container; 698package DC::UI::Container;
688 699
689our @ISA = dc::UI::Base::; 700our @ISA = DC::UI::Base::;
690 701
691sub new { 702sub new {
692 my ($class, %arg) = @_; 703 my ($class, %arg) = @_;
693 704
694 my $children = delete $arg{children}; 705 my $children = delete $arg{children};
788 $_->draw for $self->visible_children; 799 $_->draw for $self->visible_children;
789} 800}
790 801
791############################################################################# 802#############################################################################
792 803
793package dc::UI::Bin; 804package DC::UI::Bin;
794 805
795our @ISA = dc::UI::Container::; 806our @ISA = DC::UI::Container::;
796 807
797sub new { 808sub new {
798 my ($class, %arg) = @_; 809 my ($class, %arg) = @_;
799 810
800 my $child = (delete $arg{child}) || new dc::UI::Empty::; 811 my $child = (delete $arg{child}) || new DC::UI::Empty::;
801 812
802 $class->SUPER::new (children => [$child], %arg) 813 $class->SUPER::new (children => [$child], %arg)
803} 814}
804 815
805sub add { 816sub add {
812sub remove { 823sub remove {
813 my ($self, $widget) = @_; 824 my ($self, $widget) = @_;
814 825
815 $self->SUPER::remove ($widget); 826 $self->SUPER::remove ($widget);
816 827
817 $self->{children} = [new dc::UI::Empty] 828 $self->{children} = [new DC::UI::Empty]
818 unless @{$self->{children}}; 829 unless @{$self->{children}};
819} 830}
820 831
821sub child { $_[0]->{children}[0] } 832sub child { $_[0]->{children}[0] }
822 833
833} 844}
834 845
835############################################################################# 846#############################################################################
836# back-buffered drawing area 847# back-buffered drawing area
837 848
838package dc::UI::Window; 849package DC::UI::Window;
839 850
840our @ISA = dc::UI::Bin::; 851our @ISA = DC::UI::Bin::;
841 852
842use dc::OpenGL; 853use DC::OpenGL;
843 854
844sub new { 855sub new {
845 my ($class, %arg) = @_; 856 my ($class, %arg) = @_;
846 857
847 my $self = $class->SUPER::new (%arg); 858 my $self = $class->SUPER::new (%arg);
869} 880}
870 881
871sub render_child { 882sub render_child {
872 my ($self) = @_; 883 my ($self) = @_;
873 884
874 $self->{texture} = new_from_opengl dc::Texture $self->{w}, $self->{h}, sub { 885 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
875 glClearColor 0, 0, 0, 0; 886 glClearColor 0, 0, 0, 0;
876 glClear GL_COLOR_BUFFER_BIT; 887 glClear GL_COLOR_BUFFER_BIT;
877 888
878 { 889 {
879 package dc::UI::Base; 890 package DC::UI::Base;
880 891
881 local ($draw_x, $draw_y, $draw_w, $draw_h) = 892 local ($draw_x, $draw_y, $draw_w, $draw_h) =
882 (0, 0, $self->{w}, $self->{h}); 893 (0, 0, $self->{w}, $self->{h});
883 894
884 $self->_render; 895 $self->_render;
901 glDisable GL_TEXTURE_2D; 912 glDisable GL_TEXTURE_2D;
902} 913}
903 914
904############################################################################# 915#############################################################################
905 916
906package dc::UI::ViewPort; 917package DC::UI::ViewPort;
907 918
908use List::Util qw(min max); 919use List::Util qw(min max);
909 920
910our @ISA = dc::UI::Window::; 921our @ISA = DC::UI::Window::;
911 922
912sub new { 923sub new {
913 my $class = shift; 924 my $class = shift;
914 925
915 $class->SUPER::new ( 926 $class->SUPER::new (
999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1001 ) { 1012 ) {
1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1013 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1003 } else { 1014 } else {
1004 $self->dc::UI::Base::find_widget ($x, $y) 1015 $self->DC::UI::Base::find_widget ($x, $y)
1005 } 1016 }
1006} 1017}
1007 1018
1008sub _render { 1019sub _render {
1009 my ($self) = @_; 1020 my ($self) = @_;
1010 1021
1011 local $dc::UI::Base::draw_x = $dc::UI::Base::draw_x - $self->{view_x}; 1022 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}; 1023 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1013 1024
1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1025 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1015 1026
1016 $self->SUPER::_render; 1027 $self->SUPER::_render;
1017} 1028}
1018 1029
1019############################################################################# 1030#############################################################################
1020 1031
1021package dc::UI::ScrolledWindow; 1032package DC::UI::ScrolledWindow;
1022 1033
1023our @ISA = dc::UI::Table::; 1034our @ISA = DC::UI::Table::;
1024 1035
1025sub new { 1036sub new {
1026 my ($class, %arg) = @_; 1037 my ($class, %arg) = @_;
1027 1038
1028 my $child = delete $arg{child}; 1039 my $child = delete $arg{child};
1029 1040
1030 my $self; 1041 my $self;
1031 1042
1032 my $hslider = new dc::UI::Slider 1043 my $hslider = new DC::UI::Slider
1033 c_col => 0, 1044 c_col => 0,
1034 c_row => 1, 1045 c_row => 1,
1035 vertical => 0, 1046 vertical => 0,
1036 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1037 on_changed => sub { 1048 on_changed => sub {
1038 $self->{hpos} = $_[1]; 1049 $self->{hpos} = $_[1];
1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1051 },
1041 ; 1052 ;
1042 1053
1043 my $vslider = new dc::UI::Slider 1054 my $vslider = new DC::UI::Slider
1044 c_col => 1, 1055 c_col => 1,
1045 c_row => 0, 1056 c_row => 0,
1046 vertical => 1, 1057 vertical => 1,
1047 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1048 on_changed => sub { 1059 on_changed => sub {
1060 col_expand => [1, 0], 1071 col_expand => [1, 0],
1061 row_expand => [1, 0], 1072 row_expand => [1, 0],
1062 %arg, 1073 %arg,
1063 ); 1074 );
1064 1075
1065 $self->{vp} = new dc::UI::ViewPort 1076 $self->{vp} = new DC::UI::ViewPort
1066 c_col => 0, 1077 c_col => 0,
1067 c_row => 0, 1078 c_row => 0,
1068 expand => 1, 1079 expand => 1,
1069 scroll_x => $self->{scroll_x}, 1080 scroll_x => $self->{scroll_x},
1070 scroll_y => $self->{scroll_y}, 1081 scroll_y => $self->{scroll_y},
1196 $self->SUPER::invoke_size_allocate ($w, $h) 1207 $self->SUPER::invoke_size_allocate ($w, $h)
1197} 1208}
1198 1209
1199############################################################################# 1210#############################################################################
1200 1211
1201package dc::UI::Frame; 1212package DC::UI::Frame;
1202 1213
1203our @ISA = dc::UI::Bin::; 1214our @ISA = DC::UI::Bin::;
1204 1215
1205use dc::OpenGL; 1216use DC::OpenGL;
1206 1217
1207sub new { 1218sub new {
1208 my $class = shift; 1219 my $class = shift;
1209 1220
1210 $class->SUPER::new ( 1221 $class->SUPER::new (
1229 $self->SUPER::_draw; 1240 $self->SUPER::_draw;
1230} 1241}
1231 1242
1232############################################################################# 1243#############################################################################
1233 1244
1234package dc::UI::FancyFrame; 1245package DC::UI::FancyFrame;
1235 1246
1236our @ISA = dc::UI::Bin::; 1247our @ISA = DC::UI::Bin::;
1237 1248
1238use dc::OpenGL; 1249use DC::OpenGL;
1239 1250
1240sub new { 1251sub new {
1241 my ($class, %arg) = @_; 1252 my ($class, %arg) = @_;
1242 1253
1243 if ((exists $arg{label}) && !ref $arg{label}) { 1254 if ((exists $arg{label}) && !ref $arg{label}) {
1244 $arg{label} = new dc::UI::Label 1255 $arg{label} = new DC::UI::Label
1245 align => 1, 1256 align => 1,
1246 valign => 0, 1257 valign => 0.5,
1247 text => $arg{label}, 1258 text => $arg{label},
1248 fontsize => ($arg{border} || 0.8) * 0.75; 1259 fontsize => ($arg{border} || 0.8) * 0.75;
1249 } 1260 }
1250 1261
1251 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1252 # label => "", 1263 # label => "",
1253 fg => [0.6, 0.3, 0.1], 1264 fg => undef,
1254 border => 0.8, 1265 border => 0.8,
1255 style => 'single', 1266 style => 'single',
1256 %arg, 1267 %arg,
1257 ); 1268 );
1258 1269
1261 1272
1262sub add { 1273sub add {
1263 my ($self, @widgets) = @_; 1274 my ($self, @widgets) = @_;
1264 1275
1265 $self->SUPER::add (@widgets); 1276 $self->SUPER::add (@widgets);
1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label}; 1277 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1267} 1278}
1268 1279
1269sub border { 1280sub border {
1270 int $_[0]{border} * $::FONTSIZE 1281 int $_[0]{border} * $::FONTSIZE
1271} 1282}
1311 my $border = $self->border; 1322 my $border = $self->border;
1312 my ($w, $h) = ($self->{w}, $self->{h}); 1323 my ($w, $h) = ($self->{w}, $self->{h});
1313 1324
1314 $child->draw; 1325 $child->draw;
1315 1326
1316 glColor @{$self->{fg}}; 1327 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1317 glBegin GL_LINE_STRIP; 1328 glBegin GL_LINE_STRIP;
1318 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1329 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1330 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1320 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1331 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; 1332 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1329 } 1340 }
1330} 1341}
1331 1342
1332############################################################################# 1343#############################################################################
1333 1344
1334package dc::UI::Toplevel; 1345package DC::UI::Toplevel;
1335 1346
1336our @ISA = dc::UI::Bin::; 1347our @ISA = DC::UI::Bin::;
1337 1348
1338use dc::OpenGL; 1349use DC::OpenGL;
1339 1350
1340my $bg = 1351my $bg =
1341 new_from_file dc::Texture dc::find_rcfile "d1_bg.png", 1352 new_from_resource DC::Texture "d1_bg.png",
1342 mipmap => 1, wrap => 1; 1353 mipmap => 1, wrap => 1;
1343 1354
1344my @border = 1355my @border =
1345 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1356 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); 1357 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1347 1358
1348my @icon = 1359my @icon =
1349 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1360 map { new_from_resource DC::Texture $_, mipmap => 1 }
1350 qw(x1_move.png x1_resize.png); 1361 qw(x1_move.png x1_resize.png);
1351 1362
1352sub new { 1363sub new {
1353 my ($class, %arg) = @_; 1364 my ($class, %arg) = @_;
1354 1365
1355 my $self = $class->SUPER::new ( 1366 my $self = $class->SUPER::new (
1356 bg => [1, 1, 1, 1], 1367 bg => [1, 1, 1, 1],
1357 border_bg => [1, 1, 1, 1], 1368 border_bg => [1, 1, 1, 1],
1358 border => 0.6, 1369 border => 0.8,
1359 can_events => 1, 1370 can_events => 1,
1360 min_w => 64, 1371 min_w => 64,
1361 min_h => 32, 1372 min_h => 32,
1362 %arg, 1373 %arg,
1363 ); 1374 );
1364 1375
1365 $self->{title_widget} = new dc::UI::Label 1376 $self->{title_widget} = new DC::UI::Label
1366 align => 0, 1377 align => 0.5,
1367 valign => 1, 1378 valign => 1,
1368 text => $self->{title}, 1379 text => $self->{title},
1369 fontsize => $self->{border}, 1380 fontsize => $self->{border},
1370 if exists $self->{title}; 1381 if exists $self->{title};
1371 1382
1372 if ($self->{has_close_button}) { 1383 if ($self->{has_close_button}) {
1373 $self->{close_button} = 1384 $self->{close_button} =
1374 new dc::UI::ImageButton 1385 new DC::UI::ImageButton
1375 path => 'x1_close.png', 1386 path => 'x1_close.png',
1376 on_activate => sub { $self->emit ("delete") }; 1387 on_activate => sub { $self->emit ("delete") };
1377 1388
1378 $self->dc::UI::Container::add ($self->{close_button}); 1389 $self->DC::UI::Container::add ($self->{close_button});
1379 } 1390 }
1380 1391
1381 $self 1392 $self
1382} 1393}
1383 1394
1384sub add { 1395sub add {
1385 my ($self, @widgets) = @_; 1396 my ($self, @widgets) = @_;
1386 1397
1387 $self->SUPER::add (@widgets); 1398 $self->SUPER::add (@widgets);
1388 $self->dc::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1399 $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}; 1400 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1390} 1401}
1391 1402
1392sub border { 1403sub border {
1393 int $_[0]{border} * $::FONTSIZE 1404 int $_[0]{border} * $::FONTSIZE
1394} 1405}
1579 if $self->{close_button}; 1590 if $self->{close_button};
1580} 1591}
1581 1592
1582############################################################################# 1593#############################################################################
1583 1594
1584package dc::UI::Table; 1595package DC::UI::Table;
1585 1596
1586our @ISA = dc::UI::Container::; 1597our @ISA = DC::UI::Container::;
1587 1598
1588use List::Util qw(max sum); 1599use List::Util qw(max sum);
1589 1600
1590use dc::OpenGL; 1601use DC::OpenGL;
1591 1602
1592sub new { 1603sub new {
1593 my $class = shift; 1604 my $class = shift;
1594 1605
1595 $class->SUPER::new ( 1606 $class->SUPER::new (
1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1650 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640 1651
1641 my $sw = sum @w[$c .. $c + $cs - 1]; 1652 my $sw = sum @w[$c .. $c + $cs - 1];
1642 1653
1643 if ($w > $sw) { 1654 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1655 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 } 1656 }
1646 } 1657 }
1647 1658
1648 # second pass, rows 1659 # second pass, rows
1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1660 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1661 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1651 1662
1652 my $sh = sum @h[$r .. $r + $rs - 1]; 1663 my $sh = sum @h[$r .. $r + $rs - 1];
1653 1664
1654 if ($h > $sh) { 1665 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1666 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1656 } 1667 }
1657 } 1668 }
1658 1669
1659 (\@w, \@h) 1670 (\@w, \@h)
1660} 1671}
1683 @col_expand = (1) x @$ws unless @col_expand; 1694 @col_expand = (1) x @$ws unless @col_expand;
1684 my $col_expand = (sum @col_expand) || 1; 1695 my $col_expand = (sum @col_expand) || 1;
1685 1696
1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1697 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1687 1698
1688 dc::UI::harmonize $ws; 1699 DC::UI::harmonize $ws;
1689 1700
1690 my @row_expand = @{$self->{row_expand}}; 1701 my @row_expand = @{$self->{row_expand}};
1691 @row_expand = (1) x @$ws unless @row_expand; 1702 @row_expand = (1) x @$ws unless @row_expand;
1692 my $row_expand = (sum @row_expand) || 1; 1703 my $row_expand = (sum @row_expand) || 1;
1693 1704
1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1705 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1695 1706
1696 dc::UI::harmonize $hs; 1707 DC::UI::harmonize $hs;
1697 1708
1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1709 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1710 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1700 1711
1701 for my $widget ($self->children) { 1712 for my $widget ($self->children) {
1710 1 1721 1
1711} 1722}
1712 1723
1713############################################################################# 1724#############################################################################
1714 1725
1715package dc::UI::Fixed; 1726package DC::UI::Fixed;
1716 1727
1717use List::Util qw(min max); 1728use List::Util qw(min max);
1718 1729
1719our @ISA = dc::UI::Container::; 1730our @ISA = DC::UI::Container::;
1720 1731
1721sub _scale($$$) { 1732sub _scale($$$) {
1722 my ($rel, $val, $max) = @_; 1733 my ($rel, $val, $max) = @_;
1723 1734
1724 $rel ? $val * $max : $val 1735 $rel ? $val * $max : $val
1776 1 1787 1
1777} 1788}
1778 1789
1779############################################################################# 1790#############################################################################
1780 1791
1781package dc::UI::Box; 1792package DC::UI::Box;
1782 1793
1783our @ISA = dc::UI::Container::; 1794our @ISA = DC::UI::Container::;
1784 1795
1785sub size_request { 1796sub size_request {
1786 my ($self) = @_; 1797 my ($self) = @_;
1787 1798
1788 my @children = $self->visible_children; 1799 my @children = $self->visible_children;
1823 $req[$_] += $space * $children[$_]{expand} 1834 $req[$_] += $space * $children[$_]{expand}
1824 for 0 .. $#children; 1835 for 0 .. $#children;
1825 } 1836 }
1826 } 1837 }
1827 1838
1828 dc::UI::harmonize \@req; 1839 DC::UI::harmonize \@req;
1829 1840
1830 my $pos = 0; 1841 my $pos = 0;
1831 for (0 .. $#children) { 1842 for (0 .. $#children) {
1832 my $alloc = $req[$_]; 1843 my $alloc = $req[$_];
1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1844 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1838 1 1849 1
1839} 1850}
1840 1851
1841############################################################################# 1852#############################################################################
1842 1853
1843package dc::UI::HBox; 1854package DC::UI::HBox;
1844 1855
1845our @ISA = dc::UI::Box::; 1856our @ISA = DC::UI::Box::;
1846 1857
1847sub new { 1858sub new {
1848 my $class = shift; 1859 my $class = shift;
1849 1860
1850 $class->SUPER::new ( 1861 $class->SUPER::new (
1853 ) 1864 )
1854} 1865}
1855 1866
1856############################################################################# 1867#############################################################################
1857 1868
1858package dc::UI::VBox; 1869package DC::UI::VBox;
1859 1870
1860our @ISA = dc::UI::Box::; 1871our @ISA = DC::UI::Box::;
1861 1872
1862sub new { 1873sub new {
1863 my $class = shift; 1874 my $class = shift;
1864 1875
1865 $class->SUPER::new ( 1876 $class->SUPER::new (
1868 ) 1879 )
1869} 1880}
1870 1881
1871############################################################################# 1882#############################################################################
1872 1883
1873package dc::UI::Label; 1884package DC::UI::Label;
1874 1885
1875our @ISA = dc::UI::DrawBG::; 1886our @ISA = DC::UI::DrawBG::;
1876 1887
1877use dc::OpenGL; 1888use DC::OpenGL;
1878 1889
1879sub new { 1890sub new {
1880 my ($class, %arg) = @_; 1891 my ($class, %arg) = @_;
1881 1892
1882 my $self = $class->SUPER::new ( 1893 my $self = $class->SUPER::new (
1887 #text => initial text 1898 #text => initial text
1888 #markup => initial narkup 1899 #markup => initial narkup
1889 #max_w => maximum pixel width 1900 #max_w => maximum pixel width
1890 #style => 0, # render flags 1901 #style => 0, # render flags
1891 ellipsise => 3, # end 1902 ellipsise => 3, # end
1892 layout => (new dc::Layout), 1903 layout => (new DC::Layout),
1893 fontsize => 1, 1904 fontsize => 1,
1894 align => -1, 1905 align => 0.5,
1895 valign => -1, 1906 valign => 0.5,
1896 padding_x => 2, 1907 padding_x => 4,
1897 padding_y => 2, 1908 padding_y => 2,
1898 can_events => 0, 1909 can_events => 0,
1899 %arg 1910 %arg
1900 ); 1911 );
1901 1912
1902 if (exists $self->{template}) { 1913 if (exists $self->{template}) {
1903 my $layout = new dc::Layout; 1914 my $layout = new DC::Layout;
1904 $layout->set_text (delete $self->{template}); 1915 $layout->set_text (delete $self->{template});
1905 $self->{template} = $layout; 1916 $self->{template} = $layout;
1906 } 1917 }
1907 1918
1908 if (exists $self->{markup}) { 1919 if (exists $self->{markup}) {
1967 1978
1968 $self->{size_req} ||= do { 1979 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh; 1980 my ($max_w, $max_h) = $self->get_max_wh;
1970 1981
1971 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1982 $self->{layout}->set_font ($self->{font}) if $self->{font};
1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1983 $self->{layout}->set_width ($max_w);
1973 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1984 $self->{layout}->set_ellipsise ($self->{ellipsise});
1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1985 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1986 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1976 1987
1977 my ($w, $h) = $self->{layout}->size; 1988 my ($w, $h) = $self->{layout}->size;
1978 1989
1979 if (exists $self->{template}) { 1990 if (exists $self->{template}) {
1980 $self->{template}->set_font ($self->{font}) if $self->{font}; 1991 $self->{template}->set_font ($self->{font}) if $self->{font};
1981 $self->{template}->set_width ($self->{max_w} || -1); 1992 $self->{template}->set_width ($max_w);
1982 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1993 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1983 1994
1984 my ($w2, $h2) = $self->{template}->size; 1995 my ($w2, $h2) = $self->{template}->size;
1985 1996
1986 $w = List::Util::max $w, $w2; 1997 $w = List::Util::max $w, $w2;
2042 2053
2043 [$self->{layout}->size] 2054 [$self->{layout}->size]
2044 }; 2055 };
2045 2056
2046 unless (exists $self->{ox}) { 2057 unless (exists $self->{ox}) {
2047 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2058 $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} 2059 $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 2060
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2056 }; 2062 };
2057 2063
2058# unless ($self->{list}) { 2064# unless ($self->{list}) {
2059# $self->{list} = dc::OpenGL::glGenList; 2065# $self->{list} = DC::OpenGL::glGenList;
2060# dc::OpenGL::glNewList $self->{list}; 2066# DC::OpenGL::glNewList $self->{list};
2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2067# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# dc::OpenGL::glEndList; 2068# DC::OpenGL::glEndList;
2063# } 2069# }
2064# 2070#
2065# dc::OpenGL::glCallList $self->{list}; 2071# DC::OpenGL::glCallList $self->{list};
2066 2072
2067 $self->{layout}->draw; 2073 $self->{layout}->draw;
2068} 2074}
2069 2075
2070#sub destroy { 2076#sub destroy {
2071# my ($self) = @_; 2077# my ($self) = @_;
2072# 2078#
2073# dc::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2079# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074# 2080#
2075# $self->SUPER::destroy; 2081# $self->SUPER::destroy;
2076#} 2082#}
2077 2083
2078############################################################################# 2084#############################################################################
2079 2085
2080package dc::UI::EntryBase; 2086package DC::UI::EntryBase;
2081 2087
2082our @ISA = dc::UI::Label::; 2088our @ISA = DC::UI::Label::;
2083 2089
2084use dc::OpenGL; 2090use DC::OpenGL;
2085 2091
2086sub new { 2092sub new {
2087 my $class = shift; 2093 my $class = shift;
2088 2094
2089 $class->SUPER::new ( 2095 $class->SUPER::new (
2090 fg => [1, 1, 1], 2096 fg => [1, 1, 1],
2091 bg => [0, 0, 0, 0.2], 2097 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1], 2098 outline => undef,
2093 active_bg => [0, 0, 1, .2], 2099 active_bg => [0, 0, 1, .2],
2094 active_fg => [1, 1, 1], 2100 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0], 2101 active_outline => [1, 1, 0],
2096 can_hover => 1, 2102 can_hover => 1,
2097 can_focus => 1, 2103 can_focus => 1,
2104 align => 0,
2098 valign => 0, 2105 valign => 0.5,
2099 can_events => 1, 2106 can_events => 1,
2100 ellipsise => 0, 2107 ellipsise => 0,
2108 padding_x => 4,
2109 padding_y => 2,
2101 #text => ... 2110 #text => ...
2102 #hidden => "*", 2111 #hidden => "*",
2103 @_ 2112 @_
2104 ) 2113 )
2105} 2114}
2152 2161
2153 my $text = $self->get_text; 2162 my $text = $self->get_text;
2154 2163
2155 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2164 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2156 2165
2157 if ($uni == 8) { 2166 if ($sym == DC::SDLK_BACKSPACE) {
2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2167 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2159 } elsif ($uni == 127) { 2168 } elsif ($sym == DC::SDLK_DELETE) {
2160 substr $text, $self->{cursor}, 1, ""; 2169 substr $text, $self->{cursor}, 1, "";
2161 } elsif ($sym == dc::SDLK_LEFT) { 2170 } elsif ($sym == DC::SDLK_LEFT) {
2162 --$self->{cursor} if $self->{cursor}; 2171 --$self->{cursor} if $self->{cursor};
2163 } elsif ($sym == dc::SDLK_RIGHT) { 2172 } elsif ($sym == DC::SDLK_RIGHT) {
2164 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2173 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2165 } elsif ($sym == dc::SDLK_HOME) { 2174 } elsif ($sym == DC::SDLK_HOME) {
2166 # what a hack 2175 # what a hack
2167 $self->{cursor} = 2176 $self->{cursor} =
2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2177 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2169 ? length $1 2178 ? length $1
2170 : 0; 2179 : 0;
2171 } elsif ($sym == dc::SDLK_END) { 2180 } elsif ($sym == DC::SDLK_END) {
2172 # uh, again 2181 # uh, again
2173 $self->{cursor} = 2182 $self->{cursor} =
2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2183 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2175 ? $self->{cursor} + length $1 2184 ? $self->{cursor} + length $1
2176 : length $self->{text}; 2185 : length $self->{text};
2267 glColor @{$self->{active_outline}}; 2276 glColor @{$self->{active_outline}};
2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2277 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2269 glLineWidth 1; 2278 glLineWidth 1;
2270 2279
2271 } else { 2280 } else {
2272 glColor @{$self->{outline}}; 2281 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2273 glBegin GL_LINE_STRIP; 2282 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5; 2283 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5; 2284 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5; 2285 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5; 2286 glVertex $self->{w} - .5, $self->{h} * .5;
2279 } 2288 }
2280} 2289}
2281 2290
2282############################################################################# 2291#############################################################################
2283 2292
2284package dc::UI::Entry; 2293package DC::UI::Entry;
2285 2294
2286our @ISA = dc::UI::EntryBase::; 2295our @ISA = DC::UI::EntryBase::;
2287 2296
2288use dc::OpenGL; 2297use DC::OpenGL;
2298
2299sub new {
2300 my $class = shift;
2301
2302 $class->SUPER::new (
2303 history_pointer => -1,
2304 @_
2305 )
2306}
2307
2289 2308
2290sub invoke_key_down { 2309sub invoke_key_down {
2291 my ($self, $ev) = @_; 2310 my ($self, $ev) = @_;
2292 2311
2293 my $sym = $ev->{sym}; 2312 my $sym = $ev->{sym};
2299 $self->{history_pointer} = -1; 2318 $self->{history_pointer} = -1;
2300 $self->{history_saveback} = ''; 2319 $self->{history_saveback} = '';
2301 $self->emit (activate => $txt); 2320 $self->emit (activate => $txt);
2302 $self->update; 2321 $self->update;
2303 2322
2304 } elsif ($sym == dc::SDLK_UP) { 2323 } elsif ($sym == DC::SDLK_UP) {
2305 if ($self->{history_pointer} < 0) { 2324 if ($self->{history_pointer} < 0) {
2306 $self->{history_saveback} = $self->get_text; 2325 $self->{history_saveback} = $self->get_text;
2307 } 2326 }
2308 if (@{$self->{history} || []} > 0) { 2327 if (@{$self->{history} || []} > 0) {
2309 $self->{history_pointer}++; 2328 $self->{history_pointer}++;
2311 $self->{history_pointer} = @{$self->{history} || []} - 1; 2330 $self->{history_pointer} = @{$self->{history} || []} - 1;
2312 } 2331 }
2313 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2332 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2314 } 2333 }
2315 2334
2316 } elsif ($sym == dc::SDLK_DOWN) { 2335 } elsif ($sym == DC::SDLK_DOWN) {
2317 $self->{history_pointer}--; 2336 $self->{history_pointer}--;
2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2337 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2319 2338
2320 if ($self->{history_pointer} >= 0) { 2339 if ($self->{history_pointer} >= 0) {
2321 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2340 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2322 } else { 2341 } else {
2342 if (defined $self->{history_saveback}) {
2323 $self->set_text ($self->{history_saveback}); 2343 $self->set_text ($self->{history_saveback});
2344 $self->{history_saveback} = undef;
2345 }
2324 } 2346 }
2325 2347
2326 } else { 2348 } else {
2327 return $self->SUPER::invoke_key_down ($ev) 2349 return $self->SUPER::invoke_key_down ($ev)
2328 } 2350 }
2330 1 2352 1
2331} 2353}
2332 2354
2333############################################################################# 2355#############################################################################
2334 2356
2335package dc::UI::TextEdit; 2357package DC::UI::TextEdit;
2336 2358
2337our @ISA = dc::UI::EntryBase::; 2359our @ISA = DC::UI::EntryBase::;
2338 2360
2339use dc::OpenGL; 2361use DC::OpenGL;
2362
2363sub new {
2364 my $class = shift;
2365
2366 $class->SUPER::new (
2367 padding_y => 4,
2368
2369 @_
2370 )
2371}
2340 2372
2341sub move_cursor_ver { 2373sub move_cursor_ver {
2342 my ($self, $dy) = @_; 2374 my ($self, $dy) = @_;
2343 2375
2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2376 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2345 2377
2346 $y += $dy; 2378 $line += $dy;
2347 2379
2348 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2380 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2349 $self->{cursor} = $index; 2381 $self->{cursor} = $index;
2350 delete $self->{cur_h}; 2382 delete $self->{cur_h};
2351 $self->update; 2383 $self->update;
2352 return; 2384 return;
2353 } 2385 }
2356sub invoke_key_down { 2388sub invoke_key_down {
2357 my ($self, $ev) = @_; 2389 my ($self, $ev) = @_;
2358 2390
2359 my $sym = $ev->{sym}; 2391 my $sym = $ev->{sym};
2360 2392
2361 if ($sym == dc::SDLK_UP) { 2393 if ($sym == DC::SDLK_UP) {
2362 $self->move_cursor_ver (-1); 2394 $self->move_cursor_ver (-1);
2363 } elsif ($sym == dc::SDLK_DOWN) { 2395 } elsif ($sym == DC::SDLK_DOWN) {
2364 $self->move_cursor_ver (+1); 2396 $self->move_cursor_ver (+1);
2365 } else { 2397 } else {
2366 return $self->SUPER::invoke_key_down ($ev) 2398 return $self->SUPER::invoke_key_down ($ev)
2367 } 2399 }
2368 2400
2369 1 2401 1
2370} 2402}
2371 2403
2372############################################################################# 2404#############################################################################
2373 2405
2374package dc::UI::ButtonBin; 2406package DC::UI::ButtonBin;
2375 2407
2376our @ISA = dc::UI::Bin::; 2408our @ISA = DC::UI::Bin::;
2377 2409
2378use dc::OpenGL; 2410use DC::OpenGL;
2379 2411
2380my @tex = 2412my @tex =
2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2413 map { new_from_resource DC::Texture $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png); 2414 qw(b1_button_inactive.png b1_button_active.png);
2383 2415
2384sub new { 2416sub new {
2385 my $class = shift; 2417 my $class = shift;
2386 2418
2387 $class->SUPER::new ( 2419 $class->SUPER::new (
2388 can_hover => 1, 2420 can_hover => 1,
2389 align => 0, 2421 align => 0.5,
2390 valign => 0, 2422 valign => 0.5,
2391 can_events => 1, 2423 can_events => 1,
2392 @_ 2424 @_
2393 ) 2425 )
2394} 2426}
2395 2427
2418 $self->SUPER::_draw; 2450 $self->SUPER::_draw;
2419} 2451}
2420 2452
2421############################################################################# 2453#############################################################################
2422 2454
2423package dc::UI::Button; 2455package DC::UI::Button;
2424 2456
2425our @ISA = dc::UI::Label::; 2457our @ISA = DC::UI::Label::;
2426 2458
2427use dc::OpenGL; 2459use DC::OpenGL;
2428 2460
2429my @tex = 2461my @tex =
2430 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2462 map { new_from_resource DC::Texture $_, mipmap => 1 }
2431 qw(b1_button_inactive.png b1_button_active.png); 2463 qw(b1_button_inactive.png b1_button_active.png);
2432 2464
2433sub new { 2465sub new {
2434 my $class = shift; 2466 my $class = shift;
2435 2467
2436 $class->SUPER::new ( 2468 $class->SUPER::new (
2437 padding_x => 4, 2469 padding_x => 8,
2438 padding_y => 4, 2470 padding_y => 4,
2439 fg => [1.0, 1.0, 1.0], 2471 fg => [1.0, 1.0, 1.0],
2440 active_fg => [0.8, 0.8, 0.8], 2472 active_fg => [0.8, 0.8, 0.8],
2441 can_hover => 1, 2473 can_hover => 1,
2442 align => 0, 2474 align => 0.5,
2443 valign => 0, 2475 valign => 0.5,
2444 can_events => 1, 2476 can_events => 1,
2445 @_ 2477 @_
2446 ) 2478 )
2447} 2479}
2448 2480
2473 $self->SUPER::_draw; 2505 $self->SUPER::_draw;
2474} 2506}
2475 2507
2476############################################################################# 2508#############################################################################
2477 2509
2478package dc::UI::CheckBox; 2510package DC::UI::CheckBox;
2479 2511
2480our @ISA = dc::UI::DrawBG::; 2512our @ISA = DC::UI::DrawBG::;
2481 2513
2482my @tex = 2514my @tex =
2483 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2515 map { new_from_resource DC::Texture $_, mipmap => 1 }
2484 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2516 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2485 2517
2486use dc::OpenGL; 2518use DC::OpenGL;
2487 2519
2488sub new { 2520sub new {
2489 my $class = shift; 2521 my $class = shift;
2490 2522
2491 $class->SUPER::new ( 2523 $class->SUPER::new (
2548 glDisable GL_TEXTURE_2D; 2580 glDisable GL_TEXTURE_2D;
2549} 2581}
2550 2582
2551############################################################################# 2583#############################################################################
2552 2584
2553package dc::UI::Image; 2585package DC::UI::Image;
2554 2586
2555our @ISA = dc::UI::Base::; 2587our @ISA = DC::UI::Base::;
2556 2588
2557use dc::OpenGL; 2589use DC::OpenGL;
2558 2590
2559our %texture_cache; 2591our %texture_cache;
2560 2592
2561sub new { 2593sub new {
2562 my $class = shift; 2594 my $class = shift;
2569 2601
2570 $self->{path} || $self->{tex} 2602 $self->{path} || $self->{tex}
2571 or Carp::croak "'path' or 'tex' attributes required"; 2603 or Carp::croak "'path' or 'tex' attributes required";
2572 2604
2573 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2605 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2574 new_from_file dc::Texture dc::find_rcfile $self->{path}, mipmap => 1; 2606 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2575 2607
2576 dc::weaken $texture_cache{$self->{path}}; 2608 DC::weaken $texture_cache{$self->{path}};
2577 2609
2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2610 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2579 2611
2580 $self 2612 $self
2581} 2613}
2582 2614
2583sub STORABLE_freeze { 2615sub STORABLE_freeze {
2584 my ($self, $cloning) = @_; 2616 my ($self, $cloning) = @_;
2585 2617
2586 $self->{path} 2618 $self->{path}
2587 or die "cannot serialise dc::UI::Image on non-loadable images\n"; 2619 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2588 2620
2589 $self->{path} 2621 $self->{path}
2590} 2622}
2591 2623
2592sub STORABLE_attach { 2624sub STORABLE_attach {
2623 glDisable GL_TEXTURE_2D; 2655 glDisable GL_TEXTURE_2D;
2624} 2656}
2625 2657
2626############################################################################# 2658#############################################################################
2627 2659
2628package dc::UI::ImageButton; 2660package DC::UI::ImageButton;
2629 2661
2630our @ISA = dc::UI::Image::; 2662our @ISA = DC::UI::Image::;
2631 2663
2632use dc::OpenGL; 2664use DC::OpenGL;
2633 2665
2634my %textures; 2666my %textures;
2635 2667
2636sub new { 2668sub new {
2637 my $class = shift; 2669 my $class = shift;
2640 padding_x => 4, 2672 padding_x => 4,
2641 padding_y => 4, 2673 padding_y => 4,
2642 fg => [1, 1, 1], 2674 fg => [1, 1, 1],
2643 active_fg => [0, 0, 1], 2675 active_fg => [0, 0, 1],
2644 can_hover => 1, 2676 can_hover => 1,
2645 align => 0, 2677 align => 0.5,
2646 valign => 0, 2678 valign => 0.5,
2647 can_events => 1, 2679 can_events => 1,
2648 @_ 2680 @_
2649 ); 2681 );
2650} 2682}
2651 2683
2665 1 2697 1
2666} 2698}
2667 2699
2668############################################################################# 2700#############################################################################
2669 2701
2670package dc::UI::VGauge; 2702package DC::UI::VGauge;
2671 2703
2672our @ISA = dc::UI::Base::; 2704our @ISA = DC::UI::Base::;
2673 2705
2674use List::Util qw(min max); 2706use List::Util qw(min max);
2675 2707
2676use dc::OpenGL; 2708use DC::OpenGL;
2677 2709
2678my %tex = ( 2710my %tex = (
2679 food => [ 2711 food => [
2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2712 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2713 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2682 ], 2714 ],
2683 grace => [ 2715 grace => [
2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2716 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/ 2717 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2686 ], 2718 ],
2687 hp => [ 2719 hp => [
2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2720 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2721 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2690 ], 2722 ],
2691 mana => [ 2723 mana => [
2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2724 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/ 2725 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2694 ], 2726 ],
2695); 2727);
2696 2728
2697# eg. VGauge->new (gauge => 'food'), default gauge: food 2729# eg. VGauge->new (gauge => 'food'), default gauge: food
2800 glDisable GL_TEXTURE_2D; 2832 glDisable GL_TEXTURE_2D;
2801} 2833}
2802 2834
2803############################################################################# 2835#############################################################################
2804 2836
2805package dc::UI::Progress; 2837package DC::UI::Progress;
2806 2838
2807our @ISA = dc::UI::Label::; 2839our @ISA = DC::UI::Label::;
2808 2840
2809use dc::OpenGL; 2841use DC::OpenGL;
2810 2842
2811sub new { 2843sub new {
2812 my ($class, %arg) = @_; 2844 my ($class, %arg) = @_;
2813 2845
2814 my $self = $class->SUPER::new ( 2846 my $self = $class->SUPER::new (
2815 fg => [1, 1, 1], 2847 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2], 2848 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8], 2849 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0], 2850 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9, 2851 fontsize => 0.9,
2820 valign => 0, 2852 valign => 0.5,
2821 align => 0, 2853 align => 0.5,
2822 can_events => 1, 2854 can_events => 1,
2823 ellipsise => 1, 2855 ellipsise => 1,
2824 label => "%d%%", 2856 label => "%d%%",
2825 %arg, 2857 %arg,
2826 ); 2858 );
2834 my ($self, $label) = @_; 2866 my ($self, $label) = @_;
2835 2867
2836 return if $self->{label} eq $label; 2868 return if $self->{label} eq $label;
2837 $self->{label} = $label; 2869 $self->{label} = $label;
2838 2870
2839 $self->dc::UI::Progress::set_value (0 + delete $self->{value}); 2871 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2840} 2872}
2841 2873
2842sub set_value { 2874sub set_value {
2843 my ($self, $value) = @_; 2875 my ($self, $value) = @_;
2844 2876
2881 } 2913 }
2882} 2914}
2883 2915
2884############################################################################# 2916#############################################################################
2885 2917
2886package dc::UI::ExperienceProgress; 2918package DC::UI::ExperienceProgress;
2887 2919
2888our @ISA = dc::UI::Progress::; 2920our @ISA = DC::UI::Progress::;
2889 2921
2890sub new { 2922sub new {
2891 my ($class, %arg) = @_; 2923 my ($class, %arg) = @_;
2892 2924
2893 my $self = $class->SUPER::new ( 2925 my $self = $class->SUPER::new (
2938 $self->SUPER::set_value ($v); 2970 $self->SUPER::set_value ($v);
2939} 2971}
2940 2972
2941############################################################################# 2973#############################################################################
2942 2974
2943package dc::UI::Gauge; 2975package DC::UI::Gauge;
2944 2976
2945our @ISA = dc::UI::VBox::; 2977our @ISA = DC::UI::VBox::;
2946 2978
2947sub new { 2979sub new {
2948 my ($class, %arg) = @_; 2980 my ($class, %arg) = @_;
2949 2981
2950 my $self = $class->SUPER::new ( 2982 my $self = $class->SUPER::new (
2952 can_hover => 1, 2984 can_hover => 1,
2953 can_events => 1, 2985 can_events => 1,
2954 %arg, 2986 %arg,
2955 ); 2987 );
2956 2988
2957 $self->add ($self->{value} = new dc::UI::Label valign => +1, align => 0, template => "999"); 2989 $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); 2990 $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"); 2991 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2960 2992
2961 $self 2993 $self
2962} 2994}
2963 2995
2964sub set_fontsize { 2996sub set_fontsize {
2985 $self->{value}->set_text ($val); 3017 $self->{value}->set_text ($val);
2986} 3018}
2987 3019
2988############################################################################# 3020#############################################################################
2989 3021
2990package dc::UI::Slider; 3022package DC::UI::Slider;
2991 3023
2992use strict; 3024use strict;
2993 3025
2994use dc::OpenGL; 3026use DC::OpenGL;
2995 3027
2996our @ISA = dc::UI::DrawBG::; 3028our @ISA = DC::UI::DrawBG::;
2997 3029
2998my @tex = 3030my @tex =
2999 map { new_from_file dc::Texture dc::find_rcfile $_ } 3031 map { new_from_resource DC::Texture $_ }
3000 qw(s1_slider.png s1_slider_bg.png); 3032 qw(s1_slider.png s1_slider_bg.png);
3001 3033
3002sub new { 3034sub new {
3003 my $class = shift; 3035 my $class = shift;
3004 3036
3041sub set_value { 3073sub set_value {
3042 my ($self, $value) = @_; 3074 my ($self, $value) = @_;
3043 3075
3044 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3076 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3045 3077
3046 $hi = $lo + 1 if $hi <= $lo; 3078 $hi = $lo if $hi < $lo;
3047 3079
3048 $page = $hi - $lo if $page > $hi - $lo; 3080 $value = $hi - $page if $value > $hi - $page;
3049
3050 $value = $lo if $value < $lo; 3081 $value = $lo if $value < $lo;
3051 $value = $hi - $page if $value > $hi - $page;
3052 3082
3053 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3083 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3054 if $unit; 3084 if $unit;
3055 3085
3056 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3086 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3100sub invoke_mouse_wheel { 3130sub invoke_mouse_wheel {
3101 my ($self, $ev) = @_; 3131 my ($self, $ev) = @_;
3102 3132
3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3133 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3104 3134
3105 my $pagepart = $ev->{mod} & dc::KMOD_SHIFT ? 1 : 0.2; 3135 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3106 3136
3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3137 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3108 3138
3109 1 3139 1
3110} 3140}
3120 my ($self) = @_; 3150 my ($self) = @_;
3121 3151
3122 unless ($self->{knob_w}) { 3152 unless ($self->{knob_w}) {
3123 $self->set_value ($self->{range}[0]); 3153 $self->set_value ($self->{range}[0]);
3124 3154
3125 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3155 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3126 my $range = ($hi - $page - $lo) || 1e-100; 3156 my $range = ($hi - $page - $lo) || 1e-10;
3127 3157
3128 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3158 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3129 3159
3130 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3160 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3131 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3161 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3132 3162
3133 $value = ($value - $lo) / $range; 3163 $value = ($value - $lo) / $range;
3163 glDisable GL_TEXTURE_2D; 3193 glDisable GL_TEXTURE_2D;
3164} 3194}
3165 3195
3166############################################################################# 3196#############################################################################
3167 3197
3168package dc::UI::ValSlider; 3198package DC::UI::ValSlider;
3169 3199
3170our @ISA = dc::UI::HBox::; 3200our @ISA = DC::UI::HBox::;
3171 3201
3172sub new { 3202sub new {
3173 my ($class, %arg) = @_; 3203 my ($class, %arg) = @_;
3174 3204
3175 my $range = delete $arg{range}; 3205 my $range = delete $arg{range};
3176 3206
3177 my $self = $class->SUPER::new ( 3207 my $self = $class->SUPER::new (
3178 slider => (new dc::UI::Slider expand => 1, range => $range), 3208 slider => (new DC::UI::Slider expand => 1, range => $range),
3179 entry => (new dc::UI::Label text => "", template => delete $arg{template}), 3209 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3180 to_value => sub { shift }, 3210 to_value => sub { shift },
3181 from_value => sub { shift }, 3211 from_value => sub { shift },
3182 %arg, 3212 %arg,
3183 ); 3213 );
3184 3214
3204sub set_range { shift->{slider}->set_range (@_) } 3234sub set_range { shift->{slider}->set_range (@_) }
3205sub set_value { shift->{slider}->set_value (@_) } 3235sub set_value { shift->{slider}->set_value (@_) }
3206 3236
3207############################################################################# 3237#############################################################################
3208 3238
3209package dc::UI::TextScroller; 3239package DC::UI::TextScroller;
3210 3240
3211our @ISA = dc::UI::HBox::; 3241our @ISA = DC::UI::HBox::;
3212 3242
3213use dc::OpenGL; 3243use DC::OpenGL;
3214 3244
3215sub new { 3245sub new {
3216 my $class = shift; 3246 my $class = shift;
3217 3247
3218 my $self = $class->SUPER::new ( 3248 my $self = $class->SUPER::new (
3220 can_events => 1, 3250 can_events => 1,
3221 indent => 0, 3251 indent => 0,
3222 #font => default_font 3252 #font => default_font
3223 @_, 3253 @_,
3224 3254
3225 layout => (new dc::Layout), 3255 layout => (new DC::Layout),
3226 par => [], 3256 par => [],
3227 max_par => 0, 3257 max_par => 0,
3228 height => 0, 3258 height => 0,
3229 children => [ 3259 children => [
3230 (new dc::UI::Empty expand => 1), 3260 (new DC::UI::Empty expand => 1),
3231 (new dc::UI::Slider vertical => 1), 3261 (new DC::UI::Slider vertical => 1),
3232 ], 3262 ],
3233 ); 3263 );
3234 3264
3235 $self->{children}[1]->connect (changed => sub { $self->update }); 3265 $self->{children}[1]->connect (changed => sub { $self->update });
3236 3266
3292 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3322 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3293 $layout->set_markup ($para->{markup}); 3323 $layout->set_markup ($para->{markup});
3294 3324
3295 $layout->set_shapes ( 3325 $layout->set_shapes (
3296 map 3326 map
3297 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3327 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3298 @{$para->{widget}} 3328 @{$para->{widget}}
3299 ); 3329 );
3300 3330
3301 $layout 3331 $layout
3302} 3332}
3419 $ROOT->on_post_alloc ($self => sub { 3449 $ROOT->on_post_alloc ($self => sub {
3420 $self->force_uptodate; 3450 $self->force_uptodate;
3421 3451
3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3452 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3423 3453
3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub { 3454 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3425 glClearColor 0, 0, 0, 0; 3455 glClearColor 0, 0, 0, 0;
3426 glClear GL_COLOR_BUFFER_BIT; 3456 glClear GL_COLOR_BUFFER_BIT;
3427 3457
3428 package dc::UI::Base; 3458 package DC::UI::Base;
3429 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3459 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3430 (0, 0, $self->{w}, $self->{h}); 3460 (0, 0, $self->{w}, $self->{h});
3431 3461
3432 my $top = int $self->{children}[1]{range}[0]; 3462 my $top = int $self->{children}[1]{range}[0];
3433 3463
3492 $self->{children}[1]->draw; 3522 $self->{children}[1]->draw;
3493} 3523}
3494 3524
3495############################################################################# 3525#############################################################################
3496 3526
3497package dc::UI::Animator; 3527package DC::UI::Animator;
3498 3528
3499use dc::OpenGL; 3529use DC::OpenGL;
3500 3530
3501our @ISA = dc::UI::Bin::; 3531our @ISA = DC::UI::Bin::;
3502 3532
3503sub moveto { 3533sub moveto {
3504 my ($self, $x, $y) = @_; 3534 my ($self, $x, $y) = @_;
3505 3535
3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3536 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3534 glPopMatrix; 3564 glPopMatrix;
3535} 3565}
3536 3566
3537############################################################################# 3567#############################################################################
3538 3568
3539package dc::UI::Flopper; 3569package DC::UI::Flopper;
3540 3570
3541our @ISA = dc::UI::Button::; 3571our @ISA = DC::UI::Button::;
3542 3572
3543sub new { 3573sub new {
3544 my $class = shift; 3574 my $class = shift;
3545 3575
3546 my $self = $class->SUPER::new ( 3576 my $self = $class->SUPER::new (
3558 $self->{other}->toggle_visibility; 3588 $self->{other}->toggle_visibility;
3559} 3589}
3560 3590
3561############################################################################# 3591#############################################################################
3562 3592
3563package dc::UI::Tooltip; 3593package DC::UI::Tooltip;
3564 3594
3565our @ISA = dc::UI::Bin::; 3595our @ISA = DC::UI::Bin::;
3566 3596
3567use dc::OpenGL; 3597use DC::OpenGL;
3568 3598
3569sub new { 3599sub new {
3570 my $class = shift; 3600 my $class = shift;
3571 3601
3572 $class->SUPER::new ( 3602 $class->SUPER::new (
3579 my ($self, $widget) = @_; 3609 my ($self, $widget) = @_;
3580 3610
3581 my $tip = $widget->{tooltip}; 3611 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip; 3612 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583 3613
3584 $tip = dc::Pod::section_label tooltip => $1 3614 $tip = DC::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/; 3615 if $tip =~ /^#(.*)$/;
3586 3616
3587 if ($ENV{CFPLUS_DEBUG} & 2) { 3617 if ($ENV{CFPLUS_DEBUG} & 2) {
3588 $tip .= "\n\n" . (ref $widget) . "\n" 3618 $tip .= "\n\n" . (ref $widget) . "\n"
3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3619 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3592 } 3622 }
3593 3623
3594 $tip =~ s/^\n+//; 3624 $tip =~ s/^\n+//;
3595 $tip =~ s/\n+$//; 3625 $tip =~ s/\n+$//;
3596 3626
3597 $self->add (new dc::UI::Label 3627 $self->add (new DC::UI::Label
3628 fg => $DC::THEME{tooltip_fg},
3598 markup => $tip, 3629 markup => $tip,
3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3630 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3631 align => 0,
3600 fontsize => 0.8, 3632 fontsize => 0.8,
3601 style => 1, # FLAG_INVERSE 3633 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3602 ellipsise => 0, 3634 ellipsise => 0,
3603 font => ($widget->{tooltip_font} || $::FONT_PROP), 3635 font => ($widget->{tooltip_font} || $::FONT_PROP),
3604 ); 3636 );
3605} 3637}
3606 3638
3643sub _draw { 3675sub _draw {
3644 my ($self) = @_; 3676 my ($self) = @_;
3645 3677
3646 my ($w, $h) = @$self{qw(w h)}; 3678 my ($w, $h) = @$self{qw(w h)};
3647 3679
3648 glColor 1, 0.8, 0.4; 3680 glColor @{ $DC::THEME{tooltip_bg} };
3649 glRect 0, 0, $w, $h; 3681 glRect 0, 0, $w, $h;
3650 3682
3651 glColor 0, 0, 0; 3683 glColor @{ $DC::THEME{tooltip_border} };
3652 glRect_lineloop .5, .5, $w + .5, $h + .5; 3684 glRect_lineloop .5, .5, $w + .5, $h + .5;
3653 3685
3654 glTranslate 2, 2; 3686 glTranslate 2, 2;
3655 3687
3656 $self->SUPER::_draw; 3688 $self->SUPER::_draw;
3657} 3689}
3658 3690
3659############################################################################# 3691#############################################################################
3660 3692
3661package dc::UI::Face; 3693package DC::UI::Face;
3662 3694
3663our @ISA = dc::UI::DrawBG::; 3695our @ISA = DC::UI::DrawBG::;
3664 3696
3665use dc::OpenGL; 3697use DC::OpenGL;
3666 3698
3667sub new { 3699sub new {
3668 my $class = shift; 3700 my $class = shift;
3669 3701
3670 my $self = $class->SUPER::new ( 3702 my $self = $class->SUPER::new (
3674 can_events => 0, 3706 can_events => 0,
3675 @_, 3707 @_,
3676 ); 3708 );
3677 3709
3678 if ($self->{anim} && $self->{animspeed}) { 3710 if ($self->{anim} && $self->{animspeed}) {
3679 dc::weaken (my $widget = $self); 3711 DC::weaken (my $widget = $self);
3680 3712
3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; 3713 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { 3714 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3683 return unless $::CONN; 3715 return unless $::CONN;
3684 3716
3790 $self->SUPER::destroy; 3822 $self->SUPER::destroy;
3791} 3823}
3792 3824
3793############################################################################# 3825#############################################################################
3794 3826
3795package dc::UI::Buttonbar; 3827package DC::UI::Buttonbar;
3796 3828
3797our @ISA = dc::UI::HBox::; 3829our @ISA = DC::UI::HBox::;
3798 3830
3799# TODO: should actually wrap buttons and other goodies. 3831# TODO: should actually wrap buttons and other goodies.
3800 3832
3801############################################################################# 3833#############################################################################
3802 3834
3803package dc::UI::Menu; 3835package DC::UI::Menu;
3804 3836
3805our @ISA = dc::UI::Toplevel::; 3837our @ISA = DC::UI::Toplevel::;
3806 3838
3807use dc::OpenGL; 3839use DC::OpenGL;
3808 3840
3809sub new { 3841sub new {
3810 my $class = shift; 3842 my $class = shift;
3811 3843
3812 my $self = $class->SUPER::new ( 3844 my $self = $class->SUPER::new (
3813 items => [], 3845 items => [],
3814 z => 100, 3846 z => 100,
3815 @_, 3847 @_,
3816 ); 3848 );
3817 3849
3818 $self->add ($self->{vbox} = new dc::UI::VBox); 3850 $self->add ($self->{vbox} = new DC::UI::VBox);
3819 3851
3820 for my $item (@{ $self->{items} }) { 3852 for my $item (@{ $self->{items} }) {
3821 my ($widget, $cb, $tooltip) = @$item; 3853 my ($widget, $cb, $tooltip) = @$item;
3822 3854
3823 # handle various types of items, only text for now 3855 # handle various types of items, only text for now
3824 if (!ref $widget) { 3856 if (!ref $widget) {
3825 if ($widget =~ /\t/) { 3857 if ($widget =~ /\t/) {
3826 my ($left, $right) = split /\t/, $widget, 2; 3858 my ($left, $right) = split /\t/, $widget, 2;
3827 3859
3828 $widget = new dc::UI::HBox 3860 $widget = new DC::UI::HBox
3829 can_hover => 1, 3861 can_hover => 1,
3830 can_events => 1, 3862 can_events => 1,
3831 tooltip => $tooltip, 3863 tooltip => $tooltip,
3832 children => [ 3864 children => [
3833 (new dc::UI::Label markup => $left, expand => 1), 3865 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3834 (new dc::UI::Label markup => $right, align => +1), 3866 (new DC::UI::Label markup => $right, align => 1),
3835 ], 3867 ],
3836 ; 3868 ;
3837 3869
3838 } else { 3870 } else {
3839 $widget = new dc::UI::Label 3871 $widget = new DC::UI::Label
3840 can_hover => 1, 3872 can_hover => 1,
3841 can_events => 1, 3873 can_events => 1,
3874 align => 0,
3842 markup => $widget, 3875 markup => $widget,
3843 tooltip => $tooltip; 3876 tooltip => $tooltip;
3844 } 3877 }
3845 } 3878 }
3846 3879
3892 1 3925 1
3893} 3926}
3894 3927
3895############################################################################# 3928#############################################################################
3896 3929
3897package dc::UI::Multiplexer; 3930package DC::UI::Multiplexer;
3898 3931
3899our @ISA = dc::UI::Container::; 3932our @ISA = DC::UI::Container::;
3900 3933
3901sub new { 3934sub new {
3902 my $class = shift; 3935 my $class = shift;
3903 3936
3904 my $self = $class->SUPER::new ( 3937 my $self = $class->SUPER::new (
3905 @_, 3938 @_,
3906 ); 3939 );
3907 3940
3908 $self->{current} = $self->{children}[0] 3941 $self->set_current_page (0);
3909 if @{ $self->{children} };
3910 3942
3911 $self 3943 $self
3912} 3944}
3913 3945
3914sub add { 3946sub add {
3915 my ($self, @widgets) = @_; 3947 my ($self, @widgets) = @_;
3916 3948
3917 $self->SUPER::add (@widgets); 3949 $self->SUPER::add (@widgets);
3918 3950
3919 $self->{current} = $self->{children}[0] 3951 $self->set_current_page (0)
3920 if @{ $self->{children} }; 3952 if @widgets == @{ $self->{children} };
3921} 3953}
3922 3954
3923sub get_current_page { 3955sub get_current_page {
3924 my ($self) = @_; 3956 my ($self) = @_;
3925 3957
3931 3963
3932 my $widget = ref $page_or_widget 3964 my $widget = ref $page_or_widget
3933 ? $page_or_widget 3965 ? $page_or_widget
3934 : $self->{children}[$page_or_widget]; 3966 : $self->{children}[$page_or_widget];
3935 3967
3968 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3969
3936 $self->{current} = $widget; 3970 if (($self->{current} = $widget)) {
3971 $self->{current}->set_visible if $self->{current} && $self->{visible};
3937 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3972 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3938 3973
3939 $self->emit (page_changed => $self->{current}); 3974 $self->emit (page_changed => $self->{current});
3975 }
3940 3976
3941 $self->realloc; 3977 $self->realloc;
3942} 3978}
3943 3979
3944sub visible_children { 3980sub visible_children {
3945 $_[0]{current} 3981 $_[0]{current} || ()
3946} 3982}
3947 3983
3948sub size_request { 3984sub size_request {
3949 my ($self) = @_; 3985 my ($self) = @_;
3950 3986
3987 $self->{current}
3951 $self->{current}->size_request 3988 ? $self->{current}->size_request
3989 : (0, 0)
3952} 3990}
3953 3991
3954sub invoke_size_allocate { 3992sub invoke_size_allocate {
3955 my ($self, $w, $h) = @_; 3993 my ($self, $w, $h) = @_;
3956 3994
3957 $self->{current}->configure (0, 0, $w, $h); 3995 $self->{current}->configure (0, 0, $w, $h)
3996 if $self->{current};
3958 3997
3959 1 3998 1
3960} 3999}
3961 4000
3962sub _draw { 4001sub _draw {
3963 my ($self) = @_; 4002 my ($self) = @_;
3964 4003
3965 $self->{current}->draw; 4004 $self->{current}->draw
4005 if $self->{current};
3966} 4006}
3967 4007
3968############################################################################# 4008#############################################################################
3969 4009
3970package dc::UI::Notebook; 4010package DC::UI::Notebook;
3971 4011
3972use dc::OpenGL; 4012use DC::OpenGL;
3973 4013
3974our @ISA = dc::UI::VBox::; 4014our @ISA = DC::UI::VBox::;
3975 4015
3976sub new { 4016sub new {
3977 my $class = shift; 4017 my $class = shift;
3978 4018
3979 my $self = $class->SUPER::new ( 4019 my $self = $class->SUPER::new (
3980 buttonbar => (new dc::UI::Buttonbar), 4020 buttonbar => (new DC::UI::Buttonbar),
3981 multiplexer => (new dc::UI::Multiplexer expand => 1), 4021 multiplexer => (new DC::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2], 4022 active_outline => [.7, .7, 0.2],
3983 # filter => # will be put between multiplexer and $self 4023 # filter => # will be put between multiplexer and $self
3984 @_, 4024 @_,
3985 ); 4025 );
3986 4026
3995 4035
3996 for my $child (@$widgets) { 4036 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child; 4037 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do { 4038 $child->{c_tab_} ||= do {
3999 my $tab = 4039 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "dc::UI::Base") 4040 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4001 ? $child->{c_tab} 4041 ? $child->{c_tab}
4002 : new dc::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4042 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003 4043
4004 $tab->connect (activate => sub { 4044 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child); 4045 $wself->set_current_page ($child);
4006 }); 4046 });
4007 4047
4037} 4077}
4038 4078
4039sub pages { 4079sub pages {
4040 my ($self) = @_; 4080 my ($self) = @_;
4041 $self->{multiplexer}->children 4081 $self->{multiplexer}->children
4082}
4083
4084sub page_index {
4085 my ($self, $widget) = @_;
4086
4087 my $i = 0;
4088 for ($self->pages) {
4089 if ($_ eq $widget) { return $i };
4090 $i++;
4091 }
4092
4093 undef
4042} 4094}
4043 4095
4044sub add_tab { 4096sub add_tab {
4045 my ($self, $title, $widget, $tooltip) = @_; 4097 my ($self, $title, $widget, $tooltip) = @_;
4046 4098
4083 } 4135 }
4084} 4136}
4085 4137
4086############################################################################# 4138#############################################################################
4087 4139
4088package dc::UI::Selector; 4140package DC::UI::Selector;
4089 4141
4090use utf8; 4142use utf8;
4091 4143
4092our @ISA = dc::UI::Button::; 4144our @ISA = DC::UI::Button::;
4093 4145
4094sub new { 4146sub new {
4095 my $class = shift; 4147 my $class = shift;
4096 4148
4097 my $self = $class->SUPER::new ( 4149 my $self = $class->SUPER::new (
4114 my ($value, $title, $tooltip) = @$_; 4166 my ($value, $title, $tooltip) = @$_;
4115 4167
4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4168 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4117 } 4169 }
4118 4170
4119 dc::UI::Menu->new (items => \@menu_items)->popup ($ev); 4171 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4120} 4172}
4121 4173
4122sub _set_value { 4174sub _set_value {
4123 my ($self, $value) = @_; 4175 my ($self, $value) = @_;
4124 4176
4147 $self->_set_value ($self->{value}); 4199 $self->_set_value ($self->{value});
4148} 4200}
4149 4201
4150############################################################################# 4202#############################################################################
4151 4203
4152package dc::UI::Statusbox; 4204package DC::UI::Statusbox;
4153 4205
4154our @ISA = dc::UI::VBox::; 4206our @ISA = DC::UI::VBox::;
4155 4207
4156sub new { 4208sub new {
4157 my $class = shift; 4209 my $class = shift;
4158 4210
4159 my $self = $class->SUPER::new ( 4211 my $self = $class->SUPER::new (
4160 fontsize => 0.8, 4212 fontsize => 0.8,
4161 @_, 4213 @_,
4162 ); 4214 );
4163 4215
4164 dc::weaken (my $this = $self); 4216 DC::weaken (my $this = $self);
4165 4217
4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4218 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4167 4219
4168 $self 4220 $self
4169} 4221}
4170 4222
4171sub reorder { 4223sub reorder {
4172 my ($self) = @_; 4224 my ($self) = @_;
4173 my $NOW = Time::HiRes::time; 4225 my $NOW = EV::time;
4174 4226
4175 # freeze display when hovering over any label 4227 # freeze display when hovering over any label
4176 return if $dc::UI::TOOLTIP->{owner} 4228 return if $DC::UI::TOOLTIP->{owner}
4177 && grep $dc::UI::TOOLTIP->{owner} == $_->{label}, 4229 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4178 values %{ $self->{item} }; 4230 values %{ $self->{item} };
4179 4231
4180 while (my ($k, $v) = each %{ $self->{item} }) { 4232 while (my ($k, $v) = each %{ $self->{item} }) {
4181 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4233 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4182 } 4234 }
4203 for ($short) { 4255 for ($short) {
4204 s/^\s+//; 4256 s/^\s+//;
4205 s/\s+/ /g; 4257 s/\s+/ /g;
4206 } 4258 }
4207 4259
4208 new dc::UI::Label 4260 new DC::UI::Label
4209 markup => $short, 4261 markup => $short,
4210 tooltip => $item->{tooltip}, 4262 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4263 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4264 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4265 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4266 max_w => $::WIDTH * 0.44,
4267 align => 0,
4215 fg => [@{ $item->{fg} }], 4268 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4269 can_events => 1,
4217 can_hover => 1 4270 can_hover => 1
4218 }; 4271 };
4219 4272
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4280 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4281 }
4229 4282
4230 push @widgets, $label; 4283 push @widgets, $label;
4231 } 4284 }
4285
4286 my $hash = join ",", @widgets;
4287 return if $hash eq $self->{last_widget_hash};
4288 $self->{last_widget_hash} = $hash;
4232 4289
4233 $self->clear; 4290 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4291 $self->SUPER::add (reverse @widgets);
4235} 4292}
4236 4293
4292 $self->SUPER::destroy; 4349 $self->SUPER::destroy;
4293} 4350}
4294 4351
4295############################################################################# 4352#############################################################################
4296 4353
4297package dc::UI::Root; 4354package DC::UI::Root;
4298 4355
4299our @ISA = dc::UI::Container::; 4356our @ISA = DC::UI::Container::;
4300 4357
4301use List::Util qw(min max); 4358use List::Util qw(min max);
4302 4359
4303use dc::OpenGL; 4360use DC::OpenGL;
4304 4361
4305sub new { 4362sub new {
4306 my $class = shift; 4363 my $class = shift;
4307 4364
4308 my $self = $class->SUPER::new ( 4365 my $self = $class->SUPER::new (
4309 visible => 1, 4366 visible => 1,
4310 @_, 4367 @_,
4311 ); 4368 );
4312 4369
4313 dc::weaken ($self->{root} = $self); 4370 DC::weaken ($self->{root} = $self);
4314 4371
4315 $self 4372 $self
4316} 4373}
4317 4374
4318sub size_request { 4375sub size_request {
4443 4500
4444 delete $queue{$widget+0}; 4501 delete $queue{$widget+0};
4445 4502
4446 my ($w, $h) = $widget->size_request; 4503 my ($w, $h) = $widget->size_request;
4447 4504
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4505 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4506 $h += $widget->{padding_y} * 2;
4507
4508 $w = max $widget->{min_w}, $w;
4509 $h = max $widget->{min_h}, $h;
4450 4510
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4511 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4512 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4513
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4514 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4581 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4582 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4583 glLoadIdentity;
4524 4584
4525 { 4585 {
4526 package dc::UI::Base; 4586 package DC::UI::Base;
4527 4587
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4588 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4589 (0, 0, $self->{w}, $self->{h});
4530 4590
4531 $self->_draw; 4591 $self->_draw;
4532 } 4592 }
4533} 4593}
4534 4594
4535############################################################################# 4595#############################################################################
4536 4596
4537package dc::UI; 4597package DC::UI;
4538 4598
4539$ROOT = new dc::UI::Root; 4599$ROOT = new DC::UI::Root;
4540$TOOLTIP = new dc::UI::Tooltip z => 900; 4600$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4601
45421 46021
4543 4603

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines