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.439 by root, Sun Sep 16 13:04:36 2007 UTC vs.
Revision 1.454 by root, Wed Dec 26 21:03:21 2007 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
360 $self->{force_h} = $h; 364 $self->{force_h} = $h;
361 365
362 $self->realloc; 366 $self->realloc;
363} 367}
364 368
365# travsle 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
369 return ($self->{alloc_w}, $self->{alloc_h})
370 if $self->{alloc_w} && !$self->{root}{realloc}{$self+0};
371 372
372 return $self->{parent}->get_max_wh 373 return $self->{parent}->get_max_wh
373 if $self->{parent}; 374 if $self->{parent};
374 375
375 ($::WIDTH, $::HEIGHT) 376 ($::WIDTH, $::HEIGHT)
388 my ($self, $x, $y, $w, $h) = @_; 389 my ($self, $x, $y, $w, $h) = @_;
389 390
390 if ($self->{aspect}) { 391 if ($self->{aspect}) {
391 my ($ow, $oh) = ($w, $h); 392 my ($ow, $oh) = ($w, $h);
392 393
393 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 394 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
394 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 395 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
395 396
396 # use alignment to adjust x, y 397 # use alignment to adjust x, y
397 398
398 $x += int 0.5 * ($ow - $w); 399 $x += int 0.5 * ($ow - $w);
399 $y += int 0.5 * ($oh - $h); 400 $y += int 0.5 * ($oh - $h);
440 441
441 return if $self->{tooltip} eq $tooltip; 442 return if $self->{tooltip} eq $tooltip;
442 443
443 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
444 445
445 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($DC::UI::TOOLTIP->{owner} == $self) {
446 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $DC::UI::TOOLTIP->{owner};
447 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $DC::UI::TOOLTIP_WATCHER->invoke;
448 } 449 }
449} 450}
450 451
451# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
452sub coord2local { 453sub coord2local {
453 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
454 455
455 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
456 457
457 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
458} 459}
459 460
460# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
461sub coord2global { 462sub coord2global {
462 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
463 464
464 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
465 466
466 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
467} 468}
468 469
469sub invoke_focus_in { 470sub invoke_focus_in {
518sub connect { 519sub connect {
519 my ($self, $signal, $cb) = @_; 520 my ($self, $signal, $cb) = @_;
520 521
521 push @{ $self->{signal_cb}{$signal} }, $cb; 522 push @{ $self->{signal_cb}{$signal} }, $cb;
522 523
523 defined wantarray and CFPlus::guard { 524 defined wantarray and DC::guard {
524 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
525 @{ $self->{signal_cb}{$signal} }; 526 @{ $self->{signal_cb}{$signal} };
526 } 527 }
527} 528}
528 529
566# in .xs 567# in .xs
567 568
568sub set_parent { 569sub set_parent {
569 my ($self, $parent) = @_; 570 my ($self, $parent) = @_;
570 571
571 CFPlus::weaken ($self->{parent} = $parent); 572 DC::weaken ($self->{parent} = $parent);
572 $self->set_visible if $parent->{visible}; 573 $self->set_visible if $parent->{visible};
573} 574}
574 575
575sub realloc { 576sub realloc {
576 my ($self) = @_; 577 my ($self) = @_;
614} 615}
615 616
616sub DESTROY { 617sub DESTROY {
617 my ($self) = @_; 618 my ($self) = @_;
618 619
619 return if CFPlus::in_destruct; 620 return if DC::in_destruct;
620 621
621 local $@; 622 local $@;
622 eval { $self->destroy }; 623 eval { $self->destroy };
623 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
624 625
625 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
626} 627}
627 628
628############################################################################# 629#############################################################################
629 630
630package CFPlus::UI::DrawBG; 631package DC::UI::DrawBG;
631 632
632our @ISA = CFPlus::UI::Base::; 633our @ISA = DC::UI::Base::;
633 634
634use strict; 635use strict;
635use CFPlus::OpenGL; 636use DC::OpenGL;
636 637
637sub new { 638sub new {
638 my $class = shift; 639 my $class = shift;
639 640
640 $class->SUPER::new ( 641 $class->SUPER::new (
662 } 663 }
663} 664}
664 665
665############################################################################# 666#############################################################################
666 667
667package CFPlus::UI::Empty; 668package DC::UI::Empty;
668 669
669our @ISA = CFPlus::UI::Base::; 670our @ISA = DC::UI::Base::;
670 671
671sub new { 672sub new {
672 my ($class, %arg) = @_; 673 my ($class, %arg) = @_;
673 $class->SUPER::new (can_events => 0, %arg); 674 $class->SUPER::new (can_events => 0, %arg);
674} 675}
681 682
682sub draw { } 683sub draw { }
683 684
684############################################################################# 685#############################################################################
685 686
686package CFPlus::UI::Container; 687package DC::UI::Container;
687 688
688our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
689 690
690sub new { 691sub new {
691 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
692 693
693 my $children = delete $arg{children}; 694 my $children = delete $arg{children};
787 $_->draw for $self->visible_children; 788 $_->draw for $self->visible_children;
788} 789}
789 790
790############################################################################# 791#############################################################################
791 792
792package CFPlus::UI::Bin; 793package DC::UI::Bin;
793 794
794our @ISA = CFPlus::UI::Container::; 795our @ISA = DC::UI::Container::;
795 796
796sub new { 797sub new {
797 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
798 799
799 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 800 my $child = (delete $arg{child}) || new DC::UI::Empty::;
800 801
801 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
802} 803}
803 804
804sub add { 805sub add {
811sub remove { 812sub remove {
812 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
813 814
814 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
815 816
816 $self->{children} = [new CFPlus::UI::Empty] 817 $self->{children} = [new DC::UI::Empty]
817 unless @{$self->{children}}; 818 unless @{$self->{children}};
818} 819}
819 820
820sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
821 822
832} 833}
833 834
834############################################################################# 835#############################################################################
835# back-buffered drawing area 836# back-buffered drawing area
836 837
837package CFPlus::UI::Window; 838package DC::UI::Window;
838 839
839our @ISA = CFPlus::UI::Bin::; 840our @ISA = DC::UI::Bin::;
840 841
841use CFPlus::OpenGL; 842use DC::OpenGL;
842 843
843sub new { 844sub new {
844 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
845 846
846 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
868} 869}
869 870
870sub render_child { 871sub render_child {
871 my ($self) = @_; 872 my ($self) = @_;
872 873
873 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 874 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
874 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
875 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
876 877
877 { 878 {
878 package CFPlus::UI::Base; 879 package DC::UI::Base;
879 880
880 local ($draw_x, $draw_y, $draw_w, $draw_h) = 881 local ($draw_x, $draw_y, $draw_w, $draw_h) =
881 (0, 0, $self->{w}, $self->{h}); 882 (0, 0, $self->{w}, $self->{h});
882 883
883 $self->_render; 884 $self->_render;
900 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
901} 902}
902 903
903############################################################################# 904#############################################################################
904 905
905package CFPlus::UI::ViewPort; 906package DC::UI::ViewPort;
906 907
907use List::Util qw(min max); 908use List::Util qw(min max);
908 909
909our @ISA = CFPlus::UI::Window::; 910our @ISA = DC::UI::Window::;
910 911
911sub new { 912sub new {
912 my $class = shift; 913 my $class = shift;
913 914
914 $class->SUPER::new ( 915 $class->SUPER::new (
998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
999 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1000 ) { 1001 ) {
1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1002 } else { 1003 } else {
1003 $self->CFPlus::UI::Base::find_widget ($x, $y) 1004 $self->DC::UI::Base::find_widget ($x, $y)
1004 } 1005 }
1005} 1006}
1006 1007
1007sub _render { 1008sub _render {
1008 my ($self) = @_; 1009 my ($self) = @_;
1009 1010
1010 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1011 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1011 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1012 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1012 1013
1013 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1014 1015
1015 $self->SUPER::_render; 1016 $self->SUPER::_render;
1016} 1017}
1017 1018
1018############################################################################# 1019#############################################################################
1019 1020
1020package CFPlus::UI::ScrolledWindow; 1021package DC::UI::ScrolledWindow;
1021 1022
1022our @ISA = CFPlus::UI::Table::; 1023our @ISA = DC::UI::Table::;
1023 1024
1024sub new { 1025sub new {
1025 my ($class, %arg) = @_; 1026 my ($class, %arg) = @_;
1026 1027
1027 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1028 1029
1029 my $self; 1030 my $self;
1030 1031
1031 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new DC::UI::Slider
1032 c_col => 0, 1033 c_col => 0,
1033 c_row => 1, 1034 c_row => 1,
1034 vertical => 0, 1035 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub { 1037 on_changed => sub {
1037 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 }, 1040 },
1040 ; 1041 ;
1041 1042
1042 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new DC::UI::Slider
1043 c_col => 1, 1044 c_col => 1,
1044 c_row => 0, 1045 c_row => 0,
1045 vertical => 1, 1046 vertical => 1,
1046 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1047 on_changed => sub { 1048 on_changed => sub {
1059 col_expand => [1, 0], 1060 col_expand => [1, 0],
1060 row_expand => [1, 0], 1061 row_expand => [1, 0],
1061 %arg, 1062 %arg,
1062 ); 1063 );
1063 1064
1064 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new DC::UI::ViewPort
1065 c_col => 0, 1066 c_col => 0,
1066 c_row => 0, 1067 c_row => 0,
1067 expand => 1, 1068 expand => 1,
1068 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1195 $self->SUPER::invoke_size_allocate ($w, $h) 1196 $self->SUPER::invoke_size_allocate ($w, $h)
1196} 1197}
1197 1198
1198############################################################################# 1199#############################################################################
1199 1200
1200package CFPlus::UI::Frame; 1201package DC::UI::Frame;
1201 1202
1202our @ISA = CFPlus::UI::Bin::; 1203our @ISA = DC::UI::Bin::;
1203 1204
1204use CFPlus::OpenGL; 1205use DC::OpenGL;
1205 1206
1206sub new { 1207sub new {
1207 my $class = shift; 1208 my $class = shift;
1208 1209
1209 $class->SUPER::new ( 1210 $class->SUPER::new (
1228 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1229} 1230}
1230 1231
1231############################################################################# 1232#############################################################################
1232 1233
1233package CFPlus::UI::FancyFrame; 1234package DC::UI::FancyFrame;
1234 1235
1235our @ISA = CFPlus::UI::Bin::; 1236our @ISA = DC::UI::Bin::;
1236 1237
1237use CFPlus::OpenGL; 1238use DC::OpenGL;
1238 1239
1239sub new { 1240sub new {
1240 my ($class, %arg) = @_; 1241 my ($class, %arg) = @_;
1241 1242
1242 if ((exists $arg{label}) && !ref $arg{label}) { 1243 if ((exists $arg{label}) && !ref $arg{label}) {
1243 $arg{label} = new CFPlus::UI::Label 1244 $arg{label} = new DC::UI::Label
1244 align => 1, 1245 align => 1,
1245 valign => 0, 1246 valign => 0,
1246 text => $arg{label}, 1247 text => $arg{label},
1247 fontsize => ($arg{border} || 0.8) * 0.75; 1248 fontsize => ($arg{border} || 0.8) * 0.75;
1248 } 1249 }
1260 1261
1261sub add { 1262sub add {
1262 my ($self, @widgets) = @_; 1263 my ($self, @widgets) = @_;
1263 1264
1264 $self->SUPER::add (@widgets); 1265 $self->SUPER::add (@widgets);
1265 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1266 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1266} 1267}
1267 1268
1268sub border { 1269sub border {
1269 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
1270} 1271}
1328 } 1329 }
1329} 1330}
1330 1331
1331############################################################################# 1332#############################################################################
1332 1333
1333package CFPlus::UI::Toplevel; 1334package DC::UI::Toplevel;
1334 1335
1335our @ISA = CFPlus::UI::Bin::; 1336our @ISA = DC::UI::Bin::;
1336 1337
1337use CFPlus::OpenGL; 1338use DC::OpenGL;
1338 1339
1339my $bg = 1340my $bg =
1340 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1341 new_from_file DC::Texture DC::find_rcfile "d1_bg.png",
1341 mipmap => 1, wrap => 1; 1342 mipmap => 1, wrap => 1;
1342 1343
1343my @border = 1344my @border =
1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1345 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1345 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1346 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1346 1347
1347my @icon = 1348my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1349 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png); 1350 qw(x1_move.png x1_resize.png);
1350 1351
1351sub new { 1352sub new {
1352 my ($class, %arg) = @_; 1353 my ($class, %arg) = @_;
1353 1354
1359 min_w => 64, 1360 min_w => 64,
1360 min_h => 32, 1361 min_h => 32,
1361 %arg, 1362 %arg,
1362 ); 1363 );
1363 1364
1364 $self->{title_widget} = new CFPlus::UI::Label 1365 $self->{title_widget} = new DC::UI::Label
1365 align => 0, 1366 align => 0,
1366 valign => 1, 1367 valign => 1,
1367 text => $self->{title}, 1368 text => $self->{title},
1368 fontsize => $self->{border}, 1369 fontsize => $self->{border},
1369 if exists $self->{title}; 1370 if exists $self->{title};
1370 1371
1371 if ($self->{has_close_button}) { 1372 if ($self->{has_close_button}) {
1372 $self->{close_button} = 1373 $self->{close_button} =
1373 new CFPlus::UI::ImageButton 1374 new DC::UI::ImageButton
1374 path => 'x1_close.png', 1375 path => 'x1_close.png',
1375 on_activate => sub { $self->emit ("delete") }; 1376 on_activate => sub { $self->emit ("delete") };
1376 1377
1377 $self->CFPlus::UI::Container::add ($self->{close_button}); 1378 $self->DC::UI::Container::add ($self->{close_button});
1378 } 1379 }
1379 1380
1380 $self 1381 $self
1381} 1382}
1382 1383
1383sub add { 1384sub add {
1384 my ($self, @widgets) = @_; 1385 my ($self, @widgets) = @_;
1385 1386
1386 $self->SUPER::add (@widgets); 1387 $self->SUPER::add (@widgets);
1387 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1388 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1388 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1389 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1389} 1390}
1390 1391
1391sub border { 1392sub border {
1392 int $_[0]{border} * $::FONTSIZE 1393 int $_[0]{border} * $::FONTSIZE
1394}
1395
1396sub get_max_wh {
1397 my ($self) = @_;
1398
1399 return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w};
1401
1402 $self->SUPER::get_max_wh
1393} 1403}
1394 1404
1395sub size_request { 1405sub size_request {
1396 my ($self) = @_; 1406 my ($self) = @_;
1397 1407
1569 if $self->{close_button}; 1579 if $self->{close_button};
1570} 1580}
1571 1581
1572############################################################################# 1582#############################################################################
1573 1583
1574package CFPlus::UI::Table; 1584package DC::UI::Table;
1575 1585
1576our @ISA = CFPlus::UI::Container::; 1586our @ISA = DC::UI::Container::;
1577 1587
1578use List::Util qw(max sum); 1588use List::Util qw(max sum);
1579 1589
1580use CFPlus::OpenGL; 1590use DC::OpenGL;
1581 1591
1582sub new { 1592sub new {
1583 my $class = shift; 1593 my $class = shift;
1584 1594
1585 $class->SUPER::new ( 1595 $class->SUPER::new (
1673 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1674 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1675 1685
1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1677 1687
1678 CFPlus::UI::harmonize $ws; 1688 DC::UI::harmonize $ws;
1679 1689
1680 my @row_expand = @{$self->{row_expand}}; 1690 my @row_expand = @{$self->{row_expand}};
1681 @row_expand = (1) x @$ws unless @row_expand; 1691 @row_expand = (1) x @$ws unless @row_expand;
1682 my $row_expand = (sum @row_expand) || 1; 1692 my $row_expand = (sum @row_expand) || 1;
1683 1693
1684 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1685 1695
1686 CFPlus::UI::harmonize $hs; 1696 DC::UI::harmonize $hs;
1687 1697
1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1690 1700
1691 for my $widget ($self->children) { 1701 for my $widget ($self->children) {
1700 1 1710 1
1701} 1711}
1702 1712
1703############################################################################# 1713#############################################################################
1704 1714
1705package CFPlus::UI::Fixed; 1715package DC::UI::Fixed;
1706 1716
1707use List::Util qw(min max); 1717use List::Util qw(min max);
1708 1718
1709our @ISA = CFPlus::UI::Container::; 1719our @ISA = DC::UI::Container::;
1710 1720
1711sub _scale($$$) { 1721sub _scale($$$) {
1712 my ($rel, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1713 1723
1714 $rel ? $val * $max : $val 1724 $rel ? $val * $max : $val
1766 1 1776 1
1767} 1777}
1768 1778
1769############################################################################# 1779#############################################################################
1770 1780
1771package CFPlus::UI::Box; 1781package DC::UI::Box;
1772 1782
1773our @ISA = CFPlus::UI::Container::; 1783our @ISA = DC::UI::Container::;
1774 1784
1775sub size_request { 1785sub size_request {
1776 my ($self) = @_; 1786 my ($self) = @_;
1777 1787
1778 my @children = $self->visible_children; 1788 my @children = $self->visible_children;
1813 $req[$_] += $space * $children[$_]{expand} 1823 $req[$_] += $space * $children[$_]{expand}
1814 for 0 .. $#children; 1824 for 0 .. $#children;
1815 } 1825 }
1816 } 1826 }
1817 1827
1818 CFPlus::UI::harmonize \@req; 1828 DC::UI::harmonize \@req;
1819 1829
1820 my $pos = 0; 1830 my $pos = 0;
1821 for (0 .. $#children) { 1831 for (0 .. $#children) {
1822 my $alloc = $req[$_]; 1832 my $alloc = $req[$_];
1823 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1828 1 1838 1
1829} 1839}
1830 1840
1831############################################################################# 1841#############################################################################
1832 1842
1833package CFPlus::UI::HBox; 1843package DC::UI::HBox;
1834 1844
1835our @ISA = CFPlus::UI::Box::; 1845our @ISA = DC::UI::Box::;
1836 1846
1837sub new { 1847sub new {
1838 my $class = shift; 1848 my $class = shift;
1839 1849
1840 $class->SUPER::new ( 1850 $class->SUPER::new (
1843 ) 1853 )
1844} 1854}
1845 1855
1846############################################################################# 1856#############################################################################
1847 1857
1848package CFPlus::UI::VBox; 1858package DC::UI::VBox;
1849 1859
1850our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1851 1861
1852sub new { 1862sub new {
1853 my $class = shift; 1863 my $class = shift;
1854 1864
1855 $class->SUPER::new ( 1865 $class->SUPER::new (
1858 ) 1868 )
1859} 1869}
1860 1870
1861############################################################################# 1871#############################################################################
1862 1872
1863package CFPlus::UI::Label; 1873package DC::UI::Label;
1864 1874
1865our @ISA = CFPlus::UI::DrawBG::; 1875our @ISA = DC::UI::DrawBG::;
1866 1876
1867use CFPlus::OpenGL; 1877use DC::OpenGL;
1868 1878
1869sub new { 1879sub new {
1870 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1871 1881
1872 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1877 #text => initial text 1887 #text => initial text
1878 #markup => initial narkup 1888 #markup => initial narkup
1879 #max_w => maximum pixel width 1889 #max_w => maximum pixel width
1880 #style => 0, # render flags 1890 #style => 0, # render flags
1881 ellipsise => 3, # end 1891 ellipsise => 3, # end
1882 layout => (new CFPlus::Layout), 1892 layout => (new DC::Layout),
1883 fontsize => 1, 1893 fontsize => 1,
1884 align => -1, 1894 align => -1,
1885 valign => -1, 1895 valign => -1,
1886 padding_x => 2, 1896 padding_x => 2,
1887 padding_y => 2, 1897 padding_y => 2,
1888 can_events => 0, 1898 can_events => 0,
1889 %arg 1899 %arg
1890 ); 1900 );
1891 1901
1892 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1893 my $layout = new CFPlus::Layout; 1903 my $layout = new DC::Layout;
1894 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1895 $self->{template} = $layout; 1905 $self->{template} = $layout;
1896 } 1906 }
1897 1907
1898 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
2044 2054
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2046 }; 2056 };
2047 2057
2048# unless ($self->{list}) { 2058# unless ($self->{list}) {
2049# $self->{list} = CFPlus::OpenGL::glGenList; 2059# $self->{list} = DC::OpenGL::glGenList;
2050# CFPlus::OpenGL::glNewList $self->{list}; 2060# DC::OpenGL::glNewList $self->{list};
2051# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052# CFPlus::OpenGL::glEndList; 2062# DC::OpenGL::glEndList;
2053# } 2063# }
2054# 2064#
2055# CFPlus::OpenGL::glCallList $self->{list}; 2065# DC::OpenGL::glCallList $self->{list};
2056 2066
2057 $self->{layout}->draw; 2067 $self->{layout}->draw;
2058} 2068}
2059 2069
2060#sub destroy { 2070#sub destroy {
2061# my ($self) = @_; 2071# my ($self) = @_;
2062# 2072#
2063# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2073# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2064# 2074#
2065# $self->SUPER::destroy; 2075# $self->SUPER::destroy;
2066#} 2076#}
2067 2077
2068############################################################################# 2078#############################################################################
2069 2079
2070package CFPlus::UI::EntryBase; 2080package DC::UI::EntryBase;
2071 2081
2072our @ISA = CFPlus::UI::Label::; 2082our @ISA = DC::UI::Label::;
2073 2083
2074use CFPlus::OpenGL; 2084use DC::OpenGL;
2075 2085
2076sub new { 2086sub new {
2077 my $class = shift; 2087 my $class = shift;
2078 2088
2079 $class->SUPER::new ( 2089 $class->SUPER::new (
2146 2156
2147 if ($uni == 8) { 2157 if ($uni == 8) {
2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2149 } elsif ($uni == 127) { 2159 } elsif ($uni == 127) {
2150 substr $text, $self->{cursor}, 1, ""; 2160 substr $text, $self->{cursor}, 1, "";
2151 } elsif ($sym == CFPlus::SDLK_LEFT) { 2161 } elsif ($sym == DC::SDLK_LEFT) {
2152 --$self->{cursor} if $self->{cursor}; 2162 --$self->{cursor} if $self->{cursor};
2153 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2163 } elsif ($sym == DC::SDLK_RIGHT) {
2154 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2164 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2155 } elsif ($sym == CFPlus::SDLK_HOME) { 2165 } elsif ($sym == DC::SDLK_HOME) {
2156 # what a hack 2166 # what a hack
2157 $self->{cursor} = 2167 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1 2169 ? length $1
2160 : 0; 2170 : 0;
2161 } elsif ($sym == CFPlus::SDLK_END) { 2171 } elsif ($sym == DC::SDLK_END) {
2162 # uh, again 2172 # uh, again
2163 $self->{cursor} = 2173 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2165 ? $self->{cursor} + length $1 2175 ? $self->{cursor} + length $1
2166 : length $self->{text}; 2176 : length $self->{text};
2269 } 2279 }
2270} 2280}
2271 2281
2272############################################################################# 2282#############################################################################
2273 2283
2274package CFPlus::UI::Entry; 2284package DC::UI::Entry;
2275 2285
2276our @ISA = CFPlus::UI::EntryBase::; 2286our @ISA = DC::UI::EntryBase::;
2277 2287
2278use CFPlus::OpenGL; 2288use DC::OpenGL;
2279 2289
2280sub invoke_key_down { 2290sub invoke_key_down {
2281 my ($self, $ev) = @_; 2291 my ($self, $ev) = @_;
2282 2292
2283 my $sym = $ev->{sym}; 2293 my $sym = $ev->{sym};
2289 $self->{history_pointer} = -1; 2299 $self->{history_pointer} = -1;
2290 $self->{history_saveback} = ''; 2300 $self->{history_saveback} = '';
2291 $self->emit (activate => $txt); 2301 $self->emit (activate => $txt);
2292 $self->update; 2302 $self->update;
2293 2303
2294 } elsif ($sym == CFPlus::SDLK_UP) { 2304 } elsif ($sym == DC::SDLK_UP) {
2295 if ($self->{history_pointer} < 0) { 2305 if ($self->{history_pointer} < 0) {
2296 $self->{history_saveback} = $self->get_text; 2306 $self->{history_saveback} = $self->get_text;
2297 } 2307 }
2298 if (@{$self->{history} || []} > 0) { 2308 if (@{$self->{history} || []} > 0) {
2299 $self->{history_pointer}++; 2309 $self->{history_pointer}++;
2301 $self->{history_pointer} = @{$self->{history} || []} - 1; 2311 $self->{history_pointer} = @{$self->{history} || []} - 1;
2302 } 2312 }
2303 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2313 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2304 } 2314 }
2305 2315
2306 } elsif ($sym == CFPlus::SDLK_DOWN) { 2316 } elsif ($sym == DC::SDLK_DOWN) {
2307 $self->{history_pointer}--; 2317 $self->{history_pointer}--;
2308 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2309 2319
2310 if ($self->{history_pointer} >= 0) { 2320 if ($self->{history_pointer} >= 0) {
2311 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2321 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2320 1 2330 1
2321} 2331}
2322 2332
2323############################################################################# 2333#############################################################################
2324 2334
2325package CFPlus::UI::TextEdit; 2335package DC::UI::TextEdit;
2326 2336
2327our @ISA = CFPlus::UI::EntryBase::; 2337our @ISA = DC::UI::EntryBase::;
2328 2338
2329use CFPlus::OpenGL; 2339use DC::OpenGL;
2330 2340
2331sub move_cursor_ver { 2341sub move_cursor_ver {
2332 my ($self, $dy) = @_; 2342 my ($self, $dy) = @_;
2333 2343
2334 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2346sub invoke_key_down { 2356sub invoke_key_down {
2347 my ($self, $ev) = @_; 2357 my ($self, $ev) = @_;
2348 2358
2349 my $sym = $ev->{sym}; 2359 my $sym = $ev->{sym};
2350 2360
2351 if ($sym == CFPlus::SDLK_UP) { 2361 if ($sym == DC::SDLK_UP) {
2352 $self->move_cursor_ver (-1); 2362 $self->move_cursor_ver (-1);
2353 } elsif ($sym == CFPlus::SDLK_DOWN) { 2363 } elsif ($sym == DC::SDLK_DOWN) {
2354 $self->move_cursor_ver (+1); 2364 $self->move_cursor_ver (+1);
2355 } else { 2365 } else {
2356 return $self->SUPER::invoke_key_down ($ev) 2366 return $self->SUPER::invoke_key_down ($ev)
2357 } 2367 }
2358 2368
2359 1 2369 1
2360} 2370}
2361 2371
2362############################################################################# 2372#############################################################################
2363 2373
2364package CFPlus::UI::ButtonBin; 2374package DC::UI::ButtonBin;
2365 2375
2366our @ISA = CFPlus::UI::Bin::; 2376our @ISA = DC::UI::Bin::;
2367 2377
2368use CFPlus::OpenGL; 2378use DC::OpenGL;
2369 2379
2370my @tex = 2380my @tex =
2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2381 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2372 qw(b1_button_inactive.png b1_button_active.png); 2382 qw(b1_button_inactive.png b1_button_active.png);
2373 2383
2374sub new { 2384sub new {
2375 my $class = shift; 2385 my $class = shift;
2376 2386
2408 $self->SUPER::_draw; 2418 $self->SUPER::_draw;
2409} 2419}
2410 2420
2411############################################################################# 2421#############################################################################
2412 2422
2413package CFPlus::UI::Button; 2423package DC::UI::Button;
2414 2424
2415our @ISA = CFPlus::UI::Label::; 2425our @ISA = DC::UI::Label::;
2416 2426
2417use CFPlus::OpenGL; 2427use DC::OpenGL;
2418 2428
2419my @tex = 2429my @tex =
2420 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2430 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2421 qw(b1_button_inactive.png b1_button_active.png); 2431 qw(b1_button_inactive.png b1_button_active.png);
2422 2432
2423sub new { 2433sub new {
2424 my $class = shift; 2434 my $class = shift;
2425 2435
2463 $self->SUPER::_draw; 2473 $self->SUPER::_draw;
2464} 2474}
2465 2475
2466############################################################################# 2476#############################################################################
2467 2477
2468package CFPlus::UI::CheckBox; 2478package DC::UI::CheckBox;
2469 2479
2470our @ISA = CFPlus::UI::DrawBG::; 2480our @ISA = DC::UI::DrawBG::;
2471 2481
2472my @tex = 2482my @tex =
2473 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2483 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2474 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2484 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2475 2485
2476use CFPlus::OpenGL; 2486use DC::OpenGL;
2477 2487
2478sub new { 2488sub new {
2479 my $class = shift; 2489 my $class = shift;
2480 2490
2481 $class->SUPER::new ( 2491 $class->SUPER::new (
2538 glDisable GL_TEXTURE_2D; 2548 glDisable GL_TEXTURE_2D;
2539} 2549}
2540 2550
2541############################################################################# 2551#############################################################################
2542 2552
2543package CFPlus::UI::Image; 2553package DC::UI::Image;
2544 2554
2545our @ISA = CFPlus::UI::Base::; 2555our @ISA = DC::UI::Base::;
2546 2556
2547use CFPlus::OpenGL; 2557use DC::OpenGL;
2548 2558
2549our %texture_cache; 2559our %texture_cache;
2550 2560
2551sub new { 2561sub new {
2552 my $class = shift; 2562 my $class = shift;
2559 2569
2560 $self->{path} || $self->{tex} 2570 $self->{path} || $self->{tex}
2561 or Carp::croak "'path' or 'tex' attributes required"; 2571 or Carp::croak "'path' or 'tex' attributes required";
2562 2572
2563 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2573 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2564 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2574 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1;
2565 2575
2566 CFPlus::weaken $texture_cache{$self->{path}}; 2576 DC::weaken $texture_cache{$self->{path}};
2567 2577
2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2569 2579
2570 $self 2580 $self
2571} 2581}
2572 2582
2573sub STORABLE_freeze { 2583sub STORABLE_freeze {
2574 my ($self, $cloning) = @_; 2584 my ($self, $cloning) = @_;
2575 2585
2576 $self->{path} 2586 $self->{path}
2577 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2587 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2578 2588
2579 $self->{path} 2589 $self->{path}
2580} 2590}
2581 2591
2582sub STORABLE_attach { 2592sub STORABLE_attach {
2613 glDisable GL_TEXTURE_2D; 2623 glDisable GL_TEXTURE_2D;
2614} 2624}
2615 2625
2616############################################################################# 2626#############################################################################
2617 2627
2618package CFPlus::UI::ImageButton; 2628package DC::UI::ImageButton;
2619 2629
2620our @ISA = CFPlus::UI::Image::; 2630our @ISA = DC::UI::Image::;
2621 2631
2622use CFPlus::OpenGL; 2632use DC::OpenGL;
2623 2633
2624my %textures; 2634my %textures;
2625 2635
2626sub new { 2636sub new {
2627 my $class = shift; 2637 my $class = shift;
2655 1 2665 1
2656} 2666}
2657 2667
2658############################################################################# 2668#############################################################################
2659 2669
2660package CFPlus::UI::VGauge; 2670package DC::UI::VGauge;
2661 2671
2662our @ISA = CFPlus::UI::Base::; 2672our @ISA = DC::UI::Base::;
2663 2673
2664use List::Util qw(min max); 2674use List::Util qw(min max);
2665 2675
2666use CFPlus::OpenGL; 2676use DC::OpenGL;
2667 2677
2668my %tex = ( 2678my %tex = (
2669 food => [ 2679 food => [
2670 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2680 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2671 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2672 ], 2682 ],
2673 grace => [ 2683 grace => [
2674 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2684 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2675 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2685 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2676 ], 2686 ],
2677 hp => [ 2687 hp => [
2678 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2688 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2679 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2680 ], 2690 ],
2681 mana => [ 2691 mana => [
2682 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2683 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2693 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2684 ], 2694 ],
2685); 2695);
2686 2696
2687# eg. VGauge->new (gauge => 'food'), default gauge: food 2697# eg. VGauge->new (gauge => 'food'), default gauge: food
2790 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
2791} 2801}
2792 2802
2793############################################################################# 2803#############################################################################
2794 2804
2795package CFPlus::UI::Progress; 2805package DC::UI::Progress;
2796 2806
2797our @ISA = CFPlus::UI::Label::; 2807our @ISA = DC::UI::Label::;
2798 2808
2799use CFPlus::OpenGL; 2809use DC::OpenGL;
2800 2810
2801sub new { 2811sub new {
2802 my ($class, %arg) = @_; 2812 my ($class, %arg) = @_;
2803 2813
2804 my $self = $class->SUPER::new ( 2814 my $self = $class->SUPER::new (
2824 my ($self, $label) = @_; 2834 my ($self, $label) = @_;
2825 2835
2826 return if $self->{label} eq $label; 2836 return if $self->{label} eq $label;
2827 $self->{label} = $label; 2837 $self->{label} = $label;
2828 2838
2829 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2839 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2830} 2840}
2831 2841
2832sub set_value { 2842sub set_value {
2833 my ($self, $value) = @_; 2843 my ($self, $value) = @_;
2834 2844
2871 } 2881 }
2872} 2882}
2873 2883
2874############################################################################# 2884#############################################################################
2875 2885
2876package CFPlus::UI::ExperienceProgress; 2886package DC::UI::ExperienceProgress;
2877 2887
2878our @ISA = CFPlus::UI::Progress::; 2888our @ISA = DC::UI::Progress::;
2879 2889
2880sub new { 2890sub new {
2881 my ($class, %arg) = @_; 2891 my ($class, %arg) = @_;
2882 2892
2883 my $self = $class->SUPER::new ( 2893 my $self = $class->SUPER::new (
2928 $self->SUPER::set_value ($v); 2938 $self->SUPER::set_value ($v);
2929} 2939}
2930 2940
2931############################################################################# 2941#############################################################################
2932 2942
2933package CFPlus::UI::Gauge; 2943package DC::UI::Gauge;
2934 2944
2935our @ISA = CFPlus::UI::VBox::; 2945our @ISA = DC::UI::VBox::;
2936 2946
2937sub new { 2947sub new {
2938 my ($class, %arg) = @_; 2948 my ($class, %arg) = @_;
2939 2949
2940 my $self = $class->SUPER::new ( 2950 my $self = $class->SUPER::new (
2942 can_hover => 1, 2952 can_hover => 1,
2943 can_events => 1, 2953 can_events => 1,
2944 %arg, 2954 %arg,
2945 ); 2955 );
2946 2956
2947 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2957 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999");
2948 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2958 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2949 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2959 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999");
2950 2960
2951 $self 2961 $self
2952} 2962}
2953 2963
2954sub set_fontsize { 2964sub set_fontsize {
2975 $self->{value}->set_text ($val); 2985 $self->{value}->set_text ($val);
2976} 2986}
2977 2987
2978############################################################################# 2988#############################################################################
2979 2989
2980package CFPlus::UI::Slider; 2990package DC::UI::Slider;
2981 2991
2982use strict; 2992use strict;
2983 2993
2984use CFPlus::OpenGL; 2994use DC::OpenGL;
2985 2995
2986our @ISA = CFPlus::UI::DrawBG::; 2996our @ISA = DC::UI::DrawBG::;
2987 2997
2988my @tex = 2998my @tex =
2989 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 2999 map { new_from_file DC::Texture DC::find_rcfile $_ }
2990 qw(s1_slider.png s1_slider_bg.png); 3000 qw(s1_slider.png s1_slider_bg.png);
2991 3001
2992sub new { 3002sub new {
2993 my $class = shift; 3003 my $class = shift;
2994 3004
3090sub invoke_mouse_wheel { 3100sub invoke_mouse_wheel {
3091 my ($self, $ev) = @_; 3101 my ($self, $ev) = @_;
3092 3102
3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3094 3104
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3105 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3096 3106
3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3098 3108
3099 1 3109 1
3100} 3110}
3153 glDisable GL_TEXTURE_2D; 3163 glDisable GL_TEXTURE_2D;
3154} 3164}
3155 3165
3156############################################################################# 3166#############################################################################
3157 3167
3158package CFPlus::UI::ValSlider; 3168package DC::UI::ValSlider;
3159 3169
3160our @ISA = CFPlus::UI::HBox::; 3170our @ISA = DC::UI::HBox::;
3161 3171
3162sub new { 3172sub new {
3163 my ($class, %arg) = @_; 3173 my ($class, %arg) = @_;
3164 3174
3165 my $range = delete $arg{range}; 3175 my $range = delete $arg{range};
3166 3176
3167 my $self = $class->SUPER::new ( 3177 my $self = $class->SUPER::new (
3168 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3178 slider => (new DC::UI::Slider expand => 1, range => $range),
3169 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3179 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3170 to_value => sub { shift }, 3180 to_value => sub { shift },
3171 from_value => sub { shift }, 3181 from_value => sub { shift },
3172 %arg, 3182 %arg,
3173 ); 3183 );
3174 3184
3194sub set_range { shift->{slider}->set_range (@_) } 3204sub set_range { shift->{slider}->set_range (@_) }
3195sub set_value { shift->{slider}->set_value (@_) } 3205sub set_value { shift->{slider}->set_value (@_) }
3196 3206
3197############################################################################# 3207#############################################################################
3198 3208
3199package CFPlus::UI::TextScroller; 3209package DC::UI::TextScroller;
3200 3210
3201our @ISA = CFPlus::UI::HBox::; 3211our @ISA = DC::UI::HBox::;
3202 3212
3203use CFPlus::OpenGL; 3213use DC::OpenGL;
3204 3214
3205sub new { 3215sub new {
3206 my $class = shift; 3216 my $class = shift;
3207 3217
3208 my $self = $class->SUPER::new ( 3218 my $self = $class->SUPER::new (
3210 can_events => 1, 3220 can_events => 1,
3211 indent => 0, 3221 indent => 0,
3212 #font => default_font 3222 #font => default_font
3213 @_, 3223 @_,
3214 3224
3215 layout => (new CFPlus::Layout), 3225 layout => (new DC::Layout),
3216 par => [], 3226 par => [],
3217 max_par => 0, 3227 max_par => 0,
3218 height => 0, 3228 height => 0,
3219 children => [ 3229 children => [
3220 (new CFPlus::UI::Empty expand => 1), 3230 (new DC::UI::Empty expand => 1),
3221 (new CFPlus::UI::Slider vertical => 1), 3231 (new DC::UI::Slider vertical => 1),
3222 ], 3232 ],
3223 ); 3233 );
3224 3234
3225 $self->{children}[1]->connect (changed => sub { $self->update }); 3235 $self->{children}[1]->connect (changed => sub { $self->update });
3226 3236
3409 $ROOT->on_post_alloc ($self => sub { 3419 $ROOT->on_post_alloc ($self => sub {
3410 $self->force_uptodate; 3420 $self->force_uptodate;
3411 3421
3412 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3413 3423
3414 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3424 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3415 glClearColor 0, 0, 0, 0; 3425 glClearColor 0, 0, 0, 0;
3416 glClear GL_COLOR_BUFFER_BIT; 3426 glClear GL_COLOR_BUFFER_BIT;
3417 3427
3418 package CFPlus::UI::Base; 3428 package DC::UI::Base;
3419 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3429 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3420 (0, 0, $self->{w}, $self->{h}); 3430 (0, 0, $self->{w}, $self->{h});
3421 3431
3422 my $top = int $self->{children}[1]{range}[0]; 3432 my $top = int $self->{children}[1]{range}[0];
3423 3433
3482 $self->{children}[1]->draw; 3492 $self->{children}[1]->draw;
3483} 3493}
3484 3494
3485############################################################################# 3495#############################################################################
3486 3496
3487package CFPlus::UI::Animator; 3497package DC::UI::Animator;
3488 3498
3489use CFPlus::OpenGL; 3499use DC::OpenGL;
3490 3500
3491our @ISA = CFPlus::UI::Bin::; 3501our @ISA = DC::UI::Bin::;
3492 3502
3493sub moveto { 3503sub moveto {
3494 my ($self, $x, $y) = @_; 3504 my ($self, $x, $y) = @_;
3495 3505
3496 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3524 glPopMatrix; 3534 glPopMatrix;
3525} 3535}
3526 3536
3527############################################################################# 3537#############################################################################
3528 3538
3529package CFPlus::UI::Flopper; 3539package DC::UI::Flopper;
3530 3540
3531our @ISA = CFPlus::UI::Button::; 3541our @ISA = DC::UI::Button::;
3532 3542
3533sub new { 3543sub new {
3534 my $class = shift; 3544 my $class = shift;
3535 3545
3536 my $self = $class->SUPER::new ( 3546 my $self = $class->SUPER::new (
3548 $self->{other}->toggle_visibility; 3558 $self->{other}->toggle_visibility;
3549} 3559}
3550 3560
3551############################################################################# 3561#############################################################################
3552 3562
3553package CFPlus::UI::Tooltip; 3563package DC::UI::Tooltip;
3554 3564
3555our @ISA = CFPlus::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3556 3566
3557use CFPlus::OpenGL; 3567use DC::OpenGL;
3558 3568
3559sub new { 3569sub new {
3560 my $class = shift; 3570 my $class = shift;
3561 3571
3562 $class->SUPER::new ( 3572 $class->SUPER::new (
3569 my ($self, $widget) = @_; 3579 my ($self, $widget) = @_;
3570 3580
3571 my $tip = $widget->{tooltip}; 3581 my $tip = $widget->{tooltip};
3572 $tip = $tip->($widget) if "CODE" eq ref $tip; 3582 $tip = $tip->($widget) if "CODE" eq ref $tip;
3573 3583
3574 $tip = CFPlus::Pod::section_label tooltip => $1 3584 $tip = DC::Pod::section_label tooltip => $1
3575 if $tip =~ /^#(.*)$/; 3585 if $tip =~ /^#(.*)$/;
3576 3586
3577 if ($ENV{CFPLUS_DEBUG} & 2) { 3587 if ($ENV{CFPLUS_DEBUG} & 2) {
3578 $tip .= "\n\n" . (ref $widget) . "\n" 3588 $tip .= "\n\n" . (ref $widget) . "\n"
3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3582 } 3592 }
3583 3593
3584 $tip =~ s/^\n+//; 3594 $tip =~ s/^\n+//;
3585 $tip =~ s/\n+$//; 3595 $tip =~ s/\n+$//;
3586 3596
3587 $self->add (new CFPlus::UI::Label 3597 $self->add (new DC::UI::Label
3588 markup => $tip, 3598 markup => $tip,
3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3590 fontsize => 0.8, 3600 fontsize => 0.8,
3591 style => 1, # FLAG_INVERSE 3601 style => 1, # FLAG_INVERSE
3592 ellipsise => 0, 3602 ellipsise => 0,
3646 $self->SUPER::_draw; 3656 $self->SUPER::_draw;
3647} 3657}
3648 3658
3649############################################################################# 3659#############################################################################
3650 3660
3651package CFPlus::UI::Face; 3661package DC::UI::Face;
3652 3662
3653our @ISA = CFPlus::UI::DrawBG::; 3663our @ISA = DC::UI::DrawBG::;
3654 3664
3655use CFPlus::OpenGL; 3665use DC::OpenGL;
3656 3666
3657sub new { 3667sub new {
3658 my $class = shift; 3668 my $class = shift;
3659 3669
3660 my $self = $class->SUPER::new ( 3670 my $self = $class->SUPER::new (
3664 can_events => 0, 3674 can_events => 0,
3665 @_, 3675 @_,
3666 ); 3676 );
3667 3677
3668 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3669 CFPlus::weaken (my $widget = $self); 3679 DC::weaken (my $widget = $self);
3670 3680
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3673 $self->{timer} = Event->timer (
3674 parked => 1,
3675 cb => sub {
3676 return unless $::CONN; 3683 return unless $::CONN;
3677 3684
3678 my $w = $widget 3685 my $w = $widget
3679 or return; 3686 or return;
3680 3687
3681 ++$w->{frame}; 3688 ++$w->{frame};
3682 $w->update_face; 3689 $w->update_face;
3683 3690
3684 # somehow, $widget can go away 3691 # somehow, $widget can go away
3685 $w->update; 3692 $w->update;
3686 $w->update_timer; 3693 $w->update_timer;
3687 },
3688 ); 3694 };
3689 3695
3690 $self->update_face; 3696 $self->update_face;
3691 $self->update_timer; 3697 $self->update_timer;
3692 } 3698 }
3693 3699
3698 my ($self) = @_; 3704 my ($self) = @_;
3699 3705
3700 return unless $self->{timer}; 3706 return unless $self->{timer};
3701 3707
3702 if ($self->{visible}) { 3708 if ($self->{visible}) {
3703 $self->{timer}->at (
3704 $self->{anim_start}
3705 + $self->{animspeed}
3706 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3707 );
3708 $self->{timer}->start; 3709 $self->{timer}->start;
3709 } else { 3710 } else {
3710 $self->{timer}->stop; 3711 $self->{timer}->stop;
3711 } 3712 }
3712} 3713}
3789 $self->SUPER::destroy; 3790 $self->SUPER::destroy;
3790} 3791}
3791 3792
3792############################################################################# 3793#############################################################################
3793 3794
3794package CFPlus::UI::Buttonbar; 3795package DC::UI::Buttonbar;
3795 3796
3796our @ISA = CFPlus::UI::HBox::; 3797our @ISA = DC::UI::HBox::;
3797 3798
3798# TODO: should actually wrap buttons and other goodies. 3799# TODO: should actually wrap buttons and other goodies.
3799 3800
3800############################################################################# 3801#############################################################################
3801 3802
3802package CFPlus::UI::Menu; 3803package DC::UI::Menu;
3803 3804
3804our @ISA = CFPlus::UI::Toplevel::; 3805our @ISA = DC::UI::Toplevel::;
3805 3806
3806use CFPlus::OpenGL; 3807use DC::OpenGL;
3807 3808
3808sub new { 3809sub new {
3809 my $class = shift; 3810 my $class = shift;
3810 3811
3811 my $self = $class->SUPER::new ( 3812 my $self = $class->SUPER::new (
3812 items => [], 3813 items => [],
3813 z => 100, 3814 z => 100,
3814 @_, 3815 @_,
3815 ); 3816 );
3816 3817
3817 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3818 $self->add ($self->{vbox} = new DC::UI::VBox);
3818 3819
3819 for my $item (@{ $self->{items} }) { 3820 for my $item (@{ $self->{items} }) {
3820 my ($widget, $cb, $tooltip) = @$item; 3821 my ($widget, $cb, $tooltip) = @$item;
3821 3822
3822 # handle various types of items, only text for now 3823 # handle various types of items, only text for now
3823 if (!ref $widget) { 3824 if (!ref $widget) {
3824 if ($widget =~ /\t/) { 3825 if ($widget =~ /\t/) {
3825 my ($left, $right) = split /\t/, $widget, 2; 3826 my ($left, $right) = split /\t/, $widget, 2;
3826 3827
3827 $widget = new CFPlus::UI::HBox 3828 $widget = new DC::UI::HBox
3828 can_hover => 1, 3829 can_hover => 1,
3829 can_events => 1, 3830 can_events => 1,
3830 tooltip => $tooltip, 3831 tooltip => $tooltip,
3831 children => [ 3832 children => [
3832 (new CFPlus::UI::Label markup => $left, expand => 1), 3833 (new DC::UI::Label markup => $left, expand => 1),
3833 (new CFPlus::UI::Label markup => $right, align => +1), 3834 (new DC::UI::Label markup => $right, align => +1),
3834 ], 3835 ],
3835 ; 3836 ;
3836 3837
3837 } else { 3838 } else {
3838 $widget = new CFPlus::UI::Label 3839 $widget = new DC::UI::Label
3839 can_hover => 1, 3840 can_hover => 1,
3840 can_events => 1, 3841 can_events => 1,
3841 markup => $widget, 3842 markup => $widget,
3842 tooltip => $tooltip; 3843 tooltip => $tooltip;
3843 } 3844 }
3891 1 3892 1
3892} 3893}
3893 3894
3894############################################################################# 3895#############################################################################
3895 3896
3896package CFPlus::UI::Multiplexer; 3897package DC::UI::Multiplexer;
3897 3898
3898our @ISA = CFPlus::UI::Container::; 3899our @ISA = DC::UI::Container::;
3899 3900
3900sub new { 3901sub new {
3901 my $class = shift; 3902 my $class = shift;
3902 3903
3903 my $self = $class->SUPER::new ( 3904 my $self = $class->SUPER::new (
3964 $self->{current}->draw; 3965 $self->{current}->draw;
3965} 3966}
3966 3967
3967############################################################################# 3968#############################################################################
3968 3969
3969package CFPlus::UI::Notebook; 3970package DC::UI::Notebook;
3970 3971
3971use CFPlus::OpenGL; 3972use DC::OpenGL;
3972 3973
3973our @ISA = CFPlus::UI::VBox::; 3974our @ISA = DC::UI::VBox::;
3974 3975
3975sub new { 3976sub new {
3976 my $class = shift; 3977 my $class = shift;
3977 3978
3978 my $self = $class->SUPER::new ( 3979 my $self = $class->SUPER::new (
3979 buttonbar => (new CFPlus::UI::Buttonbar), 3980 buttonbar => (new DC::UI::Buttonbar),
3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3981 multiplexer => (new DC::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2], 3982 active_outline => [.7, .7, 0.2],
3982 # filter => # will be put between multiplexer and $self 3983 # filter => # will be put between multiplexer and $self
3983 @_, 3984 @_,
3984 ); 3985 );
3985 3986
3994 3995
3995 for my $child (@$widgets) { 3996 for my $child (@$widgets) {
3996 Scalar::Util::weaken $child; 3997 Scalar::Util::weaken $child;
3997 $child->{c_tab_} ||= do { 3998 $child->{c_tab_} ||= do {
3998 my $tab = 3999 my $tab =
3999 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4000 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4000 ? $child->{c_tab} 4001 ? $child->{c_tab}
4001 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4002 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4002 4003
4003 $tab->connect (activate => sub { 4004 $tab->connect (activate => sub {
4004 $wself->set_current_page ($child); 4005 $wself->set_current_page ($child);
4005 }); 4006 });
4006 4007
4082 } 4083 }
4083} 4084}
4084 4085
4085############################################################################# 4086#############################################################################
4086 4087
4087package CFPlus::UI::Selector; 4088package DC::UI::Selector;
4088 4089
4089use utf8; 4090use utf8;
4090 4091
4091our @ISA = CFPlus::UI::Button::; 4092our @ISA = DC::UI::Button::;
4092 4093
4093sub new { 4094sub new {
4094 my $class = shift; 4095 my $class = shift;
4095 4096
4096 my $self = $class->SUPER::new ( 4097 my $self = $class->SUPER::new (
4113 my ($value, $title, $tooltip) = @$_; 4114 my ($value, $title, $tooltip) = @$_;
4114 4115
4115 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4116 } 4117 }
4117 4118
4118 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4119 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4119} 4120}
4120 4121
4121sub _set_value { 4122sub _set_value {
4122 my ($self, $value) = @_; 4123 my ($self, $value) = @_;
4123 4124
4124 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4125 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4126 $item ||= $self->{options}[0]
4125 or return; 4127 or return;
4126 4128
4127 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
4128 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
4129 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
4130} 4132}
4131 4133
4132sub set_value { 4134sub set_value {
4133 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
4134 4136
4136 4138
4137 $self->_set_value ($value); 4139 $self->_set_value ($value);
4138 $self->emit (changed => $value); 4140 $self->emit (changed => $value);
4139} 4141}
4140 4142
4143sub set_options {
4144 my ($self, $options) = @_;
4145
4146 $self->{options} = $options;
4147 $self->_set_value ($self->{value});
4148}
4149
4141############################################################################# 4150#############################################################################
4142 4151
4143package CFPlus::UI::Statusbox; 4152package DC::UI::Statusbox;
4144 4153
4145our @ISA = CFPlus::UI::VBox::; 4154our @ISA = DC::UI::VBox::;
4146 4155
4147sub new { 4156sub new {
4148 my $class = shift; 4157 my $class = shift;
4149 4158
4150 my $self = $class->SUPER::new ( 4159 my $self = $class->SUPER::new (
4151 fontsize => 0.8, 4160 fontsize => 0.8,
4152 @_, 4161 @_,
4153 ); 4162 );
4154 4163
4155 CFPlus::weaken (my $this = $self); 4164 DC::weaken (my $this = $self);
4156 4165
4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4158 4167
4159 $self 4168 $self
4160} 4169}
4161 4170
4162sub reorder { 4171sub reorder {
4163 my ($self) = @_; 4172 my ($self) = @_;
4164 my $NOW = Time::HiRes::time; 4173 my $NOW = Time::HiRes::time;
4165 4174
4166 # freeze display when hovering over any label 4175 # freeze display when hovering over any label
4167 return if $CFPlus::UI::TOOLTIP->{owner} 4176 return if $DC::UI::TOOLTIP->{owner}
4168 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4177 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4169 values %{ $self->{item} }; 4178 values %{ $self->{item} };
4170 4179
4171 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
4172 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4173 } 4182 }
4183
4184 $self->{timer}->set (1, 1);
4174 4185
4175 my @widgets; 4186 my @widgets;
4176 4187
4177 my @items = sort { 4188 my @items = sort {
4178 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4179 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4180 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4181
4182 $self->{timer}->interval (1);
4183 4192
4184 my $count = 10 + 1; 4193 my $count = 10 + 1;
4185 for my $item (@items) { 4194 for my $item (@items) {
4186 last unless --$count; 4195 last unless --$count;
4187 4196
4194 for ($short) { 4203 for ($short) {
4195 s/^\s+//; 4204 s/^\s+//;
4196 s/\s+/ /g; 4205 s/\s+/ /g;
4197 } 4206 }
4198 4207
4199 new CFPlus::UI::Label 4208 new DC::UI::Label
4200 markup => $short, 4209 markup => $short,
4201 tooltip => $item->{tooltip}, 4210 tooltip => $item->{tooltip},
4202 tooltip_font => $::FONT_PROP, 4211 tooltip_font => $::FONT_PROP,
4203 tooltip_width => 0.67, 4212 tooltip_width => 0.67,
4204 fontsize => $item->{fontsize} || $self->{fontsize}, 4213 fontsize => $item->{fontsize} || $self->{fontsize},
4211 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4212 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4213 $label->update; 4222 $label->update;
4214 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4215 if $diff < 1; 4224 if $diff < 1;
4216 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4217 } else { 4226 } else {
4218 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4219 } 4228 }
4220 4229
4221 push @widgets, $label; 4230 push @widgets, $label;
4283 $self->SUPER::destroy; 4292 $self->SUPER::destroy;
4284} 4293}
4285 4294
4286############################################################################# 4295#############################################################################
4287 4296
4288package CFPlus::UI::Root; 4297package DC::UI::Root;
4289 4298
4290our @ISA = CFPlus::UI::Container::; 4299our @ISA = DC::UI::Container::;
4291 4300
4292use List::Util qw(min max); 4301use List::Util qw(min max);
4293 4302
4294use CFPlus::OpenGL; 4303use DC::OpenGL;
4295 4304
4296sub new { 4305sub new {
4297 my $class = shift; 4306 my $class = shift;
4298 4307
4299 my $self = $class->SUPER::new ( 4308 my $self = $class->SUPER::new (
4300 visible => 1, 4309 visible => 1,
4301 @_, 4310 @_,
4302 ); 4311 );
4303 4312
4304 CFPlus::weaken ($self->{root} = $self); 4313 DC::weaken ($self->{root} = $self);
4305 4314
4306 $self 4315 $self
4307} 4316}
4308 4317
4309sub size_request { 4318sub size_request {
4357} 4366}
4358 4367
4359sub update { 4368sub update {
4360 my ($self) = @_; 4369 my ($self) = @_;
4361 4370
4362 $::WANT_REFRESH->start; 4371 $::WANT_REFRESH = 1;
4363} 4372}
4364 4373
4365sub add { 4374sub add {
4366 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4367 4376
4512 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4513 glMatrixMode GL_MODELVIEW; 4522 glMatrixMode GL_MODELVIEW;
4514 glLoadIdentity; 4523 glLoadIdentity;
4515 4524
4516 { 4525 {
4517 package CFPlus::UI::Base; 4526 package DC::UI::Base;
4518 4527
4519 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4528 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4520 (0, 0, $self->{w}, $self->{h}); 4529 (0, 0, $self->{w}, $self->{h});
4521 4530
4522 $self->_draw; 4531 $self->_draw;
4523 } 4532 }
4524} 4533}
4525 4534
4526############################################################################# 4535#############################################################################
4527 4536
4528package CFPlus::UI; 4537package DC::UI;
4529 4538
4530$ROOT = new CFPlus::UI::Root; 4539$ROOT = new DC::UI::Root;
4531$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4540$TOOLTIP = new DC::UI::Tooltip z => 900;
4532 4541
45331 45421
4534 4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines