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.475 by root, Sun Jan 11 22:15:23 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 (
663 } 676 }
664} 677}
665 678
666############################################################################# 679#############################################################################
667 680
668package dc::UI::Empty; 681package DC::UI::Empty;
669 682
670our @ISA = dc::UI::Base::; 683our @ISA = DC::UI::Base::;
671 684
672sub new { 685sub new {
673 my ($class, %arg) = @_; 686 my ($class, %arg) = @_;
674 $class->SUPER::new (can_events => 0, %arg); 687 $class->SUPER::new (can_events => 0, %arg);
675} 688}
682 695
683sub draw { } 696sub draw { }
684 697
685############################################################################# 698#############################################################################
686 699
687package dc::UI::Container; 700package DC::UI::Container;
688 701
689our @ISA = dc::UI::Base::; 702our @ISA = DC::UI::Base::;
690 703
691sub new { 704sub new {
692 my ($class, %arg) = @_; 705 my ($class, %arg) = @_;
693 706
694 my $children = delete $arg{children}; 707 my $children = delete $arg{children};
788 $_->draw for $self->visible_children; 801 $_->draw for $self->visible_children;
789} 802}
790 803
791############################################################################# 804#############################################################################
792 805
793package dc::UI::Bin; 806package DC::UI::Bin;
794 807
795our @ISA = dc::UI::Container::; 808our @ISA = DC::UI::Container::;
796 809
797sub new { 810sub new {
798 my ($class, %arg) = @_; 811 my ($class, %arg) = @_;
799 812
800 my $child = (delete $arg{child}) || new dc::UI::Empty::; 813 my $child = (delete $arg{child}) || new DC::UI::Empty::;
801 814
802 $class->SUPER::new (children => [$child], %arg) 815 $class->SUPER::new (children => [$child], %arg)
803} 816}
804 817
805sub add { 818sub add {
812sub remove { 825sub remove {
813 my ($self, $widget) = @_; 826 my ($self, $widget) = @_;
814 827
815 $self->SUPER::remove ($widget); 828 $self->SUPER::remove ($widget);
816 829
817 $self->{children} = [new dc::UI::Empty] 830 $self->{children} = [new DC::UI::Empty]
818 unless @{$self->{children}}; 831 unless @{$self->{children}};
819} 832}
820 833
821sub child { $_[0]->{children}[0] } 834sub child { $_[0]->{children}[0] }
822 835
833} 846}
834 847
835############################################################################# 848#############################################################################
836# back-buffered drawing area 849# back-buffered drawing area
837 850
838package dc::UI::Window; 851package DC::UI::Window;
839 852
840our @ISA = dc::UI::Bin::; 853our @ISA = DC::UI::Bin::;
841 854
842use dc::OpenGL; 855use DC::OpenGL;
843 856
844sub new { 857sub new {
845 my ($class, %arg) = @_; 858 my ($class, %arg) = @_;
846 859
847 my $self = $class->SUPER::new (%arg); 860 my $self = $class->SUPER::new (%arg);
869} 882}
870 883
871sub render_child { 884sub render_child {
872 my ($self) = @_; 885 my ($self) = @_;
873 886
874 $self->{texture} = new_from_opengl dc::Texture $self->{w}, $self->{h}, sub { 887 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
875 glClearColor 0, 0, 0, 0; 888 glClearColor 0, 0, 0, 0;
876 glClear GL_COLOR_BUFFER_BIT; 889 glClear GL_COLOR_BUFFER_BIT;
877 890
878 { 891 {
879 package dc::UI::Base; 892 package DC::UI::Base;
880 893
881 local ($draw_x, $draw_y, $draw_w, $draw_h) = 894 local ($draw_x, $draw_y, $draw_w, $draw_h) =
882 (0, 0, $self->{w}, $self->{h}); 895 (0, 0, $self->{w}, $self->{h});
883 896
884 $self->_render; 897 $self->_render;
901 glDisable GL_TEXTURE_2D; 914 glDisable GL_TEXTURE_2D;
902} 915}
903 916
904############################################################################# 917#############################################################################
905 918
906package dc::UI::ViewPort; 919package DC::UI::ViewPort;
907 920
908use List::Util qw(min max); 921use List::Util qw(min max);
909 922
910our @ISA = dc::UI::Window::; 923our @ISA = DC::UI::Window::;
911 924
912sub new { 925sub new {
913 my $class = shift; 926 my $class = shift;
914 927
915 $class->SUPER::new ( 928 $class->SUPER::new (
999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1012 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1013 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1001 ) { 1014 ) {
1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1015 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1003 } else { 1016 } else {
1004 $self->dc::UI::Base::find_widget ($x, $y) 1017 $self->DC::UI::Base::find_widget ($x, $y)
1005 } 1018 }
1006} 1019}
1007 1020
1008sub _render { 1021sub _render {
1009 my ($self) = @_; 1022 my ($self) = @_;
1010 1023
1011 local $dc::UI::Base::draw_x = $dc::UI::Base::draw_x - $self->{view_x}; 1024 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}; 1025 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1013 1026
1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1027 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1015 1028
1016 $self->SUPER::_render; 1029 $self->SUPER::_render;
1017} 1030}
1018 1031
1019############################################################################# 1032#############################################################################
1020 1033
1021package dc::UI::ScrolledWindow; 1034package DC::UI::ScrolledWindow;
1022 1035
1023our @ISA = dc::UI::Table::; 1036our @ISA = DC::UI::Table::;
1024 1037
1025sub new { 1038sub new {
1026 my ($class, %arg) = @_; 1039 my ($class, %arg) = @_;
1027 1040
1028 my $child = delete $arg{child}; 1041 my $child = delete $arg{child};
1029 1042
1030 my $self; 1043 my $self;
1031 1044
1032 my $hslider = new dc::UI::Slider 1045 my $hslider = new DC::UI::Slider
1033 c_col => 0, 1046 c_col => 0,
1034 c_row => 1, 1047 c_row => 1,
1035 vertical => 0, 1048 vertical => 0,
1036 range => [0, 0, 1, 0.01], # HACK fix 1049 range => [0, 0, 1, 0.01], # HACK fix
1037 on_changed => sub { 1050 on_changed => sub {
1038 $self->{hpos} = $_[1]; 1051 $self->{hpos} = $_[1];
1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1053 },
1041 ; 1054 ;
1042 1055
1043 my $vslider = new dc::UI::Slider 1056 my $vslider = new DC::UI::Slider
1044 c_col => 1, 1057 c_col => 1,
1045 c_row => 0, 1058 c_row => 0,
1046 vertical => 1, 1059 vertical => 1,
1047 range => [0, 0, 1, 0.01], # HACK fix 1060 range => [0, 0, 1, 0.01], # HACK fix
1048 on_changed => sub { 1061 on_changed => sub {
1060 col_expand => [1, 0], 1073 col_expand => [1, 0],
1061 row_expand => [1, 0], 1074 row_expand => [1, 0],
1062 %arg, 1075 %arg,
1063 ); 1076 );
1064 1077
1065 $self->{vp} = new dc::UI::ViewPort 1078 $self->{vp} = new DC::UI::ViewPort
1066 c_col => 0, 1079 c_col => 0,
1067 c_row => 0, 1080 c_row => 0,
1068 expand => 1, 1081 expand => 1,
1069 scroll_x => $self->{scroll_x}, 1082 scroll_x => $self->{scroll_x},
1070 scroll_y => $self->{scroll_y}, 1083 scroll_y => $self->{scroll_y},
1196 $self->SUPER::invoke_size_allocate ($w, $h) 1209 $self->SUPER::invoke_size_allocate ($w, $h)
1197} 1210}
1198 1211
1199############################################################################# 1212#############################################################################
1200 1213
1201package dc::UI::Frame; 1214package DC::UI::Frame;
1202 1215
1203our @ISA = dc::UI::Bin::; 1216our @ISA = DC::UI::Bin::;
1204 1217
1205use dc::OpenGL; 1218use DC::OpenGL;
1206 1219
1207sub new { 1220sub new {
1208 my $class = shift; 1221 my $class = shift;
1209 1222
1210 $class->SUPER::new ( 1223 $class->SUPER::new (
1229 $self->SUPER::_draw; 1242 $self->SUPER::_draw;
1230} 1243}
1231 1244
1232############################################################################# 1245#############################################################################
1233 1246
1234package dc::UI::FancyFrame; 1247package DC::UI::FancyFrame;
1235 1248
1236our @ISA = dc::UI::Bin::; 1249our @ISA = DC::UI::Bin::;
1237 1250
1238use dc::OpenGL; 1251use DC::OpenGL;
1239 1252
1240sub new { 1253sub new {
1241 my ($class, %arg) = @_; 1254 my ($class, %arg) = @_;
1242 1255
1243 if ((exists $arg{label}) && !ref $arg{label}) { 1256 if ((exists $arg{label}) && !ref $arg{label}) {
1244 $arg{label} = new dc::UI::Label 1257 $arg{label} = new DC::UI::Label
1245 align => 1, 1258 align => 1,
1246 valign => 0, 1259 valign => 0.5,
1247 text => $arg{label}, 1260 text => $arg{label},
1248 fontsize => ($arg{border} || 0.8) * 0.75; 1261 fontsize => ($arg{border} || 0.8) * 0.75;
1249 } 1262 }
1250 1263
1251 my $self = $class->SUPER::new ( 1264 my $self = $class->SUPER::new (
1252 # label => "", 1265 # label => "",
1253 fg => [0.6, 0.3, 0.1], 1266 fg => undef,
1254 border => 0.8, 1267 border => 0.8,
1255 style => 'single', 1268 style => 'single',
1256 %arg, 1269 %arg,
1257 ); 1270 );
1258 1271
1261 1274
1262sub add { 1275sub add {
1263 my ($self, @widgets) = @_; 1276 my ($self, @widgets) = @_;
1264 1277
1265 $self->SUPER::add (@widgets); 1278 $self->SUPER::add (@widgets);
1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label}; 1279 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1267} 1280}
1268 1281
1269sub border { 1282sub border {
1270 int $_[0]{border} * $::FONTSIZE 1283 int $_[0]{border} * $::FONTSIZE
1271} 1284}
1311 my $border = $self->border; 1324 my $border = $self->border;
1312 my ($w, $h) = ($self->{w}, $self->{h}); 1325 my ($w, $h) = ($self->{w}, $self->{h});
1313 1326
1314 $child->draw; 1327 $child->draw;
1315 1328
1316 glColor @{$self->{fg}}; 1329 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1317 glBegin GL_LINE_STRIP; 1330 glBegin GL_LINE_STRIP;
1318 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1331 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1332 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1320 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1333 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; 1334 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1329 } 1342 }
1330} 1343}
1331 1344
1332############################################################################# 1345#############################################################################
1333 1346
1334package dc::UI::Toplevel; 1347package DC::UI::Toplevel;
1335 1348
1336our @ISA = dc::UI::Bin::; 1349our @ISA = DC::UI::Bin::;
1337 1350
1338use dc::OpenGL; 1351use DC::OpenGL;
1339 1352
1340my $bg = 1353my $bg =
1341 new_from_file dc::Texture dc::find_rcfile "d1_bg.png", 1354 new_from_resource DC::Texture "d1_bg.png",
1342 mipmap => 1, wrap => 1; 1355 mipmap => 1, wrap => 1;
1343 1356
1344my @border = 1357my @border =
1345 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1358 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); 1359 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1347 1360
1348my @icon = 1361my @icon =
1349 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1362 map { new_from_resource DC::Texture $_, mipmap => 1 }
1350 qw(x1_move.png x1_resize.png); 1363 qw(x1_move.png x1_resize.png);
1351 1364
1352sub new { 1365sub new {
1353 my ($class, %arg) = @_; 1366 my ($class, %arg) = @_;
1354 1367
1355 my $self = $class->SUPER::new ( 1368 my $self = $class->SUPER::new (
1356 bg => [1, 1, 1, 1], 1369 bg => [1, 1, 1, 1],
1357 border_bg => [1, 1, 1, 1], 1370 border_bg => [1, 1, 1, 1],
1358 border => 0.6, 1371 border => 0.8,
1359 can_events => 1, 1372 can_events => 1,
1360 min_w => 64, 1373 min_w => 64,
1361 min_h => 32, 1374 min_h => 32,
1362 %arg, 1375 %arg,
1363 ); 1376 );
1364 1377
1365 $self->{title_widget} = new dc::UI::Label 1378 $self->{title_widget} = new DC::UI::Label
1366 align => 0, 1379 align => 0.5,
1367 valign => 1, 1380 valign => 1,
1368 text => $self->{title}, 1381 text => $self->{title},
1369 fontsize => $self->{border}, 1382 fontsize => $self->{border},
1370 if exists $self->{title}; 1383 if exists $self->{title};
1371 1384
1372 if ($self->{has_close_button}) { 1385 if ($self->{has_close_button}) {
1373 $self->{close_button} = 1386 $self->{close_button} =
1374 new dc::UI::ImageButton 1387 new DC::UI::ImageButton
1375 path => 'x1_close.png', 1388 path => 'x1_close.png',
1376 on_activate => sub { $self->emit ("delete") }; 1389 on_activate => sub { $self->emit ("delete") };
1377 1390
1378 $self->dc::UI::Container::add ($self->{close_button}); 1391 $self->DC::UI::Container::add ($self->{close_button});
1379 } 1392 }
1380 1393
1381 $self 1394 $self
1382} 1395}
1383 1396
1384sub add { 1397sub add {
1385 my ($self, @widgets) = @_; 1398 my ($self, @widgets) = @_;
1386 1399
1387 $self->SUPER::add (@widgets); 1400 $self->SUPER::add (@widgets);
1388 $self->dc::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1401 $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}; 1402 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1390} 1403}
1391 1404
1392sub border { 1405sub border {
1393 int $_[0]{border} * $::FONTSIZE 1406 int $_[0]{border} * $::FONTSIZE
1394} 1407}
1532 glEnable GL_TEXTURE_2D; 1545 glEnable GL_TEXTURE_2D;
1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1546 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1534 1547
1535 my $border = $self->border; 1548 my $border = $self->border;
1536 1549
1550 if ($border) {
1537 glColor @{ $self->{border_bg} }; 1551 glColor @{ $self->{border_bg} };
1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1552 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1553 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1554 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1541 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1555 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1542 1556
1543 # move 1557 # move
1544 my $w2 = ($w - $border) * .5; 1558 my $w2 = ($w - $border) * .5;
1545 my $h2 = ($h - $border) * .5; 1559 my $h2 = ($h - $border) * .5;
1546 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1560 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1547 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1561 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1548 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1562 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1549 1563
1550 # resize 1564 # resize
1551 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1565 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1566 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1553 unless $self->{has_close_button}; 1567 unless $self->{has_close_button};
1554 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1568 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1555 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1569 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1570 }
1556 1571
1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1572 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1558 glColor @{ $self->{bg} }; 1573 glColor @{ $self->{bg} };
1559 1574
1560 # TODO: repeat texture not scale 1575 # TODO: repeat texture not scale
1579 if $self->{close_button}; 1594 if $self->{close_button};
1580} 1595}
1581 1596
1582############################################################################# 1597#############################################################################
1583 1598
1584package dc::UI::Table; 1599package DC::UI::Table;
1585 1600
1586our @ISA = dc::UI::Container::; 1601our @ISA = DC::UI::Container::;
1587 1602
1588use List::Util qw(max sum); 1603use List::Util qw(max sum);
1589 1604
1590use dc::OpenGL; 1605use DC::OpenGL;
1591 1606
1592sub new { 1607sub new {
1593 my $class = shift; 1608 my $class = shift;
1594 1609
1595 $class->SUPER::new ( 1610 $class->SUPER::new (
1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1654 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640 1655
1641 my $sw = sum @w[$c .. $c + $cs - 1]; 1656 my $sw = sum @w[$c .. $c + $cs - 1];
1642 1657
1643 if ($w > $sw) { 1658 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1659 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 } 1660 }
1646 } 1661 }
1647 1662
1648 # second pass, rows 1663 # second pass, rows
1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1664 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1665 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1651 1666
1652 my $sh = sum @h[$r .. $r + $rs - 1]; 1667 my $sh = sum @h[$r .. $r + $rs - 1];
1653 1668
1654 if ($h > $sh) { 1669 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1670 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1656 } 1671 }
1657 } 1672 }
1658 1673
1659 (\@w, \@h) 1674 (\@w, \@h)
1660} 1675}
1683 @col_expand = (1) x @$ws unless @col_expand; 1698 @col_expand = (1) x @$ws unless @col_expand;
1684 my $col_expand = (sum @col_expand) || 1; 1699 my $col_expand = (sum @col_expand) || 1;
1685 1700
1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1701 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1687 1702
1688 dc::UI::harmonize $ws; 1703 DC::UI::harmonize $ws;
1689 1704
1690 my @row_expand = @{$self->{row_expand}}; 1705 my @row_expand = @{$self->{row_expand}};
1691 @row_expand = (1) x @$ws unless @row_expand; 1706 @row_expand = (1) x @$ws unless @row_expand;
1692 my $row_expand = (sum @row_expand) || 1; 1707 my $row_expand = (sum @row_expand) || 1;
1693 1708
1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1709 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1695 1710
1696 dc::UI::harmonize $hs; 1711 DC::UI::harmonize $hs;
1697 1712
1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1713 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1714 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1700 1715
1701 for my $widget ($self->children) { 1716 for my $widget ($self->children) {
1710 1 1725 1
1711} 1726}
1712 1727
1713############################################################################# 1728#############################################################################
1714 1729
1715package dc::UI::Fixed; 1730package DC::UI::Fixed;
1716 1731
1717use List::Util qw(min max); 1732use List::Util qw(min max);
1718 1733
1719our @ISA = dc::UI::Container::; 1734our @ISA = DC::UI::Container::;
1720 1735
1721sub _scale($$$) { 1736sub _scale($$$) {
1722 my ($rel, $val, $max) = @_; 1737 my ($rel, $val, $max) = @_;
1723 1738
1724 $rel ? $val * $max : $val 1739 $rel ? $val * $max : $val
1776 1 1791 1
1777} 1792}
1778 1793
1779############################################################################# 1794#############################################################################
1780 1795
1781package dc::UI::Box; 1796package DC::UI::Box;
1782 1797
1783our @ISA = dc::UI::Container::; 1798our @ISA = DC::UI::Container::;
1784 1799
1785sub size_request { 1800sub size_request {
1786 my ($self) = @_; 1801 my ($self) = @_;
1787 1802
1788 my @children = $self->visible_children; 1803 my @children = $self->visible_children;
1823 $req[$_] += $space * $children[$_]{expand} 1838 $req[$_] += $space * $children[$_]{expand}
1824 for 0 .. $#children; 1839 for 0 .. $#children;
1825 } 1840 }
1826 } 1841 }
1827 1842
1828 dc::UI::harmonize \@req; 1843 DC::UI::harmonize \@req;
1829 1844
1830 my $pos = 0; 1845 my $pos = 0;
1831 for (0 .. $#children) { 1846 for (0 .. $#children) {
1832 my $alloc = $req[$_]; 1847 my $alloc = $req[$_];
1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1848 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1838 1 1853 1
1839} 1854}
1840 1855
1841############################################################################# 1856#############################################################################
1842 1857
1843package dc::UI::HBox; 1858package DC::UI::HBox;
1844 1859
1845our @ISA = dc::UI::Box::; 1860our @ISA = DC::UI::Box::;
1846 1861
1847sub new { 1862sub new {
1848 my $class = shift; 1863 my $class = shift;
1849 1864
1850 $class->SUPER::new ( 1865 $class->SUPER::new (
1853 ) 1868 )
1854} 1869}
1855 1870
1856############################################################################# 1871#############################################################################
1857 1872
1858package dc::UI::VBox; 1873package DC::UI::VBox;
1859 1874
1860our @ISA = dc::UI::Box::; 1875our @ISA = DC::UI::Box::;
1861 1876
1862sub new { 1877sub new {
1863 my $class = shift; 1878 my $class = shift;
1864 1879
1865 $class->SUPER::new ( 1880 $class->SUPER::new (
1868 ) 1883 )
1869} 1884}
1870 1885
1871############################################################################# 1886#############################################################################
1872 1887
1873package dc::UI::Label; 1888package DC::UI::Label;
1874 1889
1875our @ISA = dc::UI::DrawBG::; 1890our @ISA = DC::UI::DrawBG::;
1876 1891
1877use dc::OpenGL; 1892use DC::OpenGL;
1878 1893
1879sub new { 1894sub new {
1880 my ($class, %arg) = @_; 1895 my ($class, %arg) = @_;
1881 1896
1882 my $self = $class->SUPER::new ( 1897 my $self = $class->SUPER::new (
1887 #text => initial text 1902 #text => initial text
1888 #markup => initial narkup 1903 #markup => initial narkup
1889 #max_w => maximum pixel width 1904 #max_w => maximum pixel width
1890 #style => 0, # render flags 1905 #style => 0, # render flags
1891 ellipsise => 3, # end 1906 ellipsise => 3, # end
1892 layout => (new dc::Layout), 1907 layout => (new DC::Layout),
1893 fontsize => 1, 1908 fontsize => 1,
1894 align => -1, 1909 align => 0.5,
1895 valign => -1, 1910 valign => 0.5,
1896 padding_x => 2, 1911 padding_x => 4,
1897 padding_y => 2, 1912 padding_y => 2,
1898 can_events => 0, 1913 can_events => 0,
1899 %arg 1914 %arg
1900 ); 1915 );
1901 1916
1902 if (exists $self->{template}) { 1917 if (exists $self->{template}) {
1903 my $layout = new dc::Layout; 1918 my $layout = new DC::Layout;
1904 $layout->set_text (delete $self->{template}); 1919 $layout->set_text (delete $self->{template});
1905 $self->{template} = $layout; 1920 $self->{template} = $layout;
1906 } 1921 }
1907 1922
1908 if (exists $self->{markup}) { 1923 if (exists $self->{markup}) {
1967 1982
1968 $self->{size_req} ||= do { 1983 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh; 1984 my ($max_w, $max_h) = $self->get_max_wh;
1970 1985
1971 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1986 $self->{layout}->set_font ($self->{font}) if $self->{font};
1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1987 $self->{layout}->set_width ($max_w);
1973 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1988 $self->{layout}->set_ellipsise ($self->{ellipsise});
1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1989 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1990 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1976 1991
1977 my ($w, $h) = $self->{layout}->size; 1992 my ($w, $h) = $self->{layout}->size;
1978 1993
1979 if (exists $self->{template}) { 1994 if (exists $self->{template}) {
1980 $self->{template}->set_font ($self->{font}) if $self->{font}; 1995 $self->{template}->set_font ($self->{font}) if $self->{font};
1981 $self->{template}->set_width ($self->{max_w} || -1); 1996 $self->{template}->set_width ($max_w);
1982 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1983 1998
1984 my ($w2, $h2) = $self->{template}->size; 1999 my ($w2, $h2) = $self->{template}->size;
1985 2000
1986 $w = List::Util::max $w, $w2; 2001 $w = List::Util::max $w, $w2;
2042 2057
2043 [$self->{layout}->size] 2058 [$self->{layout}->size]
2044 }; 2059 };
2045 2060
2046 unless (exists $self->{ox}) { 2061 unless (exists $self->{ox}) {
2047 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2062 $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} 2063 $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 2064
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2065 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2056 }; 2066 };
2057 2067
2058# unless ($self->{list}) { 2068# unless ($self->{list}) {
2059# $self->{list} = dc::OpenGL::glGenList; 2069# $self->{list} = DC::OpenGL::glGenList;
2060# dc::OpenGL::glNewList $self->{list}; 2070# DC::OpenGL::glNewList $self->{list};
2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2071# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# dc::OpenGL::glEndList; 2072# DC::OpenGL::glEndList;
2063# } 2073# }
2064# 2074#
2065# dc::OpenGL::glCallList $self->{list}; 2075# DC::OpenGL::glCallList $self->{list};
2066 2076
2067 $self->{layout}->draw; 2077 $self->{layout}->draw;
2068} 2078}
2069 2079
2070#sub destroy { 2080#sub destroy {
2071# my ($self) = @_; 2081# my ($self) = @_;
2072# 2082#
2073# dc::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2083# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074# 2084#
2075# $self->SUPER::destroy; 2085# $self->SUPER::destroy;
2076#} 2086#}
2077 2087
2078############################################################################# 2088#############################################################################
2079 2089
2080package dc::UI::EntryBase; 2090package DC::UI::EntryBase;
2081 2091
2082our @ISA = dc::UI::Label::; 2092our @ISA = DC::UI::Label::;
2083 2093
2084use dc::OpenGL; 2094use DC::OpenGL;
2085 2095
2086sub new { 2096sub new {
2087 my $class = shift; 2097 my $class = shift;
2088 2098
2089 $class->SUPER::new ( 2099 $class->SUPER::new (
2090 fg => [1, 1, 1], 2100 fg => [1, 1, 1],
2091 bg => [0, 0, 0, 0.2], 2101 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1], 2102 outline => undef,
2093 active_bg => [0, 0, 1, .2], 2103 active_bg => [0, 0, 1, .2],
2094 active_fg => [1, 1, 1], 2104 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0], 2105 active_outline => [1, 1, 0],
2096 can_hover => 1, 2106 can_hover => 1,
2097 can_focus => 1, 2107 can_focus => 1,
2108 align => 0,
2098 valign => 0, 2109 valign => 0.5,
2099 can_events => 1, 2110 can_events => 1,
2100 ellipsise => 0, 2111 ellipsise => 0,
2112 padding_x => 4,
2113 padding_y => 2,
2101 #text => ... 2114 #text => ...
2102 #hidden => "*", 2115 #hidden => "*",
2103 @_ 2116 @_
2104 ) 2117 )
2105} 2118}
2152 2165
2153 my $text = $self->get_text; 2166 my $text = $self->get_text;
2154 2167
2155 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2168 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2156 2169
2157 if ($uni == 8) { 2170 if ($sym == DC::SDLK_BACKSPACE) {
2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2171 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2159 } elsif ($uni == 127) { 2172 } elsif ($sym == DC::SDLK_DELETE) {
2160 substr $text, $self->{cursor}, 1, ""; 2173 substr $text, $self->{cursor}, 1, "";
2161 } elsif ($sym == dc::SDLK_LEFT) { 2174 } elsif ($sym == DC::SDLK_LEFT) {
2162 --$self->{cursor} if $self->{cursor}; 2175 --$self->{cursor} if $self->{cursor};
2163 } elsif ($sym == dc::SDLK_RIGHT) { 2176 } elsif ($sym == DC::SDLK_RIGHT) {
2164 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2177 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2165 } elsif ($sym == dc::SDLK_HOME) { 2178 } elsif ($sym == DC::SDLK_HOME) {
2166 # what a hack 2179 # what a hack
2167 $self->{cursor} = 2180 $self->{cursor} =
2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2181 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2169 ? length $1 2182 ? length $1
2170 : 0; 2183 : 0;
2171 } elsif ($sym == dc::SDLK_END) { 2184 } elsif ($sym == DC::SDLK_END) {
2172 # uh, again 2185 # uh, again
2173 $self->{cursor} = 2186 $self->{cursor} =
2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2187 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2175 ? $self->{cursor} + length $1 2188 ? $self->{cursor} + length $1
2176 : length $self->{text}; 2189 : length $self->{text};
2267 glColor @{$self->{active_outline}}; 2280 glColor @{$self->{active_outline}};
2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2281 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2269 glLineWidth 1; 2282 glLineWidth 1;
2270 2283
2271 } else { 2284 } else {
2272 glColor @{$self->{outline}}; 2285 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2273 glBegin GL_LINE_STRIP; 2286 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5; 2287 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5; 2288 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5; 2289 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5; 2290 glVertex $self->{w} - .5, $self->{h} * .5;
2279 } 2292 }
2280} 2293}
2281 2294
2282############################################################################# 2295#############################################################################
2283 2296
2284package dc::UI::Entry; 2297package DC::UI::Entry;
2285 2298
2286our @ISA = dc::UI::EntryBase::; 2299our @ISA = DC::UI::EntryBase::;
2287 2300
2288use dc::OpenGL; 2301use DC::OpenGL;
2302
2303sub new {
2304 my $class = shift;
2305
2306 $class->SUPER::new (
2307 history_pointer => -1,
2308 @_
2309 )
2310}
2311
2289 2312
2290sub invoke_key_down { 2313sub invoke_key_down {
2291 my ($self, $ev) = @_; 2314 my ($self, $ev) = @_;
2292 2315
2293 my $sym = $ev->{sym}; 2316 my $sym = $ev->{sym};
2299 $self->{history_pointer} = -1; 2322 $self->{history_pointer} = -1;
2300 $self->{history_saveback} = ''; 2323 $self->{history_saveback} = '';
2301 $self->emit (activate => $txt); 2324 $self->emit (activate => $txt);
2302 $self->update; 2325 $self->update;
2303 2326
2304 } elsif ($sym == dc::SDLK_UP) { 2327 } elsif ($sym == DC::SDLK_UP) {
2305 if ($self->{history_pointer} < 0) { 2328 if ($self->{history_pointer} < 0) {
2306 $self->{history_saveback} = $self->get_text; 2329 $self->{history_saveback} = $self->get_text;
2307 } 2330 }
2308 if (@{$self->{history} || []} > 0) { 2331 if (@{$self->{history} || []} > 0) {
2309 $self->{history_pointer}++; 2332 $self->{history_pointer}++;
2311 $self->{history_pointer} = @{$self->{history} || []} - 1; 2334 $self->{history_pointer} = @{$self->{history} || []} - 1;
2312 } 2335 }
2313 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2336 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2314 } 2337 }
2315 2338
2316 } elsif ($sym == dc::SDLK_DOWN) { 2339 } elsif ($sym == DC::SDLK_DOWN) {
2317 $self->{history_pointer}--; 2340 $self->{history_pointer}--;
2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2341 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2319 2342
2320 if ($self->{history_pointer} >= 0) { 2343 if ($self->{history_pointer} >= 0) {
2321 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2344 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2322 } else { 2345 } else {
2346 if (defined $self->{history_saveback}) {
2323 $self->set_text ($self->{history_saveback}); 2347 $self->set_text ($self->{history_saveback});
2348 $self->{history_saveback} = undef;
2349 }
2324 } 2350 }
2325 2351
2326 } else { 2352 } else {
2327 return $self->SUPER::invoke_key_down ($ev) 2353 return $self->SUPER::invoke_key_down ($ev)
2328 } 2354 }
2330 1 2356 1
2331} 2357}
2332 2358
2333############################################################################# 2359#############################################################################
2334 2360
2335package dc::UI::TextEdit; 2361package DC::UI::TextEdit;
2336 2362
2337our @ISA = dc::UI::EntryBase::; 2363our @ISA = DC::UI::EntryBase::;
2338 2364
2339use dc::OpenGL; 2365use DC::OpenGL;
2366
2367sub new {
2368 my $class = shift;
2369
2370 $class->SUPER::new (
2371 padding_y => 4,
2372
2373 @_
2374 )
2375}
2340 2376
2341sub move_cursor_ver { 2377sub move_cursor_ver {
2342 my ($self, $dy) = @_; 2378 my ($self, $dy) = @_;
2343 2379
2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2380 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2345 2381
2346 $y += $dy; 2382 $line += $dy;
2347 2383
2348 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2384 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2349 $self->{cursor} = $index; 2385 $self->{cursor} = $index;
2350 delete $self->{cur_h}; 2386 delete $self->{cur_h};
2351 $self->update; 2387 $self->update;
2352 return; 2388 return;
2353 } 2389 }
2356sub invoke_key_down { 2392sub invoke_key_down {
2357 my ($self, $ev) = @_; 2393 my ($self, $ev) = @_;
2358 2394
2359 my $sym = $ev->{sym}; 2395 my $sym = $ev->{sym};
2360 2396
2361 if ($sym == dc::SDLK_UP) { 2397 if ($sym == DC::SDLK_UP) {
2362 $self->move_cursor_ver (-1); 2398 $self->move_cursor_ver (-1);
2363 } elsif ($sym == dc::SDLK_DOWN) { 2399 } elsif ($sym == DC::SDLK_DOWN) {
2364 $self->move_cursor_ver (+1); 2400 $self->move_cursor_ver (+1);
2365 } else { 2401 } else {
2366 return $self->SUPER::invoke_key_down ($ev) 2402 return $self->SUPER::invoke_key_down ($ev)
2367 } 2403 }
2368 2404
2369 1 2405 1
2370} 2406}
2371 2407
2372############################################################################# 2408#############################################################################
2373 2409
2374package dc::UI::ButtonBin; 2410package DC::UI::ButtonBin;
2375 2411
2376our @ISA = dc::UI::Bin::; 2412our @ISA = DC::UI::Bin::;
2377 2413
2378use dc::OpenGL; 2414use DC::OpenGL;
2379 2415
2380my @tex = 2416my @tex =
2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2417 map { new_from_resource DC::Texture $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png); 2418 qw(b1_button_inactive.png b1_button_active.png);
2383 2419
2384sub new { 2420sub new {
2385 my $class = shift; 2421 my $class = shift;
2386 2422
2387 $class->SUPER::new ( 2423 $class->SUPER::new (
2388 can_hover => 1, 2424 can_hover => 1,
2389 align => 0, 2425 align => 0.5,
2390 valign => 0, 2426 valign => 0.5,
2391 can_events => 1, 2427 can_events => 1,
2392 @_ 2428 @_
2393 ) 2429 )
2394} 2430}
2395 2431
2418 $self->SUPER::_draw; 2454 $self->SUPER::_draw;
2419} 2455}
2420 2456
2421############################################################################# 2457#############################################################################
2422 2458
2423package dc::UI::Button; 2459package DC::UI::Button;
2424 2460
2425our @ISA = dc::UI::Label::; 2461our @ISA = DC::UI::Label::;
2426 2462
2427use dc::OpenGL; 2463use DC::OpenGL;
2428 2464
2429my @tex = 2465my @tex =
2430 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2466 map { new_from_resource DC::Texture $_, mipmap => 1 }
2431 qw(b1_button_inactive.png b1_button_active.png); 2467 qw(b1_button_inactive.png b1_button_active.png);
2432 2468
2433sub new { 2469sub new {
2434 my $class = shift; 2470 my $class = shift;
2435 2471
2436 $class->SUPER::new ( 2472 $class->SUPER::new (
2437 padding_x => 4, 2473 padding_x => 8,
2438 padding_y => 4, 2474 padding_y => 4,
2439 fg => [1.0, 1.0, 1.0], 2475 fg => [1.0, 1.0, 1.0],
2440 active_fg => [0.8, 0.8, 0.8], 2476 active_fg => [0.8, 0.8, 0.8],
2441 can_hover => 1, 2477 can_hover => 1,
2442 align => 0, 2478 align => 0.5,
2443 valign => 0, 2479 valign => 0.5,
2444 can_events => 1, 2480 can_events => 1,
2445 @_ 2481 @_
2446 ) 2482 )
2447} 2483}
2448 2484
2473 $self->SUPER::_draw; 2509 $self->SUPER::_draw;
2474} 2510}
2475 2511
2476############################################################################# 2512#############################################################################
2477 2513
2478package dc::UI::CheckBox; 2514package DC::UI::CheckBox;
2479 2515
2480our @ISA = dc::UI::DrawBG::; 2516our @ISA = DC::UI::DrawBG::;
2481 2517
2482my @tex = 2518my @tex =
2483 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2519 map { new_from_resource DC::Texture $_, mipmap => 1 }
2484 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2520 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2485 2521
2486use dc::OpenGL; 2522use DC::OpenGL;
2487 2523
2488sub new { 2524sub new {
2489 my $class = shift; 2525 my $class = shift;
2490 2526
2491 $class->SUPER::new ( 2527 $class->SUPER::new (
2528 fontsize => 1,
2492 padding_x => 2, 2529 padding_x => 2,
2493 padding_y => 2, 2530 padding_y => 2,
2494 fg => [1, 1, 1], 2531 fg => [1, 1, 1],
2495 active_fg => [1, 1, 0], 2532 active_fg => [1, 1, 0],
2496 bg => [0, 0, 0, 0.2], 2533 bg => [0, 0, 0, 0.2],
2502} 2539}
2503 2540
2504sub size_request { 2541sub size_request {
2505 my ($self) = @_; 2542 my ($self) = @_;
2506 2543
2507 (6) x 2 2544 ($self->{fontsize} * $::FONTSIZE) x 2
2508} 2545}
2509 2546
2510sub toggle { 2547sub toggle {
2511 my ($self) = @_; 2548 my ($self) = @_;
2512 2549
2548 glDisable GL_TEXTURE_2D; 2585 glDisable GL_TEXTURE_2D;
2549} 2586}
2550 2587
2551############################################################################# 2588#############################################################################
2552 2589
2553package dc::UI::Image; 2590package DC::UI::Image;
2554 2591
2555our @ISA = dc::UI::Base::; 2592our @ISA = DC::UI::Base::;
2556 2593
2557use dc::OpenGL; 2594use DC::OpenGL;
2558 2595
2559our %texture_cache; 2596our %texture_cache;
2560 2597
2561sub new { 2598sub new {
2562 my $class = shift; 2599 my $class = shift;
2569 2606
2570 $self->{path} || $self->{tex} 2607 $self->{path} || $self->{tex}
2571 or Carp::croak "'path' or 'tex' attributes required"; 2608 or Carp::croak "'path' or 'tex' attributes required";
2572 2609
2573 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2610 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2574 new_from_file dc::Texture dc::find_rcfile $self->{path}, mipmap => 1; 2611 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2575 2612
2576 dc::weaken $texture_cache{$self->{path}}; 2613 DC::weaken $texture_cache{$self->{path}};
2577 2614
2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2615 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2579 2616
2580 $self 2617 $self
2581} 2618}
2582 2619
2583sub STORABLE_freeze { 2620sub STORABLE_freeze {
2584 my ($self, $cloning) = @_; 2621 my ($self, $cloning) = @_;
2585 2622
2586 $self->{path} 2623 $self->{path}
2587 or die "cannot serialise dc::UI::Image on non-loadable images\n"; 2624 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2588 2625
2589 $self->{path} 2626 $self->{path}
2590} 2627}
2591 2628
2592sub STORABLE_attach { 2629sub STORABLE_attach {
2623 glDisable GL_TEXTURE_2D; 2660 glDisable GL_TEXTURE_2D;
2624} 2661}
2625 2662
2626############################################################################# 2663#############################################################################
2627 2664
2628package dc::UI::ImageButton; 2665package DC::UI::ImageButton;
2629 2666
2630our @ISA = dc::UI::Image::; 2667our @ISA = DC::UI::Image::;
2631 2668
2632use dc::OpenGL; 2669use DC::OpenGL;
2633 2670
2634my %textures; 2671my %textures;
2635 2672
2636sub new { 2673sub new {
2637 my $class = shift; 2674 my $class = shift;
2640 padding_x => 4, 2677 padding_x => 4,
2641 padding_y => 4, 2678 padding_y => 4,
2642 fg => [1, 1, 1], 2679 fg => [1, 1, 1],
2643 active_fg => [0, 0, 1], 2680 active_fg => [0, 0, 1],
2644 can_hover => 1, 2681 can_hover => 1,
2645 align => 0, 2682 align => 0.5,
2646 valign => 0, 2683 valign => 0.5,
2647 can_events => 1, 2684 can_events => 1,
2648 @_ 2685 @_
2649 ); 2686 );
2650} 2687}
2651 2688
2665 1 2702 1
2666} 2703}
2667 2704
2668############################################################################# 2705#############################################################################
2669 2706
2670package dc::UI::VGauge; 2707package DC::UI::VGauge;
2671 2708
2672our @ISA = dc::UI::Base::; 2709our @ISA = DC::UI::Base::;
2673 2710
2674use List::Util qw(min max); 2711use List::Util qw(min max);
2675 2712
2676use dc::OpenGL; 2713use DC::OpenGL;
2677 2714
2678my %tex = ( 2715my %tex = (
2679 food => [ 2716 food => [
2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2717 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2718 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2682 ], 2719 ],
2683 grace => [ 2720 grace => [
2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2721 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/ 2722 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2686 ], 2723 ],
2687 hp => [ 2724 hp => [
2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2725 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2726 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2690 ], 2727 ],
2691 mana => [ 2728 mana => [
2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2729 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/ 2730 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2694 ], 2731 ],
2695); 2732);
2696 2733
2697# eg. VGauge->new (gauge => 'food'), default gauge: food 2734# eg. VGauge->new (gauge => 'food'), default gauge: food
2800 glDisable GL_TEXTURE_2D; 2837 glDisable GL_TEXTURE_2D;
2801} 2838}
2802 2839
2803############################################################################# 2840#############################################################################
2804 2841
2805package dc::UI::Progress; 2842package DC::UI::Progress;
2806 2843
2807our @ISA = dc::UI::Label::; 2844our @ISA = DC::UI::Label::;
2808 2845
2809use dc::OpenGL; 2846use DC::OpenGL;
2810 2847
2811sub new { 2848sub new {
2812 my ($class, %arg) = @_; 2849 my ($class, %arg) = @_;
2813 2850
2814 my $self = $class->SUPER::new ( 2851 my $self = $class->SUPER::new (
2852 padding_x => 2,
2853 padding_y => 2,
2815 fg => [1, 1, 1], 2854 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2], 2855 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8], 2856 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0], 2857 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9, 2858 fontsize => 0.9,
2820 valign => 0, 2859 valign => 0.5,
2821 align => 0, 2860 align => 0.5,
2822 can_events => 1, 2861 can_events => 1,
2823 ellipsise => 1, 2862 ellipsise => 1,
2824 label => "%d%%", 2863 label => "%d%%",
2825 %arg, 2864 %arg,
2826 ); 2865 );
2834 my ($self, $label) = @_; 2873 my ($self, $label) = @_;
2835 2874
2836 return if $self->{label} eq $label; 2875 return if $self->{label} eq $label;
2837 $self->{label} = $label; 2876 $self->{label} = $label;
2838 2877
2839 $self->dc::UI::Progress::set_value (0 + delete $self->{value}); 2878 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2840} 2879}
2841 2880
2842sub set_value { 2881sub set_value {
2843 my ($self, $value) = @_; 2882 my ($self, $value) = @_;
2844 2883
2859 my ($self) = @_; 2898 my ($self) = @_;
2860 2899
2861 glEnable GL_BLEND; 2900 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2901 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863 2902
2903 my $px = $self->{padding_x};
2904 my $py = $self->{padding_y};
2905
2864 if ($self->{value} >= 0) { 2906 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2907 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2866 2908
2867 glColor_premultiply @{$self->{bar}}; 2909 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2; 2910 glRect $px, $py, $s, $self->{h} - $py;
2869 glColor_premultiply @{$self->{bg}}; 2911 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2912 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2871 } 2913 }
2872 2914
2873 glColor_premultiply @{$self->{outline}}; 2915 glColor_premultiply @{$self->{outline}};
2916
2917 $px -= .5;
2918 $py -= .5;
2919
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2920 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2875 2921
2876 glDisable GL_BLEND; 2922 glDisable GL_BLEND;
2877 2923
2878 { 2924 {
2879 local $self->{bg}; # do not draw background 2925 local $self->{bg}; # do not draw background
2881 } 2927 }
2882} 2928}
2883 2929
2884############################################################################# 2930#############################################################################
2885 2931
2886package dc::UI::ExperienceProgress; 2932package DC::UI::ExperienceProgress;
2887 2933
2888our @ISA = dc::UI::Progress::; 2934our @ISA = DC::UI::Progress::;
2889 2935
2890sub new { 2936sub new {
2891 my ($class, %arg) = @_; 2937 my ($class, %arg) = @_;
2892 2938
2939 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2940
2893 my $self = $class->SUPER::new ( 2941 my $self = $class->SUPER::new (
2942 %arg,
2894 tooltip => sub { 2943 tooltip => sub {
2895 my ($self) = @_; 2944 my ($self) = @_;
2896 2945
2897 sprintf "level %d\n%s points\n%s next level\n%s to go", 2946 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2947 $tt,
2898 $self->{lvl}, 2948 $self->{lvl},
2899 ::formsep ($self->{exp}), 2949 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}), 2950 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}), 2951 ::formsep ($self->{nxt} - $self->{exp}),
2952 $self->_percent * 100,
2902 }, 2953 },
2903 %arg
2904 ); 2954 );
2905 2955
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2956 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN; 2957 if $::CONN;
2908 2958
2916 if $::CONN; 2966 if $::CONN;
2917 2967
2918 $self->SUPER::DESTROY; 2968 $self->SUPER::DESTROY;
2919} 2969}
2920 2970
2971sub _percent {
2972 my ($self) = @_;
2973
2974 my $table = $::CONN && $::CONN->{exp_table}
2975 or return -1;
2976
2977 my $l0 = $table->[$self->{lvl} - 1];
2978 my $l1 = $table->[$self->{lvl}];
2979
2980 $self->{nxt} = $l1;
2981
2982 ($self->{exp} - $l0) / ($l1 - $l0)
2983}
2984
2921sub set_value { 2985sub set_value {
2922 my ($self, $lvl, $exp) = @_; 2986 my ($self, $lvl, $exp) = @_;
2923 2987
2924 $self->{lvl} = $lvl; 2988 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp; 2989 $self->{exp} = $exp;
2926 2990
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); 2991 $self->SUPER::set_value ($self->_percent);
2939} 2992}
2940 2993
2941############################################################################# 2994#############################################################################
2942 2995
2943package dc::UI::Gauge; 2996package DC::UI::Gauge;
2944 2997
2945our @ISA = dc::UI::VBox::; 2998our @ISA = DC::UI::VBox::;
2946 2999
2947sub new { 3000sub new {
2948 my ($class, %arg) = @_; 3001 my ($class, %arg) = @_;
2949 3002
2950 my $self = $class->SUPER::new ( 3003 my $self = $class->SUPER::new (
2952 can_hover => 1, 3005 can_hover => 1,
2953 can_events => 1, 3006 can_events => 1,
2954 %arg, 3007 %arg,
2955 ); 3008 );
2956 3009
2957 $self->add ($self->{value} = new dc::UI::Label valign => +1, align => 0, template => "999"); 3010 $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); 3011 $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"); 3012 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2960 3013
2961 $self 3014 $self
2962} 3015}
2963 3016
2964sub set_fontsize { 3017sub set_fontsize {
2985 $self->{value}->set_text ($val); 3038 $self->{value}->set_text ($val);
2986} 3039}
2987 3040
2988############################################################################# 3041#############################################################################
2989 3042
2990package dc::UI::Slider; 3043package DC::UI::Slider;
2991 3044
2992use strict; 3045use strict;
2993 3046
2994use dc::OpenGL; 3047use DC::OpenGL;
2995 3048
2996our @ISA = dc::UI::DrawBG::; 3049our @ISA = DC::UI::DrawBG::;
2997 3050
2998my @tex = 3051my @tex =
2999 map { new_from_file dc::Texture dc::find_rcfile $_ } 3052 map { new_from_resource DC::Texture $_ }
3000 qw(s1_slider.png s1_slider_bg.png); 3053 qw(s1_slider.png s1_slider_bg.png);
3001 3054
3002sub new { 3055sub new {
3003 my $class = shift; 3056 my $class = shift;
3004 3057
3041sub set_value { 3094sub set_value {
3042 my ($self, $value) = @_; 3095 my ($self, $value) = @_;
3043 3096
3044 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3097 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3045 3098
3046 $hi = $lo + 1 if $hi <= $lo; 3099 $hi = $lo if $hi < $lo;
3047 3100
3048 $page = $hi - $lo if $page > $hi - $lo; 3101 $value = $hi - $page if $value > $hi - $page;
3049
3050 $value = $lo if $value < $lo; 3102 $value = $lo if $value < $lo;
3051 $value = $hi - $page if $value > $hi - $page;
3052 3103
3053 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3104 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3054 if $unit; 3105 if $unit;
3055 3106
3056 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3107 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3100sub invoke_mouse_wheel { 3151sub invoke_mouse_wheel {
3101 my ($self, $ev) = @_; 3152 my ($self, $ev) = @_;
3102 3153
3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3154 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3104 3155
3105 my $pagepart = $ev->{mod} & dc::KMOD_SHIFT ? 1 : 0.2; 3156 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3106 3157
3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3158 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3108 3159
3109 1 3160 1
3110} 3161}
3120 my ($self) = @_; 3171 my ($self) = @_;
3121 3172
3122 unless ($self->{knob_w}) { 3173 unless ($self->{knob_w}) {
3123 $self->set_value ($self->{range}[0]); 3174 $self->set_value ($self->{range}[0]);
3124 3175
3125 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3176 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3126 my $range = ($hi - $page - $lo) || 1e-100; 3177 my $range = ($hi - $page - $lo) || 1e-10;
3127 3178
3128 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3179 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3129 3180
3130 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3181 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3131 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3182 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3132 3183
3133 $value = ($value - $lo) / $range; 3184 $value = ($value - $lo) / $range;
3163 glDisable GL_TEXTURE_2D; 3214 glDisable GL_TEXTURE_2D;
3164} 3215}
3165 3216
3166############################################################################# 3217#############################################################################
3167 3218
3168package dc::UI::ValSlider; 3219package DC::UI::ValSlider;
3169 3220
3170our @ISA = dc::UI::HBox::; 3221our @ISA = DC::UI::HBox::;
3171 3222
3172sub new { 3223sub new {
3173 my ($class, %arg) = @_; 3224 my ($class, %arg) = @_;
3174 3225
3175 my $range = delete $arg{range}; 3226 my $range = delete $arg{range};
3176 3227
3177 my $self = $class->SUPER::new ( 3228 my $self = $class->SUPER::new (
3178 slider => (new dc::UI::Slider expand => 1, range => $range), 3229 slider => (new DC::UI::Slider expand => 1, range => $range),
3179 entry => (new dc::UI::Label text => "", template => delete $arg{template}), 3230 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3180 to_value => sub { shift }, 3231 to_value => sub { shift },
3181 from_value => sub { shift }, 3232 from_value => sub { shift },
3182 %arg, 3233 %arg,
3183 ); 3234 );
3184 3235
3204sub set_range { shift->{slider}->set_range (@_) } 3255sub set_range { shift->{slider}->set_range (@_) }
3205sub set_value { shift->{slider}->set_value (@_) } 3256sub set_value { shift->{slider}->set_value (@_) }
3206 3257
3207############################################################################# 3258#############################################################################
3208 3259
3209package dc::UI::TextScroller; 3260package DC::UI::TextScroller;
3210 3261
3211our @ISA = dc::UI::HBox::; 3262our @ISA = DC::UI::HBox::;
3212 3263
3213use dc::OpenGL; 3264use DC::OpenGL;
3214 3265
3215sub new { 3266sub new {
3216 my $class = shift; 3267 my $class = shift;
3217 3268
3218 my $self = $class->SUPER::new ( 3269 my $self = $class->SUPER::new (
3220 can_events => 1, 3271 can_events => 1,
3221 indent => 0, 3272 indent => 0,
3222 #font => default_font 3273 #font => default_font
3223 @_, 3274 @_,
3224 3275
3225 layout => (new dc::Layout), 3276 layout => (new DC::Layout),
3226 par => [], 3277 par => [],
3227 max_par => 0, 3278 max_par => 0,
3228 height => 0, 3279 height => 0,
3229 children => [ 3280 children => [
3230 (new dc::UI::Empty expand => 1), 3281 (new DC::UI::Empty expand => 1),
3231 (new dc::UI::Slider vertical => 1), 3282 (new DC::UI::Slider vertical => 1),
3232 ], 3283 ],
3233 ); 3284 );
3234 3285
3235 $self->{children}[1]->connect (changed => sub { $self->update }); 3286 $self->{children}[1]->connect (changed => sub { $self->update });
3236 3287
3292 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3343 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3293 $layout->set_markup ($para->{markup}); 3344 $layout->set_markup ($para->{markup});
3294 3345
3295 $layout->set_shapes ( 3346 $layout->set_shapes (
3296 map 3347 map
3297 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3348 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3298 @{$para->{widget}} 3349 @{$para->{widget}}
3299 ); 3350 );
3300 3351
3301 $layout 3352 $layout
3302} 3353}
3419 $ROOT->on_post_alloc ($self => sub { 3470 $ROOT->on_post_alloc ($self => sub {
3420 $self->force_uptodate; 3471 $self->force_uptodate;
3421 3472
3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3473 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3423 3474
3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub { 3475 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3425 glClearColor 0, 0, 0, 0; 3476 glClearColor 0, 0, 0, 0;
3426 glClear GL_COLOR_BUFFER_BIT; 3477 glClear GL_COLOR_BUFFER_BIT;
3427 3478
3428 package dc::UI::Base; 3479 package DC::UI::Base;
3429 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3480 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3430 (0, 0, $self->{w}, $self->{h}); 3481 (0, 0, $self->{w}, $self->{h});
3431 3482
3432 my $top = int $self->{children}[1]{range}[0]; 3483 my $top = int $self->{children}[1]{range}[0];
3433 3484
3492 $self->{children}[1]->draw; 3543 $self->{children}[1]->draw;
3493} 3544}
3494 3545
3495############################################################################# 3546#############################################################################
3496 3547
3497package dc::UI::Animator; 3548package DC::UI::Animator;
3498 3549
3499use dc::OpenGL; 3550use DC::OpenGL;
3500 3551
3501our @ISA = dc::UI::Bin::; 3552our @ISA = DC::UI::Bin::;
3502 3553
3503sub moveto { 3554sub moveto {
3504 my ($self, $x, $y) = @_; 3555 my ($self, $x, $y) = @_;
3505 3556
3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3557 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3534 glPopMatrix; 3585 glPopMatrix;
3535} 3586}
3536 3587
3537############################################################################# 3588#############################################################################
3538 3589
3539package dc::UI::Flopper; 3590package DC::UI::Flopper;
3540 3591
3541our @ISA = dc::UI::Button::; 3592our @ISA = DC::UI::Button::;
3542 3593
3543sub new { 3594sub new {
3544 my $class = shift; 3595 my $class = shift;
3545 3596
3546 my $self = $class->SUPER::new ( 3597 my $self = $class->SUPER::new (
3558 $self->{other}->toggle_visibility; 3609 $self->{other}->toggle_visibility;
3559} 3610}
3560 3611
3561############################################################################# 3612#############################################################################
3562 3613
3563package dc::UI::Tooltip; 3614package DC::UI::Tooltip;
3564 3615
3565our @ISA = dc::UI::Bin::; 3616our @ISA = DC::UI::Bin::;
3566 3617
3567use dc::OpenGL; 3618use DC::OpenGL;
3568 3619
3569sub new { 3620sub new {
3570 my $class = shift; 3621 my $class = shift;
3571 3622
3572 $class->SUPER::new ( 3623 $class->SUPER::new (
3579 my ($self, $widget) = @_; 3630 my ($self, $widget) = @_;
3580 3631
3581 my $tip = $widget->{tooltip}; 3632 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip; 3633 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583 3634
3584 $tip = dc::Pod::section_label tooltip => $1 3635 $tip = DC::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/; 3636 if $tip =~ /^#(.*)$/;
3586 3637
3587 if ($ENV{CFPLUS_DEBUG} & 2) { 3638 if ($ENV{CFPLUS_DEBUG} & 2) {
3588 $tip .= "\n\n" . (ref $widget) . "\n" 3639 $tip .= "\n\n" . (ref $widget) . "\n"
3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3640 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3592 } 3643 }
3593 3644
3594 $tip =~ s/^\n+//; 3645 $tip =~ s/^\n+//;
3595 $tip =~ s/\n+$//; 3646 $tip =~ s/\n+$//;
3596 3647
3597 $self->add (new dc::UI::Label 3648 $self->add (new DC::UI::Label
3649 fg => $DC::THEME{tooltip_fg},
3598 markup => $tip, 3650 markup => $tip,
3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3651 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3652 align => 0,
3600 fontsize => 0.8, 3653 fontsize => 0.8,
3601 style => 1, # FLAG_INVERSE 3654 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3602 ellipsise => 0, 3655 ellipsise => 0,
3603 font => ($widget->{tooltip_font} || $::FONT_PROP), 3656 font => ($widget->{tooltip_font} || $::FONT_PROP),
3604 ); 3657 );
3605} 3658}
3606 3659
3643sub _draw { 3696sub _draw {
3644 my ($self) = @_; 3697 my ($self) = @_;
3645 3698
3646 my ($w, $h) = @$self{qw(w h)}; 3699 my ($w, $h) = @$self{qw(w h)};
3647 3700
3648 glColor 1, 0.8, 0.4; 3701 glColor @{ $DC::THEME{tooltip_bg} };
3649 glRect 0, 0, $w, $h; 3702 glRect 0, 0, $w, $h;
3650 3703
3651 glColor 0, 0, 0; 3704 glColor @{ $DC::THEME{tooltip_border} };
3652 glRect_lineloop .5, .5, $w + .5, $h + .5; 3705 glRect_lineloop .5, .5, $w + .5, $h + .5;
3653 3706
3654 glTranslate 2, 2; 3707 glTranslate 2, 2;
3655 3708
3656 $self->SUPER::_draw; 3709 $self->SUPER::_draw;
3657} 3710}
3658 3711
3659############################################################################# 3712#############################################################################
3660 3713
3661package dc::UI::Face; 3714package DC::UI::Face;
3662 3715
3663our @ISA = dc::UI::DrawBG::; 3716our @ISA = DC::UI::DrawBG::;
3664 3717
3665use dc::OpenGL; 3718use DC::OpenGL;
3666 3719
3667sub new { 3720sub new {
3668 my $class = shift; 3721 my $class = shift;
3669 3722
3670 my $self = $class->SUPER::new ( 3723 my $self = $class->SUPER::new (
3674 can_events => 0, 3727 can_events => 0,
3675 @_, 3728 @_,
3676 ); 3729 );
3677 3730
3678 if ($self->{anim} && $self->{animspeed}) { 3731 if ($self->{anim} && $self->{animspeed}) {
3679 dc::weaken (my $widget = $self); 3732 DC::weaken (my $widget = $self);
3680 3733
3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; 3734 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { 3735 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3683 return unless $::CONN; 3736 return unless $::CONN;
3684 3737
3790 $self->SUPER::destroy; 3843 $self->SUPER::destroy;
3791} 3844}
3792 3845
3793############################################################################# 3846#############################################################################
3794 3847
3795package dc::UI::Buttonbar; 3848package DC::UI::Buttonbar;
3796 3849
3797our @ISA = dc::UI::HBox::; 3850our @ISA = DC::UI::HBox::;
3798 3851
3799# TODO: should actually wrap buttons and other goodies. 3852# TODO: should actually wrap buttons and other goodies.
3800 3853
3801############################################################################# 3854#############################################################################
3802 3855
3803package dc::UI::Menu; 3856package DC::UI::Menu;
3804 3857
3805our @ISA = dc::UI::Toplevel::; 3858our @ISA = DC::UI::Toplevel::;
3806 3859
3807use dc::OpenGL; 3860use DC::OpenGL;
3808 3861
3809sub new { 3862sub new {
3810 my $class = shift; 3863 my $class = shift;
3811 3864
3812 my $self = $class->SUPER::new ( 3865 my $self = $class->SUPER::new (
3813 items => [], 3866 items => [],
3814 z => 100, 3867 z => 100,
3815 @_, 3868 @_,
3816 ); 3869 );
3817 3870
3818 $self->add ($self->{vbox} = new dc::UI::VBox); 3871 $self->add ($self->{vbox} = new DC::UI::VBox);
3819 3872
3820 for my $item (@{ $self->{items} }) { 3873 for my $item (@{ $self->{items} }) {
3821 my ($widget, $cb, $tooltip) = @$item; 3874 my ($widget, $cb, $tooltip) = @$item;
3822 3875
3823 # handle various types of items, only text for now 3876 # handle various types of items, only text for now
3824 if (!ref $widget) { 3877 if (!ref $widget) {
3825 if ($widget =~ /\t/) { 3878 if ($widget =~ /\t/) {
3826 my ($left, $right) = split /\t/, $widget, 2; 3879 my ($left, $right) = split /\t/, $widget, 2;
3827 3880
3828 $widget = new dc::UI::HBox 3881 $widget = new DC::UI::HBox
3829 can_hover => 1, 3882 can_hover => 1,
3830 can_events => 1, 3883 can_events => 1,
3831 tooltip => $tooltip, 3884 tooltip => $tooltip,
3832 children => [ 3885 children => [
3833 (new dc::UI::Label markup => $left, expand => 1), 3886 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3834 (new dc::UI::Label markup => $right, align => +1), 3887 (new DC::UI::Label markup => $right, align => 1),
3835 ], 3888 ],
3836 ; 3889 ;
3837 3890
3838 } else { 3891 } else {
3839 $widget = new dc::UI::Label 3892 $widget = new DC::UI::Label
3840 can_hover => 1, 3893 can_hover => 1,
3841 can_events => 1, 3894 can_events => 1,
3895 align => 0,
3842 markup => $widget, 3896 markup => $widget,
3843 tooltip => $tooltip; 3897 tooltip => $tooltip;
3844 } 3898 }
3845 } 3899 }
3846 3900
3861 # maybe save $GRAB? must be careful about events... 3915 # maybe save $GRAB? must be careful about events...
3862 $GRAB = $self; 3916 $GRAB = $self;
3863 $self->{button} = $ev->{button}; 3917 $self->{button} = $ev->{button};
3864 3918
3865 $self->show; 3919 $self->show;
3866 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3920
3921 my $x = $ev->{x};
3922 my $y = $ev->{y};
3923
3924 $self->{root}->on_post_alloc ($self => sub {
3925 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3926 });
3927
3928 1 # so it can be used inside event handlers
3867} 3929}
3868 3930
3869sub invoke_mouse_motion { 3931sub invoke_mouse_motion {
3870 my ($self, $ev, $x, $y) = @_; 3932 my ($self, $ev, $x, $y) = @_;
3871 3933
3892 1 3954 1
3893} 3955}
3894 3956
3895############################################################################# 3957#############################################################################
3896 3958
3897package dc::UI::Multiplexer; 3959package DC::UI::Multiplexer;
3898 3960
3899our @ISA = dc::UI::Container::; 3961our @ISA = DC::UI::Container::;
3900 3962
3901sub new { 3963sub new {
3902 my $class = shift; 3964 my $class = shift;
3903 3965
3904 my $self = $class->SUPER::new ( 3966 my $self = $class->SUPER::new (
3905 @_, 3967 @_,
3906 ); 3968 );
3907 3969
3908 $self->{current} = $self->{children}[0] 3970 $self->set_current_page (0);
3909 if @{ $self->{children} };
3910 3971
3911 $self 3972 $self
3912} 3973}
3913 3974
3914sub add { 3975sub add {
3915 my ($self, @widgets) = @_; 3976 my ($self, @widgets) = @_;
3916 3977
3917 $self->SUPER::add (@widgets); 3978 $self->SUPER::add (@widgets);
3918 3979
3919 $self->{current} = $self->{children}[0] 3980 $self->set_current_page (0)
3920 if @{ $self->{children} }; 3981 if @widgets == @{ $self->{children} };
3921} 3982}
3922 3983
3923sub get_current_page { 3984sub get_current_page {
3924 my ($self) = @_; 3985 my ($self) = @_;
3925 3986
3931 3992
3932 my $widget = ref $page_or_widget 3993 my $widget = ref $page_or_widget
3933 ? $page_or_widget 3994 ? $page_or_widget
3934 : $self->{children}[$page_or_widget]; 3995 : $self->{children}[$page_or_widget];
3935 3996
3997 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3998
3936 $self->{current} = $widget; 3999 if (($self->{current} = $widget)) {
4000 $self->{current}->set_visible if $self->{current} && $self->{visible};
3937 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4001 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3938 4002
3939 $self->emit (page_changed => $self->{current}); 4003 $self->emit (page_changed => $self->{current});
4004 }
3940 4005
3941 $self->realloc; 4006 $self->realloc;
3942} 4007}
3943 4008
3944sub visible_children { 4009sub visible_children {
3945 $_[0]{current} 4010 $_[0]{current} || ()
3946} 4011}
3947 4012
3948sub size_request { 4013sub size_request {
3949 my ($self) = @_; 4014 my ($self) = @_;
3950 4015
4016 $self->{current}
3951 $self->{current}->size_request 4017 ? $self->{current}->size_request
4018 : (0, 0)
3952} 4019}
3953 4020
3954sub invoke_size_allocate { 4021sub invoke_size_allocate {
3955 my ($self, $w, $h) = @_; 4022 my ($self, $w, $h) = @_;
3956 4023
3957 $self->{current}->configure (0, 0, $w, $h); 4024 $self->{current}->configure (0, 0, $w, $h)
4025 if $self->{current};
3958 4026
3959 1 4027 1
3960} 4028}
3961 4029
3962sub _draw { 4030sub _draw {
3963 my ($self) = @_; 4031 my ($self) = @_;
3964 4032
3965 $self->{current}->draw; 4033 $self->{current}->draw
4034 if $self->{current};
3966} 4035}
3967 4036
3968############################################################################# 4037#############################################################################
3969 4038
3970package dc::UI::Notebook; 4039package DC::UI::Notebook;
3971 4040
3972use dc::OpenGL; 4041use DC::OpenGL;
3973 4042
3974our @ISA = dc::UI::VBox::; 4043our @ISA = DC::UI::VBox::;
3975 4044
3976sub new { 4045sub new {
3977 my $class = shift; 4046 my $class = shift;
3978 4047
3979 my $self = $class->SUPER::new ( 4048 my $self = $class->SUPER::new (
3980 buttonbar => (new dc::UI::Buttonbar), 4049 buttonbar => (new DC::UI::Buttonbar),
3981 multiplexer => (new dc::UI::Multiplexer expand => 1), 4050 multiplexer => (new DC::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2], 4051 active_outline => [.7, .7, 0.2],
3983 # filter => # will be put between multiplexer and $self 4052 # filter => # will be put between multiplexer and $self
3984 @_, 4053 @_,
3985 ); 4054 );
3986 4055
3995 4064
3996 for my $child (@$widgets) { 4065 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child; 4066 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do { 4067 $child->{c_tab_} ||= do {
3999 my $tab = 4068 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "dc::UI::Base") 4069 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4001 ? $child->{c_tab} 4070 ? $child->{c_tab}
4002 : new dc::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4071 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003 4072
4004 $tab->connect (activate => sub { 4073 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child); 4074 $wself->set_current_page ($child);
4006 }); 4075 });
4007 4076
4037} 4106}
4038 4107
4039sub pages { 4108sub pages {
4040 my ($self) = @_; 4109 my ($self) = @_;
4041 $self->{multiplexer}->children 4110 $self->{multiplexer}->children
4111}
4112
4113sub page_index {
4114 my ($self, $widget) = @_;
4115
4116 my $i = 0;
4117 for ($self->pages) {
4118 if ($_ eq $widget) { return $i };
4119 $i++;
4120 }
4121
4122 undef
4042} 4123}
4043 4124
4044sub add_tab { 4125sub add_tab {
4045 my ($self, $title, $widget, $tooltip) = @_; 4126 my ($self, $title, $widget, $tooltip) = @_;
4046 4127
4083 } 4164 }
4084} 4165}
4085 4166
4086############################################################################# 4167#############################################################################
4087 4168
4088package dc::UI::Selector; 4169package DC::UI::Selector;
4089 4170
4090use utf8; 4171use utf8;
4091 4172
4092our @ISA = dc::UI::Button::; 4173our @ISA = DC::UI::Button::;
4093 4174
4094sub new { 4175sub new {
4095 my $class = shift; 4176 my $class = shift;
4096 4177
4097 my $self = $class->SUPER::new ( 4178 my $self = $class->SUPER::new (
4114 my ($value, $title, $tooltip) = @$_; 4195 my ($value, $title, $tooltip) = @$_;
4115 4196
4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4197 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4117 } 4198 }
4118 4199
4119 dc::UI::Menu->new (items => \@menu_items)->popup ($ev); 4200 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4120} 4201}
4121 4202
4122sub _set_value { 4203sub _set_value {
4123 my ($self, $value) = @_; 4204 my ($self, $value) = @_;
4124 4205
4147 $self->_set_value ($self->{value}); 4228 $self->_set_value ($self->{value});
4148} 4229}
4149 4230
4150############################################################################# 4231#############################################################################
4151 4232
4152package dc::UI::Statusbox; 4233package DC::UI::Statusbox;
4153 4234
4154our @ISA = dc::UI::VBox::; 4235our @ISA = DC::UI::VBox::;
4155 4236
4156sub new { 4237sub new {
4157 my $class = shift; 4238 my $class = shift;
4158 4239
4159 my $self = $class->SUPER::new ( 4240 my $self = $class->SUPER::new (
4160 fontsize => 0.8, 4241 fontsize => 0.8,
4161 @_, 4242 @_,
4162 ); 4243 );
4163 4244
4164 dc::weaken (my $this = $self); 4245 DC::weaken (my $this = $self);
4165 4246
4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4247 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4167 4248
4168 $self 4249 $self
4169} 4250}
4170 4251
4171sub reorder { 4252sub reorder {
4172 my ($self) = @_; 4253 my ($self) = @_;
4173 my $NOW = Time::HiRes::time; 4254 my $NOW = EV::time;
4174 4255
4175 # freeze display when hovering over any label 4256 # freeze display when hovering over any label
4176 return if $dc::UI::TOOLTIP->{owner} 4257 return if $DC::UI::TOOLTIP->{owner}
4177 && grep $dc::UI::TOOLTIP->{owner} == $_->{label}, 4258 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4178 values %{ $self->{item} }; 4259 values %{ $self->{item} };
4179 4260
4180 while (my ($k, $v) = each %{ $self->{item} }) { 4261 while (my ($k, $v) = each %{ $self->{item} }) {
4181 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4262 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4182 } 4263 }
4203 for ($short) { 4284 for ($short) {
4204 s/^\s+//; 4285 s/^\s+//;
4205 s/\s+/ /g; 4286 s/\s+/ /g;
4206 } 4287 }
4207 4288
4208 new dc::UI::Label 4289 new DC::UI::Label
4209 markup => $short, 4290 markup => $short,
4210 tooltip => $item->{tooltip}, 4291 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4292 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4293 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4294 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4295 max_w => $::WIDTH * 0.44,
4296 align => 0,
4215 fg => [@{ $item->{fg} }], 4297 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4298 can_events => 1,
4217 can_hover => 1 4299 can_hover => 1
4218 }; 4300 };
4219 4301
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4309 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4310 }
4229 4311
4230 push @widgets, $label; 4312 push @widgets, $label;
4231 } 4313 }
4314
4315 my $hash = join ",", @widgets;
4316 return if $hash eq $self->{last_widget_hash};
4317 $self->{last_widget_hash} = $hash;
4232 4318
4233 $self->clear; 4319 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4320 $self->SUPER::add (reverse @widgets);
4235} 4321}
4236 4322
4292 $self->SUPER::destroy; 4378 $self->SUPER::destroy;
4293} 4379}
4294 4380
4295############################################################################# 4381#############################################################################
4296 4382
4297package dc::UI::Root; 4383package DC::UI::Root;
4298 4384
4299our @ISA = dc::UI::Container::; 4385our @ISA = DC::UI::Container::;
4300 4386
4301use List::Util qw(min max); 4387use List::Util qw(min max);
4302 4388
4303use dc::OpenGL; 4389use DC::OpenGL;
4304 4390
4305sub new { 4391sub new {
4306 my $class = shift; 4392 my $class = shift;
4307 4393
4308 my $self = $class->SUPER::new ( 4394 my $self = $class->SUPER::new (
4309 visible => 1, 4395 visible => 1,
4310 @_, 4396 @_,
4311 ); 4397 );
4312 4398
4313 dc::weaken ($self->{root} = $self); 4399 DC::weaken ($self->{root} = $self);
4314 4400
4315 $self 4401 $self
4316} 4402}
4317 4403
4318sub size_request { 4404sub size_request {
4443 4529
4444 delete $queue{$widget+0}; 4530 delete $queue{$widget+0};
4445 4531
4446 my ($w, $h) = $widget->size_request; 4532 my ($w, $h) = $widget->size_request;
4447 4533
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4534 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4535 $h += $widget->{padding_y} * 2;
4536
4537 $w = max $widget->{min_w}, $w;
4538 $h = max $widget->{min_h}, $h;
4450 4539
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4540 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4541 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4542
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4543 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4610 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4611 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4612 glLoadIdentity;
4524 4613
4525 { 4614 {
4526 package dc::UI::Base; 4615 package DC::UI::Base;
4527 4616
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4617 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4618 (0, 0, $self->{w}, $self->{h});
4530 4619
4531 $self->_draw; 4620 $self->_draw;
4532 } 4621 }
4533} 4622}
4534 4623
4535############################################################################# 4624#############################################################################
4536 4625
4537package dc::UI; 4626package DC::UI;
4538 4627
4539$ROOT = new dc::UI::Root; 4628$ROOT = new DC::UI::Root;
4540$TOOLTIP = new dc::UI::Tooltip z => 900; 4629$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4630
45421 46311
4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines