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.472 by root, Wed Dec 24 04:09:27 2008 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 43 }
43 44
44 $TOOLTIP->hide; 45 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
47}); 48};
48 49
49sub get_layout { 50sub get_layout {
50 my $layout; 51 my $layout;
51 52
52 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
88 89
89 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 92
92 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
93 } 94 }
94} 95}
95 96
96sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
97 my ($ev) = @_; 98 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
104 105
105 $GRAB = $widget; 106 $GRAB = $widget;
106 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
107 108
108 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
109 } 110 }
110 111
111 if ($GRAB) { 112 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 114 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 117
117 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
119 120
120 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 140 $grab->update if $grab;
140 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
141 142
142 check_hover $widget; 143 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
144 } 145 }
145} 146}
146 147
147sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
148 my ($ev) = @_; 149 my ($ev) = @_;
210 reconfigure_widgets; 211 reconfigure_widgets;
211} 212}
212 213
213############################################################################# 214#############################################################################
214 215
215package CFPlus::UI::Event; 216package DC::UI::Event;
216 217
217sub xy { 218sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 220}
220 221
221############################################################################# 222#############################################################################
222 223
223package CFPlus::UI::Base; 224package DC::UI::Base;
224 225
225use strict; 226use strict;
226 227
227use CFPlus::OpenGL; 228use DC::OpenGL;
228 229
229sub new { 230sub new {
230 my $class = shift; 231 my $class = shift;
231 232
232 my $self = bless { 233 my $self = bless {
237 h => undef, 238 h => undef,
238 can_events => 1, 239 can_events => 1,
239 @_ 240 @_
240 }, $class; 241 }, $class;
241 242
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 244
244 for (keys %$self) { 245 for (keys %$self) {
245 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
247 } 248 }
248 } 249 }
249 250
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 256
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 259
259 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
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
288 return if $self->{visible}; 289 return if $self->{visible};
290
291 $self->{parent} && $self->{parent}{root}#d#
292 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 293
290 $self->{root} = $self->{parent}{root}; 294 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 295 $self->{visible} = $self->{parent}{visible} + 1;
292 296
293 $self->emit (visibility_change => 1); 297 $self->emit (visibility_change => 1);
294 298
295 $self->realloc if !exists $self->{req_w}; 299 $self->realloc if !exists $self->{req_w};
296 300
297 $_->set_visible for $self->children; 301 $_->set_visible for $self->visible_children;
298} 302}
299 303
300sub set_invisible { 304sub set_invisible {
301 my ($self) = @_; 305 my ($self) = @_;
302 306
308 delete $self->{root}; 312 delete $self->{root};
309 313
310 undef $GRAB if $GRAB == $self; 314 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 315 undef $HOVER if $HOVER == $self;
312 316
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 317 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 318 if $TOOLTIP->{owner} == $self;
315 319
316 $self->emit ("focus_out"); 320 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 321 $self->emit (visibility_change => 0);
318} 322}
337sub hide { 341sub hide {
338 my ($self) = @_; 342 my ($self) = @_;
339 343
340 $self->set_invisible; 344 $self->set_invisible;
341 345
346 # extra $parent copy for 5.8.8+ bug workaround
347 # (otherwise $_[0] in remove gets freed
348 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 349 $parent->remove ($self);
343 if $self->{parent}; 350 }
344} 351}
345 352
346sub move_abs { 353sub move_abs {
347 my ($self, $x, $y, $z) = @_; 354 my ($self, $x, $y, $z) = @_;
348 355
360 $self->{force_h} = $h; 367 $self->{force_h} = $h;
361 368
362 $self->realloc; 369 $self->realloc;
363} 370}
364 371
372# traverse the widget chain up to find the maximum "physical" size constraints
373sub get_max_wh {
374 my ($self) = @_;
375
376 my ($w, $h) = @$self{qw(max_w max_h)};
377
378 if ($w <= 0 || $h <= 0) {
379 my ($mw, $mh) = $self->{parent}
380 ? $self->{parent}->get_max_wh
381 : ($::WIDTH, $::HEIGHT);
382
383 $w = $mw if $w <= 0;
384 $h = $mh if $h <= 0;
385 }
386
387 ($w, $h)
388}
389
365sub size_request { 390sub size_request {
366 require Carp; 391 require Carp;
367 Carp::confess "size_request is abstract"; 392 Carp::confess "size_request is abstract";
368} 393}
369 394
375 my ($self, $x, $y, $w, $h) = @_; 400 my ($self, $x, $y, $w, $h) = @_;
376 401
377 if ($self->{aspect}) { 402 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 403 my ($ow, $oh) = ($w, $h);
379 404
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 405 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 406 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 407
383 # use alignment to adjust x, y 408 # use alignment to adjust x, y
384 409
385 $x += int 0.5 * ($ow - $w); 410 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 411 $y += int 0.5 * ($oh - $h);
427 452
428 return if $self->{tooltip} eq $tooltip; 453 return if $self->{tooltip} eq $tooltip;
429 454
430 $self->{tooltip} = $tooltip; 455 $self->{tooltip} = $tooltip;
431 456
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 457 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 458 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 459 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 460 }
436} 461}
437 462
438# translate global coordinates to local coordinate system 463# translate global coordinates to local coordinate system
439sub coord2local { 464sub coord2local {
440 my ($self, $x, $y) = @_; 465 my ($self, $x, $y) = @_;
441 466
442 Carp::confess unless $self->{parent};#d# 467 return (undef, undef) unless $self->{parent};
443 468
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 469 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 470}
446 471
447# translate local coordinates to global coordinate system 472# translate local coordinates to global coordinate system
448sub coord2global { 473sub coord2global {
449 my ($self, $x, $y) = @_; 474 my ($self, $x, $y) = @_;
450 475
451 Carp::confess unless $self->{parent};#d# 476 return (undef, undef) unless $self->{parent};
452 477
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 478 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 479}
455 480
456sub invoke_focus_in { 481sub invoke_focus_in {
505sub connect { 530sub connect {
506 my ($self, $signal, $cb) = @_; 531 my ($self, $signal, $cb) = @_;
507 532
508 push @{ $self->{signal_cb}{$signal} }, $cb; 533 push @{ $self->{signal_cb}{$signal} }, $cb;
509 534
510 defined wantarray and CFPlus::guard { 535 defined wantarray and DC::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 537 @{ $self->{signal_cb}{$signal} };
513 } 538 }
514} 539}
515 540
553# in .xs 578# in .xs
554 579
555sub set_parent { 580sub set_parent {
556 my ($self, $parent) = @_; 581 my ($self, $parent) = @_;
557 582
558 CFPlus::weaken ($self->{parent} = $parent); 583 DC::weaken ($self->{parent} = $parent);
559 $self->set_visible if $parent->{visible}; 584 $self->set_visible if $parent->{visible};
560} 585}
561 586
562sub realloc { 587sub realloc {
563 my ($self) = @_; 588 my ($self) = @_;
601} 626}
602 627
603sub DESTROY { 628sub DESTROY {
604 my ($self) = @_; 629 my ($self) = @_;
605 630
606 return if CFPlus::in_destruct; 631 return if DC::in_destruct;
607 632
608 local $@; 633 local $@;
609 eval { $self->destroy }; 634 eval { $self->destroy };
610 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 635 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
611 636
612 delete $WIDGET{$self+0}; 637 delete $WIDGET{$self+0};
613} 638}
614 639
615############################################################################# 640#############################################################################
616 641
617package CFPlus::UI::DrawBG; 642package DC::UI::DrawBG;
618 643
619our @ISA = CFPlus::UI::Base::; 644our @ISA = DC::UI::Base::;
620 645
621use strict; 646use strict;
622use CFPlus::OpenGL; 647use DC::OpenGL;
623 648
624sub new { 649sub new {
625 my $class = shift; 650 my $class = shift;
626 651
627 $class->SUPER::new ( 652 $class->SUPER::new (
649 } 674 }
650} 675}
651 676
652############################################################################# 677#############################################################################
653 678
654package CFPlus::UI::Empty; 679package DC::UI::Empty;
655 680
656our @ISA = CFPlus::UI::Base::; 681our @ISA = DC::UI::Base::;
657 682
658sub new { 683sub new {
659 my ($class, %arg) = @_; 684 my ($class, %arg) = @_;
660 $class->SUPER::new (can_events => 0, %arg); 685 $class->SUPER::new (can_events => 0, %arg);
661} 686}
668 693
669sub draw { } 694sub draw { }
670 695
671############################################################################# 696#############################################################################
672 697
673package CFPlus::UI::Container; 698package DC::UI::Container;
674 699
675our @ISA = CFPlus::UI::Base::; 700our @ISA = DC::UI::Base::;
676 701
677sub new { 702sub new {
678 my ($class, %arg) = @_; 703 my ($class, %arg) = @_;
679 704
680 my $children = delete $arg{children}; 705 my $children = delete $arg{children};
774 $_->draw for $self->visible_children; 799 $_->draw for $self->visible_children;
775} 800}
776 801
777############################################################################# 802#############################################################################
778 803
779package CFPlus::UI::Bin; 804package DC::UI::Bin;
780 805
781our @ISA = CFPlus::UI::Container::; 806our @ISA = DC::UI::Container::;
782 807
783sub new { 808sub new {
784 my ($class, %arg) = @_; 809 my ($class, %arg) = @_;
785 810
786 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 811 my $child = (delete $arg{child}) || new DC::UI::Empty::;
787 812
788 $class->SUPER::new (children => [$child], %arg) 813 $class->SUPER::new (children => [$child], %arg)
789} 814}
790 815
791sub add { 816sub add {
798sub remove { 823sub remove {
799 my ($self, $widget) = @_; 824 my ($self, $widget) = @_;
800 825
801 $self->SUPER::remove ($widget); 826 $self->SUPER::remove ($widget);
802 827
803 $self->{children} = [new CFPlus::UI::Empty] 828 $self->{children} = [new DC::UI::Empty]
804 unless @{$self->{children}}; 829 unless @{$self->{children}};
805} 830}
806 831
807sub child { $_[0]->{children}[0] } 832sub child { $_[0]->{children}[0] }
808 833
817 842
818 1 843 1
819} 844}
820 845
821############################################################################# 846#############################################################################
822
823# back-buffered drawing area 847# back-buffered drawing area
824 848
825package CFPlus::UI::Window; 849package DC::UI::Window;
826 850
827our @ISA = CFPlus::UI::Bin::; 851our @ISA = DC::UI::Bin::;
828 852
829use CFPlus::OpenGL; 853use DC::OpenGL;
830 854
831sub new { 855sub new {
832 my ($class, %arg) = @_; 856 my ($class, %arg) = @_;
833 857
834 my $self = $class->SUPER::new (%arg); 858 my $self = $class->SUPER::new (%arg);
856} 880}
857 881
858sub render_child { 882sub render_child {
859 my ($self) = @_; 883 my ($self) = @_;
860 884
861 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 885 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
862 glClearColor 0, 0, 0, 0; 886 glClearColor 0, 0, 0, 0;
863 glClear GL_COLOR_BUFFER_BIT; 887 glClear GL_COLOR_BUFFER_BIT;
864 888
865 { 889 {
866 package CFPlus::UI::Base; 890 package DC::UI::Base;
867 891
868 local ($draw_x, $draw_y, $draw_w, $draw_h) = 892 local ($draw_x, $draw_y, $draw_w, $draw_h) =
869 (0, 0, $self->{w}, $self->{h}); 893 (0, 0, $self->{w}, $self->{h});
870 894
871 $self->_render; 895 $self->_render;
888 glDisable GL_TEXTURE_2D; 912 glDisable GL_TEXTURE_2D;
889} 913}
890 914
891############################################################################# 915#############################################################################
892 916
893package CFPlus::UI::ViewPort; 917package DC::UI::ViewPort;
894 918
895use List::Util qw(min max); 919use List::Util qw(min max);
896 920
897our @ISA = CFPlus::UI::Window::; 921our @ISA = DC::UI::Window::;
898 922
899sub new { 923sub new {
900 my $class = shift; 924 my $class = shift;
901 925
902 $class->SUPER::new ( 926 $class->SUPER::new (
986 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
987 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
988 ) { 1012 ) {
989 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1013 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
990 } else { 1014 } else {
991 $self->CFPlus::UI::Base::find_widget ($x, $y) 1015 $self->DC::UI::Base::find_widget ($x, $y)
992 } 1016 }
993} 1017}
994 1018
995sub _render { 1019sub _render {
996 my ($self) = @_; 1020 my ($self) = @_;
997 1021
998 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1022 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
999 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1023 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1000 1024
1001 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1025 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1002 1026
1003 $self->SUPER::_render; 1027 $self->SUPER::_render;
1004} 1028}
1005 1029
1006############################################################################# 1030#############################################################################
1007 1031
1008package CFPlus::UI::ScrolledWindow; 1032package DC::UI::ScrolledWindow;
1009 1033
1010our @ISA = CFPlus::UI::Table::; 1034our @ISA = DC::UI::Table::;
1011 1035
1012sub new { 1036sub new {
1013 my ($class, %arg) = @_; 1037 my ($class, %arg) = @_;
1014 1038
1015 my $child = delete $arg{child}; 1039 my $child = delete $arg{child};
1016 1040
1017 my $self; 1041 my $self;
1018 1042
1019 my $hslider = new CFPlus::UI::Slider 1043 my $hslider = new DC::UI::Slider
1020 c_col => 0, 1044 c_col => 0,
1021 c_row => 1, 1045 c_row => 1,
1022 vertical => 0, 1046 vertical => 0,
1023 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1048 on_changed => sub {
1025 $self->{hpos} = $_[1]; 1049 $self->{hpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1027 }, 1051 },
1028 ; 1052 ;
1029 1053
1030 my $vslider = new CFPlus::UI::Slider 1054 my $vslider = new DC::UI::Slider
1031 c_col => 1, 1055 c_col => 1,
1032 c_row => 0, 1056 c_row => 0,
1033 vertical => 1, 1057 vertical => 1,
1034 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1035 on_changed => sub { 1059 on_changed => sub {
1047 col_expand => [1, 0], 1071 col_expand => [1, 0],
1048 row_expand => [1, 0], 1072 row_expand => [1, 0],
1049 %arg, 1073 %arg,
1050 ); 1074 );
1051 1075
1052 $self->{vp} = new CFPlus::UI::ViewPort 1076 $self->{vp} = new DC::UI::ViewPort
1053 c_col => 0, 1077 c_col => 0,
1054 c_row => 0, 1078 c_row => 0,
1055 expand => 1, 1079 expand => 1,
1056 scroll_x => $self->{scroll_x}, 1080 scroll_x => $self->{scroll_x},
1057 scroll_y => $self->{scroll_y}, 1081 scroll_y => $self->{scroll_y},
1183 $self->SUPER::invoke_size_allocate ($w, $h) 1207 $self->SUPER::invoke_size_allocate ($w, $h)
1184} 1208}
1185 1209
1186############################################################################# 1210#############################################################################
1187 1211
1188package CFPlus::UI::Frame; 1212package DC::UI::Frame;
1189 1213
1190our @ISA = CFPlus::UI::Bin::; 1214our @ISA = DC::UI::Bin::;
1191 1215
1192use CFPlus::OpenGL; 1216use DC::OpenGL;
1193 1217
1194sub new { 1218sub new {
1195 my $class = shift; 1219 my $class = shift;
1196 1220
1197 $class->SUPER::new ( 1221 $class->SUPER::new (
1216 $self->SUPER::_draw; 1240 $self->SUPER::_draw;
1217} 1241}
1218 1242
1219############################################################################# 1243#############################################################################
1220 1244
1221package CFPlus::UI::FancyFrame; 1245package DC::UI::FancyFrame;
1222 1246
1223our @ISA = CFPlus::UI::Bin::; 1247our @ISA = DC::UI::Bin::;
1224 1248
1225use CFPlus::OpenGL; 1249use DC::OpenGL;
1226 1250
1227sub new { 1251sub new {
1228 my ($class, %arg) = @_; 1252 my ($class, %arg) = @_;
1229 1253
1230 if ((exists $arg{label}) && !ref $arg{label}) { 1254 if ((exists $arg{label}) && !ref $arg{label}) {
1231 $arg{label} = new CFPlus::UI::Label 1255 $arg{label} = new DC::UI::Label
1232 align => 1, 1256 align => 1,
1233 valign => 0, 1257 valign => 0.5,
1234 text => $arg{label}, 1258 text => $arg{label},
1235 fontsize => ($arg{border} || 0.8) * 0.75; 1259 fontsize => ($arg{border} || 0.8) * 0.75;
1236 } 1260 }
1237 1261
1238 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1239 # label => "", 1263 # label => "",
1240 fg => [0.6, 0.3, 0.1], 1264 fg => undef,
1241 border => 0.8, 1265 border => 0.8,
1242 style => 'single', 1266 style => 'single',
1243 %arg, 1267 %arg,
1244 ); 1268 );
1245 1269
1248 1272
1249sub add { 1273sub add {
1250 my ($self, @widgets) = @_; 1274 my ($self, @widgets) = @_;
1251 1275
1252 $self->SUPER::add (@widgets); 1276 $self->SUPER::add (@widgets);
1253 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1277 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1254} 1278}
1255 1279
1256sub border { 1280sub border {
1257 int $_[0]{border} * $::FONTSIZE 1281 int $_[0]{border} * $::FONTSIZE
1258} 1282}
1298 my $border = $self->border; 1322 my $border = $self->border;
1299 my ($w, $h) = ($self->{w}, $self->{h}); 1323 my ($w, $h) = ($self->{w}, $self->{h});
1300 1324
1301 $child->draw; 1325 $child->draw;
1302 1326
1303 glColor @{$self->{fg}}; 1327 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1304 glBegin GL_LINE_STRIP; 1328 glBegin GL_LINE_STRIP;
1305 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1329 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1306 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1330 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1307 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1331 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1308 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1332 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1316 } 1340 }
1317} 1341}
1318 1342
1319############################################################################# 1343#############################################################################
1320 1344
1321package CFPlus::UI::Toplevel; 1345package DC::UI::Toplevel;
1322 1346
1323our @ISA = CFPlus::UI::Bin::; 1347our @ISA = DC::UI::Bin::;
1324 1348
1325use CFPlus::OpenGL; 1349use DC::OpenGL;
1326 1350
1327my $bg = 1351my $bg =
1328 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1352 new_from_resource DC::Texture "d1_bg.png",
1329 mipmap => 1, wrap => 1; 1353 mipmap => 1, wrap => 1;
1330 1354
1331my @border = 1355my @border =
1332 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1356 map { new_from_resource DC::Texture $_, mipmap => 1 }
1333 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1357 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1334 1358
1335my @icon = 1359my @icon =
1336 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1360 map { new_from_resource DC::Texture $_, mipmap => 1 }
1337 qw(x1_move.png x1_resize.png); 1361 qw(x1_move.png x1_resize.png);
1338 1362
1339sub new { 1363sub new {
1340 my ($class, %arg) = @_; 1364 my ($class, %arg) = @_;
1341 1365
1342 my $self = $class->SUPER::new ( 1366 my $self = $class->SUPER::new (
1343 bg => [1, 1, 1, 1], 1367 bg => [1, 1, 1, 1],
1344 border_bg => [1, 1, 1, 1], 1368 border_bg => [1, 1, 1, 1],
1345 border => 0.6, 1369 border => 0.8,
1346 can_events => 1, 1370 can_events => 1,
1347 min_w => 64, 1371 min_w => 64,
1348 min_h => 32, 1372 min_h => 32,
1349 %arg, 1373 %arg,
1350 ); 1374 );
1351 1375
1352 $self->{title_widget} = new CFPlus::UI::Label 1376 $self->{title_widget} = new DC::UI::Label
1353 align => 0, 1377 align => 0.5,
1354 valign => 1, 1378 valign => 1,
1355 text => $self->{title}, 1379 text => $self->{title},
1356 fontsize => $self->{border}, 1380 fontsize => $self->{border},
1357 if exists $self->{title}; 1381 if exists $self->{title};
1358 1382
1359 if ($self->{has_close_button}) { 1383 if ($self->{has_close_button}) {
1360 $self->{close_button} = 1384 $self->{close_button} =
1361 new CFPlus::UI::ImageButton 1385 new DC::UI::ImageButton
1362 path => 'x1_close.png', 1386 path => 'x1_close.png',
1363 on_activate => sub { $self->emit ("delete") }; 1387 on_activate => sub { $self->emit ("delete") };
1364 1388
1365 $self->CFPlus::UI::Container::add ($self->{close_button}); 1389 $self->DC::UI::Container::add ($self->{close_button});
1366 } 1390 }
1367 1391
1368 $self 1392 $self
1369} 1393}
1370 1394
1371sub add { 1395sub add {
1372 my ($self, @widgets) = @_; 1396 my ($self, @widgets) = @_;
1373 1397
1374 $self->SUPER::add (@widgets); 1398 $self->SUPER::add (@widgets);
1375 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1399 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1376 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1400 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1377} 1401}
1378 1402
1379sub border { 1403sub border {
1380 int $_[0]{border} * $::FONTSIZE 1404 int $_[0]{border} * $::FONTSIZE
1405}
1406
1407sub get_max_wh {
1408 my ($self) = @_;
1409
1410 return ($self->{w}, $self->{h})
1411 if $self->{visible} && $self->{w};
1412
1413 $self->SUPER::get_max_wh
1381} 1414}
1382 1415
1383sub size_request { 1416sub size_request {
1384 my ($self) = @_; 1417 my ($self) = @_;
1385 1418
1557 if $self->{close_button}; 1590 if $self->{close_button};
1558} 1591}
1559 1592
1560############################################################################# 1593#############################################################################
1561 1594
1562package CFPlus::UI::Table; 1595package DC::UI::Table;
1563 1596
1564our @ISA = CFPlus::UI::Container::; 1597our @ISA = DC::UI::Container::;
1565 1598
1566use List::Util qw(max sum); 1599use List::Util qw(max sum);
1567 1600
1568use CFPlus::OpenGL; 1601use DC::OpenGL;
1569 1602
1570sub new { 1603sub new {
1571 my $class = shift; 1604 my $class = shift;
1572 1605
1573 $class->SUPER::new ( 1606 $class->SUPER::new (
1617 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1650 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1618 1651
1619 my $sw = sum @w[$c .. $c + $cs - 1]; 1652 my $sw = sum @w[$c .. $c + $cs - 1];
1620 1653
1621 if ($w > $sw) { 1654 if ($w > $sw) {
1622 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1655 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1623 } 1656 }
1624 } 1657 }
1625 1658
1626 # second pass, rows 1659 # second pass, rows
1627 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1660 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1628 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1661 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1629 1662
1630 my $sh = sum @h[$r .. $r + $rs - 1]; 1663 my $sh = sum @h[$r .. $r + $rs - 1];
1631 1664
1632 if ($h > $sh) { 1665 if ($h > $sh) {
1633 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1666 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1634 } 1667 }
1635 } 1668 }
1636 1669
1637 (\@w, \@h) 1670 (\@w, \@h)
1638} 1671}
1661 @col_expand = (1) x @$ws unless @col_expand; 1694 @col_expand = (1) x @$ws unless @col_expand;
1662 my $col_expand = (sum @col_expand) || 1; 1695 my $col_expand = (sum @col_expand) || 1;
1663 1696
1664 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1697 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1665 1698
1666 CFPlus::UI::harmonize $ws; 1699 DC::UI::harmonize $ws;
1667 1700
1668 my @row_expand = @{$self->{row_expand}}; 1701 my @row_expand = @{$self->{row_expand}};
1669 @row_expand = (1) x @$ws unless @row_expand; 1702 @row_expand = (1) x @$ws unless @row_expand;
1670 my $row_expand = (sum @row_expand) || 1; 1703 my $row_expand = (sum @row_expand) || 1;
1671 1704
1672 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1705 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1673 1706
1674 CFPlus::UI::harmonize $hs; 1707 DC::UI::harmonize $hs;
1675 1708
1676 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1709 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1677 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1710 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1678 1711
1679 for my $widget ($self->children) { 1712 for my $widget ($self->children) {
1688 1 1721 1
1689} 1722}
1690 1723
1691############################################################################# 1724#############################################################################
1692 1725
1693package CFPlus::UI::Fixed; 1726package DC::UI::Fixed;
1694 1727
1695use List::Util qw(min max); 1728use List::Util qw(min max);
1696 1729
1697our @ISA = CFPlus::UI::Container::; 1730our @ISA = DC::UI::Container::;
1698 1731
1699sub _scale($$$) { 1732sub _scale($$$) {
1700 my ($rel, $val, $max) = @_; 1733 my ($rel, $val, $max) = @_;
1701 1734
1702 $rel ? $val * $max : $val 1735 $rel ? $val * $max : $val
1754 1 1787 1
1755} 1788}
1756 1789
1757############################################################################# 1790#############################################################################
1758 1791
1759package CFPlus::UI::Box; 1792package DC::UI::Box;
1760 1793
1761our @ISA = CFPlus::UI::Container::; 1794our @ISA = DC::UI::Container::;
1762 1795
1763sub size_request { 1796sub size_request {
1764 my ($self) = @_; 1797 my ($self) = @_;
1765 1798
1766 my @children = $self->visible_children; 1799 my @children = $self->visible_children;
1801 $req[$_] += $space * $children[$_]{expand} 1834 $req[$_] += $space * $children[$_]{expand}
1802 for 0 .. $#children; 1835 for 0 .. $#children;
1803 } 1836 }
1804 } 1837 }
1805 1838
1806 CFPlus::UI::harmonize \@req; 1839 DC::UI::harmonize \@req;
1807 1840
1808 my $pos = 0; 1841 my $pos = 0;
1809 for (0 .. $#children) { 1842 for (0 .. $#children) {
1810 my $alloc = $req[$_]; 1843 my $alloc = $req[$_];
1811 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1844 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1816 1 1849 1
1817} 1850}
1818 1851
1819############################################################################# 1852#############################################################################
1820 1853
1821package CFPlus::UI::HBox; 1854package DC::UI::HBox;
1822 1855
1823our @ISA = CFPlus::UI::Box::; 1856our @ISA = DC::UI::Box::;
1824 1857
1825sub new { 1858sub new {
1826 my $class = shift; 1859 my $class = shift;
1827 1860
1828 $class->SUPER::new ( 1861 $class->SUPER::new (
1831 ) 1864 )
1832} 1865}
1833 1866
1834############################################################################# 1867#############################################################################
1835 1868
1836package CFPlus::UI::VBox; 1869package DC::UI::VBox;
1837 1870
1838our @ISA = CFPlus::UI::Box::; 1871our @ISA = DC::UI::Box::;
1839 1872
1840sub new { 1873sub new {
1841 my $class = shift; 1874 my $class = shift;
1842 1875
1843 $class->SUPER::new ( 1876 $class->SUPER::new (
1846 ) 1879 )
1847} 1880}
1848 1881
1849############################################################################# 1882#############################################################################
1850 1883
1851package CFPlus::UI::Label; 1884package DC::UI::Label;
1852 1885
1853our @ISA = CFPlus::UI::DrawBG::; 1886our @ISA = DC::UI::DrawBG::;
1854 1887
1855use CFPlus::OpenGL; 1888use DC::OpenGL;
1856 1889
1857sub new { 1890sub new {
1858 my ($class, %arg) = @_; 1891 my ($class, %arg) = @_;
1859 1892
1860 my $self = $class->SUPER::new ( 1893 my $self = $class->SUPER::new (
1865 #text => initial text 1898 #text => initial text
1866 #markup => initial narkup 1899 #markup => initial narkup
1867 #max_w => maximum pixel width 1900 #max_w => maximum pixel width
1868 #style => 0, # render flags 1901 #style => 0, # render flags
1869 ellipsise => 3, # end 1902 ellipsise => 3, # end
1870 layout => (new CFPlus::Layout), 1903 layout => (new DC::Layout),
1871 fontsize => 1, 1904 fontsize => 1,
1872 align => -1, 1905 align => 0.5,
1873 valign => -1, 1906 valign => 0.5,
1874 padding_x => 2, 1907 padding_x => 4,
1875 padding_y => 2, 1908 padding_y => 2,
1876 can_events => 0, 1909 can_events => 0,
1877 %arg 1910 %arg
1878 ); 1911 );
1879 1912
1880 if (exists $self->{template}) { 1913 if (exists $self->{template}) {
1881 my $layout = new CFPlus::Layout; 1914 my $layout = new DC::Layout;
1882 $layout->set_text (delete $self->{template}); 1915 $layout->set_text (delete $self->{template});
1883 $self->{template} = $layout; 1916 $self->{template} = $layout;
1884 } 1917 }
1885 1918
1886 if (exists $self->{markup}) { 1919 if (exists $self->{markup}) {
1942 1975
1943sub size_request { 1976sub size_request {
1944 my ($self) = @_; 1977 my ($self) = @_;
1945 1978
1946 $self->{size_req} ||= do { 1979 $self->{size_req} ||= do {
1980 my ($max_w, $max_h) = $self->get_max_wh;
1981
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1982 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1983 $self->{layout}->set_width ($max_w);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1984 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1985 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1986 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1987
1953 my ($w, $h) = $self->{layout}->size; 1988 my ($w, $h) = $self->{layout}->size;
1954 1989
1955 if (exists $self->{template}) { 1990 if (exists $self->{template}) {
1956 $self->{template}->set_font ($self->{font}) if $self->{font}; 1991 $self->{template}->set_font ($self->{font}) if $self->{font};
1957 $self->{template}->set_width ($self->{max_w} || -1); 1992 $self->{template}->set_width ($max_w);
1958 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1993 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1959 1994
1960 my ($w2, $h2) = $self->{template}->size; 1995 my ($w2, $h2) = $self->{template}->size;
1961 1996
1962 $w = List::Util::max $w, $w2; 1997 $w = List::Util::max $w, $w2;
2018 2053
2019 [$self->{layout}->size] 2054 [$self->{layout}->size]
2020 }; 2055 };
2021 2056
2022 unless (exists $self->{ox}) { 2057 unless (exists $self->{ox}) {
2023 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2058 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2024 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2059 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2025 : ($self->{w} - $size->[0]) * 0.5);
2026
2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2029 : ($self->{h} - $size->[1]) * 0.5);
2030 2060
2031 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2032 }; 2062 };
2033 2063
2034# unless ($self->{list}) { 2064# unless ($self->{list}) {
2035# $self->{list} = CFPlus::OpenGL::glGenList; 2065# $self->{list} = DC::OpenGL::glGenList;
2036# CFPlus::OpenGL::glNewList $self->{list}; 2066# DC::OpenGL::glNewList $self->{list};
2037# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2067# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2038# CFPlus::OpenGL::glEndList; 2068# DC::OpenGL::glEndList;
2039# } 2069# }
2040# 2070#
2041# CFPlus::OpenGL::glCallList $self->{list}; 2071# DC::OpenGL::glCallList $self->{list};
2042 2072
2043 $self->{layout}->draw; 2073 $self->{layout}->draw;
2044} 2074}
2045 2075
2046#sub destroy { 2076#sub destroy {
2047# my ($self) = @_; 2077# my ($self) = @_;
2048# 2078#
2049# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2079# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2050# 2080#
2051# $self->SUPER::destroy; 2081# $self->SUPER::destroy;
2052#} 2082#}
2053 2083
2054############################################################################# 2084#############################################################################
2055 2085
2056package CFPlus::UI::EntryBase; 2086package DC::UI::EntryBase;
2057 2087
2058our @ISA = CFPlus::UI::Label::; 2088our @ISA = DC::UI::Label::;
2059 2089
2060use CFPlus::OpenGL; 2090use DC::OpenGL;
2061 2091
2062sub new { 2092sub new {
2063 my $class = shift; 2093 my $class = shift;
2064 2094
2065 $class->SUPER::new ( 2095 $class->SUPER::new (
2066 fg => [1, 1, 1], 2096 fg => [1, 1, 1],
2067 bg => [0, 0, 0, 0.2], 2097 bg => [0, 0, 0, 0.2],
2068 outline => [0.6, 0.3, 0.1], 2098 outline => undef,
2069 active_bg => [0, 0, 1, .2], 2099 active_bg => [0, 0, 1, .2],
2070 active_fg => [1, 1, 1], 2100 active_fg => [1, 1, 1],
2071 active_outline => [1, 1, 0], 2101 active_outline => [1, 1, 0],
2072 can_hover => 1, 2102 can_hover => 1,
2073 can_focus => 1, 2103 can_focus => 1,
2104 align => 0,
2074 valign => 0, 2105 valign => 0.5,
2075 can_events => 1, 2106 can_events => 1,
2076 ellipsise => 0, 2107 ellipsise => 0,
2108 padding_x => 4,
2109 padding_y => 2,
2077 #text => ... 2110 #text => ...
2078 #hidden => "*", 2111 #hidden => "*",
2079 @_ 2112 @_
2080 ) 2113 )
2081} 2114}
2128 2161
2129 my $text = $self->get_text; 2162 my $text = $self->get_text;
2130 2163
2131 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2164 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2132 2165
2133 if ($uni == 8) { 2166 if ($sym == DC::SDLK_BACKSPACE) {
2134 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2167 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2135 } elsif ($uni == 127) { 2168 } elsif ($sym == DC::SDLK_DELETE) {
2136 substr $text, $self->{cursor}, 1, ""; 2169 substr $text, $self->{cursor}, 1, "";
2137 } elsif ($sym == CFPlus::SDLK_LEFT) { 2170 } elsif ($sym == DC::SDLK_LEFT) {
2138 --$self->{cursor} if $self->{cursor}; 2171 --$self->{cursor} if $self->{cursor};
2139 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2172 } elsif ($sym == DC::SDLK_RIGHT) {
2140 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2173 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2141 } elsif ($sym == CFPlus::SDLK_HOME) { 2174 } elsif ($sym == DC::SDLK_HOME) {
2142 # what a hack 2175 # what a hack
2143 $self->{cursor} = 2176 $self->{cursor} =
2144 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2177 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2145 ? length $1 2178 ? length $1
2146 : 0; 2179 : 0;
2147 } elsif ($sym == CFPlus::SDLK_END) { 2180 } elsif ($sym == DC::SDLK_END) {
2148 # uh, again 2181 # uh, again
2149 $self->{cursor} = 2182 $self->{cursor} =
2150 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2183 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2151 ? $self->{cursor} + length $1 2184 ? $self->{cursor} + length $1
2152 : length $self->{text}; 2185 : length $self->{text};
2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2266 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2234 } 2267 }
2235 2268
2236 glColor_premultiply @{$self->{active_fg}}; 2269 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2270 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2271 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}; 2272 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2273 glEnd;
2241 2274
2242 glLineWidth 3; 2275 glLineWidth 3;
2243 glColor @{$self->{active_outline}}; 2276 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2277 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2245 glLineWidth 1; 2278 glLineWidth 1;
2246 2279
2247 } else { 2280 } else {
2248 glColor @{$self->{outline}}; 2281 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP; 2282 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5; 2283 glVertex .5, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3; 2284 glVertex .5, $self->{h} - 2.5;
2253 glVertex $self->{w} - 1, $self->{h} - 3; 2285 glVertex $self->{w} - .5, $self->{h} - 2.5;
2254 glVertex $self->{w} - 1, $self->{h} * .5; 2286 glVertex $self->{w} - .5, $self->{h} * .5;
2255 glEnd; 2287 glEnd;
2256 } 2288 }
2257} 2289}
2258 2290
2259############################################################################# 2291#############################################################################
2260 2292
2261package CFPlus::UI::Entry; 2293package DC::UI::Entry;
2262 2294
2263our @ISA = CFPlus::UI::EntryBase::; 2295our @ISA = DC::UI::EntryBase::;
2264 2296
2265use CFPlus::OpenGL; 2297use DC::OpenGL;
2298
2299sub new {
2300 my $class = shift;
2301
2302 $class->SUPER::new (
2303 history_pointer => -1,
2304 @_
2305 )
2306}
2307
2266 2308
2267sub invoke_key_down { 2309sub invoke_key_down {
2268 my ($self, $ev) = @_; 2310 my ($self, $ev) = @_;
2269 2311
2270 my $sym = $ev->{sym}; 2312 my $sym = $ev->{sym};
2276 $self->{history_pointer} = -1; 2318 $self->{history_pointer} = -1;
2277 $self->{history_saveback} = ''; 2319 $self->{history_saveback} = '';
2278 $self->emit (activate => $txt); 2320 $self->emit (activate => $txt);
2279 $self->update; 2321 $self->update;
2280 2322
2281 } elsif ($sym == CFPlus::SDLK_UP) { 2323 } elsif ($sym == DC::SDLK_UP) {
2282 if ($self->{history_pointer} < 0) { 2324 if ($self->{history_pointer} < 0) {
2283 $self->{history_saveback} = $self->get_text; 2325 $self->{history_saveback} = $self->get_text;
2284 } 2326 }
2285 if (@{$self->{history} || []} > 0) { 2327 if (@{$self->{history} || []} > 0) {
2286 $self->{history_pointer}++; 2328 $self->{history_pointer}++;
2288 $self->{history_pointer} = @{$self->{history} || []} - 1; 2330 $self->{history_pointer} = @{$self->{history} || []} - 1;
2289 } 2331 }
2290 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2332 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2291 } 2333 }
2292 2334
2293 } elsif ($sym == CFPlus::SDLK_DOWN) { 2335 } elsif ($sym == DC::SDLK_DOWN) {
2294 $self->{history_pointer}--; 2336 $self->{history_pointer}--;
2295 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2337 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2296 2338
2297 if ($self->{history_pointer} >= 0) { 2339 if ($self->{history_pointer} >= 0) {
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2340 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2299 } else { 2341 } else {
2342 if (defined $self->{history_saveback}) {
2300 $self->set_text ($self->{history_saveback}); 2343 $self->set_text ($self->{history_saveback});
2344 $self->{history_saveback} = undef;
2345 }
2301 } 2346 }
2302 2347
2303 } else { 2348 } else {
2304 return $self->SUPER::invoke_key_down ($ev) 2349 return $self->SUPER::invoke_key_down ($ev)
2305 } 2350 }
2307 1 2352 1
2308} 2353}
2309 2354
2310############################################################################# 2355#############################################################################
2311 2356
2312package CFPlus::UI::TextEdit; 2357package DC::UI::TextEdit;
2313 2358
2314our @ISA = CFPlus::UI::EntryBase::; 2359our @ISA = DC::UI::EntryBase::;
2315 2360
2316use CFPlus::OpenGL; 2361use DC::OpenGL;
2362
2363sub new {
2364 my $class = shift;
2365
2366 $class->SUPER::new (
2367 padding_y => 4,
2368
2369 @_
2370 )
2371}
2317 2372
2318sub move_cursor_ver { 2373sub move_cursor_ver {
2319 my ($self, $dy) = @_; 2374 my ($self, $dy) = @_;
2320 2375
2321 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2376 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2322 2377
2323 $y += $dy; 2378 $line += $dy;
2324 2379
2325 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2380 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2326 $self->{cursor} = $index; 2381 $self->{cursor} = $index;
2327 delete $self->{cur_h}; 2382 delete $self->{cur_h};
2328 $self->update; 2383 $self->update;
2329 return; 2384 return;
2330 } 2385 }
2333sub invoke_key_down { 2388sub invoke_key_down {
2334 my ($self, $ev) = @_; 2389 my ($self, $ev) = @_;
2335 2390
2336 my $sym = $ev->{sym}; 2391 my $sym = $ev->{sym};
2337 2392
2338 if ($sym == CFPlus::SDLK_UP) { 2393 if ($sym == DC::SDLK_UP) {
2339 $self->move_cursor_ver (-1); 2394 $self->move_cursor_ver (-1);
2340 } elsif ($sym == CFPlus::SDLK_DOWN) { 2395 } elsif ($sym == DC::SDLK_DOWN) {
2341 $self->move_cursor_ver (+1); 2396 $self->move_cursor_ver (+1);
2342 } else { 2397 } else {
2343 return $self->SUPER::invoke_key_down ($ev) 2398 return $self->SUPER::invoke_key_down ($ev)
2344 } 2399 }
2345 2400
2346 1 2401 1
2347} 2402}
2348 2403
2349############################################################################# 2404#############################################################################
2350 2405
2351package CFPlus::UI::ButtonBin; 2406package DC::UI::ButtonBin;
2352 2407
2353our @ISA = CFPlus::UI::Bin::; 2408our @ISA = DC::UI::Bin::;
2354 2409
2355use CFPlus::OpenGL; 2410use DC::OpenGL;
2356 2411
2357my @tex = 2412my @tex =
2358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2413 map { new_from_resource DC::Texture $_, mipmap => 1 }
2359 qw(b1_button_inactive.png b1_button_active.png); 2414 qw(b1_button_inactive.png b1_button_active.png);
2360 2415
2361sub new { 2416sub new {
2362 my $class = shift; 2417 my $class = shift;
2363 2418
2364 $class->SUPER::new ( 2419 $class->SUPER::new (
2365 can_hover => 1, 2420 can_hover => 1,
2366 align => 0, 2421 align => 0.5,
2367 valign => 0, 2422 valign => 0.5,
2368 can_events => 1, 2423 can_events => 1,
2369 @_ 2424 @_
2370 ) 2425 )
2371} 2426}
2372 2427
2395 $self->SUPER::_draw; 2450 $self->SUPER::_draw;
2396} 2451}
2397 2452
2398############################################################################# 2453#############################################################################
2399 2454
2400package CFPlus::UI::Button; 2455package DC::UI::Button;
2401 2456
2402our @ISA = CFPlus::UI::Label::; 2457our @ISA = DC::UI::Label::;
2403 2458
2404use CFPlus::OpenGL; 2459use DC::OpenGL;
2405 2460
2406my @tex = 2461my @tex =
2407 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2462 map { new_from_resource DC::Texture $_, mipmap => 1 }
2408 qw(b1_button_inactive.png b1_button_active.png); 2463 qw(b1_button_inactive.png b1_button_active.png);
2409 2464
2410sub new { 2465sub new {
2411 my $class = shift; 2466 my $class = shift;
2412 2467
2413 $class->SUPER::new ( 2468 $class->SUPER::new (
2414 padding_x => 4, 2469 padding_x => 8,
2415 padding_y => 4, 2470 padding_y => 4,
2416 fg => [1.0, 1.0, 1.0], 2471 fg => [1.0, 1.0, 1.0],
2417 active_fg => [0.8, 0.8, 0.8], 2472 active_fg => [0.8, 0.8, 0.8],
2418 can_hover => 1, 2473 can_hover => 1,
2419 align => 0, 2474 align => 0.5,
2420 valign => 0, 2475 valign => 0.5,
2421 can_events => 1, 2476 can_events => 1,
2422 @_ 2477 @_
2423 ) 2478 )
2424} 2479}
2425 2480
2450 $self->SUPER::_draw; 2505 $self->SUPER::_draw;
2451} 2506}
2452 2507
2453############################################################################# 2508#############################################################################
2454 2509
2455package CFPlus::UI::CheckBox; 2510package DC::UI::CheckBox;
2456 2511
2457our @ISA = CFPlus::UI::DrawBG::; 2512our @ISA = DC::UI::DrawBG::;
2458 2513
2459my @tex = 2514my @tex =
2460 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2515 map { new_from_resource DC::Texture $_, mipmap => 1 }
2461 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2516 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2462 2517
2463use CFPlus::OpenGL; 2518use DC::OpenGL;
2464 2519
2465sub new { 2520sub new {
2466 my $class = shift; 2521 my $class = shift;
2467 2522
2468 $class->SUPER::new ( 2523 $class->SUPER::new (
2508sub _draw { 2563sub _draw {
2509 my ($self) = @_; 2564 my ($self) = @_;
2510 2565
2511 $self->SUPER::_draw; 2566 $self->SUPER::_draw;
2512 2567
2513 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2568 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2514 2569
2515 my ($w, $h) = @$self{qw(w h)}; 2570 my ($w, $h) = @$self{qw(w h)};
2516 2571
2517 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2572 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2518 2573
2525 glDisable GL_TEXTURE_2D; 2580 glDisable GL_TEXTURE_2D;
2526} 2581}
2527 2582
2528############################################################################# 2583#############################################################################
2529 2584
2530package CFPlus::UI::Image; 2585package DC::UI::Image;
2531 2586
2532our @ISA = CFPlus::UI::Base::; 2587our @ISA = DC::UI::Base::;
2533 2588
2534use CFPlus::OpenGL; 2589use DC::OpenGL;
2535 2590
2536our %texture_cache; 2591our %texture_cache;
2537 2592
2538sub new { 2593sub new {
2539 my $class = shift; 2594 my $class = shift;
2546 2601
2547 $self->{path} || $self->{tex} 2602 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2603 or Carp::croak "'path' or 'tex' attributes required";
2549 2604
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2605 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2606 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2552 2607
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2608 DC::weaken $texture_cache{$self->{path}};
2554 2609
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2610 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2611
2557 $self 2612 $self
2558} 2613}
2559 2614
2560sub STORABLE_freeze { 2615sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2616 my ($self, $cloning) = @_;
2562 2617
2563 $self->{path} 2618 $self->{path}
2564 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2619 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2565 2620
2566 $self->{path} 2621 $self->{path}
2567} 2622}
2568 2623
2569sub STORABLE_attach { 2624sub STORABLE_attach {
2600 glDisable GL_TEXTURE_2D; 2655 glDisable GL_TEXTURE_2D;
2601} 2656}
2602 2657
2603############################################################################# 2658#############################################################################
2604 2659
2605package CFPlus::UI::ImageButton; 2660package DC::UI::ImageButton;
2606 2661
2607our @ISA = CFPlus::UI::Image::; 2662our @ISA = DC::UI::Image::;
2608 2663
2609use CFPlus::OpenGL; 2664use DC::OpenGL;
2610 2665
2611my %textures; 2666my %textures;
2612 2667
2613sub new { 2668sub new {
2614 my $class = shift; 2669 my $class = shift;
2617 padding_x => 4, 2672 padding_x => 4,
2618 padding_y => 4, 2673 padding_y => 4,
2619 fg => [1, 1, 1], 2674 fg => [1, 1, 1],
2620 active_fg => [0, 0, 1], 2675 active_fg => [0, 0, 1],
2621 can_hover => 1, 2676 can_hover => 1,
2622 align => 0, 2677 align => 0.5,
2623 valign => 0, 2678 valign => 0.5,
2624 can_events => 1, 2679 can_events => 1,
2625 @_ 2680 @_
2626 ); 2681 );
2627} 2682}
2628 2683
2642 1 2697 1
2643} 2698}
2644 2699
2645############################################################################# 2700#############################################################################
2646 2701
2647package CFPlus::UI::VGauge; 2702package DC::UI::VGauge;
2648 2703
2649our @ISA = CFPlus::UI::Base::; 2704our @ISA = DC::UI::Base::;
2650 2705
2651use List::Util qw(min max); 2706use List::Util qw(min max);
2652 2707
2653use CFPlus::OpenGL; 2708use DC::OpenGL;
2654 2709
2655my %tex = ( 2710my %tex = (
2656 food => [ 2711 food => [
2657 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2712 map { new_from_resource DC::Texture $_, mipmap => 1 }
2658 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2713 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2659 ], 2714 ],
2660 grace => [ 2715 grace => [
2661 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2716 map { new_from_resource DC::Texture $_, mipmap => 1 }
2662 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2717 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2663 ], 2718 ],
2664 hp => [ 2719 hp => [
2665 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2720 map { new_from_resource DC::Texture $_, mipmap => 1 }
2666 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2721 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2667 ], 2722 ],
2668 mana => [ 2723 mana => [
2669 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2670 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2725 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2671 ], 2726 ],
2672); 2727);
2673 2728
2674# eg. VGauge->new (gauge => 'food'), default gauge: food 2729# eg. VGauge->new (gauge => 'food'), default gauge: food
2777 glDisable GL_TEXTURE_2D; 2832 glDisable GL_TEXTURE_2D;
2778} 2833}
2779 2834
2780############################################################################# 2835#############################################################################
2781 2836
2782package CFPlus::UI::Progress; 2837package DC::UI::Progress;
2783 2838
2784our @ISA = CFPlus::UI::Label::; 2839our @ISA = DC::UI::Label::;
2785 2840
2786use CFPlus::OpenGL; 2841use DC::OpenGL;
2787 2842
2788sub new { 2843sub new {
2789 my ($class, %arg) = @_; 2844 my ($class, %arg) = @_;
2790 2845
2791 my $self = $class->SUPER::new ( 2846 my $self = $class->SUPER::new (
2792 fg => [1, 1, 1], 2847 fg => [1, 1, 1],
2793 bg => [0, 0, 1, 0.2], 2848 bg => [0, 0, 1, 0.2],
2794 bar => [0.7, 0.5, 0.1, 0.8], 2849 bar => [0.7, 0.5, 0.1, 0.8],
2795 outline => [0.4, 0.3, 0], 2850 outline => [0.4, 0.3, 0],
2796 fontsize => 0.9, 2851 fontsize => 0.9,
2797 valign => 0, 2852 valign => 0.5,
2798 align => 0, 2853 align => 0.5,
2799 can_events => 1, 2854 can_events => 1,
2800 ellipsise => 1, 2855 ellipsise => 1,
2801 label => "%d%%", 2856 label => "%d%%",
2802 %arg, 2857 %arg,
2803 ); 2858 );
2811 my ($self, $label) = @_; 2866 my ($self, $label) = @_;
2812 2867
2813 return if $self->{label} eq $label; 2868 return if $self->{label} eq $label;
2814 $self->{label} = $label; 2869 $self->{label} = $label;
2815 2870
2816 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2871 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2817} 2872}
2818 2873
2819sub set_value { 2874sub set_value {
2820 my ($self, $value) = @_; 2875 my ($self, $value) = @_;
2821 2876
2837 2892
2838 glEnable GL_BLEND; 2893 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2894 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840 2895
2841 if ($self->{value} >= 0) { 2896 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2897 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2843 2898
2844 glColor_premultiply @{$self->{bar}}; 2899 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2; 2900 glRect 2, 2, $s, $self->{h} - 2;
2846 glColor_premultiply @{$self->{bg}}; 2901 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2902 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2848 } 2903 }
2849 2904
2850 glColor_premultiply @{$self->{outline}}; 2905 glColor_premultiply @{$self->{outline}};
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2906 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2852 2907
2853 glDisable GL_BLEND; 2908 glDisable GL_BLEND;
2854 2909
2855 { 2910 {
2856 local $self->{bg}; # do not draw background 2911 local $self->{bg}; # do not draw background
2858 } 2913 }
2859} 2914}
2860 2915
2861############################################################################# 2916#############################################################################
2862 2917
2863package CFPlus::UI::ExperienceProgress; 2918package DC::UI::ExperienceProgress;
2864 2919
2865our @ISA = CFPlus::UI::Progress::; 2920our @ISA = DC::UI::Progress::;
2866 2921
2867sub new { 2922sub new {
2868 my ($class, %arg) = @_; 2923 my ($class, %arg) = @_;
2869 2924
2870 my $self = $class->SUPER::new ( 2925 my $self = $class->SUPER::new (
2915 $self->SUPER::set_value ($v); 2970 $self->SUPER::set_value ($v);
2916} 2971}
2917 2972
2918############################################################################# 2973#############################################################################
2919 2974
2920package CFPlus::UI::Gauge; 2975package DC::UI::Gauge;
2921 2976
2922our @ISA = CFPlus::UI::VBox::; 2977our @ISA = DC::UI::VBox::;
2923 2978
2924sub new { 2979sub new {
2925 my ($class, %arg) = @_; 2980 my ($class, %arg) = @_;
2926 2981
2927 my $self = $class->SUPER::new ( 2982 my $self = $class->SUPER::new (
2929 can_hover => 1, 2984 can_hover => 1,
2930 can_events => 1, 2985 can_events => 1,
2931 %arg, 2986 %arg,
2932 ); 2987 );
2933 2988
2934 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2989 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2935 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2990 $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"); 2991 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2937 2992
2938 $self 2993 $self
2939} 2994}
2940 2995
2941sub set_fontsize { 2996sub set_fontsize {
2962 $self->{value}->set_text ($val); 3017 $self->{value}->set_text ($val);
2963} 3018}
2964 3019
2965############################################################################# 3020#############################################################################
2966 3021
2967package CFPlus::UI::Slider; 3022package DC::UI::Slider;
2968 3023
2969use strict; 3024use strict;
2970 3025
2971use CFPlus::OpenGL; 3026use DC::OpenGL;
2972 3027
2973our @ISA = CFPlus::UI::DrawBG::; 3028our @ISA = DC::UI::DrawBG::;
2974 3029
2975my @tex = 3030my @tex =
2976 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3031 map { new_from_resource DC::Texture $_ }
2977 qw(s1_slider.png s1_slider_bg.png); 3032 qw(s1_slider.png s1_slider_bg.png);
2978 3033
2979sub new { 3034sub new {
2980 my $class = shift; 3035 my $class = shift;
2981 3036
3018sub set_value { 3073sub set_value {
3019 my ($self, $value) = @_; 3074 my ($self, $value) = @_;
3020 3075
3021 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3076 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3022 3077
3023 $hi = $lo + 1 if $hi <= $lo; 3078 $hi = $lo if $hi < $lo;
3024 3079
3025 $page = $hi - $lo if $page > $hi - $lo; 3080 $value = $hi - $page if $value > $hi - $page;
3026
3027 $value = $lo if $value < $lo; 3081 $value = $lo if $value < $lo;
3028 $value = $hi - $page if $value > $hi - $page;
3029 3082
3030 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3083 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3031 if $unit; 3084 if $unit;
3032 3085
3033 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3086 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3077sub invoke_mouse_wheel { 3130sub invoke_mouse_wheel {
3078 my ($self, $ev) = @_; 3131 my ($self, $ev) = @_;
3079 3132
3080 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3133 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3081 3134
3082 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3135 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3083 3136
3084 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3137 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3085 3138
3086 1 3139 1
3087} 3140}
3097 my ($self) = @_; 3150 my ($self) = @_;
3098 3151
3099 unless ($self->{knob_w}) { 3152 unless ($self->{knob_w}) {
3100 $self->set_value ($self->{range}[0]); 3153 $self->set_value ($self->{range}[0]);
3101 3154
3102 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3155 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3103 my $range = ($hi - $page - $lo) || 1e-100; 3156 my $range = ($hi - $page - $lo) || 1e-10;
3104 3157
3105 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3158 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3106 3159
3107 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3160 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3108 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3161 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3109 3162
3110 $value = ($value - $lo) / $range; 3163 $value = ($value - $lo) / $range;
3140 glDisable GL_TEXTURE_2D; 3193 glDisable GL_TEXTURE_2D;
3141} 3194}
3142 3195
3143############################################################################# 3196#############################################################################
3144 3197
3145package CFPlus::UI::ValSlider; 3198package DC::UI::ValSlider;
3146 3199
3147our @ISA = CFPlus::UI::HBox::; 3200our @ISA = DC::UI::HBox::;
3148 3201
3149sub new { 3202sub new {
3150 my ($class, %arg) = @_; 3203 my ($class, %arg) = @_;
3151 3204
3152 my $range = delete $arg{range}; 3205 my $range = delete $arg{range};
3153 3206
3154 my $self = $class->SUPER::new ( 3207 my $self = $class->SUPER::new (
3155 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3208 slider => (new DC::UI::Slider expand => 1, range => $range),
3156 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3209 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3157 to_value => sub { shift }, 3210 to_value => sub { shift },
3158 from_value => sub { shift }, 3211 from_value => sub { shift },
3159 %arg, 3212 %arg,
3160 ); 3213 );
3161 3214
3181sub set_range { shift->{slider}->set_range (@_) } 3234sub set_range { shift->{slider}->set_range (@_) }
3182sub set_value { shift->{slider}->set_value (@_) } 3235sub set_value { shift->{slider}->set_value (@_) }
3183 3236
3184############################################################################# 3237#############################################################################
3185 3238
3186package CFPlus::UI::TextScroller; 3239package DC::UI::TextScroller;
3187 3240
3188our @ISA = CFPlus::UI::HBox::; 3241our @ISA = DC::UI::HBox::;
3189 3242
3190use CFPlus::OpenGL; 3243use DC::OpenGL;
3191 3244
3192sub new { 3245sub new {
3193 my $class = shift; 3246 my $class = shift;
3194 3247
3195 my $self = $class->SUPER::new ( 3248 my $self = $class->SUPER::new (
3197 can_events => 1, 3250 can_events => 1,
3198 indent => 0, 3251 indent => 0,
3199 #font => default_font 3252 #font => default_font
3200 @_, 3253 @_,
3201 3254
3202 layout => (new CFPlus::Layout), 3255 layout => (new DC::Layout),
3203 par => [], 3256 par => [],
3204 max_par => 0, 3257 max_par => 0,
3205 height => 0, 3258 height => 0,
3206 children => [ 3259 children => [
3207 (new CFPlus::UI::Empty expand => 1), 3260 (new DC::UI::Empty expand => 1),
3208 (new CFPlus::UI::Slider vertical => 1), 3261 (new DC::UI::Slider vertical => 1),
3209 ], 3262 ],
3210 ); 3263 );
3211 3264
3212 $self->{children}[1]->connect (changed => sub { $self->update }); 3265 $self->{children}[1]->connect (changed => sub { $self->update });
3213 3266
3269 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3322 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3270 $layout->set_markup ($para->{markup}); 3323 $layout->set_markup ($para->{markup});
3271 3324
3272 $layout->set_shapes ( 3325 $layout->set_shapes (
3273 map 3326 map
3274 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3327 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3275 @{$para->{widget}} 3328 @{$para->{widget}}
3276 ); 3329 );
3277 3330
3278 $layout 3331 $layout
3279} 3332}
3396 $ROOT->on_post_alloc ($self => sub { 3449 $ROOT->on_post_alloc ($self => sub {
3397 $self->force_uptodate; 3450 $self->force_uptodate;
3398 3451
3399 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3452 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3400 3453
3401 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3454 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3402 glClearColor 0, 0, 0, 0; 3455 glClearColor 0, 0, 0, 0;
3403 glClear GL_COLOR_BUFFER_BIT; 3456 glClear GL_COLOR_BUFFER_BIT;
3404 3457
3405 package CFPlus::UI::Base; 3458 package DC::UI::Base;
3406 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3459 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3407 (0, 0, $self->{w}, $self->{h}); 3460 (0, 0, $self->{w}, $self->{h});
3408 3461
3409 my $top = int $self->{children}[1]{range}[0]; 3462 my $top = int $self->{children}[1]{range}[0];
3410 3463
3469 $self->{children}[1]->draw; 3522 $self->{children}[1]->draw;
3470} 3523}
3471 3524
3472############################################################################# 3525#############################################################################
3473 3526
3474package CFPlus::UI::Animator; 3527package DC::UI::Animator;
3475 3528
3476use CFPlus::OpenGL; 3529use DC::OpenGL;
3477 3530
3478our @ISA = CFPlus::UI::Bin::; 3531our @ISA = DC::UI::Bin::;
3479 3532
3480sub moveto { 3533sub moveto {
3481 my ($self, $x, $y) = @_; 3534 my ($self, $x, $y) = @_;
3482 3535
3483 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3536 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3511 glPopMatrix; 3564 glPopMatrix;
3512} 3565}
3513 3566
3514############################################################################# 3567#############################################################################
3515 3568
3516package CFPlus::UI::Flopper; 3569package DC::UI::Flopper;
3517 3570
3518our @ISA = CFPlus::UI::Button::; 3571our @ISA = DC::UI::Button::;
3519 3572
3520sub new { 3573sub new {
3521 my $class = shift; 3574 my $class = shift;
3522 3575
3523 my $self = $class->SUPER::new ( 3576 my $self = $class->SUPER::new (
3535 $self->{other}->toggle_visibility; 3588 $self->{other}->toggle_visibility;
3536} 3589}
3537 3590
3538############################################################################# 3591#############################################################################
3539 3592
3540package CFPlus::UI::Tooltip; 3593package DC::UI::Tooltip;
3541 3594
3542our @ISA = CFPlus::UI::Bin::; 3595our @ISA = DC::UI::Bin::;
3543 3596
3544use CFPlus::OpenGL; 3597use DC::OpenGL;
3545 3598
3546sub new { 3599sub new {
3547 my $class = shift; 3600 my $class = shift;
3548 3601
3549 $class->SUPER::new ( 3602 $class->SUPER::new (
3556 my ($self, $widget) = @_; 3609 my ($self, $widget) = @_;
3557 3610
3558 my $tip = $widget->{tooltip}; 3611 my $tip = $widget->{tooltip};
3559 $tip = $tip->($widget) if "CODE" eq ref $tip; 3612 $tip = $tip->($widget) if "CODE" eq ref $tip;
3560 3613
3561 $tip = CFPlus::Pod::section_label tooltip => $1 3614 $tip = DC::Pod::section_label tooltip => $1
3562 if $tip =~ /^#(.*)$/; 3615 if $tip =~ /^#(.*)$/;
3563 3616
3564 if ($ENV{CFPLUS_DEBUG} & 2) { 3617 if ($ENV{CFPLUS_DEBUG} & 2) {
3565 $tip .= "\n\n" . (ref $widget) . "\n" 3618 $tip .= "\n\n" . (ref $widget) . "\n"
3566 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3619 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3569 } 3622 }
3570 3623
3571 $tip =~ s/^\n+//; 3624 $tip =~ s/^\n+//;
3572 $tip =~ s/\n+$//; 3625 $tip =~ s/\n+$//;
3573 3626
3574 $self->add (new CFPlus::UI::Label 3627 $self->add (new DC::UI::Label
3628 fg => $DC::THEME{tooltip_fg},
3575 markup => $tip, 3629 markup => $tip,
3576 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3630 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3631 align => 0,
3577 fontsize => 0.8, 3632 fontsize => 0.8,
3578 style => 1, # FLAG_INVERSE 3633 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3579 ellipsise => 0, 3634 ellipsise => 0,
3580 font => ($widget->{tooltip_font} || $::FONT_PROP), 3635 font => ($widget->{tooltip_font} || $::FONT_PROP),
3581 ); 3636 );
3582} 3637}
3583 3638
3618} 3673}
3619 3674
3620sub _draw { 3675sub _draw {
3621 my ($self) = @_; 3676 my ($self) = @_;
3622 3677
3623 glTranslate 0.375, 0.375;
3624
3625 my ($w, $h) = @$self{qw(w h)}; 3678 my ($w, $h) = @$self{qw(w h)};
3626 3679
3627 glColor 1, 0.8, 0.4; 3680 glColor @{ $DC::THEME{tooltip_bg} };
3628 glRect 0, 0, $w, $h; 3681 glRect 0, 0, $w, $h;
3629 3682
3630 glColor 0, 0, 0; 3683 glColor @{ $DC::THEME{tooltip_border} };
3631 glRect_lineloop 0, 0, $w, $h; 3684 glRect_lineloop .5, .5, $w + .5, $h + .5;
3632 3685
3633 glTranslate 2 - 0.375, 2 - 0.375; 3686 glTranslate 2, 2;
3634 3687
3635 $self->SUPER::_draw; 3688 $self->SUPER::_draw;
3636} 3689}
3637 3690
3638############################################################################# 3691#############################################################################
3639 3692
3640package CFPlus::UI::Face; 3693package DC::UI::Face;
3641 3694
3642our @ISA = CFPlus::UI::DrawBG::; 3695our @ISA = DC::UI::DrawBG::;
3643 3696
3644use CFPlus::OpenGL; 3697use DC::OpenGL;
3645 3698
3646sub new { 3699sub new {
3647 my $class = shift; 3700 my $class = shift;
3648 3701
3649 my $self = $class->SUPER::new ( 3702 my $self = $class->SUPER::new (
3653 can_events => 0, 3706 can_events => 0,
3654 @_, 3707 @_,
3655 ); 3708 );
3656 3709
3657 if ($self->{anim} && $self->{animspeed}) { 3710 if ($self->{anim} && $self->{animspeed}) {
3658 CFPlus::weaken (my $widget = $self); 3711 DC::weaken (my $widget = $self);
3659 3712
3660 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3713 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3661 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3714 $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; 3715 return unless $::CONN;
3666 3716
3667 my $w = $widget 3717 my $w = $widget
3668 or return; 3718 or return;
3669 3719
3670 ++$w->{frame}; 3720 ++$w->{frame};
3671 $w->update_face; 3721 $w->update_face;
3672 3722
3673 # somehow, $widget can go away 3723 # somehow, $widget can go away
3674 $w->update; 3724 $w->update;
3675 $w->update_timer; 3725 $w->update_timer;
3676 },
3677 ); 3726 };
3678 3727
3679 $self->update_face; 3728 $self->update_face;
3680 $self->update_timer; 3729 $self->update_timer;
3681 } 3730 }
3682 3731
3687 my ($self) = @_; 3736 my ($self) = @_;
3688 3737
3689 return unless $self->{timer}; 3738 return unless $self->{timer};
3690 3739
3691 if ($self->{visible}) { 3740 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; 3741 $self->{timer}->start;
3698 } else { 3742 } else {
3699 $self->{timer}->stop; 3743 $self->{timer}->stop;
3700 } 3744 }
3701} 3745}
3778 $self->SUPER::destroy; 3822 $self->SUPER::destroy;
3779} 3823}
3780 3824
3781############################################################################# 3825#############################################################################
3782 3826
3783package CFPlus::UI::Buttonbar; 3827package DC::UI::Buttonbar;
3784 3828
3785our @ISA = CFPlus::UI::HBox::; 3829our @ISA = DC::UI::HBox::;
3786 3830
3787# TODO: should actually wrap buttons and other goodies. 3831# TODO: should actually wrap buttons and other goodies.
3788 3832
3789############################################################################# 3833#############################################################################
3790 3834
3791package CFPlus::UI::Menu; 3835package DC::UI::Menu;
3792 3836
3793our @ISA = CFPlus::UI::Toplevel::; 3837our @ISA = DC::UI::Toplevel::;
3794 3838
3795use CFPlus::OpenGL; 3839use DC::OpenGL;
3796 3840
3797sub new { 3841sub new {
3798 my $class = shift; 3842 my $class = shift;
3799 3843
3800 my $self = $class->SUPER::new ( 3844 my $self = $class->SUPER::new (
3801 items => [], 3845 items => [],
3802 z => 100, 3846 z => 100,
3803 @_, 3847 @_,
3804 ); 3848 );
3805 3849
3806 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3850 $self->add ($self->{vbox} = new DC::UI::VBox);
3807 3851
3808 for my $item (@{ $self->{items} }) { 3852 for my $item (@{ $self->{items} }) {
3809 my ($widget, $cb, $tooltip) = @$item; 3853 my ($widget, $cb, $tooltip) = @$item;
3810 3854
3811 # handle various types of items, only text for now 3855 # handle various types of items, only text for now
3812 if (!ref $widget) { 3856 if (!ref $widget) {
3813 if ($widget =~ /\t/) { 3857 if ($widget =~ /\t/) {
3814 my ($left, $right) = split /\t/, $widget, 2; 3858 my ($left, $right) = split /\t/, $widget, 2;
3815 3859
3816 $widget = new CFPlus::UI::HBox 3860 $widget = new DC::UI::HBox
3817 can_hover => 1, 3861 can_hover => 1,
3818 can_events => 1, 3862 can_events => 1,
3819 tooltip => $tooltip, 3863 tooltip => $tooltip,
3820 children => [ 3864 children => [
3821 (new CFPlus::UI::Label markup => $left, expand => 1), 3865 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3822 (new CFPlus::UI::Label markup => $right, align => +1), 3866 (new DC::UI::Label markup => $right, align => 1),
3823 ], 3867 ],
3824 ; 3868 ;
3825 3869
3826 } else { 3870 } else {
3827 $widget = new CFPlus::UI::Label 3871 $widget = new DC::UI::Label
3828 can_hover => 1, 3872 can_hover => 1,
3829 can_events => 1, 3873 can_events => 1,
3874 align => 0,
3830 markup => $widget, 3875 markup => $widget,
3831 tooltip => $tooltip; 3876 tooltip => $tooltip;
3832 } 3877 }
3833 } 3878 }
3834 3879
3880 1 3925 1
3881} 3926}
3882 3927
3883############################################################################# 3928#############################################################################
3884 3929
3885package CFPlus::UI::Multiplexer; 3930package DC::UI::Multiplexer;
3886 3931
3887our @ISA = CFPlus::UI::Container::; 3932our @ISA = DC::UI::Container::;
3888 3933
3889sub new { 3934sub new {
3890 my $class = shift; 3935 my $class = shift;
3891 3936
3892 my $self = $class->SUPER::new ( 3937 my $self = $class->SUPER::new (
3893 @_, 3938 @_,
3894 ); 3939 );
3895 3940
3896 $self->{current} = $self->{children}[0] 3941 $self->set_current_page (0);
3897 if @{ $self->{children} };
3898 3942
3899 $self 3943 $self
3900} 3944}
3901 3945
3902sub add { 3946sub add {
3903 my ($self, @widgets) = @_; 3947 my ($self, @widgets) = @_;
3904 3948
3905 $self->SUPER::add (@widgets); 3949 $self->SUPER::add (@widgets);
3906 3950
3907 $self->{current} = $self->{children}[0] 3951 $self->set_current_page (0)
3908 if @{ $self->{children} }; 3952 if @widgets == @{ $self->{children} };
3909} 3953}
3910 3954
3911sub get_current_page { 3955sub get_current_page {
3912 my ($self) = @_; 3956 my ($self) = @_;
3913 3957
3919 3963
3920 my $widget = ref $page_or_widget 3964 my $widget = ref $page_or_widget
3921 ? $page_or_widget 3965 ? $page_or_widget
3922 : $self->{children}[$page_or_widget]; 3966 : $self->{children}[$page_or_widget];
3923 3967
3968 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3969
3924 $self->{current} = $widget; 3970 if (($self->{current} = $widget)) {
3971 $self->{current}->set_visible if $self->{current} && $self->{visible};
3925 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3972 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3926 3973
3927 $self->emit (page_changed => $self->{current}); 3974 $self->emit (page_changed => $self->{current});
3975 }
3928 3976
3929 $self->realloc; 3977 $self->realloc;
3930} 3978}
3931 3979
3932sub visible_children { 3980sub visible_children {
3933 $_[0]{current} 3981 $_[0]{current} || ()
3934} 3982}
3935 3983
3936sub size_request { 3984sub size_request {
3937 my ($self) = @_; 3985 my ($self) = @_;
3938 3986
3987 $self->{current}
3939 $self->{current}->size_request 3988 ? $self->{current}->size_request
3989 : (0, 0)
3940} 3990}
3941 3991
3942sub invoke_size_allocate { 3992sub invoke_size_allocate {
3943 my ($self, $w, $h) = @_; 3993 my ($self, $w, $h) = @_;
3944 3994
3945 $self->{current}->configure (0, 0, $w, $h); 3995 $self->{current}->configure (0, 0, $w, $h)
3996 if $self->{current};
3946 3997
3947 1 3998 1
3948} 3999}
3949 4000
3950sub _draw { 4001sub _draw {
3951 my ($self) = @_; 4002 my ($self) = @_;
3952 4003
3953 $self->{current}->draw; 4004 $self->{current}->draw
4005 if $self->{current};
3954} 4006}
3955 4007
3956############################################################################# 4008#############################################################################
3957 4009
3958package CFPlus::UI::Notebook; 4010package DC::UI::Notebook;
3959 4011
3960use CFPlus::OpenGL; 4012use DC::OpenGL;
3961 4013
3962our @ISA = CFPlus::UI::VBox::; 4014our @ISA = DC::UI::VBox::;
3963 4015
3964sub new { 4016sub new {
3965 my $class = shift; 4017 my $class = shift;
3966 4018
3967 my $self = $class->SUPER::new ( 4019 my $self = $class->SUPER::new (
3968 buttonbar => (new CFPlus::UI::Buttonbar), 4020 buttonbar => (new DC::UI::Buttonbar),
3969 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4021 multiplexer => (new DC::UI::Multiplexer expand => 1),
3970 active_outline => [1, 1, 0], 4022 active_outline => [.7, .7, 0.2],
3971 # filter => # will be put between multiplexer and $self 4023 # filter => # will be put between multiplexer and $self
3972 @_, 4024 @_,
3973 ); 4025 );
3974 4026
3975 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4027 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3983 4035
3984 for my $child (@$widgets) { 4036 for my $child (@$widgets) {
3985 Scalar::Util::weaken $child; 4037 Scalar::Util::weaken $child;
3986 $child->{c_tab_} ||= do { 4038 $child->{c_tab_} ||= do {
3987 my $tab = 4039 my $tab =
3988 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4040 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3989 ? $child->{c_tab} 4041 ? $child->{c_tab}
3990 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4042 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3991 4043
3992 $tab->connect (activate => sub { 4044 $tab->connect (activate => sub {
3993 $wself->set_current_page ($child); 4045 $wself->set_current_page ($child);
3994 }); 4046 });
3995 4047
4027sub pages { 4079sub pages {
4028 my ($self) = @_; 4080 my ($self) = @_;
4029 $self->{multiplexer}->children 4081 $self->{multiplexer}->children
4030} 4082}
4031 4083
4084sub page_index {
4085 my ($self, $widget) = @_;
4086
4087 my $i = 0;
4088 for ($self->pages) {
4089 if ($_ eq $widget) { return $i };
4090 $i++;
4091 }
4092
4093 undef
4094}
4095
4032sub add_tab { 4096sub add_tab {
4033 my ($self, $title, $widget, $tooltip) = @_; 4097 my ($self, $title, $widget, $tooltip) = @_;
4034 4098
4035 $title = [$title, $tooltip] unless ref $title; 4099 $title = [$title, $tooltip] unless ref $title;
4036 $widget->{c_tab} = $title; 4100 $widget->{c_tab} = $title;
4056 4120
4057 $self->SUPER::_draw (); 4121 $self->SUPER::_draw ();
4058 4122
4059 if (my $cur = $self->{multiplexer}{current}) { 4123 if (my $cur = $self->{multiplexer}{current}) {
4060 if ($cur = $cur->{c_tab_}) { 4124 if ($cur = $cur->{c_tab_}) {
4061 glTranslate $cur->{x}, $cur->{y}; 4125 glTranslate $self->{buttonbar}{x} + $cur->{x},
4126 $self->{buttonbar}{y} + $cur->{y};
4062 glLineWidth 3; 4127 glLineWidth 3;
4128 #glEnable GL_BLEND;
4129 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4063 glColor @{$self->{active_outline}}; 4130 glColor @{$self->{active_outline}};
4064 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4131 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4065 glLineWidth 1; 4132 glLineWidth 1;
4133 #glDisable GL_BLEND;
4066 } 4134 }
4067 } 4135 }
4068} 4136}
4069 4137
4070############################################################################# 4138#############################################################################
4071 4139
4072package CFPlus::UI::Selector; 4140package DC::UI::Selector;
4073 4141
4074use utf8; 4142use utf8;
4075 4143
4076our @ISA = CFPlus::UI::Button::; 4144our @ISA = DC::UI::Button::;
4077 4145
4078sub new { 4146sub new {
4079 my $class = shift; 4147 my $class = shift;
4080 4148
4081 my $self = $class->SUPER::new ( 4149 my $self = $class->SUPER::new (
4098 my ($value, $title, $tooltip) = @$_; 4166 my ($value, $title, $tooltip) = @$_;
4099 4167
4100 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4168 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4101 } 4169 }
4102 4170
4103 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4171 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4104} 4172}
4105 4173
4106sub _set_value { 4174sub _set_value {
4107 my ($self, $value) = @_; 4175 my ($self, $value) = @_;
4108 4176
4109 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4177 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4178 $item ||= $self->{options}[0]
4110 or return; 4179 or return;
4111 4180
4112 $self->{value} = $item->[0]; 4181 $self->{value} = $item->[0];
4113 $self->set_markup ("$item->[1] ⇓"); 4182 $self->set_markup ("$item->[1] ⇓");
4114 $self->set_tooltip ($item->[2]); 4183# $self->set_tooltip ($item->[2]);
4115} 4184}
4116 4185
4117sub set_value { 4186sub set_value {
4118 my ($self, $value) = @_; 4187 my ($self, $value) = @_;
4119 4188
4121 4190
4122 $self->_set_value ($value); 4191 $self->_set_value ($value);
4123 $self->emit (changed => $value); 4192 $self->emit (changed => $value);
4124} 4193}
4125 4194
4195sub set_options {
4196 my ($self, $options) = @_;
4197
4198 $self->{options} = $options;
4199 $self->_set_value ($self->{value});
4200}
4201
4126############################################################################# 4202#############################################################################
4127 4203
4128package CFPlus::UI::Statusbox; 4204package DC::UI::Statusbox;
4129 4205
4130our @ISA = CFPlus::UI::VBox::; 4206our @ISA = DC::UI::VBox::;
4131 4207
4132sub new { 4208sub new {
4133 my $class = shift; 4209 my $class = shift;
4134 4210
4135 my $self = $class->SUPER::new ( 4211 my $self = $class->SUPER::new (
4136 fontsize => 0.8, 4212 fontsize => 0.8,
4137 @_, 4213 @_,
4138 ); 4214 );
4139 4215
4140 CFPlus::weaken (my $this = $self); 4216 DC::weaken (my $this = $self);
4141 4217
4142 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4218 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4143 4219
4144 $self 4220 $self
4145} 4221}
4146 4222
4147sub reorder { 4223sub reorder {
4148 my ($self) = @_; 4224 my ($self) = @_;
4149 my $NOW = Time::HiRes::time; 4225 my $NOW = EV::time;
4150 4226
4151 # freeze display when hovering over any label 4227 # freeze display when hovering over any label
4152 return if $CFPlus::UI::TOOLTIP->{owner} 4228 return if $DC::UI::TOOLTIP->{owner}
4153 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4229 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4154 values %{ $self->{item} }; 4230 values %{ $self->{item} };
4155 4231
4156 while (my ($k, $v) = each %{ $self->{item} }) { 4232 while (my ($k, $v) = each %{ $self->{item} }) {
4157 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4233 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4158 } 4234 }
4235
4236 $self->{timer}->set (1, 1);
4159 4237
4160 my @widgets; 4238 my @widgets;
4161 4239
4162 my @items = sort { 4240 my @items = sort {
4163 $a->{pri} <=> $b->{pri} 4241 $a->{pri} <=> $b->{pri}
4164 or $b->{id} <=> $a->{id} 4242 or $b->{id} <=> $a->{id}
4165 } values %{ $self->{item} }; 4243 } values %{ $self->{item} };
4166
4167 $self->{timer}->interval (1);
4168 4244
4169 my $count = 10 + 1; 4245 my $count = 10 + 1;
4170 for my $item (@items) { 4246 for my $item (@items) {
4171 last unless --$count; 4247 last unless --$count;
4172 4248
4179 for ($short) { 4255 for ($short) {
4180 s/^\s+//; 4256 s/^\s+//;
4181 s/\s+/ /g; 4257 s/\s+/ /g;
4182 } 4258 }
4183 4259
4184 new CFPlus::UI::Label 4260 new DC::UI::Label
4185 markup => $short, 4261 markup => $short,
4186 tooltip => $item->{tooltip}, 4262 tooltip => $item->{tooltip},
4187 tooltip_font => $::FONT_PROP, 4263 tooltip_font => $::FONT_PROP,
4188 tooltip_width => 0.67, 4264 tooltip_width => 0.67,
4189 fontsize => $item->{fontsize} || $self->{fontsize}, 4265 fontsize => $item->{fontsize} || $self->{fontsize},
4190 max_w => $::WIDTH * 0.44, 4266 max_w => $::WIDTH * 0.44,
4267 align => 0,
4191 fg => [@{ $item->{fg} }], 4268 fg => [@{ $item->{fg} }],
4192 can_events => 1, 4269 can_events => 1,
4193 can_hover => 1 4270 can_hover => 1
4194 }; 4271 };
4195 4272
4196 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4273 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4197 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4274 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4198 $label->update; 4275 $label->update;
4199 $label->set_max_size (undef, $label->{req_h} * $diff) 4276 $label->set_max_size (undef, $label->{req_h} * $diff)
4200 if $diff < 1; 4277 if $diff < 1;
4201 $self->{timer}->interval (1/30); 4278 $self->{timer}->set (1/30, 1/30);
4202 } else { 4279 } else {
4203 $label->{fg}[3] = $item->{fg}[3] || 1; 4280 $label->{fg}[3] = $item->{fg}[3] || 1;
4204 } 4281 }
4205 4282
4206 push @widgets, $label; 4283 push @widgets, $label;
4207 } 4284 }
4285
4286 my $hash = join ",", @widgets;
4287 return if $hash eq $self->{last_widget_hash};
4288 $self->{last_widget_hash} = $hash;
4208 4289
4209 $self->clear; 4290 $self->clear;
4210 $self->SUPER::add (reverse @widgets); 4291 $self->SUPER::add (reverse @widgets);
4211} 4292}
4212 4293
4268 $self->SUPER::destroy; 4349 $self->SUPER::destroy;
4269} 4350}
4270 4351
4271############################################################################# 4352#############################################################################
4272 4353
4273package CFPlus::UI::Root; 4354package DC::UI::Root;
4274 4355
4275our @ISA = CFPlus::UI::Container::; 4356our @ISA = DC::UI::Container::;
4276 4357
4277use List::Util qw(min max); 4358use List::Util qw(min max);
4278 4359
4279use CFPlus::OpenGL; 4360use DC::OpenGL;
4280 4361
4281sub new { 4362sub new {
4282 my $class = shift; 4363 my $class = shift;
4283 4364
4284 my $self = $class->SUPER::new ( 4365 my $self = $class->SUPER::new (
4285 visible => 1, 4366 visible => 1,
4286 @_, 4367 @_,
4287 ); 4368 );
4288 4369
4289 CFPlus::weaken ($self->{root} = $self); 4370 DC::weaken ($self->{root} = $self);
4290 4371
4291 $self 4372 $self
4292} 4373}
4293 4374
4294sub size_request { 4375sub size_request {
4342} 4423}
4343 4424
4344sub update { 4425sub update {
4345 my ($self) = @_; 4426 my ($self) = @_;
4346 4427
4347 $::WANT_REFRESH->start; 4428 $::WANT_REFRESH = 1;
4348} 4429}
4349 4430
4350sub add { 4431sub add {
4351 my ($self, @children) = @_; 4432 my ($self, @children) = @_;
4352 4433
4419 4500
4420 delete $queue{$widget+0}; 4501 delete $queue{$widget+0};
4421 4502
4422 my ($w, $h) = $widget->size_request; 4503 my ($w, $h) = $widget->size_request;
4423 4504
4424 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4505 $w += $widget->{padding_x} * 2;
4425 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4506 $h += $widget->{padding_y} * 2;
4507
4508 $w = max $widget->{min_w}, $w;
4509 $h = max $widget->{min_h}, $h;
4426 4510
4427 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4511 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4428 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4512 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4429 4513
4430 $w = $widget->{force_w} if exists $widget->{force_w}; 4514 $w = $widget->{force_w} if exists $widget->{force_w};
4497 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4581 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4498 glMatrixMode GL_MODELVIEW; 4582 glMatrixMode GL_MODELVIEW;
4499 glLoadIdentity; 4583 glLoadIdentity;
4500 4584
4501 { 4585 {
4502 package CFPlus::UI::Base; 4586 package DC::UI::Base;
4503 4587
4504 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4588 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4505 (0, 0, $self->{w}, $self->{h}); 4589 (0, 0, $self->{w}, $self->{h});
4506 4590
4507 $self->_draw; 4591 $self->_draw;
4508 } 4592 }
4509} 4593}
4510 4594
4511############################################################################# 4595#############################################################################
4512 4596
4513package CFPlus::UI; 4597package DC::UI;
4514 4598
4515$ROOT = new CFPlus::UI::Root; 4599$ROOT = new DC::UI::Root;
4516$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4600$TOOLTIP = new DC::UI::Tooltip z => 900;
4517 4601
45181 46021
4519 4603

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines