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.437 by root, Wed Aug 22 22:36:26 2007 UTC vs.
Revision 1.453 by root, Wed Dec 26 20:46:39 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};
269 $self->emit ("destroy"); 270 $self->emit ("destroy");
270 %$self = (); 271 %$self = ();
271} 272}
272 273
273sub TO_JSON { 274sub TO_JSON {
274 { __w_ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
275} 276}
276 277
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
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
377}
378
365sub size_request { 379sub size_request {
366 require Carp; 380 require Carp;
367 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
368} 382}
369 383
375 my ($self, $x, $y, $w, $h) = @_; 389 my ($self, $x, $y, $w, $h) = @_;
376 390
377 if ($self->{aspect}) { 391 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 392 my ($ow, $oh) = ($w, $h);
379 393
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 394 $w = List::Util::min $w, dc::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 395 $h = List::Util::min $h, dc::ceil $w / $self->{aspect};
382 396
383 # use alignment to adjust x, y 397 # use alignment to adjust x, y
384 398
385 $x += int 0.5 * ($ow - $w); 399 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 400 $y += int 0.5 * ($oh - $h);
427 441
428 return if $self->{tooltip} eq $tooltip; 442 return if $self->{tooltip} eq $tooltip;
429 443
430 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
431 445
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($dc::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $dc::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $dc::UI::TOOLTIP_WATCHER->invoke;
435 } 449 }
436} 450}
437 451
438# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
439sub coord2local { 453sub coord2local {
440 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
441 455
442 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
443 457
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 459}
446 460
447# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
448sub coord2global { 462sub coord2global {
449 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
450 464
451 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
452 466
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 468}
455 469
456sub invoke_focus_in { 470sub invoke_focus_in {
505sub connect { 519sub connect {
506 my ($self, $signal, $cb) = @_; 520 my ($self, $signal, $cb) = @_;
507 521
508 push @{ $self->{signal_cb}{$signal} }, $cb; 522 push @{ $self->{signal_cb}{$signal} }, $cb;
509 523
510 defined wantarray and CFPlus::guard { 524 defined wantarray and dc::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 526 @{ $self->{signal_cb}{$signal} };
513 } 527 }
514} 528}
515 529
553# in .xs 567# in .xs
554 568
555sub set_parent { 569sub set_parent {
556 my ($self, $parent) = @_; 570 my ($self, $parent) = @_;
557 571
558 CFPlus::weaken ($self->{parent} = $parent); 572 dc::weaken ($self->{parent} = $parent);
559 $self->set_visible if $parent->{visible}; 573 $self->set_visible if $parent->{visible};
560} 574}
561 575
562sub realloc { 576sub realloc {
563 my ($self) = @_; 577 my ($self) = @_;
601} 615}
602 616
603sub DESTROY { 617sub DESTROY {
604 my ($self) = @_; 618 my ($self) = @_;
605 619
606 return if CFPlus::in_destruct; 620 return if dc::in_destruct;
607 621
608 local $@; 622 local $@;
609 eval { $self->destroy }; 623 eval { $self->destroy };
610 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
611 625
612 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
613} 627}
614 628
615############################################################################# 629#############################################################################
616 630
617package CFPlus::UI::DrawBG; 631package dc::UI::DrawBG;
618 632
619our @ISA = CFPlus::UI::Base::; 633our @ISA = dc::UI::Base::;
620 634
621use strict; 635use strict;
622use CFPlus::OpenGL; 636use dc::OpenGL;
623 637
624sub new { 638sub new {
625 my $class = shift; 639 my $class = shift;
626 640
627 $class->SUPER::new ( 641 $class->SUPER::new (
649 } 663 }
650} 664}
651 665
652############################################################################# 666#############################################################################
653 667
654package CFPlus::UI::Empty; 668package dc::UI::Empty;
655 669
656our @ISA = CFPlus::UI::Base::; 670our @ISA = dc::UI::Base::;
657 671
658sub new { 672sub new {
659 my ($class, %arg) = @_; 673 my ($class, %arg) = @_;
660 $class->SUPER::new (can_events => 0, %arg); 674 $class->SUPER::new (can_events => 0, %arg);
661} 675}
668 682
669sub draw { } 683sub draw { }
670 684
671############################################################################# 685#############################################################################
672 686
673package CFPlus::UI::Container; 687package dc::UI::Container;
674 688
675our @ISA = CFPlus::UI::Base::; 689our @ISA = dc::UI::Base::;
676 690
677sub new { 691sub new {
678 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
679 693
680 my $children = delete $arg{children}; 694 my $children = delete $arg{children};
774 $_->draw for $self->visible_children; 788 $_->draw for $self->visible_children;
775} 789}
776 790
777############################################################################# 791#############################################################################
778 792
779package CFPlus::UI::Bin; 793package dc::UI::Bin;
780 794
781our @ISA = CFPlus::UI::Container::; 795our @ISA = dc::UI::Container::;
782 796
783sub new { 797sub new {
784 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
785 799
786 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 800 my $child = (delete $arg{child}) || new dc::UI::Empty::;
787 801
788 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
789} 803}
790 804
791sub add { 805sub add {
798sub remove { 812sub remove {
799 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
800 814
801 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
802 816
803 $self->{children} = [new CFPlus::UI::Empty] 817 $self->{children} = [new dc::UI::Empty]
804 unless @{$self->{children}}; 818 unless @{$self->{children}};
805} 819}
806 820
807sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
808 822
817 831
818 1 832 1
819} 833}
820 834
821############################################################################# 835#############################################################################
822
823# back-buffered drawing area 836# back-buffered drawing area
824 837
825package CFPlus::UI::Window; 838package dc::UI::Window;
826 839
827our @ISA = CFPlus::UI::Bin::; 840our @ISA = dc::UI::Bin::;
828 841
829use CFPlus::OpenGL; 842use dc::OpenGL;
830 843
831sub new { 844sub new {
832 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
833 846
834 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
856} 869}
857 870
858sub render_child { 871sub render_child {
859 my ($self) = @_; 872 my ($self) = @_;
860 873
861 $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 {
862 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
863 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
864 877
865 { 878 {
866 package CFPlus::UI::Base; 879 package dc::UI::Base;
867 880
868 local ($draw_x, $draw_y, $draw_w, $draw_h) = 881 local ($draw_x, $draw_y, $draw_w, $draw_h) =
869 (0, 0, $self->{w}, $self->{h}); 882 (0, 0, $self->{w}, $self->{h});
870 883
871 $self->_render; 884 $self->_render;
888 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
889} 902}
890 903
891############################################################################# 904#############################################################################
892 905
893package CFPlus::UI::ViewPort; 906package dc::UI::ViewPort;
894 907
895use List::Util qw(min max); 908use List::Util qw(min max);
896 909
897our @ISA = CFPlus::UI::Window::; 910our @ISA = dc::UI::Window::;
898 911
899sub new { 912sub new {
900 my $class = shift; 913 my $class = shift;
901 914
902 $class->SUPER::new ( 915 $class->SUPER::new (
986 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
987 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
988 ) { 1001 ) {
989 $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})
990 } else { 1003 } else {
991 $self->CFPlus::UI::Base::find_widget ($x, $y) 1004 $self->dc::UI::Base::find_widget ($x, $y)
992 } 1005 }
993} 1006}
994 1007
995sub _render { 1008sub _render {
996 my ($self) = @_; 1009 my ($self) = @_;
997 1010
998 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};
999 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};
1000 1013
1001 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1002 1015
1003 $self->SUPER::_render; 1016 $self->SUPER::_render;
1004} 1017}
1005 1018
1006############################################################################# 1019#############################################################################
1007 1020
1008package CFPlus::UI::ScrolledWindow; 1021package dc::UI::ScrolledWindow;
1009 1022
1010our @ISA = CFPlus::UI::Table::; 1023our @ISA = dc::UI::Table::;
1011 1024
1012sub new { 1025sub new {
1013 my ($class, %arg) = @_; 1026 my ($class, %arg) = @_;
1014 1027
1015 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1016 1029
1017 my $self; 1030 my $self;
1018 1031
1019 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new dc::UI::Slider
1020 c_col => 0, 1033 c_col => 0,
1021 c_row => 1, 1034 c_row => 1,
1022 vertical => 0, 1035 vertical => 0,
1023 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1037 on_changed => sub {
1025 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1027 }, 1040 },
1028 ; 1041 ;
1029 1042
1030 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new dc::UI::Slider
1031 c_col => 1, 1044 c_col => 1,
1032 c_row => 0, 1045 c_row => 0,
1033 vertical => 1, 1046 vertical => 1,
1034 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1035 on_changed => sub { 1048 on_changed => sub {
1047 col_expand => [1, 0], 1060 col_expand => [1, 0],
1048 row_expand => [1, 0], 1061 row_expand => [1, 0],
1049 %arg, 1062 %arg,
1050 ); 1063 );
1051 1064
1052 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new dc::UI::ViewPort
1053 c_col => 0, 1066 c_col => 0,
1054 c_row => 0, 1067 c_row => 0,
1055 expand => 1, 1068 expand => 1,
1056 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1057 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1183 $self->SUPER::invoke_size_allocate ($w, $h) 1196 $self->SUPER::invoke_size_allocate ($w, $h)
1184} 1197}
1185 1198
1186############################################################################# 1199#############################################################################
1187 1200
1188package CFPlus::UI::Frame; 1201package dc::UI::Frame;
1189 1202
1190our @ISA = CFPlus::UI::Bin::; 1203our @ISA = dc::UI::Bin::;
1191 1204
1192use CFPlus::OpenGL; 1205use dc::OpenGL;
1193 1206
1194sub new { 1207sub new {
1195 my $class = shift; 1208 my $class = shift;
1196 1209
1197 $class->SUPER::new ( 1210 $class->SUPER::new (
1216 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1217} 1230}
1218 1231
1219############################################################################# 1232#############################################################################
1220 1233
1221package CFPlus::UI::FancyFrame; 1234package dc::UI::FancyFrame;
1222 1235
1223our @ISA = CFPlus::UI::Bin::; 1236our @ISA = dc::UI::Bin::;
1224 1237
1225use CFPlus::OpenGL; 1238use dc::OpenGL;
1226 1239
1227sub new { 1240sub new {
1228 my ($class, %arg) = @_; 1241 my ($class, %arg) = @_;
1229 1242
1230 if ((exists $arg{label}) && !ref $arg{label}) { 1243 if ((exists $arg{label}) && !ref $arg{label}) {
1231 $arg{label} = new CFPlus::UI::Label 1244 $arg{label} = new dc::UI::Label
1232 align => 1, 1245 align => 1,
1233 valign => 0, 1246 valign => 0,
1234 text => $arg{label}, 1247 text => $arg{label},
1235 fontsize => ($arg{border} || 0.8) * 0.75; 1248 fontsize => ($arg{border} || 0.8) * 0.75;
1236 } 1249 }
1248 1261
1249sub add { 1262sub add {
1250 my ($self, @widgets) = @_; 1263 my ($self, @widgets) = @_;
1251 1264
1252 $self->SUPER::add (@widgets); 1265 $self->SUPER::add (@widgets);
1253 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label};
1254} 1267}
1255 1268
1256sub border { 1269sub border {
1257 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
1258} 1271}
1316 } 1329 }
1317} 1330}
1318 1331
1319############################################################################# 1332#############################################################################
1320 1333
1321package CFPlus::UI::Toplevel; 1334package dc::UI::Toplevel;
1322 1335
1323our @ISA = CFPlus::UI::Bin::; 1336our @ISA = dc::UI::Bin::;
1324 1337
1325use CFPlus::OpenGL; 1338use dc::OpenGL;
1326 1339
1327my $bg = 1340my $bg =
1328 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1341 new_from_file dc::Texture dc::find_rcfile "d1_bg.png",
1329 mipmap => 1, wrap => 1; 1342 mipmap => 1, wrap => 1;
1330 1343
1331my @border = 1344my @border =
1332 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1345 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1333 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);
1334 1347
1335my @icon = 1348my @icon =
1336 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1349 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
1337 qw(x1_move.png x1_resize.png); 1350 qw(x1_move.png x1_resize.png);
1338 1351
1339sub new { 1352sub new {
1340 my ($class, %arg) = @_; 1353 my ($class, %arg) = @_;
1341 1354
1347 min_w => 64, 1360 min_w => 64,
1348 min_h => 32, 1361 min_h => 32,
1349 %arg, 1362 %arg,
1350 ); 1363 );
1351 1364
1352 $self->{title_widget} = new CFPlus::UI::Label 1365 $self->{title_widget} = new dc::UI::Label
1353 align => 0, 1366 align => 0,
1354 valign => 1, 1367 valign => 1,
1355 text => $self->{title}, 1368 text => $self->{title},
1356 fontsize => $self->{border}, 1369 fontsize => $self->{border},
1357 if exists $self->{title}; 1370 if exists $self->{title};
1358 1371
1359 if ($self->{has_close_button}) { 1372 if ($self->{has_close_button}) {
1360 $self->{close_button} = 1373 $self->{close_button} =
1361 new CFPlus::UI::ImageButton 1374 new dc::UI::ImageButton
1362 path => 'x1_close.png', 1375 path => 'x1_close.png',
1363 on_activate => sub { $self->emit ("delete") }; 1376 on_activate => sub { $self->emit ("delete") };
1364 1377
1365 $self->CFPlus::UI::Container::add ($self->{close_button}); 1378 $self->dc::UI::Container::add ($self->{close_button});
1366 } 1379 }
1367 1380
1368 $self 1381 $self
1369} 1382}
1370 1383
1371sub add { 1384sub add {
1372 my ($self, @widgets) = @_; 1385 my ($self, @widgets) = @_;
1373 1386
1374 $self->SUPER::add (@widgets); 1387 $self->SUPER::add (@widgets);
1375 $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};
1376 $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};
1377} 1390}
1378 1391
1379sub border { 1392sub border {
1380 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
1381} 1403}
1382 1404
1383sub size_request { 1405sub size_request {
1384 my ($self) = @_; 1406 my ($self) = @_;
1385 1407
1557 if $self->{close_button}; 1579 if $self->{close_button};
1558} 1580}
1559 1581
1560############################################################################# 1582#############################################################################
1561 1583
1562package CFPlus::UI::Table; 1584package dc::UI::Table;
1563 1585
1564our @ISA = CFPlus::UI::Container::; 1586our @ISA = dc::UI::Container::;
1565 1587
1566use List::Util qw(max sum); 1588use List::Util qw(max sum);
1567 1589
1568use CFPlus::OpenGL; 1590use dc::OpenGL;
1569 1591
1570sub new { 1592sub new {
1571 my $class = shift; 1593 my $class = shift;
1572 1594
1573 $class->SUPER::new ( 1595 $class->SUPER::new (
1661 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1662 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1663 1685
1664 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1665 1687
1666 CFPlus::UI::harmonize $ws; 1688 dc::UI::harmonize $ws;
1667 1689
1668 my @row_expand = @{$self->{row_expand}}; 1690 my @row_expand = @{$self->{row_expand}};
1669 @row_expand = (1) x @$ws unless @row_expand; 1691 @row_expand = (1) x @$ws unless @row_expand;
1670 my $row_expand = (sum @row_expand) || 1; 1692 my $row_expand = (sum @row_expand) || 1;
1671 1693
1672 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1673 1695
1674 CFPlus::UI::harmonize $hs; 1696 dc::UI::harmonize $hs;
1675 1697
1676 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1677 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1678 1700
1679 for my $widget ($self->children) { 1701 for my $widget ($self->children) {
1688 1 1710 1
1689} 1711}
1690 1712
1691############################################################################# 1713#############################################################################
1692 1714
1693package CFPlus::UI::Fixed; 1715package dc::UI::Fixed;
1694 1716
1695use List::Util qw(min max); 1717use List::Util qw(min max);
1696 1718
1697our @ISA = CFPlus::UI::Container::; 1719our @ISA = dc::UI::Container::;
1698 1720
1699sub _scale($$$) { 1721sub _scale($$$) {
1700 my ($rel, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1701 1723
1702 $rel ? $val * $max : $val 1724 $rel ? $val * $max : $val
1754 1 1776 1
1755} 1777}
1756 1778
1757############################################################################# 1779#############################################################################
1758 1780
1759package CFPlus::UI::Box; 1781package dc::UI::Box;
1760 1782
1761our @ISA = CFPlus::UI::Container::; 1783our @ISA = dc::UI::Container::;
1762 1784
1763sub size_request { 1785sub size_request {
1764 my ($self) = @_; 1786 my ($self) = @_;
1765 1787
1766 my @children = $self->visible_children; 1788 my @children = $self->visible_children;
1801 $req[$_] += $space * $children[$_]{expand} 1823 $req[$_] += $space * $children[$_]{expand}
1802 for 0 .. $#children; 1824 for 0 .. $#children;
1803 } 1825 }
1804 } 1826 }
1805 1827
1806 CFPlus::UI::harmonize \@req; 1828 dc::UI::harmonize \@req;
1807 1829
1808 my $pos = 0; 1830 my $pos = 0;
1809 for (0 .. $#children) { 1831 for (0 .. $#children) {
1810 my $alloc = $req[$_]; 1832 my $alloc = $req[$_];
1811 $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));
1816 1 1838 1
1817} 1839}
1818 1840
1819############################################################################# 1841#############################################################################
1820 1842
1821package CFPlus::UI::HBox; 1843package dc::UI::HBox;
1822 1844
1823our @ISA = CFPlus::UI::Box::; 1845our @ISA = dc::UI::Box::;
1824 1846
1825sub new { 1847sub new {
1826 my $class = shift; 1848 my $class = shift;
1827 1849
1828 $class->SUPER::new ( 1850 $class->SUPER::new (
1831 ) 1853 )
1832} 1854}
1833 1855
1834############################################################################# 1856#############################################################################
1835 1857
1836package CFPlus::UI::VBox; 1858package dc::UI::VBox;
1837 1859
1838our @ISA = CFPlus::UI::Box::; 1860our @ISA = dc::UI::Box::;
1839 1861
1840sub new { 1862sub new {
1841 my $class = shift; 1863 my $class = shift;
1842 1864
1843 $class->SUPER::new ( 1865 $class->SUPER::new (
1846 ) 1868 )
1847} 1869}
1848 1870
1849############################################################################# 1871#############################################################################
1850 1872
1851package CFPlus::UI::Label; 1873package dc::UI::Label;
1852 1874
1853our @ISA = CFPlus::UI::DrawBG::; 1875our @ISA = dc::UI::DrawBG::;
1854 1876
1855use CFPlus::OpenGL; 1877use dc::OpenGL;
1856 1878
1857sub new { 1879sub new {
1858 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1859 1881
1860 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1865 #text => initial text 1887 #text => initial text
1866 #markup => initial narkup 1888 #markup => initial narkup
1867 #max_w => maximum pixel width 1889 #max_w => maximum pixel width
1868 #style => 0, # render flags 1890 #style => 0, # render flags
1869 ellipsise => 3, # end 1891 ellipsise => 3, # end
1870 layout => (new CFPlus::Layout), 1892 layout => (new dc::Layout),
1871 fontsize => 1, 1893 fontsize => 1,
1872 align => -1, 1894 align => -1,
1873 valign => -1, 1895 valign => -1,
1874 padding_x => 2, 1896 padding_x => 2,
1875 padding_y => 2, 1897 padding_y => 2,
1876 can_events => 0, 1898 can_events => 0,
1877 %arg 1899 %arg
1878 ); 1900 );
1879 1901
1880 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1881 my $layout = new CFPlus::Layout; 1903 my $layout = new dc::Layout;
1882 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1883 $self->{template} = $layout; 1905 $self->{template} = $layout;
1884 } 1906 }
1885 1907
1886 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
1942 1964
1943sub size_request { 1965sub size_request {
1944 my ($self) = @_; 1966 my ($self) = @_;
1945 1967
1946 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1976
1953 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2030 2054
2031 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2032 }; 2056 };
2033 2057
2034# unless ($self->{list}) { 2058# unless ($self->{list}) {
2035# $self->{list} = CFPlus::OpenGL::glGenList; 2059# $self->{list} = dc::OpenGL::glGenList;
2036# CFPlus::OpenGL::glNewList $self->{list}; 2060# dc::OpenGL::glNewList $self->{list};
2037# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2038# CFPlus::OpenGL::glEndList; 2062# dc::OpenGL::glEndList;
2039# } 2063# }
2040# 2064#
2041# CFPlus::OpenGL::glCallList $self->{list}; 2065# dc::OpenGL::glCallList $self->{list};
2042 2066
2043 $self->{layout}->draw; 2067 $self->{layout}->draw;
2044} 2068}
2045 2069
2046#sub destroy { 2070#sub destroy {
2047# my ($self) = @_; 2071# my ($self) = @_;
2048# 2072#
2049# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2073# dc::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2050# 2074#
2051# $self->SUPER::destroy; 2075# $self->SUPER::destroy;
2052#} 2076#}
2053 2077
2054############################################################################# 2078#############################################################################
2055 2079
2056package CFPlus::UI::EntryBase; 2080package dc::UI::EntryBase;
2057 2081
2058our @ISA = CFPlus::UI::Label::; 2082our @ISA = dc::UI::Label::;
2059 2083
2060use CFPlus::OpenGL; 2084use dc::OpenGL;
2061 2085
2062sub new { 2086sub new {
2063 my $class = shift; 2087 my $class = shift;
2064 2088
2065 $class->SUPER::new ( 2089 $class->SUPER::new (
2132 2156
2133 if ($uni == 8) { 2157 if ($uni == 8) {
2134 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2135 } elsif ($uni == 127) { 2159 } elsif ($uni == 127) {
2136 substr $text, $self->{cursor}, 1, ""; 2160 substr $text, $self->{cursor}, 1, "";
2137 } elsif ($sym == CFPlus::SDLK_LEFT) { 2161 } elsif ($sym == dc::SDLK_LEFT) {
2138 --$self->{cursor} if $self->{cursor}; 2162 --$self->{cursor} if $self->{cursor};
2139 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2163 } elsif ($sym == dc::SDLK_RIGHT) {
2140 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2164 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2141 } elsif ($sym == CFPlus::SDLK_HOME) { 2165 } elsif ($sym == dc::SDLK_HOME) {
2142 # what a hack 2166 # what a hack
2143 $self->{cursor} = 2167 $self->{cursor} =
2144 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2145 ? length $1 2169 ? length $1
2146 : 0; 2170 : 0;
2147 } elsif ($sym == CFPlus::SDLK_END) { 2171 } elsif ($sym == dc::SDLK_END) {
2148 # uh, again 2172 # uh, again
2149 $self->{cursor} = 2173 $self->{cursor} =
2150 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2151 ? $self->{cursor} + length $1 2175 ? $self->{cursor} + length $1
2152 : length $self->{text}; 2176 : length $self->{text};
2255 } 2279 }
2256} 2280}
2257 2281
2258############################################################################# 2282#############################################################################
2259 2283
2260package CFPlus::UI::Entry; 2284package dc::UI::Entry;
2261 2285
2262our @ISA = CFPlus::UI::EntryBase::; 2286our @ISA = dc::UI::EntryBase::;
2263 2287
2264use CFPlus::OpenGL; 2288use dc::OpenGL;
2265 2289
2266sub invoke_key_down { 2290sub invoke_key_down {
2267 my ($self, $ev) = @_; 2291 my ($self, $ev) = @_;
2268 2292
2269 my $sym = $ev->{sym}; 2293 my $sym = $ev->{sym};
2275 $self->{history_pointer} = -1; 2299 $self->{history_pointer} = -1;
2276 $self->{history_saveback} = ''; 2300 $self->{history_saveback} = '';
2277 $self->emit (activate => $txt); 2301 $self->emit (activate => $txt);
2278 $self->update; 2302 $self->update;
2279 2303
2280 } elsif ($sym == CFPlus::SDLK_UP) { 2304 } elsif ($sym == dc::SDLK_UP) {
2281 if ($self->{history_pointer} < 0) { 2305 if ($self->{history_pointer} < 0) {
2282 $self->{history_saveback} = $self->get_text; 2306 $self->{history_saveback} = $self->get_text;
2283 } 2307 }
2284 if (@{$self->{history} || []} > 0) { 2308 if (@{$self->{history} || []} > 0) {
2285 $self->{history_pointer}++; 2309 $self->{history_pointer}++;
2287 $self->{history_pointer} = @{$self->{history} || []} - 1; 2311 $self->{history_pointer} = @{$self->{history} || []} - 1;
2288 } 2312 }
2289 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2313 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2290 } 2314 }
2291 2315
2292 } elsif ($sym == CFPlus::SDLK_DOWN) { 2316 } elsif ($sym == dc::SDLK_DOWN) {
2293 $self->{history_pointer}--; 2317 $self->{history_pointer}--;
2294 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2295 2319
2296 if ($self->{history_pointer} >= 0) { 2320 if ($self->{history_pointer} >= 0) {
2297 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2321 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2306 1 2330 1
2307} 2331}
2308 2332
2309############################################################################# 2333#############################################################################
2310 2334
2311package CFPlus::UI::TextEdit; 2335package dc::UI::TextEdit;
2312 2336
2313our @ISA = CFPlus::UI::EntryBase::; 2337our @ISA = dc::UI::EntryBase::;
2314 2338
2315use CFPlus::OpenGL; 2339use dc::OpenGL;
2316 2340
2317sub move_cursor_ver { 2341sub move_cursor_ver {
2318 my ($self, $dy) = @_; 2342 my ($self, $dy) = @_;
2319 2343
2320 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2332sub invoke_key_down { 2356sub invoke_key_down {
2333 my ($self, $ev) = @_; 2357 my ($self, $ev) = @_;
2334 2358
2335 my $sym = $ev->{sym}; 2359 my $sym = $ev->{sym};
2336 2360
2337 if ($sym == CFPlus::SDLK_UP) { 2361 if ($sym == dc::SDLK_UP) {
2338 $self->move_cursor_ver (-1); 2362 $self->move_cursor_ver (-1);
2339 } elsif ($sym == CFPlus::SDLK_DOWN) { 2363 } elsif ($sym == dc::SDLK_DOWN) {
2340 $self->move_cursor_ver (+1); 2364 $self->move_cursor_ver (+1);
2341 } else { 2365 } else {
2342 return $self->SUPER::invoke_key_down ($ev) 2366 return $self->SUPER::invoke_key_down ($ev)
2343 } 2367 }
2344 2368
2345 1 2369 1
2346} 2370}
2347 2371
2348############################################################################# 2372#############################################################################
2349 2373
2350package CFPlus::UI::ButtonBin; 2374package dc::UI::ButtonBin;
2351 2375
2352our @ISA = CFPlus::UI::Bin::; 2376our @ISA = dc::UI::Bin::;
2353 2377
2354use CFPlus::OpenGL; 2378use dc::OpenGL;
2355 2379
2356my @tex = 2380my @tex =
2357 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2358 qw(b1_button_inactive.png b1_button_active.png); 2382 qw(b1_button_inactive.png b1_button_active.png);
2359 2383
2360sub new { 2384sub new {
2361 my $class = shift; 2385 my $class = shift;
2362 2386
2394 $self->SUPER::_draw; 2418 $self->SUPER::_draw;
2395} 2419}
2396 2420
2397############################################################################# 2421#############################################################################
2398 2422
2399package CFPlus::UI::Button; 2423package dc::UI::Button;
2400 2424
2401our @ISA = CFPlus::UI::Label::; 2425our @ISA = dc::UI::Label::;
2402 2426
2403use CFPlus::OpenGL; 2427use dc::OpenGL;
2404 2428
2405my @tex = 2429my @tex =
2406 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2430 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2407 qw(b1_button_inactive.png b1_button_active.png); 2431 qw(b1_button_inactive.png b1_button_active.png);
2408 2432
2409sub new { 2433sub new {
2410 my $class = shift; 2434 my $class = shift;
2411 2435
2449 $self->SUPER::_draw; 2473 $self->SUPER::_draw;
2450} 2474}
2451 2475
2452############################################################################# 2476#############################################################################
2453 2477
2454package CFPlus::UI::CheckBox; 2478package dc::UI::CheckBox;
2455 2479
2456our @ISA = CFPlus::UI::DrawBG::; 2480our @ISA = dc::UI::DrawBG::;
2457 2481
2458my @tex = 2482my @tex =
2459 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2483 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2460 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2484 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2461 2485
2462use CFPlus::OpenGL; 2486use dc::OpenGL;
2463 2487
2464sub new { 2488sub new {
2465 my $class = shift; 2489 my $class = shift;
2466 2490
2467 $class->SUPER::new ( 2491 $class->SUPER::new (
2524 glDisable GL_TEXTURE_2D; 2548 glDisable GL_TEXTURE_2D;
2525} 2549}
2526 2550
2527############################################################################# 2551#############################################################################
2528 2552
2529package CFPlus::UI::Image; 2553package dc::UI::Image;
2530 2554
2531our @ISA = CFPlus::UI::Base::; 2555our @ISA = dc::UI::Base::;
2532 2556
2533use CFPlus::OpenGL; 2557use dc::OpenGL;
2534 2558
2535our %texture_cache; 2559our %texture_cache;
2536 2560
2537sub new { 2561sub new {
2538 my $class = shift; 2562 my $class = shift;
2545 2569
2546 $self->{path} || $self->{tex} 2570 $self->{path} || $self->{tex}
2547 or Carp::croak "'path' or 'tex' attributes required"; 2571 or Carp::croak "'path' or 'tex' attributes required";
2548 2572
2549 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2573 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2550 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;
2551 2575
2552 CFPlus::weaken $texture_cache{$self->{path}}; 2576 dc::weaken $texture_cache{$self->{path}};
2553 2577
2554 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2555 2579
2556 $self 2580 $self
2557} 2581}
2558 2582
2559sub STORABLE_freeze { 2583sub STORABLE_freeze {
2560 my ($self, $cloning) = @_; 2584 my ($self, $cloning) = @_;
2561 2585
2562 $self->{path} 2586 $self->{path}
2563 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";
2564 2588
2565 $self->{path} 2589 $self->{path}
2566} 2590}
2567 2591
2568sub STORABLE_attach { 2592sub STORABLE_attach {
2599 glDisable GL_TEXTURE_2D; 2623 glDisable GL_TEXTURE_2D;
2600} 2624}
2601 2625
2602############################################################################# 2626#############################################################################
2603 2627
2604package CFPlus::UI::ImageButton; 2628package dc::UI::ImageButton;
2605 2629
2606our @ISA = CFPlus::UI::Image::; 2630our @ISA = dc::UI::Image::;
2607 2631
2608use CFPlus::OpenGL; 2632use dc::OpenGL;
2609 2633
2610my %textures; 2634my %textures;
2611 2635
2612sub new { 2636sub new {
2613 my $class = shift; 2637 my $class = shift;
2641 1 2665 1
2642} 2666}
2643 2667
2644############################################################################# 2668#############################################################################
2645 2669
2646package CFPlus::UI::VGauge; 2670package dc::UI::VGauge;
2647 2671
2648our @ISA = CFPlus::UI::Base::; 2672our @ISA = dc::UI::Base::;
2649 2673
2650use List::Util qw(min max); 2674use List::Util qw(min max);
2651 2675
2652use CFPlus::OpenGL; 2676use dc::OpenGL;
2653 2677
2654my %tex = ( 2678my %tex = (
2655 food => [ 2679 food => [
2656 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2657 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2658 ], 2682 ],
2659 grace => [ 2683 grace => [
2660 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2661 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/
2662 ], 2686 ],
2663 hp => [ 2687 hp => [
2664 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2665 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2666 ], 2690 ],
2667 mana => [ 2691 mana => [
2668 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 }
2669 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/
2670 ], 2694 ],
2671); 2695);
2672 2696
2673# eg. VGauge->new (gauge => 'food'), default gauge: food 2697# eg. VGauge->new (gauge => 'food'), default gauge: food
2776 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
2777} 2801}
2778 2802
2779############################################################################# 2803#############################################################################
2780 2804
2781package CFPlus::UI::Progress; 2805package dc::UI::Progress;
2782 2806
2783our @ISA = CFPlus::UI::Label::; 2807our @ISA = dc::UI::Label::;
2784 2808
2785use CFPlus::OpenGL; 2809use dc::OpenGL;
2786 2810
2787sub new { 2811sub new {
2788 my ($class, %arg) = @_; 2812 my ($class, %arg) = @_;
2789 2813
2790 my $self = $class->SUPER::new ( 2814 my $self = $class->SUPER::new (
2810 my ($self, $label) = @_; 2834 my ($self, $label) = @_;
2811 2835
2812 return if $self->{label} eq $label; 2836 return if $self->{label} eq $label;
2813 $self->{label} = $label; 2837 $self->{label} = $label;
2814 2838
2815 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2839 $self->dc::UI::Progress::set_value (0 + delete $self->{value});
2816} 2840}
2817 2841
2818sub set_value { 2842sub set_value {
2819 my ($self, $value) = @_; 2843 my ($self, $value) = @_;
2820 2844
2857 } 2881 }
2858} 2882}
2859 2883
2860############################################################################# 2884#############################################################################
2861 2885
2862package CFPlus::UI::ExperienceProgress; 2886package dc::UI::ExperienceProgress;
2863 2887
2864our @ISA = CFPlus::UI::Progress::; 2888our @ISA = dc::UI::Progress::;
2865 2889
2866sub new { 2890sub new {
2867 my ($class, %arg) = @_; 2891 my ($class, %arg) = @_;
2868 2892
2869 my $self = $class->SUPER::new ( 2893 my $self = $class->SUPER::new (
2914 $self->SUPER::set_value ($v); 2938 $self->SUPER::set_value ($v);
2915} 2939}
2916 2940
2917############################################################################# 2941#############################################################################
2918 2942
2919package CFPlus::UI::Gauge; 2943package dc::UI::Gauge;
2920 2944
2921our @ISA = CFPlus::UI::VBox::; 2945our @ISA = dc::UI::VBox::;
2922 2946
2923sub new { 2947sub new {
2924 my ($class, %arg) = @_; 2948 my ($class, %arg) = @_;
2925 2949
2926 my $self = $class->SUPER::new ( 2950 my $self = $class->SUPER::new (
2928 can_hover => 1, 2952 can_hover => 1,
2929 can_events => 1, 2953 can_events => 1,
2930 %arg, 2954 %arg,
2931 ); 2955 );
2932 2956
2933 $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");
2934 $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);
2935 $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");
2936 2960
2937 $self 2961 $self
2938} 2962}
2939 2963
2940sub set_fontsize { 2964sub set_fontsize {
2961 $self->{value}->set_text ($val); 2985 $self->{value}->set_text ($val);
2962} 2986}
2963 2987
2964############################################################################# 2988#############################################################################
2965 2989
2966package CFPlus::UI::Slider; 2990package dc::UI::Slider;
2967 2991
2968use strict; 2992use strict;
2969 2993
2970use CFPlus::OpenGL; 2994use dc::OpenGL;
2971 2995
2972our @ISA = CFPlus::UI::DrawBG::; 2996our @ISA = dc::UI::DrawBG::;
2973 2997
2974my @tex = 2998my @tex =
2975 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 2999 map { new_from_file dc::Texture dc::find_rcfile $_ }
2976 qw(s1_slider.png s1_slider_bg.png); 3000 qw(s1_slider.png s1_slider_bg.png);
2977 3001
2978sub new { 3002sub new {
2979 my $class = shift; 3003 my $class = shift;
2980 3004
3076sub invoke_mouse_wheel { 3100sub invoke_mouse_wheel {
3077 my ($self, $ev) = @_; 3101 my ($self, $ev) = @_;
3078 3102
3079 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3080 3104
3081 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3105 my $pagepart = $ev->{mod} & dc::KMOD_SHIFT ? 1 : 0.2;
3082 3106
3083 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3084 3108
3085 1 3109 1
3086} 3110}
3139 glDisable GL_TEXTURE_2D; 3163 glDisable GL_TEXTURE_2D;
3140} 3164}
3141 3165
3142############################################################################# 3166#############################################################################
3143 3167
3144package CFPlus::UI::ValSlider; 3168package dc::UI::ValSlider;
3145 3169
3146our @ISA = CFPlus::UI::HBox::; 3170our @ISA = dc::UI::HBox::;
3147 3171
3148sub new { 3172sub new {
3149 my ($class, %arg) = @_; 3173 my ($class, %arg) = @_;
3150 3174
3151 my $range = delete $arg{range}; 3175 my $range = delete $arg{range};
3152 3176
3153 my $self = $class->SUPER::new ( 3177 my $self = $class->SUPER::new (
3154 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3178 slider => (new dc::UI::Slider expand => 1, range => $range),
3155 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3179 entry => (new dc::UI::Label text => "", template => delete $arg{template}),
3156 to_value => sub { shift }, 3180 to_value => sub { shift },
3157 from_value => sub { shift }, 3181 from_value => sub { shift },
3158 %arg, 3182 %arg,
3159 ); 3183 );
3160 3184
3180sub set_range { shift->{slider}->set_range (@_) } 3204sub set_range { shift->{slider}->set_range (@_) }
3181sub set_value { shift->{slider}->set_value (@_) } 3205sub set_value { shift->{slider}->set_value (@_) }
3182 3206
3183############################################################################# 3207#############################################################################
3184 3208
3185package CFPlus::UI::TextScroller; 3209package dc::UI::TextScroller;
3186 3210
3187our @ISA = CFPlus::UI::HBox::; 3211our @ISA = dc::UI::HBox::;
3188 3212
3189use CFPlus::OpenGL; 3213use dc::OpenGL;
3190 3214
3191sub new { 3215sub new {
3192 my $class = shift; 3216 my $class = shift;
3193 3217
3194 my $self = $class->SUPER::new ( 3218 my $self = $class->SUPER::new (
3196 can_events => 1, 3220 can_events => 1,
3197 indent => 0, 3221 indent => 0,
3198 #font => default_font 3222 #font => default_font
3199 @_, 3223 @_,
3200 3224
3201 layout => (new CFPlus::Layout), 3225 layout => (new dc::Layout),
3202 par => [], 3226 par => [],
3203 max_par => 0, 3227 max_par => 0,
3204 height => 0, 3228 height => 0,
3205 children => [ 3229 children => [
3206 (new CFPlus::UI::Empty expand => 1), 3230 (new dc::UI::Empty expand => 1),
3207 (new CFPlus::UI::Slider vertical => 1), 3231 (new dc::UI::Slider vertical => 1),
3208 ], 3232 ],
3209 ); 3233 );
3210 3234
3211 $self->{children}[1]->connect (changed => sub { $self->update }); 3235 $self->{children}[1]->connect (changed => sub { $self->update });
3212 3236
3395 $ROOT->on_post_alloc ($self => sub { 3419 $ROOT->on_post_alloc ($self => sub {
3396 $self->force_uptodate; 3420 $self->force_uptodate;
3397 3421
3398 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3399 3423
3400 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub {
3401 glClearColor 0, 0, 0, 0; 3425 glClearColor 0, 0, 0, 0;
3402 glClear GL_COLOR_BUFFER_BIT; 3426 glClear GL_COLOR_BUFFER_BIT;
3403 3427
3404 package CFPlus::UI::Base; 3428 package dc::UI::Base;
3405 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3429 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3406 (0, 0, $self->{w}, $self->{h}); 3430 (0, 0, $self->{w}, $self->{h});
3407 3431
3408 my $top = int $self->{children}[1]{range}[0]; 3432 my $top = int $self->{children}[1]{range}[0];
3409 3433
3468 $self->{children}[1]->draw; 3492 $self->{children}[1]->draw;
3469} 3493}
3470 3494
3471############################################################################# 3495#############################################################################
3472 3496
3473package CFPlus::UI::Animator; 3497package dc::UI::Animator;
3474 3498
3475use CFPlus::OpenGL; 3499use dc::OpenGL;
3476 3500
3477our @ISA = CFPlus::UI::Bin::; 3501our @ISA = dc::UI::Bin::;
3478 3502
3479sub moveto { 3503sub moveto {
3480 my ($self, $x, $y) = @_; 3504 my ($self, $x, $y) = @_;
3481 3505
3482 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3510 glPopMatrix; 3534 glPopMatrix;
3511} 3535}
3512 3536
3513############################################################################# 3537#############################################################################
3514 3538
3515package CFPlus::UI::Flopper; 3539package dc::UI::Flopper;
3516 3540
3517our @ISA = CFPlus::UI::Button::; 3541our @ISA = dc::UI::Button::;
3518 3542
3519sub new { 3543sub new {
3520 my $class = shift; 3544 my $class = shift;
3521 3545
3522 my $self = $class->SUPER::new ( 3546 my $self = $class->SUPER::new (
3534 $self->{other}->toggle_visibility; 3558 $self->{other}->toggle_visibility;
3535} 3559}
3536 3560
3537############################################################################# 3561#############################################################################
3538 3562
3539package CFPlus::UI::Tooltip; 3563package dc::UI::Tooltip;
3540 3564
3541our @ISA = CFPlus::UI::Bin::; 3565our @ISA = dc::UI::Bin::;
3542 3566
3543use CFPlus::OpenGL; 3567use dc::OpenGL;
3544 3568
3545sub new { 3569sub new {
3546 my $class = shift; 3570 my $class = shift;
3547 3571
3548 $class->SUPER::new ( 3572 $class->SUPER::new (
3555 my ($self, $widget) = @_; 3579 my ($self, $widget) = @_;
3556 3580
3557 my $tip = $widget->{tooltip}; 3581 my $tip = $widget->{tooltip};
3558 $tip = $tip->($widget) if "CODE" eq ref $tip; 3582 $tip = $tip->($widget) if "CODE" eq ref $tip;
3559 3583
3560 $tip = CFPlus::Pod::section_label tooltip => $1 3584 $tip = dc::Pod::section_label tooltip => $1
3561 if $tip =~ /^#(.*)$/; 3585 if $tip =~ /^#(.*)$/;
3562 3586
3563 if ($ENV{CFPLUS_DEBUG} & 2) { 3587 if ($ENV{CFPLUS_DEBUG} & 2) {
3564 $tip .= "\n\n" . (ref $widget) . "\n" 3588 $tip .= "\n\n" . (ref $widget) . "\n"
3565 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3568 } 3592 }
3569 3593
3570 $tip =~ s/^\n+//; 3594 $tip =~ s/^\n+//;
3571 $tip =~ s/\n+$//; 3595 $tip =~ s/\n+$//;
3572 3596
3573 $self->add (new CFPlus::UI::Label 3597 $self->add (new dc::UI::Label
3574 markup => $tip, 3598 markup => $tip,
3575 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3576 fontsize => 0.8, 3600 fontsize => 0.8,
3577 style => 1, # FLAG_INVERSE 3601 style => 1, # FLAG_INVERSE
3578 ellipsise => 0, 3602 ellipsise => 0,
3632 $self->SUPER::_draw; 3656 $self->SUPER::_draw;
3633} 3657}
3634 3658
3635############################################################################# 3659#############################################################################
3636 3660
3637package CFPlus::UI::Face; 3661package dc::UI::Face;
3638 3662
3639our @ISA = CFPlus::UI::DrawBG::; 3663our @ISA = dc::UI::DrawBG::;
3640 3664
3641use CFPlus::OpenGL; 3665use dc::OpenGL;
3642 3666
3643sub new { 3667sub new {
3644 my $class = shift; 3668 my $class = shift;
3645 3669
3646 my $self = $class->SUPER::new ( 3670 my $self = $class->SUPER::new (
3650 can_events => 0, 3674 can_events => 0,
3651 @_, 3675 @_,
3652 ); 3676 );
3653 3677
3654 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3655 CFPlus::weaken (my $widget = $self); 3679 dc::weaken (my $widget = $self);
3656 3680
3657 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3658 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3659 $self->{timer} = Event->timer (
3660 parked => 1,
3661 cb => sub {
3662 return unless $::CONN; 3683 return unless $::CONN;
3663 3684
3664 my $w = $widget 3685 my $w = $widget
3665 or return; 3686 or return;
3666 3687
3667 ++$w->{frame}; 3688 ++$w->{frame};
3668 $w->update_face; 3689 $w->update_face;
3669 3690
3670 # somehow, $widget can go away 3691 # somehow, $widget can go away
3671 $w->update; 3692 $w->update;
3672 $w->update_timer; 3693 $w->update_timer;
3673 },
3674 ); 3694 };
3675 3695
3676 $self->update_face; 3696 $self->update_face;
3677 $self->update_timer; 3697 $self->update_timer;
3678 } 3698 }
3679 3699
3684 my ($self) = @_; 3704 my ($self) = @_;
3685 3705
3686 return unless $self->{timer}; 3706 return unless $self->{timer};
3687 3707
3688 if ($self->{visible}) { 3708 if ($self->{visible}) {
3689 $self->{timer}->at (
3690 $self->{anim_start}
3691 + $self->{animspeed}
3692 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3693 );
3694 $self->{timer}->start; 3709 $self->{timer}->start;
3695 } else { 3710 } else {
3696 $self->{timer}->stop; 3711 $self->{timer}->stop;
3697 } 3712 }
3698} 3713}
3775 $self->SUPER::destroy; 3790 $self->SUPER::destroy;
3776} 3791}
3777 3792
3778############################################################################# 3793#############################################################################
3779 3794
3780package CFPlus::UI::Buttonbar; 3795package dc::UI::Buttonbar;
3781 3796
3782our @ISA = CFPlus::UI::HBox::; 3797our @ISA = dc::UI::HBox::;
3783 3798
3784# TODO: should actually wrap buttons and other goodies. 3799# TODO: should actually wrap buttons and other goodies.
3785 3800
3786############################################################################# 3801#############################################################################
3787 3802
3788package CFPlus::UI::Menu; 3803package dc::UI::Menu;
3789 3804
3790our @ISA = CFPlus::UI::Toplevel::; 3805our @ISA = dc::UI::Toplevel::;
3791 3806
3792use CFPlus::OpenGL; 3807use dc::OpenGL;
3793 3808
3794sub new { 3809sub new {
3795 my $class = shift; 3810 my $class = shift;
3796 3811
3797 my $self = $class->SUPER::new ( 3812 my $self = $class->SUPER::new (
3798 items => [], 3813 items => [],
3799 z => 100, 3814 z => 100,
3800 @_, 3815 @_,
3801 ); 3816 );
3802 3817
3803 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3818 $self->add ($self->{vbox} = new dc::UI::VBox);
3804 3819
3805 for my $item (@{ $self->{items} }) { 3820 for my $item (@{ $self->{items} }) {
3806 my ($widget, $cb, $tooltip) = @$item; 3821 my ($widget, $cb, $tooltip) = @$item;
3807 3822
3808 # handle various types of items, only text for now 3823 # handle various types of items, only text for now
3809 if (!ref $widget) { 3824 if (!ref $widget) {
3810 if ($widget =~ /\t/) { 3825 if ($widget =~ /\t/) {
3811 my ($left, $right) = split /\t/, $widget, 2; 3826 my ($left, $right) = split /\t/, $widget, 2;
3812 3827
3813 $widget = new CFPlus::UI::HBox 3828 $widget = new dc::UI::HBox
3814 can_hover => 1, 3829 can_hover => 1,
3815 can_events => 1, 3830 can_events => 1,
3816 tooltip => $tooltip, 3831 tooltip => $tooltip,
3817 children => [ 3832 children => [
3818 (new CFPlus::UI::Label markup => $left, expand => 1), 3833 (new dc::UI::Label markup => $left, expand => 1),
3819 (new CFPlus::UI::Label markup => $right, align => +1), 3834 (new dc::UI::Label markup => $right, align => +1),
3820 ], 3835 ],
3821 ; 3836 ;
3822 3837
3823 } else { 3838 } else {
3824 $widget = new CFPlus::UI::Label 3839 $widget = new dc::UI::Label
3825 can_hover => 1, 3840 can_hover => 1,
3826 can_events => 1, 3841 can_events => 1,
3827 markup => $widget, 3842 markup => $widget,
3828 tooltip => $tooltip; 3843 tooltip => $tooltip;
3829 } 3844 }
3877 1 3892 1
3878} 3893}
3879 3894
3880############################################################################# 3895#############################################################################
3881 3896
3882package CFPlus::UI::Multiplexer; 3897package dc::UI::Multiplexer;
3883 3898
3884our @ISA = CFPlus::UI::Container::; 3899our @ISA = dc::UI::Container::;
3885 3900
3886sub new { 3901sub new {
3887 my $class = shift; 3902 my $class = shift;
3888 3903
3889 my $self = $class->SUPER::new ( 3904 my $self = $class->SUPER::new (
3950 $self->{current}->draw; 3965 $self->{current}->draw;
3951} 3966}
3952 3967
3953############################################################################# 3968#############################################################################
3954 3969
3955package CFPlus::UI::Notebook; 3970package dc::UI::Notebook;
3956 3971
3957use CFPlus::OpenGL; 3972use dc::OpenGL;
3958 3973
3959our @ISA = CFPlus::UI::VBox::; 3974our @ISA = dc::UI::VBox::;
3960 3975
3961sub new { 3976sub new {
3962 my $class = shift; 3977 my $class = shift;
3963 3978
3964 my $self = $class->SUPER::new ( 3979 my $self = $class->SUPER::new (
3965 buttonbar => (new CFPlus::UI::Buttonbar), 3980 buttonbar => (new dc::UI::Buttonbar),
3966 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3981 multiplexer => (new dc::UI::Multiplexer expand => 1),
3967 active_outline => [.7, .7, 0.2], 3982 active_outline => [.7, .7, 0.2],
3968 # filter => # will be put between multiplexer and $self 3983 # filter => # will be put between multiplexer and $self
3969 @_, 3984 @_,
3970 ); 3985 );
3971 3986
3980 3995
3981 for my $child (@$widgets) { 3996 for my $child (@$widgets) {
3982 Scalar::Util::weaken $child; 3997 Scalar::Util::weaken $child;
3983 $child->{c_tab_} ||= do { 3998 $child->{c_tab_} ||= do {
3984 my $tab = 3999 my $tab =
3985 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4000 (UNIVERSAL::isa $child->{c_tab}, "dc::UI::Base")
3986 ? $child->{c_tab} 4001 ? $child->{c_tab}
3987 : 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];
3988 4003
3989 $tab->connect (activate => sub { 4004 $tab->connect (activate => sub {
3990 $wself->set_current_page ($child); 4005 $wself->set_current_page ($child);
3991 }); 4006 });
3992 4007
4068 } 4083 }
4069} 4084}
4070 4085
4071############################################################################# 4086#############################################################################
4072 4087
4073package CFPlus::UI::Selector; 4088package dc::UI::Selector;
4074 4089
4075use utf8; 4090use utf8;
4076 4091
4077our @ISA = CFPlus::UI::Button::; 4092our @ISA = dc::UI::Button::;
4078 4093
4079sub new { 4094sub new {
4080 my $class = shift; 4095 my $class = shift;
4081 4096
4082 my $self = $class->SUPER::new ( 4097 my $self = $class->SUPER::new (
4099 my ($value, $title, $tooltip) = @$_; 4114 my ($value, $title, $tooltip) = @$_;
4100 4115
4101 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4102 } 4117 }
4103 4118
4104 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4119 dc::UI::Menu->new (items => \@menu_items)->popup ($ev);
4105} 4120}
4106 4121
4107sub _set_value { 4122sub _set_value {
4108 my ($self, $value) = @_; 4123 my ($self, $value) = @_;
4109 4124
4110 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4125 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4126 $item ||= $self->{options}[0]
4111 or return; 4127 or return;
4112 4128
4113 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
4114 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
4115 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
4116} 4132}
4117 4133
4118sub set_value { 4134sub set_value {
4119 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
4120 4136
4122 4138
4123 $self->_set_value ($value); 4139 $self->_set_value ($value);
4124 $self->emit (changed => $value); 4140 $self->emit (changed => $value);
4125} 4141}
4126 4142
4143sub set_options {
4144 my ($self, $options) = @_;
4145
4146 $self->{options} = $options;
4147 $self->_set_value ($self->{value});
4148}
4149
4127############################################################################# 4150#############################################################################
4128 4151
4129package CFPlus::UI::Statusbox; 4152package dc::UI::Statusbox;
4130 4153
4131our @ISA = CFPlus::UI::VBox::; 4154our @ISA = dc::UI::VBox::;
4132 4155
4133sub new { 4156sub new {
4134 my $class = shift; 4157 my $class = shift;
4135 4158
4136 my $self = $class->SUPER::new ( 4159 my $self = $class->SUPER::new (
4137 fontsize => 0.8, 4160 fontsize => 0.8,
4138 @_, 4161 @_,
4139 ); 4162 );
4140 4163
4141 CFPlus::weaken (my $this = $self); 4164 dc::weaken (my $this = $self);
4142 4165
4143 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4144 4167
4145 $self 4168 $self
4146} 4169}
4147 4170
4148sub reorder { 4171sub reorder {
4149 my ($self) = @_; 4172 my ($self) = @_;
4150 my $NOW = Time::HiRes::time; 4173 my $NOW = Time::HiRes::time;
4151 4174
4152 # freeze display when hovering over any label 4175 # freeze display when hovering over any label
4153 return if $CFPlus::UI::TOOLTIP->{owner} 4176 return if $dc::UI::TOOLTIP->{owner}
4154 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4177 && grep $dc::UI::TOOLTIP->{owner} == $_->{label},
4155 values %{ $self->{item} }; 4178 values %{ $self->{item} };
4156 4179
4157 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
4158 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4159 } 4182 }
4183
4184 $self->{timer}->set (1, 1);
4160 4185
4161 my @widgets; 4186 my @widgets;
4162 4187
4163 my @items = sort { 4188 my @items = sort {
4164 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4165 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4166 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4167
4168 $self->{timer}->interval (1);
4169 4192
4170 my $count = 10 + 1; 4193 my $count = 10 + 1;
4171 for my $item (@items) { 4194 for my $item (@items) {
4172 last unless --$count; 4195 last unless --$count;
4173 4196
4180 for ($short) { 4203 for ($short) {
4181 s/^\s+//; 4204 s/^\s+//;
4182 s/\s+/ /g; 4205 s/\s+/ /g;
4183 } 4206 }
4184 4207
4185 new CFPlus::UI::Label 4208 new dc::UI::Label
4186 markup => $short, 4209 markup => $short,
4187 tooltip => $item->{tooltip}, 4210 tooltip => $item->{tooltip},
4188 tooltip_font => $::FONT_PROP, 4211 tooltip_font => $::FONT_PROP,
4189 tooltip_width => 0.67, 4212 tooltip_width => 0.67,
4190 fontsize => $item->{fontsize} || $self->{fontsize}, 4213 fontsize => $item->{fontsize} || $self->{fontsize},
4197 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4198 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4199 $label->update; 4222 $label->update;
4200 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4201 if $diff < 1; 4224 if $diff < 1;
4202 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4203 } else { 4226 } else {
4204 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4205 } 4228 }
4206 4229
4207 push @widgets, $label; 4230 push @widgets, $label;
4269 $self->SUPER::destroy; 4292 $self->SUPER::destroy;
4270} 4293}
4271 4294
4272############################################################################# 4295#############################################################################
4273 4296
4274package CFPlus::UI::Root; 4297package dc::UI::Root;
4275 4298
4276our @ISA = CFPlus::UI::Container::; 4299our @ISA = dc::UI::Container::;
4277 4300
4278use List::Util qw(min max); 4301use List::Util qw(min max);
4279 4302
4280use CFPlus::OpenGL; 4303use dc::OpenGL;
4281 4304
4282sub new { 4305sub new {
4283 my $class = shift; 4306 my $class = shift;
4284 4307
4285 my $self = $class->SUPER::new ( 4308 my $self = $class->SUPER::new (
4286 visible => 1, 4309 visible => 1,
4287 @_, 4310 @_,
4288 ); 4311 );
4289 4312
4290 CFPlus::weaken ($self->{root} = $self); 4313 dc::weaken ($self->{root} = $self);
4291 4314
4292 $self 4315 $self
4293} 4316}
4294 4317
4295sub size_request { 4318sub size_request {
4343} 4366}
4344 4367
4345sub update { 4368sub update {
4346 my ($self) = @_; 4369 my ($self) = @_;
4347 4370
4348 $::WANT_REFRESH->start; 4371 $::WANT_REFRESH = 1;
4349} 4372}
4350 4373
4351sub add { 4374sub add {
4352 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4353 4376
4498 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4499 glMatrixMode GL_MODELVIEW; 4522 glMatrixMode GL_MODELVIEW;
4500 glLoadIdentity; 4523 glLoadIdentity;
4501 4524
4502 { 4525 {
4503 package CFPlus::UI::Base; 4526 package dc::UI::Base;
4504 4527
4505 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4528 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4506 (0, 0, $self->{w}, $self->{h}); 4529 (0, 0, $self->{w}, $self->{h});
4507 4530
4508 $self->_draw; 4531 $self->_draw;
4509 } 4532 }
4510} 4533}
4511 4534
4512############################################################################# 4535#############################################################################
4513 4536
4514package CFPlus::UI; 4537package dc::UI;
4515 4538
4516$ROOT = new CFPlus::UI::Root; 4539$ROOT = new dc::UI::Root;
4517$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4540$TOOLTIP = new dc::UI::Tooltip z => 900;
4518 4541
45191 45421
4520 4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines