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.447 by root, Sat Dec 22 06:50:45 2007 UTC vs.
Revision 1.473 by root, Sun Jan 11 03:19:47 2009 UTC

1package CFPlus::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 CFPlus; 8use DC;
9use CFPlus::Pod; 9use DC::Pod;
10use CFPlus::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;
16our $TOOLTIP; 16our $TOOLTIP;
17our $BUTTON_STATE; 17our $BUTTON_STATE;
18 18
19our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20 20
21our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop; 22 $_[0]->stop;
23 23
24 if (!$GRAB) { 24 if (!$GRAB) {
25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
26 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
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} & CFPlus::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);
189# call when resolution changes etc. 189# call when resolution changes etc.
190sub rescale_widgets { 190sub rescale_widgets {
191 my ($sx, $sy) = @_; 191 my ($sx, $sy) = @_;
192 192
193 for my $widget (values %WIDGET) { 193 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 194 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 195 $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.]+$/; 196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 197
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 198 $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}; 199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
211 reconfigure_widgets; 211 reconfigure_widgets;
212} 212}
213 213
214############################################################################# 214#############################################################################
215 215
216package CFPlus::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 CFPlus::UI::Base; 224package DC::UI::Base;
225 225
226use strict; 226use strict;
227 227
228use CFPlus::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 CFPlus::weaken ($CFPlus::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 = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
252 $self->{x} = $layout->{x} * $CFPlus::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} * $CFPlus::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} * $CFPlus::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} * $CFPlus::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 $CFPlus::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 $CFPlus::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}
338sub hide { 341sub hide {
339 my ($self) = @_; 342 my ($self) = @_;
340 343
341 $self->set_invisible; 344 $self->set_invisible;
342 345
346 # extra $parent copy for 5.8.8+ bug workaround
347 # (otherwise $_[0] in remove gets freed
348 if (my $parent = $self->{parent}) {
343 $self->{parent}->remove ($self) 349 $parent->remove ($self);
344 if $self->{parent}; 350 }
345} 351}
346 352
347sub move_abs { 353sub move_abs {
348 my ($self, $x, $y, $z) = @_; 354 my ($self, $x, $y, $z) = @_;
349 355
365 371
366# 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
367sub get_max_wh { 373sub get_max_wh {
368 my ($self) = @_; 374 my ($self) = @_;
369 375
376 my ($w, $h) = @$self{qw(max_w max_h)};
377
378 if ($w <= 0 || $h <= 0) {
379 my ($mw, $mh) = $self->{parent}
370 return $self->{parent}->get_max_wh 380 ? $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT) 381 : ($::WIDTH, $::HEIGHT);
382
383 $w = $mw if $w <= 0;
384 $h = $mh if $h <= 0;
385 }
386
387 ($w, $h)
374} 388}
375 389
376sub size_request { 390sub size_request {
377 require Carp; 391 require Carp;
378 Carp::confess "size_request is abstract"; 392 Carp::confess "size_request is abstract";
386 my ($self, $x, $y, $w, $h) = @_; 400 my ($self, $x, $y, $w, $h) = @_;
387 401
388 if ($self->{aspect}) { 402 if ($self->{aspect}) {
389 my ($ow, $oh) = ($w, $h); 403 my ($ow, $oh) = ($w, $h);
390 404
391 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 405 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
392 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 406 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
393 407
394 # use alignment to adjust x, y 408 # use alignment to adjust x, y
395 409
396 $x += int 0.5 * ($ow - $w); 410 $x += int 0.5 * ($ow - $w);
397 $y += int 0.5 * ($oh - $h); 411 $y += int 0.5 * ($oh - $h);
438 452
439 return if $self->{tooltip} eq $tooltip; 453 return if $self->{tooltip} eq $tooltip;
440 454
441 $self->{tooltip} = $tooltip; 455 $self->{tooltip} = $tooltip;
442 456
443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 457 if ($DC::UI::TOOLTIP->{owner} == $self) {
444 delete $CFPlus::UI::TOOLTIP->{owner}; 458 delete $DC::UI::TOOLTIP->{owner};
445 $CFPlus::UI::TOOLTIP_WATCHER->invoke; 459 $DC::UI::TOOLTIP_WATCHER->invoke;
446 } 460 }
447} 461}
448 462
449# translate global coordinates to local coordinate system 463# translate global coordinates to local coordinate system
450sub coord2local { 464sub coord2local {
516sub connect { 530sub connect {
517 my ($self, $signal, $cb) = @_; 531 my ($self, $signal, $cb) = @_;
518 532
519 push @{ $self->{signal_cb}{$signal} }, $cb; 533 push @{ $self->{signal_cb}{$signal} }, $cb;
520 534
521 defined wantarray and CFPlus::guard { 535 defined wantarray and DC::guard {
522 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
523 @{ $self->{signal_cb}{$signal} }; 537 @{ $self->{signal_cb}{$signal} };
524 } 538 }
525} 539}
526 540
564# in .xs 578# in .xs
565 579
566sub set_parent { 580sub set_parent {
567 my ($self, $parent) = @_; 581 my ($self, $parent) = @_;
568 582
569 CFPlus::weaken ($self->{parent} = $parent); 583 DC::weaken ($self->{parent} = $parent);
570 $self->set_visible if $parent->{visible}; 584 $self->set_visible if $parent->{visible};
571} 585}
572 586
573sub realloc { 587sub realloc {
574 my ($self) = @_; 588 my ($self) = @_;
612} 626}
613 627
614sub DESTROY { 628sub DESTROY {
615 my ($self) = @_; 629 my ($self) = @_;
616 630
617 return if CFPlus::in_destruct; 631 return if DC::in_destruct;
618 632
619 local $@; 633 local $@;
620 eval { $self->destroy }; 634 eval { $self->destroy };
621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 635 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
622 636
623 delete $WIDGET{$self+0}; 637 delete $WIDGET{$self+0};
624} 638}
625 639
626############################################################################# 640#############################################################################
627 641
628package CFPlus::UI::DrawBG; 642package DC::UI::DrawBG;
629 643
630our @ISA = CFPlus::UI::Base::; 644our @ISA = DC::UI::Base::;
631 645
632use strict; 646use strict;
633use CFPlus::OpenGL; 647use DC::OpenGL;
634 648
635sub new { 649sub new {
636 my $class = shift; 650 my $class = shift;
637 651
638 $class->SUPER::new ( 652 $class->SUPER::new (
660 } 674 }
661} 675}
662 676
663############################################################################# 677#############################################################################
664 678
665package CFPlus::UI::Empty; 679package DC::UI::Empty;
666 680
667our @ISA = CFPlus::UI::Base::; 681our @ISA = DC::UI::Base::;
668 682
669sub new { 683sub new {
670 my ($class, %arg) = @_; 684 my ($class, %arg) = @_;
671 $class->SUPER::new (can_events => 0, %arg); 685 $class->SUPER::new (can_events => 0, %arg);
672} 686}
679 693
680sub draw { } 694sub draw { }
681 695
682############################################################################# 696#############################################################################
683 697
684package CFPlus::UI::Container; 698package DC::UI::Container;
685 699
686our @ISA = CFPlus::UI::Base::; 700our @ISA = DC::UI::Base::;
687 701
688sub new { 702sub new {
689 my ($class, %arg) = @_; 703 my ($class, %arg) = @_;
690 704
691 my $children = delete $arg{children}; 705 my $children = delete $arg{children};
785 $_->draw for $self->visible_children; 799 $_->draw for $self->visible_children;
786} 800}
787 801
788############################################################################# 802#############################################################################
789 803
790package CFPlus::UI::Bin; 804package DC::UI::Bin;
791 805
792our @ISA = CFPlus::UI::Container::; 806our @ISA = DC::UI::Container::;
793 807
794sub new { 808sub new {
795 my ($class, %arg) = @_; 809 my ($class, %arg) = @_;
796 810
797 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 811 my $child = (delete $arg{child}) || new DC::UI::Empty::;
798 812
799 $class->SUPER::new (children => [$child], %arg) 813 $class->SUPER::new (children => [$child], %arg)
800} 814}
801 815
802sub add { 816sub add {
809sub remove { 823sub remove {
810 my ($self, $widget) = @_; 824 my ($self, $widget) = @_;
811 825
812 $self->SUPER::remove ($widget); 826 $self->SUPER::remove ($widget);
813 827
814 $self->{children} = [new CFPlus::UI::Empty] 828 $self->{children} = [new DC::UI::Empty]
815 unless @{$self->{children}}; 829 unless @{$self->{children}};
816} 830}
817 831
818sub child { $_[0]->{children}[0] } 832sub child { $_[0]->{children}[0] }
819 833
830} 844}
831 845
832############################################################################# 846#############################################################################
833# back-buffered drawing area 847# back-buffered drawing area
834 848
835package CFPlus::UI::Window; 849package DC::UI::Window;
836 850
837our @ISA = CFPlus::UI::Bin::; 851our @ISA = DC::UI::Bin::;
838 852
839use CFPlus::OpenGL; 853use DC::OpenGL;
840 854
841sub new { 855sub new {
842 my ($class, %arg) = @_; 856 my ($class, %arg) = @_;
843 857
844 my $self = $class->SUPER::new (%arg); 858 my $self = $class->SUPER::new (%arg);
866} 880}
867 881
868sub render_child { 882sub render_child {
869 my ($self) = @_; 883 my ($self) = @_;
870 884
871 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 885 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
872 glClearColor 0, 0, 0, 0; 886 glClearColor 0, 0, 0, 0;
873 glClear GL_COLOR_BUFFER_BIT; 887 glClear GL_COLOR_BUFFER_BIT;
874 888
875 { 889 {
876 package CFPlus::UI::Base; 890 package DC::UI::Base;
877 891
878 local ($draw_x, $draw_y, $draw_w, $draw_h) = 892 local ($draw_x, $draw_y, $draw_w, $draw_h) =
879 (0, 0, $self->{w}, $self->{h}); 893 (0, 0, $self->{w}, $self->{h});
880 894
881 $self->_render; 895 $self->_render;
898 glDisable GL_TEXTURE_2D; 912 glDisable GL_TEXTURE_2D;
899} 913}
900 914
901############################################################################# 915#############################################################################
902 916
903package CFPlus::UI::ViewPort; 917package DC::UI::ViewPort;
904 918
905use List::Util qw(min max); 919use List::Util qw(min max);
906 920
907our @ISA = CFPlus::UI::Window::; 921our @ISA = DC::UI::Window::;
908 922
909sub new { 923sub new {
910 my $class = shift; 924 my $class = shift;
911 925
912 $class->SUPER::new ( 926 $class->SUPER::new (
996 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
997 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
998 ) { 1012 ) {
999 $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})
1000 } else { 1014 } else {
1001 $self->CFPlus::UI::Base::find_widget ($x, $y) 1015 $self->DC::UI::Base::find_widget ($x, $y)
1002 } 1016 }
1003} 1017}
1004 1018
1005sub _render { 1019sub _render {
1006 my ($self) = @_; 1020 my ($self) = @_;
1007 1021
1008 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1022 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1009 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1023 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1010 1024
1011 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1025 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1012 1026
1013 $self->SUPER::_render; 1027 $self->SUPER::_render;
1014} 1028}
1015 1029
1016############################################################################# 1030#############################################################################
1017 1031
1018package CFPlus::UI::ScrolledWindow; 1032package DC::UI::ScrolledWindow;
1019 1033
1020our @ISA = CFPlus::UI::Table::; 1034our @ISA = DC::UI::Table::;
1021 1035
1022sub new { 1036sub new {
1023 my ($class, %arg) = @_; 1037 my ($class, %arg) = @_;
1024 1038
1025 my $child = delete $arg{child}; 1039 my $child = delete $arg{child};
1026 1040
1027 my $self; 1041 my $self;
1028 1042
1029 my $hslider = new CFPlus::UI::Slider 1043 my $hslider = new DC::UI::Slider
1030 c_col => 0, 1044 c_col => 0,
1031 c_row => 1, 1045 c_row => 1,
1032 vertical => 0, 1046 vertical => 0,
1033 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1034 on_changed => sub { 1048 on_changed => sub {
1035 $self->{hpos} = $_[1]; 1049 $self->{hpos} = $_[1];
1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1037 }, 1051 },
1038 ; 1052 ;
1039 1053
1040 my $vslider = new CFPlus::UI::Slider 1054 my $vslider = new DC::UI::Slider
1041 c_col => 1, 1055 c_col => 1,
1042 c_row => 0, 1056 c_row => 0,
1043 vertical => 1, 1057 vertical => 1,
1044 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1045 on_changed => sub { 1059 on_changed => sub {
1057 col_expand => [1, 0], 1071 col_expand => [1, 0],
1058 row_expand => [1, 0], 1072 row_expand => [1, 0],
1059 %arg, 1073 %arg,
1060 ); 1074 );
1061 1075
1062 $self->{vp} = new CFPlus::UI::ViewPort 1076 $self->{vp} = new DC::UI::ViewPort
1063 c_col => 0, 1077 c_col => 0,
1064 c_row => 0, 1078 c_row => 0,
1065 expand => 1, 1079 expand => 1,
1066 scroll_x => $self->{scroll_x}, 1080 scroll_x => $self->{scroll_x},
1067 scroll_y => $self->{scroll_y}, 1081 scroll_y => $self->{scroll_y},
1193 $self->SUPER::invoke_size_allocate ($w, $h) 1207 $self->SUPER::invoke_size_allocate ($w, $h)
1194} 1208}
1195 1209
1196############################################################################# 1210#############################################################################
1197 1211
1198package CFPlus::UI::Frame; 1212package DC::UI::Frame;
1199 1213
1200our @ISA = CFPlus::UI::Bin::; 1214our @ISA = DC::UI::Bin::;
1201 1215
1202use CFPlus::OpenGL; 1216use DC::OpenGL;
1203 1217
1204sub new { 1218sub new {
1205 my $class = shift; 1219 my $class = shift;
1206 1220
1207 $class->SUPER::new ( 1221 $class->SUPER::new (
1226 $self->SUPER::_draw; 1240 $self->SUPER::_draw;
1227} 1241}
1228 1242
1229############################################################################# 1243#############################################################################
1230 1244
1231package CFPlus::UI::FancyFrame; 1245package DC::UI::FancyFrame;
1232 1246
1233our @ISA = CFPlus::UI::Bin::; 1247our @ISA = DC::UI::Bin::;
1234 1248
1235use CFPlus::OpenGL; 1249use DC::OpenGL;
1236 1250
1237sub new { 1251sub new {
1238 my ($class, %arg) = @_; 1252 my ($class, %arg) = @_;
1239 1253
1240 if ((exists $arg{label}) && !ref $arg{label}) { 1254 if ((exists $arg{label}) && !ref $arg{label}) {
1241 $arg{label} = new CFPlus::UI::Label 1255 $arg{label} = new DC::UI::Label
1242 align => 1, 1256 align => 1,
1243 valign => 0, 1257 valign => 0.5,
1244 text => $arg{label}, 1258 text => $arg{label},
1245 fontsize => ($arg{border} || 0.8) * 0.75; 1259 fontsize => ($arg{border} || 0.8) * 0.75;
1246 } 1260 }
1247 1261
1248 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1249 # label => "", 1263 # label => "",
1250 fg => [0.6, 0.3, 0.1], 1264 fg => undef,
1251 border => 0.8, 1265 border => 0.8,
1252 style => 'single', 1266 style => 'single',
1253 %arg, 1267 %arg,
1254 ); 1268 );
1255 1269
1258 1272
1259sub add { 1273sub add {
1260 my ($self, @widgets) = @_; 1274 my ($self, @widgets) = @_;
1261 1275
1262 $self->SUPER::add (@widgets); 1276 $self->SUPER::add (@widgets);
1263 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1277 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1264} 1278}
1265 1279
1266sub border { 1280sub border {
1267 int $_[0]{border} * $::FONTSIZE 1281 int $_[0]{border} * $::FONTSIZE
1268} 1282}
1308 my $border = $self->border; 1322 my $border = $self->border;
1309 my ($w, $h) = ($self->{w}, $self->{h}); 1323 my ($w, $h) = ($self->{w}, $self->{h});
1310 1324
1311 $child->draw; 1325 $child->draw;
1312 1326
1313 glColor @{$self->{fg}}; 1327 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1314 glBegin GL_LINE_STRIP; 1328 glBegin GL_LINE_STRIP;
1315 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1329 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1330 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1317 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;
1318 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;
1326 } 1340 }
1327} 1341}
1328 1342
1329############################################################################# 1343#############################################################################
1330 1344
1331package CFPlus::UI::Toplevel; 1345package DC::UI::Toplevel;
1332 1346
1333our @ISA = CFPlus::UI::Bin::; 1347our @ISA = DC::UI::Bin::;
1334 1348
1335use CFPlus::OpenGL; 1349use DC::OpenGL;
1336 1350
1337my $bg = 1351my $bg =
1338 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1352 new_from_resource DC::Texture "d1_bg.png",
1339 mipmap => 1, wrap => 1; 1353 mipmap => 1, wrap => 1;
1340 1354
1341my @border = 1355my @border =
1342 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1356 map { new_from_resource DC::Texture $_, mipmap => 1 }
1343 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);
1344 1358
1345my @icon = 1359my @icon =
1346 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1360 map { new_from_resource DC::Texture $_, mipmap => 1 }
1347 qw(x1_move.png x1_resize.png); 1361 qw(x1_move.png x1_resize.png);
1348 1362
1349sub new { 1363sub new {
1350 my ($class, %arg) = @_; 1364 my ($class, %arg) = @_;
1351 1365
1352 my $self = $class->SUPER::new ( 1366 my $self = $class->SUPER::new (
1353 bg => [1, 1, 1, 1], 1367 bg => [1, 1, 1, 1],
1354 border_bg => [1, 1, 1, 1], 1368 border_bg => [1, 1, 1, 1],
1355 border => 0.6, 1369 border => 0.8,
1356 can_events => 1, 1370 can_events => 1,
1357 min_w => 64, 1371 min_w => 64,
1358 min_h => 32, 1372 min_h => 32,
1359 %arg, 1373 %arg,
1360 ); 1374 );
1361 1375
1362 $self->{title_widget} = new CFPlus::UI::Label 1376 $self->{title_widget} = new DC::UI::Label
1363 align => 0, 1377 align => 0.5,
1364 valign => 1, 1378 valign => 1,
1365 text => $self->{title}, 1379 text => $self->{title},
1366 fontsize => $self->{border}, 1380 fontsize => $self->{border},
1367 if exists $self->{title}; 1381 if exists $self->{title};
1368 1382
1369 if ($self->{has_close_button}) { 1383 if ($self->{has_close_button}) {
1370 $self->{close_button} = 1384 $self->{close_button} =
1371 new CFPlus::UI::ImageButton 1385 new DC::UI::ImageButton
1372 path => 'x1_close.png', 1386 path => 'x1_close.png',
1373 on_activate => sub { $self->emit ("delete") }; 1387 on_activate => sub { $self->emit ("delete") };
1374 1388
1375 $self->CFPlus::UI::Container::add ($self->{close_button}); 1389 $self->DC::UI::Container::add ($self->{close_button});
1376 } 1390 }
1377 1391
1378 $self 1392 $self
1379} 1393}
1380 1394
1381sub add { 1395sub add {
1382 my ($self, @widgets) = @_; 1396 my ($self, @widgets) = @_;
1383 1397
1384 $self->SUPER::add (@widgets); 1398 $self->SUPER::add (@widgets);
1385 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1399 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1386 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1400 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1387} 1401}
1388 1402
1389sub border { 1403sub border {
1390 int $_[0]{border} * $::FONTSIZE 1404 int $_[0]{border} * $::FONTSIZE
1391} 1405}
1529 glEnable GL_TEXTURE_2D; 1543 glEnable GL_TEXTURE_2D;
1530 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1544 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1531 1545
1532 my $border = $self->border; 1546 my $border = $self->border;
1533 1547
1548 if ($border) {
1534 glColor @{ $self->{border_bg} }; 1549 glColor @{ $self->{border_bg} };
1535 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1550 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1536 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1551 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1537 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1552 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1538 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1553 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1539 1554
1540 # move 1555 # move
1541 my $w2 = ($w - $border) * .5; 1556 my $w2 = ($w - $border) * .5;
1542 my $h2 = ($h - $border) * .5; 1557 my $h2 = ($h - $border) * .5;
1543 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1558 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1559 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1545 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1560 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1546 1561
1547 # resize 1562 # resize
1548 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1563 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1564 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1550 unless $self->{has_close_button}; 1565 unless $self->{has_close_button};
1551 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1566 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1567 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1568 }
1553 1569
1554 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1570 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1555 glColor @{ $self->{bg} }; 1571 glColor @{ $self->{bg} };
1556 1572
1557 # TODO: repeat texture not scale 1573 # TODO: repeat texture not scale
1576 if $self->{close_button}; 1592 if $self->{close_button};
1577} 1593}
1578 1594
1579############################################################################# 1595#############################################################################
1580 1596
1581package CFPlus::UI::Table; 1597package DC::UI::Table;
1582 1598
1583our @ISA = CFPlus::UI::Container::; 1599our @ISA = DC::UI::Container::;
1584 1600
1585use List::Util qw(max sum); 1601use List::Util qw(max sum);
1586 1602
1587use CFPlus::OpenGL; 1603use DC::OpenGL;
1588 1604
1589sub new { 1605sub new {
1590 my $class = shift; 1606 my $class = shift;
1591 1607
1592 $class->SUPER::new ( 1608 $class->SUPER::new (
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1652 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1637 1653
1638 my $sw = sum @w[$c .. $c + $cs - 1]; 1654 my $sw = sum @w[$c .. $c + $cs - 1];
1639 1655
1640 if ($w > $sw) { 1656 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1657 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1642 } 1658 }
1643 } 1659 }
1644 1660
1645 # second pass, rows 1661 # second pass, rows
1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1662 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1663 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1648 1664
1649 my $sh = sum @h[$r .. $r + $rs - 1]; 1665 my $sh = sum @h[$r .. $r + $rs - 1];
1650 1666
1651 if ($h > $sh) { 1667 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1668 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1653 } 1669 }
1654 } 1670 }
1655 1671
1656 (\@w, \@h) 1672 (\@w, \@h)
1657} 1673}
1680 @col_expand = (1) x @$ws unless @col_expand; 1696 @col_expand = (1) x @$ws unless @col_expand;
1681 my $col_expand = (sum @col_expand) || 1; 1697 my $col_expand = (sum @col_expand) || 1;
1682 1698
1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1699 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1684 1700
1685 CFPlus::UI::harmonize $ws; 1701 DC::UI::harmonize $ws;
1686 1702
1687 my @row_expand = @{$self->{row_expand}}; 1703 my @row_expand = @{$self->{row_expand}};
1688 @row_expand = (1) x @$ws unless @row_expand; 1704 @row_expand = (1) x @$ws unless @row_expand;
1689 my $row_expand = (sum @row_expand) || 1; 1705 my $row_expand = (sum @row_expand) || 1;
1690 1706
1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1707 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1692 1708
1693 CFPlus::UI::harmonize $hs; 1709 DC::UI::harmonize $hs;
1694 1710
1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1711 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1712 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1697 1713
1698 for my $widget ($self->children) { 1714 for my $widget ($self->children) {
1707 1 1723 1
1708} 1724}
1709 1725
1710############################################################################# 1726#############################################################################
1711 1727
1712package CFPlus::UI::Fixed; 1728package DC::UI::Fixed;
1713 1729
1714use List::Util qw(min max); 1730use List::Util qw(min max);
1715 1731
1716our @ISA = CFPlus::UI::Container::; 1732our @ISA = DC::UI::Container::;
1717 1733
1718sub _scale($$$) { 1734sub _scale($$$) {
1719 my ($rel, $val, $max) = @_; 1735 my ($rel, $val, $max) = @_;
1720 1736
1721 $rel ? $val * $max : $val 1737 $rel ? $val * $max : $val
1773 1 1789 1
1774} 1790}
1775 1791
1776############################################################################# 1792#############################################################################
1777 1793
1778package CFPlus::UI::Box; 1794package DC::UI::Box;
1779 1795
1780our @ISA = CFPlus::UI::Container::; 1796our @ISA = DC::UI::Container::;
1781 1797
1782sub size_request { 1798sub size_request {
1783 my ($self) = @_; 1799 my ($self) = @_;
1784 1800
1785 my @children = $self->visible_children; 1801 my @children = $self->visible_children;
1820 $req[$_] += $space * $children[$_]{expand} 1836 $req[$_] += $space * $children[$_]{expand}
1821 for 0 .. $#children; 1837 for 0 .. $#children;
1822 } 1838 }
1823 } 1839 }
1824 1840
1825 CFPlus::UI::harmonize \@req; 1841 DC::UI::harmonize \@req;
1826 1842
1827 my $pos = 0; 1843 my $pos = 0;
1828 for (0 .. $#children) { 1844 for (0 .. $#children) {
1829 my $alloc = $req[$_]; 1845 my $alloc = $req[$_];
1830 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1846 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1835 1 1851 1
1836} 1852}
1837 1853
1838############################################################################# 1854#############################################################################
1839 1855
1840package CFPlus::UI::HBox; 1856package DC::UI::HBox;
1841 1857
1842our @ISA = CFPlus::UI::Box::; 1858our @ISA = DC::UI::Box::;
1843 1859
1844sub new { 1860sub new {
1845 my $class = shift; 1861 my $class = shift;
1846 1862
1847 $class->SUPER::new ( 1863 $class->SUPER::new (
1850 ) 1866 )
1851} 1867}
1852 1868
1853############################################################################# 1869#############################################################################
1854 1870
1855package CFPlus::UI::VBox; 1871package DC::UI::VBox;
1856 1872
1857our @ISA = CFPlus::UI::Box::; 1873our @ISA = DC::UI::Box::;
1858 1874
1859sub new { 1875sub new {
1860 my $class = shift; 1876 my $class = shift;
1861 1877
1862 $class->SUPER::new ( 1878 $class->SUPER::new (
1865 ) 1881 )
1866} 1882}
1867 1883
1868############################################################################# 1884#############################################################################
1869 1885
1870package CFPlus::UI::Label; 1886package DC::UI::Label;
1871 1887
1872our @ISA = CFPlus::UI::DrawBG::; 1888our @ISA = DC::UI::DrawBG::;
1873 1889
1874use CFPlus::OpenGL; 1890use DC::OpenGL;
1875 1891
1876sub new { 1892sub new {
1877 my ($class, %arg) = @_; 1893 my ($class, %arg) = @_;
1878 1894
1879 my $self = $class->SUPER::new ( 1895 my $self = $class->SUPER::new (
1884 #text => initial text 1900 #text => initial text
1885 #markup => initial narkup 1901 #markup => initial narkup
1886 #max_w => maximum pixel width 1902 #max_w => maximum pixel width
1887 #style => 0, # render flags 1903 #style => 0, # render flags
1888 ellipsise => 3, # end 1904 ellipsise => 3, # end
1889 layout => (new CFPlus::Layout), 1905 layout => (new DC::Layout),
1890 fontsize => 1, 1906 fontsize => 1,
1891 align => -1, 1907 align => 0.5,
1892 valign => -1, 1908 valign => 0.5,
1893 padding_x => 2, 1909 padding_x => 4,
1894 padding_y => 2, 1910 padding_y => 2,
1895 can_events => 0, 1911 can_events => 0,
1896 %arg 1912 %arg
1897 ); 1913 );
1898 1914
1899 if (exists $self->{template}) { 1915 if (exists $self->{template}) {
1900 my $layout = new CFPlus::Layout; 1916 my $layout = new DC::Layout;
1901 $layout->set_text (delete $self->{template}); 1917 $layout->set_text (delete $self->{template});
1902 $self->{template} = $layout; 1918 $self->{template} = $layout;
1903 } 1919 }
1904 1920
1905 if (exists $self->{markup}) { 1921 if (exists $self->{markup}) {
1964 1980
1965 $self->{size_req} ||= do { 1981 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh; 1982 my ($max_w, $max_h) = $self->get_max_wh;
1967 1983
1968 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1984 $self->{layout}->set_font ($self->{font}) if $self->{font};
1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1985 $self->{layout}->set_width ($max_w);
1970 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1986 $self->{layout}->set_ellipsise ($self->{ellipsise});
1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1973 1989
1974 my ($w, $h) = $self->{layout}->size; 1990 my ($w, $h) = $self->{layout}->size;
1975 1991
1976 if (exists $self->{template}) { 1992 if (exists $self->{template}) {
1977 $self->{template}->set_font ($self->{font}) if $self->{font}; 1993 $self->{template}->set_font ($self->{font}) if $self->{font};
1978 $self->{template}->set_width ($self->{max_w} || -1); 1994 $self->{template}->set_width ($max_w);
1979 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1995 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1980 1996
1981 my ($w2, $h2) = $self->{template}->size; 1997 my ($w2, $h2) = $self->{template}->size;
1982 1998
1983 $w = List::Util::max $w, $w2; 1999 $w = List::Util::max $w, $w2;
2039 2055
2040 [$self->{layout}->size] 2056 [$self->{layout}->size]
2041 }; 2057 };
2042 2058
2043 unless (exists $self->{ox}) { 2059 unless (exists $self->{ox}) {
2044 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2060 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2045 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2061 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2046 : ($self->{w} - $size->[0]) * 0.5);
2047
2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2050 : ($self->{h} - $size->[1]) * 0.5);
2051 2062
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2063 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2053 }; 2064 };
2054 2065
2055# unless ($self->{list}) { 2066# unless ($self->{list}) {
2056# $self->{list} = CFPlus::OpenGL::glGenList; 2067# $self->{list} = DC::OpenGL::glGenList;
2057# CFPlus::OpenGL::glNewList $self->{list}; 2068# DC::OpenGL::glNewList $self->{list};
2058# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2069# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2059# CFPlus::OpenGL::glEndList; 2070# DC::OpenGL::glEndList;
2060# } 2071# }
2061# 2072#
2062# CFPlus::OpenGL::glCallList $self->{list}; 2073# DC::OpenGL::glCallList $self->{list};
2063 2074
2064 $self->{layout}->draw; 2075 $self->{layout}->draw;
2065} 2076}
2066 2077
2067#sub destroy { 2078#sub destroy {
2068# my ($self) = @_; 2079# my ($self) = @_;
2069# 2080#
2070# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2081# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2071# 2082#
2072# $self->SUPER::destroy; 2083# $self->SUPER::destroy;
2073#} 2084#}
2074 2085
2075############################################################################# 2086#############################################################################
2076 2087
2077package CFPlus::UI::EntryBase; 2088package DC::UI::EntryBase;
2078 2089
2079our @ISA = CFPlus::UI::Label::; 2090our @ISA = DC::UI::Label::;
2080 2091
2081use CFPlus::OpenGL; 2092use DC::OpenGL;
2082 2093
2083sub new { 2094sub new {
2084 my $class = shift; 2095 my $class = shift;
2085 2096
2086 $class->SUPER::new ( 2097 $class->SUPER::new (
2087 fg => [1, 1, 1], 2098 fg => [1, 1, 1],
2088 bg => [0, 0, 0, 0.2], 2099 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1], 2100 outline => undef,
2090 active_bg => [0, 0, 1, .2], 2101 active_bg => [0, 0, 1, .2],
2091 active_fg => [1, 1, 1], 2102 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0], 2103 active_outline => [1, 1, 0],
2093 can_hover => 1, 2104 can_hover => 1,
2094 can_focus => 1, 2105 can_focus => 1,
2106 align => 0,
2095 valign => 0, 2107 valign => 0.5,
2096 can_events => 1, 2108 can_events => 1,
2097 ellipsise => 0, 2109 ellipsise => 0,
2110 padding_x => 4,
2111 padding_y => 2,
2098 #text => ... 2112 #text => ...
2099 #hidden => "*", 2113 #hidden => "*",
2100 @_ 2114 @_
2101 ) 2115 )
2102} 2116}
2149 2163
2150 my $text = $self->get_text; 2164 my $text = $self->get_text;
2151 2165
2152 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2166 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2153 2167
2154 if ($uni == 8) { 2168 if ($sym == DC::SDLK_BACKSPACE) {
2155 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2169 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2156 } elsif ($uni == 127) { 2170 } elsif ($sym == DC::SDLK_DELETE) {
2157 substr $text, $self->{cursor}, 1, ""; 2171 substr $text, $self->{cursor}, 1, "";
2158 } elsif ($sym == CFPlus::SDLK_LEFT) { 2172 } elsif ($sym == DC::SDLK_LEFT) {
2159 --$self->{cursor} if $self->{cursor}; 2173 --$self->{cursor} if $self->{cursor};
2160 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2174 } elsif ($sym == DC::SDLK_RIGHT) {
2161 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2175 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2162 } elsif ($sym == CFPlus::SDLK_HOME) { 2176 } elsif ($sym == DC::SDLK_HOME) {
2163 # what a hack 2177 # what a hack
2164 $self->{cursor} = 2178 $self->{cursor} =
2165 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2179 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2166 ? length $1 2180 ? length $1
2167 : 0; 2181 : 0;
2168 } elsif ($sym == CFPlus::SDLK_END) { 2182 } elsif ($sym == DC::SDLK_END) {
2169 # uh, again 2183 # uh, again
2170 $self->{cursor} = 2184 $self->{cursor} =
2171 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2185 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2172 ? $self->{cursor} + length $1 2186 ? $self->{cursor} + length $1
2173 : length $self->{text}; 2187 : length $self->{text};
2264 glColor @{$self->{active_outline}}; 2278 glColor @{$self->{active_outline}};
2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2279 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2266 glLineWidth 1; 2280 glLineWidth 1;
2267 2281
2268 } else { 2282 } else {
2269 glColor @{$self->{outline}}; 2283 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2270 glBegin GL_LINE_STRIP; 2284 glBegin GL_LINE_STRIP;
2271 glVertex .5, $self->{h} * .5; 2285 glVertex .5, $self->{h} * .5;
2272 glVertex .5, $self->{h} - 2.5; 2286 glVertex .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} - 2.5; 2287 glVertex $self->{w} - .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - .5, $self->{h} * .5; 2288 glVertex $self->{w} - .5, $self->{h} * .5;
2276 } 2290 }
2277} 2291}
2278 2292
2279############################################################################# 2293#############################################################################
2280 2294
2281package CFPlus::UI::Entry; 2295package DC::UI::Entry;
2282 2296
2283our @ISA = CFPlus::UI::EntryBase::; 2297our @ISA = DC::UI::EntryBase::;
2284 2298
2285use CFPlus::OpenGL; 2299use DC::OpenGL;
2300
2301sub new {
2302 my $class = shift;
2303
2304 $class->SUPER::new (
2305 history_pointer => -1,
2306 @_
2307 )
2308}
2309
2286 2310
2287sub invoke_key_down { 2311sub invoke_key_down {
2288 my ($self, $ev) = @_; 2312 my ($self, $ev) = @_;
2289 2313
2290 my $sym = $ev->{sym}; 2314 my $sym = $ev->{sym};
2296 $self->{history_pointer} = -1; 2320 $self->{history_pointer} = -1;
2297 $self->{history_saveback} = ''; 2321 $self->{history_saveback} = '';
2298 $self->emit (activate => $txt); 2322 $self->emit (activate => $txt);
2299 $self->update; 2323 $self->update;
2300 2324
2301 } elsif ($sym == CFPlus::SDLK_UP) { 2325 } elsif ($sym == DC::SDLK_UP) {
2302 if ($self->{history_pointer} < 0) { 2326 if ($self->{history_pointer} < 0) {
2303 $self->{history_saveback} = $self->get_text; 2327 $self->{history_saveback} = $self->get_text;
2304 } 2328 }
2305 if (@{$self->{history} || []} > 0) { 2329 if (@{$self->{history} || []} > 0) {
2306 $self->{history_pointer}++; 2330 $self->{history_pointer}++;
2308 $self->{history_pointer} = @{$self->{history} || []} - 1; 2332 $self->{history_pointer} = @{$self->{history} || []} - 1;
2309 } 2333 }
2310 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2334 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2311 } 2335 }
2312 2336
2313 } elsif ($sym == CFPlus::SDLK_DOWN) { 2337 } elsif ($sym == DC::SDLK_DOWN) {
2314 $self->{history_pointer}--; 2338 $self->{history_pointer}--;
2315 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2339 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2316 2340
2317 if ($self->{history_pointer} >= 0) { 2341 if ($self->{history_pointer} >= 0) {
2318 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2319 } else { 2343 } else {
2344 if (defined $self->{history_saveback}) {
2320 $self->set_text ($self->{history_saveback}); 2345 $self->set_text ($self->{history_saveback});
2346 $self->{history_saveback} = undef;
2347 }
2321 } 2348 }
2322 2349
2323 } else { 2350 } else {
2324 return $self->SUPER::invoke_key_down ($ev) 2351 return $self->SUPER::invoke_key_down ($ev)
2325 } 2352 }
2327 1 2354 1
2328} 2355}
2329 2356
2330############################################################################# 2357#############################################################################
2331 2358
2332package CFPlus::UI::TextEdit; 2359package DC::UI::TextEdit;
2333 2360
2334our @ISA = CFPlus::UI::EntryBase::; 2361our @ISA = DC::UI::EntryBase::;
2335 2362
2336use CFPlus::OpenGL; 2363use DC::OpenGL;
2364
2365sub new {
2366 my $class = shift;
2367
2368 $class->SUPER::new (
2369 padding_y => 4,
2370
2371 @_
2372 )
2373}
2337 2374
2338sub move_cursor_ver { 2375sub move_cursor_ver {
2339 my ($self, $dy) = @_; 2376 my ($self, $dy) = @_;
2340 2377
2341 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2378 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2342 2379
2343 $y += $dy; 2380 $line += $dy;
2344 2381
2345 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2382 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2346 $self->{cursor} = $index; 2383 $self->{cursor} = $index;
2347 delete $self->{cur_h}; 2384 delete $self->{cur_h};
2348 $self->update; 2385 $self->update;
2349 return; 2386 return;
2350 } 2387 }
2353sub invoke_key_down { 2390sub invoke_key_down {
2354 my ($self, $ev) = @_; 2391 my ($self, $ev) = @_;
2355 2392
2356 my $sym = $ev->{sym}; 2393 my $sym = $ev->{sym};
2357 2394
2358 if ($sym == CFPlus::SDLK_UP) { 2395 if ($sym == DC::SDLK_UP) {
2359 $self->move_cursor_ver (-1); 2396 $self->move_cursor_ver (-1);
2360 } elsif ($sym == CFPlus::SDLK_DOWN) { 2397 } elsif ($sym == DC::SDLK_DOWN) {
2361 $self->move_cursor_ver (+1); 2398 $self->move_cursor_ver (+1);
2362 } else { 2399 } else {
2363 return $self->SUPER::invoke_key_down ($ev) 2400 return $self->SUPER::invoke_key_down ($ev)
2364 } 2401 }
2365 2402
2366 1 2403 1
2367} 2404}
2368 2405
2369############################################################################# 2406#############################################################################
2370 2407
2371package CFPlus::UI::ButtonBin; 2408package DC::UI::ButtonBin;
2372 2409
2373our @ISA = CFPlus::UI::Bin::; 2410our @ISA = DC::UI::Bin::;
2374 2411
2375use CFPlus::OpenGL; 2412use DC::OpenGL;
2376 2413
2377my @tex = 2414my @tex =
2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2415 map { new_from_resource DC::Texture $_, mipmap => 1 }
2379 qw(b1_button_inactive.png b1_button_active.png); 2416 qw(b1_button_inactive.png b1_button_active.png);
2380 2417
2381sub new { 2418sub new {
2382 my $class = shift; 2419 my $class = shift;
2383 2420
2384 $class->SUPER::new ( 2421 $class->SUPER::new (
2385 can_hover => 1, 2422 can_hover => 1,
2386 align => 0, 2423 align => 0.5,
2387 valign => 0, 2424 valign => 0.5,
2388 can_events => 1, 2425 can_events => 1,
2389 @_ 2426 @_
2390 ) 2427 )
2391} 2428}
2392 2429
2415 $self->SUPER::_draw; 2452 $self->SUPER::_draw;
2416} 2453}
2417 2454
2418############################################################################# 2455#############################################################################
2419 2456
2420package CFPlus::UI::Button; 2457package DC::UI::Button;
2421 2458
2422our @ISA = CFPlus::UI::Label::; 2459our @ISA = DC::UI::Label::;
2423 2460
2424use CFPlus::OpenGL; 2461use DC::OpenGL;
2425 2462
2426my @tex = 2463my @tex =
2427 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2464 map { new_from_resource DC::Texture $_, mipmap => 1 }
2428 qw(b1_button_inactive.png b1_button_active.png); 2465 qw(b1_button_inactive.png b1_button_active.png);
2429 2466
2430sub new { 2467sub new {
2431 my $class = shift; 2468 my $class = shift;
2432 2469
2433 $class->SUPER::new ( 2470 $class->SUPER::new (
2434 padding_x => 4, 2471 padding_x => 8,
2435 padding_y => 4, 2472 padding_y => 4,
2436 fg => [1.0, 1.0, 1.0], 2473 fg => [1.0, 1.0, 1.0],
2437 active_fg => [0.8, 0.8, 0.8], 2474 active_fg => [0.8, 0.8, 0.8],
2438 can_hover => 1, 2475 can_hover => 1,
2439 align => 0, 2476 align => 0.5,
2440 valign => 0, 2477 valign => 0.5,
2441 can_events => 1, 2478 can_events => 1,
2442 @_ 2479 @_
2443 ) 2480 )
2444} 2481}
2445 2482
2470 $self->SUPER::_draw; 2507 $self->SUPER::_draw;
2471} 2508}
2472 2509
2473############################################################################# 2510#############################################################################
2474 2511
2475package CFPlus::UI::CheckBox; 2512package DC::UI::CheckBox;
2476 2513
2477our @ISA = CFPlus::UI::DrawBG::; 2514our @ISA = DC::UI::DrawBG::;
2478 2515
2479my @tex = 2516my @tex =
2480 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2517 map { new_from_resource DC::Texture $_, mipmap => 1 }
2481 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2518 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2482 2519
2483use CFPlus::OpenGL; 2520use DC::OpenGL;
2484 2521
2485sub new { 2522sub new {
2486 my $class = shift; 2523 my $class = shift;
2487 2524
2488 $class->SUPER::new ( 2525 $class->SUPER::new (
2545 glDisable GL_TEXTURE_2D; 2582 glDisable GL_TEXTURE_2D;
2546} 2583}
2547 2584
2548############################################################################# 2585#############################################################################
2549 2586
2550package CFPlus::UI::Image; 2587package DC::UI::Image;
2551 2588
2552our @ISA = CFPlus::UI::Base::; 2589our @ISA = DC::UI::Base::;
2553 2590
2554use CFPlus::OpenGL; 2591use DC::OpenGL;
2555 2592
2556our %texture_cache; 2593our %texture_cache;
2557 2594
2558sub new { 2595sub new {
2559 my $class = shift; 2596 my $class = shift;
2566 2603
2567 $self->{path} || $self->{tex} 2604 $self->{path} || $self->{tex}
2568 or Carp::croak "'path' or 'tex' attributes required"; 2605 or Carp::croak "'path' or 'tex' attributes required";
2569 2606
2570 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2607 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2571 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2608 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2572 2609
2573 CFPlus::weaken $texture_cache{$self->{path}}; 2610 DC::weaken $texture_cache{$self->{path}};
2574 2611
2575 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2612 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2576 2613
2577 $self 2614 $self
2578} 2615}
2579 2616
2580sub STORABLE_freeze { 2617sub STORABLE_freeze {
2581 my ($self, $cloning) = @_; 2618 my ($self, $cloning) = @_;
2582 2619
2583 $self->{path} 2620 $self->{path}
2584 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2621 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2585 2622
2586 $self->{path} 2623 $self->{path}
2587} 2624}
2588 2625
2589sub STORABLE_attach { 2626sub STORABLE_attach {
2620 glDisable GL_TEXTURE_2D; 2657 glDisable GL_TEXTURE_2D;
2621} 2658}
2622 2659
2623############################################################################# 2660#############################################################################
2624 2661
2625package CFPlus::UI::ImageButton; 2662package DC::UI::ImageButton;
2626 2663
2627our @ISA = CFPlus::UI::Image::; 2664our @ISA = DC::UI::Image::;
2628 2665
2629use CFPlus::OpenGL; 2666use DC::OpenGL;
2630 2667
2631my %textures; 2668my %textures;
2632 2669
2633sub new { 2670sub new {
2634 my $class = shift; 2671 my $class = shift;
2637 padding_x => 4, 2674 padding_x => 4,
2638 padding_y => 4, 2675 padding_y => 4,
2639 fg => [1, 1, 1], 2676 fg => [1, 1, 1],
2640 active_fg => [0, 0, 1], 2677 active_fg => [0, 0, 1],
2641 can_hover => 1, 2678 can_hover => 1,
2642 align => 0, 2679 align => 0.5,
2643 valign => 0, 2680 valign => 0.5,
2644 can_events => 1, 2681 can_events => 1,
2645 @_ 2682 @_
2646 ); 2683 );
2647} 2684}
2648 2685
2662 1 2699 1
2663} 2700}
2664 2701
2665############################################################################# 2702#############################################################################
2666 2703
2667package CFPlus::UI::VGauge; 2704package DC::UI::VGauge;
2668 2705
2669our @ISA = CFPlus::UI::Base::; 2706our @ISA = DC::UI::Base::;
2670 2707
2671use List::Util qw(min max); 2708use List::Util qw(min max);
2672 2709
2673use CFPlus::OpenGL; 2710use DC::OpenGL;
2674 2711
2675my %tex = ( 2712my %tex = (
2676 food => [ 2713 food => [
2677 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2714 map { new_from_resource DC::Texture $_, mipmap => 1 }
2678 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2715 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2679 ], 2716 ],
2680 grace => [ 2717 grace => [
2681 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2718 map { new_from_resource DC::Texture $_, mipmap => 1 }
2682 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2719 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2683 ], 2720 ],
2684 hp => [ 2721 hp => [
2685 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2722 map { new_from_resource DC::Texture $_, mipmap => 1 }
2686 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2723 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2687 ], 2724 ],
2688 mana => [ 2725 mana => [
2689 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2726 map { new_from_resource DC::Texture $_, mipmap => 1 }
2690 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2727 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2691 ], 2728 ],
2692); 2729);
2693 2730
2694# eg. VGauge->new (gauge => 'food'), default gauge: food 2731# eg. VGauge->new (gauge => 'food'), default gauge: food
2797 glDisable GL_TEXTURE_2D; 2834 glDisable GL_TEXTURE_2D;
2798} 2835}
2799 2836
2800############################################################################# 2837#############################################################################
2801 2838
2802package CFPlus::UI::Progress; 2839package DC::UI::Progress;
2803 2840
2804our @ISA = CFPlus::UI::Label::; 2841our @ISA = DC::UI::Label::;
2805 2842
2806use CFPlus::OpenGL; 2843use DC::OpenGL;
2807 2844
2808sub new { 2845sub new {
2809 my ($class, %arg) = @_; 2846 my ($class, %arg) = @_;
2810 2847
2811 my $self = $class->SUPER::new ( 2848 my $self = $class->SUPER::new (
2849 padding_x => 2,
2850 padding_y => 2,
2812 fg => [1, 1, 1], 2851 fg => [1, 1, 1],
2813 bg => [0, 0, 1, 0.2], 2852 bg => [0, 0, 1, 0.2],
2814 bar => [0.7, 0.5, 0.1, 0.8], 2853 bar => [0.7, 0.5, 0.1, 0.8],
2815 outline => [0.4, 0.3, 0], 2854 outline => [0.4, 0.3, 0],
2816 fontsize => 0.9, 2855 fontsize => 0.9,
2817 valign => 0, 2856 valign => 0.5,
2818 align => 0, 2857 align => 0.5,
2819 can_events => 1, 2858 can_events => 1,
2820 ellipsise => 1, 2859 ellipsise => 1,
2821 label => "%d%%", 2860 label => "%d%%",
2822 %arg, 2861 %arg,
2823 ); 2862 );
2831 my ($self, $label) = @_; 2870 my ($self, $label) = @_;
2832 2871
2833 return if $self->{label} eq $label; 2872 return if $self->{label} eq $label;
2834 $self->{label} = $label; 2873 $self->{label} = $label;
2835 2874
2836 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2875 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2837} 2876}
2838 2877
2839sub set_value { 2878sub set_value {
2840 my ($self, $value) = @_; 2879 my ($self, $value) = @_;
2841 2880
2856 my ($self) = @_; 2895 my ($self) = @_;
2857 2896
2858 glEnable GL_BLEND; 2897 glEnable GL_BLEND;
2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2898 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2860 2899
2900 my $px = $self->{padding_x};
2901 my $py = $self->{padding_y};
2902
2861 if ($self->{value} >= 0) { 2903 if ($self->{value} >= 0) {
2862 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2904 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2863 2905
2864 glColor_premultiply @{$self->{bar}}; 2906 glColor_premultiply @{$self->{bar}};
2865 glRect 2, 2, $s, $self->{h} - 2; 2907 glRect $px, $py, $s, $self->{h} - $py;
2866 glColor_premultiply @{$self->{bg}}; 2908 glColor_premultiply @{$self->{bg}};
2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2909 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2868 } 2910 }
2869 2911
2870 glColor_premultiply @{$self->{outline}}; 2912 glColor_premultiply @{$self->{outline}};
2913
2914 $px -= .5;
2915 $py -= .5;
2916
2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2917 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2872 2918
2873 glDisable GL_BLEND; 2919 glDisable GL_BLEND;
2874 2920
2875 { 2921 {
2876 local $self->{bg}; # do not draw background 2922 local $self->{bg}; # do not draw background
2878 } 2924 }
2879} 2925}
2880 2926
2881############################################################################# 2927#############################################################################
2882 2928
2883package CFPlus::UI::ExperienceProgress; 2929package DC::UI::ExperienceProgress;
2884 2930
2885our @ISA = CFPlus::UI::Progress::; 2931our @ISA = DC::UI::Progress::;
2886 2932
2887sub new { 2933sub new {
2888 my ($class, %arg) = @_; 2934 my ($class, %arg) = @_;
2889 2935
2936 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2937
2890 my $self = $class->SUPER::new ( 2938 my $self = $class->SUPER::new (
2939 %arg,
2891 tooltip => sub { 2940 tooltip => sub {
2892 my ($self) = @_; 2941 my ($self) = @_;
2893 2942
2894 sprintf "level %d\n%s points\n%s next level\n%s to go", 2943 sprintf "%slevel %d\n%s points\n%s next level\n%s to go",
2944 $tt,
2895 $self->{lvl}, 2945 $self->{lvl},
2896 ::formsep ($self->{exp}), 2946 ::formsep ($self->{exp}),
2897 ::formsep ($self->{nxt}), 2947 ::formsep ($self->{nxt}),
2898 ::formsep ($self->{nxt} - $self->{exp}), 2948 ::formsep ($self->{nxt} - $self->{exp}),
2899 }, 2949 },
2900 %arg
2901 ); 2950 );
2902 2951
2903 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2952 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2904 if $::CONN; 2953 if $::CONN;
2905 2954
2935 $self->SUPER::set_value ($v); 2984 $self->SUPER::set_value ($v);
2936} 2985}
2937 2986
2938############################################################################# 2987#############################################################################
2939 2988
2940package CFPlus::UI::Gauge; 2989package DC::UI::Gauge;
2941 2990
2942our @ISA = CFPlus::UI::VBox::; 2991our @ISA = DC::UI::VBox::;
2943 2992
2944sub new { 2993sub new {
2945 my ($class, %arg) = @_; 2994 my ($class, %arg) = @_;
2946 2995
2947 my $self = $class->SUPER::new ( 2996 my $self = $class->SUPER::new (
2949 can_hover => 1, 2998 can_hover => 1,
2950 can_events => 1, 2999 can_events => 1,
2951 %arg, 3000 %arg,
2952 ); 3001 );
2953 3002
2954 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3003 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2955 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3004 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2956 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3005 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2957 3006
2958 $self 3007 $self
2959} 3008}
2960 3009
2961sub set_fontsize { 3010sub set_fontsize {
2982 $self->{value}->set_text ($val); 3031 $self->{value}->set_text ($val);
2983} 3032}
2984 3033
2985############################################################################# 3034#############################################################################
2986 3035
2987package CFPlus::UI::Slider; 3036package DC::UI::Slider;
2988 3037
2989use strict; 3038use strict;
2990 3039
2991use CFPlus::OpenGL; 3040use DC::OpenGL;
2992 3041
2993our @ISA = CFPlus::UI::DrawBG::; 3042our @ISA = DC::UI::DrawBG::;
2994 3043
2995my @tex = 3044my @tex =
2996 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3045 map { new_from_resource DC::Texture $_ }
2997 qw(s1_slider.png s1_slider_bg.png); 3046 qw(s1_slider.png s1_slider_bg.png);
2998 3047
2999sub new { 3048sub new {
3000 my $class = shift; 3049 my $class = shift;
3001 3050
3038sub set_value { 3087sub set_value {
3039 my ($self, $value) = @_; 3088 my ($self, $value) = @_;
3040 3089
3041 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3090 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3042 3091
3043 $hi = $lo + 1 if $hi <= $lo; 3092 $hi = $lo if $hi < $lo;
3044 3093
3045 $page = $hi - $lo if $page > $hi - $lo; 3094 $value = $hi - $page if $value > $hi - $page;
3046
3047 $value = $lo if $value < $lo; 3095 $value = $lo if $value < $lo;
3048 $value = $hi - $page if $value > $hi - $page;
3049 3096
3050 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3097 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3051 if $unit; 3098 if $unit;
3052 3099
3053 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3100 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3097sub invoke_mouse_wheel { 3144sub invoke_mouse_wheel {
3098 my ($self, $ev) = @_; 3145 my ($self, $ev) = @_;
3099 3146
3100 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3147 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3101 3148
3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3149 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3103 3150
3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3151 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3105 3152
3106 1 3153 1
3107} 3154}
3117 my ($self) = @_; 3164 my ($self) = @_;
3118 3165
3119 unless ($self->{knob_w}) { 3166 unless ($self->{knob_w}) {
3120 $self->set_value ($self->{range}[0]); 3167 $self->set_value ($self->{range}[0]);
3121 3168
3122 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3169 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3123 my $range = ($hi - $page - $lo) || 1e-100; 3170 my $range = ($hi - $page - $lo) || 1e-10;
3124 3171
3125 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3172 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3126 3173
3127 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3174 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3128 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3175 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3129 3176
3130 $value = ($value - $lo) / $range; 3177 $value = ($value - $lo) / $range;
3160 glDisable GL_TEXTURE_2D; 3207 glDisable GL_TEXTURE_2D;
3161} 3208}
3162 3209
3163############################################################################# 3210#############################################################################
3164 3211
3165package CFPlus::UI::ValSlider; 3212package DC::UI::ValSlider;
3166 3213
3167our @ISA = CFPlus::UI::HBox::; 3214our @ISA = DC::UI::HBox::;
3168 3215
3169sub new { 3216sub new {
3170 my ($class, %arg) = @_; 3217 my ($class, %arg) = @_;
3171 3218
3172 my $range = delete $arg{range}; 3219 my $range = delete $arg{range};
3173 3220
3174 my $self = $class->SUPER::new ( 3221 my $self = $class->SUPER::new (
3175 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3222 slider => (new DC::UI::Slider expand => 1, range => $range),
3176 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3223 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3177 to_value => sub { shift }, 3224 to_value => sub { shift },
3178 from_value => sub { shift }, 3225 from_value => sub { shift },
3179 %arg, 3226 %arg,
3180 ); 3227 );
3181 3228
3201sub set_range { shift->{slider}->set_range (@_) } 3248sub set_range { shift->{slider}->set_range (@_) }
3202sub set_value { shift->{slider}->set_value (@_) } 3249sub set_value { shift->{slider}->set_value (@_) }
3203 3250
3204############################################################################# 3251#############################################################################
3205 3252
3206package CFPlus::UI::TextScroller; 3253package DC::UI::TextScroller;
3207 3254
3208our @ISA = CFPlus::UI::HBox::; 3255our @ISA = DC::UI::HBox::;
3209 3256
3210use CFPlus::OpenGL; 3257use DC::OpenGL;
3211 3258
3212sub new { 3259sub new {
3213 my $class = shift; 3260 my $class = shift;
3214 3261
3215 my $self = $class->SUPER::new ( 3262 my $self = $class->SUPER::new (
3217 can_events => 1, 3264 can_events => 1,
3218 indent => 0, 3265 indent => 0,
3219 #font => default_font 3266 #font => default_font
3220 @_, 3267 @_,
3221 3268
3222 layout => (new CFPlus::Layout), 3269 layout => (new DC::Layout),
3223 par => [], 3270 par => [],
3224 max_par => 0, 3271 max_par => 0,
3225 height => 0, 3272 height => 0,
3226 children => [ 3273 children => [
3227 (new CFPlus::UI::Empty expand => 1), 3274 (new DC::UI::Empty expand => 1),
3228 (new CFPlus::UI::Slider vertical => 1), 3275 (new DC::UI::Slider vertical => 1),
3229 ], 3276 ],
3230 ); 3277 );
3231 3278
3232 $self->{children}[1]->connect (changed => sub { $self->update }); 3279 $self->{children}[1]->connect (changed => sub { $self->update });
3233 3280
3289 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3336 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3290 $layout->set_markup ($para->{markup}); 3337 $layout->set_markup ($para->{markup});
3291 3338
3292 $layout->set_shapes ( 3339 $layout->set_shapes (
3293 map 3340 map
3294 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3341 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3295 @{$para->{widget}} 3342 @{$para->{widget}}
3296 ); 3343 );
3297 3344
3298 $layout 3345 $layout
3299} 3346}
3416 $ROOT->on_post_alloc ($self => sub { 3463 $ROOT->on_post_alloc ($self => sub {
3417 $self->force_uptodate; 3464 $self->force_uptodate;
3418 3465
3419 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3466 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3420 3467
3421 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3468 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3422 glClearColor 0, 0, 0, 0; 3469 glClearColor 0, 0, 0, 0;
3423 glClear GL_COLOR_BUFFER_BIT; 3470 glClear GL_COLOR_BUFFER_BIT;
3424 3471
3425 package CFPlus::UI::Base; 3472 package DC::UI::Base;
3426 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3473 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3427 (0, 0, $self->{w}, $self->{h}); 3474 (0, 0, $self->{w}, $self->{h});
3428 3475
3429 my $top = int $self->{children}[1]{range}[0]; 3476 my $top = int $self->{children}[1]{range}[0];
3430 3477
3489 $self->{children}[1]->draw; 3536 $self->{children}[1]->draw;
3490} 3537}
3491 3538
3492############################################################################# 3539#############################################################################
3493 3540
3494package CFPlus::UI::Animator; 3541package DC::UI::Animator;
3495 3542
3496use CFPlus::OpenGL; 3543use DC::OpenGL;
3497 3544
3498our @ISA = CFPlus::UI::Bin::; 3545our @ISA = DC::UI::Bin::;
3499 3546
3500sub moveto { 3547sub moveto {
3501 my ($self, $x, $y) = @_; 3548 my ($self, $x, $y) = @_;
3502 3549
3503 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3550 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3531 glPopMatrix; 3578 glPopMatrix;
3532} 3579}
3533 3580
3534############################################################################# 3581#############################################################################
3535 3582
3536package CFPlus::UI::Flopper; 3583package DC::UI::Flopper;
3537 3584
3538our @ISA = CFPlus::UI::Button::; 3585our @ISA = DC::UI::Button::;
3539 3586
3540sub new { 3587sub new {
3541 my $class = shift; 3588 my $class = shift;
3542 3589
3543 my $self = $class->SUPER::new ( 3590 my $self = $class->SUPER::new (
3555 $self->{other}->toggle_visibility; 3602 $self->{other}->toggle_visibility;
3556} 3603}
3557 3604
3558############################################################################# 3605#############################################################################
3559 3606
3560package CFPlus::UI::Tooltip; 3607package DC::UI::Tooltip;
3561 3608
3562our @ISA = CFPlus::UI::Bin::; 3609our @ISA = DC::UI::Bin::;
3563 3610
3564use CFPlus::OpenGL; 3611use DC::OpenGL;
3565 3612
3566sub new { 3613sub new {
3567 my $class = shift; 3614 my $class = shift;
3568 3615
3569 $class->SUPER::new ( 3616 $class->SUPER::new (
3576 my ($self, $widget) = @_; 3623 my ($self, $widget) = @_;
3577 3624
3578 my $tip = $widget->{tooltip}; 3625 my $tip = $widget->{tooltip};
3579 $tip = $tip->($widget) if "CODE" eq ref $tip; 3626 $tip = $tip->($widget) if "CODE" eq ref $tip;
3580 3627
3581 $tip = CFPlus::Pod::section_label tooltip => $1 3628 $tip = DC::Pod::section_label tooltip => $1
3582 if $tip =~ /^#(.*)$/; 3629 if $tip =~ /^#(.*)$/;
3583 3630
3584 if ($ENV{CFPLUS_DEBUG} & 2) { 3631 if ($ENV{CFPLUS_DEBUG} & 2) {
3585 $tip .= "\n\n" . (ref $widget) . "\n" 3632 $tip .= "\n\n" . (ref $widget) . "\n"
3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3633 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3589 } 3636 }
3590 3637
3591 $tip =~ s/^\n+//; 3638 $tip =~ s/^\n+//;
3592 $tip =~ s/\n+$//; 3639 $tip =~ s/\n+$//;
3593 3640
3594 $self->add (new CFPlus::UI::Label 3641 $self->add (new DC::UI::Label
3642 fg => $DC::THEME{tooltip_fg},
3595 markup => $tip, 3643 markup => $tip,
3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3644 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3645 align => 0,
3597 fontsize => 0.8, 3646 fontsize => 0.8,
3598 style => 1, # FLAG_INVERSE 3647 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3599 ellipsise => 0, 3648 ellipsise => 0,
3600 font => ($widget->{tooltip_font} || $::FONT_PROP), 3649 font => ($widget->{tooltip_font} || $::FONT_PROP),
3601 ); 3650 );
3602} 3651}
3603 3652
3640sub _draw { 3689sub _draw {
3641 my ($self) = @_; 3690 my ($self) = @_;
3642 3691
3643 my ($w, $h) = @$self{qw(w h)}; 3692 my ($w, $h) = @$self{qw(w h)};
3644 3693
3645 glColor 1, 0.8, 0.4; 3694 glColor @{ $DC::THEME{tooltip_bg} };
3646 glRect 0, 0, $w, $h; 3695 glRect 0, 0, $w, $h;
3647 3696
3648 glColor 0, 0, 0; 3697 glColor @{ $DC::THEME{tooltip_border} };
3649 glRect_lineloop .5, .5, $w + .5, $h + .5; 3698 glRect_lineloop .5, .5, $w + .5, $h + .5;
3650 3699
3651 glTranslate 2, 2; 3700 glTranslate 2, 2;
3652 3701
3653 $self->SUPER::_draw; 3702 $self->SUPER::_draw;
3654} 3703}
3655 3704
3656############################################################################# 3705#############################################################################
3657 3706
3658package CFPlus::UI::Face; 3707package DC::UI::Face;
3659 3708
3660our @ISA = CFPlus::UI::DrawBG::; 3709our @ISA = DC::UI::DrawBG::;
3661 3710
3662use CFPlus::OpenGL; 3711use DC::OpenGL;
3663 3712
3664sub new { 3713sub new {
3665 my $class = shift; 3714 my $class = shift;
3666 3715
3667 my $self = $class->SUPER::new ( 3716 my $self = $class->SUPER::new (
3671 can_events => 0, 3720 can_events => 0,
3672 @_, 3721 @_,
3673 ); 3722 );
3674 3723
3675 if ($self->{anim} && $self->{animspeed}) { 3724 if ($self->{anim} && $self->{animspeed}) {
3676 CFPlus::weaken (my $widget = $self); 3725 DC::weaken (my $widget = $self);
3677 3726
3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3727 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed}; 3728 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3681 return unless $::CONN; 3729 return unless $::CONN;
3682 3730
3683 my $w = $widget 3731 my $w = $widget
3684 or return; 3732 or return;
3685 3733
3701sub update_timer { 3749sub update_timer {
3702 my ($self) = @_; 3750 my ($self) = @_;
3703 3751
3704 return unless $self->{timer}; 3752 return unless $self->{timer};
3705 3753
3706 $self->{timer}->stop;
3707 if ($self->{visible}) { 3754 if ($self->{visible}) {
3708 $self->{timer}->set (
3709 $self->{anim_start}
3710 + $self->{animspeed}
3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3712 );
3713 $self->{timer}->start; 3755 $self->{timer}->start;
3756 } else {
3757 $self->{timer}->stop;
3714 } 3758 }
3715} 3759}
3716 3760
3717sub update_face { 3761sub update_face {
3718 my ($self) = @_; 3762 my ($self) = @_;
3792 $self->SUPER::destroy; 3836 $self->SUPER::destroy;
3793} 3837}
3794 3838
3795############################################################################# 3839#############################################################################
3796 3840
3797package CFPlus::UI::Buttonbar; 3841package DC::UI::Buttonbar;
3798 3842
3799our @ISA = CFPlus::UI::HBox::; 3843our @ISA = DC::UI::HBox::;
3800 3844
3801# TODO: should actually wrap buttons and other goodies. 3845# TODO: should actually wrap buttons and other goodies.
3802 3846
3803############################################################################# 3847#############################################################################
3804 3848
3805package CFPlus::UI::Menu; 3849package DC::UI::Menu;
3806 3850
3807our @ISA = CFPlus::UI::Toplevel::; 3851our @ISA = DC::UI::Toplevel::;
3808 3852
3809use CFPlus::OpenGL; 3853use DC::OpenGL;
3810 3854
3811sub new { 3855sub new {
3812 my $class = shift; 3856 my $class = shift;
3813 3857
3814 my $self = $class->SUPER::new ( 3858 my $self = $class->SUPER::new (
3815 items => [], 3859 items => [],
3816 z => 100, 3860 z => 100,
3817 @_, 3861 @_,
3818 ); 3862 );
3819 3863
3820 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3864 $self->add ($self->{vbox} = new DC::UI::VBox);
3821 3865
3822 for my $item (@{ $self->{items} }) { 3866 for my $item (@{ $self->{items} }) {
3823 my ($widget, $cb, $tooltip) = @$item; 3867 my ($widget, $cb, $tooltip) = @$item;
3824 3868
3825 # handle various types of items, only text for now 3869 # handle various types of items, only text for now
3826 if (!ref $widget) { 3870 if (!ref $widget) {
3827 if ($widget =~ /\t/) { 3871 if ($widget =~ /\t/) {
3828 my ($left, $right) = split /\t/, $widget, 2; 3872 my ($left, $right) = split /\t/, $widget, 2;
3829 3873
3830 $widget = new CFPlus::UI::HBox 3874 $widget = new DC::UI::HBox
3831 can_hover => 1, 3875 can_hover => 1,
3832 can_events => 1, 3876 can_events => 1,
3833 tooltip => $tooltip, 3877 tooltip => $tooltip,
3834 children => [ 3878 children => [
3835 (new CFPlus::UI::Label markup => $left, expand => 1), 3879 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3836 (new CFPlus::UI::Label markup => $right, align => +1), 3880 (new DC::UI::Label markup => $right, align => 1),
3837 ], 3881 ],
3838 ; 3882 ;
3839 3883
3840 } else { 3884 } else {
3841 $widget = new CFPlus::UI::Label 3885 $widget = new DC::UI::Label
3842 can_hover => 1, 3886 can_hover => 1,
3843 can_events => 1, 3887 can_events => 1,
3888 align => 0,
3844 markup => $widget, 3889 markup => $widget,
3845 tooltip => $tooltip; 3890 tooltip => $tooltip;
3846 } 3891 }
3847 } 3892 }
3848 3893
3864 $GRAB = $self; 3909 $GRAB = $self;
3865 $self->{button} = $ev->{button}; 3910 $self->{button} = $ev->{button};
3866 3911
3867 $self->show; 3912 $self->show;
3868 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3913 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
3914
3915 1 # so it can be used inside event handlers
3869} 3916}
3870 3917
3871sub invoke_mouse_motion { 3918sub invoke_mouse_motion {
3872 my ($self, $ev, $x, $y) = @_; 3919 my ($self, $ev, $x, $y) = @_;
3873 3920
3894 1 3941 1
3895} 3942}
3896 3943
3897############################################################################# 3944#############################################################################
3898 3945
3899package CFPlus::UI::Multiplexer; 3946package DC::UI::Multiplexer;
3900 3947
3901our @ISA = CFPlus::UI::Container::; 3948our @ISA = DC::UI::Container::;
3902 3949
3903sub new { 3950sub new {
3904 my $class = shift; 3951 my $class = shift;
3905 3952
3906 my $self = $class->SUPER::new ( 3953 my $self = $class->SUPER::new (
3907 @_, 3954 @_,
3908 ); 3955 );
3909 3956
3910 $self->{current} = $self->{children}[0] 3957 $self->set_current_page (0);
3911 if @{ $self->{children} };
3912 3958
3913 $self 3959 $self
3914} 3960}
3915 3961
3916sub add { 3962sub add {
3917 my ($self, @widgets) = @_; 3963 my ($self, @widgets) = @_;
3918 3964
3919 $self->SUPER::add (@widgets); 3965 $self->SUPER::add (@widgets);
3920 3966
3921 $self->{current} = $self->{children}[0] 3967 $self->set_current_page (0)
3922 if @{ $self->{children} }; 3968 if @widgets == @{ $self->{children} };
3923} 3969}
3924 3970
3925sub get_current_page { 3971sub get_current_page {
3926 my ($self) = @_; 3972 my ($self) = @_;
3927 3973
3933 3979
3934 my $widget = ref $page_or_widget 3980 my $widget = ref $page_or_widget
3935 ? $page_or_widget 3981 ? $page_or_widget
3936 : $self->{children}[$page_or_widget]; 3982 : $self->{children}[$page_or_widget];
3937 3983
3984 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3985
3938 $self->{current} = $widget; 3986 if (($self->{current} = $widget)) {
3987 $self->{current}->set_visible if $self->{current} && $self->{visible};
3939 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3988 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3940 3989
3941 $self->emit (page_changed => $self->{current}); 3990 $self->emit (page_changed => $self->{current});
3991 }
3942 3992
3943 $self->realloc; 3993 $self->realloc;
3944} 3994}
3945 3995
3946sub visible_children { 3996sub visible_children {
3947 $_[0]{current} 3997 $_[0]{current} || ()
3948} 3998}
3949 3999
3950sub size_request { 4000sub size_request {
3951 my ($self) = @_; 4001 my ($self) = @_;
3952 4002
4003 $self->{current}
3953 $self->{current}->size_request 4004 ? $self->{current}->size_request
4005 : (0, 0)
3954} 4006}
3955 4007
3956sub invoke_size_allocate { 4008sub invoke_size_allocate {
3957 my ($self, $w, $h) = @_; 4009 my ($self, $w, $h) = @_;
3958 4010
3959 $self->{current}->configure (0, 0, $w, $h); 4011 $self->{current}->configure (0, 0, $w, $h)
4012 if $self->{current};
3960 4013
3961 1 4014 1
3962} 4015}
3963 4016
3964sub _draw { 4017sub _draw {
3965 my ($self) = @_; 4018 my ($self) = @_;
3966 4019
3967 $self->{current}->draw; 4020 $self->{current}->draw
4021 if $self->{current};
3968} 4022}
3969 4023
3970############################################################################# 4024#############################################################################
3971 4025
3972package CFPlus::UI::Notebook; 4026package DC::UI::Notebook;
3973 4027
3974use CFPlus::OpenGL; 4028use DC::OpenGL;
3975 4029
3976our @ISA = CFPlus::UI::VBox::; 4030our @ISA = DC::UI::VBox::;
3977 4031
3978sub new { 4032sub new {
3979 my $class = shift; 4033 my $class = shift;
3980 4034
3981 my $self = $class->SUPER::new ( 4035 my $self = $class->SUPER::new (
3982 buttonbar => (new CFPlus::UI::Buttonbar), 4036 buttonbar => (new DC::UI::Buttonbar),
3983 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4037 multiplexer => (new DC::UI::Multiplexer expand => 1),
3984 active_outline => [.7, .7, 0.2], 4038 active_outline => [.7, .7, 0.2],
3985 # filter => # will be put between multiplexer and $self 4039 # filter => # will be put between multiplexer and $self
3986 @_, 4040 @_,
3987 ); 4041 );
3988 4042
3997 4051
3998 for my $child (@$widgets) { 4052 for my $child (@$widgets) {
3999 Scalar::Util::weaken $child; 4053 Scalar::Util::weaken $child;
4000 $child->{c_tab_} ||= do { 4054 $child->{c_tab_} ||= do {
4001 my $tab = 4055 my $tab =
4002 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4056 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4003 ? $child->{c_tab} 4057 ? $child->{c_tab}
4004 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4058 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4005 4059
4006 $tab->connect (activate => sub { 4060 $tab->connect (activate => sub {
4007 $wself->set_current_page ($child); 4061 $wself->set_current_page ($child);
4008 }); 4062 });
4009 4063
4039} 4093}
4040 4094
4041sub pages { 4095sub pages {
4042 my ($self) = @_; 4096 my ($self) = @_;
4043 $self->{multiplexer}->children 4097 $self->{multiplexer}->children
4098}
4099
4100sub page_index {
4101 my ($self, $widget) = @_;
4102
4103 my $i = 0;
4104 for ($self->pages) {
4105 if ($_ eq $widget) { return $i };
4106 $i++;
4107 }
4108
4109 undef
4044} 4110}
4045 4111
4046sub add_tab { 4112sub add_tab {
4047 my ($self, $title, $widget, $tooltip) = @_; 4113 my ($self, $title, $widget, $tooltip) = @_;
4048 4114
4085 } 4151 }
4086} 4152}
4087 4153
4088############################################################################# 4154#############################################################################
4089 4155
4090package CFPlus::UI::Selector; 4156package DC::UI::Selector;
4091 4157
4092use utf8; 4158use utf8;
4093 4159
4094our @ISA = CFPlus::UI::Button::; 4160our @ISA = DC::UI::Button::;
4095 4161
4096sub new { 4162sub new {
4097 my $class = shift; 4163 my $class = shift;
4098 4164
4099 my $self = $class->SUPER::new ( 4165 my $self = $class->SUPER::new (
4116 my ($value, $title, $tooltip) = @$_; 4182 my ($value, $title, $tooltip) = @$_;
4117 4183
4118 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4184 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4119 } 4185 }
4120 4186
4121 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4187 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4122} 4188}
4123 4189
4124sub _set_value { 4190sub _set_value {
4125 my ($self, $value) = @_; 4191 my ($self, $value) = @_;
4126 4192
4149 $self->_set_value ($self->{value}); 4215 $self->_set_value ($self->{value});
4150} 4216}
4151 4217
4152############################################################################# 4218#############################################################################
4153 4219
4154package CFPlus::UI::Statusbox; 4220package DC::UI::Statusbox;
4155 4221
4156our @ISA = CFPlus::UI::VBox::; 4222our @ISA = DC::UI::VBox::;
4157 4223
4158sub new { 4224sub new {
4159 my $class = shift; 4225 my $class = shift;
4160 4226
4161 my $self = $class->SUPER::new ( 4227 my $self = $class->SUPER::new (
4162 fontsize => 0.8, 4228 fontsize => 0.8,
4163 @_, 4229 @_,
4164 ); 4230 );
4165 4231
4166 CFPlus::weaken (my $this = $self); 4232 DC::weaken (my $this = $self);
4167 4233
4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4234 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4169 4235
4170 $self 4236 $self
4171} 4237}
4172 4238
4173sub reorder { 4239sub reorder {
4174 my ($self) = @_; 4240 my ($self) = @_;
4175 my $NOW = Time::HiRes::time; 4241 my $NOW = EV::time;
4176 4242
4177 # freeze display when hovering over any label 4243 # freeze display when hovering over any label
4178 return if $CFPlus::UI::TOOLTIP->{owner} 4244 return if $DC::UI::TOOLTIP->{owner}
4179 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4245 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4180 values %{ $self->{item} }; 4246 values %{ $self->{item} };
4181 4247
4182 while (my ($k, $v) = each %{ $self->{item} }) { 4248 while (my ($k, $v) = each %{ $self->{item} }) {
4183 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4249 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4184 } 4250 }
4251
4252 $self->{timer}->set (1, 1);
4185 4253
4186 my @widgets; 4254 my @widgets;
4187 4255
4188 my @items = sort { 4256 my @items = sort {
4189 $a->{pri} <=> $b->{pri} 4257 $a->{pri} <=> $b->{pri}
4203 for ($short) { 4271 for ($short) {
4204 s/^\s+//; 4272 s/^\s+//;
4205 s/\s+/ /g; 4273 s/\s+/ /g;
4206 } 4274 }
4207 4275
4208 new CFPlus::UI::Label 4276 new DC::UI::Label
4209 markup => $short, 4277 markup => $short,
4210 tooltip => $item->{tooltip}, 4278 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4279 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4280 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4281 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4282 max_w => $::WIDTH * 0.44,
4283 align => 0,
4215 fg => [@{ $item->{fg} }], 4284 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4285 can_events => 1,
4217 can_hover => 1 4286 can_hover => 1
4218 }; 4287 };
4219 4288
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4296 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4297 }
4229 4298
4230 push @widgets, $label; 4299 push @widgets, $label;
4231 } 4300 }
4301
4302 my $hash = join ",", @widgets;
4303 return if $hash eq $self->{last_widget_hash};
4304 $self->{last_widget_hash} = $hash;
4232 4305
4233 $self->clear; 4306 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4307 $self->SUPER::add (reverse @widgets);
4235} 4308}
4236 4309
4292 $self->SUPER::destroy; 4365 $self->SUPER::destroy;
4293} 4366}
4294 4367
4295############################################################################# 4368#############################################################################
4296 4369
4297package CFPlus::UI::Root; 4370package DC::UI::Root;
4298 4371
4299our @ISA = CFPlus::UI::Container::; 4372our @ISA = DC::UI::Container::;
4300 4373
4301use List::Util qw(min max); 4374use List::Util qw(min max);
4302 4375
4303use CFPlus::OpenGL; 4376use DC::OpenGL;
4304 4377
4305sub new { 4378sub new {
4306 my $class = shift; 4379 my $class = shift;
4307 4380
4308 my $self = $class->SUPER::new ( 4381 my $self = $class->SUPER::new (
4309 visible => 1, 4382 visible => 1,
4310 @_, 4383 @_,
4311 ); 4384 );
4312 4385
4313 CFPlus::weaken ($self->{root} = $self); 4386 DC::weaken ($self->{root} = $self);
4314 4387
4315 $self 4388 $self
4316} 4389}
4317 4390
4318sub size_request { 4391sub size_request {
4366} 4439}
4367 4440
4368sub update { 4441sub update {
4369 my ($self) = @_; 4442 my ($self) = @_;
4370 4443
4371 $::WANT_REFRESH->start; 4444 $::WANT_REFRESH = 1;
4372} 4445}
4373 4446
4374sub add { 4447sub add {
4375 my ($self, @children) = @_; 4448 my ($self, @children) = @_;
4376 4449
4443 4516
4444 delete $queue{$widget+0}; 4517 delete $queue{$widget+0};
4445 4518
4446 my ($w, $h) = $widget->size_request; 4519 my ($w, $h) = $widget->size_request;
4447 4520
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4521 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4522 $h += $widget->{padding_y} * 2;
4523
4524 $w = max $widget->{min_w}, $w;
4525 $h = max $widget->{min_h}, $h;
4450 4526
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4527 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4528 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4529
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4530 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4597 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4598 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4599 glLoadIdentity;
4524 4600
4525 { 4601 {
4526 package CFPlus::UI::Base; 4602 package DC::UI::Base;
4527 4603
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4604 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4605 (0, 0, $self->{w}, $self->{h});
4530 4606
4531 $self->_draw; 4607 $self->_draw;
4532 } 4608 }
4533} 4609}
4534 4610
4535############################################################################# 4611#############################################################################
4536 4612
4537package CFPlus::UI; 4613package DC::UI;
4538 4614
4539$ROOT = new CFPlus::UI::Root; 4615$ROOT = new DC::UI::Root;
4540$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4616$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4617
45421 46181
4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines