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.388 by root, Fri Jul 20 18:30:03 2007 UTC vs.
Revision 1.454 by root, Wed Dec 26 21:03:21 2007 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
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};
741 can_events => 0, 698 can_events => 0,
742 %arg, 699 %arg,
743 ); 700 );
744 701
745 $self->add (@$children) 702 $self->add (@$children)
746 if $children; 703 if $children && @$children;
747 704
748 $self 705 $self
749} 706}
750 707
751sub realloc { 708sub realloc {
760 my ($self, @widgets) = @_; 717 my ($self, @widgets) = @_;
761 718
762 $_->set_parent ($self) 719 $_->set_parent ($self)
763 for @widgets; 720 for @widgets;
764 721
722 # TODO: only do this in widgets that need it, e.g. root, fixed
765 use sort 'stable'; 723 use sort 'stable';
766 724
767 $self->{children} = [ 725 $self->{children} = [
768 sort { $a->{z} <=> $b->{z} } 726 sort { $a->{z} <=> $b->{z} }
769 @{$self->{children}}, @widgets 727 @{$self->{children}}, @widgets
770 ]; 728 ];
771 729
772 $self->realloc; 730 $self->realloc;
731
732 $self->emit (c_add => \@widgets);
733
734 map $_+0, @widgets
773} 735}
774 736
775sub children { 737sub children {
776 @{ $_[0]{children} } 738 @{ $_[0]{children} }
777} 739}
778 740
779sub remove { 741sub remove {
780 my ($self, $child) = @_; 742 my ($self, @widgets) = @_;
781 743
744 $self->emit (c_remove => \@widgets);
745
746 for my $child (@widgets) {
782 delete $child->{parent}; 747 delete $child->{parent};
783 $child->hide; 748 $child->hide;
784
785 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 749 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
750 }
786 751
787 $self->realloc; 752 $self->realloc;
788} 753}
789 754
790sub clear { 755sub clear {
791 my ($self) = @_; 756 my ($self) = @_;
792 757
793 my $children = delete $self->{children}; 758 my $children = $self->{children};
794 $self->{children} = []; 759 $self->{children} = [];
795 760
796 for (@$children) { 761 for (@$children) {
797 delete $_->{parent}; 762 delete $_->{parent};
798 $_->hide; 763 $_->hide;
818} 783}
819 784
820sub _draw { 785sub _draw {
821 my ($self) = @_; 786 my ($self) = @_;
822 787
823 $_->draw for @{$self->{children}}; 788 $_->draw for $self->visible_children;
824} 789}
825 790
826############################################################################# 791#############################################################################
827 792
828package CFPlus::UI::Bin; 793package DC::UI::Bin;
829 794
830our @ISA = CFPlus::UI::Container::; 795our @ISA = DC::UI::Container::;
831 796
832sub new { 797sub new {
833 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
834 799
835 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 800 my $child = (delete $arg{child}) || new DC::UI::Empty::;
836 801
837 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
838} 803}
839 804
840sub add { 805sub add {
841 my ($self, $child) = @_; 806 my ($self, $child) = @_;
842 807
843 $self->SUPER::remove ($_) for @{ $self->{children} }; 808 $self->clear;
844 $self->SUPER::add ($child); 809 $self->SUPER::add ($child);
845} 810}
846 811
847sub remove { 812sub remove {
848 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
849 814
850 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
851 816
852 $self->{children} = [new CFPlus::UI::Empty] 817 $self->{children} = [new DC::UI::Empty]
853 unless @{$self->{children}}; 818 unless @{$self->{children}};
854} 819}
855 820
856sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
857 822
866 831
867 1 832 1
868} 833}
869 834
870############################################################################# 835#############################################################################
871
872# back-buffered drawing area 836# back-buffered drawing area
873 837
874package CFPlus::UI::Window; 838package DC::UI::Window;
875 839
876our @ISA = CFPlus::UI::Bin::; 840our @ISA = DC::UI::Bin::;
877 841
878use CFPlus::OpenGL; 842use DC::OpenGL;
879 843
880sub new { 844sub new {
881 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
882 846
883 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
905} 869}
906 870
907sub render_child { 871sub render_child {
908 my ($self) = @_; 872 my ($self) = @_;
909 873
910 $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 {
911 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
912 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
913 877
914 { 878 {
915 package CFPlus::UI::Base; 879 package DC::UI::Base;
916 880
917 local ($draw_x, $draw_y, $draw_w, $draw_h) = 881 local ($draw_x, $draw_y, $draw_w, $draw_h) =
918 (0, 0, $self->{w}, $self->{h}); 882 (0, 0, $self->{w}, $self->{h});
919 883
920 $self->_render; 884 $self->_render;
937 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
938} 902}
939 903
940############################################################################# 904#############################################################################
941 905
942package CFPlus::UI::ViewPort; 906package DC::UI::ViewPort;
943 907
944use List::Util qw(min max); 908use List::Util qw(min max);
945 909
946our @ISA = CFPlus::UI::Window::; 910our @ISA = DC::UI::Window::;
947 911
948sub new { 912sub new {
949 my $class = shift; 913 my $class = shift;
950 914
951 $class->SUPER::new ( 915 $class->SUPER::new (
958sub size_request { 922sub size_request {
959 my ($self) = @_; 923 my ($self) = @_;
960 924
961 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 925 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
962 926
963 $w = 10 if $self->{scroll_x}; 927 $w = 1 if $self->{scroll_x};
964 $h = 10 if $self->{scroll_y}; 928 $h = 1 if $self->{scroll_y};
965 929
966 ($w, $h) 930 ($w, $h)
967} 931}
968 932
969sub invoke_size_allocate { 933sub invoke_size_allocate {
990 $self->{view_x} = $x; 954 $self->{view_x} = $x;
991 $self->{view_y} = $y; 955 $self->{view_y} = $y;
992 956
993 $self->emit (changed => $x, $y); 957 $self->emit (changed => $x, $y);
994 $self->update; 958 $self->update;
959 }
960}
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);
995 } 977 }
996} 978}
997 979
998# 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
999sub coord2local { 981sub coord2local {
1017 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1018 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1019 ) { 1001 ) {
1020 $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})
1021 } else { 1003 } else {
1022 $self->CFPlus::UI::Base::find_widget ($x, $y) 1004 $self->DC::UI::Base::find_widget ($x, $y)
1023 } 1005 }
1024} 1006}
1025 1007
1026sub _render { 1008sub _render {
1027 my ($self) = @_; 1009 my ($self) = @_;
1028 1010
1029 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};
1030 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};
1031 1013
1032 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1033 1015
1034 $self->SUPER::_render; 1016 $self->SUPER::_render;
1035} 1017}
1036 1018
1037############################################################################# 1019#############################################################################
1038 1020
1039package CFPlus::UI::ScrolledWindow; 1021package DC::UI::ScrolledWindow;
1040 1022
1041our @ISA = CFPlus::UI::Table::; 1023our @ISA = DC::UI::Table::;
1042 1024
1043sub new { 1025sub new {
1044 my ($class, %arg) = @_; 1026 my ($class, %arg) = @_;
1045 1027
1046 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1047 1029
1048 my $self; 1030 my $self;
1049 1031
1050 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new DC::UI::Slider
1033 c_col => 0,
1034 c_row => 1,
1051 vertical => 0, 1035 vertical => 0,
1052 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1053 on_changed => sub { 1037 on_changed => sub {
1054 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1055 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1056 }, 1040 },
1057 ; 1041 ;
1058 1042
1059 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new DC::UI::Slider
1044 c_col => 1,
1045 c_row => 0,
1060 vertical => 1, 1046 vertical => 1,
1061 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1062 on_changed => sub { 1048 on_changed => sub {
1063 $self->{vpos} = $_[1]; 1049 $self->{vpos} = $_[1];
1064 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1074 col_expand => [1, 0], 1060 col_expand => [1, 0],
1075 row_expand => [1, 0], 1061 row_expand => [1, 0],
1076 %arg, 1062 %arg,
1077 ); 1063 );
1078 1064
1079 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new DC::UI::ViewPort
1066 c_col => 0,
1067 c_row => 0,
1080 expand => 1, 1068 expand => 1,
1081 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1082 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1083 on_changed => sub { 1071 on_changed => sub {
1084 my ($vp, $x, $y) = @_; 1072 my ($vp, $x, $y) = @_;
1086 $vp->{parent}{hslider}->set_value ($x); 1074 $vp->{parent}{hslider}->set_value ($x);
1087 $vp->{parent}{vslider}->set_value ($y); 1075 $vp->{parent}{vslider}->set_value ($y);
1088 1076
1089 0 1077 0
1090 }, 1078 },
1079 on_size_allocate => sub {
1080 my ($vp, $w, $h) = @_;
1081 $vp->{parent}->update_slider;
1082 0
1083 },
1091 ; 1084 ;
1092 1085
1093 $self->SUPER::add_at (0, 0, $self->{vp}); 1086 $self->SUPER::add ($self->{vp});
1094 1087
1095 $self->add ($child) if $child; 1088 $self->add ($child) if $child;
1096 1089
1097 $self 1090 $self
1098} 1091}
1099
1100#TODO# update range on size_allocate depending on child
1101 1092
1102sub add { 1093sub add {
1103 my ($self, $widget) = @_; 1094 my ($self, $widget) = @_;
1104 1095
1105 $self->{vp}->add ($self->{child} = $widget); 1096 $self->{vp}->add ($self->{child} = $widget);
1106} 1097}
1107 1098
1099sub set_offset { shift->{vp}->set_offset (@_) }
1100sub set_center { shift->{vp}->set_center (@_) }
1101sub make_visible { shift->{vp}->make_visible (@_) }
1102
1108sub update_slider { 1103sub update_slider {
1109 my ($self) = @_; 1104 my ($self) = @_;
1110 1105
1111 my $child = ($self->{vp} or return)->child; 1106 my $child = ($self->{vp} or return)->child;
1112 1107
1108 if ($self->{scroll_x}) {
1113 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1109 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1114 $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]);
1115 1111
1116 my $visible = $w1 > $w2; 1112 my $visible = $w1 > $w2;
1117 if ($visible != $self->{hslider}{visible}) { 1113 if ($visible != $self->{hslider_visible}) {
1114 $self->{hslider_visible} = $visible;
1118 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) 1115 $visible ? $self->SUPER::add ($self->{hslider})
1119 : $self->{hslider}->hide; 1116 : $self->SUPER::remove ($self->{hslider});
1117 }
1120 } 1118 }
1121 1119
1120 if ($self->{scroll_y}) {
1122 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1121 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1123 $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]);
1124 1123
1125 my $visible = $h1 > $h2; 1124 my $visible = $h1 > $h2;
1126 if ($visible != $self->{vslider}{visible}) { 1125 if ($visible != $self->{vslider_visible}) {
1126 $self->{vslider_visible} = $visible;
1127 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) 1127 $visible ? $self->SUPER::add ($self->{vslider})
1128 : $self->{vslider}->hide; 1128 : $self->SUPER::remove ($self->{vslider});
1129 }
1130 }
1131}
1132
1133sub start_dragging {
1134 my ($self, $ev) = @_;
1135
1136 $self->grab_focus;
1137
1138 my $ox = $self->{vp}{view_x};
1139 my $oy = $self->{vp}{view_y};
1140
1141 $self->{motion} = sub {
1142 my ($ev, $x, $y) = @_;
1143
1144 $ox -= $ev->{xrel};
1145 $oy -= $ev->{yrel};
1146
1147 $self->{vp}->set_offset ($ox, $oy);
1129 } 1148 };
1130}
1131
1132sub update {
1133 my ($self) = @_;
1134
1135 $self->update_slider;
1136 $self->SUPER::update;
1137} 1149}
1138 1150
1139sub invoke_mouse_wheel { 1151sub invoke_mouse_wheel {
1140 my ($self, $ev) = @_; 1152 my ($self, $ev) = @_;
1141 1153
1142 $self->{vslider}->emit (mouse_wheel => $ev) 1154 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1143 if $ev->{dy};
1144
1145 $self->{hslider}->emit (mouse_wheel => $ev) 1155 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1146 if $ev->{dx};
1147 1156
1148 1 1157 1
1149} 1158}
1150 1159
1151sub invoke_button_down { 1160sub invoke_button_down {
1152 my ($self, $ev, $x, $y) = @_; 1161 my ($self, $ev, $x, $y) = @_;
1153 1162
1154 if ($ev->{button} == 2) { 1163 if ($ev->{button} == 2) {
1155 $self->grab_focus; 1164 $self->start_dragging ($ev);
1156
1157 my $ox = $self->{vp}{view_x} + $ev->{x};
1158 my $oy = $self->{vp}{view_y} + $ev->{y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1164 $self->update;
1165 };
1166
1167 return 1; 1165 return 1;
1168 } 1166 }
1169 1167
1170 0 1168 0
1171} 1169}
1198 $self->SUPER::invoke_size_allocate ($w, $h) 1196 $self->SUPER::invoke_size_allocate ($w, $h)
1199} 1197}
1200 1198
1201############################################################################# 1199#############################################################################
1202 1200
1203package CFPlus::UI::Frame; 1201package DC::UI::Frame;
1204 1202
1205our @ISA = CFPlus::UI::Bin::; 1203our @ISA = DC::UI::Bin::;
1206 1204
1207use CFPlus::OpenGL; 1205use DC::OpenGL;
1208 1206
1209sub new { 1207sub new {
1210 my $class = shift; 1208 my $class = shift;
1211 1209
1212 $class->SUPER::new ( 1210 $class->SUPER::new (
1222 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
1223 1221
1224 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
1225 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1226 glColor_premultiply @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
1227
1228 glBegin GL_QUADS;
1229 glVertex 0 , 0;
1230 glVertex 0 , $h;
1231 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
1232 glVertex $w, 0;
1233 glEnd;
1234
1235 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
1236 } 1227 }
1237 1228
1238 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1239} 1230}
1240 1231
1241############################################################################# 1232#############################################################################
1242 1233
1243package CFPlus::UI::FancyFrame; 1234package DC::UI::FancyFrame;
1244 1235
1245our @ISA = CFPlus::UI::Bin::; 1236our @ISA = DC::UI::Bin::;
1246 1237
1247use CFPlus::OpenGL; 1238use DC::OpenGL;
1248 1239
1249sub new { 1240sub new {
1250 my ($class, %arg) = @_; 1241 my ($class, %arg) = @_;
1251 1242
1252 if ((exists $arg{label}) && !ref $arg{label}) { 1243 if ((exists $arg{label}) && !ref $arg{label}) {
1253 $arg{label} = new CFPlus::UI::Label 1244 $arg{label} = new DC::UI::Label
1254 align => 1, 1245 align => 1,
1255 valign => 0, 1246 valign => 0,
1256 text => $arg{label}, 1247 text => $arg{label},
1257 fontsize => ($arg{border} || 0.8) * 0.75; 1248 fontsize => ($arg{border} || 0.8) * 0.75;
1258 } 1249 }
1270 1261
1271sub add { 1262sub add {
1272 my ($self, @widgets) = @_; 1263 my ($self, @widgets) = @_;
1273 1264
1274 $self->SUPER::add (@widgets); 1265 $self->SUPER::add (@widgets);
1275 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1266 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1276} 1267}
1277 1268
1278sub border { 1269sub border {
1279 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
1280} 1271}
1338 } 1329 }
1339} 1330}
1340 1331
1341############################################################################# 1332#############################################################################
1342 1333
1343package CFPlus::UI::Toplevel; 1334package DC::UI::Toplevel;
1344 1335
1345our @ISA = CFPlus::UI::Bin::; 1336our @ISA = DC::UI::Bin::;
1346 1337
1347use CFPlus::OpenGL; 1338use DC::OpenGL;
1348 1339
1349my $bg = 1340my $bg =
1350 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1341 new_from_file DC::Texture DC::find_rcfile "d1_bg.png",
1351 mipmap => 1, wrap => 1; 1342 mipmap => 1, wrap => 1;
1352 1343
1353my @border = 1344my @border =
1354 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1345 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1355 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);
1356 1347
1357my @icon = 1348my @icon =
1358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1349 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1359 qw(x1_move.png x1_resize.png); 1350 qw(x1_move.png x1_resize.png);
1360 1351
1361sub new { 1352sub new {
1362 my ($class, %arg) = @_; 1353 my ($class, %arg) = @_;
1363 1354
1369 min_w => 64, 1360 min_w => 64,
1370 min_h => 32, 1361 min_h => 32,
1371 %arg, 1362 %arg,
1372 ); 1363 );
1373 1364
1374 $self->{title_widget} = new CFPlus::UI::Label 1365 $self->{title_widget} = new DC::UI::Label
1375 align => 0, 1366 align => 0,
1376 valign => 1, 1367 valign => 1,
1377 text => $self->{title}, 1368 text => $self->{title},
1378 fontsize => $self->{border}, 1369 fontsize => $self->{border},
1379 if exists $self->{title}; 1370 if exists $self->{title};
1380 1371
1381 if ($self->{has_close_button}) { 1372 if ($self->{has_close_button}) {
1382 $self->{close_button} = 1373 $self->{close_button} =
1383 new CFPlus::UI::ImageButton 1374 new DC::UI::ImageButton
1384 path => 'x1_close.png', 1375 path => 'x1_close.png',
1385 on_activate => sub { $self->emit ("delete") }; 1376 on_activate => sub { $self->emit ("delete") };
1386 1377
1387 $self->CFPlus::UI::Container::add ($self->{close_button}); 1378 $self->DC::UI::Container::add ($self->{close_button});
1388 } 1379 }
1389 1380
1390 $self 1381 $self
1391} 1382}
1392 1383
1393sub add { 1384sub add {
1394 my ($self, @widgets) = @_; 1385 my ($self, @widgets) = @_;
1395 1386
1396 $self->SUPER::add (@widgets); 1387 $self->SUPER::add (@widgets);
1397 $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};
1398 $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};
1399} 1390}
1400 1391
1401sub border { 1392sub border {
1402 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
1403} 1403}
1404 1404
1405sub size_request { 1405sub size_request {
1406 my ($self) = @_; 1406 my ($self) = @_;
1407 1407
1579 if $self->{close_button}; 1579 if $self->{close_button};
1580} 1580}
1581 1581
1582############################################################################# 1582#############################################################################
1583 1583
1584package CFPlus::UI::Table; 1584package DC::UI::Table;
1585 1585
1586our @ISA = CFPlus::UI::Base::; 1586our @ISA = DC::UI::Container::;
1587 1587
1588use List::Util qw(max sum); 1588use List::Util qw(max sum);
1589 1589
1590use CFPlus::OpenGL; 1590use DC::OpenGL;
1591 1591
1592sub new { 1592sub new {
1593 my $class = shift; 1593 my $class = shift;
1594 1594
1595 $class->SUPER::new ( 1595 $class->SUPER::new (
1596 children => [],
1597 col_expand => [], 1596 col_expand => [],
1598 row_expand => [], 1597 row_expand => [],
1599 @_, 1598 @_,
1600 ) 1599 )
1601} 1600}
1602 1601
1603sub children {
1604 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1605}
1606
1607# TODO: store row/col info in child widget and use standard add/del
1608sub add { 1602sub add {
1609 my $self = shift; 1603 my ($self, @widgets) = @_;
1610 1604
1611 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# 1605 for my $child (@widgets) {
1612 $self->add_at (@_); 1606 $child->{c_rowspan} ||= 1;
1607 $child->{c_colspan} ||= 1;
1608 }
1609
1610 $self->SUPER::add (@widgets);
1613} 1611}
1614 1612
1615sub add_at { 1613sub add_at {
1616 my $self = shift; 1614 my $self = shift;
1617 1615
1616 my @widgets;
1617
1618 while (@_) { 1618 while (@_) {
1619 my ($col, $row, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1620 1620
1621 $child->set_parent ($self); 1621 $child->{c_row} = $row;
1622 $self->{children}[$row][$col] = $child; 1622 $child->{c_col} = $col;
1623 }
1624 1623
1625 $self->{force_realloc} = 1; 1624 push @widgets, $child;
1626 $self->{force_size_alloc} = 1; 1625 }
1627 $self->realloc;
1628}
1629 1626
1630sub remove { 1627 $self->add (@widgets);
1628}
1629
1630sub get_wh {
1631 my ($self, $child) = @_; 1631 my ($self) = @_;
1632 1632
1633 for (@{ $self->{children} }) { 1633 my (@w, @h);
1634 for (@{ $_ || [] }) { 1634
1635 $_ = undef if $_ == $child; 1635 my @children = $self->children;
1636
1637 # first pass, columns
1638 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640
1641 my $sw = sum @w[$c .. $c + $cs - 1];
1642
1643 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1636 } 1645 }
1637 } 1646 }
1638}
1639 1647
1640# TODO: move to container class maybe? send children a signal on removal? 1648 # second pass, rows
1641sub clear { 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1642 my ($self) = @_;
1643
1644 my @children = $self->children;
1645 delete $self->{children};
1646
1647 for (@children) {
1648 delete $_->{parent};
1649 $_->hide;
1650 }
1651
1652 $self->realloc;
1653}
1654
1655sub get_wh {
1656 my ($self) = @_;
1657
1658 my (@w, @h);
1659
1660 for my $y (0 .. $#{$self->{children}}) {
1661 my $row = $self->{children}[$y]
1662 or next;
1663
1664 for my $x (0 .. $#$row) {
1665 my $widget = $row->[$x]
1666 or next;
1667 my ($w, $h) = @$widget{qw(req_w req_h)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1668 1651
1669 $w[$x] = max $w[$x], $w; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1670 $h[$y] = max $h[$y], $h; 1653
1654 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1671 } 1656 }
1672 } 1657 }
1673 1658
1674 (\@w, \@h) 1659 (\@w, \@h)
1675} 1660}
1691 my ($ws, $hs) = $self->get_wh; 1676 my ($ws, $hs) = $self->get_wh;
1692 1677
1693 my $req_w = (sum @$ws) || 1; 1678 my $req_w = (sum @$ws) || 1;
1694 my $req_h = (sum @$hs) || 1; 1679 my $req_h = (sum @$hs) || 1;
1695 1680
1696 # TODO: nicer code 1681 # now linearly scale the rows/columns to the allocated size
1697 my @col_expand = @{$self->{col_expand}}; 1682 my @col_expand = @{$self->{col_expand}};
1698 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1699 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1700 1685
1701 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1702 1687
1703 CFPlus::UI::harmonize $ws; 1688 DC::UI::harmonize $ws;
1704 1689
1705 my @row_expand = @{$self->{row_expand}}; 1690 my @row_expand = @{$self->{row_expand}};
1706 @row_expand = (1) x @$ws unless @row_expand; 1691 @row_expand = (1) x @$ws unless @row_expand;
1707 my $row_expand = (sum @row_expand) || 1; 1692 my $row_expand = (sum @row_expand) || 1;
1708 1693
1709 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1710 1695
1711 CFPlus::UI::harmonize $hs; 1696 DC::UI::harmonize $hs;
1712 1697
1713 my $y; 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1714 1700
1715 for my $r (0 .. $#{$self->{children}}) { 1701 for my $widget ($self->children) {
1716 my $row = $self->{children}[$r] 1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1717 or next;
1718 1703
1719 my $x = 0; 1704 $widget->configure (
1720 my $row_h = $hs->[$r]; 1705 $x[$c], $y[$r],
1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1721 1707 );
1722 for my $c (0 .. $#$row) {
1723 my $col_w = $ws->[$c];
1724
1725 if (my $widget = $row->[$c]) {
1726 $widget->configure ($x, $y, $col_w, $row_h);
1727 }
1728
1729 $x += $col_w;
1730 }
1731
1732 $y += $row_h;
1733 } 1708 }
1734 1709
1735 1 1710 1
1736} 1711}
1737 1712
1738sub find_widget {
1739 my ($self, $x, $y) = @_;
1740
1741 $x -= $self->{x};
1742 $y -= $self->{y};
1743
1744 my $res;
1745
1746 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1747 $res = $_->find_widget ($x, $y)
1748 and return $res;
1749 }
1750
1751 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1752}
1753
1754sub _draw {
1755 my ($self) = @_;
1756
1757 for (grep $_, @{$self->{children}}) {
1758 $_->draw for grep $_, @$_;
1759 }
1760}
1761
1762############################################################################# 1713#############################################################################
1763 1714
1764package CFPlus::UI::Fixed; 1715package DC::UI::Fixed;
1765 1716
1766use List::Util qw(min max); 1717use List::Util qw(min max);
1767 1718
1768our @ISA = CFPlus::UI::Container::; 1719our @ISA = DC::UI::Container::;
1769
1770sub add {
1771 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1772
1773 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1774 $self->SUPER::add ($child);
1775}
1776 1720
1777sub _scale($$$) { 1721sub _scale($$$) {
1778 my ($mode, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1779 1723
1780 $mode eq "abs" ? $val 1724 $rel ? $val * $max : $val
1781 : $mode eq "rel" ? $val * $max
1782 : 0
1783} 1725}
1784 1726
1785sub size_request { 1727sub size_request {
1786 my ($self) = @_; 1728 my ($self) = @_;
1787 1729
1788 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1730 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1789 1731
1790 # determine overall size by querying abs widgets 1732 # determine overall size by querying abs widgets
1791 for my $child ($self->visible_children) { 1733 for my $child ($self->visible_children) {
1792 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};
1793 1737
1794 if ($pos eq "abs") {
1795 $w = _scale $size, $w, $child->{req_w};
1796 $h = _scale $size, $h, $child->{req_h};
1797
1798 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1738 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1799 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1739 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1800 } 1740 }
1801 } 1741 }
1802 1742
1803 my $W = $x2 - $x1; 1743 my $W = $x2 - $x1;
1804 my $H = $y2 - $y1; 1744 my $H = $y2 - $y1;
1805 1745
1806 # now layout remaining widgets 1746 # now layout remaining widgets
1807 for my $child ($self->visible_children) { 1747 for my $child ($self->visible_children) {
1808 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;
1809 1751
1810 if ($pos ne "abs") {
1811 $x = _scale $pos, $x, $W;
1812 $y = _scale $pos, $x, $H;
1813 $w = _scale $size, $w, $child->{req_w};
1814 $h = _scale $size, $h, $child->{req_h};
1815
1816 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1752 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1817 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1753 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1818 } 1754 }
1819 } 1755 }
1820 1756
1821 my $W = $x2 - $x1; 1757 my $W = $x2 - $x1;
1822 my $H = $y2 - $y1; 1758 my $H = $y2 - $y1;
1826 1762
1827sub invoke_size_allocate { 1763sub invoke_size_allocate {
1828 my ($self, $W, $H) = @_; 1764 my ($self, $W, $H) = @_;
1829 1765
1830 for my $child ($self->visible_children) { 1766 for my $child ($self->visible_children) {
1831 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;
1832 1769
1833 $x = _scale $pos, $x, $W; 1770 $x += $child->{c_halign} * $child->{req_w};
1834 $y = _scale $pos, $x, $H; 1771 $y += $child->{c_valign} * $child->{req_h};
1835 $w = _scale $size, $w, $child->{req_w};
1836 $h = _scale $size, $h, $child->{req_h};
1837 1772
1838 $child->configure ($x, $y, $w, $h); 1773 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1839 } 1774 }
1840 1775
1841 1 1776 1
1842} 1777}
1843 1778
1844############################################################################# 1779#############################################################################
1845 1780
1846package CFPlus::UI::Box; 1781package DC::UI::Box;
1847 1782
1848our @ISA = CFPlus::UI::Container::; 1783our @ISA = DC::UI::Container::;
1849 1784
1850sub size_request { 1785sub size_request {
1851 my ($self) = @_; 1786 my ($self) = @_;
1787
1788 my @children = $self->visible_children;
1852 1789
1853 $self->{vertical} 1790 $self->{vertical}
1854 ? ( 1791 ? (
1855 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1792 (List::Util::max map $_->{req_w}, @children),
1856 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_h}, @children),
1857 ) 1794 )
1858 : ( 1795 : (
1859 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1796 (List::Util::sum map $_->{req_w}, @children),
1860 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1797 (List::Util::max map $_->{req_h}, @children),
1861 ) 1798 )
1862} 1799}
1863 1800
1864sub invoke_size_allocate { 1801sub invoke_size_allocate {
1865 my ($self, $w, $h) = @_; 1802 my ($self, $w, $h) = @_;
1886 $req[$_] += $space * $children[$_]{expand} 1823 $req[$_] += $space * $children[$_]{expand}
1887 for 0 .. $#children; 1824 for 0 .. $#children;
1888 } 1825 }
1889 } 1826 }
1890 1827
1891 CFPlus::UI::harmonize \@req; 1828 DC::UI::harmonize \@req;
1892 1829
1893 my $pos = 0; 1830 my $pos = 0;
1894 for (0 .. $#children) { 1831 for (0 .. $#children) {
1895 my $alloc = $req[$_]; 1832 my $alloc = $req[$_];
1896 $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));
1901 1 1838 1
1902} 1839}
1903 1840
1904############################################################################# 1841#############################################################################
1905 1842
1906package CFPlus::UI::HBox; 1843package DC::UI::HBox;
1907 1844
1908our @ISA = CFPlus::UI::Box::; 1845our @ISA = DC::UI::Box::;
1909 1846
1910sub new { 1847sub new {
1911 my $class = shift; 1848 my $class = shift;
1912 1849
1913 $class->SUPER::new ( 1850 $class->SUPER::new (
1916 ) 1853 )
1917} 1854}
1918 1855
1919############################################################################# 1856#############################################################################
1920 1857
1921package CFPlus::UI::VBox; 1858package DC::UI::VBox;
1922 1859
1923our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1924 1861
1925sub new { 1862sub new {
1926 my $class = shift; 1863 my $class = shift;
1927 1864
1928 $class->SUPER::new ( 1865 $class->SUPER::new (
1931 ) 1868 )
1932} 1869}
1933 1870
1934############################################################################# 1871#############################################################################
1935 1872
1936package CFPlus::UI::Label; 1873package DC::UI::Label;
1937 1874
1938our @ISA = CFPlus::UI::DrawBG::; 1875our @ISA = DC::UI::DrawBG::;
1939 1876
1940use CFPlus::OpenGL; 1877use DC::OpenGL;
1941 1878
1942sub new { 1879sub new {
1943 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1944 1881
1945 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1950 #text => initial text 1887 #text => initial text
1951 #markup => initial narkup 1888 #markup => initial narkup
1952 #max_w => maximum pixel width 1889 #max_w => maximum pixel width
1953 #style => 0, # render flags 1890 #style => 0, # render flags
1954 ellipsise => 3, # end 1891 ellipsise => 3, # end
1955 layout => (new CFPlus::Layout), 1892 layout => (new DC::Layout),
1956 fontsize => 1, 1893 fontsize => 1,
1957 align => -1, 1894 align => -1,
1958 valign => -1, 1895 valign => -1,
1959 padding_x => 2, 1896 padding_x => 2,
1960 padding_y => 2, 1897 padding_y => 2,
1961 can_events => 0, 1898 can_events => 0,
1962 %arg 1899 %arg
1963 ); 1900 );
1964 1901
1965 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1966 my $layout = new CFPlus::Layout; 1903 my $layout = new DC::Layout;
1967 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1968 $self->{template} = $layout; 1905 $self->{template} = $layout;
1969 } 1906 }
1970 1907
1971 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
1989 1926
1990 delete $self->{ox}; 1927 delete $self->{ox};
1991 $self->SUPER::realloc; 1928 $self->SUPER::realloc;
1992} 1929}
1993 1930
1931sub clear {
1932 my ($self) = @_;
1933
1934 $self->set_text ("");
1935}
1936
1994sub set_text { 1937sub set_text {
1995 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1996 1939
1997 return if $self->{text} eq "T$text"; 1940 return if $self->{text} eq "T$text";
1998 $self->{text} = "T$text"; 1941 $self->{text} = "T$text";
2021 1964
2022sub size_request { 1965sub size_request {
2023 my ($self) = @_; 1966 my ($self) = @_;
2024 1967
2025 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
2026 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
2027 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
2028 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
2029 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2030 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2031 1976
2032 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2104 : ($self->{w} - $size->[0]) * 0.5); 2049 : ($self->{w} - $size->[0]) * 0.5);
2105 2050
2106 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2107 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2108 : ($self->{h} - $size->[1]) * 0.5); 2053 : ($self->{h} - $size->[1]) * 0.5);
2054
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2109 }; 2056 };
2110 2057
2111 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2058# unless ($self->{list}) {
2112 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2059# $self->{list} = DC::OpenGL::glGenList;
2113 2060# DC::OpenGL::glNewList $self->{list};
2114 $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;
2115} 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#}
2116 2077
2117############################################################################# 2078#############################################################################
2118 2079
2119package CFPlus::UI::EntryBase; 2080package DC::UI::EntryBase;
2120 2081
2121our @ISA = CFPlus::UI::Label::; 2082our @ISA = DC::UI::Label::;
2122 2083
2123use CFPlus::OpenGL; 2084use DC::OpenGL;
2124 2085
2125sub new { 2086sub new {
2126 my $class = shift; 2087 my $class = shift;
2127 2088
2128 $class->SUPER::new ( 2089 $class->SUPER::new (
2129 fg => [1, 1, 1], 2090 fg => [1, 1, 1],
2130 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1],
2131 active_bg => [1, 1, 1, 0.5], 2093 active_bg => [0, 0, 1, .2],
2132 active_fg => [0, 0, 0], 2094 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0],
2133 can_hover => 1, 2096 can_hover => 1,
2134 can_focus => 1, 2097 can_focus => 1,
2135 valign => 0, 2098 valign => 0,
2136 can_events => 1, 2099 can_events => 1,
2137 ellipsise => 0, 2100 ellipsise => 0,
2193 2156
2194 if ($uni == 8) { 2157 if ($uni == 8) {
2195 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2196 } elsif ($uni == 127) { 2159 } elsif ($uni == 127) {
2197 substr $text, $self->{cursor}, 1, ""; 2160 substr $text, $self->{cursor}, 1, "";
2198 } elsif ($sym == CFPlus::SDLK_LEFT) { 2161 } elsif ($sym == DC::SDLK_LEFT) {
2199 --$self->{cursor} if $self->{cursor}; 2162 --$self->{cursor} if $self->{cursor};
2200 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2163 } elsif ($sym == DC::SDLK_RIGHT) {
2201 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2164 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2202 } elsif ($sym == CFPlus::SDLK_HOME) { 2165 } elsif ($sym == DC::SDLK_HOME) {
2203 # what a hack 2166 # what a hack
2204 $self->{cursor} = 2167 $self->{cursor} =
2205 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2206 ? length $1 2169 ? length $1
2207 : 0; 2170 : 0;
2208 } elsif ($sym == CFPlus::SDLK_END) { 2171 } elsif ($sym == DC::SDLK_END) {
2209 # uh, again 2172 # uh, again
2210 $self->{cursor} = 2173 $self->{cursor} =
2211 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2212 ? $self->{cursor} + length $1 2175 ? $self->{cursor} + length $1
2213 : length $self->{text}; 2176 : length $self->{text};
2277 glColor_premultiply @{$self->{bg}}; 2240 glColor_premultiply @{$self->{bg}};
2278 } 2241 }
2279 2242
2280 glEnable GL_BLEND; 2243 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2244 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2282 glBegin GL_QUADS;
2283 glVertex 0 , 0;
2284 glVertex 0 , $self->{h};
2285 glVertex $self->{w}, $self->{h}; 2245 glRect 0, 0, $self->{w}, $self->{h};
2286 glVertex $self->{w}, 0;
2287 glEnd;
2288 glDisable GL_BLEND; 2246 glDisable GL_BLEND;
2289 2247
2290 $self->SUPER::_draw; 2248 $self->SUPER::_draw;
2291 2249
2292 #TODO: force update every cursor change :( 2250 #TODO: force update every cursor change :(
2294 2252
2295 unless (exists $self->{cur_h}) { 2253 unless (exists $self->{cur_h}) {
2296 my $text = substr $self->{text}, 0, $self->{cursor}; 2254 my $text = substr $self->{text}, 0, $self->{cursor};
2297 utf8::encode $text; 2255 utf8::encode $text;
2298 2256
2299 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2300 } 2258 }
2301 2259
2260 glColor_premultiply @{$self->{active_fg}};
2302 glBegin GL_LINES; 2261 glBegin GL_LINES;
2303 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2262 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2304 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2263 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2305 glEnd; 2264 glEnd;
2265
2266 glLineWidth 3;
2267 glColor @{$self->{active_outline}};
2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2269 glLineWidth 1;
2270
2271 } else {
2272 glColor @{$self->{outline}};
2273 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5;
2278 glEnd;
2306 } 2279 }
2307} 2280}
2308 2281
2309############################################################################# 2282#############################################################################
2310 2283
2311package CFPlus::UI::Entry; 2284package DC::UI::Entry;
2312 2285
2313our @ISA = CFPlus::UI::EntryBase::; 2286our @ISA = DC::UI::EntryBase::;
2314 2287
2315use CFPlus::OpenGL; 2288use DC::OpenGL;
2316 2289
2317sub invoke_key_down { 2290sub invoke_key_down {
2318 my ($self, $ev) = @_; 2291 my ($self, $ev) = @_;
2319 2292
2320 my $sym = $ev->{sym}; 2293 my $sym = $ev->{sym};
2326 $self->{history_pointer} = -1; 2299 $self->{history_pointer} = -1;
2327 $self->{history_saveback} = ''; 2300 $self->{history_saveback} = '';
2328 $self->emit (activate => $txt); 2301 $self->emit (activate => $txt);
2329 $self->update; 2302 $self->update;
2330 2303
2331 } elsif ($sym == CFPlus::SDLK_UP) { 2304 } elsif ($sym == DC::SDLK_UP) {
2332 if ($self->{history_pointer} < 0) { 2305 if ($self->{history_pointer} < 0) {
2333 $self->{history_saveback} = $self->get_text; 2306 $self->{history_saveback} = $self->get_text;
2334 } 2307 }
2335 if (@{$self->{history} || []} > 0) { 2308 if (@{$self->{history} || []} > 0) {
2336 $self->{history_pointer}++; 2309 $self->{history_pointer}++;
2338 $self->{history_pointer} = @{$self->{history} || []} - 1; 2311 $self->{history_pointer} = @{$self->{history} || []} - 1;
2339 } 2312 }
2340 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2313 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2341 } 2314 }
2342 2315
2343 } elsif ($sym == CFPlus::SDLK_DOWN) { 2316 } elsif ($sym == DC::SDLK_DOWN) {
2344 $self->{history_pointer}--; 2317 $self->{history_pointer}--;
2345 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2346 2319
2347 if ($self->{history_pointer} >= 0) { 2320 if ($self->{history_pointer} >= 0) {
2348 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2321 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2357 1 2330 1
2358} 2331}
2359 2332
2360############################################################################# 2333#############################################################################
2361 2334
2362package CFPlus::UI::TextEdit; 2335package DC::UI::TextEdit;
2363 2336
2364our @ISA = CFPlus::UI::EntryBase::; 2337our @ISA = DC::UI::EntryBase::;
2365 2338
2366use CFPlus::OpenGL; 2339use DC::OpenGL;
2367 2340
2368sub move_cursor_ver { 2341sub move_cursor_ver {
2369 my ($self, $dy) = @_; 2342 my ($self, $dy) = @_;
2370 2343
2371 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2383sub invoke_key_down { 2356sub invoke_key_down {
2384 my ($self, $ev) = @_; 2357 my ($self, $ev) = @_;
2385 2358
2386 my $sym = $ev->{sym}; 2359 my $sym = $ev->{sym};
2387 2360
2388 if ($sym == CFPlus::SDLK_UP) { 2361 if ($sym == DC::SDLK_UP) {
2389 $self->move_cursor_ver (-1); 2362 $self->move_cursor_ver (-1);
2390 } elsif ($sym == CFPlus::SDLK_DOWN) { 2363 } elsif ($sym == DC::SDLK_DOWN) {
2391 $self->move_cursor_ver (+1); 2364 $self->move_cursor_ver (+1);
2392 } else { 2365 } else {
2393 return $self->SUPER::invoke_key_down ($ev) 2366 return $self->SUPER::invoke_key_down ($ev)
2394 } 2367 }
2395 2368
2396 1 2369 1
2397} 2370}
2398 2371
2399############################################################################# 2372#############################################################################
2400 2373
2401package CFPlus::UI::Button; 2374package DC::UI::ButtonBin;
2402 2375
2403our @ISA = CFPlus::UI::Label::; 2376our @ISA = DC::UI::Bin::;
2404 2377
2405use CFPlus::OpenGL; 2378use DC::OpenGL;
2406 2379
2407my @tex = 2380my @tex =
2408 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2381 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png);
2383
2384sub new {
2385 my $class = shift;
2386
2387 $class->SUPER::new (
2388 can_hover => 1,
2389 align => 0,
2390 valign => 0,
2391 can_events => 1,
2392 @_
2393 )
2394}
2395
2396sub invoke_button_up {
2397 my ($self, $ev, $x, $y) = @_;
2398
2399 $self->emit ("activate")
2400 if $x >= 0 && $x < $self->{w}
2401 && $y >= 0 && $y < $self->{h};
2402
2403 1
2404}
2405
2406sub _draw {
2407 my ($self) = @_;
2408
2409 glEnable GL_TEXTURE_2D;
2410 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2411 glColor 0, 0, 0, 1;
2412
2413 my $tex = $tex[$GRAB == $self];
2414 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2415
2416 glDisable GL_TEXTURE_2D;
2417
2418 $self->SUPER::_draw;
2419}
2420
2421#############################################################################
2422
2423package DC::UI::Button;
2424
2425our @ISA = DC::UI::Label::;
2426
2427use DC::OpenGL;
2428
2429my @tex =
2430 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2409 qw(b1_button_inactive.png b1_button_active.png); 2431 qw(b1_button_inactive.png b1_button_active.png);
2410 2432
2411sub new { 2433sub new {
2412 my $class = shift; 2434 my $class = shift;
2413 2435
2451 $self->SUPER::_draw; 2473 $self->SUPER::_draw;
2452} 2474}
2453 2475
2454############################################################################# 2476#############################################################################
2455 2477
2456package CFPlus::UI::CheckBox; 2478package DC::UI::CheckBox;
2457 2479
2458our @ISA = CFPlus::UI::DrawBG::; 2480our @ISA = DC::UI::DrawBG::;
2459 2481
2460my @tex = 2482my @tex =
2461 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2483 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2462 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2484 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2463 2485
2464use CFPlus::OpenGL; 2486use DC::OpenGL;
2465 2487
2466sub new { 2488sub new {
2467 my $class = shift; 2489 my $class = shift;
2468 2490
2469 $class->SUPER::new ( 2491 $class->SUPER::new (
2509sub _draw { 2531sub _draw {
2510 my ($self) = @_; 2532 my ($self) = @_;
2511 2533
2512 $self->SUPER::_draw; 2534 $self->SUPER::_draw;
2513 2535
2514 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2536 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2515 2537
2516 my ($w, $h) = @$self{qw(w h)}; 2538 my ($w, $h) = @$self{qw(w h)};
2517 2539
2518 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2540 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2519 2541
2526 glDisable GL_TEXTURE_2D; 2548 glDisable GL_TEXTURE_2D;
2527} 2549}
2528 2550
2529############################################################################# 2551#############################################################################
2530 2552
2531package CFPlus::UI::Image; 2553package DC::UI::Image;
2532 2554
2533our @ISA = CFPlus::UI::Base::; 2555our @ISA = DC::UI::Base::;
2534 2556
2535use CFPlus::OpenGL; 2557use DC::OpenGL;
2536 2558
2537our %texture_cache; 2559our %texture_cache;
2538 2560
2539sub new { 2561sub new {
2540 my $class = shift; 2562 my $class = shift;
2541 2563
2542 my $self = $class->SUPER::new ( 2564 my $self = $class->SUPER::new (
2543 can_events => 0, 2565 can_events => 0,
2566 scale => 1,
2544 @_, 2567 @_,
2545 ); 2568 );
2546 2569
2547 $self->{path} || $self->{tex} 2570 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2571 or Carp::croak "'path' or 'tex' attributes required";
2549 2572
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2573 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2574 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1;
2552 2575
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2576 DC::weaken $texture_cache{$self->{path}};
2554 2577
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2579
2557 $self 2580 $self
2558} 2581}
2559 2582
2560sub STORABLE_freeze { 2583sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2584 my ($self, $cloning) = @_;
2562 2585
2563 $self->{path} 2586 $self->{path}
2564 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2587 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2565 2588
2566 $self->{path} 2589 $self->{path}
2567} 2590}
2568 2591
2569sub STORABLE_attach { 2592sub STORABLE_attach {
2573} 2596}
2574 2597
2575sub size_request { 2598sub size_request {
2576 my ($self) = @_; 2599 my ($self) = @_;
2577 2600
2578 ($self->{tex}{w}, $self->{tex}{h}) 2601 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2579} 2602}
2580 2603
2581sub _draw { 2604sub _draw {
2582 my ($self) = @_; 2605 my ($self) = @_;
2583 2606
2593 } 2616 }
2594 2617
2595 glEnable GL_TEXTURE_2D; 2618 glEnable GL_TEXTURE_2D;
2596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2619 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2597 2620
2598 $tex->draw_quad (0, 0, $w, $h); 2621 $tex->draw_quad_alpha (0, 0, $w, $h);
2599 2622
2600 glDisable GL_TEXTURE_2D; 2623 glDisable GL_TEXTURE_2D;
2601} 2624}
2602 2625
2603############################################################################# 2626#############################################################################
2604 2627
2605package CFPlus::UI::ImageButton; 2628package DC::UI::ImageButton;
2606 2629
2607our @ISA = CFPlus::UI::Image::; 2630our @ISA = DC::UI::Image::;
2608 2631
2609use CFPlus::OpenGL; 2632use DC::OpenGL;
2610 2633
2611my %textures; 2634my %textures;
2612 2635
2613sub new { 2636sub new {
2614 my $class = shift; 2637 my $class = shift;
2624 can_events => 1, 2647 can_events => 1,
2625 @_ 2648 @_
2626 ); 2649 );
2627} 2650}
2628 2651
2652sub invoke_button_down {
2653 my ($self, $ev, $x, $y) = @_;
2654
2655 1
2656}
2657
2629sub invoke_button_up { 2658sub invoke_button_up {
2630 my ($self, $ev, $x, $y) = @_; 2659 my ($self, $ev, $x, $y) = @_;
2631 2660
2632 $self->emit ("activate") 2661 $self->emit ("activate")
2633 if $x >= 0 && $x < $self->{w} 2662 if $x >= 0 && $x < $self->{w}
2636 1 2665 1
2637} 2666}
2638 2667
2639############################################################################# 2668#############################################################################
2640 2669
2641package CFPlus::UI::VGauge; 2670package DC::UI::VGauge;
2642 2671
2643our @ISA = CFPlus::UI::Base::; 2672our @ISA = DC::UI::Base::;
2644 2673
2645use List::Util qw(min max); 2674use List::Util qw(min max);
2646 2675
2647use CFPlus::OpenGL; 2676use DC::OpenGL;
2648 2677
2649my %tex = ( 2678my %tex = (
2650 food => [ 2679 food => [
2651 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2680 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2652 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2653 ], 2682 ],
2654 grace => [ 2683 grace => [
2655 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2684 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2656 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2685 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2657 ], 2686 ],
2658 hp => [ 2687 hp => [
2659 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2688 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2660 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2661 ], 2690 ],
2662 mana => [ 2691 mana => [
2663 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2664 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2693 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2665 ], 2694 ],
2666); 2695);
2667 2696
2668# eg. VGauge->new (gauge => 'food'), default gauge: food 2697# eg. VGauge->new (gauge => 'food'), default gauge: food
2771 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
2772} 2801}
2773 2802
2774############################################################################# 2803#############################################################################
2775 2804
2805package DC::UI::Progress;
2806
2807our @ISA = DC::UI::Label::;
2808
2809use DC::OpenGL;
2810
2811sub new {
2812 my ($class, %arg) = @_;
2813
2814 my $self = $class->SUPER::new (
2815 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9,
2820 valign => 0,
2821 align => 0,
2822 can_events => 1,
2823 ellipsise => 1,
2824 label => "%d%%",
2825 %arg,
2826 );
2827
2828 $self->set_value ($arg{value} || -1);
2829
2830 $self
2831}
2832
2833sub set_label {
2834 my ($self, $label) = @_;
2835
2836 return if $self->{label} eq $label;
2837 $self->{label} = $label;
2838
2839 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2840}
2841
2842sub set_value {
2843 my ($self, $value) = @_;
2844
2845 if ($self->{value} ne $value) {
2846 $self->{value} = $value;
2847
2848 if ($value < 0) {
2849 $self->set_text ("-");
2850 } else {
2851 $self->set_text (sprintf $self->{label}, $value * 100);
2852 }
2853
2854 $self->update;
2855 }
2856}
2857
2858sub _draw {
2859 my ($self) = @_;
2860
2861 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863
2864 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2866
2867 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2;
2869 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2871 }
2872
2873 glColor_premultiply @{$self->{outline}};
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2875
2876 glDisable GL_BLEND;
2877
2878 {
2879 local $self->{bg}; # do not draw background
2880 $self->SUPER::_draw;
2881 }
2882}
2883
2884#############################################################################
2885
2886package DC::UI::ExperienceProgress;
2887
2888our @ISA = DC::UI::Progress::;
2889
2890sub new {
2891 my ($class, %arg) = @_;
2892
2893 my $self = $class->SUPER::new (
2894 tooltip => sub {
2895 my ($self) = @_;
2896
2897 sprintf "level %d\n%s points\n%s next level\n%s to go",
2898 $self->{lvl},
2899 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}),
2902 },
2903 %arg
2904 );
2905
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN;
2908
2909 $self
2910}
2911
2912sub DESTROY {
2913 my ($self) = @_;
2914
2915 delete $::CONN->{on_exp_update}{$self+0}
2916 if $::CONN;
2917
2918 $self->SUPER::DESTROY;
2919}
2920
2921sub set_value {
2922 my ($self, $lvl, $exp) = @_;
2923
2924 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp;
2926
2927 my $v = -1;
2928
2929 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2930 my $l0 = $table->[$lvl - 1];
2931 my $l1 = $table->[$lvl];
2932
2933 $self->{nxt} = $l1;
2934
2935 $v = ($exp - $l0) / ($l1 - $l0);
2936 }
2937
2938 $self->SUPER::set_value ($v);
2939}
2940
2941#############################################################################
2942
2776package CFPlus::UI::Gauge; 2943package DC::UI::Gauge;
2777 2944
2778our @ISA = CFPlus::UI::VBox::; 2945our @ISA = DC::UI::VBox::;
2779 2946
2780sub new { 2947sub new {
2781 my ($class, %arg) = @_; 2948 my ($class, %arg) = @_;
2782 2949
2783 my $self = $class->SUPER::new ( 2950 my $self = $class->SUPER::new (
2785 can_hover => 1, 2952 can_hover => 1,
2786 can_events => 1, 2953 can_events => 1,
2787 %arg, 2954 %arg,
2788 ); 2955 );
2789 2956
2790 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2957 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999");
2791 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2958 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2792 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2959 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999");
2793 2960
2794 $self 2961 $self
2795} 2962}
2796 2963
2797sub set_fontsize { 2964sub set_fontsize {
2818 $self->{value}->set_text ($val); 2985 $self->{value}->set_text ($val);
2819} 2986}
2820 2987
2821############################################################################# 2988#############################################################################
2822 2989
2823package CFPlus::UI::Slider; 2990package DC::UI::Slider;
2824 2991
2825use strict; 2992use strict;
2826 2993
2827use CFPlus::OpenGL; 2994use DC::OpenGL;
2828 2995
2829our @ISA = CFPlus::UI::DrawBG::; 2996our @ISA = DC::UI::DrawBG::;
2830 2997
2831my @tex = 2998my @tex =
2832 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 2999 map { new_from_file DC::Texture DC::find_rcfile $_ }
2833 qw(s1_slider.png s1_slider_bg.png); 3000 qw(s1_slider.png s1_slider_bg.png);
2834 3001
2835sub new { 3002sub new {
2836 my $class = shift; 3003 my $class = shift;
2837 3004
2905 3072
2906 $self->SUPER::invoke_button_down ($ev, $x, $y); 3073 $self->SUPER::invoke_button_down ($ev, $x, $y);
2907 3074
2908 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3075 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2909 3076
2910 $self->invoke_mouse_motion ($ev, $x, $y) 3077 $self->invoke_mouse_motion ($ev, $x, $y);
3078
3079 1
2911} 3080}
2912 3081
2913sub invoke_mouse_motion { 3082sub invoke_mouse_motion {
2914 my ($self, $ev, $x, $y) = @_; 3083 my ($self, $ev, $x, $y) = @_;
2915 3084
2931sub invoke_mouse_wheel { 3100sub invoke_mouse_wheel {
2932 my ($self, $ev) = @_; 3101 my ($self, $ev) = @_;
2933 3102
2934 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2935 3104
2936 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3105 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2937 3106
2938 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2939 3108
2940 ! ! $delta 3109 1
2941} 3110}
2942 3111
2943sub update { 3112sub update {
2944 my ($self) = @_; 3113 my ($self) = @_;
2945 3114
2994 glDisable GL_TEXTURE_2D; 3163 glDisable GL_TEXTURE_2D;
2995} 3164}
2996 3165
2997############################################################################# 3166#############################################################################
2998 3167
2999package CFPlus::UI::ValSlider; 3168package DC::UI::ValSlider;
3000 3169
3001our @ISA = CFPlus::UI::HBox::; 3170our @ISA = DC::UI::HBox::;
3002 3171
3003sub new { 3172sub new {
3004 my ($class, %arg) = @_; 3173 my ($class, %arg) = @_;
3005 3174
3006 my $range = delete $arg{range}; 3175 my $range = delete $arg{range};
3007 3176
3008 my $self = $class->SUPER::new ( 3177 my $self = $class->SUPER::new (
3009 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3178 slider => (new DC::UI::Slider expand => 1, range => $range),
3010 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3179 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3011 to_value => sub { shift }, 3180 to_value => sub { shift },
3012 from_value => sub { shift }, 3181 from_value => sub { shift },
3013 %arg, 3182 %arg,
3014 ); 3183 );
3015 3184
3035sub set_range { shift->{slider}->set_range (@_) } 3204sub set_range { shift->{slider}->set_range (@_) }
3036sub set_value { shift->{slider}->set_value (@_) } 3205sub set_value { shift->{slider}->set_value (@_) }
3037 3206
3038############################################################################# 3207#############################################################################
3039 3208
3040package CFPlus::UI::TextScroller; 3209package DC::UI::TextScroller;
3041 3210
3042our @ISA = CFPlus::UI::HBox::; 3211our @ISA = DC::UI::HBox::;
3043 3212
3044use CFPlus::OpenGL; 3213use DC::OpenGL;
3045 3214
3046sub new { 3215sub new {
3047 my $class = shift; 3216 my $class = shift;
3048 3217
3049 my $self = $class->SUPER::new ( 3218 my $self = $class->SUPER::new (
3051 can_events => 1, 3220 can_events => 1,
3052 indent => 0, 3221 indent => 0,
3053 #font => default_font 3222 #font => default_font
3054 @_, 3223 @_,
3055 3224
3056 layout => (new CFPlus::Layout), 3225 layout => (new DC::Layout),
3057 par => [], 3226 par => [],
3058 max_par => 0, 3227 max_par => 0,
3059 height => 0, 3228 height => 0,
3060 children => [ 3229 children => [
3061 (new CFPlus::UI::Empty expand => 1), 3230 (new DC::UI::Empty expand => 1),
3062 (new CFPlus::UI::Slider vertical => 1), 3231 (new DC::UI::Slider vertical => 1),
3063 ], 3232 ],
3064 ); 3233 );
3065 3234
3066 $self->{children}[1]->connect (changed => sub { $self->update }); 3235 $self->{children}[1]->connect (changed => sub { $self->update });
3067 3236
3076} 3245}
3077 3246
3078sub size_request { 3247sub size_request {
3079 my ($self) = @_; 3248 my ($self) = @_;
3080 3249
3081 my ($empty, $slider) = @{ $self->{children} }; 3250 my ($empty, $slider) = $self->visible_children;
3082 3251
3083 local $self->{children} = [$empty, $slider]; 3252 local $self->{children} = [$empty, $slider];
3084 $self->SUPER::size_request 3253 $self->SUPER::size_request
3085} 3254}
3086 3255
3250 $ROOT->on_post_alloc ($self => sub { 3419 $ROOT->on_post_alloc ($self => sub {
3251 $self->force_uptodate; 3420 $self->force_uptodate;
3252 3421
3253 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3254 3423
3255 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3424 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3256 glClearColor 0, 0, 0, 0; 3425 glClearColor 0, 0, 0, 0;
3257 glClear GL_COLOR_BUFFER_BIT; 3426 glClear GL_COLOR_BUFFER_BIT;
3258 3427
3259 package CFPlus::UI::Base; 3428 package DC::UI::Base;
3260 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3429 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3261 (0, 0, $self->{w}, $self->{h}); 3430 (0, 0, $self->{w}, $self->{h});
3262 3431
3263 my $top = int $self->{children}[1]{range}[0]; 3432 my $top = int $self->{children}[1]{range}[0];
3264 3433
3275 3444
3276 if ($y0 < $y + $h && $y < $y1) { 3445 if ($y0 < $y + $h && $y < $y1) {
3277 my $layout = $self->get_layout ($para); 3446 my $layout = $self->get_layout ($para);
3278 3447
3279 $layout->render ($para->{indent}, $y - $y0); 3448 $layout->render ($para->{indent}, $y - $y0);
3449 $layout->draw;
3280 3450
3281 if (my @w = @{ $para->{widget} }) { 3451 if (my @w = @{ $para->{widget} }) {
3282 my @s = $layout->get_shapes; 3452 my @s = $layout->get_shapes;
3283 3453
3284 for (@w) { 3454 for (@w) {
3322 $self->{children}[1]->draw; 3492 $self->{children}[1]->draw;
3323} 3493}
3324 3494
3325############################################################################# 3495#############################################################################
3326 3496
3327package CFPlus::UI::Animator; 3497package DC::UI::Animator;
3328 3498
3329use CFPlus::OpenGL; 3499use DC::OpenGL;
3330 3500
3331our @ISA = CFPlus::UI::Bin::; 3501our @ISA = DC::UI::Bin::;
3332 3502
3333sub moveto { 3503sub moveto {
3334 my ($self, $x, $y) = @_; 3504 my ($self, $x, $y) = @_;
3335 3505
3336 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3364 glPopMatrix; 3534 glPopMatrix;
3365} 3535}
3366 3536
3367############################################################################# 3537#############################################################################
3368 3538
3369package CFPlus::UI::Flopper; 3539package DC::UI::Flopper;
3370 3540
3371our @ISA = CFPlus::UI::Button::; 3541our @ISA = DC::UI::Button::;
3372 3542
3373sub new { 3543sub new {
3374 my $class = shift; 3544 my $class = shift;
3375 3545
3376 my $self = $class->SUPER::new ( 3546 my $self = $class->SUPER::new (
3388 $self->{other}->toggle_visibility; 3558 $self->{other}->toggle_visibility;
3389} 3559}
3390 3560
3391############################################################################# 3561#############################################################################
3392 3562
3393package CFPlus::UI::Tooltip; 3563package DC::UI::Tooltip;
3394 3564
3395our @ISA = CFPlus::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3396 3566
3397use CFPlus::OpenGL; 3567use DC::OpenGL;
3398 3568
3399sub new { 3569sub new {
3400 my $class = shift; 3570 my $class = shift;
3401 3571
3402 $class->SUPER::new ( 3572 $class->SUPER::new (
3406} 3576}
3407 3577
3408sub set_tooltip_from { 3578sub set_tooltip_from {
3409 my ($self, $widget) = @_; 3579 my ($self, $widget) = @_;
3410 3580
3411 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3412 if $widget->{tooltip} =~ /^#(.*)$/;
3413
3414 my $tooltip = $widget->{tooltip}; 3581 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583
3584 $tip = DC::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/;
3415 3586
3416 if ($ENV{CFPLUS_DEBUG} & 2) { 3587 if ($ENV{CFPLUS_DEBUG} & 2) {
3417 $tooltip .= "\n\n" . (ref $widget) . "\n" 3588 $tip .= "\n\n" . (ref $widget) . "\n"
3418 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3419 . "req $widget->{req_w} $widget->{req_h}\n" 3590 . "req $widget->{req_w} $widget->{req_h}\n"
3420 . "visible $widget->{visible}"; 3591 . "visible $widget->{visible}";
3421 } 3592 }
3422 3593
3423 $tooltip =~ s/^\n+//; 3594 $tip =~ s/^\n+//;
3424 $tooltip =~ s/\n+$//; 3595 $tip =~ s/\n+$//;
3425 3596
3426 $self->add (new CFPlus::UI::Label 3597 $self->add (new DC::UI::Label
3427 markup => $tooltip, 3598 markup => $tip,
3428 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3429 fontsize => 0.8, 3600 fontsize => 0.8,
3430 style => 1, # FLAG_INVERSE 3601 style => 1, # FLAG_INVERSE
3431 ellipsise => 0, 3602 ellipsise => 0,
3432 font => ($widget->{tooltip_font} || $::FONT_PROP), 3603 font => ($widget->{tooltip_font} || $::FONT_PROP),
3470} 3641}
3471 3642
3472sub _draw { 3643sub _draw {
3473 my ($self) = @_; 3644 my ($self) = @_;
3474 3645
3475 glTranslate 0.375, 0.375;
3476
3477 my ($w, $h) = @$self{qw(w h)}; 3646 my ($w, $h) = @$self{qw(w h)};
3478 3647
3479 glColor 1, 0.8, 0.4; 3648 glColor 1, 0.8, 0.4;
3480 glBegin GL_QUADS; 3649 glRect 0, 0, $w, $h;
3481 glVertex 0 , 0;
3482 glVertex 0 , $h;
3483 glVertex $w, $h;
3484 glVertex $w, 0;
3485 glEnd;
3486 3650
3487 glColor 0, 0, 0; 3651 glColor 0, 0, 0;
3488 glBegin GL_LINE_LOOP; 3652 glRect_lineloop .5, .5, $w + .5, $h + .5;
3489 glVertex 0 , 0;
3490 glVertex 0 , $h;
3491 glVertex $w, $h;
3492 glVertex $w, 0;
3493 glEnd;
3494 3653
3495 glTranslate 2 - 0.375, 2 - 0.375; 3654 glTranslate 2, 2;
3496 3655
3497 $self->SUPER::_draw; 3656 $self->SUPER::_draw;
3498} 3657}
3499 3658
3500############################################################################# 3659#############################################################################
3501 3660
3502package CFPlus::UI::Face; 3661package DC::UI::Face;
3503 3662
3504our @ISA = CFPlus::UI::DrawBG::; 3663our @ISA = DC::UI::DrawBG::;
3505 3664
3506use CFPlus::OpenGL; 3665use DC::OpenGL;
3507 3666
3508sub new { 3667sub new {
3509 my $class = shift; 3668 my $class = shift;
3510 3669
3511 my $self = $class->SUPER::new ( 3670 my $self = $class->SUPER::new (
3515 can_events => 0, 3674 can_events => 0,
3516 @_, 3675 @_,
3517 ); 3676 );
3518 3677
3519 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3520 CFPlus::weaken (my $widget = $self); 3679 DC::weaken (my $widget = $self);
3521 3680
3522 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3523 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3524 $self->{timer} = Event->timer (
3525 parked => 1,
3526 cb => sub {
3527 return unless $::CONN && $widget; 3683 return unless $::CONN;
3528 3684
3685 my $w = $widget
3686 or return;
3687
3529 ++$widget->{frame}; 3688 ++$w->{frame};
3530 $widget->update_face; 3689 $w->update_face;
3690
3691 # somehow, $widget can go away
3531 $widget->update; 3692 $w->update;
3532
3533 $widget->update_timer; 3693 $w->update_timer;
3534 },
3535 ); 3694 };
3536 3695
3537 $self->update_face; 3696 $self->update_face;
3538 $self->update_timer; 3697 $self->update_timer;
3539 } 3698 }
3540 3699
3545 my ($self) = @_; 3704 my ($self) = @_;
3546 3705
3547 return unless $self->{timer}; 3706 return unless $self->{timer};
3548 3707
3549 if ($self->{visible}) { 3708 if ($self->{visible}) {
3550 $self->{timer}->at (
3551 $self->{anim_start}
3552 + $self->{animspeed}
3553 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3554 );
3555 $self->{timer}->start; 3709 $self->{timer}->start;
3556 } else { 3710 } else {
3557 $self->{timer}->stop; 3711 $self->{timer}->stop;
3558 } 3712 }
3559} 3713}
3560 3714
3561sub update_face { 3715sub update_face {
3562 my ($self) = @_; 3716 my ($self) = @_;
3563 3717
3564 return unless $::CONN;
3565
3566 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3567 if ($anim && @$anim) {
3568 delete $self->{wait_face};
3569 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3570 }
3571 }
3572}
3573
3574sub size_request {
3575 my ($self) = @_;
3576
3577 if ($::CONN) { 3718 if ($::CONN) {
3578 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3719 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3579 if (my $tex = $::CONN->{texture}[$faceid]) { 3720 if ($anim && @$anim) {
3580 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3721 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3581 } else { 3722 delete $self->{face_change_cb};
3582 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3723
3583 $self->realloc; 3724 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3725 unless ($tex->{name} || $tex->{loading}) {
3726 $tex->upload (sub { $self->reconfigure });
3727 }
3584 }); 3728 }
3585 } 3729 }
3586 } 3730 }
3587 } 3731 }
3732}
3733
3734sub size_request {
3735 my ($self) = @_;
3736
3737 if ($::CONN) {
3738 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3739 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3740 if ($tex->{name}) {
3741 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3742 } elsif (!$tex->{loading}) {
3743 $tex->upload (sub { $self->reconfigure });
3744 }
3745 }
3746
3747 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3748 }
3749 }
3588 3750
3589 ($self->{size_w} || 8, $self->{size_h} || 8) 3751 ($self->{size_w} || 8, $self->{size_h} || 8)
3590} 3752}
3591 3753
3592sub update { 3754sub update {
3606} 3768}
3607 3769
3608sub _draw { 3770sub _draw {
3609 my ($self) = @_; 3771 my ($self) = @_;
3610 3772
3611 return unless $::CONN;
3612
3613 $self->SUPER::_draw; 3773 $self->SUPER::_draw;
3614 3774
3615 my $faceid = $::CONN->{faceid}[$self->{face}] 3775 if (my $tex = $self->{tex}) {
3616 or return;
3617
3618 my $tex = $::CONN->{texture}[$faceid];
3619
3620 if ($tex) {
3621 glEnable GL_TEXTURE_2D; 3776 glEnable GL_TEXTURE_2D;
3622 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3777 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3623 glColor 0, 0, 0, 1; 3778 glColor 0, 0, 0, 1;
3624 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3779 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3625 glDisable GL_TEXTURE_2D; 3780 glDisable GL_TEXTURE_2D;
3635 $self->SUPER::destroy; 3790 $self->SUPER::destroy;
3636} 3791}
3637 3792
3638############################################################################# 3793#############################################################################
3639 3794
3640package CFPlus::UI::Buttonbar; 3795package DC::UI::Buttonbar;
3641 3796
3642our @ISA = CFPlus::UI::HBox::; 3797our @ISA = DC::UI::HBox::;
3643 3798
3644# TODO: should actually wrap buttons and other goodies. 3799# TODO: should actually wrap buttons and other goodies.
3645 3800
3646############################################################################# 3801#############################################################################
3647 3802
3648package CFPlus::UI::Menu; 3803package DC::UI::Menu;
3649 3804
3650our @ISA = CFPlus::UI::Toplevel::; 3805our @ISA = DC::UI::Toplevel::;
3651 3806
3652use CFPlus::OpenGL; 3807use DC::OpenGL;
3653 3808
3654sub new { 3809sub new {
3655 my $class = shift; 3810 my $class = shift;
3656 3811
3657 my $self = $class->SUPER::new ( 3812 my $self = $class->SUPER::new (
3658 items => [], 3813 items => [],
3659 z => 100, 3814 z => 100,
3660 @_, 3815 @_,
3661 ); 3816 );
3662 3817
3663 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3818 $self->add ($self->{vbox} = new DC::UI::VBox);
3664 3819
3665 for my $item (@{ $self->{items} }) { 3820 for my $item (@{ $self->{items} }) {
3666 my ($widget, $cb, $tooltip) = @$item; 3821 my ($widget, $cb, $tooltip) = @$item;
3667 3822
3668 # handle various types of items, only text for now 3823 # handle various types of items, only text for now
3669 if (!ref $widget) { 3824 if (!ref $widget) {
3670 if ($widget =~ /\t/) { 3825 if ($widget =~ /\t/) {
3671 my ($left, $right) = split /\t/, $widget, 2; 3826 my ($left, $right) = split /\t/, $widget, 2;
3672 3827
3673 $widget = new CFPlus::UI::HBox 3828 $widget = new DC::UI::HBox
3674 can_hover => 1, 3829 can_hover => 1,
3675 can_events => 1, 3830 can_events => 1,
3676 tooltip => $tooltip, 3831 tooltip => $tooltip,
3677 children => [ 3832 children => [
3678 (new CFPlus::UI::Label markup => $left, expand => 1), 3833 (new DC::UI::Label markup => $left, expand => 1),
3679 (new CFPlus::UI::Label markup => $right, align => +1), 3834 (new DC::UI::Label markup => $right, align => +1),
3680 ], 3835 ],
3681 ; 3836 ;
3682 3837
3683 } else { 3838 } else {
3684 $widget = new CFPlus::UI::Label 3839 $widget = new DC::UI::Label
3685 can_hover => 1, 3840 can_hover => 1,
3686 can_events => 1, 3841 can_events => 1,
3687 markup => $widget, 3842 markup => $widget,
3688 tooltip => $tooltip; 3843 tooltip => $tooltip;
3689 } 3844 }
3737 1 3892 1
3738} 3893}
3739 3894
3740############################################################################# 3895#############################################################################
3741 3896
3742package CFPlus::UI::Multiplexer; 3897package DC::UI::Multiplexer;
3743 3898
3744our @ISA = CFPlus::UI::Container::; 3899our @ISA = DC::UI::Container::;
3745 3900
3746sub new { 3901sub new {
3747 my $class = shift; 3902 my $class = shift;
3748 3903
3749 my $self = $class->SUPER::new ( 3904 my $self = $class->SUPER::new (
3810 $self->{current}->draw; 3965 $self->{current}->draw;
3811} 3966}
3812 3967
3813############################################################################# 3968#############################################################################
3814 3969
3815package CFPlus::UI::Notebook; 3970package DC::UI::Notebook;
3816 3971
3972use DC::OpenGL;
3973
3817our @ISA = CFPlus::UI::VBox::; 3974our @ISA = DC::UI::VBox::;
3818 3975
3819sub new { 3976sub new {
3820 my $class = shift; 3977 my $class = shift;
3821 3978
3822 my $self = $class->SUPER::new ( 3979 my $self = $class->SUPER::new (
3823 buttonbar => (new CFPlus::UI::Buttonbar), 3980 buttonbar => (new DC::UI::Buttonbar),
3824 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3981 multiplexer => (new DC::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2],
3825 # filter => # will be put between multiplexer and $self 3983 # filter => # will be put between multiplexer and $self
3826 @_, 3984 @_,
3827 ); 3985 );
3828 3986
3829 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3987 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3830 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3988 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3831 3989
3990 {
3991 Scalar::Util::weaken (my $wself = $self);
3992
3993 $self->{multiplexer}->connect (c_add => sub {
3994 my ($mplex, $widgets) = @_;
3995
3996 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do {
3999 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4001 ? $child->{c_tab}
4002 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003
4004 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child);
4006 });
4007
4008 $tab
4009 };
4010
4011 $self->{buttonbar}->add ($child->{c_tab_});
4012 }
4013 });
4014
4015 $self->{multiplexer}->connect (c_remove => sub {
4016 my ($mplex, $widgets) = @_;
4017
4018 for my $child (@$widgets) {
4019 $wself->{buttonbar}->remove ($child->{c_tab_});
4020 }
4021 });
4022 }
4023
3832 $self 4024 $self
3833} 4025}
3834 4026
3835sub add { 4027sub add {
4028 my ($self, @widgets) = @_;
4029
4030 $self->{multiplexer}->add (@widgets)
4031}
4032
4033sub remove {
4034 my ($self, @widgets) = @_;
4035
4036 $self->{multiplexer}->remove (@widgets)
4037}
4038
4039sub pages {
4040 my ($self) = @_;
4041 $self->{multiplexer}->children
4042}
4043
4044sub add_tab {
3836 my ($self, $title, $widget, $tooltip) = @_; 4045 my ($self, $title, $widget, $tooltip) = @_;
3837 4046
3838 CFPlus::weaken $self; 4047 $title = [$title, $tooltip] unless ref $title;
4048 $widget->{c_tab} = $title;
3839 4049
3840 $self->{buttonbar}->add (new CFPlus::UI::Button
3841 markup => $title,
3842 tooltip => $tooltip,
3843 on_activate => sub { $self->set_current_page ($widget) },
3844 );
3845
3846 $self->{multiplexer}->add ($widget); 4050 $self->add ($widget);
3847} 4051}
3848 4052
3849sub get_current_page { 4053sub get_current_page {
3850 my ($self) = @_; 4054 my ($self) = @_;
3851 4055
3857 4061
3858 $self->{multiplexer}->set_current_page ($page); 4062 $self->{multiplexer}->set_current_page ($page);
3859 $self->emit (page_changed => $self->{multiplexer}{current}); 4063 $self->emit (page_changed => $self->{multiplexer}{current});
3860} 4064}
3861 4065
4066sub _draw {
4067 my ($self) = @_;
4068
4069 $self->SUPER::_draw ();
4070
4071 if (my $cur = $self->{multiplexer}{current}) {
4072 if ($cur = $cur->{c_tab_}) {
4073 glTranslate $self->{buttonbar}{x} + $cur->{x},
4074 $self->{buttonbar}{y} + $cur->{y};
4075 glLineWidth 3;
4076 #glEnable GL_BLEND;
4077 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4078 glColor @{$self->{active_outline}};
4079 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4080 glLineWidth 1;
4081 #glDisable GL_BLEND;
4082 }
4083 }
4084}
4085
3862############################################################################# 4086#############################################################################
3863 4087
3864package CFPlus::UI::Selector; 4088package DC::UI::Selector;
3865 4089
3866use utf8; 4090use utf8;
3867 4091
3868our @ISA = CFPlus::UI::Button::; 4092our @ISA = DC::UI::Button::;
3869 4093
3870sub new { 4094sub new {
3871 my $class = shift; 4095 my $class = shift;
3872 4096
3873 my $self = $class->SUPER::new ( 4097 my $self = $class->SUPER::new (
3890 my ($value, $title, $tooltip) = @$_; 4114 my ($value, $title, $tooltip) = @$_;
3891 4115
3892 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3893 } 4117 }
3894 4118
3895 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4119 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3896} 4120}
3897 4121
3898sub _set_value { 4122sub _set_value {
3899 my ($self, $value) = @_; 4123 my ($self, $value) = @_;
3900 4124
3901 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4125 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4126 $item ||= $self->{options}[0]
3902 or return; 4127 or return;
3903 4128
3904 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
3905 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
3906 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
3907} 4132}
3908 4133
3909sub set_value { 4134sub set_value {
3910 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
3911 4136
3913 4138
3914 $self->_set_value ($value); 4139 $self->_set_value ($value);
3915 $self->emit (changed => $value); 4140 $self->emit (changed => $value);
3916} 4141}
3917 4142
4143sub set_options {
4144 my ($self, $options) = @_;
4145
4146 $self->{options} = $options;
4147 $self->_set_value ($self->{value});
4148}
4149
3918############################################################################# 4150#############################################################################
3919 4151
3920package CFPlus::UI::Statusbox; 4152package DC::UI::Statusbox;
3921 4153
3922our @ISA = CFPlus::UI::VBox::; 4154our @ISA = DC::UI::VBox::;
3923 4155
3924sub new { 4156sub new {
3925 my $class = shift; 4157 my $class = shift;
3926 4158
3927 my $self = $class->SUPER::new ( 4159 my $self = $class->SUPER::new (
3928 fontsize => 0.8, 4160 fontsize => 0.8,
3929 @_, 4161 @_,
3930 ); 4162 );
3931 4163
3932 CFPlus::weaken (my $this = $self); 4164 DC::weaken (my $this = $self);
3933 4165
3934 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3935 4167
3936 $self 4168 $self
3937} 4169}
3938 4170
3939sub reorder { 4171sub reorder {
3940 my ($self) = @_; 4172 my ($self) = @_;
3941 my $NOW = Time::HiRes::time; 4173 my $NOW = Time::HiRes::time;
3942 4174
3943 # freeze display when hovering over any label 4175 # freeze display when hovering over any label
3944 return if $CFPlus::UI::TOOLTIP->{owner} 4176 return if $DC::UI::TOOLTIP->{owner}
3945 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4177 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3946 values %{ $self->{item} }; 4178 values %{ $self->{item} };
3947 4179
3948 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
3949 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3950 } 4182 }
4183
4184 $self->{timer}->set (1, 1);
3951 4185
3952 my @widgets; 4186 my @widgets;
3953 4187
3954 my @items = sort { 4188 my @items = sort {
3955 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
3956 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
3957 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
3958
3959 $self->{timer}->interval (1);
3960 4192
3961 my $count = 10 + 1; 4193 my $count = 10 + 1;
3962 for my $item (@items) { 4194 for my $item (@items) {
3963 last unless --$count; 4195 last unless --$count;
3964 4196
3971 for ($short) { 4203 for ($short) {
3972 s/^\s+//; 4204 s/^\s+//;
3973 s/\s+/ /g; 4205 s/\s+/ /g;
3974 } 4206 }
3975 4207
3976 new CFPlus::UI::Label 4208 new DC::UI::Label
3977 markup => $short, 4209 markup => $short,
3978 tooltip => $item->{tooltip}, 4210 tooltip => $item->{tooltip},
3979 tooltip_font => $::FONT_PROP, 4211 tooltip_font => $::FONT_PROP,
3980 tooltip_width => 0.67, 4212 tooltip_width => 0.67,
3981 fontsize => $item->{fontsize} || $self->{fontsize}, 4213 fontsize => $item->{fontsize} || $self->{fontsize},
3988 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3989 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3990 $label->update; 4222 $label->update;
3991 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
3992 if $diff < 1; 4224 if $diff < 1;
3993 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
3994 } else { 4226 } else {
3995 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
3996 } 4228 }
3997 4229
3998 push @widgets, $label; 4230 push @widgets, $label;
4060 $self->SUPER::destroy; 4292 $self->SUPER::destroy;
4061} 4293}
4062 4294
4063############################################################################# 4295#############################################################################
4064 4296
4065package CFPlus::UI::Root; 4297package DC::UI::Root;
4066 4298
4067our @ISA = CFPlus::UI::Container::; 4299our @ISA = DC::UI::Container::;
4068 4300
4069use List::Util qw(min max); 4301use List::Util qw(min max);
4070 4302
4071use CFPlus::OpenGL; 4303use DC::OpenGL;
4072 4304
4073sub new { 4305sub new {
4074 my $class = shift; 4306 my $class = shift;
4075 4307
4076 my $self = $class->SUPER::new ( 4308 my $self = $class->SUPER::new (
4077 visible => 1, 4309 visible => 1,
4078 @_, 4310 @_,
4079 ); 4311 );
4080 4312
4081 CFPlus::weaken ($self->{root} = $self); 4313 DC::weaken ($self->{root} = $self);
4082 4314
4083 $self 4315 $self
4084} 4316}
4085 4317
4086sub size_request { 4318sub size_request {
4134} 4366}
4135 4367
4136sub update { 4368sub update {
4137 my ($self) = @_; 4369 my ($self) = @_;
4138 4370
4139 $::WANT_REFRESH++; 4371 $::WANT_REFRESH = 1;
4140} 4372}
4141 4373
4142sub add { 4374sub add {
4143 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4144 4376
4181 while ($self->{refresh_hook}) { 4413 while ($self->{refresh_hook}) {
4182 $_->() 4414 $_->()
4183 for values %{delete $self->{refresh_hook}}; 4415 for values %{delete $self->{refresh_hook}};
4184 } 4416 }
4185 4417
4186 if ($self->{realloc}) { 4418 while ($self->{realloc}) {
4187 my %queue; 4419 my %queue;
4188 my @queue; 4420 my @queue;
4189 my $widget; 4421 my $widget;
4190 4422
4191 outer: 4423 outer:
4238 } 4470 }
4239 } 4471 }
4240 4472
4241 delete $self->{realloc}{$widget+0}; 4473 delete $self->{realloc}{$widget+0};
4242 } 4474 }
4243 }
4244 4475
4245 while (my $size_alloc = delete $self->{size_alloc}) { 4476 while (my $size_alloc = delete $self->{size_alloc}) {
4246 my @queue = sort { $b->{visible} <=> $a->{visible} } 4477 my @queue = sort { $a->{visible} <=> $b->{visible} }
4247 values %$size_alloc; 4478 values %$size_alloc;
4248 4479
4249 while () { 4480 while () {
4250 my $widget = pop @queue || last; 4481 my $widget = pop @queue || last;
4251 4482
4252 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4483 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4253 4484
4254 $w = 0 if $w < 0;
4255 $h = 0 if $h < 0;
4256
4257 $w = max $widget->{min_w}, $w; 4485 $w = max $widget->{min_w}, $w;
4258 $h = max $widget->{min_h}, $h; 4486 $h = max $widget->{min_h}, $h;
4259 4487
4260# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4488# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4261# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4489# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4262 4490
4263 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4491 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4264 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4492 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4265 4493
4266 $w = int $w + 0.5; 4494 $w = int $w + 0.5;
4267 $h = int $h + 0.5; 4495 $h = int $h + 0.5;
4268 4496
4269 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4497 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4270 $widget->{old_w} = $widget->{w}; 4498 $widget->{old_w} = $widget->{w};
4271 $widget->{old_h} = $widget->{h}; 4499 $widget->{old_h} = $widget->{h};
4272 4500
4273 $widget->{w} = $w; 4501 $widget->{w} = $w;
4274 $widget->{h} = $h; 4502 $widget->{h} = $h;
4275 4503
4276 $widget->emit (size_allocate => $w, $h); 4504 $widget->emit (size_allocate => $w, $h);
4505 }
4277 } 4506 }
4278 } 4507 }
4279 } 4508 }
4280 4509
4281 while ($self->{post_alloc_hook}) { 4510 while ($self->{post_alloc_hook}) {
4292 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4293 glMatrixMode GL_MODELVIEW; 4522 glMatrixMode GL_MODELVIEW;
4294 glLoadIdentity; 4523 glLoadIdentity;
4295 4524
4296 { 4525 {
4297 package CFPlus::UI::Base; 4526 package DC::UI::Base;
4298 4527
4299 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4528 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4300 (0, 0, $self->{w}, $self->{h}); 4529 (0, 0, $self->{w}, $self->{h});
4301 4530
4302 $self->_draw; 4531 $self->_draw;
4303 } 4532 }
4304} 4533}
4305 4534
4306############################################################################# 4535#############################################################################
4307 4536
4308package CFPlus::UI; 4537package DC::UI;
4309 4538
4310$ROOT = new CFPlus::UI::Root; 4539$ROOT = new DC::UI::Root;
4311$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4540$TOOLTIP = new DC::UI::Tooltip z => 900;
4312 4541
43131 45421
4314 4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines