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.436 by root, Wed Aug 22 21:40:57 2007 UTC vs.
Revision 1.458 by root, Fri Dec 28 15:05:33 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 (
1617 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1618 1640
1619 my $sw = sum @w[$c .. $c + $cs - 1]; 1641 my $sw = sum @w[$c .. $c + $cs - 1];
1620 1642
1621 if ($w > $sw) { 1643 if ($w > $sw) {
1622 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1644 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1623 } 1645 }
1624 } 1646 }
1625 1647
1626 # second pass, rows 1648 # second pass, rows
1627 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1628 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1629 1651
1630 my $sh = sum @h[$r .. $r + $rs - 1]; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1631 1653
1632 if ($h > $sh) { 1654 if ($h > $sh) {
1633 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1655 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1634 } 1656 }
1635 } 1657 }
1636 1658
1637 (\@w, \@h) 1659 (\@w, \@h)
1638} 1660}
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 (
2072 can_hover => 1, 2096 can_hover => 1,
2073 can_focus => 1, 2097 can_focus => 1,
2074 valign => 0, 2098 valign => 0,
2075 can_events => 1, 2099 can_events => 1,
2076 ellipsise => 0, 2100 ellipsise => 0,
2101 padding_x => 4,
2102 padding_y => 2,
2077 #text => ... 2103 #text => ...
2078 #hidden => "*", 2104 #hidden => "*",
2079 @_ 2105 @_
2080 ) 2106 )
2081} 2107}
2132 2158
2133 if ($uni == 8) { 2159 if ($uni == 8) {
2134 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2160 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2135 } elsif ($uni == 127) { 2161 } elsif ($uni == 127) {
2136 substr $text, $self->{cursor}, 1, ""; 2162 substr $text, $self->{cursor}, 1, "";
2137 } elsif ($sym == CFPlus::SDLK_LEFT) { 2163 } elsif ($sym == DC::SDLK_LEFT) {
2138 --$self->{cursor} if $self->{cursor}; 2164 --$self->{cursor} if $self->{cursor};
2139 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2165 } elsif ($sym == DC::SDLK_RIGHT) {
2140 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2166 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2141 } elsif ($sym == CFPlus::SDLK_HOME) { 2167 } elsif ($sym == DC::SDLK_HOME) {
2142 # what a hack 2168 # what a hack
2143 $self->{cursor} = 2169 $self->{cursor} =
2144 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2170 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2145 ? length $1 2171 ? length $1
2146 : 0; 2172 : 0;
2147 } elsif ($sym == CFPlus::SDLK_END) { 2173 } elsif ($sym == DC::SDLK_END) {
2148 # uh, again 2174 # uh, again
2149 $self->{cursor} = 2175 $self->{cursor} =
2150 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2176 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2151 ? $self->{cursor} + length $1 2177 ? $self->{cursor} + length $1
2152 : length $self->{text}; 2178 : length $self->{text};
2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2259 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2234 } 2260 }
2235 2261
2236 glColor_premultiply @{$self->{active_fg}}; 2262 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2263 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2264 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2265 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2266 glEnd;
2241 2267
2242 glLineWidth 3; 2268 glLineWidth 3;
2243 glColor @{$self->{active_outline}}; 2269 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2270 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2245 glLineWidth 1; 2271 glLineWidth 1;
2246 2272
2247 } else { 2273 } else {
2248 glColor @{$self->{outline}}; 2274 glColor @{$self->{outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP; 2275 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5; 2276 glVertex .5, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3; 2277 glVertex .5, $self->{h} - 2.5;
2253 glVertex $self->{w} - 1, $self->{h} - 3; 2278 glVertex $self->{w} - .5, $self->{h} - 2.5;
2254 glVertex $self->{w} - 1, $self->{h} * .5; 2279 glVertex $self->{w} - .5, $self->{h} * .5;
2255 glEnd; 2280 glEnd;
2256 } 2281 }
2257} 2282}
2258 2283
2259############################################################################# 2284#############################################################################
2260 2285
2261package CFPlus::UI::Entry; 2286package DC::UI::Entry;
2262 2287
2263our @ISA = CFPlus::UI::EntryBase::; 2288our @ISA = DC::UI::EntryBase::;
2264 2289
2265use CFPlus::OpenGL; 2290use DC::OpenGL;
2266 2291
2267sub invoke_key_down { 2292sub invoke_key_down {
2268 my ($self, $ev) = @_; 2293 my ($self, $ev) = @_;
2269 2294
2270 my $sym = $ev->{sym}; 2295 my $sym = $ev->{sym};
2276 $self->{history_pointer} = -1; 2301 $self->{history_pointer} = -1;
2277 $self->{history_saveback} = ''; 2302 $self->{history_saveback} = '';
2278 $self->emit (activate => $txt); 2303 $self->emit (activate => $txt);
2279 $self->update; 2304 $self->update;
2280 2305
2281 } elsif ($sym == CFPlus::SDLK_UP) { 2306 } elsif ($sym == DC::SDLK_UP) {
2282 if ($self->{history_pointer} < 0) { 2307 if ($self->{history_pointer} < 0) {
2283 $self->{history_saveback} = $self->get_text; 2308 $self->{history_saveback} = $self->get_text;
2284 } 2309 }
2285 if (@{$self->{history} || []} > 0) { 2310 if (@{$self->{history} || []} > 0) {
2286 $self->{history_pointer}++; 2311 $self->{history_pointer}++;
2288 $self->{history_pointer} = @{$self->{history} || []} - 1; 2313 $self->{history_pointer} = @{$self->{history} || []} - 1;
2289 } 2314 }
2290 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2315 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2291 } 2316 }
2292 2317
2293 } elsif ($sym == CFPlus::SDLK_DOWN) { 2318 } elsif ($sym == DC::SDLK_DOWN) {
2294 $self->{history_pointer}--; 2319 $self->{history_pointer}--;
2295 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2320 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2296 2321
2297 if ($self->{history_pointer} >= 0) { 2322 if ($self->{history_pointer} >= 0) {
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2323 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2307 1 2332 1
2308} 2333}
2309 2334
2310############################################################################# 2335#############################################################################
2311 2336
2312package CFPlus::UI::TextEdit; 2337package DC::UI::TextEdit;
2313 2338
2314our @ISA = CFPlus::UI::EntryBase::; 2339our @ISA = DC::UI::EntryBase::;
2315 2340
2316use CFPlus::OpenGL; 2341use DC::OpenGL;
2342
2343sub new {
2344 my $class = shift;
2345
2346 $class->SUPER::new (
2347 padding_y => 4,
2348
2349 @_
2350 )
2351}
2317 2352
2318sub move_cursor_ver { 2353sub move_cursor_ver {
2319 my ($self, $dy) = @_; 2354 my ($self, $dy) = @_;
2320 2355
2321 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2356 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2322 2357
2323 $y += $dy; 2358 $line += $dy;
2324 2359
2325 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2360 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2326 $self->{cursor} = $index; 2361 $self->{cursor} = $index;
2327 delete $self->{cur_h}; 2362 delete $self->{cur_h};
2328 $self->update; 2363 $self->update;
2329 return; 2364 return;
2330 } 2365 }
2333sub invoke_key_down { 2368sub invoke_key_down {
2334 my ($self, $ev) = @_; 2369 my ($self, $ev) = @_;
2335 2370
2336 my $sym = $ev->{sym}; 2371 my $sym = $ev->{sym};
2337 2372
2338 if ($sym == CFPlus::SDLK_UP) { 2373 if ($sym == DC::SDLK_UP) {
2339 $self->move_cursor_ver (-1); 2374 $self->move_cursor_ver (-1);
2340 } elsif ($sym == CFPlus::SDLK_DOWN) { 2375 } elsif ($sym == DC::SDLK_DOWN) {
2341 $self->move_cursor_ver (+1); 2376 $self->move_cursor_ver (+1);
2342 } else { 2377 } else {
2343 return $self->SUPER::invoke_key_down ($ev) 2378 return $self->SUPER::invoke_key_down ($ev)
2344 } 2379 }
2345 2380
2346 1 2381 1
2347} 2382}
2348 2383
2349############################################################################# 2384#############################################################################
2350 2385
2351package CFPlus::UI::ButtonBin; 2386package DC::UI::ButtonBin;
2352 2387
2353our @ISA = CFPlus::UI::Bin::; 2388our @ISA = DC::UI::Bin::;
2354 2389
2355use CFPlus::OpenGL; 2390use DC::OpenGL;
2356 2391
2357my @tex = 2392my @tex =
2358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2393 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2359 qw(b1_button_inactive.png b1_button_active.png); 2394 qw(b1_button_inactive.png b1_button_active.png);
2360 2395
2361sub new { 2396sub new {
2362 my $class = shift; 2397 my $class = shift;
2363 2398
2395 $self->SUPER::_draw; 2430 $self->SUPER::_draw;
2396} 2431}
2397 2432
2398############################################################################# 2433#############################################################################
2399 2434
2400package CFPlus::UI::Button; 2435package DC::UI::Button;
2401 2436
2402our @ISA = CFPlus::UI::Label::; 2437our @ISA = DC::UI::Label::;
2403 2438
2404use CFPlus::OpenGL; 2439use DC::OpenGL;
2405 2440
2406my @tex = 2441my @tex =
2407 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2442 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2408 qw(b1_button_inactive.png b1_button_active.png); 2443 qw(b1_button_inactive.png b1_button_active.png);
2409 2444
2410sub new { 2445sub new {
2411 my $class = shift; 2446 my $class = shift;
2412 2447
2413 $class->SUPER::new ( 2448 $class->SUPER::new (
2414 padding_x => 4, 2449 padding_x => 8,
2415 padding_y => 4, 2450 padding_y => 4,
2416 fg => [1.0, 1.0, 1.0], 2451 fg => [1.0, 1.0, 1.0],
2417 active_fg => [0.8, 0.8, 0.8], 2452 active_fg => [0.8, 0.8, 0.8],
2418 can_hover => 1, 2453 can_hover => 1,
2419 align => 0, 2454 align => 0,
2450 $self->SUPER::_draw; 2485 $self->SUPER::_draw;
2451} 2486}
2452 2487
2453############################################################################# 2488#############################################################################
2454 2489
2455package CFPlus::UI::CheckBox; 2490package DC::UI::CheckBox;
2456 2491
2457our @ISA = CFPlus::UI::DrawBG::; 2492our @ISA = DC::UI::DrawBG::;
2458 2493
2459my @tex = 2494my @tex =
2460 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2495 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2461 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2496 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2462 2497
2463use CFPlus::OpenGL; 2498use DC::OpenGL;
2464 2499
2465sub new { 2500sub new {
2466 my $class = shift; 2501 my $class = shift;
2467 2502
2468 $class->SUPER::new ( 2503 $class->SUPER::new (
2508sub _draw { 2543sub _draw {
2509 my ($self) = @_; 2544 my ($self) = @_;
2510 2545
2511 $self->SUPER::_draw; 2546 $self->SUPER::_draw;
2512 2547
2513 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2548 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2514 2549
2515 my ($w, $h) = @$self{qw(w h)}; 2550 my ($w, $h) = @$self{qw(w h)};
2516 2551
2517 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2552 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2518 2553
2525 glDisable GL_TEXTURE_2D; 2560 glDisable GL_TEXTURE_2D;
2526} 2561}
2527 2562
2528############################################################################# 2563#############################################################################
2529 2564
2530package CFPlus::UI::Image; 2565package DC::UI::Image;
2531 2566
2532our @ISA = CFPlus::UI::Base::; 2567our @ISA = DC::UI::Base::;
2533 2568
2534use CFPlus::OpenGL; 2569use DC::OpenGL;
2535 2570
2536our %texture_cache; 2571our %texture_cache;
2537 2572
2538sub new { 2573sub new {
2539 my $class = shift; 2574 my $class = shift;
2546 2581
2547 $self->{path} || $self->{tex} 2582 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2583 or Carp::croak "'path' or 'tex' attributes required";
2549 2584
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2585 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2586 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1;
2552 2587
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2588 DC::weaken $texture_cache{$self->{path}};
2554 2589
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2590 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2591
2557 $self 2592 $self
2558} 2593}
2559 2594
2560sub STORABLE_freeze { 2595sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2596 my ($self, $cloning) = @_;
2562 2597
2563 $self->{path} 2598 $self->{path}
2564 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2599 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2565 2600
2566 $self->{path} 2601 $self->{path}
2567} 2602}
2568 2603
2569sub STORABLE_attach { 2604sub STORABLE_attach {
2600 glDisable GL_TEXTURE_2D; 2635 glDisable GL_TEXTURE_2D;
2601} 2636}
2602 2637
2603############################################################################# 2638#############################################################################
2604 2639
2605package CFPlus::UI::ImageButton; 2640package DC::UI::ImageButton;
2606 2641
2607our @ISA = CFPlus::UI::Image::; 2642our @ISA = DC::UI::Image::;
2608 2643
2609use CFPlus::OpenGL; 2644use DC::OpenGL;
2610 2645
2611my %textures; 2646my %textures;
2612 2647
2613sub new { 2648sub new {
2614 my $class = shift; 2649 my $class = shift;
2642 1 2677 1
2643} 2678}
2644 2679
2645############################################################################# 2680#############################################################################
2646 2681
2647package CFPlus::UI::VGauge; 2682package DC::UI::VGauge;
2648 2683
2649our @ISA = CFPlus::UI::Base::; 2684our @ISA = DC::UI::Base::;
2650 2685
2651use List::Util qw(min max); 2686use List::Util qw(min max);
2652 2687
2653use CFPlus::OpenGL; 2688use DC::OpenGL;
2654 2689
2655my %tex = ( 2690my %tex = (
2656 food => [ 2691 food => [
2657 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2658 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2693 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2659 ], 2694 ],
2660 grace => [ 2695 grace => [
2661 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2662 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2697 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2663 ], 2698 ],
2664 hp => [ 2699 hp => [
2665 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2666 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2701 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2667 ], 2702 ],
2668 mana => [ 2703 mana => [
2669 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2670 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2705 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2671 ], 2706 ],
2672); 2707);
2673 2708
2674# eg. VGauge->new (gauge => 'food'), default gauge: food 2709# eg. VGauge->new (gauge => 'food'), default gauge: food
2777 glDisable GL_TEXTURE_2D; 2812 glDisable GL_TEXTURE_2D;
2778} 2813}
2779 2814
2780############################################################################# 2815#############################################################################
2781 2816
2782package CFPlus::UI::Progress; 2817package DC::UI::Progress;
2783 2818
2784our @ISA = CFPlus::UI::Label::; 2819our @ISA = DC::UI::Label::;
2785 2820
2786use CFPlus::OpenGL; 2821use DC::OpenGL;
2787 2822
2788sub new { 2823sub new {
2789 my ($class, %arg) = @_; 2824 my ($class, %arg) = @_;
2790 2825
2791 my $self = $class->SUPER::new ( 2826 my $self = $class->SUPER::new (
2811 my ($self, $label) = @_; 2846 my ($self, $label) = @_;
2812 2847
2813 return if $self->{label} eq $label; 2848 return if $self->{label} eq $label;
2814 $self->{label} = $label; 2849 $self->{label} = $label;
2815 2850
2816 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2851 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2817} 2852}
2818 2853
2819sub set_value { 2854sub set_value {
2820 my ($self, $value) = @_; 2855 my ($self, $value) = @_;
2821 2856
2837 2872
2838 glEnable GL_BLEND; 2873 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2874 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840 2875
2841 if ($self->{value} >= 0) { 2876 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2877 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2843 2878
2844 glColor_premultiply @{$self->{bar}}; 2879 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2; 2880 glRect 2, 2, $s, $self->{h} - 2;
2846 glColor_premultiply @{$self->{bg}}; 2881 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2882 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2848 } 2883 }
2849 2884
2850 glColor_premultiply @{$self->{outline}}; 2885 glColor_premultiply @{$self->{outline}};
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2886 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2852 2887
2853 glDisable GL_BLEND; 2888 glDisable GL_BLEND;
2854 2889
2855 { 2890 {
2856 local $self->{bg}; # do not draw background 2891 local $self->{bg}; # do not draw background
2858 } 2893 }
2859} 2894}
2860 2895
2861############################################################################# 2896#############################################################################
2862 2897
2863package CFPlus::UI::ExperienceProgress; 2898package DC::UI::ExperienceProgress;
2864 2899
2865our @ISA = CFPlus::UI::Progress::; 2900our @ISA = DC::UI::Progress::;
2866 2901
2867sub new { 2902sub new {
2868 my ($class, %arg) = @_; 2903 my ($class, %arg) = @_;
2869 2904
2870 my $self = $class->SUPER::new ( 2905 my $self = $class->SUPER::new (
2915 $self->SUPER::set_value ($v); 2950 $self->SUPER::set_value ($v);
2916} 2951}
2917 2952
2918############################################################################# 2953#############################################################################
2919 2954
2920package CFPlus::UI::Gauge; 2955package DC::UI::Gauge;
2921 2956
2922our @ISA = CFPlus::UI::VBox::; 2957our @ISA = DC::UI::VBox::;
2923 2958
2924sub new { 2959sub new {
2925 my ($class, %arg) = @_; 2960 my ($class, %arg) = @_;
2926 2961
2927 my $self = $class->SUPER::new ( 2962 my $self = $class->SUPER::new (
2929 can_hover => 1, 2964 can_hover => 1,
2930 can_events => 1, 2965 can_events => 1,
2931 %arg, 2966 %arg,
2932 ); 2967 );
2933 2968
2934 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2969 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999");
2935 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2970 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2936 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2971 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999");
2937 2972
2938 $self 2973 $self
2939} 2974}
2940 2975
2941sub set_fontsize { 2976sub set_fontsize {
2962 $self->{value}->set_text ($val); 2997 $self->{value}->set_text ($val);
2963} 2998}
2964 2999
2965############################################################################# 3000#############################################################################
2966 3001
2967package CFPlus::UI::Slider; 3002package DC::UI::Slider;
2968 3003
2969use strict; 3004use strict;
2970 3005
2971use CFPlus::OpenGL; 3006use DC::OpenGL;
2972 3007
2973our @ISA = CFPlus::UI::DrawBG::; 3008our @ISA = DC::UI::DrawBG::;
2974 3009
2975my @tex = 3010my @tex =
2976 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3011 map { new_from_file DC::Texture DC::find_rcfile $_ }
2977 qw(s1_slider.png s1_slider_bg.png); 3012 qw(s1_slider.png s1_slider_bg.png);
2978 3013
2979sub new { 3014sub new {
2980 my $class = shift; 3015 my $class = shift;
2981 3016
3077sub invoke_mouse_wheel { 3112sub invoke_mouse_wheel {
3078 my ($self, $ev) = @_; 3113 my ($self, $ev) = @_;
3079 3114
3080 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3115 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3081 3116
3082 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3117 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3083 3118
3084 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3119 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3085 3120
3086 1 3121 1
3087} 3122}
3140 glDisable GL_TEXTURE_2D; 3175 glDisable GL_TEXTURE_2D;
3141} 3176}
3142 3177
3143############################################################################# 3178#############################################################################
3144 3179
3145package CFPlus::UI::ValSlider; 3180package DC::UI::ValSlider;
3146 3181
3147our @ISA = CFPlus::UI::HBox::; 3182our @ISA = DC::UI::HBox::;
3148 3183
3149sub new { 3184sub new {
3150 my ($class, %arg) = @_; 3185 my ($class, %arg) = @_;
3151 3186
3152 my $range = delete $arg{range}; 3187 my $range = delete $arg{range};
3153 3188
3154 my $self = $class->SUPER::new ( 3189 my $self = $class->SUPER::new (
3155 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3190 slider => (new DC::UI::Slider expand => 1, range => $range),
3156 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3191 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3157 to_value => sub { shift }, 3192 to_value => sub { shift },
3158 from_value => sub { shift }, 3193 from_value => sub { shift },
3159 %arg, 3194 %arg,
3160 ); 3195 );
3161 3196
3181sub set_range { shift->{slider}->set_range (@_) } 3216sub set_range { shift->{slider}->set_range (@_) }
3182sub set_value { shift->{slider}->set_value (@_) } 3217sub set_value { shift->{slider}->set_value (@_) }
3183 3218
3184############################################################################# 3219#############################################################################
3185 3220
3186package CFPlus::UI::TextScroller; 3221package DC::UI::TextScroller;
3187 3222
3188our @ISA = CFPlus::UI::HBox::; 3223our @ISA = DC::UI::HBox::;
3189 3224
3190use CFPlus::OpenGL; 3225use DC::OpenGL;
3191 3226
3192sub new { 3227sub new {
3193 my $class = shift; 3228 my $class = shift;
3194 3229
3195 my $self = $class->SUPER::new ( 3230 my $self = $class->SUPER::new (
3197 can_events => 1, 3232 can_events => 1,
3198 indent => 0, 3233 indent => 0,
3199 #font => default_font 3234 #font => default_font
3200 @_, 3235 @_,
3201 3236
3202 layout => (new CFPlus::Layout), 3237 layout => (new DC::Layout),
3203 par => [], 3238 par => [],
3204 max_par => 0, 3239 max_par => 0,
3205 height => 0, 3240 height => 0,
3206 children => [ 3241 children => [
3207 (new CFPlus::UI::Empty expand => 1), 3242 (new DC::UI::Empty expand => 1),
3208 (new CFPlus::UI::Slider vertical => 1), 3243 (new DC::UI::Slider vertical => 1),
3209 ], 3244 ],
3210 ); 3245 );
3211 3246
3212 $self->{children}[1]->connect (changed => sub { $self->update }); 3247 $self->{children}[1]->connect (changed => sub { $self->update });
3213 3248
3396 $ROOT->on_post_alloc ($self => sub { 3431 $ROOT->on_post_alloc ($self => sub {
3397 $self->force_uptodate; 3432 $self->force_uptodate;
3398 3433
3399 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3434 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3400 3435
3401 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3436 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3402 glClearColor 0, 0, 0, 0; 3437 glClearColor 0, 0, 0, 0;
3403 glClear GL_COLOR_BUFFER_BIT; 3438 glClear GL_COLOR_BUFFER_BIT;
3404 3439
3405 package CFPlus::UI::Base; 3440 package DC::UI::Base;
3406 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3441 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3407 (0, 0, $self->{w}, $self->{h}); 3442 (0, 0, $self->{w}, $self->{h});
3408 3443
3409 my $top = int $self->{children}[1]{range}[0]; 3444 my $top = int $self->{children}[1]{range}[0];
3410 3445
3469 $self->{children}[1]->draw; 3504 $self->{children}[1]->draw;
3470} 3505}
3471 3506
3472############################################################################# 3507#############################################################################
3473 3508
3474package CFPlus::UI::Animator; 3509package DC::UI::Animator;
3475 3510
3476use CFPlus::OpenGL; 3511use DC::OpenGL;
3477 3512
3478our @ISA = CFPlus::UI::Bin::; 3513our @ISA = DC::UI::Bin::;
3479 3514
3480sub moveto { 3515sub moveto {
3481 my ($self, $x, $y) = @_; 3516 my ($self, $x, $y) = @_;
3482 3517
3483 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3518 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3511 glPopMatrix; 3546 glPopMatrix;
3512} 3547}
3513 3548
3514############################################################################# 3549#############################################################################
3515 3550
3516package CFPlus::UI::Flopper; 3551package DC::UI::Flopper;
3517 3552
3518our @ISA = CFPlus::UI::Button::; 3553our @ISA = DC::UI::Button::;
3519 3554
3520sub new { 3555sub new {
3521 my $class = shift; 3556 my $class = shift;
3522 3557
3523 my $self = $class->SUPER::new ( 3558 my $self = $class->SUPER::new (
3535 $self->{other}->toggle_visibility; 3570 $self->{other}->toggle_visibility;
3536} 3571}
3537 3572
3538############################################################################# 3573#############################################################################
3539 3574
3540package CFPlus::UI::Tooltip; 3575package DC::UI::Tooltip;
3541 3576
3542our @ISA = CFPlus::UI::Bin::; 3577our @ISA = DC::UI::Bin::;
3543 3578
3544use CFPlus::OpenGL; 3579use DC::OpenGL;
3545 3580
3546sub new { 3581sub new {
3547 my $class = shift; 3582 my $class = shift;
3548 3583
3549 $class->SUPER::new ( 3584 $class->SUPER::new (
3556 my ($self, $widget) = @_; 3591 my ($self, $widget) = @_;
3557 3592
3558 my $tip = $widget->{tooltip}; 3593 my $tip = $widget->{tooltip};
3559 $tip = $tip->($widget) if "CODE" eq ref $tip; 3594 $tip = $tip->($widget) if "CODE" eq ref $tip;
3560 3595
3561 $tip = CFPlus::Pod::section_label tooltip => $1 3596 $tip = DC::Pod::section_label tooltip => $1
3562 if $tip =~ /^#(.*)$/; 3597 if $tip =~ /^#(.*)$/;
3563 3598
3564 if ($ENV{CFPLUS_DEBUG} & 2) { 3599 if ($ENV{CFPLUS_DEBUG} & 2) {
3565 $tip .= "\n\n" . (ref $widget) . "\n" 3600 $tip .= "\n\n" . (ref $widget) . "\n"
3566 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3601 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3569 } 3604 }
3570 3605
3571 $tip =~ s/^\n+//; 3606 $tip =~ s/^\n+//;
3572 $tip =~ s/\n+$//; 3607 $tip =~ s/\n+$//;
3573 3608
3574 $self->add (new CFPlus::UI::Label 3609 $self->add (new DC::UI::Label
3575 markup => $tip, 3610 markup => $tip,
3576 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3611 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3577 fontsize => 0.8, 3612 fontsize => 0.8,
3578 style => 1, # FLAG_INVERSE 3613 style => 1, # FLAG_INVERSE
3579 ellipsise => 0, 3614 ellipsise => 0,
3618} 3653}
3619 3654
3620sub _draw { 3655sub _draw {
3621 my ($self) = @_; 3656 my ($self) = @_;
3622 3657
3623 glTranslate 0.375, 0.375;
3624
3625 my ($w, $h) = @$self{qw(w h)}; 3658 my ($w, $h) = @$self{qw(w h)};
3626 3659
3627 glColor 1, 0.8, 0.4; 3660 glColor 1, 0.8, 0.4;
3628 glRect 0, 0, $w, $h; 3661 glRect 0, 0, $w, $h;
3629 3662
3630 glColor 0, 0, 0; 3663 glColor 0, 0, 0;
3631 glRect_lineloop 0, 0, $w, $h; 3664 glRect_lineloop .5, .5, $w + .5, $h + .5;
3632 3665
3633 glTranslate 2 - 0.375, 2 - 0.375; 3666 glTranslate 2, 2;
3634 3667
3635 $self->SUPER::_draw; 3668 $self->SUPER::_draw;
3636} 3669}
3637 3670
3638############################################################################# 3671#############################################################################
3639 3672
3640package CFPlus::UI::Face; 3673package DC::UI::Face;
3641 3674
3642our @ISA = CFPlus::UI::DrawBG::; 3675our @ISA = DC::UI::DrawBG::;
3643 3676
3644use CFPlus::OpenGL; 3677use DC::OpenGL;
3645 3678
3646sub new { 3679sub new {
3647 my $class = shift; 3680 my $class = shift;
3648 3681
3649 my $self = $class->SUPER::new ( 3682 my $self = $class->SUPER::new (
3653 can_events => 0, 3686 can_events => 0,
3654 @_, 3687 @_,
3655 ); 3688 );
3656 3689
3657 if ($self->{anim} && $self->{animspeed}) { 3690 if ($self->{anim} && $self->{animspeed}) {
3658 CFPlus::weaken (my $widget = $self); 3691 DC::weaken (my $widget = $self);
3659 3692
3660 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3693 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3661 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3694 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3662 $self->{timer} = Event->timer (
3663 parked => 1,
3664 cb => sub {
3665 return unless $::CONN; 3695 return unless $::CONN;
3666 3696
3667 my $w = $widget 3697 my $w = $widget
3668 or return; 3698 or return;
3669 3699
3670 ++$w->{frame}; 3700 ++$w->{frame};
3671 $w->update_face; 3701 $w->update_face;
3672 3702
3673 # somehow, $widget can go away 3703 # somehow, $widget can go away
3674 $w->update; 3704 $w->update;
3675 $w->update_timer; 3705 $w->update_timer;
3676 },
3677 ); 3706 };
3678 3707
3679 $self->update_face; 3708 $self->update_face;
3680 $self->update_timer; 3709 $self->update_timer;
3681 } 3710 }
3682 3711
3687 my ($self) = @_; 3716 my ($self) = @_;
3688 3717
3689 return unless $self->{timer}; 3718 return unless $self->{timer};
3690 3719
3691 if ($self->{visible}) { 3720 if ($self->{visible}) {
3692 $self->{timer}->at (
3693 $self->{anim_start}
3694 + $self->{animspeed}
3695 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3696 );
3697 $self->{timer}->start; 3721 $self->{timer}->start;
3698 } else { 3722 } else {
3699 $self->{timer}->stop; 3723 $self->{timer}->stop;
3700 } 3724 }
3701} 3725}
3778 $self->SUPER::destroy; 3802 $self->SUPER::destroy;
3779} 3803}
3780 3804
3781############################################################################# 3805#############################################################################
3782 3806
3783package CFPlus::UI::Buttonbar; 3807package DC::UI::Buttonbar;
3784 3808
3785our @ISA = CFPlus::UI::HBox::; 3809our @ISA = DC::UI::HBox::;
3786 3810
3787# TODO: should actually wrap buttons and other goodies. 3811# TODO: should actually wrap buttons and other goodies.
3788 3812
3789############################################################################# 3813#############################################################################
3790 3814
3791package CFPlus::UI::Menu; 3815package DC::UI::Menu;
3792 3816
3793our @ISA = CFPlus::UI::Toplevel::; 3817our @ISA = DC::UI::Toplevel::;
3794 3818
3795use CFPlus::OpenGL; 3819use DC::OpenGL;
3796 3820
3797sub new { 3821sub new {
3798 my $class = shift; 3822 my $class = shift;
3799 3823
3800 my $self = $class->SUPER::new ( 3824 my $self = $class->SUPER::new (
3801 items => [], 3825 items => [],
3802 z => 100, 3826 z => 100,
3803 @_, 3827 @_,
3804 ); 3828 );
3805 3829
3806 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3830 $self->add ($self->{vbox} = new DC::UI::VBox);
3807 3831
3808 for my $item (@{ $self->{items} }) { 3832 for my $item (@{ $self->{items} }) {
3809 my ($widget, $cb, $tooltip) = @$item; 3833 my ($widget, $cb, $tooltip) = @$item;
3810 3834
3811 # handle various types of items, only text for now 3835 # handle various types of items, only text for now
3812 if (!ref $widget) { 3836 if (!ref $widget) {
3813 if ($widget =~ /\t/) { 3837 if ($widget =~ /\t/) {
3814 my ($left, $right) = split /\t/, $widget, 2; 3838 my ($left, $right) = split /\t/, $widget, 2;
3815 3839
3816 $widget = new CFPlus::UI::HBox 3840 $widget = new DC::UI::HBox
3817 can_hover => 1, 3841 can_hover => 1,
3818 can_events => 1, 3842 can_events => 1,
3819 tooltip => $tooltip, 3843 tooltip => $tooltip,
3820 children => [ 3844 children => [
3821 (new CFPlus::UI::Label markup => $left, expand => 1), 3845 (new DC::UI::Label markup => $left, expand => 1),
3822 (new CFPlus::UI::Label markup => $right, align => +1), 3846 (new DC::UI::Label markup => $right, align => +1),
3823 ], 3847 ],
3824 ; 3848 ;
3825 3849
3826 } else { 3850 } else {
3827 $widget = new CFPlus::UI::Label 3851 $widget = new DC::UI::Label
3828 can_hover => 1, 3852 can_hover => 1,
3829 can_events => 1, 3853 can_events => 1,
3830 markup => $widget, 3854 markup => $widget,
3831 tooltip => $tooltip; 3855 tooltip => $tooltip;
3832 } 3856 }
3880 1 3904 1
3881} 3905}
3882 3906
3883############################################################################# 3907#############################################################################
3884 3908
3885package CFPlus::UI::Multiplexer; 3909package DC::UI::Multiplexer;
3886 3910
3887our @ISA = CFPlus::UI::Container::; 3911our @ISA = DC::UI::Container::;
3888 3912
3889sub new { 3913sub new {
3890 my $class = shift; 3914 my $class = shift;
3891 3915
3892 my $self = $class->SUPER::new ( 3916 my $self = $class->SUPER::new (
3953 $self->{current}->draw; 3977 $self->{current}->draw;
3954} 3978}
3955 3979
3956############################################################################# 3980#############################################################################
3957 3981
3958package CFPlus::UI::Notebook; 3982package DC::UI::Notebook;
3959 3983
3960use CFPlus::OpenGL; 3984use DC::OpenGL;
3961 3985
3962our @ISA = CFPlus::UI::VBox::; 3986our @ISA = DC::UI::VBox::;
3963 3987
3964sub new { 3988sub new {
3965 my $class = shift; 3989 my $class = shift;
3966 3990
3967 my $self = $class->SUPER::new ( 3991 my $self = $class->SUPER::new (
3968 buttonbar => (new CFPlus::UI::Buttonbar), 3992 buttonbar => (new DC::UI::Buttonbar),
3969 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3993 multiplexer => (new DC::UI::Multiplexer expand => 1),
3970 active_outline => [1, 1, 0], 3994 active_outline => [.7, .7, 0.2],
3971 # filter => # will be put between multiplexer and $self 3995 # filter => # will be put between multiplexer and $self
3972 @_, 3996 @_,
3973 ); 3997 );
3974 3998
3975 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3999 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3983 4007
3984 for my $child (@$widgets) { 4008 for my $child (@$widgets) {
3985 Scalar::Util::weaken $child; 4009 Scalar::Util::weaken $child;
3986 $child->{c_tab_} ||= do { 4010 $child->{c_tab_} ||= do {
3987 my $tab = 4011 my $tab =
3988 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4012 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3989 ? $child->{c_tab} 4013 ? $child->{c_tab}
3990 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4014 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3991 4015
3992 $tab->connect (activate => sub { 4016 $tab->connect (activate => sub {
3993 $wself->set_current_page ($child); 4017 $wself->set_current_page ($child);
3994 }); 4018 });
3995 4019
4056 4080
4057 $self->SUPER::_draw (); 4081 $self->SUPER::_draw ();
4058 4082
4059 if (my $cur = $self->{multiplexer}{current}) { 4083 if (my $cur = $self->{multiplexer}{current}) {
4060 if ($cur = $cur->{c_tab_}) { 4084 if ($cur = $cur->{c_tab_}) {
4061 glTranslate $cur->{x}, $cur->{y}; 4085 glTranslate $self->{buttonbar}{x} + $cur->{x},
4086 $self->{buttonbar}{y} + $cur->{y};
4062 glLineWidth 3; 4087 glLineWidth 3;
4088 #glEnable GL_BLEND;
4089 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4063 glColor @{$self->{active_outline}}; 4090 glColor @{$self->{active_outline}};
4064 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4091 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4065 glLineWidth 1; 4092 glLineWidth 1;
4093 #glDisable GL_BLEND;
4066 } 4094 }
4067 } 4095 }
4068} 4096}
4069 4097
4070############################################################################# 4098#############################################################################
4071 4099
4072package CFPlus::UI::Selector; 4100package DC::UI::Selector;
4073 4101
4074use utf8; 4102use utf8;
4075 4103
4076our @ISA = CFPlus::UI::Button::; 4104our @ISA = DC::UI::Button::;
4077 4105
4078sub new { 4106sub new {
4079 my $class = shift; 4107 my $class = shift;
4080 4108
4081 my $self = $class->SUPER::new ( 4109 my $self = $class->SUPER::new (
4098 my ($value, $title, $tooltip) = @$_; 4126 my ($value, $title, $tooltip) = @$_;
4099 4127
4100 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4128 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4101 } 4129 }
4102 4130
4103 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4131 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4104} 4132}
4105 4133
4106sub _set_value { 4134sub _set_value {
4107 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
4108 4136
4109 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4137 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4138 $item ||= $self->{options}[0]
4110 or return; 4139 or return;
4111 4140
4112 $self->{value} = $item->[0]; 4141 $self->{value} = $item->[0];
4113 $self->set_markup ("$item->[1] ⇓"); 4142 $self->set_markup ("$item->[1] ⇓");
4114 $self->set_tooltip ($item->[2]); 4143# $self->set_tooltip ($item->[2]);
4115} 4144}
4116 4145
4117sub set_value { 4146sub set_value {
4118 my ($self, $value) = @_; 4147 my ($self, $value) = @_;
4119 4148
4121 4150
4122 $self->_set_value ($value); 4151 $self->_set_value ($value);
4123 $self->emit (changed => $value); 4152 $self->emit (changed => $value);
4124} 4153}
4125 4154
4155sub set_options {
4156 my ($self, $options) = @_;
4157
4158 $self->{options} = $options;
4159 $self->_set_value ($self->{value});
4160}
4161
4126############################################################################# 4162#############################################################################
4127 4163
4128package CFPlus::UI::Statusbox; 4164package DC::UI::Statusbox;
4129 4165
4130our @ISA = CFPlus::UI::VBox::; 4166our @ISA = DC::UI::VBox::;
4131 4167
4132sub new { 4168sub new {
4133 my $class = shift; 4169 my $class = shift;
4134 4170
4135 my $self = $class->SUPER::new ( 4171 my $self = $class->SUPER::new (
4136 fontsize => 0.8, 4172 fontsize => 0.8,
4137 @_, 4173 @_,
4138 ); 4174 );
4139 4175
4140 CFPlus::weaken (my $this = $self); 4176 DC::weaken (my $this = $self);
4141 4177
4142 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4178 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4143 4179
4144 $self 4180 $self
4145} 4181}
4146 4182
4147sub reorder { 4183sub reorder {
4148 my ($self) = @_; 4184 my ($self) = @_;
4149 my $NOW = Time::HiRes::time; 4185 my $NOW = Time::HiRes::time;
4150 4186
4151 # freeze display when hovering over any label 4187 # freeze display when hovering over any label
4152 return if $CFPlus::UI::TOOLTIP->{owner} 4188 return if $DC::UI::TOOLTIP->{owner}
4153 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4189 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4154 values %{ $self->{item} }; 4190 values %{ $self->{item} };
4155 4191
4156 while (my ($k, $v) = each %{ $self->{item} }) { 4192 while (my ($k, $v) = each %{ $self->{item} }) {
4157 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4193 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4158 } 4194 }
4195
4196 $self->{timer}->set (1, 1);
4159 4197
4160 my @widgets; 4198 my @widgets;
4161 4199
4162 my @items = sort { 4200 my @items = sort {
4163 $a->{pri} <=> $b->{pri} 4201 $a->{pri} <=> $b->{pri}
4164 or $b->{id} <=> $a->{id} 4202 or $b->{id} <=> $a->{id}
4165 } values %{ $self->{item} }; 4203 } values %{ $self->{item} };
4166
4167 $self->{timer}->interval (1);
4168 4204
4169 my $count = 10 + 1; 4205 my $count = 10 + 1;
4170 for my $item (@items) { 4206 for my $item (@items) {
4171 last unless --$count; 4207 last unless --$count;
4172 4208
4179 for ($short) { 4215 for ($short) {
4180 s/^\s+//; 4216 s/^\s+//;
4181 s/\s+/ /g; 4217 s/\s+/ /g;
4182 } 4218 }
4183 4219
4184 new CFPlus::UI::Label 4220 new DC::UI::Label
4185 markup => $short, 4221 markup => $short,
4186 tooltip => $item->{tooltip}, 4222 tooltip => $item->{tooltip},
4187 tooltip_font => $::FONT_PROP, 4223 tooltip_font => $::FONT_PROP,
4188 tooltip_width => 0.67, 4224 tooltip_width => 0.67,
4189 fontsize => $item->{fontsize} || $self->{fontsize}, 4225 fontsize => $item->{fontsize} || $self->{fontsize},
4196 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4232 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4197 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4233 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4198 $label->update; 4234 $label->update;
4199 $label->set_max_size (undef, $label->{req_h} * $diff) 4235 $label->set_max_size (undef, $label->{req_h} * $diff)
4200 if $diff < 1; 4236 if $diff < 1;
4201 $self->{timer}->interval (1/30); 4237 $self->{timer}->set (1/30, 1/30);
4202 } else { 4238 } else {
4203 $label->{fg}[3] = $item->{fg}[3] || 1; 4239 $label->{fg}[3] = $item->{fg}[3] || 1;
4204 } 4240 }
4205 4241
4206 push @widgets, $label; 4242 push @widgets, $label;
4268 $self->SUPER::destroy; 4304 $self->SUPER::destroy;
4269} 4305}
4270 4306
4271############################################################################# 4307#############################################################################
4272 4308
4273package CFPlus::UI::Root; 4309package DC::UI::Root;
4274 4310
4275our @ISA = CFPlus::UI::Container::; 4311our @ISA = DC::UI::Container::;
4276 4312
4277use List::Util qw(min max); 4313use List::Util qw(min max);
4278 4314
4279use CFPlus::OpenGL; 4315use DC::OpenGL;
4280 4316
4281sub new { 4317sub new {
4282 my $class = shift; 4318 my $class = shift;
4283 4319
4284 my $self = $class->SUPER::new ( 4320 my $self = $class->SUPER::new (
4285 visible => 1, 4321 visible => 1,
4286 @_, 4322 @_,
4287 ); 4323 );
4288 4324
4289 CFPlus::weaken ($self->{root} = $self); 4325 DC::weaken ($self->{root} = $self);
4290 4326
4291 $self 4327 $self
4292} 4328}
4293 4329
4294sub size_request { 4330sub size_request {
4342} 4378}
4343 4379
4344sub update { 4380sub update {
4345 my ($self) = @_; 4381 my ($self) = @_;
4346 4382
4347 $::WANT_REFRESH->start; 4383 $::WANT_REFRESH = 1;
4348} 4384}
4349 4385
4350sub add { 4386sub add {
4351 my ($self, @children) = @_; 4387 my ($self, @children) = @_;
4352 4388
4497 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4533 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4498 glMatrixMode GL_MODELVIEW; 4534 glMatrixMode GL_MODELVIEW;
4499 glLoadIdentity; 4535 glLoadIdentity;
4500 4536
4501 { 4537 {
4502 package CFPlus::UI::Base; 4538 package DC::UI::Base;
4503 4539
4504 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4540 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4505 (0, 0, $self->{w}, $self->{h}); 4541 (0, 0, $self->{w}, $self->{h});
4506 4542
4507 $self->_draw; 4543 $self->_draw;
4508 } 4544 }
4509} 4545}
4510 4546
4511############################################################################# 4547#############################################################################
4512 4548
4513package CFPlus::UI; 4549package DC::UI;
4514 4550
4515$ROOT = new CFPlus::UI::Root; 4551$ROOT = new DC::UI::Root;
4516$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4552$TOOLTIP = new DC::UI::Tooltip z => 900;
4517 4553
45181 45541
4519 4555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines