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.442 by root, Mon Oct 8 17:01:03 2007 UTC vs.
Revision 1.468 by root, Wed Sep 3 10:36:25 2008 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 ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 43 }
43 44
44 $TOOLTIP->hide; 45 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
47}); 48};
48 49
49sub get_layout { 50sub get_layout {
50 my $layout; 51 my $layout;
51 52
52 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
88 89
89 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 92
92 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
93 } 94 }
94} 95}
95 96
96sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
97 my ($ev) = @_; 98 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
104 105
105 $GRAB = $widget; 106 $GRAB = $widget;
106 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
107 108
108 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
109 } 110 }
110 111
111 if ($GRAB) { 112 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 114 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 117
117 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
119 120
120 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 140 $grab->update if $grab;
140 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
141 142
142 check_hover $widget; 143 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
144 } 145 }
145} 146}
146 147
147sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
148 my ($ev) = @_; 149 my ($ev) = @_;
210 reconfigure_widgets; 211 reconfigure_widgets;
211} 212}
212 213
213############################################################################# 214#############################################################################
214 215
215package CFPlus::UI::Event; 216package DC::UI::Event;
216 217
217sub xy { 218sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 220}
220 221
221############################################################################# 222#############################################################################
222 223
223package CFPlus::UI::Base; 224package DC::UI::Base;
224 225
225use strict; 226use strict;
226 227
227use CFPlus::OpenGL; 228use DC::OpenGL;
228 229
229sub new { 230sub new {
230 my $class = shift; 231 my $class = shift;
231 232
232 my $self = bless { 233 my $self = bless {
237 h => undef, 238 h => undef,
238 can_events => 1, 239 can_events => 1,
239 @_ 240 @_
240 }, $class; 241 }, $class;
241 242
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 244
244 for (keys %$self) { 245 for (keys %$self) {
245 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
247 } 248 }
248 } 249 }
249 250
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $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};
252 $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};
253 $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};
254 $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};
255 256
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 259
259 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
277sub show { 278sub show {
278 my ($self) = @_; 279 my ($self) = @_;
279 280
280 return if $self->{parent}; 281 return if $self->{parent};
281 282
282 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
283} 284}
284 285
285sub set_visible { 286sub set_visible {
286 my ($self) = @_; 287 my ($self) = @_;
287 288
308 delete $self->{root}; 309 delete $self->{root};
309 310
310 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
312 313
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
315 316
316 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
318} 319}
337sub hide { 338sub hide {
338 my ($self) = @_; 339 my ($self) = @_;
339 340
340 $self->set_invisible; 341 $self->set_invisible;
341 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 346 $parent->remove ($self);
343 if $self->{parent}; 347 }
344} 348}
345 349
346sub move_abs { 350sub move_abs {
347 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
348 352
364 368
365# traverse the widget chain up to find the maximum "physical" size constraints 369# traverse the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh { 370sub get_max_wh {
367 my ($self) = @_; 371 my ($self) = @_;
368 372
373 my ($w, $h) = @$self{qw(max_w max_h)};
374
375 if ($w <= 0 || $h <= 0) {
376 my ($mw, $mh) = $self->{parent}
369 return $self->{parent}->get_max_wh 377 ? $self->{parent}->get_max_wh
370 if $self->{parent};
371
372 ($::WIDTH, $::HEIGHT) 378 : ($::WIDTH, $::HEIGHT);
379
380 $w = $mw if $w <= 0;
381 $h = $mh if $h <= 0;
382 }
383
384 ($w, $h)
373} 385}
374 386
375sub size_request { 387sub size_request {
376 require Carp; 388 require Carp;
377 Carp::confess "size_request is abstract"; 389 Carp::confess "size_request is abstract";
385 my ($self, $x, $y, $w, $h) = @_; 397 my ($self, $x, $y, $w, $h) = @_;
386 398
387 if ($self->{aspect}) { 399 if ($self->{aspect}) {
388 my ($ow, $oh) = ($w, $h); 400 my ($ow, $oh) = ($w, $h);
389 401
390 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 402 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
391 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 403 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
392 404
393 # use alignment to adjust x, y 405 # use alignment to adjust x, y
394 406
395 $x += int 0.5 * ($ow - $w); 407 $x += int 0.5 * ($ow - $w);
396 $y += int 0.5 * ($oh - $h); 408 $y += int 0.5 * ($oh - $h);
437 449
438 return if $self->{tooltip} eq $tooltip; 450 return if $self->{tooltip} eq $tooltip;
439 451
440 $self->{tooltip} = $tooltip; 452 $self->{tooltip} = $tooltip;
441 453
442 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 454 if ($DC::UI::TOOLTIP->{owner} == $self) {
443 delete $CFPlus::UI::TOOLTIP->{owner}; 455 delete $DC::UI::TOOLTIP->{owner};
444 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 456 $DC::UI::TOOLTIP_WATCHER->invoke;
445 } 457 }
446} 458}
447 459
448# translate global coordinates to local coordinate system 460# translate global coordinates to local coordinate system
449sub coord2local { 461sub coord2local {
450 my ($self, $x, $y) = @_; 462 my ($self, $x, $y) = @_;
451 463
452 Carp::confess unless $self->{parent};#d# 464 return (undef, undef) unless $self->{parent};
453 465
454 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 466 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
455} 467}
456 468
457# translate local coordinates to global coordinate system 469# translate local coordinates to global coordinate system
458sub coord2global { 470sub coord2global {
459 my ($self, $x, $y) = @_; 471 my ($self, $x, $y) = @_;
460 472
461 Carp::confess unless $self->{parent};#d# 473 return (undef, undef) unless $self->{parent};
462 474
463 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 475 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
464} 476}
465 477
466sub invoke_focus_in { 478sub invoke_focus_in {
515sub connect { 527sub connect {
516 my ($self, $signal, $cb) = @_; 528 my ($self, $signal, $cb) = @_;
517 529
518 push @{ $self->{signal_cb}{$signal} }, $cb; 530 push @{ $self->{signal_cb}{$signal} }, $cb;
519 531
520 defined wantarray and CFPlus::guard { 532 defined wantarray and DC::guard {
521 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 533 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
522 @{ $self->{signal_cb}{$signal} }; 534 @{ $self->{signal_cb}{$signal} };
523 } 535 }
524} 536}
525 537
563# in .xs 575# in .xs
564 576
565sub set_parent { 577sub set_parent {
566 my ($self, $parent) = @_; 578 my ($self, $parent) = @_;
567 579
568 CFPlus::weaken ($self->{parent} = $parent); 580 DC::weaken ($self->{parent} = $parent);
569 $self->set_visible if $parent->{visible}; 581 $self->set_visible if $parent->{visible};
570} 582}
571 583
572sub realloc { 584sub realloc {
573 my ($self) = @_; 585 my ($self) = @_;
611} 623}
612 624
613sub DESTROY { 625sub DESTROY {
614 my ($self) = @_; 626 my ($self) = @_;
615 627
616 return if CFPlus::in_destruct; 628 return if DC::in_destruct;
617 629
618 local $@; 630 local $@;
619 eval { $self->destroy }; 631 eval { $self->destroy };
620 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 632 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
621 633
622 delete $WIDGET{$self+0}; 634 delete $WIDGET{$self+0};
623} 635}
624 636
625############################################################################# 637#############################################################################
626 638
627package CFPlus::UI::DrawBG; 639package DC::UI::DrawBG;
628 640
629our @ISA = CFPlus::UI::Base::; 641our @ISA = DC::UI::Base::;
630 642
631use strict; 643use strict;
632use CFPlus::OpenGL; 644use DC::OpenGL;
633 645
634sub new { 646sub new {
635 my $class = shift; 647 my $class = shift;
636 648
637 $class->SUPER::new ( 649 $class->SUPER::new (
659 } 671 }
660} 672}
661 673
662############################################################################# 674#############################################################################
663 675
664package CFPlus::UI::Empty; 676package DC::UI::Empty;
665 677
666our @ISA = CFPlus::UI::Base::; 678our @ISA = DC::UI::Base::;
667 679
668sub new { 680sub new {
669 my ($class, %arg) = @_; 681 my ($class, %arg) = @_;
670 $class->SUPER::new (can_events => 0, %arg); 682 $class->SUPER::new (can_events => 0, %arg);
671} 683}
678 690
679sub draw { } 691sub draw { }
680 692
681############################################################################# 693#############################################################################
682 694
683package CFPlus::UI::Container; 695package DC::UI::Container;
684 696
685our @ISA = CFPlus::UI::Base::; 697our @ISA = DC::UI::Base::;
686 698
687sub new { 699sub new {
688 my ($class, %arg) = @_; 700 my ($class, %arg) = @_;
689 701
690 my $children = delete $arg{children}; 702 my $children = delete $arg{children};
784 $_->draw for $self->visible_children; 796 $_->draw for $self->visible_children;
785} 797}
786 798
787############################################################################# 799#############################################################################
788 800
789package CFPlus::UI::Bin; 801package DC::UI::Bin;
790 802
791our @ISA = CFPlus::UI::Container::; 803our @ISA = DC::UI::Container::;
792 804
793sub new { 805sub new {
794 my ($class, %arg) = @_; 806 my ($class, %arg) = @_;
795 807
796 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 808 my $child = (delete $arg{child}) || new DC::UI::Empty::;
797 809
798 $class->SUPER::new (children => [$child], %arg) 810 $class->SUPER::new (children => [$child], %arg)
799} 811}
800 812
801sub add { 813sub add {
808sub remove { 820sub remove {
809 my ($self, $widget) = @_; 821 my ($self, $widget) = @_;
810 822
811 $self->SUPER::remove ($widget); 823 $self->SUPER::remove ($widget);
812 824
813 $self->{children} = [new CFPlus::UI::Empty] 825 $self->{children} = [new DC::UI::Empty]
814 unless @{$self->{children}}; 826 unless @{$self->{children}};
815} 827}
816 828
817sub child { $_[0]->{children}[0] } 829sub child { $_[0]->{children}[0] }
818 830
829} 841}
830 842
831############################################################################# 843#############################################################################
832# back-buffered drawing area 844# back-buffered drawing area
833 845
834package CFPlus::UI::Window; 846package DC::UI::Window;
835 847
836our @ISA = CFPlus::UI::Bin::; 848our @ISA = DC::UI::Bin::;
837 849
838use CFPlus::OpenGL; 850use DC::OpenGL;
839 851
840sub new { 852sub new {
841 my ($class, %arg) = @_; 853 my ($class, %arg) = @_;
842 854
843 my $self = $class->SUPER::new (%arg); 855 my $self = $class->SUPER::new (%arg);
865} 877}
866 878
867sub render_child { 879sub render_child {
868 my ($self) = @_; 880 my ($self) = @_;
869 881
870 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 882 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
871 glClearColor 0, 0, 0, 0; 883 glClearColor 0, 0, 0, 0;
872 glClear GL_COLOR_BUFFER_BIT; 884 glClear GL_COLOR_BUFFER_BIT;
873 885
874 { 886 {
875 package CFPlus::UI::Base; 887 package DC::UI::Base;
876 888
877 local ($draw_x, $draw_y, $draw_w, $draw_h) = 889 local ($draw_x, $draw_y, $draw_w, $draw_h) =
878 (0, 0, $self->{w}, $self->{h}); 890 (0, 0, $self->{w}, $self->{h});
879 891
880 $self->_render; 892 $self->_render;
897 glDisable GL_TEXTURE_2D; 909 glDisable GL_TEXTURE_2D;
898} 910}
899 911
900############################################################################# 912#############################################################################
901 913
902package CFPlus::UI::ViewPort; 914package DC::UI::ViewPort;
903 915
904use List::Util qw(min max); 916use List::Util qw(min max);
905 917
906our @ISA = CFPlus::UI::Window::; 918our @ISA = DC::UI::Window::;
907 919
908sub new { 920sub new {
909 my $class = shift; 921 my $class = shift;
910 922
911 $class->SUPER::new ( 923 $class->SUPER::new (
995 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1007 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
996 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1008 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
997 ) { 1009 ) {
998 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1010 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
999 } else { 1011 } else {
1000 $self->CFPlus::UI::Base::find_widget ($x, $y) 1012 $self->DC::UI::Base::find_widget ($x, $y)
1001 } 1013 }
1002} 1014}
1003 1015
1004sub _render { 1016sub _render {
1005 my ($self) = @_; 1017 my ($self) = @_;
1006 1018
1007 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1019 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1008 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1020 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1009 1021
1010 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1022 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1011 1023
1012 $self->SUPER::_render; 1024 $self->SUPER::_render;
1013} 1025}
1014 1026
1015############################################################################# 1027#############################################################################
1016 1028
1017package CFPlus::UI::ScrolledWindow; 1029package DC::UI::ScrolledWindow;
1018 1030
1019our @ISA = CFPlus::UI::Table::; 1031our @ISA = DC::UI::Table::;
1020 1032
1021sub new { 1033sub new {
1022 my ($class, %arg) = @_; 1034 my ($class, %arg) = @_;
1023 1035
1024 my $child = delete $arg{child}; 1036 my $child = delete $arg{child};
1025 1037
1026 my $self; 1038 my $self;
1027 1039
1028 my $hslider = new CFPlus::UI::Slider 1040 my $hslider = new DC::UI::Slider
1029 c_col => 0, 1041 c_col => 0,
1030 c_row => 1, 1042 c_row => 1,
1031 vertical => 0, 1043 vertical => 0,
1032 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1033 on_changed => sub { 1045 on_changed => sub {
1034 $self->{hpos} = $_[1]; 1046 $self->{hpos} = $_[1];
1035 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1036 }, 1048 },
1037 ; 1049 ;
1038 1050
1039 my $vslider = new CFPlus::UI::Slider 1051 my $vslider = new DC::UI::Slider
1040 c_col => 1, 1052 c_col => 1,
1041 c_row => 0, 1053 c_row => 0,
1042 vertical => 1, 1054 vertical => 1,
1043 range => [0, 0, 1, 0.01], # HACK fix 1055 range => [0, 0, 1, 0.01], # HACK fix
1044 on_changed => sub { 1056 on_changed => sub {
1056 col_expand => [1, 0], 1068 col_expand => [1, 0],
1057 row_expand => [1, 0], 1069 row_expand => [1, 0],
1058 %arg, 1070 %arg,
1059 ); 1071 );
1060 1072
1061 $self->{vp} = new CFPlus::UI::ViewPort 1073 $self->{vp} = new DC::UI::ViewPort
1062 c_col => 0, 1074 c_col => 0,
1063 c_row => 0, 1075 c_row => 0,
1064 expand => 1, 1076 expand => 1,
1065 scroll_x => $self->{scroll_x}, 1077 scroll_x => $self->{scroll_x},
1066 scroll_y => $self->{scroll_y}, 1078 scroll_y => $self->{scroll_y},
1192 $self->SUPER::invoke_size_allocate ($w, $h) 1204 $self->SUPER::invoke_size_allocate ($w, $h)
1193} 1205}
1194 1206
1195############################################################################# 1207#############################################################################
1196 1208
1197package CFPlus::UI::Frame; 1209package DC::UI::Frame;
1198 1210
1199our @ISA = CFPlus::UI::Bin::; 1211our @ISA = DC::UI::Bin::;
1200 1212
1201use CFPlus::OpenGL; 1213use DC::OpenGL;
1202 1214
1203sub new { 1215sub new {
1204 my $class = shift; 1216 my $class = shift;
1205 1217
1206 $class->SUPER::new ( 1218 $class->SUPER::new (
1225 $self->SUPER::_draw; 1237 $self->SUPER::_draw;
1226} 1238}
1227 1239
1228############################################################################# 1240#############################################################################
1229 1241
1230package CFPlus::UI::FancyFrame; 1242package DC::UI::FancyFrame;
1231 1243
1232our @ISA = CFPlus::UI::Bin::; 1244our @ISA = DC::UI::Bin::;
1233 1245
1234use CFPlus::OpenGL; 1246use DC::OpenGL;
1235 1247
1236sub new { 1248sub new {
1237 my ($class, %arg) = @_; 1249 my ($class, %arg) = @_;
1238 1250
1239 if ((exists $arg{label}) && !ref $arg{label}) { 1251 if ((exists $arg{label}) && !ref $arg{label}) {
1240 $arg{label} = new CFPlus::UI::Label 1252 $arg{label} = new DC::UI::Label
1241 align => 1, 1253 align => 1,
1242 valign => 0, 1254 valign => 0.5,
1243 text => $arg{label}, 1255 text => $arg{label},
1244 fontsize => ($arg{border} || 0.8) * 0.75; 1256 fontsize => ($arg{border} || 0.8) * 0.75;
1245 } 1257 }
1246 1258
1247 my $self = $class->SUPER::new ( 1259 my $self = $class->SUPER::new (
1248 # label => "", 1260 # label => "",
1249 fg => [0.6, 0.3, 0.1], 1261 fg => undef,
1250 border => 0.8, 1262 border => 0.8,
1251 style => 'single', 1263 style => 'single',
1252 %arg, 1264 %arg,
1253 ); 1265 );
1254 1266
1257 1269
1258sub add { 1270sub add {
1259 my ($self, @widgets) = @_; 1271 my ($self, @widgets) = @_;
1260 1272
1261 $self->SUPER::add (@widgets); 1273 $self->SUPER::add (@widgets);
1262 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1274 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1263} 1275}
1264 1276
1265sub border { 1277sub border {
1266 int $_[0]{border} * $::FONTSIZE 1278 int $_[0]{border} * $::FONTSIZE
1267} 1279}
1307 my $border = $self->border; 1319 my $border = $self->border;
1308 my ($w, $h) = ($self->{w}, $self->{h}); 1320 my ($w, $h) = ($self->{w}, $self->{h});
1309 1321
1310 $child->draw; 1322 $child->draw;
1311 1323
1312 glColor @{$self->{fg}}; 1324 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1313 glBegin GL_LINE_STRIP; 1325 glBegin GL_LINE_STRIP;
1314 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1326 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1315 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1327 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1328 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1317 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1329 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1325 } 1337 }
1326} 1338}
1327 1339
1328############################################################################# 1340#############################################################################
1329 1341
1330package CFPlus::UI::Toplevel; 1342package DC::UI::Toplevel;
1331 1343
1332our @ISA = CFPlus::UI::Bin::; 1344our @ISA = DC::UI::Bin::;
1333 1345
1334use CFPlus::OpenGL; 1346use DC::OpenGL;
1335 1347
1336my $bg = 1348my $bg =
1337 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1349 new_from_resource DC::Texture "d1_bg.png",
1338 mipmap => 1, wrap => 1; 1350 mipmap => 1, wrap => 1;
1339 1351
1340my @border = 1352my @border =
1341 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1353 map { new_from_resource DC::Texture $_, mipmap => 1 }
1342 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1354 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1343 1355
1344my @icon = 1356my @icon =
1345 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1357 map { new_from_resource DC::Texture $_, mipmap => 1 }
1346 qw(x1_move.png x1_resize.png); 1358 qw(x1_move.png x1_resize.png);
1347 1359
1348sub new { 1360sub new {
1349 my ($class, %arg) = @_; 1361 my ($class, %arg) = @_;
1350 1362
1351 my $self = $class->SUPER::new ( 1363 my $self = $class->SUPER::new (
1352 bg => [1, 1, 1, 1], 1364 bg => [1, 1, 1, 1],
1353 border_bg => [1, 1, 1, 1], 1365 border_bg => [1, 1, 1, 1],
1354 border => 0.6, 1366 border => 1,
1355 can_events => 1, 1367 can_events => 1,
1356 min_w => 64, 1368 min_w => 64,
1357 min_h => 32, 1369 min_h => 32,
1358 %arg, 1370 %arg,
1359 ); 1371 );
1360 1372
1361 $self->{title_widget} = new CFPlus::UI::Label 1373 $self->{title_widget} = new DC::UI::Label
1362 align => 0, 1374 align => 0.5,
1363 valign => 1, 1375 valign => 1,
1364 text => $self->{title}, 1376 text => $self->{title},
1365 fontsize => $self->{border}, 1377 fontsize => $self->{border},
1366 if exists $self->{title}; 1378 if exists $self->{title};
1367 1379
1368 if ($self->{has_close_button}) { 1380 if ($self->{has_close_button}) {
1369 $self->{close_button} = 1381 $self->{close_button} =
1370 new CFPlus::UI::ImageButton 1382 new DC::UI::ImageButton
1371 path => 'x1_close.png', 1383 path => 'x1_close.png',
1372 on_activate => sub { $self->emit ("delete") }; 1384 on_activate => sub { $self->emit ("delete") };
1373 1385
1374 $self->CFPlus::UI::Container::add ($self->{close_button}); 1386 $self->DC::UI::Container::add ($self->{close_button});
1375 } 1387 }
1376 1388
1377 $self 1389 $self
1378} 1390}
1379 1391
1380sub add { 1392sub add {
1381 my ($self, @widgets) = @_; 1393 my ($self, @widgets) = @_;
1382 1394
1383 $self->SUPER::add (@widgets); 1395 $self->SUPER::add (@widgets);
1384 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1396 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1385 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1397 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1386} 1398}
1387 1399
1388sub border { 1400sub border {
1389 int $_[0]{border} * $::FONTSIZE 1401 int $_[0]{border} * $::FONTSIZE
1390} 1402}
1575 if $self->{close_button}; 1587 if $self->{close_button};
1576} 1588}
1577 1589
1578############################################################################# 1590#############################################################################
1579 1591
1580package CFPlus::UI::Table; 1592package DC::UI::Table;
1581 1593
1582our @ISA = CFPlus::UI::Container::; 1594our @ISA = DC::UI::Container::;
1583 1595
1584use List::Util qw(max sum); 1596use List::Util qw(max sum);
1585 1597
1586use CFPlus::OpenGL; 1598use DC::OpenGL;
1587 1599
1588sub new { 1600sub new {
1589 my $class = shift; 1601 my $class = shift;
1590 1602
1591 $class->SUPER::new ( 1603 $class->SUPER::new (
1635 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1647 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1636 1648
1637 my $sw = sum @w[$c .. $c + $cs - 1]; 1649 my $sw = sum @w[$c .. $c + $cs - 1];
1638 1650
1639 if ($w > $sw) { 1651 if ($w > $sw) {
1640 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1652 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1641 } 1653 }
1642 } 1654 }
1643 1655
1644 # second pass, rows 1656 # second pass, rows
1645 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1657 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1646 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1658 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1647 1659
1648 my $sh = sum @h[$r .. $r + $rs - 1]; 1660 my $sh = sum @h[$r .. $r + $rs - 1];
1649 1661
1650 if ($h > $sh) { 1662 if ($h > $sh) {
1651 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1663 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1652 } 1664 }
1653 } 1665 }
1654 1666
1655 (\@w, \@h) 1667 (\@w, \@h)
1656} 1668}
1679 @col_expand = (1) x @$ws unless @col_expand; 1691 @col_expand = (1) x @$ws unless @col_expand;
1680 my $col_expand = (sum @col_expand) || 1; 1692 my $col_expand = (sum @col_expand) || 1;
1681 1693
1682 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1694 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1683 1695
1684 CFPlus::UI::harmonize $ws; 1696 DC::UI::harmonize $ws;
1685 1697
1686 my @row_expand = @{$self->{row_expand}}; 1698 my @row_expand = @{$self->{row_expand}};
1687 @row_expand = (1) x @$ws unless @row_expand; 1699 @row_expand = (1) x @$ws unless @row_expand;
1688 my $row_expand = (sum @row_expand) || 1; 1700 my $row_expand = (sum @row_expand) || 1;
1689 1701
1690 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1702 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1691 1703
1692 CFPlus::UI::harmonize $hs; 1704 DC::UI::harmonize $hs;
1693 1705
1694 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1706 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1695 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1707 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1696 1708
1697 for my $widget ($self->children) { 1709 for my $widget ($self->children) {
1706 1 1718 1
1707} 1719}
1708 1720
1709############################################################################# 1721#############################################################################
1710 1722
1711package CFPlus::UI::Fixed; 1723package DC::UI::Fixed;
1712 1724
1713use List::Util qw(min max); 1725use List::Util qw(min max);
1714 1726
1715our @ISA = CFPlus::UI::Container::; 1727our @ISA = DC::UI::Container::;
1716 1728
1717sub _scale($$$) { 1729sub _scale($$$) {
1718 my ($rel, $val, $max) = @_; 1730 my ($rel, $val, $max) = @_;
1719 1731
1720 $rel ? $val * $max : $val 1732 $rel ? $val * $max : $val
1772 1 1784 1
1773} 1785}
1774 1786
1775############################################################################# 1787#############################################################################
1776 1788
1777package CFPlus::UI::Box; 1789package DC::UI::Box;
1778 1790
1779our @ISA = CFPlus::UI::Container::; 1791our @ISA = DC::UI::Container::;
1780 1792
1781sub size_request { 1793sub size_request {
1782 my ($self) = @_; 1794 my ($self) = @_;
1783 1795
1784 my @children = $self->visible_children; 1796 my @children = $self->visible_children;
1819 $req[$_] += $space * $children[$_]{expand} 1831 $req[$_] += $space * $children[$_]{expand}
1820 for 0 .. $#children; 1832 for 0 .. $#children;
1821 } 1833 }
1822 } 1834 }
1823 1835
1824 CFPlus::UI::harmonize \@req; 1836 DC::UI::harmonize \@req;
1825 1837
1826 my $pos = 0; 1838 my $pos = 0;
1827 for (0 .. $#children) { 1839 for (0 .. $#children) {
1828 my $alloc = $req[$_]; 1840 my $alloc = $req[$_];
1829 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1841 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1834 1 1846 1
1835} 1847}
1836 1848
1837############################################################################# 1849#############################################################################
1838 1850
1839package CFPlus::UI::HBox; 1851package DC::UI::HBox;
1840 1852
1841our @ISA = CFPlus::UI::Box::; 1853our @ISA = DC::UI::Box::;
1842 1854
1843sub new { 1855sub new {
1844 my $class = shift; 1856 my $class = shift;
1845 1857
1846 $class->SUPER::new ( 1858 $class->SUPER::new (
1849 ) 1861 )
1850} 1862}
1851 1863
1852############################################################################# 1864#############################################################################
1853 1865
1854package CFPlus::UI::VBox; 1866package DC::UI::VBox;
1855 1867
1856our @ISA = CFPlus::UI::Box::; 1868our @ISA = DC::UI::Box::;
1857 1869
1858sub new { 1870sub new {
1859 my $class = shift; 1871 my $class = shift;
1860 1872
1861 $class->SUPER::new ( 1873 $class->SUPER::new (
1864 ) 1876 )
1865} 1877}
1866 1878
1867############################################################################# 1879#############################################################################
1868 1880
1869package CFPlus::UI::Label; 1881package DC::UI::Label;
1870 1882
1871our @ISA = CFPlus::UI::DrawBG::; 1883our @ISA = DC::UI::DrawBG::;
1872 1884
1873use CFPlus::OpenGL; 1885use DC::OpenGL;
1874 1886
1875sub new { 1887sub new {
1876 my ($class, %arg) = @_; 1888 my ($class, %arg) = @_;
1877 1889
1878 my $self = $class->SUPER::new ( 1890 my $self = $class->SUPER::new (
1883 #text => initial text 1895 #text => initial text
1884 #markup => initial narkup 1896 #markup => initial narkup
1885 #max_w => maximum pixel width 1897 #max_w => maximum pixel width
1886 #style => 0, # render flags 1898 #style => 0, # render flags
1887 ellipsise => 3, # end 1899 ellipsise => 3, # end
1888 layout => (new CFPlus::Layout), 1900 layout => (new DC::Layout),
1889 fontsize => 1, 1901 fontsize => 1,
1890 align => -1, 1902 align => 0.5,
1891 valign => -1, 1903 valign => 0.5,
1892 padding_x => 2, 1904 padding_x => 4,
1893 padding_y => 2, 1905 padding_y => 2,
1894 can_events => 0, 1906 can_events => 0,
1895 %arg 1907 %arg
1896 ); 1908 );
1897 1909
1898 if (exists $self->{template}) { 1910 if (exists $self->{template}) {
1899 my $layout = new CFPlus::Layout; 1911 my $layout = new DC::Layout;
1900 $layout->set_text (delete $self->{template}); 1912 $layout->set_text (delete $self->{template});
1901 $self->{template} = $layout; 1913 $self->{template} = $layout;
1902 } 1914 }
1903 1915
1904 if (exists $self->{markup}) { 1916 if (exists $self->{markup}) {
1963 1975
1964 $self->{size_req} ||= do { 1976 $self->{size_req} ||= do {
1965 my ($max_w, $max_h) = $self->get_max_wh; 1977 my ($max_w, $max_h) = $self->get_max_wh;
1966 1978
1967 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1979 $self->{layout}->set_font ($self->{font}) if $self->{font};
1968 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1980 $self->{layout}->set_width ($max_w);
1969 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1981 $self->{layout}->set_ellipsise ($self->{ellipsise});
1970 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1982 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1971 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1983 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1972 1984
1973 my ($w, $h) = $self->{layout}->size; 1985 my ($w, $h) = $self->{layout}->size;
1974 1986
1975 if (exists $self->{template}) { 1987 if (exists $self->{template}) {
1976 $self->{template}->set_font ($self->{font}) if $self->{font}; 1988 $self->{template}->set_font ($self->{font}) if $self->{font};
1977 $self->{template}->set_width ($self->{max_w} || -1); 1989 $self->{template}->set_width ($max_w);
1978 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1990 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1979 1991
1980 my ($w2, $h2) = $self->{template}->size; 1992 my ($w2, $h2) = $self->{template}->size;
1981 1993
1982 $w = List::Util::max $w, $w2; 1994 $w = List::Util::max $w, $w2;
2038 2050
2039 [$self->{layout}->size] 2051 [$self->{layout}->size]
2040 }; 2052 };
2041 2053
2042 unless (exists $self->{ox}) { 2054 unless (exists $self->{ox}) {
2043 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2055 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2044 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2056 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2045 : ($self->{w} - $size->[0]) * 0.5);
2046
2047 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2048 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2049 : ($self->{h} - $size->[1]) * 0.5);
2050 2057
2051 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2058 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052 }; 2059 };
2053 2060
2054# unless ($self->{list}) { 2061# unless ($self->{list}) {
2055# $self->{list} = CFPlus::OpenGL::glGenList; 2062# $self->{list} = DC::OpenGL::glGenList;
2056# CFPlus::OpenGL::glNewList $self->{list}; 2063# DC::OpenGL::glNewList $self->{list};
2057# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2064# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2058# CFPlus::OpenGL::glEndList; 2065# DC::OpenGL::glEndList;
2059# } 2066# }
2060# 2067#
2061# CFPlus::OpenGL::glCallList $self->{list}; 2068# DC::OpenGL::glCallList $self->{list};
2062 2069
2063 $self->{layout}->draw; 2070 $self->{layout}->draw;
2064} 2071}
2065 2072
2066#sub destroy { 2073#sub destroy {
2067# my ($self) = @_; 2074# my ($self) = @_;
2068# 2075#
2069# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2076# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2070# 2077#
2071# $self->SUPER::destroy; 2078# $self->SUPER::destroy;
2072#} 2079#}
2073 2080
2074############################################################################# 2081#############################################################################
2075 2082
2076package CFPlus::UI::EntryBase; 2083package DC::UI::EntryBase;
2077 2084
2078our @ISA = CFPlus::UI::Label::; 2085our @ISA = DC::UI::Label::;
2079 2086
2080use CFPlus::OpenGL; 2087use DC::OpenGL;
2081 2088
2082sub new { 2089sub new {
2083 my $class = shift; 2090 my $class = shift;
2084 2091
2085 $class->SUPER::new ( 2092 $class->SUPER::new (
2086 fg => [1, 1, 1], 2093 fg => [1, 1, 1],
2087 bg => [0, 0, 0, 0.2], 2094 bg => [0, 0, 0, 0.2],
2088 outline => [0.6, 0.3, 0.1], 2095 outline => undef,
2089 active_bg => [0, 0, 1, .2], 2096 active_bg => [0, 0, 1, .2],
2090 active_fg => [1, 1, 1], 2097 active_fg => [1, 1, 1],
2091 active_outline => [1, 1, 0], 2098 active_outline => [1, 1, 0],
2092 can_hover => 1, 2099 can_hover => 1,
2093 can_focus => 1, 2100 can_focus => 1,
2101 align => 0,
2094 valign => 0, 2102 valign => 0.5,
2095 can_events => 1, 2103 can_events => 1,
2096 ellipsise => 0, 2104 ellipsise => 0,
2105 padding_x => 4,
2106 padding_y => 2,
2097 #text => ... 2107 #text => ...
2098 #hidden => "*", 2108 #hidden => "*",
2099 @_ 2109 @_
2100 ) 2110 )
2101} 2111}
2148 2158
2149 my $text = $self->get_text; 2159 my $text = $self->get_text;
2150 2160
2151 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2161 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2152 2162
2153 if ($uni == 8) { 2163 if ($sym == DC::SDLK_BACKSPACE) {
2154 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2164 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2155 } elsif ($uni == 127) { 2165 } elsif ($sym == DC::SDLK_DELETE) {
2156 substr $text, $self->{cursor}, 1, ""; 2166 substr $text, $self->{cursor}, 1, "";
2157 } elsif ($sym == CFPlus::SDLK_LEFT) { 2167 } elsif ($sym == DC::SDLK_LEFT) {
2158 --$self->{cursor} if $self->{cursor}; 2168 --$self->{cursor} if $self->{cursor};
2159 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2169 } elsif ($sym == DC::SDLK_RIGHT) {
2160 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2170 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2161 } elsif ($sym == CFPlus::SDLK_HOME) { 2171 } elsif ($sym == DC::SDLK_HOME) {
2162 # what a hack 2172 # what a hack
2163 $self->{cursor} = 2173 $self->{cursor} =
2164 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2174 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2165 ? length $1 2175 ? length $1
2166 : 0; 2176 : 0;
2167 } elsif ($sym == CFPlus::SDLK_END) { 2177 } elsif ($sym == DC::SDLK_END) {
2168 # uh, again 2178 # uh, again
2169 $self->{cursor} = 2179 $self->{cursor} =
2170 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2180 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2171 ? $self->{cursor} + length $1 2181 ? $self->{cursor} + length $1
2172 : length $self->{text}; 2182 : length $self->{text};
2263 glColor @{$self->{active_outline}}; 2273 glColor @{$self->{active_outline}};
2264 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2274 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2265 glLineWidth 1; 2275 glLineWidth 1;
2266 2276
2267 } else { 2277 } else {
2268 glColor @{$self->{outline}}; 2278 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2269 glBegin GL_LINE_STRIP; 2279 glBegin GL_LINE_STRIP;
2270 glVertex .5, $self->{h} * .5; 2280 glVertex .5, $self->{h} * .5;
2271 glVertex .5, $self->{h} - 2.5; 2281 glVertex .5, $self->{h} - 2.5;
2272 glVertex $self->{w} - .5, $self->{h} - 2.5; 2282 glVertex $self->{w} - .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} * .5; 2283 glVertex $self->{w} - .5, $self->{h} * .5;
2275 } 2285 }
2276} 2286}
2277 2287
2278############################################################################# 2288#############################################################################
2279 2289
2280package CFPlus::UI::Entry; 2290package DC::UI::Entry;
2281 2291
2282our @ISA = CFPlus::UI::EntryBase::; 2292our @ISA = DC::UI::EntryBase::;
2283 2293
2284use CFPlus::OpenGL; 2294use DC::OpenGL;
2295
2296sub new {
2297 my $class = shift;
2298
2299 $class->SUPER::new (
2300 history_pointer => -1,
2301 @_
2302 )
2303}
2304
2285 2305
2286sub invoke_key_down { 2306sub invoke_key_down {
2287 my ($self, $ev) = @_; 2307 my ($self, $ev) = @_;
2288 2308
2289 my $sym = $ev->{sym}; 2309 my $sym = $ev->{sym};
2295 $self->{history_pointer} = -1; 2315 $self->{history_pointer} = -1;
2296 $self->{history_saveback} = ''; 2316 $self->{history_saveback} = '';
2297 $self->emit (activate => $txt); 2317 $self->emit (activate => $txt);
2298 $self->update; 2318 $self->update;
2299 2319
2300 } elsif ($sym == CFPlus::SDLK_UP) { 2320 } elsif ($sym == DC::SDLK_UP) {
2301 if ($self->{history_pointer} < 0) { 2321 if ($self->{history_pointer} < 0) {
2302 $self->{history_saveback} = $self->get_text; 2322 $self->{history_saveback} = $self->get_text;
2303 } 2323 }
2304 if (@{$self->{history} || []} > 0) { 2324 if (@{$self->{history} || []} > 0) {
2305 $self->{history_pointer}++; 2325 $self->{history_pointer}++;
2307 $self->{history_pointer} = @{$self->{history} || []} - 1; 2327 $self->{history_pointer} = @{$self->{history} || []} - 1;
2308 } 2328 }
2309 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2329 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2310 } 2330 }
2311 2331
2312 } elsif ($sym == CFPlus::SDLK_DOWN) { 2332 } elsif ($sym == DC::SDLK_DOWN) {
2313 $self->{history_pointer}--; 2333 $self->{history_pointer}--;
2314 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2334 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2315 2335
2316 if ($self->{history_pointer} >= 0) { 2336 if ($self->{history_pointer} >= 0) {
2317 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2337 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2318 } else { 2338 } else {
2339 if (defined $self->{history_saveback}) {
2319 $self->set_text ($self->{history_saveback}); 2340 $self->set_text ($self->{history_saveback});
2341 $self->{history_saveback} = undef;
2342 }
2320 } 2343 }
2321 2344
2322 } else { 2345 } else {
2323 return $self->SUPER::invoke_key_down ($ev) 2346 return $self->SUPER::invoke_key_down ($ev)
2324 } 2347 }
2326 1 2349 1
2327} 2350}
2328 2351
2329############################################################################# 2352#############################################################################
2330 2353
2331package CFPlus::UI::TextEdit; 2354package DC::UI::TextEdit;
2332 2355
2333our @ISA = CFPlus::UI::EntryBase::; 2356our @ISA = DC::UI::EntryBase::;
2334 2357
2335use CFPlus::OpenGL; 2358use DC::OpenGL;
2359
2360sub new {
2361 my $class = shift;
2362
2363 $class->SUPER::new (
2364 padding_y => 4,
2365
2366 @_
2367 )
2368}
2336 2369
2337sub move_cursor_ver { 2370sub move_cursor_ver {
2338 my ($self, $dy) = @_; 2371 my ($self, $dy) = @_;
2339 2372
2340 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2373 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2341 2374
2342 $y += $dy; 2375 $line += $dy;
2343 2376
2344 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2377 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2345 $self->{cursor} = $index; 2378 $self->{cursor} = $index;
2346 delete $self->{cur_h}; 2379 delete $self->{cur_h};
2347 $self->update; 2380 $self->update;
2348 return; 2381 return;
2349 } 2382 }
2352sub invoke_key_down { 2385sub invoke_key_down {
2353 my ($self, $ev) = @_; 2386 my ($self, $ev) = @_;
2354 2387
2355 my $sym = $ev->{sym}; 2388 my $sym = $ev->{sym};
2356 2389
2357 if ($sym == CFPlus::SDLK_UP) { 2390 if ($sym == DC::SDLK_UP) {
2358 $self->move_cursor_ver (-1); 2391 $self->move_cursor_ver (-1);
2359 } elsif ($sym == CFPlus::SDLK_DOWN) { 2392 } elsif ($sym == DC::SDLK_DOWN) {
2360 $self->move_cursor_ver (+1); 2393 $self->move_cursor_ver (+1);
2361 } else { 2394 } else {
2362 return $self->SUPER::invoke_key_down ($ev) 2395 return $self->SUPER::invoke_key_down ($ev)
2363 } 2396 }
2364 2397
2365 1 2398 1
2366} 2399}
2367 2400
2368############################################################################# 2401#############################################################################
2369 2402
2370package CFPlus::UI::ButtonBin; 2403package DC::UI::ButtonBin;
2371 2404
2372our @ISA = CFPlus::UI::Bin::; 2405our @ISA = DC::UI::Bin::;
2373 2406
2374use CFPlus::OpenGL; 2407use DC::OpenGL;
2375 2408
2376my @tex = 2409my @tex =
2377 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2410 map { new_from_resource DC::Texture $_, mipmap => 1 }
2378 qw(b1_button_inactive.png b1_button_active.png); 2411 qw(b1_button_inactive.png b1_button_active.png);
2379 2412
2380sub new { 2413sub new {
2381 my $class = shift; 2414 my $class = shift;
2382 2415
2383 $class->SUPER::new ( 2416 $class->SUPER::new (
2384 can_hover => 1, 2417 can_hover => 1,
2385 align => 0, 2418 align => 0.5,
2386 valign => 0, 2419 valign => 0.5,
2387 can_events => 1, 2420 can_events => 1,
2388 @_ 2421 @_
2389 ) 2422 )
2390} 2423}
2391 2424
2414 $self->SUPER::_draw; 2447 $self->SUPER::_draw;
2415} 2448}
2416 2449
2417############################################################################# 2450#############################################################################
2418 2451
2419package CFPlus::UI::Button; 2452package DC::UI::Button;
2420 2453
2421our @ISA = CFPlus::UI::Label::; 2454our @ISA = DC::UI::Label::;
2422 2455
2423use CFPlus::OpenGL; 2456use DC::OpenGL;
2424 2457
2425my @tex = 2458my @tex =
2426 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2459 map { new_from_resource DC::Texture $_, mipmap => 1 }
2427 qw(b1_button_inactive.png b1_button_active.png); 2460 qw(b1_button_inactive.png b1_button_active.png);
2428 2461
2429sub new { 2462sub new {
2430 my $class = shift; 2463 my $class = shift;
2431 2464
2432 $class->SUPER::new ( 2465 $class->SUPER::new (
2433 padding_x => 4, 2466 padding_x => 8,
2434 padding_y => 4, 2467 padding_y => 4,
2435 fg => [1.0, 1.0, 1.0], 2468 fg => [1.0, 1.0, 1.0],
2436 active_fg => [0.8, 0.8, 0.8], 2469 active_fg => [0.8, 0.8, 0.8],
2437 can_hover => 1, 2470 can_hover => 1,
2438 align => 0, 2471 align => 0.5,
2439 valign => 0, 2472 valign => 0.5,
2440 can_events => 1, 2473 can_events => 1,
2441 @_ 2474 @_
2442 ) 2475 )
2443} 2476}
2444 2477
2469 $self->SUPER::_draw; 2502 $self->SUPER::_draw;
2470} 2503}
2471 2504
2472############################################################################# 2505#############################################################################
2473 2506
2474package CFPlus::UI::CheckBox; 2507package DC::UI::CheckBox;
2475 2508
2476our @ISA = CFPlus::UI::DrawBG::; 2509our @ISA = DC::UI::DrawBG::;
2477 2510
2478my @tex = 2511my @tex =
2479 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2512 map { new_from_resource DC::Texture $_, mipmap => 1 }
2480 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2513 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2481 2514
2482use CFPlus::OpenGL; 2515use DC::OpenGL;
2483 2516
2484sub new { 2517sub new {
2485 my $class = shift; 2518 my $class = shift;
2486 2519
2487 $class->SUPER::new ( 2520 $class->SUPER::new (
2544 glDisable GL_TEXTURE_2D; 2577 glDisable GL_TEXTURE_2D;
2545} 2578}
2546 2579
2547############################################################################# 2580#############################################################################
2548 2581
2549package CFPlus::UI::Image; 2582package DC::UI::Image;
2550 2583
2551our @ISA = CFPlus::UI::Base::; 2584our @ISA = DC::UI::Base::;
2552 2585
2553use CFPlus::OpenGL; 2586use DC::OpenGL;
2554 2587
2555our %texture_cache; 2588our %texture_cache;
2556 2589
2557sub new { 2590sub new {
2558 my $class = shift; 2591 my $class = shift;
2565 2598
2566 $self->{path} || $self->{tex} 2599 $self->{path} || $self->{tex}
2567 or Carp::croak "'path' or 'tex' attributes required"; 2600 or Carp::croak "'path' or 'tex' attributes required";
2568 2601
2569 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2602 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2570 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2603 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2571 2604
2572 CFPlus::weaken $texture_cache{$self->{path}}; 2605 DC::weaken $texture_cache{$self->{path}};
2573 2606
2574 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2607 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2575 2608
2576 $self 2609 $self
2577} 2610}
2578 2611
2579sub STORABLE_freeze { 2612sub STORABLE_freeze {
2580 my ($self, $cloning) = @_; 2613 my ($self, $cloning) = @_;
2581 2614
2582 $self->{path} 2615 $self->{path}
2583 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2616 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2584 2617
2585 $self->{path} 2618 $self->{path}
2586} 2619}
2587 2620
2588sub STORABLE_attach { 2621sub STORABLE_attach {
2619 glDisable GL_TEXTURE_2D; 2652 glDisable GL_TEXTURE_2D;
2620} 2653}
2621 2654
2622############################################################################# 2655#############################################################################
2623 2656
2624package CFPlus::UI::ImageButton; 2657package DC::UI::ImageButton;
2625 2658
2626our @ISA = CFPlus::UI::Image::; 2659our @ISA = DC::UI::Image::;
2627 2660
2628use CFPlus::OpenGL; 2661use DC::OpenGL;
2629 2662
2630my %textures; 2663my %textures;
2631 2664
2632sub new { 2665sub new {
2633 my $class = shift; 2666 my $class = shift;
2636 padding_x => 4, 2669 padding_x => 4,
2637 padding_y => 4, 2670 padding_y => 4,
2638 fg => [1, 1, 1], 2671 fg => [1, 1, 1],
2639 active_fg => [0, 0, 1], 2672 active_fg => [0, 0, 1],
2640 can_hover => 1, 2673 can_hover => 1,
2641 align => 0, 2674 align => 0.5,
2642 valign => 0, 2675 valign => 0.5,
2643 can_events => 1, 2676 can_events => 1,
2644 @_ 2677 @_
2645 ); 2678 );
2646} 2679}
2647 2680
2661 1 2694 1
2662} 2695}
2663 2696
2664############################################################################# 2697#############################################################################
2665 2698
2666package CFPlus::UI::VGauge; 2699package DC::UI::VGauge;
2667 2700
2668our @ISA = CFPlus::UI::Base::; 2701our @ISA = DC::UI::Base::;
2669 2702
2670use List::Util qw(min max); 2703use List::Util qw(min max);
2671 2704
2672use CFPlus::OpenGL; 2705use DC::OpenGL;
2673 2706
2674my %tex = ( 2707my %tex = (
2675 food => [ 2708 food => [
2676 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2709 map { new_from_resource DC::Texture $_, mipmap => 1 }
2677 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2710 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2678 ], 2711 ],
2679 grace => [ 2712 grace => [
2680 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2713 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2714 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2682 ], 2715 ],
2683 hp => [ 2716 hp => [
2684 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2717 map { new_from_resource DC::Texture $_, mipmap => 1 }
2685 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2718 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2686 ], 2719 ],
2687 mana => [ 2720 mana => [
2688 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2721 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2722 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2690 ], 2723 ],
2691); 2724);
2692 2725
2693# eg. VGauge->new (gauge => 'food'), default gauge: food 2726# eg. VGauge->new (gauge => 'food'), default gauge: food
2796 glDisable GL_TEXTURE_2D; 2829 glDisable GL_TEXTURE_2D;
2797} 2830}
2798 2831
2799############################################################################# 2832#############################################################################
2800 2833
2801package CFPlus::UI::Progress; 2834package DC::UI::Progress;
2802 2835
2803our @ISA = CFPlus::UI::Label::; 2836our @ISA = DC::UI::Label::;
2804 2837
2805use CFPlus::OpenGL; 2838use DC::OpenGL;
2806 2839
2807sub new { 2840sub new {
2808 my ($class, %arg) = @_; 2841 my ($class, %arg) = @_;
2809 2842
2810 my $self = $class->SUPER::new ( 2843 my $self = $class->SUPER::new (
2811 fg => [1, 1, 1], 2844 fg => [1, 1, 1],
2812 bg => [0, 0, 1, 0.2], 2845 bg => [0, 0, 1, 0.2],
2813 bar => [0.7, 0.5, 0.1, 0.8], 2846 bar => [0.7, 0.5, 0.1, 0.8],
2814 outline => [0.4, 0.3, 0], 2847 outline => [0.4, 0.3, 0],
2815 fontsize => 0.9, 2848 fontsize => 0.9,
2816 valign => 0, 2849 valign => 0.5,
2817 align => 0, 2850 align => 0.5,
2818 can_events => 1, 2851 can_events => 1,
2819 ellipsise => 1, 2852 ellipsise => 1,
2820 label => "%d%%", 2853 label => "%d%%",
2821 %arg, 2854 %arg,
2822 ); 2855 );
2830 my ($self, $label) = @_; 2863 my ($self, $label) = @_;
2831 2864
2832 return if $self->{label} eq $label; 2865 return if $self->{label} eq $label;
2833 $self->{label} = $label; 2866 $self->{label} = $label;
2834 2867
2835 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2868 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2836} 2869}
2837 2870
2838sub set_value { 2871sub set_value {
2839 my ($self, $value) = @_; 2872 my ($self, $value) = @_;
2840 2873
2877 } 2910 }
2878} 2911}
2879 2912
2880############################################################################# 2913#############################################################################
2881 2914
2882package CFPlus::UI::ExperienceProgress; 2915package DC::UI::ExperienceProgress;
2883 2916
2884our @ISA = CFPlus::UI::Progress::; 2917our @ISA = DC::UI::Progress::;
2885 2918
2886sub new { 2919sub new {
2887 my ($class, %arg) = @_; 2920 my ($class, %arg) = @_;
2888 2921
2889 my $self = $class->SUPER::new ( 2922 my $self = $class->SUPER::new (
2934 $self->SUPER::set_value ($v); 2967 $self->SUPER::set_value ($v);
2935} 2968}
2936 2969
2937############################################################################# 2970#############################################################################
2938 2971
2939package CFPlus::UI::Gauge; 2972package DC::UI::Gauge;
2940 2973
2941our @ISA = CFPlus::UI::VBox::; 2974our @ISA = DC::UI::VBox::;
2942 2975
2943sub new { 2976sub new {
2944 my ($class, %arg) = @_; 2977 my ($class, %arg) = @_;
2945 2978
2946 my $self = $class->SUPER::new ( 2979 my $self = $class->SUPER::new (
2948 can_hover => 1, 2981 can_hover => 1,
2949 can_events => 1, 2982 can_events => 1,
2950 %arg, 2983 %arg,
2951 ); 2984 );
2952 2985
2953 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2986 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2954 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2987 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2955 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2988 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2956 2989
2957 $self 2990 $self
2958} 2991}
2959 2992
2960sub set_fontsize { 2993sub set_fontsize {
2981 $self->{value}->set_text ($val); 3014 $self->{value}->set_text ($val);
2982} 3015}
2983 3016
2984############################################################################# 3017#############################################################################
2985 3018
2986package CFPlus::UI::Slider; 3019package DC::UI::Slider;
2987 3020
2988use strict; 3021use strict;
2989 3022
2990use CFPlus::OpenGL; 3023use DC::OpenGL;
2991 3024
2992our @ISA = CFPlus::UI::DrawBG::; 3025our @ISA = DC::UI::DrawBG::;
2993 3026
2994my @tex = 3027my @tex =
2995 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3028 map { new_from_resource DC::Texture $_ }
2996 qw(s1_slider.png s1_slider_bg.png); 3029 qw(s1_slider.png s1_slider_bg.png);
2997 3030
2998sub new { 3031sub new {
2999 my $class = shift; 3032 my $class = shift;
3000 3033
3037sub set_value { 3070sub set_value {
3038 my ($self, $value) = @_; 3071 my ($self, $value) = @_;
3039 3072
3040 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3073 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3041 3074
3042 $hi = $lo + 1 if $hi <= $lo; 3075 $hi = $lo if $hi < $lo;
3043 3076
3044 $page = $hi - $lo if $page > $hi - $lo; 3077 $value = $hi - $page if $value > $hi - $page;
3045
3046 $value = $lo if $value < $lo; 3078 $value = $lo if $value < $lo;
3047 $value = $hi - $page if $value > $hi - $page;
3048 3079
3049 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3080 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3050 if $unit; 3081 if $unit;
3051 3082
3052 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3083 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3096sub invoke_mouse_wheel { 3127sub invoke_mouse_wheel {
3097 my ($self, $ev) = @_; 3128 my ($self, $ev) = @_;
3098 3129
3099 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3130 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3100 3131
3101 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3132 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3102 3133
3103 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3134 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3104 3135
3105 1 3136 1
3106} 3137}
3116 my ($self) = @_; 3147 my ($self) = @_;
3117 3148
3118 unless ($self->{knob_w}) { 3149 unless ($self->{knob_w}) {
3119 $self->set_value ($self->{range}[0]); 3150 $self->set_value ($self->{range}[0]);
3120 3151
3121 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3152 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3122 my $range = ($hi - $page - $lo) || 1e-100; 3153 my $range = ($hi - $page - $lo) || 1e-10;
3123 3154
3124 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3155 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3125 3156
3126 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3157 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3127 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3158 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3128 3159
3129 $value = ($value - $lo) / $range; 3160 $value = ($value - $lo) / $range;
3159 glDisable GL_TEXTURE_2D; 3190 glDisable GL_TEXTURE_2D;
3160} 3191}
3161 3192
3162############################################################################# 3193#############################################################################
3163 3194
3164package CFPlus::UI::ValSlider; 3195package DC::UI::ValSlider;
3165 3196
3166our @ISA = CFPlus::UI::HBox::; 3197our @ISA = DC::UI::HBox::;
3167 3198
3168sub new { 3199sub new {
3169 my ($class, %arg) = @_; 3200 my ($class, %arg) = @_;
3170 3201
3171 my $range = delete $arg{range}; 3202 my $range = delete $arg{range};
3172 3203
3173 my $self = $class->SUPER::new ( 3204 my $self = $class->SUPER::new (
3174 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3205 slider => (new DC::UI::Slider expand => 1, range => $range),
3175 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3206 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3176 to_value => sub { shift }, 3207 to_value => sub { shift },
3177 from_value => sub { shift }, 3208 from_value => sub { shift },
3178 %arg, 3209 %arg,
3179 ); 3210 );
3180 3211
3200sub set_range { shift->{slider}->set_range (@_) } 3231sub set_range { shift->{slider}->set_range (@_) }
3201sub set_value { shift->{slider}->set_value (@_) } 3232sub set_value { shift->{slider}->set_value (@_) }
3202 3233
3203############################################################################# 3234#############################################################################
3204 3235
3205package CFPlus::UI::TextScroller; 3236package DC::UI::TextScroller;
3206 3237
3207our @ISA = CFPlus::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
3208 3239
3209use CFPlus::OpenGL; 3240use DC::OpenGL;
3210 3241
3211sub new { 3242sub new {
3212 my $class = shift; 3243 my $class = shift;
3213 3244
3214 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
3216 can_events => 1, 3247 can_events => 1,
3217 indent => 0, 3248 indent => 0,
3218 #font => default_font 3249 #font => default_font
3219 @_, 3250 @_,
3220 3251
3221 layout => (new CFPlus::Layout), 3252 layout => (new DC::Layout),
3222 par => [], 3253 par => [],
3223 max_par => 0, 3254 max_par => 0,
3224 height => 0, 3255 height => 0,
3225 children => [ 3256 children => [
3226 (new CFPlus::UI::Empty expand => 1), 3257 (new DC::UI::Empty expand => 1),
3227 (new CFPlus::UI::Slider vertical => 1), 3258 (new DC::UI::Slider vertical => 1),
3228 ], 3259 ],
3229 ); 3260 );
3230 3261
3231 $self->{children}[1]->connect (changed => sub { $self->update }); 3262 $self->{children}[1]->connect (changed => sub { $self->update });
3232 3263
3288 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3319 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3289 $layout->set_markup ($para->{markup}); 3320 $layout->set_markup ($para->{markup});
3290 3321
3291 $layout->set_shapes ( 3322 $layout->set_shapes (
3292 map 3323 map
3293 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3324 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3294 @{$para->{widget}} 3325 @{$para->{widget}}
3295 ); 3326 );
3296 3327
3297 $layout 3328 $layout
3298} 3329}
3415 $ROOT->on_post_alloc ($self => sub { 3446 $ROOT->on_post_alloc ($self => sub {
3416 $self->force_uptodate; 3447 $self->force_uptodate;
3417 3448
3418 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3449 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3419 3450
3420 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3451 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3421 glClearColor 0, 0, 0, 0; 3452 glClearColor 0, 0, 0, 0;
3422 glClear GL_COLOR_BUFFER_BIT; 3453 glClear GL_COLOR_BUFFER_BIT;
3423 3454
3424 package CFPlus::UI::Base; 3455 package DC::UI::Base;
3425 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3456 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3426 (0, 0, $self->{w}, $self->{h}); 3457 (0, 0, $self->{w}, $self->{h});
3427 3458
3428 my $top = int $self->{children}[1]{range}[0]; 3459 my $top = int $self->{children}[1]{range}[0];
3429 3460
3488 $self->{children}[1]->draw; 3519 $self->{children}[1]->draw;
3489} 3520}
3490 3521
3491############################################################################# 3522#############################################################################
3492 3523
3493package CFPlus::UI::Animator; 3524package DC::UI::Animator;
3494 3525
3495use CFPlus::OpenGL; 3526use DC::OpenGL;
3496 3527
3497our @ISA = CFPlus::UI::Bin::; 3528our @ISA = DC::UI::Bin::;
3498 3529
3499sub moveto { 3530sub moveto {
3500 my ($self, $x, $y) = @_; 3531 my ($self, $x, $y) = @_;
3501 3532
3502 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3533 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3530 glPopMatrix; 3561 glPopMatrix;
3531} 3562}
3532 3563
3533############################################################################# 3564#############################################################################
3534 3565
3535package CFPlus::UI::Flopper; 3566package DC::UI::Flopper;
3536 3567
3537our @ISA = CFPlus::UI::Button::; 3568our @ISA = DC::UI::Button::;
3538 3569
3539sub new { 3570sub new {
3540 my $class = shift; 3571 my $class = shift;
3541 3572
3542 my $self = $class->SUPER::new ( 3573 my $self = $class->SUPER::new (
3554 $self->{other}->toggle_visibility; 3585 $self->{other}->toggle_visibility;
3555} 3586}
3556 3587
3557############################################################################# 3588#############################################################################
3558 3589
3559package CFPlus::UI::Tooltip; 3590package DC::UI::Tooltip;
3560 3591
3561our @ISA = CFPlus::UI::Bin::; 3592our @ISA = DC::UI::Bin::;
3562 3593
3563use CFPlus::OpenGL; 3594use DC::OpenGL;
3564 3595
3565sub new { 3596sub new {
3566 my $class = shift; 3597 my $class = shift;
3567 3598
3568 $class->SUPER::new ( 3599 $class->SUPER::new (
3575 my ($self, $widget) = @_; 3606 my ($self, $widget) = @_;
3576 3607
3577 my $tip = $widget->{tooltip}; 3608 my $tip = $widget->{tooltip};
3578 $tip = $tip->($widget) if "CODE" eq ref $tip; 3609 $tip = $tip->($widget) if "CODE" eq ref $tip;
3579 3610
3580 $tip = CFPlus::Pod::section_label tooltip => $1 3611 $tip = DC::Pod::section_label tooltip => $1
3581 if $tip =~ /^#(.*)$/; 3612 if $tip =~ /^#(.*)$/;
3582 3613
3583 if ($ENV{CFPLUS_DEBUG} & 2) { 3614 if ($ENV{CFPLUS_DEBUG} & 2) {
3584 $tip .= "\n\n" . (ref $widget) . "\n" 3615 $tip .= "\n\n" . (ref $widget) . "\n"
3585 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3616 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3588 } 3619 }
3589 3620
3590 $tip =~ s/^\n+//; 3621 $tip =~ s/^\n+//;
3591 $tip =~ s/\n+$//; 3622 $tip =~ s/\n+$//;
3592 3623
3593 $self->add (new CFPlus::UI::Label 3624 $self->add (new DC::UI::Label
3625 fg => $DC::THEME{tooltip_fg},
3594 markup => $tip, 3626 markup => $tip,
3595 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3627 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3628 align => 0,
3596 fontsize => 0.8, 3629 fontsize => 0.8,
3597 style => 1, # FLAG_INVERSE 3630 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3598 ellipsise => 0, 3631 ellipsise => 0,
3599 font => ($widget->{tooltip_font} || $::FONT_PROP), 3632 font => ($widget->{tooltip_font} || $::FONT_PROP),
3600 ); 3633 );
3601} 3634}
3602 3635
3639sub _draw { 3672sub _draw {
3640 my ($self) = @_; 3673 my ($self) = @_;
3641 3674
3642 my ($w, $h) = @$self{qw(w h)}; 3675 my ($w, $h) = @$self{qw(w h)};
3643 3676
3644 glColor 1, 0.8, 0.4; 3677 glColor @{ $DC::THEME{tooltip_bg} };
3645 glRect 0, 0, $w, $h; 3678 glRect 0, 0, $w, $h;
3646 3679
3647 glColor 0, 0, 0; 3680 glColor @{ $DC::THEME{tooltip_border} };
3648 glRect_lineloop .5, .5, $w + .5, $h + .5; 3681 glRect_lineloop .5, .5, $w + .5, $h + .5;
3649 3682
3650 glTranslate 2, 2; 3683 glTranslate 2, 2;
3651 3684
3652 $self->SUPER::_draw; 3685 $self->SUPER::_draw;
3653} 3686}
3654 3687
3655############################################################################# 3688#############################################################################
3656 3689
3657package CFPlus::UI::Face; 3690package DC::UI::Face;
3658 3691
3659our @ISA = CFPlus::UI::DrawBG::; 3692our @ISA = DC::UI::DrawBG::;
3660 3693
3661use CFPlus::OpenGL; 3694use DC::OpenGL;
3662 3695
3663sub new { 3696sub new {
3664 my $class = shift; 3697 my $class = shift;
3665 3698
3666 my $self = $class->SUPER::new ( 3699 my $self = $class->SUPER::new (
3670 can_events => 0, 3703 can_events => 0,
3671 @_, 3704 @_,
3672 ); 3705 );
3673 3706
3674 if ($self->{anim} && $self->{animspeed}) { 3707 if ($self->{anim} && $self->{animspeed}) {
3675 CFPlus::weaken (my $widget = $self); 3708 DC::weaken (my $widget = $self);
3676 3709
3677 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3710 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3678 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3711 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3679 $self->{timer} = Event->timer (
3680 parked => 1,
3681 cb => sub {
3682 return unless $::CONN; 3712 return unless $::CONN;
3683 3713
3684 my $w = $widget 3714 my $w = $widget
3685 or return; 3715 or return;
3686 3716
3687 ++$w->{frame}; 3717 ++$w->{frame};
3688 $w->update_face; 3718 $w->update_face;
3689 3719
3690 # somehow, $widget can go away 3720 # somehow, $widget can go away
3691 $w->update; 3721 $w->update;
3692 $w->update_timer; 3722 $w->update_timer;
3693 },
3694 ); 3723 };
3695 3724
3696 $self->update_face; 3725 $self->update_face;
3697 $self->update_timer; 3726 $self->update_timer;
3698 } 3727 }
3699 3728
3704 my ($self) = @_; 3733 my ($self) = @_;
3705 3734
3706 return unless $self->{timer}; 3735 return unless $self->{timer};
3707 3736
3708 if ($self->{visible}) { 3737 if ($self->{visible}) {
3709 $self->{timer}->at (
3710 $self->{anim_start}
3711 + $self->{animspeed}
3712 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3713 );
3714 $self->{timer}->start; 3738 $self->{timer}->start;
3715 } else { 3739 } else {
3716 $self->{timer}->stop; 3740 $self->{timer}->stop;
3717 } 3741 }
3718} 3742}
3795 $self->SUPER::destroy; 3819 $self->SUPER::destroy;
3796} 3820}
3797 3821
3798############################################################################# 3822#############################################################################
3799 3823
3800package CFPlus::UI::Buttonbar; 3824package DC::UI::Buttonbar;
3801 3825
3802our @ISA = CFPlus::UI::HBox::; 3826our @ISA = DC::UI::HBox::;
3803 3827
3804# TODO: should actually wrap buttons and other goodies. 3828# TODO: should actually wrap buttons and other goodies.
3805 3829
3806############################################################################# 3830#############################################################################
3807 3831
3808package CFPlus::UI::Menu; 3832package DC::UI::Menu;
3809 3833
3810our @ISA = CFPlus::UI::Toplevel::; 3834our @ISA = DC::UI::Toplevel::;
3811 3835
3812use CFPlus::OpenGL; 3836use DC::OpenGL;
3813 3837
3814sub new { 3838sub new {
3815 my $class = shift; 3839 my $class = shift;
3816 3840
3817 my $self = $class->SUPER::new ( 3841 my $self = $class->SUPER::new (
3818 items => [], 3842 items => [],
3819 z => 100, 3843 z => 100,
3820 @_, 3844 @_,
3821 ); 3845 );
3822 3846
3823 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3847 $self->add ($self->{vbox} = new DC::UI::VBox);
3824 3848
3825 for my $item (@{ $self->{items} }) { 3849 for my $item (@{ $self->{items} }) {
3826 my ($widget, $cb, $tooltip) = @$item; 3850 my ($widget, $cb, $tooltip) = @$item;
3827 3851
3828 # handle various types of items, only text for now 3852 # handle various types of items, only text for now
3829 if (!ref $widget) { 3853 if (!ref $widget) {
3830 if ($widget =~ /\t/) { 3854 if ($widget =~ /\t/) {
3831 my ($left, $right) = split /\t/, $widget, 2; 3855 my ($left, $right) = split /\t/, $widget, 2;
3832 3856
3833 $widget = new CFPlus::UI::HBox 3857 $widget = new DC::UI::HBox
3834 can_hover => 1, 3858 can_hover => 1,
3835 can_events => 1, 3859 can_events => 1,
3836 tooltip => $tooltip, 3860 tooltip => $tooltip,
3837 children => [ 3861 children => [
3838 (new CFPlus::UI::Label markup => $left, expand => 1), 3862 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3839 (new CFPlus::UI::Label markup => $right, align => +1), 3863 (new DC::UI::Label markup => $right, align => 1),
3840 ], 3864 ],
3841 ; 3865 ;
3842 3866
3843 } else { 3867 } else {
3844 $widget = new CFPlus::UI::Label 3868 $widget = new DC::UI::Label
3845 can_hover => 1, 3869 can_hover => 1,
3846 can_events => 1, 3870 can_events => 1,
3871 align => 0,
3847 markup => $widget, 3872 markup => $widget,
3848 tooltip => $tooltip; 3873 tooltip => $tooltip;
3849 } 3874 }
3850 } 3875 }
3851 3876
3897 1 3922 1
3898} 3923}
3899 3924
3900############################################################################# 3925#############################################################################
3901 3926
3902package CFPlus::UI::Multiplexer; 3927package DC::UI::Multiplexer;
3903 3928
3904our @ISA = CFPlus::UI::Container::; 3929our @ISA = DC::UI::Container::;
3905 3930
3906sub new { 3931sub new {
3907 my $class = shift; 3932 my $class = shift;
3908 3933
3909 my $self = $class->SUPER::new ( 3934 my $self = $class->SUPER::new (
3970 $self->{current}->draw; 3995 $self->{current}->draw;
3971} 3996}
3972 3997
3973############################################################################# 3998#############################################################################
3974 3999
3975package CFPlus::UI::Notebook; 4000package DC::UI::Notebook;
3976 4001
3977use CFPlus::OpenGL; 4002use DC::OpenGL;
3978 4003
3979our @ISA = CFPlus::UI::VBox::; 4004our @ISA = DC::UI::VBox::;
3980 4005
3981sub new { 4006sub new {
3982 my $class = shift; 4007 my $class = shift;
3983 4008
3984 my $self = $class->SUPER::new ( 4009 my $self = $class->SUPER::new (
3985 buttonbar => (new CFPlus::UI::Buttonbar), 4010 buttonbar => (new DC::UI::Buttonbar),
3986 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4011 multiplexer => (new DC::UI::Multiplexer expand => 1),
3987 active_outline => [.7, .7, 0.2], 4012 active_outline => [.7, .7, 0.2],
3988 # filter => # will be put between multiplexer and $self 4013 # filter => # will be put between multiplexer and $self
3989 @_, 4014 @_,
3990 ); 4015 );
3991 4016
4000 4025
4001 for my $child (@$widgets) { 4026 for my $child (@$widgets) {
4002 Scalar::Util::weaken $child; 4027 Scalar::Util::weaken $child;
4003 $child->{c_tab_} ||= do { 4028 $child->{c_tab_} ||= do {
4004 my $tab = 4029 my $tab =
4005 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4030 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4006 ? $child->{c_tab} 4031 ? $child->{c_tab}
4007 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4032 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4008 4033
4009 $tab->connect (activate => sub { 4034 $tab->connect (activate => sub {
4010 $wself->set_current_page ($child); 4035 $wself->set_current_page ($child);
4011 }); 4036 });
4012 4037
4042} 4067}
4043 4068
4044sub pages { 4069sub pages {
4045 my ($self) = @_; 4070 my ($self) = @_;
4046 $self->{multiplexer}->children 4071 $self->{multiplexer}->children
4072}
4073
4074sub page_index {
4075 my ($self, $widget) = @_;
4076
4077 my $i = 0;
4078 for ($self->pages) {
4079 if ($_ eq $widget) { return $i };
4080 $i++;
4081 }
4082
4083 undef
4047} 4084}
4048 4085
4049sub add_tab { 4086sub add_tab {
4050 my ($self, $title, $widget, $tooltip) = @_; 4087 my ($self, $title, $widget, $tooltip) = @_;
4051 4088
4088 } 4125 }
4089} 4126}
4090 4127
4091############################################################################# 4128#############################################################################
4092 4129
4093package CFPlus::UI::Selector; 4130package DC::UI::Selector;
4094 4131
4095use utf8; 4132use utf8;
4096 4133
4097our @ISA = CFPlus::UI::Button::; 4134our @ISA = DC::UI::Button::;
4098 4135
4099sub new { 4136sub new {
4100 my $class = shift; 4137 my $class = shift;
4101 4138
4102 my $self = $class->SUPER::new ( 4139 my $self = $class->SUPER::new (
4119 my ($value, $title, $tooltip) = @$_; 4156 my ($value, $title, $tooltip) = @$_;
4120 4157
4121 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4158 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4122 } 4159 }
4123 4160
4124 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4161 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4125} 4162}
4126 4163
4127sub _set_value { 4164sub _set_value {
4128 my ($self, $value) = @_; 4165 my ($self, $value) = @_;
4129 4166
4152 $self->_set_value ($self->{value}); 4189 $self->_set_value ($self->{value});
4153} 4190}
4154 4191
4155############################################################################# 4192#############################################################################
4156 4193
4157package CFPlus::UI::Statusbox; 4194package DC::UI::Statusbox;
4158 4195
4159our @ISA = CFPlus::UI::VBox::; 4196our @ISA = DC::UI::VBox::;
4160 4197
4161sub new { 4198sub new {
4162 my $class = shift; 4199 my $class = shift;
4163 4200
4164 my $self = $class->SUPER::new ( 4201 my $self = $class->SUPER::new (
4165 fontsize => 0.8, 4202 fontsize => 0.8,
4166 @_, 4203 @_,
4167 ); 4204 );
4168 4205
4169 CFPlus::weaken (my $this = $self); 4206 DC::weaken (my $this = $self);
4170 4207
4171 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4208 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4172 4209
4173 $self 4210 $self
4174} 4211}
4175 4212
4176sub reorder { 4213sub reorder {
4177 my ($self) = @_; 4214 my ($self) = @_;
4178 my $NOW = Time::HiRes::time; 4215 my $NOW = Time::HiRes::time;
4179 4216
4180 # freeze display when hovering over any label 4217 # freeze display when hovering over any label
4181 return if $CFPlus::UI::TOOLTIP->{owner} 4218 return if $DC::UI::TOOLTIP->{owner}
4182 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4219 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4183 values %{ $self->{item} }; 4220 values %{ $self->{item} };
4184 4221
4185 while (my ($k, $v) = each %{ $self->{item} }) { 4222 while (my ($k, $v) = each %{ $self->{item} }) {
4186 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4223 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4187 } 4224 }
4225
4226 $self->{timer}->set (1, 1);
4188 4227
4189 my @widgets; 4228 my @widgets;
4190 4229
4191 my @items = sort { 4230 my @items = sort {
4192 $a->{pri} <=> $b->{pri} 4231 $a->{pri} <=> $b->{pri}
4193 or $b->{id} <=> $a->{id} 4232 or $b->{id} <=> $a->{id}
4194 } values %{ $self->{item} }; 4233 } values %{ $self->{item} };
4195
4196 $self->{timer}->interval (1);
4197 4234
4198 my $count = 10 + 1; 4235 my $count = 10 + 1;
4199 for my $item (@items) { 4236 for my $item (@items) {
4200 last unless --$count; 4237 last unless --$count;
4201 4238
4208 for ($short) { 4245 for ($short) {
4209 s/^\s+//; 4246 s/^\s+//;
4210 s/\s+/ /g; 4247 s/\s+/ /g;
4211 } 4248 }
4212 4249
4213 new CFPlus::UI::Label 4250 new DC::UI::Label
4214 markup => $short, 4251 markup => $short,
4215 tooltip => $item->{tooltip}, 4252 tooltip => $item->{tooltip},
4216 tooltip_font => $::FONT_PROP, 4253 tooltip_font => $::FONT_PROP,
4217 tooltip_width => 0.67, 4254 tooltip_width => 0.67,
4218 fontsize => $item->{fontsize} || $self->{fontsize}, 4255 fontsize => $item->{fontsize} || $self->{fontsize},
4219 max_w => $::WIDTH * 0.44, 4256 max_w => $::WIDTH * 0.44,
4257 align => 0,
4220 fg => [@{ $item->{fg} }], 4258 fg => [@{ $item->{fg} }],
4221 can_events => 1, 4259 can_events => 1,
4222 can_hover => 1 4260 can_hover => 1
4223 }; 4261 };
4224 4262
4225 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4263 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4226 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4264 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4227 $label->update; 4265 $label->update;
4228 $label->set_max_size (undef, $label->{req_h} * $diff) 4266 $label->set_max_size (undef, $label->{req_h} * $diff)
4229 if $diff < 1; 4267 if $diff < 1;
4230 $self->{timer}->interval (1/30); 4268 $self->{timer}->set (1/30, 1/30);
4231 } else { 4269 } else {
4232 $label->{fg}[3] = $item->{fg}[3] || 1; 4270 $label->{fg}[3] = $item->{fg}[3] || 1;
4233 } 4271 }
4234 4272
4235 push @widgets, $label; 4273 push @widgets, $label;
4297 $self->SUPER::destroy; 4335 $self->SUPER::destroy;
4298} 4336}
4299 4337
4300############################################################################# 4338#############################################################################
4301 4339
4302package CFPlus::UI::Root; 4340package DC::UI::Root;
4303 4341
4304our @ISA = CFPlus::UI::Container::; 4342our @ISA = DC::UI::Container::;
4305 4343
4306use List::Util qw(min max); 4344use List::Util qw(min max);
4307 4345
4308use CFPlus::OpenGL; 4346use DC::OpenGL;
4309 4347
4310sub new { 4348sub new {
4311 my $class = shift; 4349 my $class = shift;
4312 4350
4313 my $self = $class->SUPER::new ( 4351 my $self = $class->SUPER::new (
4314 visible => 1, 4352 visible => 1,
4315 @_, 4353 @_,
4316 ); 4354 );
4317 4355
4318 CFPlus::weaken ($self->{root} = $self); 4356 DC::weaken ($self->{root} = $self);
4319 4357
4320 $self 4358 $self
4321} 4359}
4322 4360
4323sub size_request { 4361sub size_request {
4371} 4409}
4372 4410
4373sub update { 4411sub update {
4374 my ($self) = @_; 4412 my ($self) = @_;
4375 4413
4376 $::WANT_REFRESH->start; 4414 $::WANT_REFRESH = 1;
4377} 4415}
4378 4416
4379sub add { 4417sub add {
4380 my ($self, @children) = @_; 4418 my ($self, @children) = @_;
4381 4419
4448 4486
4449 delete $queue{$widget+0}; 4487 delete $queue{$widget+0};
4450 4488
4451 my ($w, $h) = $widget->size_request; 4489 my ($w, $h) = $widget->size_request;
4452 4490
4453 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4491 $w += $widget->{padding_x} * 2;
4454 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4492 $h += $widget->{padding_y} * 2;
4493
4494 $w = max $widget->{min_w}, $w;
4495 $h = max $widget->{min_h}, $h;
4455 4496
4456 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4497 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4457 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4498 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4458 4499
4459 $w = $widget->{force_w} if exists $widget->{force_w}; 4500 $w = $widget->{force_w} if exists $widget->{force_w};
4526 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4567 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4527 glMatrixMode GL_MODELVIEW; 4568 glMatrixMode GL_MODELVIEW;
4528 glLoadIdentity; 4569 glLoadIdentity;
4529 4570
4530 { 4571 {
4531 package CFPlus::UI::Base; 4572 package DC::UI::Base;
4532 4573
4533 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4574 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4534 (0, 0, $self->{w}, $self->{h}); 4575 (0, 0, $self->{w}, $self->{h});
4535 4576
4536 $self->_draw; 4577 $self->_draw;
4537 } 4578 }
4538} 4579}
4539 4580
4540############################################################################# 4581#############################################################################
4541 4582
4542package CFPlus::UI; 4583package DC::UI;
4543 4584
4544$ROOT = new CFPlus::UI::Root; 4585$ROOT = new DC::UI::Root;
4545$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4586$TOOLTIP = new DC::UI::Tooltip z => 900;
4546 4587
45471 45881
4548 4589

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines