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.397 by root, Sat Jul 21 14:01:35 2007 UTC vs.
Revision 1.458 by root, Fri Dec 28 15:05:33 2007 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 33
33 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
34 35
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 37 $TOOLTIP->show;
41 } 38 }
42 39
43 return; 40 return;
46 } 43 }
47 44
48 $TOOLTIP->hide; 45 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
51}); 48};
52 49
53sub get_layout { 50sub get_layout {
54 my $layout; 51 my $layout;
55 52
56 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
92 89
93 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 92
96 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
97 } 94 }
98} 95}
99 96
100sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
101 my ($ev) = @_; 98 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
108 105
109 $GRAB = $widget; 106 $GRAB = $widget;
110 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
111 108
112 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
113 } 110 }
114 111
115 if ($GRAB) { 112 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 114 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 117
121 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
123 120
124 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 140 $grab->update if $grab;
144 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
145 142
146 check_hover $widget; 143 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
152 my ($ev) = @_; 149 my ($ev) = @_;
214 reconfigure_widgets; 211 reconfigure_widgets;
215} 212}
216 213
217############################################################################# 214#############################################################################
218 215
219package CFPlus::UI::Event; 216package DC::UI::Event;
220 217
221sub xy { 218sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 220}
224 221
225############################################################################# 222#############################################################################
226 223
227package CFPlus::UI::Base; 224package DC::UI::Base;
228 225
229use strict; 226use strict;
230 227
231use CFPlus::OpenGL; 228use DC::OpenGL;
232 229
233sub new { 230sub new {
234 my $class = shift; 231 my $class = shift;
235 232
236 my $self = bless { 233 my $self = bless {
241 h => undef, 238 h => undef,
242 can_events => 1, 239 can_events => 1,
243 @_ 240 @_
244 }, $class; 241 }, $class;
245 242
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 244
248 for (keys %$self) { 245 for (keys %$self) {
249 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
251 } 248 }
252 } 249 }
253 250
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $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};
256 $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};
257 $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};
258 $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};
259 256
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 259
263 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
273 $self->emit ("destroy"); 270 $self->emit ("destroy");
274 %$self = (); 271 %$self = ();
275} 272}
276 273
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub show {
282 my ($self) = @_; 279 my ($self) = @_;
283 280
284 return if $self->{parent}; 281 return if $self->{parent};
285 282
286 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
287} 284}
288 285
289sub set_visible { 286sub set_visible {
290 my ($self) = @_; 287 my ($self) = @_;
291 288
312 delete $self->{root}; 309 delete $self->{root};
313 310
314 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
316 313
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
319 316
320 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
322} 319}
341sub hide { 338sub hide {
342 my ($self) = @_; 339 my ($self) = @_;
343 340
344 $self->set_invisible; 341 $self->set_invisible;
345 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 346 $parent->remove ($self);
347 if $self->{parent}; 347 }
348} 348}
349 349
350sub move_abs { 350sub move_abs {
351 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
352 352
364 $self->{force_h} = $h; 364 $self->{force_h} = $h;
365 365
366 $self->realloc; 366 $self->realloc;
367} 367}
368 368
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
377}
378
369sub size_request { 379sub size_request {
370 require Carp; 380 require Carp;
371 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
372} 382}
373 383
379 my ($self, $x, $y, $w, $h) = @_; 389 my ($self, $x, $y, $w, $h) = @_;
380 390
381 if ($self->{aspect}) { 391 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 392 my ($ow, $oh) = ($w, $h);
383 393
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 394 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 395 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 396
387 # use alignment to adjust x, y 397 # use alignment to adjust x, y
388 398
389 $x += int 0.5 * ($ow - $w); 399 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 400 $y += int 0.5 * ($oh - $h);
431 441
432 return if $self->{tooltip} eq $tooltip; 442 return if $self->{tooltip} eq $tooltip;
433 443
434 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
435 445
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 449 }
440} 450}
441 451
442# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
443sub coord2local { 453sub coord2local {
444 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
445 455
446 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
447 457
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 459}
450 460
451# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
452sub coord2global { 462sub coord2global {
453 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
454 464
455 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
456 466
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 468}
459 469
460sub invoke_focus_in { 470sub invoke_focus_in {
509sub connect { 519sub connect {
510 my ($self, $signal, $cb) = @_; 520 my ($self, $signal, $cb) = @_;
511 521
512 push @{ $self->{signal_cb}{$signal} }, $cb; 522 push @{ $self->{signal_cb}{$signal} }, $cb;
513 523
514 defined wantarray and CFPlus::guard { 524 defined wantarray and DC::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 526 @{ $self->{signal_cb}{$signal} };
517 } 527 }
518} 528}
519 529
557# in .xs 567# in .xs
558 568
559sub set_parent { 569sub set_parent {
560 my ($self, $parent) = @_; 570 my ($self, $parent) = @_;
561 571
562 CFPlus::weaken ($self->{parent} = $parent); 572 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 573 $self->set_visible if $parent->{visible};
564} 574}
565 575
566sub realloc { 576sub realloc {
567 my ($self) = @_; 577 my ($self) = @_;
593 603
594# using global variables seems a bit hacky, but passing through all drawing 604# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 605# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 606our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 607
598sub draw { 608#sub draw {
599 my ($self) = @_; 609#CFPlus.xs
600
601 return unless $self->{h} && $self->{w};
602
603 # update screen rectangle
604 local $draw_x = $draw_x + $self->{x};
605 local $draw_y = $draw_y + $self->{y};
606
607 # skip widgets that are entirely outside the drawing area
608 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
609 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
610
611 glPushMatrix;
612 glTranslate $self->{x}, $self->{y}, 0;
613
614 if ($self == $HOVER && $self->{can_hover}) {
615 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
616 glEnable GL_BLEND;
617 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
618 glBegin GL_QUADS;
619 glVertex 0 , 0;
620 glVertex $self->{w}, 0;
621 glVertex $self->{w}, $self->{h};
622 glVertex 0 , $self->{h};
623 glEnd;
624 glDisable GL_BLEND;
625 }
626
627 if ($ENV{CFPLUS_DEBUG} & 1) {
628 glPushMatrix;
629 glColor 1, 1, 0, 1;
630 glTranslate 0.375, 0.375;
631 glBegin GL_LINE_LOOP;
632 glVertex 0 , 0;
633 glVertex $self->{w} - 1, 0;
634 glVertex $self->{w} - 1, $self->{h} - 1;
635 glVertex 0 , $self->{h} - 1;
636 glEnd;
637 glPopMatrix;
638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
639 }
640
641 $self->_draw;
642 glPopMatrix;
643}
644 610
645sub _draw { 611sub _draw {
646 my ($self) = @_; 612 my ($self) = @_;
647 613
648 warn "no draw defined for $self\n"; 614 warn "no draw defined for $self\n";
649} 615}
650 616
651my $cntx;#d#
652sub DESTROY { 617sub DESTROY {
653 my ($self) = @_; 618 my ($self) = @_;
654 619
655 return if CFPlus::in_destruct; 620 return if DC::in_destruct;
656 621
622 local $@;
657 eval { $self->destroy }; 623 eval { $self->destroy };
658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659 625
660 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
661} 627}
662 628
663############################################################################# 629#############################################################################
664 630
665package CFPlus::UI::DrawBG; 631package DC::UI::DrawBG;
666 632
667our @ISA = CFPlus::UI::Base::; 633our @ISA = DC::UI::Base::;
668 634
669use strict; 635use strict;
670use CFPlus::OpenGL; 636use DC::OpenGL;
671 637
672sub new { 638sub new {
673 my $class = shift; 639 my $class = shift;
674
675 # range [value, low, high, page]
676 640
677 $class->SUPER::new ( 641 $class->SUPER::new (
678 #bg => [0, 0, 0, 0.2], 642 #bg => [0, 0, 0, 0.2],
679 #active_bg => [1, 1, 1, 0.5], 643 #active_bg => [1, 1, 1, 0.5],
680 @_ 644 @_
692 my ($w, $h) = @$self{qw(w h)}; 656 my ($w, $h) = @$self{qw(w h)};
693 657
694 glEnable GL_BLEND; 658 glEnable GL_BLEND;
695 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 659 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
696 glColor_premultiply @$color; 660 glColor_premultiply @$color;
697
698 glBegin GL_QUADS;
699 glVertex 0 , 0;
700 glVertex 0 , $h;
701 glVertex $w, $h; 661 glRect 0, 0, $w, $h;
702 glVertex $w, 0;
703 glEnd;
704
705 glDisable GL_BLEND; 662 glDisable GL_BLEND;
706 } 663 }
707} 664}
708 665
709############################################################################# 666#############################################################################
710 667
711package CFPlus::UI::Empty; 668package DC::UI::Empty;
712 669
713our @ISA = CFPlus::UI::Base::; 670our @ISA = DC::UI::Base::;
714 671
715sub new { 672sub new {
716 my ($class, %arg) = @_; 673 my ($class, %arg) = @_;
717 $class->SUPER::new (can_events => 0, %arg); 674 $class->SUPER::new (can_events => 0, %arg);
718} 675}
725 682
726sub draw { } 683sub draw { }
727 684
728############################################################################# 685#############################################################################
729 686
730package CFPlus::UI::Container; 687package DC::UI::Container;
731 688
732our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
733 690
734sub new { 691sub new {
735 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
736 693
737 my $children = delete $arg{children}; 694 my $children = delete $arg{children};
769 sort { $a->{z} <=> $b->{z} } 726 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 727 @{$self->{children}}, @widgets
771 ]; 728 ];
772 729
773 $self->realloc; 730 $self->realloc;
731
732 $self->emit (c_add => \@widgets);
733
734 map $_+0, @widgets
774} 735}
775 736
776sub children { 737sub children {
777 @{ $_[0]{children} } 738 @{ $_[0]{children} }
778} 739}
779 740
780sub remove { 741sub remove {
781 my ($self, $child) = @_; 742 my ($self, @widgets) = @_;
782 743
744 $self->emit (c_remove => \@widgets);
745
746 for my $child (@widgets) {
783 delete $child->{parent}; 747 delete $child->{parent};
784 $child->hide; 748 $child->hide;
785
786 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 749 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
750 }
787 751
788 $self->realloc; 752 $self->realloc;
789} 753}
790 754
791sub clear { 755sub clear {
819} 783}
820 784
821sub _draw { 785sub _draw {
822 my ($self) = @_; 786 my ($self) = @_;
823 787
824 $_->draw for @{$self->{children}}; 788 $_->draw for $self->visible_children;
825} 789}
826 790
827############################################################################# 791#############################################################################
828 792
829package CFPlus::UI::Bin; 793package DC::UI::Bin;
830 794
831our @ISA = CFPlus::UI::Container::; 795our @ISA = DC::UI::Container::;
832 796
833sub new { 797sub new {
834 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
835 799
836 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 800 my $child = (delete $arg{child}) || new DC::UI::Empty::;
837 801
838 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
839} 803}
840 804
841sub add { 805sub add {
842 my ($self, $child) = @_; 806 my ($self, $child) = @_;
843 807
844 $self->SUPER::remove ($_) for @{ $self->{children} }; 808 $self->clear;
845 $self->SUPER::add ($child); 809 $self->SUPER::add ($child);
846} 810}
847 811
848sub remove { 812sub remove {
849 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
850 814
851 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
852 816
853 $self->{children} = [new CFPlus::UI::Empty] 817 $self->{children} = [new DC::UI::Empty]
854 unless @{$self->{children}}; 818 unless @{$self->{children}};
855} 819}
856 820
857sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
858 822
867 831
868 1 832 1
869} 833}
870 834
871############################################################################# 835#############################################################################
872
873# back-buffered drawing area 836# back-buffered drawing area
874 837
875package CFPlus::UI::Window; 838package DC::UI::Window;
876 839
877our @ISA = CFPlus::UI::Bin::; 840our @ISA = DC::UI::Bin::;
878 841
879use CFPlus::OpenGL; 842use DC::OpenGL;
880 843
881sub new { 844sub new {
882 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
883 846
884 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
906} 869}
907 870
908sub render_child { 871sub render_child {
909 my ($self) = @_; 872 my ($self) = @_;
910 873
911 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 874 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
912 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
913 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
914 877
915 { 878 {
916 package CFPlus::UI::Base; 879 package DC::UI::Base;
917 880
918 local ($draw_x, $draw_y, $draw_w, $draw_h) = 881 local ($draw_x, $draw_y, $draw_w, $draw_h) =
919 (0, 0, $self->{w}, $self->{h}); 882 (0, 0, $self->{w}, $self->{h});
920 883
921 $self->_render; 884 $self->_render;
938 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
939} 902}
940 903
941############################################################################# 904#############################################################################
942 905
943package CFPlus::UI::ViewPort; 906package DC::UI::ViewPort;
944 907
945use List::Util qw(min max); 908use List::Util qw(min max);
946 909
947our @ISA = CFPlus::UI::Window::; 910our @ISA = DC::UI::Window::;
948 911
949sub new { 912sub new {
950 my $class = shift; 913 my $class = shift;
951 914
952 $class->SUPER::new ( 915 $class->SUPER::new (
994 $self->emit (changed => $x, $y); 957 $self->emit (changed => $x, $y);
995 $self->update; 958 $self->update;
996 } 959 }
997} 960}
998 961
962sub set_center {
963 my ($self, $x, $y) = @_;
964
965 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
966}
967
968sub make_visible {
969 my ($self, $x, $y, $border) = @_;
970
971 if ( $x < $self->{view_x} + $self->{w} * $border
972 || $x > $self->{view_x} + $self->{w} * (1 - $border)
973 || $y < $self->{view_y} + $self->{h} * $border
974 || $y > $self->{view_y} + $self->{h} * (1 - $border)
975 ) {
976 $self->set_center ($x, $y);
977 }
978}
979
999# hmm, this does not work for topleft of $self... but we should not ask for that 980# hmm, this does not work for topleft of $self... but we should not ask for that
1000sub coord2local { 981sub coord2local {
1001 my ($self, $x, $y) = @_; 982 my ($self, $x, $y) = @_;
1002 983
1003 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) 984 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1020 ) { 1001 ) {
1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1022 } else { 1003 } else {
1023 $self->CFPlus::UI::Base::find_widget ($x, $y) 1004 $self->DC::UI::Base::find_widget ($x, $y)
1024 } 1005 }
1025} 1006}
1026 1007
1027sub _render { 1008sub _render {
1028 my ($self) = @_; 1009 my ($self) = @_;
1029 1010
1030 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1011 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1031 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1012 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1032 1013
1033 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1034 1015
1035 $self->SUPER::_render; 1016 $self->SUPER::_render;
1036} 1017}
1037 1018
1038############################################################################# 1019#############################################################################
1039 1020
1040package CFPlus::UI::ScrolledWindow; 1021package DC::UI::ScrolledWindow;
1041 1022
1042our @ISA = CFPlus::UI::Table::; 1023our @ISA = DC::UI::Table::;
1043 1024
1044sub new { 1025sub new {
1045 my ($class, %arg) = @_; 1026 my ($class, %arg) = @_;
1046 1027
1047 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1048 1029
1049 my $self; 1030 my $self;
1050 1031
1051 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new DC::UI::Slider
1052 col => 0, 1033 c_col => 0,
1053 row => 1, 1034 c_row => 1,
1054 vertical => 0, 1035 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub { 1037 on_changed => sub {
1057 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 }, 1040 },
1060 ; 1041 ;
1061 1042
1062 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new DC::UI::Slider
1063 col => 1, 1044 c_col => 1,
1064 row => 0, 1045 c_row => 0,
1065 vertical => 1, 1046 vertical => 1,
1066 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1067 on_changed => sub { 1048 on_changed => sub {
1068 $self->{vpos} = $_[1]; 1049 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1079 col_expand => [1, 0], 1060 col_expand => [1, 0],
1080 row_expand => [1, 0], 1061 row_expand => [1, 0],
1081 %arg, 1062 %arg,
1082 ); 1063 );
1083 1064
1084 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new DC::UI::ViewPort
1085 col => 0, 1066 c_col => 0,
1086 row => 0, 1067 c_row => 0,
1087 expand => 1, 1068 expand => 1,
1088 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1090 on_changed => sub { 1071 on_changed => sub {
1091 my ($vp, $x, $y) = @_; 1072 my ($vp, $x, $y) = @_;
1093 $vp->{parent}{hslider}->set_value ($x); 1074 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y); 1075 $vp->{parent}{vslider}->set_value ($y);
1095 1076
1096 0 1077 0
1097 }, 1078 },
1079 on_size_allocate => sub {
1080 my ($vp, $w, $h) = @_;
1081 $vp->{parent}->update_slider;
1082 0
1083 },
1098 ; 1084 ;
1099 1085
1100 $self->SUPER::add ($self->{vp}); 1086 $self->SUPER::add ($self->{vp});
1101 1087
1102 $self->add ($child) if $child; 1088 $self->add ($child) if $child;
1108 my ($self, $widget) = @_; 1094 my ($self, $widget) = @_;
1109 1095
1110 $self->{vp}->add ($self->{child} = $widget); 1096 $self->{vp}->add ($self->{child} = $widget);
1111} 1097}
1112 1098
1099sub set_offset { shift->{vp}->set_offset (@_) }
1100sub set_center { shift->{vp}->set_center (@_) }
1101sub make_visible { shift->{vp}->make_visible (@_) }
1102
1113sub update_slider { 1103sub update_slider {
1114 my ($self) = @_; 1104 my ($self) = @_;
1115 1105
1116 my $child = ($self->{vp} or return)->child; 1106 my $child = ($self->{vp} or return)->child;
1117 1107
1108 if ($self->{scroll_x}) {
1118 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1109 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1119 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1110 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1120 1111
1121 my $visible = $w1 > $w2; 1112 my $visible = $w1 > $w2;
1122 if ($visible != $self->{hslider_visible}) { 1113 if ($visible != $self->{hslider_visible}) {
1123 $self->{hslider_visible} = $visible; 1114 $self->{hslider_visible} = $visible;
1124 $visible ? $self->SUPER::add ($self->{hslider}) 1115 $visible ? $self->SUPER::add ($self->{hslider})
1125 : $self->SUPER::remove ($self->{hslider}); 1116 : $self->SUPER::remove ($self->{hslider});
1117 }
1126 } 1118 }
1127 1119
1120 if ($self->{scroll_y}) {
1128 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1121 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1129 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1122 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1130 1123
1131 my $visible = $h1 > $h2; 1124 my $visible = $h1 > $h2;
1132 if ($visible != $self->{vslider_visible}) { 1125 if ($visible != $self->{vslider_visible}) {
1133 $self->{vslider_visible} = $visible; 1126 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider}) 1127 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider}); 1128 : $self->SUPER::remove ($self->{vslider});
1129 }
1136 } 1130 }
1137} 1131}
1138
1139 1132
1140sub start_dragging { 1133sub start_dragging {
1141 my ($self, $ev) = @_; 1134 my ($self, $ev) = @_;
1142 1135
1143 $self->grab_focus; 1136 $self->grab_focus;
1156} 1149}
1157 1150
1158sub invoke_mouse_wheel { 1151sub invoke_mouse_wheel {
1159 my ($self, $ev) = @_; 1152 my ($self, $ev) = @_;
1160 1153
1161 $self->{vslider}->emit (mouse_wheel => $ev); 1154 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1162 $self->{hslider}->emit (mouse_wheel => $ev); 1155 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1163 1156
1164 1 1157 1
1165} 1158}
1166 1159
1167sub invoke_button_down { 1160sub invoke_button_down {
1168 my ($self, $ev, $x, $y) = @_; 1161 my ($self, $ev, $x, $y) = @_;
1169 1162
1163 if ($ev->{button} == 2) {
1170 $self->start_dragging ($ev); 1164 $self->start_dragging ($ev);
1171 1165 return 1;
1172 1 1166 }
1167
1168 0
1173} 1169}
1174 1170
1175sub invoke_button_up { 1171sub invoke_button_up {
1176 my ($self, $ev, $x, $y) = @_; 1172 my ($self, $ev, $x, $y) = @_;
1177 1173
1200 $self->SUPER::invoke_size_allocate ($w, $h) 1196 $self->SUPER::invoke_size_allocate ($w, $h)
1201} 1197}
1202 1198
1203############################################################################# 1199#############################################################################
1204 1200
1205package CFPlus::UI::Frame; 1201package DC::UI::Frame;
1206 1202
1207our @ISA = CFPlus::UI::Bin::; 1203our @ISA = DC::UI::Bin::;
1208 1204
1209use CFPlus::OpenGL; 1205use DC::OpenGL;
1210 1206
1211sub new { 1207sub new {
1212 my $class = shift; 1208 my $class = shift;
1213 1209
1214 $class->SUPER::new ( 1210 $class->SUPER::new (
1224 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
1225 1221
1226 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
1227 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1228 glColor_premultiply @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
1229
1230 glBegin GL_QUADS;
1231 glVertex 0 , 0;
1232 glVertex 0 , $h;
1233 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
1234 glVertex $w, 0;
1235 glEnd;
1236
1237 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
1238 } 1227 }
1239 1228
1240 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1241} 1230}
1242 1231
1243############################################################################# 1232#############################################################################
1244 1233
1245package CFPlus::UI::FancyFrame; 1234package DC::UI::FancyFrame;
1246 1235
1247our @ISA = CFPlus::UI::Bin::; 1236our @ISA = DC::UI::Bin::;
1248 1237
1249use CFPlus::OpenGL; 1238use DC::OpenGL;
1250 1239
1251sub new { 1240sub new {
1252 my ($class, %arg) = @_; 1241 my ($class, %arg) = @_;
1253 1242
1254 if ((exists $arg{label}) && !ref $arg{label}) { 1243 if ((exists $arg{label}) && !ref $arg{label}) {
1255 $arg{label} = new CFPlus::UI::Label 1244 $arg{label} = new DC::UI::Label
1256 align => 1, 1245 align => 1,
1257 valign => 0, 1246 valign => 0,
1258 text => $arg{label}, 1247 text => $arg{label},
1259 fontsize => ($arg{border} || 0.8) * 0.75; 1248 fontsize => ($arg{border} || 0.8) * 0.75;
1260 } 1249 }
1272 1261
1273sub add { 1262sub add {
1274 my ($self, @widgets) = @_; 1263 my ($self, @widgets) = @_;
1275 1264
1276 $self->SUPER::add (@widgets); 1265 $self->SUPER::add (@widgets);
1277 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1266 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1278} 1267}
1279 1268
1280sub border { 1269sub border {
1281 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
1282} 1271}
1340 } 1329 }
1341} 1330}
1342 1331
1343############################################################################# 1332#############################################################################
1344 1333
1345package CFPlus::UI::Toplevel; 1334package DC::UI::Toplevel;
1346 1335
1347our @ISA = CFPlus::UI::Bin::; 1336our @ISA = DC::UI::Bin::;
1348 1337
1349use CFPlus::OpenGL; 1338use DC::OpenGL;
1350 1339
1351my $bg = 1340my $bg =
1352 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1341 new_from_file DC::Texture DC::find_rcfile "d1_bg.png",
1353 mipmap => 1, wrap => 1; 1342 mipmap => 1, wrap => 1;
1354 1343
1355my @border = 1344my @border =
1356 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1345 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1357 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1346 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1358 1347
1359my @icon = 1348my @icon =
1360 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1349 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1361 qw(x1_move.png x1_resize.png); 1350 qw(x1_move.png x1_resize.png);
1362 1351
1363sub new { 1352sub new {
1364 my ($class, %arg) = @_; 1353 my ($class, %arg) = @_;
1365 1354
1371 min_w => 64, 1360 min_w => 64,
1372 min_h => 32, 1361 min_h => 32,
1373 %arg, 1362 %arg,
1374 ); 1363 );
1375 1364
1376 $self->{title_widget} = new CFPlus::UI::Label 1365 $self->{title_widget} = new DC::UI::Label
1377 align => 0, 1366 align => 0,
1378 valign => 1, 1367 valign => 1,
1379 text => $self->{title}, 1368 text => $self->{title},
1380 fontsize => $self->{border}, 1369 fontsize => $self->{border},
1381 if exists $self->{title}; 1370 if exists $self->{title};
1382 1371
1383 if ($self->{has_close_button}) { 1372 if ($self->{has_close_button}) {
1384 $self->{close_button} = 1373 $self->{close_button} =
1385 new CFPlus::UI::ImageButton 1374 new DC::UI::ImageButton
1386 path => 'x1_close.png', 1375 path => 'x1_close.png',
1387 on_activate => sub { $self->emit ("delete") }; 1376 on_activate => sub { $self->emit ("delete") };
1388 1377
1389 $self->CFPlus::UI::Container::add ($self->{close_button}); 1378 $self->DC::UI::Container::add ($self->{close_button});
1390 } 1379 }
1391 1380
1392 $self 1381 $self
1393} 1382}
1394 1383
1395sub add { 1384sub add {
1396 my ($self, @widgets) = @_; 1385 my ($self, @widgets) = @_;
1397 1386
1398 $self->SUPER::add (@widgets); 1387 $self->SUPER::add (@widgets);
1399 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1388 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1400 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1389 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1401} 1390}
1402 1391
1403sub border { 1392sub border {
1404 int $_[0]{border} * $::FONTSIZE 1393 int $_[0]{border} * $::FONTSIZE
1394}
1395
1396sub get_max_wh {
1397 my ($self) = @_;
1398
1399 return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w};
1401
1402 $self->SUPER::get_max_wh
1405} 1403}
1406 1404
1407sub size_request { 1405sub size_request {
1408 my ($self) = @_; 1406 my ($self) = @_;
1409 1407
1581 if $self->{close_button}; 1579 if $self->{close_button};
1582} 1580}
1583 1581
1584############################################################################# 1582#############################################################################
1585 1583
1586package CFPlus::UI::Table; 1584package DC::UI::Table;
1587 1585
1588our @ISA = CFPlus::UI::Container::; 1586our @ISA = DC::UI::Container::;
1589 1587
1590use List::Util qw(max sum); 1588use List::Util qw(max sum);
1591 1589
1592use CFPlus::OpenGL; 1590use DC::OpenGL;
1593 1591
1594sub new { 1592sub new {
1595 my $class = shift; 1593 my $class = shift;
1596 1594
1597 $class->SUPER::new ( 1595 $class->SUPER::new (
1603 1601
1604sub add { 1602sub add {
1605 my ($self, @widgets) = @_; 1603 my ($self, @widgets) = @_;
1606 1604
1607 for my $child (@widgets) { 1605 for my $child (@widgets) {
1608 $child->{rowspan} ||= 1; 1606 $child->{c_rowspan} ||= 1;
1609 $child->{colspan} ||= 1; 1607 $child->{c_colspan} ||= 1;
1610 } 1608 }
1611 1609
1612 $self->SUPER::add (@widgets); 1610 $self->SUPER::add (@widgets);
1613} 1611}
1614 1612
1618 my @widgets; 1616 my @widgets;
1619 1617
1620 while (@_) { 1618 while (@_) {
1621 my ($col, $row, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1622 1620
1623 $child->{row} = $row; 1621 $child->{c_row} = $row;
1624 $child->{col} = $col; 1622 $child->{c_col} = $col;
1625 1623
1626 push @widgets, $child; 1624 push @widgets, $child;
1627 } 1625 }
1628 1626
1629 $self->add (@widgets); 1627 $self->add (@widgets);
1635 my (@w, @h); 1633 my (@w, @h);
1636 1634
1637 my @children = $self->children; 1635 my @children = $self->children;
1638 1636
1639 # first pass, columns 1637 # first pass, columns
1640 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1638 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1641 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1642 1640
1643 my $sw = sum @w[$c .. $c + $cs - 1]; 1641 my $sw = sum @w[$c .. $c + $cs - 1];
1644 1642
1645 if ($w > $sw) { 1643 if ($w > $sw) {
1646 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1644 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1647 } 1645 }
1648 } 1646 }
1649 1647
1650 # second pass, rows 1648 # second pass, rows
1651 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1652 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1653 1651
1654 my $sh = sum @h[$r .. $r + $rs - 1]; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1655 1653
1656 if ($h > $sh) { 1654 if ($h > $sh) {
1657 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1655 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1658 } 1656 }
1659 } 1657 }
1660 1658
1661 (\@w, \@h) 1659 (\@w, \@h)
1662} 1660}
1685 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1686 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1687 1685
1688 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1689 1687
1690 CFPlus::UI::harmonize $ws; 1688 DC::UI::harmonize $ws;
1691 1689
1692 my @row_expand = @{$self->{row_expand}}; 1690 my @row_expand = @{$self->{row_expand}};
1693 @row_expand = (1) x @$ws unless @row_expand; 1691 @row_expand = (1) x @$ws unless @row_expand;
1694 my $row_expand = (sum @row_expand) || 1; 1692 my $row_expand = (sum @row_expand) || 1;
1695 1693
1696 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1697 1695
1698 CFPlus::UI::harmonize $hs; 1696 DC::UI::harmonize $hs;
1699 1697
1700 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1701 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1702 1700
1703 for my $widget ($self->children) { 1701 for my $widget ($self->children) {
1704 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1705 1703
1706 $widget->configure ( 1704 $widget->configure (
1707 $x[$c], $y[$r], 1705 $x[$c], $y[$r],
1708 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1709 ); 1707 );
1712 1 1710 1
1713} 1711}
1714 1712
1715############################################################################# 1713#############################################################################
1716 1714
1717package CFPlus::UI::Fixed; 1715package DC::UI::Fixed;
1718 1716
1719use List::Util qw(min max); 1717use List::Util qw(min max);
1720 1718
1721our @ISA = CFPlus::UI::Container::; 1719our @ISA = DC::UI::Container::;
1722
1723sub add {
1724 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1725
1726 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1727 $self->SUPER::add ($child);
1728}
1729 1720
1730sub _scale($$$) { 1721sub _scale($$$) {
1731 my ($mode, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1732 1723
1733 $mode eq "abs" ? $val 1724 $rel ? $val * $max : $val
1734 : $mode eq "rel" ? $val * $max
1735 : 0
1736} 1725}
1737 1726
1738sub size_request { 1727sub size_request {
1739 my ($self) = @_; 1728 my ($self) = @_;
1740 1729
1741 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1730 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1742 1731
1743 # determine overall size by querying abs widgets 1732 # determine overall size by querying abs widgets
1744 for my $child ($self->visible_children) { 1733 for my $child ($self->visible_children) {
1745 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1734 unless ($child->{c_rel}) {
1735 my $x = $child->{c_x};
1736 my $y = $child->{c_y};
1746 1737
1747 if ($pos eq "abs") {
1748 $w = _scale $size, $w, $child->{req_w};
1749 $h = _scale $size, $h, $child->{req_h};
1750
1751 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1738 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1752 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1739 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1753 } 1740 }
1754 } 1741 }
1755 1742
1756 my $W = $x2 - $x1; 1743 my $W = $x2 - $x1;
1757 my $H = $y2 - $y1; 1744 my $H = $y2 - $y1;
1758 1745
1759 # now layout remaining widgets 1746 # now layout remaining widgets
1760 for my $child ($self->visible_children) { 1747 for my $child ($self->visible_children) {
1761 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1748 if ($child->{c_rel}) {
1749 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1750 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1762 1751
1763 if ($pos ne "abs") {
1764 $x = _scale $pos, $x, $W;
1765 $y = _scale $pos, $x, $H;
1766 $w = _scale $size, $w, $child->{req_w};
1767 $h = _scale $size, $h, $child->{req_h};
1768
1769 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1752 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1770 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1753 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1771 } 1754 }
1772 } 1755 }
1773 1756
1774 my $W = $x2 - $x1; 1757 my $W = $x2 - $x1;
1775 my $H = $y2 - $y1; 1758 my $H = $y2 - $y1;
1779 1762
1780sub invoke_size_allocate { 1763sub invoke_size_allocate {
1781 my ($self, $W, $H) = @_; 1764 my ($self, $W, $H) = @_;
1782 1765
1783 for my $child ($self->visible_children) { 1766 for my $child ($self->visible_children) {
1784 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1767 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1768 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1785 1769
1786 $x = _scale $pos, $x, $W; 1770 $x += $child->{c_halign} * $child->{req_w};
1787 $y = _scale $pos, $x, $H; 1771 $y += $child->{c_valign} * $child->{req_h};
1788 $w = _scale $size, $w, $child->{req_w};
1789 $h = _scale $size, $h, $child->{req_h};
1790 1772
1791 $child->configure ($x, $y, $w, $h); 1773 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1792 } 1774 }
1793 1775
1794 1 1776 1
1795} 1777}
1796 1778
1797############################################################################# 1779#############################################################################
1798 1780
1799package CFPlus::UI::Box; 1781package DC::UI::Box;
1800 1782
1801our @ISA = CFPlus::UI::Container::; 1783our @ISA = DC::UI::Container::;
1802 1784
1803sub size_request { 1785sub size_request {
1804 my ($self) = @_; 1786 my ($self) = @_;
1787
1788 my @children = $self->visible_children;
1805 1789
1806 $self->{vertical} 1790 $self->{vertical}
1807 ? ( 1791 ? (
1808 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1792 (List::Util::max map $_->{req_w}, @children),
1809 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_h}, @children),
1810 ) 1794 )
1811 : ( 1795 : (
1812 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1796 (List::Util::sum map $_->{req_w}, @children),
1813 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1797 (List::Util::max map $_->{req_h}, @children),
1814 ) 1798 )
1815} 1799}
1816 1800
1817sub invoke_size_allocate { 1801sub invoke_size_allocate {
1818 my ($self, $w, $h) = @_; 1802 my ($self, $w, $h) = @_;
1839 $req[$_] += $space * $children[$_]{expand} 1823 $req[$_] += $space * $children[$_]{expand}
1840 for 0 .. $#children; 1824 for 0 .. $#children;
1841 } 1825 }
1842 } 1826 }
1843 1827
1844 CFPlus::UI::harmonize \@req; 1828 DC::UI::harmonize \@req;
1845 1829
1846 my $pos = 0; 1830 my $pos = 0;
1847 for (0 .. $#children) { 1831 for (0 .. $#children) {
1848 my $alloc = $req[$_]; 1832 my $alloc = $req[$_];
1849 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1854 1 1838 1
1855} 1839}
1856 1840
1857############################################################################# 1841#############################################################################
1858 1842
1859package CFPlus::UI::HBox; 1843package DC::UI::HBox;
1860 1844
1861our @ISA = CFPlus::UI::Box::; 1845our @ISA = DC::UI::Box::;
1862 1846
1863sub new { 1847sub new {
1864 my $class = shift; 1848 my $class = shift;
1865 1849
1866 $class->SUPER::new ( 1850 $class->SUPER::new (
1869 ) 1853 )
1870} 1854}
1871 1855
1872############################################################################# 1856#############################################################################
1873 1857
1874package CFPlus::UI::VBox; 1858package DC::UI::VBox;
1875 1859
1876our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1877 1861
1878sub new { 1862sub new {
1879 my $class = shift; 1863 my $class = shift;
1880 1864
1881 $class->SUPER::new ( 1865 $class->SUPER::new (
1884 ) 1868 )
1885} 1869}
1886 1870
1887############################################################################# 1871#############################################################################
1888 1872
1889package CFPlus::UI::Label; 1873package DC::UI::Label;
1890 1874
1891our @ISA = CFPlus::UI::DrawBG::; 1875our @ISA = DC::UI::DrawBG::;
1892 1876
1893use CFPlus::OpenGL; 1877use DC::OpenGL;
1894 1878
1895sub new { 1879sub new {
1896 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1897 1881
1898 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1903 #text => initial text 1887 #text => initial text
1904 #markup => initial narkup 1888 #markup => initial narkup
1905 #max_w => maximum pixel width 1889 #max_w => maximum pixel width
1906 #style => 0, # render flags 1890 #style => 0, # render flags
1907 ellipsise => 3, # end 1891 ellipsise => 3, # end
1908 layout => (new CFPlus::Layout), 1892 layout => (new DC::Layout),
1909 fontsize => 1, 1893 fontsize => 1,
1910 align => -1, 1894 align => -1,
1911 valign => -1, 1895 valign => -1,
1912 padding_x => 2, 1896 padding_x => 2,
1913 padding_y => 2, 1897 padding_y => 2,
1914 can_events => 0, 1898 can_events => 0,
1915 %arg 1899 %arg
1916 ); 1900 );
1917 1901
1918 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1919 my $layout = new CFPlus::Layout; 1903 my $layout = new DC::Layout;
1920 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1921 $self->{template} = $layout; 1905 $self->{template} = $layout;
1922 } 1906 }
1923 1907
1924 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
1942 1926
1943 delete $self->{ox}; 1927 delete $self->{ox};
1944 $self->SUPER::realloc; 1928 $self->SUPER::realloc;
1945} 1929}
1946 1930
1931sub clear {
1932 my ($self) = @_;
1933
1934 $self->set_text ("");
1935}
1936
1947sub set_text { 1937sub set_text {
1948 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1949 1939
1950 return if $self->{text} eq "T$text"; 1940 return if $self->{text} eq "T$text";
1951 $self->{text} = "T$text"; 1941 $self->{text} = "T$text";
1974 1964
1975sub size_request { 1965sub size_request {
1976 my ($self) = @_; 1966 my ($self) = @_;
1977 1967
1978 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1979 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1980 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1981 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1982 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1983 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1984 1976
1985 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2057 : ($self->{w} - $size->[0]) * 0.5); 2049 : ($self->{w} - $size->[0]) * 0.5);
2058 2050
2059 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2060 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2061 : ($self->{h} - $size->[1]) * 0.5); 2053 : ($self->{h} - $size->[1]) * 0.5);
2054
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062 }; 2056 };
2063 2057
2064 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2058# unless ($self->{list}) {
2065 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2059# $self->{list} = DC::OpenGL::glGenList;
2066 2060# DC::OpenGL::glNewList $self->{list};
2067 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# DC::OpenGL::glEndList;
2063# }
2064#
2065# DC::OpenGL::glCallList $self->{list};
2066
2067 $self->{layout}->draw;
2068} 2068}
2069
2070#sub destroy {
2071# my ($self) = @_;
2072#
2073# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074#
2075# $self->SUPER::destroy;
2076#}
2069 2077
2070############################################################################# 2078#############################################################################
2071 2079
2072package CFPlus::UI::EntryBase; 2080package DC::UI::EntryBase;
2073 2081
2074our @ISA = CFPlus::UI::Label::; 2082our @ISA = DC::UI::Label::;
2075 2083
2076use CFPlus::OpenGL; 2084use DC::OpenGL;
2077 2085
2078sub new { 2086sub new {
2079 my $class = shift; 2087 my $class = shift;
2080 2088
2081 $class->SUPER::new ( 2089 $class->SUPER::new (
2082 fg => [1, 1, 1], 2090 fg => [1, 1, 1],
2083 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1],
2084 active_bg => [1, 1, 1, 0.5], 2093 active_bg => [0, 0, 1, .2],
2085 active_fg => [0, 0, 0], 2094 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0],
2086 can_hover => 1, 2096 can_hover => 1,
2087 can_focus => 1, 2097 can_focus => 1,
2088 valign => 0, 2098 valign => 0,
2089 can_events => 1, 2099 can_events => 1,
2090 ellipsise => 0, 2100 ellipsise => 0,
2101 padding_x => 4,
2102 padding_y => 2,
2091 #text => ... 2103 #text => ...
2092 #hidden => "*", 2104 #hidden => "*",
2093 @_ 2105 @_
2094 ) 2106 )
2095} 2107}
2146 2158
2147 if ($uni == 8) { 2159 if ($uni == 8) {
2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2160 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2149 } elsif ($uni == 127) { 2161 } elsif ($uni == 127) {
2150 substr $text, $self->{cursor}, 1, ""; 2162 substr $text, $self->{cursor}, 1, "";
2151 } elsif ($sym == CFPlus::SDLK_LEFT) { 2163 } elsif ($sym == DC::SDLK_LEFT) {
2152 --$self->{cursor} if $self->{cursor}; 2164 --$self->{cursor} if $self->{cursor};
2153 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2165 } elsif ($sym == DC::SDLK_RIGHT) {
2154 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2166 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2155 } elsif ($sym == CFPlus::SDLK_HOME) { 2167 } elsif ($sym == DC::SDLK_HOME) {
2156 # what a hack 2168 # what a hack
2157 $self->{cursor} = 2169 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2170 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1 2171 ? length $1
2160 : 0; 2172 : 0;
2161 } elsif ($sym == CFPlus::SDLK_END) { 2173 } elsif ($sym == DC::SDLK_END) {
2162 # uh, again 2174 # uh, again
2163 $self->{cursor} = 2175 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2176 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2165 ? $self->{cursor} + length $1 2177 ? $self->{cursor} + length $1
2166 : length $self->{text}; 2178 : length $self->{text};
2230 glColor_premultiply @{$self->{bg}}; 2242 glColor_premultiply @{$self->{bg}};
2231 } 2243 }
2232 2244
2233 glEnable GL_BLEND; 2245 glEnable GL_BLEND;
2234 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2246 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2235 glBegin GL_QUADS;
2236 glVertex 0 , 0;
2237 glVertex 0 , $self->{h};
2238 glVertex $self->{w}, $self->{h}; 2247 glRect 0, 0, $self->{w}, $self->{h};
2239 glVertex $self->{w}, 0;
2240 glEnd;
2241 glDisable GL_BLEND; 2248 glDisable GL_BLEND;
2242 2249
2243 $self->SUPER::_draw; 2250 $self->SUPER::_draw;
2244 2251
2245 #TODO: force update every cursor change :( 2252 #TODO: force update every cursor change :(
2247 2254
2248 unless (exists $self->{cur_h}) { 2255 unless (exists $self->{cur_h}) {
2249 my $text = substr $self->{text}, 0, $self->{cursor}; 2256 my $text = substr $self->{text}, 0, $self->{cursor};
2250 utf8::encode $text; 2257 utf8::encode $text;
2251 2258
2252 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2259 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2253 } 2260 }
2254 2261
2262 glColor_premultiply @{$self->{active_fg}};
2255 glBegin GL_LINES; 2263 glBegin GL_LINES;
2256 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2264 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2257 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2265 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2258 glEnd; 2266 glEnd;
2267
2268 glLineWidth 3;
2269 glColor @{$self->{active_outline}};
2270 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2271 glLineWidth 1;
2272
2273 } else {
2274 glColor @{$self->{outline}};
2275 glBegin GL_LINE_STRIP;
2276 glVertex .5, $self->{h} * .5;
2277 glVertex .5, $self->{h} - 2.5;
2278 glVertex $self->{w} - .5, $self->{h} - 2.5;
2279 glVertex $self->{w} - .5, $self->{h} * .5;
2280 glEnd;
2259 } 2281 }
2260} 2282}
2261 2283
2262############################################################################# 2284#############################################################################
2263 2285
2264package CFPlus::UI::Entry; 2286package DC::UI::Entry;
2265 2287
2266our @ISA = CFPlus::UI::EntryBase::; 2288our @ISA = DC::UI::EntryBase::;
2267 2289
2268use CFPlus::OpenGL; 2290use DC::OpenGL;
2269 2291
2270sub invoke_key_down { 2292sub invoke_key_down {
2271 my ($self, $ev) = @_; 2293 my ($self, $ev) = @_;
2272 2294
2273 my $sym = $ev->{sym}; 2295 my $sym = $ev->{sym};
2279 $self->{history_pointer} = -1; 2301 $self->{history_pointer} = -1;
2280 $self->{history_saveback} = ''; 2302 $self->{history_saveback} = '';
2281 $self->emit (activate => $txt); 2303 $self->emit (activate => $txt);
2282 $self->update; 2304 $self->update;
2283 2305
2284 } elsif ($sym == CFPlus::SDLK_UP) { 2306 } elsif ($sym == DC::SDLK_UP) {
2285 if ($self->{history_pointer} < 0) { 2307 if ($self->{history_pointer} < 0) {
2286 $self->{history_saveback} = $self->get_text; 2308 $self->{history_saveback} = $self->get_text;
2287 } 2309 }
2288 if (@{$self->{history} || []} > 0) { 2310 if (@{$self->{history} || []} > 0) {
2289 $self->{history_pointer}++; 2311 $self->{history_pointer}++;
2291 $self->{history_pointer} = @{$self->{history} || []} - 1; 2313 $self->{history_pointer} = @{$self->{history} || []} - 1;
2292 } 2314 }
2293 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2315 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2294 } 2316 }
2295 2317
2296 } elsif ($sym == CFPlus::SDLK_DOWN) { 2318 } elsif ($sym == DC::SDLK_DOWN) {
2297 $self->{history_pointer}--; 2319 $self->{history_pointer}--;
2298 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2320 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2299 2321
2300 if ($self->{history_pointer} >= 0) { 2322 if ($self->{history_pointer} >= 0) {
2301 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2323 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2310 1 2332 1
2311} 2333}
2312 2334
2313############################################################################# 2335#############################################################################
2314 2336
2315package CFPlus::UI::TextEdit; 2337package DC::UI::TextEdit;
2316 2338
2317our @ISA = CFPlus::UI::EntryBase::; 2339our @ISA = DC::UI::EntryBase::;
2318 2340
2319use CFPlus::OpenGL; 2341use DC::OpenGL;
2342
2343sub new {
2344 my $class = shift;
2345
2346 $class->SUPER::new (
2347 padding_y => 4,
2348
2349 @_
2350 )
2351}
2320 2352
2321sub move_cursor_ver { 2353sub move_cursor_ver {
2322 my ($self, $dy) = @_; 2354 my ($self, $dy) = @_;
2323 2355
2324 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2356 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2325 2357
2326 $y += $dy; 2358 $line += $dy;
2327 2359
2328 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2360 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2329 $self->{cursor} = $index; 2361 $self->{cursor} = $index;
2330 delete $self->{cur_h}; 2362 delete $self->{cur_h};
2331 $self->update; 2363 $self->update;
2332 return; 2364 return;
2333 } 2365 }
2336sub invoke_key_down { 2368sub invoke_key_down {
2337 my ($self, $ev) = @_; 2369 my ($self, $ev) = @_;
2338 2370
2339 my $sym = $ev->{sym}; 2371 my $sym = $ev->{sym};
2340 2372
2341 if ($sym == CFPlus::SDLK_UP) { 2373 if ($sym == DC::SDLK_UP) {
2342 $self->move_cursor_ver (-1); 2374 $self->move_cursor_ver (-1);
2343 } elsif ($sym == CFPlus::SDLK_DOWN) { 2375 } elsif ($sym == DC::SDLK_DOWN) {
2344 $self->move_cursor_ver (+1); 2376 $self->move_cursor_ver (+1);
2345 } else { 2377 } else {
2346 return $self->SUPER::invoke_key_down ($ev) 2378 return $self->SUPER::invoke_key_down ($ev)
2347 } 2379 }
2348 2380
2349 1 2381 1
2350} 2382}
2351 2383
2352############################################################################# 2384#############################################################################
2353 2385
2354package CFPlus::UI::Button; 2386package DC::UI::ButtonBin;
2355 2387
2356our @ISA = CFPlus::UI::Label::; 2388our @ISA = DC::UI::Bin::;
2357 2389
2358use CFPlus::OpenGL; 2390use DC::OpenGL;
2359 2391
2360my @tex = 2392my @tex =
2361 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2393 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2362 qw(b1_button_inactive.png b1_button_active.png); 2394 qw(b1_button_inactive.png b1_button_active.png);
2363 2395
2364sub new { 2396sub new {
2365 my $class = shift; 2397 my $class = shift;
2366 2398
2367 $class->SUPER::new ( 2399 $class->SUPER::new (
2400 can_hover => 1,
2401 align => 0,
2402 valign => 0,
2403 can_events => 1,
2404 @_
2405 )
2406}
2407
2408sub invoke_button_up {
2409 my ($self, $ev, $x, $y) = @_;
2410
2411 $self->emit ("activate")
2412 if $x >= 0 && $x < $self->{w}
2413 && $y >= 0 && $y < $self->{h};
2414
2415 1
2416}
2417
2418sub _draw {
2419 my ($self) = @_;
2420
2421 glEnable GL_TEXTURE_2D;
2422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2423 glColor 0, 0, 0, 1;
2424
2425 my $tex = $tex[$GRAB == $self];
2426 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2427
2428 glDisable GL_TEXTURE_2D;
2429
2430 $self->SUPER::_draw;
2431}
2432
2433#############################################################################
2434
2435package DC::UI::Button;
2436
2437our @ISA = DC::UI::Label::;
2438
2439use DC::OpenGL;
2440
2441my @tex =
2442 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2443 qw(b1_button_inactive.png b1_button_active.png);
2444
2445sub new {
2446 my $class = shift;
2447
2448 $class->SUPER::new (
2368 padding_x => 4, 2449 padding_x => 8,
2369 padding_y => 4, 2450 padding_y => 4,
2370 fg => [1.0, 1.0, 1.0], 2451 fg => [1.0, 1.0, 1.0],
2371 active_fg => [0.8, 0.8, 0.8], 2452 active_fg => [0.8, 0.8, 0.8],
2372 can_hover => 1, 2453 can_hover => 1,
2373 align => 0, 2454 align => 0,
2404 $self->SUPER::_draw; 2485 $self->SUPER::_draw;
2405} 2486}
2406 2487
2407############################################################################# 2488#############################################################################
2408 2489
2409package CFPlus::UI::CheckBox; 2490package DC::UI::CheckBox;
2410 2491
2411our @ISA = CFPlus::UI::DrawBG::; 2492our @ISA = DC::UI::DrawBG::;
2412 2493
2413my @tex = 2494my @tex =
2414 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2495 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2415 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2496 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2416 2497
2417use CFPlus::OpenGL; 2498use DC::OpenGL;
2418 2499
2419sub new { 2500sub new {
2420 my $class = shift; 2501 my $class = shift;
2421 2502
2422 $class->SUPER::new ( 2503 $class->SUPER::new (
2462sub _draw { 2543sub _draw {
2463 my ($self) = @_; 2544 my ($self) = @_;
2464 2545
2465 $self->SUPER::_draw; 2546 $self->SUPER::_draw;
2466 2547
2467 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2548 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2468 2549
2469 my ($w, $h) = @$self{qw(w h)}; 2550 my ($w, $h) = @$self{qw(w h)};
2470 2551
2471 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2552 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2472 2553
2479 glDisable GL_TEXTURE_2D; 2560 glDisable GL_TEXTURE_2D;
2480} 2561}
2481 2562
2482############################################################################# 2563#############################################################################
2483 2564
2484package CFPlus::UI::Image; 2565package DC::UI::Image;
2485 2566
2486our @ISA = CFPlus::UI::Base::; 2567our @ISA = DC::UI::Base::;
2487 2568
2488use CFPlus::OpenGL; 2569use DC::OpenGL;
2489 2570
2490our %texture_cache; 2571our %texture_cache;
2491 2572
2492sub new { 2573sub new {
2493 my $class = shift; 2574 my $class = shift;
2494 2575
2495 my $self = $class->SUPER::new ( 2576 my $self = $class->SUPER::new (
2496 can_events => 0, 2577 can_events => 0,
2578 scale => 1,
2497 @_, 2579 @_,
2498 ); 2580 );
2499 2581
2500 $self->{path} || $self->{tex} 2582 $self->{path} || $self->{tex}
2501 or Carp::croak "'path' or 'tex' attributes required"; 2583 or Carp::croak "'path' or 'tex' attributes required";
2502 2584
2503 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2585 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2504 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2586 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1;
2505 2587
2506 CFPlus::weaken $texture_cache{$self->{path}}; 2588 DC::weaken $texture_cache{$self->{path}};
2507 2589
2508 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2590 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2509 2591
2510 $self 2592 $self
2511} 2593}
2512 2594
2513sub STORABLE_freeze { 2595sub STORABLE_freeze {
2514 my ($self, $cloning) = @_; 2596 my ($self, $cloning) = @_;
2515 2597
2516 $self->{path} 2598 $self->{path}
2517 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2599 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2518 2600
2519 $self->{path} 2601 $self->{path}
2520} 2602}
2521 2603
2522sub STORABLE_attach { 2604sub STORABLE_attach {
2526} 2608}
2527 2609
2528sub size_request { 2610sub size_request {
2529 my ($self) = @_; 2611 my ($self) = @_;
2530 2612
2531 ($self->{tex}{w}, $self->{tex}{h}) 2613 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2532} 2614}
2533 2615
2534sub _draw { 2616sub _draw {
2535 my ($self) = @_; 2617 my ($self) = @_;
2536 2618
2546 } 2628 }
2547 2629
2548 glEnable GL_TEXTURE_2D; 2630 glEnable GL_TEXTURE_2D;
2549 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2631 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2550 2632
2551 $tex->draw_quad (0, 0, $w, $h); 2633 $tex->draw_quad_alpha (0, 0, $w, $h);
2552 2634
2553 glDisable GL_TEXTURE_2D; 2635 glDisable GL_TEXTURE_2D;
2554} 2636}
2555 2637
2556############################################################################# 2638#############################################################################
2557 2639
2558package CFPlus::UI::ImageButton; 2640package DC::UI::ImageButton;
2559 2641
2560our @ISA = CFPlus::UI::Image::; 2642our @ISA = DC::UI::Image::;
2561 2643
2562use CFPlus::OpenGL; 2644use DC::OpenGL;
2563 2645
2564my %textures; 2646my %textures;
2565 2647
2566sub new { 2648sub new {
2567 my $class = shift; 2649 my $class = shift;
2577 can_events => 1, 2659 can_events => 1,
2578 @_ 2660 @_
2579 ); 2661 );
2580} 2662}
2581 2663
2664sub invoke_button_down {
2665 my ($self, $ev, $x, $y) = @_;
2666
2667 1
2668}
2669
2582sub invoke_button_up { 2670sub invoke_button_up {
2583 my ($self, $ev, $x, $y) = @_; 2671 my ($self, $ev, $x, $y) = @_;
2584 2672
2585 $self->emit ("activate") 2673 $self->emit ("activate")
2586 if $x >= 0 && $x < $self->{w} 2674 if $x >= 0 && $x < $self->{w}
2589 1 2677 1
2590} 2678}
2591 2679
2592############################################################################# 2680#############################################################################
2593 2681
2594package CFPlus::UI::VGauge; 2682package DC::UI::VGauge;
2595 2683
2596our @ISA = CFPlus::UI::Base::; 2684our @ISA = DC::UI::Base::;
2597 2685
2598use List::Util qw(min max); 2686use List::Util qw(min max);
2599 2687
2600use CFPlus::OpenGL; 2688use DC::OpenGL;
2601 2689
2602my %tex = ( 2690my %tex = (
2603 food => [ 2691 food => [
2604 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2605 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2693 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2606 ], 2694 ],
2607 grace => [ 2695 grace => [
2608 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2609 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2697 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2610 ], 2698 ],
2611 hp => [ 2699 hp => [
2612 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2613 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2701 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2614 ], 2702 ],
2615 mana => [ 2703 mana => [
2616 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2617 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2705 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2618 ], 2706 ],
2619); 2707);
2620 2708
2621# eg. VGauge->new (gauge => 'food'), default gauge: food 2709# eg. VGauge->new (gauge => 'food'), default gauge: food
2724 glDisable GL_TEXTURE_2D; 2812 glDisable GL_TEXTURE_2D;
2725} 2813}
2726 2814
2727############################################################################# 2815#############################################################################
2728 2816
2817package DC::UI::Progress;
2818
2819our @ISA = DC::UI::Label::;
2820
2821use DC::OpenGL;
2822
2823sub new {
2824 my ($class, %arg) = @_;
2825
2826 my $self = $class->SUPER::new (
2827 fg => [1, 1, 1],
2828 bg => [0, 0, 1, 0.2],
2829 bar => [0.7, 0.5, 0.1, 0.8],
2830 outline => [0.4, 0.3, 0],
2831 fontsize => 0.9,
2832 valign => 0,
2833 align => 0,
2834 can_events => 1,
2835 ellipsise => 1,
2836 label => "%d%%",
2837 %arg,
2838 );
2839
2840 $self->set_value ($arg{value} || -1);
2841
2842 $self
2843}
2844
2845sub set_label {
2846 my ($self, $label) = @_;
2847
2848 return if $self->{label} eq $label;
2849 $self->{label} = $label;
2850
2851 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2852}
2853
2854sub set_value {
2855 my ($self, $value) = @_;
2856
2857 if ($self->{value} ne $value) {
2858 $self->{value} = $value;
2859
2860 if ($value < 0) {
2861 $self->set_text ("-");
2862 } else {
2863 $self->set_text (sprintf $self->{label}, $value * 100);
2864 }
2865
2866 $self->update;
2867 }
2868}
2869
2870sub _draw {
2871 my ($self) = @_;
2872
2873 glEnable GL_BLEND;
2874 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2875
2876 if ($self->{value} >= 0) {
2877 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2878
2879 glColor_premultiply @{$self->{bar}};
2880 glRect 2, 2, $s, $self->{h} - 2;
2881 glColor_premultiply @{$self->{bg}};
2882 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2883 }
2884
2885 glColor_premultiply @{$self->{outline}};
2886 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2887
2888 glDisable GL_BLEND;
2889
2890 {
2891 local $self->{bg}; # do not draw background
2892 $self->SUPER::_draw;
2893 }
2894}
2895
2896#############################################################################
2897
2898package DC::UI::ExperienceProgress;
2899
2900our @ISA = DC::UI::Progress::;
2901
2902sub new {
2903 my ($class, %arg) = @_;
2904
2905 my $self = $class->SUPER::new (
2906 tooltip => sub {
2907 my ($self) = @_;
2908
2909 sprintf "level %d\n%s points\n%s next level\n%s to go",
2910 $self->{lvl},
2911 ::formsep ($self->{exp}),
2912 ::formsep ($self->{nxt}),
2913 ::formsep ($self->{nxt} - $self->{exp}),
2914 },
2915 %arg
2916 );
2917
2918 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2919 if $::CONN;
2920
2921 $self
2922}
2923
2924sub DESTROY {
2925 my ($self) = @_;
2926
2927 delete $::CONN->{on_exp_update}{$self+0}
2928 if $::CONN;
2929
2930 $self->SUPER::DESTROY;
2931}
2932
2933sub set_value {
2934 my ($self, $lvl, $exp) = @_;
2935
2936 $self->{lvl} = $lvl;
2937 $self->{exp} = $exp;
2938
2939 my $v = -1;
2940
2941 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2942 my $l0 = $table->[$lvl - 1];
2943 my $l1 = $table->[$lvl];
2944
2945 $self->{nxt} = $l1;
2946
2947 $v = ($exp - $l0) / ($l1 - $l0);
2948 }
2949
2950 $self->SUPER::set_value ($v);
2951}
2952
2953#############################################################################
2954
2729package CFPlus::UI::Gauge; 2955package DC::UI::Gauge;
2730 2956
2731our @ISA = CFPlus::UI::VBox::; 2957our @ISA = DC::UI::VBox::;
2732 2958
2733sub new { 2959sub new {
2734 my ($class, %arg) = @_; 2960 my ($class, %arg) = @_;
2735 2961
2736 my $self = $class->SUPER::new ( 2962 my $self = $class->SUPER::new (
2738 can_hover => 1, 2964 can_hover => 1,
2739 can_events => 1, 2965 can_events => 1,
2740 %arg, 2966 %arg,
2741 ); 2967 );
2742 2968
2743 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2969 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999");
2744 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2970 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2745 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2971 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999");
2746 2972
2747 $self 2973 $self
2748} 2974}
2749 2975
2750sub set_fontsize { 2976sub set_fontsize {
2771 $self->{value}->set_text ($val); 2997 $self->{value}->set_text ($val);
2772} 2998}
2773 2999
2774############################################################################# 3000#############################################################################
2775 3001
2776package CFPlus::UI::Slider; 3002package DC::UI::Slider;
2777 3003
2778use strict; 3004use strict;
2779 3005
2780use CFPlus::OpenGL; 3006use DC::OpenGL;
2781 3007
2782our @ISA = CFPlus::UI::DrawBG::; 3008our @ISA = DC::UI::DrawBG::;
2783 3009
2784my @tex = 3010my @tex =
2785 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3011 map { new_from_file DC::Texture DC::find_rcfile $_ }
2786 qw(s1_slider.png s1_slider_bg.png); 3012 qw(s1_slider.png s1_slider_bg.png);
2787 3013
2788sub new { 3014sub new {
2789 my $class = shift; 3015 my $class = shift;
2790 3016
2858 3084
2859 $self->SUPER::invoke_button_down ($ev, $x, $y); 3085 $self->SUPER::invoke_button_down ($ev, $x, $y);
2860 3086
2861 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3087 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2862 3088
2863 $self->invoke_mouse_motion ($ev, $x, $y) 3089 $self->invoke_mouse_motion ($ev, $x, $y);
3090
3091 1
2864} 3092}
2865 3093
2866sub invoke_mouse_motion { 3094sub invoke_mouse_motion {
2867 my ($self, $ev, $x, $y) = @_; 3095 my ($self, $ev, $x, $y) = @_;
2868 3096
2884sub invoke_mouse_wheel { 3112sub invoke_mouse_wheel {
2885 my ($self, $ev) = @_; 3113 my ($self, $ev) = @_;
2886 3114
2887 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3115 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2888 3116
2889 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3117 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2890 3118
2891 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3119 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2892 3120
2893 ! ! $delta 3121 1
2894} 3122}
2895 3123
2896sub update { 3124sub update {
2897 my ($self) = @_; 3125 my ($self) = @_;
2898 3126
2947 glDisable GL_TEXTURE_2D; 3175 glDisable GL_TEXTURE_2D;
2948} 3176}
2949 3177
2950############################################################################# 3178#############################################################################
2951 3179
2952package CFPlus::UI::ValSlider; 3180package DC::UI::ValSlider;
2953 3181
2954our @ISA = CFPlus::UI::HBox::; 3182our @ISA = DC::UI::HBox::;
2955 3183
2956sub new { 3184sub new {
2957 my ($class, %arg) = @_; 3185 my ($class, %arg) = @_;
2958 3186
2959 my $range = delete $arg{range}; 3187 my $range = delete $arg{range};
2960 3188
2961 my $self = $class->SUPER::new ( 3189 my $self = $class->SUPER::new (
2962 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3190 slider => (new DC::UI::Slider expand => 1, range => $range),
2963 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3191 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2964 to_value => sub { shift }, 3192 to_value => sub { shift },
2965 from_value => sub { shift }, 3193 from_value => sub { shift },
2966 %arg, 3194 %arg,
2967 ); 3195 );
2968 3196
2988sub set_range { shift->{slider}->set_range (@_) } 3216sub set_range { shift->{slider}->set_range (@_) }
2989sub set_value { shift->{slider}->set_value (@_) } 3217sub set_value { shift->{slider}->set_value (@_) }
2990 3218
2991############################################################################# 3219#############################################################################
2992 3220
2993package CFPlus::UI::TextScroller; 3221package DC::UI::TextScroller;
2994 3222
2995our @ISA = CFPlus::UI::HBox::; 3223our @ISA = DC::UI::HBox::;
2996 3224
2997use CFPlus::OpenGL; 3225use DC::OpenGL;
2998 3226
2999sub new { 3227sub new {
3000 my $class = shift; 3228 my $class = shift;
3001 3229
3002 my $self = $class->SUPER::new ( 3230 my $self = $class->SUPER::new (
3004 can_events => 1, 3232 can_events => 1,
3005 indent => 0, 3233 indent => 0,
3006 #font => default_font 3234 #font => default_font
3007 @_, 3235 @_,
3008 3236
3009 layout => (new CFPlus::Layout), 3237 layout => (new DC::Layout),
3010 par => [], 3238 par => [],
3011 max_par => 0, 3239 max_par => 0,
3012 height => 0, 3240 height => 0,
3013 children => [ 3241 children => [
3014 (new CFPlus::UI::Empty expand => 1), 3242 (new DC::UI::Empty expand => 1),
3015 (new CFPlus::UI::Slider vertical => 1), 3243 (new DC::UI::Slider vertical => 1),
3016 ], 3244 ],
3017 ); 3245 );
3018 3246
3019 $self->{children}[1]->connect (changed => sub { $self->update }); 3247 $self->{children}[1]->connect (changed => sub { $self->update });
3020 3248
3029} 3257}
3030 3258
3031sub size_request { 3259sub size_request {
3032 my ($self) = @_; 3260 my ($self) = @_;
3033 3261
3034 my ($empty, $slider) = @{ $self->{children} }; 3262 my ($empty, $slider) = $self->visible_children;
3035 3263
3036 local $self->{children} = [$empty, $slider]; 3264 local $self->{children} = [$empty, $slider];
3037 $self->SUPER::size_request 3265 $self->SUPER::size_request
3038} 3266}
3039 3267
3203 $ROOT->on_post_alloc ($self => sub { 3431 $ROOT->on_post_alloc ($self => sub {
3204 $self->force_uptodate; 3432 $self->force_uptodate;
3205 3433
3206 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3434 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3207 3435
3208 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3436 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3209 glClearColor 0, 0, 0, 0; 3437 glClearColor 0, 0, 0, 0;
3210 glClear GL_COLOR_BUFFER_BIT; 3438 glClear GL_COLOR_BUFFER_BIT;
3211 3439
3212 package CFPlus::UI::Base; 3440 package DC::UI::Base;
3213 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3441 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3214 (0, 0, $self->{w}, $self->{h}); 3442 (0, 0, $self->{w}, $self->{h});
3215 3443
3216 my $top = int $self->{children}[1]{range}[0]; 3444 my $top = int $self->{children}[1]{range}[0];
3217 3445
3228 3456
3229 if ($y0 < $y + $h && $y < $y1) { 3457 if ($y0 < $y + $h && $y < $y1) {
3230 my $layout = $self->get_layout ($para); 3458 my $layout = $self->get_layout ($para);
3231 3459
3232 $layout->render ($para->{indent}, $y - $y0); 3460 $layout->render ($para->{indent}, $y - $y0);
3461 $layout->draw;
3233 3462
3234 if (my @w = @{ $para->{widget} }) { 3463 if (my @w = @{ $para->{widget} }) {
3235 my @s = $layout->get_shapes; 3464 my @s = $layout->get_shapes;
3236 3465
3237 for (@w) { 3466 for (@w) {
3275 $self->{children}[1]->draw; 3504 $self->{children}[1]->draw;
3276} 3505}
3277 3506
3278############################################################################# 3507#############################################################################
3279 3508
3280package CFPlus::UI::Animator; 3509package DC::UI::Animator;
3281 3510
3282use CFPlus::OpenGL; 3511use DC::OpenGL;
3283 3512
3284our @ISA = CFPlus::UI::Bin::; 3513our @ISA = DC::UI::Bin::;
3285 3514
3286sub moveto { 3515sub moveto {
3287 my ($self, $x, $y) = @_; 3516 my ($self, $x, $y) = @_;
3288 3517
3289 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3518 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3317 glPopMatrix; 3546 glPopMatrix;
3318} 3547}
3319 3548
3320############################################################################# 3549#############################################################################
3321 3550
3322package CFPlus::UI::Flopper; 3551package DC::UI::Flopper;
3323 3552
3324our @ISA = CFPlus::UI::Button::; 3553our @ISA = DC::UI::Button::;
3325 3554
3326sub new { 3555sub new {
3327 my $class = shift; 3556 my $class = shift;
3328 3557
3329 my $self = $class->SUPER::new ( 3558 my $self = $class->SUPER::new (
3341 $self->{other}->toggle_visibility; 3570 $self->{other}->toggle_visibility;
3342} 3571}
3343 3572
3344############################################################################# 3573#############################################################################
3345 3574
3346package CFPlus::UI::Tooltip; 3575package DC::UI::Tooltip;
3347 3576
3348our @ISA = CFPlus::UI::Bin::; 3577our @ISA = DC::UI::Bin::;
3349 3578
3350use CFPlus::OpenGL; 3579use DC::OpenGL;
3351 3580
3352sub new { 3581sub new {
3353 my $class = shift; 3582 my $class = shift;
3354 3583
3355 $class->SUPER::new ( 3584 $class->SUPER::new (
3359} 3588}
3360 3589
3361sub set_tooltip_from { 3590sub set_tooltip_from {
3362 my ($self, $widget) = @_; 3591 my ($self, $widget) = @_;
3363 3592
3364 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3365 if $widget->{tooltip} =~ /^#(.*)$/;
3366
3367 my $tooltip = $widget->{tooltip}; 3593 my $tip = $widget->{tooltip};
3594 $tip = $tip->($widget) if "CODE" eq ref $tip;
3595
3596 $tip = DC::Pod::section_label tooltip => $1
3597 if $tip =~ /^#(.*)$/;
3368 3598
3369 if ($ENV{CFPLUS_DEBUG} & 2) { 3599 if ($ENV{CFPLUS_DEBUG} & 2) {
3370 $tooltip .= "\n\n" . (ref $widget) . "\n" 3600 $tip .= "\n\n" . (ref $widget) . "\n"
3371 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3601 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3372 . "req $widget->{req_w} $widget->{req_h}\n" 3602 . "req $widget->{req_w} $widget->{req_h}\n"
3373 . "visible $widget->{visible}"; 3603 . "visible $widget->{visible}";
3374 } 3604 }
3375 3605
3376 $tooltip =~ s/^\n+//; 3606 $tip =~ s/^\n+//;
3377 $tooltip =~ s/\n+$//; 3607 $tip =~ s/\n+$//;
3378 3608
3379 $self->add (new CFPlus::UI::Label 3609 $self->add (new DC::UI::Label
3380 markup => $tooltip, 3610 markup => $tip,
3381 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3611 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3382 fontsize => 0.8, 3612 fontsize => 0.8,
3383 style => 1, # FLAG_INVERSE 3613 style => 1, # FLAG_INVERSE
3384 ellipsise => 0, 3614 ellipsise => 0,
3385 font => ($widget->{tooltip_font} || $::FONT_PROP), 3615 font => ($widget->{tooltip_font} || $::FONT_PROP),
3423} 3653}
3424 3654
3425sub _draw { 3655sub _draw {
3426 my ($self) = @_; 3656 my ($self) = @_;
3427 3657
3428 glTranslate 0.375, 0.375;
3429
3430 my ($w, $h) = @$self{qw(w h)}; 3658 my ($w, $h) = @$self{qw(w h)};
3431 3659
3432 glColor 1, 0.8, 0.4; 3660 glColor 1, 0.8, 0.4;
3433 glBegin GL_QUADS; 3661 glRect 0, 0, $w, $h;
3434 glVertex 0 , 0;
3435 glVertex 0 , $h;
3436 glVertex $w, $h;
3437 glVertex $w, 0;
3438 glEnd;
3439 3662
3440 glColor 0, 0, 0; 3663 glColor 0, 0, 0;
3441 glBegin GL_LINE_LOOP; 3664 glRect_lineloop .5, .5, $w + .5, $h + .5;
3442 glVertex 0 , 0;
3443 glVertex 0 , $h;
3444 glVertex $w, $h;
3445 glVertex $w, 0;
3446 glEnd;
3447 3665
3448 glTranslate 2 - 0.375, 2 - 0.375; 3666 glTranslate 2, 2;
3449 3667
3450 $self->SUPER::_draw; 3668 $self->SUPER::_draw;
3451} 3669}
3452 3670
3453############################################################################# 3671#############################################################################
3454 3672
3455package CFPlus::UI::Face; 3673package DC::UI::Face;
3456 3674
3457our @ISA = CFPlus::UI::DrawBG::; 3675our @ISA = DC::UI::DrawBG::;
3458 3676
3459use CFPlus::OpenGL; 3677use DC::OpenGL;
3460 3678
3461sub new { 3679sub new {
3462 my $class = shift; 3680 my $class = shift;
3463 3681
3464 my $self = $class->SUPER::new ( 3682 my $self = $class->SUPER::new (
3468 can_events => 0, 3686 can_events => 0,
3469 @_, 3687 @_,
3470 ); 3688 );
3471 3689
3472 if ($self->{anim} && $self->{animspeed}) { 3690 if ($self->{anim} && $self->{animspeed}) {
3473 CFPlus::weaken (my $widget = $self); 3691 DC::weaken (my $widget = $self);
3474 3692
3475 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3693 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3476 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3694 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3477 $self->{timer} = Event->timer (
3478 parked => 1,
3479 cb => sub {
3480 return unless $::CONN && $widget; 3695 return unless $::CONN;
3481 3696
3697 my $w = $widget
3698 or return;
3699
3482 ++$widget->{frame}; 3700 ++$w->{frame};
3483 $widget->update_face; 3701 $w->update_face;
3702
3703 # somehow, $widget can go away
3484 $widget->update; 3704 $w->update;
3485
3486 $widget->update_timer; 3705 $w->update_timer;
3487 },
3488 ); 3706 };
3489 3707
3490 $self->update_face; 3708 $self->update_face;
3491 $self->update_timer; 3709 $self->update_timer;
3492 } 3710 }
3493 3711
3498 my ($self) = @_; 3716 my ($self) = @_;
3499 3717
3500 return unless $self->{timer}; 3718 return unless $self->{timer};
3501 3719
3502 if ($self->{visible}) { 3720 if ($self->{visible}) {
3503 $self->{timer}->at (
3504 $self->{anim_start}
3505 + $self->{animspeed}
3506 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3507 );
3508 $self->{timer}->start; 3721 $self->{timer}->start;
3509 } else { 3722 } else {
3510 $self->{timer}->stop; 3723 $self->{timer}->stop;
3511 } 3724 }
3512} 3725}
3513 3726
3514sub update_face { 3727sub update_face {
3515 my ($self) = @_; 3728 my ($self) = @_;
3516 3729
3517 return unless $::CONN;
3518
3519 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3520 if ($anim && @$anim) {
3521 delete $self->{wait_face};
3522 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3523 }
3524 }
3525}
3526
3527sub size_request {
3528 my ($self) = @_;
3529
3530 if ($::CONN) { 3730 if ($::CONN) {
3531 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3731 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3532 if (my $tex = $::CONN->{texture}[$faceid]) { 3732 if ($anim && @$anim) {
3533 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3733 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3534 } else { 3734 delete $self->{face_change_cb};
3535 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3735
3536 $self->realloc; 3736 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3737 unless ($tex->{name} || $tex->{loading}) {
3738 $tex->upload (sub { $self->reconfigure });
3739 }
3537 }); 3740 }
3538 } 3741 }
3539 } 3742 }
3540 } 3743 }
3744}
3745
3746sub size_request {
3747 my ($self) = @_;
3748
3749 if ($::CONN) {
3750 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3751 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3752 if ($tex->{name}) {
3753 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3754 } elsif (!$tex->{loading}) {
3755 $tex->upload (sub { $self->reconfigure });
3756 }
3757 }
3758
3759 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3760 }
3761 }
3541 3762
3542 ($self->{size_w} || 8, $self->{size_h} || 8) 3763 ($self->{size_w} || 8, $self->{size_h} || 8)
3543} 3764}
3544 3765
3545sub update { 3766sub update {
3559} 3780}
3560 3781
3561sub _draw { 3782sub _draw {
3562 my ($self) = @_; 3783 my ($self) = @_;
3563 3784
3564 return unless $::CONN;
3565
3566 $self->SUPER::_draw; 3785 $self->SUPER::_draw;
3567 3786
3568 my $faceid = $::CONN->{faceid}[$self->{face}] 3787 if (my $tex = $self->{tex}) {
3569 or return;
3570
3571 my $tex = $::CONN->{texture}[$faceid];
3572
3573 if ($tex) {
3574 glEnable GL_TEXTURE_2D; 3788 glEnable GL_TEXTURE_2D;
3575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3789 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3576 glColor 0, 0, 0, 1; 3790 glColor 0, 0, 0, 1;
3577 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3791 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3578 glDisable GL_TEXTURE_2D; 3792 glDisable GL_TEXTURE_2D;
3588 $self->SUPER::destroy; 3802 $self->SUPER::destroy;
3589} 3803}
3590 3804
3591############################################################################# 3805#############################################################################
3592 3806
3593package CFPlus::UI::Buttonbar; 3807package DC::UI::Buttonbar;
3594 3808
3595our @ISA = CFPlus::UI::HBox::; 3809our @ISA = DC::UI::HBox::;
3596 3810
3597# TODO: should actually wrap buttons and other goodies. 3811# TODO: should actually wrap buttons and other goodies.
3598 3812
3599############################################################################# 3813#############################################################################
3600 3814
3601package CFPlus::UI::Menu; 3815package DC::UI::Menu;
3602 3816
3603our @ISA = CFPlus::UI::Toplevel::; 3817our @ISA = DC::UI::Toplevel::;
3604 3818
3605use CFPlus::OpenGL; 3819use DC::OpenGL;
3606 3820
3607sub new { 3821sub new {
3608 my $class = shift; 3822 my $class = shift;
3609 3823
3610 my $self = $class->SUPER::new ( 3824 my $self = $class->SUPER::new (
3611 items => [], 3825 items => [],
3612 z => 100, 3826 z => 100,
3613 @_, 3827 @_,
3614 ); 3828 );
3615 3829
3616 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3830 $self->add ($self->{vbox} = new DC::UI::VBox);
3617 3831
3618 for my $item (@{ $self->{items} }) { 3832 for my $item (@{ $self->{items} }) {
3619 my ($widget, $cb, $tooltip) = @$item; 3833 my ($widget, $cb, $tooltip) = @$item;
3620 3834
3621 # handle various types of items, only text for now 3835 # handle various types of items, only text for now
3622 if (!ref $widget) { 3836 if (!ref $widget) {
3623 if ($widget =~ /\t/) { 3837 if ($widget =~ /\t/) {
3624 my ($left, $right) = split /\t/, $widget, 2; 3838 my ($left, $right) = split /\t/, $widget, 2;
3625 3839
3626 $widget = new CFPlus::UI::HBox 3840 $widget = new DC::UI::HBox
3627 can_hover => 1, 3841 can_hover => 1,
3628 can_events => 1, 3842 can_events => 1,
3629 tooltip => $tooltip, 3843 tooltip => $tooltip,
3630 children => [ 3844 children => [
3631 (new CFPlus::UI::Label markup => $left, expand => 1), 3845 (new DC::UI::Label markup => $left, expand => 1),
3632 (new CFPlus::UI::Label markup => $right, align => +1), 3846 (new DC::UI::Label markup => $right, align => +1),
3633 ], 3847 ],
3634 ; 3848 ;
3635 3849
3636 } else { 3850 } else {
3637 $widget = new CFPlus::UI::Label 3851 $widget = new DC::UI::Label
3638 can_hover => 1, 3852 can_hover => 1,
3639 can_events => 1, 3853 can_events => 1,
3640 markup => $widget, 3854 markup => $widget,
3641 tooltip => $tooltip; 3855 tooltip => $tooltip;
3642 } 3856 }
3690 1 3904 1
3691} 3905}
3692 3906
3693############################################################################# 3907#############################################################################
3694 3908
3695package CFPlus::UI::Multiplexer; 3909package DC::UI::Multiplexer;
3696 3910
3697our @ISA = CFPlus::UI::Container::; 3911our @ISA = DC::UI::Container::;
3698 3912
3699sub new { 3913sub new {
3700 my $class = shift; 3914 my $class = shift;
3701 3915
3702 my $self = $class->SUPER::new ( 3916 my $self = $class->SUPER::new (
3763 $self->{current}->draw; 3977 $self->{current}->draw;
3764} 3978}
3765 3979
3766############################################################################# 3980#############################################################################
3767 3981
3768package CFPlus::UI::Notebook; 3982package DC::UI::Notebook;
3769 3983
3984use DC::OpenGL;
3985
3770our @ISA = CFPlus::UI::VBox::; 3986our @ISA = DC::UI::VBox::;
3771 3987
3772sub new { 3988sub new {
3773 my $class = shift; 3989 my $class = shift;
3774 3990
3775 my $self = $class->SUPER::new ( 3991 my $self = $class->SUPER::new (
3776 buttonbar => (new CFPlus::UI::Buttonbar), 3992 buttonbar => (new DC::UI::Buttonbar),
3777 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3993 multiplexer => (new DC::UI::Multiplexer expand => 1),
3994 active_outline => [.7, .7, 0.2],
3778 # filter => # will be put between multiplexer and $self 3995 # filter => # will be put between multiplexer and $self
3779 @_, 3996 @_,
3780 ); 3997 );
3781 3998
3782 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3999 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3783 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4000 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3784 4001
4002 {
4003 Scalar::Util::weaken (my $wself = $self);
4004
4005 $self->{multiplexer}->connect (c_add => sub {
4006 my ($mplex, $widgets) = @_;
4007
4008 for my $child (@$widgets) {
4009 Scalar::Util::weaken $child;
4010 $child->{c_tab_} ||= do {
4011 my $tab =
4012 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4013 ? $child->{c_tab}
4014 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4015
4016 $tab->connect (activate => sub {
4017 $wself->set_current_page ($child);
4018 });
4019
4020 $tab
4021 };
4022
4023 $self->{buttonbar}->add ($child->{c_tab_});
4024 }
4025 });
4026
4027 $self->{multiplexer}->connect (c_remove => sub {
4028 my ($mplex, $widgets) = @_;
4029
4030 for my $child (@$widgets) {
4031 $wself->{buttonbar}->remove ($child->{c_tab_});
4032 }
4033 });
4034 }
4035
3785 $self 4036 $self
3786} 4037}
3787 4038
3788sub add { 4039sub add {
4040 my ($self, @widgets) = @_;
4041
4042 $self->{multiplexer}->add (@widgets)
4043}
4044
4045sub remove {
4046 my ($self, @widgets) = @_;
4047
4048 $self->{multiplexer}->remove (@widgets)
4049}
4050
4051sub pages {
4052 my ($self) = @_;
4053 $self->{multiplexer}->children
4054}
4055
4056sub add_tab {
3789 my ($self, $title, $widget, $tooltip) = @_; 4057 my ($self, $title, $widget, $tooltip) = @_;
3790 4058
3791 CFPlus::weaken $self; 4059 $title = [$title, $tooltip] unless ref $title;
4060 $widget->{c_tab} = $title;
3792 4061
3793 $self->{buttonbar}->add (new CFPlus::UI::Button
3794 markup => $title,
3795 tooltip => $tooltip,
3796 on_activate => sub { $self->set_current_page ($widget) },
3797 );
3798
3799 $self->{multiplexer}->add ($widget); 4062 $self->add ($widget);
3800} 4063}
3801 4064
3802sub get_current_page { 4065sub get_current_page {
3803 my ($self) = @_; 4066 my ($self) = @_;
3804 4067
3810 4073
3811 $self->{multiplexer}->set_current_page ($page); 4074 $self->{multiplexer}->set_current_page ($page);
3812 $self->emit (page_changed => $self->{multiplexer}{current}); 4075 $self->emit (page_changed => $self->{multiplexer}{current});
3813} 4076}
3814 4077
4078sub _draw {
4079 my ($self) = @_;
4080
4081 $self->SUPER::_draw ();
4082
4083 if (my $cur = $self->{multiplexer}{current}) {
4084 if ($cur = $cur->{c_tab_}) {
4085 glTranslate $self->{buttonbar}{x} + $cur->{x},
4086 $self->{buttonbar}{y} + $cur->{y};
4087 glLineWidth 3;
4088 #glEnable GL_BLEND;
4089 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4090 glColor @{$self->{active_outline}};
4091 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4092 glLineWidth 1;
4093 #glDisable GL_BLEND;
4094 }
4095 }
4096}
4097
3815############################################################################# 4098#############################################################################
3816 4099
3817package CFPlus::UI::Selector; 4100package DC::UI::Selector;
3818 4101
3819use utf8; 4102use utf8;
3820 4103
3821our @ISA = CFPlus::UI::Button::; 4104our @ISA = DC::UI::Button::;
3822 4105
3823sub new { 4106sub new {
3824 my $class = shift; 4107 my $class = shift;
3825 4108
3826 my $self = $class->SUPER::new ( 4109 my $self = $class->SUPER::new (
3843 my ($value, $title, $tooltip) = @$_; 4126 my ($value, $title, $tooltip) = @$_;
3844 4127
3845 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4128 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3846 } 4129 }
3847 4130
3848 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4131 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3849} 4132}
3850 4133
3851sub _set_value { 4134sub _set_value {
3852 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
3853 4136
3854 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4137 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4138 $item ||= $self->{options}[0]
3855 or return; 4139 or return;
3856 4140
3857 $self->{value} = $item->[0]; 4141 $self->{value} = $item->[0];
3858 $self->set_markup ("$item->[1] ⇓"); 4142 $self->set_markup ("$item->[1] ⇓");
3859 $self->set_tooltip ($item->[2]); 4143# $self->set_tooltip ($item->[2]);
3860} 4144}
3861 4145
3862sub set_value { 4146sub set_value {
3863 my ($self, $value) = @_; 4147 my ($self, $value) = @_;
3864 4148
3866 4150
3867 $self->_set_value ($value); 4151 $self->_set_value ($value);
3868 $self->emit (changed => $value); 4152 $self->emit (changed => $value);
3869} 4153}
3870 4154
4155sub set_options {
4156 my ($self, $options) = @_;
4157
4158 $self->{options} = $options;
4159 $self->_set_value ($self->{value});
4160}
4161
3871############################################################################# 4162#############################################################################
3872 4163
3873package CFPlus::UI::Statusbox; 4164package DC::UI::Statusbox;
3874 4165
3875our @ISA = CFPlus::UI::VBox::; 4166our @ISA = DC::UI::VBox::;
3876 4167
3877sub new { 4168sub new {
3878 my $class = shift; 4169 my $class = shift;
3879 4170
3880 my $self = $class->SUPER::new ( 4171 my $self = $class->SUPER::new (
3881 fontsize => 0.8, 4172 fontsize => 0.8,
3882 @_, 4173 @_,
3883 ); 4174 );
3884 4175
3885 CFPlus::weaken (my $this = $self); 4176 DC::weaken (my $this = $self);
3886 4177
3887 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4178 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3888 4179
3889 $self 4180 $self
3890} 4181}
3891 4182
3892sub reorder { 4183sub reorder {
3893 my ($self) = @_; 4184 my ($self) = @_;
3894 my $NOW = Time::HiRes::time; 4185 my $NOW = Time::HiRes::time;
3895 4186
3896 # freeze display when hovering over any label 4187 # freeze display when hovering over any label
3897 return if $CFPlus::UI::TOOLTIP->{owner} 4188 return if $DC::UI::TOOLTIP->{owner}
3898 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4189 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3899 values %{ $self->{item} }; 4190 values %{ $self->{item} };
3900 4191
3901 while (my ($k, $v) = each %{ $self->{item} }) { 4192 while (my ($k, $v) = each %{ $self->{item} }) {
3902 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4193 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3903 } 4194 }
4195
4196 $self->{timer}->set (1, 1);
3904 4197
3905 my @widgets; 4198 my @widgets;
3906 4199
3907 my @items = sort { 4200 my @items = sort {
3908 $a->{pri} <=> $b->{pri} 4201 $a->{pri} <=> $b->{pri}
3909 or $b->{id} <=> $a->{id} 4202 or $b->{id} <=> $a->{id}
3910 } values %{ $self->{item} }; 4203 } values %{ $self->{item} };
3911
3912 $self->{timer}->interval (1);
3913 4204
3914 my $count = 10 + 1; 4205 my $count = 10 + 1;
3915 for my $item (@items) { 4206 for my $item (@items) {
3916 last unless --$count; 4207 last unless --$count;
3917 4208
3924 for ($short) { 4215 for ($short) {
3925 s/^\s+//; 4216 s/^\s+//;
3926 s/\s+/ /g; 4217 s/\s+/ /g;
3927 } 4218 }
3928 4219
3929 new CFPlus::UI::Label 4220 new DC::UI::Label
3930 markup => $short, 4221 markup => $short,
3931 tooltip => $item->{tooltip}, 4222 tooltip => $item->{tooltip},
3932 tooltip_font => $::FONT_PROP, 4223 tooltip_font => $::FONT_PROP,
3933 tooltip_width => 0.67, 4224 tooltip_width => 0.67,
3934 fontsize => $item->{fontsize} || $self->{fontsize}, 4225 fontsize => $item->{fontsize} || $self->{fontsize},
3941 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4232 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3942 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4233 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3943 $label->update; 4234 $label->update;
3944 $label->set_max_size (undef, $label->{req_h} * $diff) 4235 $label->set_max_size (undef, $label->{req_h} * $diff)
3945 if $diff < 1; 4236 if $diff < 1;
3946 $self->{timer}->interval (1/30); 4237 $self->{timer}->set (1/30, 1/30);
3947 } else { 4238 } else {
3948 $label->{fg}[3] = $item->{fg}[3] || 1; 4239 $label->{fg}[3] = $item->{fg}[3] || 1;
3949 } 4240 }
3950 4241
3951 push @widgets, $label; 4242 push @widgets, $label;
4013 $self->SUPER::destroy; 4304 $self->SUPER::destroy;
4014} 4305}
4015 4306
4016############################################################################# 4307#############################################################################
4017 4308
4018package CFPlus::UI::Root; 4309package DC::UI::Root;
4019 4310
4020our @ISA = CFPlus::UI::Container::; 4311our @ISA = DC::UI::Container::;
4021 4312
4022use List::Util qw(min max); 4313use List::Util qw(min max);
4023 4314
4024use CFPlus::OpenGL; 4315use DC::OpenGL;
4025 4316
4026sub new { 4317sub new {
4027 my $class = shift; 4318 my $class = shift;
4028 4319
4029 my $self = $class->SUPER::new ( 4320 my $self = $class->SUPER::new (
4030 visible => 1, 4321 visible => 1,
4031 @_, 4322 @_,
4032 ); 4323 );
4033 4324
4034 CFPlus::weaken ($self->{root} = $self); 4325 DC::weaken ($self->{root} = $self);
4035 4326
4036 $self 4327 $self
4037} 4328}
4038 4329
4039sub size_request { 4330sub size_request {
4087} 4378}
4088 4379
4089sub update { 4380sub update {
4090 my ($self) = @_; 4381 my ($self) = @_;
4091 4382
4092 $::WANT_REFRESH++; 4383 $::WANT_REFRESH = 1;
4093} 4384}
4094 4385
4095sub add { 4386sub add {
4096 my ($self, @children) = @_; 4387 my ($self, @children) = @_;
4097 4388
4134 while ($self->{refresh_hook}) { 4425 while ($self->{refresh_hook}) {
4135 $_->() 4426 $_->()
4136 for values %{delete $self->{refresh_hook}}; 4427 for values %{delete $self->{refresh_hook}};
4137 } 4428 }
4138 4429
4139 if ($self->{realloc}) { 4430 while ($self->{realloc}) {
4140 my %queue; 4431 my %queue;
4141 my @queue; 4432 my @queue;
4142 my $widget; 4433 my $widget;
4143 4434
4144 outer: 4435 outer:
4191 } 4482 }
4192 } 4483 }
4193 4484
4194 delete $self->{realloc}{$widget+0}; 4485 delete $self->{realloc}{$widget+0};
4195 } 4486 }
4196 }
4197 4487
4198 while (my $size_alloc = delete $self->{size_alloc}) { 4488 while (my $size_alloc = delete $self->{size_alloc}) {
4199 my @queue = sort { $b->{visible} <=> $a->{visible} } 4489 my @queue = sort { $a->{visible} <=> $b->{visible} }
4200 values %$size_alloc; 4490 values %$size_alloc;
4201 4491
4202 while () { 4492 while () {
4203 my $widget = pop @queue || last; 4493 my $widget = pop @queue || last;
4204 4494
4205 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4495 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4206 4496
4207 $w = 0 if $w < 0;
4208 $h = 0 if $h < 0;
4209
4210 $w = max $widget->{min_w}, $w; 4497 $w = max $widget->{min_w}, $w;
4211 $h = max $widget->{min_h}, $h; 4498 $h = max $widget->{min_h}, $h;
4212 4499
4213# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4500# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4214# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4501# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4215 4502
4216 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4503 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4217 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4504 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4218 4505
4219 $w = int $w + 0.5; 4506 $w = int $w + 0.5;
4220 $h = int $h + 0.5; 4507 $h = int $h + 0.5;
4221 4508
4222 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4509 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4223 $widget->{old_w} = $widget->{w}; 4510 $widget->{old_w} = $widget->{w};
4224 $widget->{old_h} = $widget->{h}; 4511 $widget->{old_h} = $widget->{h};
4225 4512
4226 $widget->{w} = $w; 4513 $widget->{w} = $w;
4227 $widget->{h} = $h; 4514 $widget->{h} = $h;
4228 4515
4229 $widget->emit (size_allocate => $w, $h); 4516 $widget->emit (size_allocate => $w, $h);
4517 }
4230 } 4518 }
4231 } 4519 }
4232 } 4520 }
4233 4521
4234 while ($self->{post_alloc_hook}) { 4522 while ($self->{post_alloc_hook}) {
4245 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4533 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4246 glMatrixMode GL_MODELVIEW; 4534 glMatrixMode GL_MODELVIEW;
4247 glLoadIdentity; 4535 glLoadIdentity;
4248 4536
4249 { 4537 {
4250 package CFPlus::UI::Base; 4538 package DC::UI::Base;
4251 4539
4252 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4540 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4253 (0, 0, $self->{w}, $self->{h}); 4541 (0, 0, $self->{w}, $self->{h});
4254 4542
4255 $self->_draw; 4543 $self->_draw;
4256 } 4544 }
4257} 4545}
4258 4546
4259############################################################################# 4547#############################################################################
4260 4548
4261package CFPlus::UI; 4549package DC::UI;
4262 4550
4263$ROOT = new CFPlus::UI::Root; 4551$ROOT = new DC::UI::Root;
4264$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4552$TOOLTIP = new DC::UI::Tooltip z => 900;
4265 4553
42661 45541
4267 4555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines