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.412 by root, Sun Jul 22 20:08:17 2007 UTC vs.
Revision 1.458 by root, Fri Dec 28 15:05:33 2007 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 33
33 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
34 35
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 37 $TOOLTIP->show;
41 } 38 }
42 39
43 return; 40 return;
46 } 43 }
47 44
48 $TOOLTIP->hide; 45 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
51}); 48};
52 49
53sub get_layout { 50sub get_layout {
54 my $layout; 51 my $layout;
55 52
56 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
92 89
93 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 92
96 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
97 } 94 }
98} 95}
99 96
100sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
101 my ($ev) = @_; 98 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
108 105
109 $GRAB = $widget; 106 $GRAB = $widget;
110 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
111 108
112 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
113 } 110 }
114 111
115 if ($GRAB) { 112 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 114 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 117
121 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
123 120
124 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 140 $grab->update if $grab;
144 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
145 142
146 check_hover $widget; 143 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
152 my ($ev) = @_; 149 my ($ev) = @_;
214 reconfigure_widgets; 211 reconfigure_widgets;
215} 212}
216 213
217############################################################################# 214#############################################################################
218 215
219package CFPlus::UI::Event; 216package DC::UI::Event;
220 217
221sub xy { 218sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 220}
224 221
225############################################################################# 222#############################################################################
226 223
227package CFPlus::UI::Base; 224package DC::UI::Base;
228 225
229use strict; 226use strict;
230 227
231use CFPlus::OpenGL; 228use DC::OpenGL;
232 229
233sub new { 230sub new {
234 my $class = shift; 231 my $class = shift;
235 232
236 my $self = bless { 233 my $self = bless {
241 h => undef, 238 h => undef,
242 can_events => 1, 239 can_events => 1,
243 @_ 240 @_
244 }, $class; 241 }, $class;
245 242
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 244
248 for (keys %$self) { 245 for (keys %$self) {
249 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
251 } 248 }
252 } 249 }
253 250
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
256 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
257 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
258 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
259 256
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 259
263 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
273 $self->emit ("destroy"); 270 $self->emit ("destroy");
274 %$self = (); 271 %$self = ();
275} 272}
276 273
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub show {
282 my ($self) = @_; 279 my ($self) = @_;
283 280
284 return if $self->{parent}; 281 return if $self->{parent};
285 282
286 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
287} 284}
288 285
289sub set_visible { 286sub set_visible {
290 my ($self) = @_; 287 my ($self) = @_;
291 288
312 delete $self->{root}; 309 delete $self->{root};
313 310
314 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
316 313
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
319 316
320 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
322} 319}
341sub hide { 338sub hide {
342 my ($self) = @_; 339 my ($self) = @_;
343 340
344 $self->set_invisible; 341 $self->set_invisible;
345 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 346 $parent->remove ($self);
347 if $self->{parent}; 347 }
348} 348}
349 349
350sub move_abs { 350sub move_abs {
351 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
352 352
364 $self->{force_h} = $h; 364 $self->{force_h} = $h;
365 365
366 $self->realloc; 366 $self->realloc;
367} 367}
368 368
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
377}
378
369sub size_request { 379sub size_request {
370 require Carp; 380 require Carp;
371 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
372} 382}
373 383
379 my ($self, $x, $y, $w, $h) = @_; 389 my ($self, $x, $y, $w, $h) = @_;
380 390
381 if ($self->{aspect}) { 391 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 392 my ($ow, $oh) = ($w, $h);
383 393
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 394 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 395 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 396
387 # use alignment to adjust x, y 397 # use alignment to adjust x, y
388 398
389 $x += int 0.5 * ($ow - $w); 399 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 400 $y += int 0.5 * ($oh - $h);
431 441
432 return if $self->{tooltip} eq $tooltip; 442 return if $self->{tooltip} eq $tooltip;
433 443
434 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
435 445
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 449 }
440} 450}
441 451
442# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
443sub coord2local { 453sub coord2local {
444 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
445 455
446 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
447 457
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 459}
450 460
451# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
452sub coord2global { 462sub coord2global {
453 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
454 464
455 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
456 466
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 468}
459 469
460sub invoke_focus_in { 470sub invoke_focus_in {
509sub connect { 519sub connect {
510 my ($self, $signal, $cb) = @_; 520 my ($self, $signal, $cb) = @_;
511 521
512 push @{ $self->{signal_cb}{$signal} }, $cb; 522 push @{ $self->{signal_cb}{$signal} }, $cb;
513 523
514 defined wantarray and CFPlus::guard { 524 defined wantarray and DC::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 526 @{ $self->{signal_cb}{$signal} };
517 } 527 }
518} 528}
519 529
557# in .xs 567# in .xs
558 568
559sub set_parent { 569sub set_parent {
560 my ($self, $parent) = @_; 570 my ($self, $parent) = @_;
561 571
562 CFPlus::weaken ($self->{parent} = $parent); 572 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 573 $self->set_visible if $parent->{visible};
564} 574}
565 575
566sub realloc { 576sub realloc {
567 my ($self) = @_; 577 my ($self) = @_;
605} 615}
606 616
607sub DESTROY { 617sub DESTROY {
608 my ($self) = @_; 618 my ($self) = @_;
609 619
610 return if CFPlus::in_destruct; 620 return if DC::in_destruct;
611 621
622 local $@;
612 eval { $self->destroy }; 623 eval { $self->destroy };
613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614 625
615 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
616} 627}
617 628
618############################################################################# 629#############################################################################
619 630
620package CFPlus::UI::DrawBG; 631package DC::UI::DrawBG;
621 632
622our @ISA = CFPlus::UI::Base::; 633our @ISA = DC::UI::Base::;
623 634
624use strict; 635use strict;
625use CFPlus::OpenGL; 636use DC::OpenGL;
626 637
627sub new { 638sub new {
628 my $class = shift; 639 my $class = shift;
629
630 # range [value, low, high, page]
631 640
632 $class->SUPER::new ( 641 $class->SUPER::new (
633 #bg => [0, 0, 0, 0.2], 642 #bg => [0, 0, 0, 0.2],
634 #active_bg => [1, 1, 1, 0.5], 643 #active_bg => [1, 1, 1, 0.5],
635 @_ 644 @_
647 my ($w, $h) = @$self{qw(w h)}; 656 my ($w, $h) = @$self{qw(w h)};
648 657
649 glEnable GL_BLEND; 658 glEnable GL_BLEND;
650 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 659 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
651 glColor_premultiply @$color; 660 glColor_premultiply @$color;
652
653 glBegin GL_QUADS;
654 glVertex 0 , 0;
655 glVertex 0 , $h;
656 glVertex $w, $h; 661 glRect 0, 0, $w, $h;
657 glVertex $w, 0;
658 glEnd;
659
660 glDisable GL_BLEND; 662 glDisable GL_BLEND;
661 } 663 }
662} 664}
663 665
664############################################################################# 666#############################################################################
665 667
666package CFPlus::UI::Empty; 668package DC::UI::Empty;
667 669
668our @ISA = CFPlus::UI::Base::; 670our @ISA = DC::UI::Base::;
669 671
670sub new { 672sub new {
671 my ($class, %arg) = @_; 673 my ($class, %arg) = @_;
672 $class->SUPER::new (can_events => 0, %arg); 674 $class->SUPER::new (can_events => 0, %arg);
673} 675}
680 682
681sub draw { } 683sub draw { }
682 684
683############################################################################# 685#############################################################################
684 686
685package CFPlus::UI::Container; 687package DC::UI::Container;
686 688
687our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
688 690
689sub new { 691sub new {
690 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
691 693
692 my $children = delete $arg{children}; 694 my $children = delete $arg{children};
725 @{$self->{children}}, @widgets 727 @{$self->{children}}, @widgets
726 ]; 728 ];
727 729
728 $self->realloc; 730 $self->realloc;
729 731
732 $self->emit (c_add => \@widgets);
733
730 map $_+0, @widgets 734 map $_+0, @widgets
731} 735}
732 736
733sub children { 737sub children {
734 @{ $_[0]{children} } 738 @{ $_[0]{children} }
735} 739}
736 740
737sub remove { 741sub remove {
738 my ($self, $child) = @_; 742 my ($self, @widgets) = @_;
739 743
744 $self->emit (c_remove => \@widgets);
745
746 for my $child (@widgets) {
740 delete $child->{parent}; 747 delete $child->{parent};
741 $child->hide; 748 $child->hide;
742
743 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 749 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
750 }
744 751
745 $self->realloc; 752 $self->realloc;
746} 753}
747 754
748sub clear { 755sub clear {
776} 783}
777 784
778sub _draw { 785sub _draw {
779 my ($self) = @_; 786 my ($self) = @_;
780 787
781 $_->draw for @{$self->{children}}; 788 $_->draw for $self->visible_children;
782} 789}
783 790
784############################################################################# 791#############################################################################
785 792
786package CFPlus::UI::Bin; 793package DC::UI::Bin;
787 794
788our @ISA = CFPlus::UI::Container::; 795our @ISA = DC::UI::Container::;
789 796
790sub new { 797sub new {
791 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
792 799
793 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 800 my $child = (delete $arg{child}) || new DC::UI::Empty::;
794 801
795 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
796} 803}
797 804
798sub add { 805sub add {
799 my ($self, $child) = @_; 806 my ($self, $child) = @_;
800 807
801 $self->SUPER::remove ($_) for @{ $self->{children} }; 808 $self->clear;
802 $self->SUPER::add ($child); 809 $self->SUPER::add ($child);
803} 810}
804 811
805sub remove { 812sub remove {
806 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
807 814
808 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
809 816
810 $self->{children} = [new CFPlus::UI::Empty] 817 $self->{children} = [new DC::UI::Empty]
811 unless @{$self->{children}}; 818 unless @{$self->{children}};
812} 819}
813 820
814sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
815 822
824 831
825 1 832 1
826} 833}
827 834
828############################################################################# 835#############################################################################
829
830# back-buffered drawing area 836# back-buffered drawing area
831 837
832package CFPlus::UI::Window; 838package DC::UI::Window;
833 839
834our @ISA = CFPlus::UI::Bin::; 840our @ISA = DC::UI::Bin::;
835 841
836use CFPlus::OpenGL; 842use DC::OpenGL;
837 843
838sub new { 844sub new {
839 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
840 846
841 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
863} 869}
864 870
865sub render_child { 871sub render_child {
866 my ($self) = @_; 872 my ($self) = @_;
867 873
868 $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 {
869 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
870 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
871 877
872 { 878 {
873 package CFPlus::UI::Base; 879 package DC::UI::Base;
874 880
875 local ($draw_x, $draw_y, $draw_w, $draw_h) = 881 local ($draw_x, $draw_y, $draw_w, $draw_h) =
876 (0, 0, $self->{w}, $self->{h}); 882 (0, 0, $self->{w}, $self->{h});
877 883
878 $self->_render; 884 $self->_render;
895 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
896} 902}
897 903
898############################################################################# 904#############################################################################
899 905
900package CFPlus::UI::ViewPort; 906package DC::UI::ViewPort;
901 907
902use List::Util qw(min max); 908use List::Util qw(min max);
903 909
904our @ISA = CFPlus::UI::Window::; 910our @ISA = DC::UI::Window::;
905 911
906sub new { 912sub new {
907 my $class = shift; 913 my $class = shift;
908 914
909 $class->SUPER::new ( 915 $class->SUPER::new (
951 $self->emit (changed => $x, $y); 957 $self->emit (changed => $x, $y);
952 $self->update; 958 $self->update;
953 } 959 }
954} 960}
955 961
962sub set_center {
963 my ($self, $x, $y) = @_;
964
965 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
966}
967
968sub make_visible {
969 my ($self, $x, $y, $border) = @_;
970
971 if ( $x < $self->{view_x} + $self->{w} * $border
972 || $x > $self->{view_x} + $self->{w} * (1 - $border)
973 || $y < $self->{view_y} + $self->{h} * $border
974 || $y > $self->{view_y} + $self->{h} * (1 - $border)
975 ) {
976 $self->set_center ($x, $y);
977 }
978}
979
956# 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
957sub coord2local { 981sub coord2local {
958 my ($self, $x, $y) = @_; 982 my ($self, $x, $y) = @_;
959 983
960 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) 984 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
975 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
976 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
977 ) { 1001 ) {
978 $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})
979 } else { 1003 } else {
980 $self->CFPlus::UI::Base::find_widget ($x, $y) 1004 $self->DC::UI::Base::find_widget ($x, $y)
981 } 1005 }
982} 1006}
983 1007
984sub _render { 1008sub _render {
985 my ($self) = @_; 1009 my ($self) = @_;
986 1010
987 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};
988 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};
989 1013
990 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
991 1015
992 $self->SUPER::_render; 1016 $self->SUPER::_render;
993} 1017}
994 1018
995############################################################################# 1019#############################################################################
996 1020
997package CFPlus::UI::ScrolledWindow; 1021package DC::UI::ScrolledWindow;
998 1022
999our @ISA = CFPlus::UI::Table::; 1023our @ISA = DC::UI::Table::;
1000 1024
1001sub new { 1025sub new {
1002 my ($class, %arg) = @_; 1026 my ($class, %arg) = @_;
1003 1027
1004 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1005 1029
1006 my $self; 1030 my $self;
1007 1031
1008 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new DC::UI::Slider
1009 col => 0, 1033 c_col => 0,
1010 row => 1, 1034 c_row => 1,
1011 vertical => 0, 1035 vertical => 0,
1012 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1013 on_changed => sub { 1037 on_changed => sub {
1014 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1015 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1016 }, 1040 },
1017 ; 1041 ;
1018 1042
1019 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new DC::UI::Slider
1020 col => 1, 1044 c_col => 1,
1021 row => 0, 1045 c_row => 0,
1022 vertical => 1, 1046 vertical => 1,
1023 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1048 on_changed => sub {
1025 $self->{vpos} = $_[1]; 1049 $self->{vpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1036 col_expand => [1, 0], 1060 col_expand => [1, 0],
1037 row_expand => [1, 0], 1061 row_expand => [1, 0],
1038 %arg, 1062 %arg,
1039 ); 1063 );
1040 1064
1041 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new DC::UI::ViewPort
1042 col => 0, 1066 c_col => 0,
1043 row => 0, 1067 c_row => 0,
1044 expand => 1, 1068 expand => 1,
1045 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1046 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1047 on_changed => sub { 1071 on_changed => sub {
1048 my ($vp, $x, $y) = @_; 1072 my ($vp, $x, $y) = @_;
1070 my ($self, $widget) = @_; 1094 my ($self, $widget) = @_;
1071 1095
1072 $self->{vp}->add ($self->{child} = $widget); 1096 $self->{vp}->add ($self->{child} = $widget);
1073} 1097}
1074 1098
1099sub set_offset { shift->{vp}->set_offset (@_) }
1100sub set_center { shift->{vp}->set_center (@_) }
1101sub make_visible { shift->{vp}->make_visible (@_) }
1102
1075sub update_slider { 1103sub update_slider {
1076 my ($self) = @_; 1104 my ($self) = @_;
1077 1105
1078 my $child = ($self->{vp} or return)->child; 1106 my $child = ($self->{vp} or return)->child;
1079 1107
1168 $self->SUPER::invoke_size_allocate ($w, $h) 1196 $self->SUPER::invoke_size_allocate ($w, $h)
1169} 1197}
1170 1198
1171############################################################################# 1199#############################################################################
1172 1200
1173package CFPlus::UI::Frame; 1201package DC::UI::Frame;
1174 1202
1175our @ISA = CFPlus::UI::Bin::; 1203our @ISA = DC::UI::Bin::;
1176 1204
1177use CFPlus::OpenGL; 1205use DC::OpenGL;
1178 1206
1179sub new { 1207sub new {
1180 my $class = shift; 1208 my $class = shift;
1181 1209
1182 $class->SUPER::new ( 1210 $class->SUPER::new (
1192 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
1193 1221
1194 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
1195 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1196 glColor_premultiply @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
1197
1198 glBegin GL_QUADS;
1199 glVertex 0 , 0;
1200 glVertex 0 , $h;
1201 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
1202 glVertex $w, 0;
1203 glEnd;
1204
1205 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
1206 } 1227 }
1207 1228
1208 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1209} 1230}
1210 1231
1211############################################################################# 1232#############################################################################
1212 1233
1213package CFPlus::UI::FancyFrame; 1234package DC::UI::FancyFrame;
1214 1235
1215our @ISA = CFPlus::UI::Bin::; 1236our @ISA = DC::UI::Bin::;
1216 1237
1217use CFPlus::OpenGL; 1238use DC::OpenGL;
1218 1239
1219sub new { 1240sub new {
1220 my ($class, %arg) = @_; 1241 my ($class, %arg) = @_;
1221 1242
1222 if ((exists $arg{label}) && !ref $arg{label}) { 1243 if ((exists $arg{label}) && !ref $arg{label}) {
1223 $arg{label} = new CFPlus::UI::Label 1244 $arg{label} = new DC::UI::Label
1224 align => 1, 1245 align => 1,
1225 valign => 0, 1246 valign => 0,
1226 text => $arg{label}, 1247 text => $arg{label},
1227 fontsize => ($arg{border} || 0.8) * 0.75; 1248 fontsize => ($arg{border} || 0.8) * 0.75;
1228 } 1249 }
1240 1261
1241sub add { 1262sub add {
1242 my ($self, @widgets) = @_; 1263 my ($self, @widgets) = @_;
1243 1264
1244 $self->SUPER::add (@widgets); 1265 $self->SUPER::add (@widgets);
1245 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1266 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1246} 1267}
1247 1268
1248sub border { 1269sub border {
1249 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
1250} 1271}
1308 } 1329 }
1309} 1330}
1310 1331
1311############################################################################# 1332#############################################################################
1312 1333
1313package CFPlus::UI::Toplevel; 1334package DC::UI::Toplevel;
1314 1335
1315our @ISA = CFPlus::UI::Bin::; 1336our @ISA = DC::UI::Bin::;
1316 1337
1317use CFPlus::OpenGL; 1338use DC::OpenGL;
1318 1339
1319my $bg = 1340my $bg =
1320 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1341 new_from_file DC::Texture DC::find_rcfile "d1_bg.png",
1321 mipmap => 1, wrap => 1; 1342 mipmap => 1, wrap => 1;
1322 1343
1323my @border = 1344my @border =
1324 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1345 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1325 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);
1326 1347
1327my @icon = 1348my @icon =
1328 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1349 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1329 qw(x1_move.png x1_resize.png); 1350 qw(x1_move.png x1_resize.png);
1330 1351
1331sub new { 1352sub new {
1332 my ($class, %arg) = @_; 1353 my ($class, %arg) = @_;
1333 1354
1339 min_w => 64, 1360 min_w => 64,
1340 min_h => 32, 1361 min_h => 32,
1341 %arg, 1362 %arg,
1342 ); 1363 );
1343 1364
1344 $self->{title_widget} = new CFPlus::UI::Label 1365 $self->{title_widget} = new DC::UI::Label
1345 align => 0, 1366 align => 0,
1346 valign => 1, 1367 valign => 1,
1347 text => $self->{title}, 1368 text => $self->{title},
1348 fontsize => $self->{border}, 1369 fontsize => $self->{border},
1349 if exists $self->{title}; 1370 if exists $self->{title};
1350 1371
1351 if ($self->{has_close_button}) { 1372 if ($self->{has_close_button}) {
1352 $self->{close_button} = 1373 $self->{close_button} =
1353 new CFPlus::UI::ImageButton 1374 new DC::UI::ImageButton
1354 path => 'x1_close.png', 1375 path => 'x1_close.png',
1355 on_activate => sub { $self->emit ("delete") }; 1376 on_activate => sub { $self->emit ("delete") };
1356 1377
1357 $self->CFPlus::UI::Container::add ($self->{close_button}); 1378 $self->DC::UI::Container::add ($self->{close_button});
1358 } 1379 }
1359 1380
1360 $self 1381 $self
1361} 1382}
1362 1383
1363sub add { 1384sub add {
1364 my ($self, @widgets) = @_; 1385 my ($self, @widgets) = @_;
1365 1386
1366 $self->SUPER::add (@widgets); 1387 $self->SUPER::add (@widgets);
1367 $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};
1368 $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};
1369} 1390}
1370 1391
1371sub border { 1392sub border {
1372 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
1373} 1403}
1374 1404
1375sub size_request { 1405sub size_request {
1376 my ($self) = @_; 1406 my ($self) = @_;
1377 1407
1549 if $self->{close_button}; 1579 if $self->{close_button};
1550} 1580}
1551 1581
1552############################################################################# 1582#############################################################################
1553 1583
1554package CFPlus::UI::Table; 1584package DC::UI::Table;
1555 1585
1556our @ISA = CFPlus::UI::Container::; 1586our @ISA = DC::UI::Container::;
1557 1587
1558use List::Util qw(max sum); 1588use List::Util qw(max sum);
1559 1589
1560use CFPlus::OpenGL; 1590use DC::OpenGL;
1561 1591
1562sub new { 1592sub new {
1563 my $class = shift; 1593 my $class = shift;
1564 1594
1565 $class->SUPER::new ( 1595 $class->SUPER::new (
1571 1601
1572sub add { 1602sub add {
1573 my ($self, @widgets) = @_; 1603 my ($self, @widgets) = @_;
1574 1604
1575 for my $child (@widgets) { 1605 for my $child (@widgets) {
1576 $child->{rowspan} ||= 1; 1606 $child->{c_rowspan} ||= 1;
1577 $child->{colspan} ||= 1; 1607 $child->{c_colspan} ||= 1;
1578 } 1608 }
1579 1609
1580 $self->SUPER::add (@widgets); 1610 $self->SUPER::add (@widgets);
1581} 1611}
1582 1612
1586 my @widgets; 1616 my @widgets;
1587 1617
1588 while (@_) { 1618 while (@_) {
1589 my ($col, $row, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1590 1620
1591 $child->{row} = $row; 1621 $child->{c_row} = $row;
1592 $child->{col} = $col; 1622 $child->{c_col} = $col;
1593 1623
1594 push @widgets, $child; 1624 push @widgets, $child;
1595 } 1625 }
1596 1626
1597 $self->add (@widgets); 1627 $self->add (@widgets);
1603 my (@w, @h); 1633 my (@w, @h);
1604 1634
1605 my @children = $self->children; 1635 my @children = $self->children;
1606 1636
1607 # first pass, columns 1637 # first pass, columns
1608 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1638 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1609 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1610 1640
1611 my $sw = sum @w[$c .. $c + $cs - 1]; 1641 my $sw = sum @w[$c .. $c + $cs - 1];
1612 1642
1613 if ($w > $sw) { 1643 if ($w > $sw) {
1614 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1644 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1615 } 1645 }
1616 } 1646 }
1617 1647
1618 # second pass, rows 1648 # second pass, rows
1619 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1620 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1621 1651
1622 my $sh = sum @h[$r .. $r + $rs - 1]; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1623 1653
1624 if ($h > $sh) { 1654 if ($h > $sh) {
1625 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1655 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1626 } 1656 }
1627 } 1657 }
1628 1658
1629 (\@w, \@h) 1659 (\@w, \@h)
1630} 1660}
1653 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1654 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1655 1685
1656 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1657 1687
1658 CFPlus::UI::harmonize $ws; 1688 DC::UI::harmonize $ws;
1659 1689
1660 my @row_expand = @{$self->{row_expand}}; 1690 my @row_expand = @{$self->{row_expand}};
1661 @row_expand = (1) x @$ws unless @row_expand; 1691 @row_expand = (1) x @$ws unless @row_expand;
1662 my $row_expand = (sum @row_expand) || 1; 1692 my $row_expand = (sum @row_expand) || 1;
1663 1693
1664 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1665 1695
1666 CFPlus::UI::harmonize $hs; 1696 DC::UI::harmonize $hs;
1667 1697
1668 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1669 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1670 1700
1671 for my $widget ($self->children) { 1701 for my $widget ($self->children) {
1672 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1673 1703
1674 $widget->configure ( 1704 $widget->configure (
1675 $x[$c], $y[$r], 1705 $x[$c], $y[$r],
1676 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1677 ); 1707 );
1680 1 1710 1
1681} 1711}
1682 1712
1683############################################################################# 1713#############################################################################
1684 1714
1685package CFPlus::UI::Fixed; 1715package DC::UI::Fixed;
1686 1716
1687use List::Util qw(min max); 1717use List::Util qw(min max);
1688 1718
1689our @ISA = CFPlus::UI::Container::; 1719our @ISA = DC::UI::Container::;
1690 1720
1691sub _scale($$$) { 1721sub _scale($$$) {
1692 my ($rel, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1693 1723
1694 $rel ? $val * $max : $val 1724 $rel ? $val * $max : $val
1746 1 1776 1
1747} 1777}
1748 1778
1749############################################################################# 1779#############################################################################
1750 1780
1751package CFPlus::UI::Box; 1781package DC::UI::Box;
1752 1782
1753our @ISA = CFPlus::UI::Container::; 1783our @ISA = DC::UI::Container::;
1754 1784
1755sub size_request { 1785sub size_request {
1756 my ($self) = @_; 1786 my ($self) = @_;
1787
1788 my @children = $self->visible_children;
1757 1789
1758 $self->{vertical} 1790 $self->{vertical}
1759 ? ( 1791 ? (
1760 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1792 (List::Util::max map $_->{req_w}, @children),
1761 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_h}, @children),
1762 ) 1794 )
1763 : ( 1795 : (
1764 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1796 (List::Util::sum map $_->{req_w}, @children),
1765 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1797 (List::Util::max map $_->{req_h}, @children),
1766 ) 1798 )
1767} 1799}
1768 1800
1769sub invoke_size_allocate { 1801sub invoke_size_allocate {
1770 my ($self, $w, $h) = @_; 1802 my ($self, $w, $h) = @_;
1791 $req[$_] += $space * $children[$_]{expand} 1823 $req[$_] += $space * $children[$_]{expand}
1792 for 0 .. $#children; 1824 for 0 .. $#children;
1793 } 1825 }
1794 } 1826 }
1795 1827
1796 CFPlus::UI::harmonize \@req; 1828 DC::UI::harmonize \@req;
1797 1829
1798 my $pos = 0; 1830 my $pos = 0;
1799 for (0 .. $#children) { 1831 for (0 .. $#children) {
1800 my $alloc = $req[$_]; 1832 my $alloc = $req[$_];
1801 $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));
1806 1 1838 1
1807} 1839}
1808 1840
1809############################################################################# 1841#############################################################################
1810 1842
1811package CFPlus::UI::HBox; 1843package DC::UI::HBox;
1812 1844
1813our @ISA = CFPlus::UI::Box::; 1845our @ISA = DC::UI::Box::;
1814 1846
1815sub new { 1847sub new {
1816 my $class = shift; 1848 my $class = shift;
1817 1849
1818 $class->SUPER::new ( 1850 $class->SUPER::new (
1821 ) 1853 )
1822} 1854}
1823 1855
1824############################################################################# 1856#############################################################################
1825 1857
1826package CFPlus::UI::VBox; 1858package DC::UI::VBox;
1827 1859
1828our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1829 1861
1830sub new { 1862sub new {
1831 my $class = shift; 1863 my $class = shift;
1832 1864
1833 $class->SUPER::new ( 1865 $class->SUPER::new (
1836 ) 1868 )
1837} 1869}
1838 1870
1839############################################################################# 1871#############################################################################
1840 1872
1841package CFPlus::UI::Label; 1873package DC::UI::Label;
1842 1874
1843our @ISA = CFPlus::UI::DrawBG::; 1875our @ISA = DC::UI::DrawBG::;
1844 1876
1845use CFPlus::OpenGL; 1877use DC::OpenGL;
1846 1878
1847sub new { 1879sub new {
1848 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1849 1881
1850 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1855 #text => initial text 1887 #text => initial text
1856 #markup => initial narkup 1888 #markup => initial narkup
1857 #max_w => maximum pixel width 1889 #max_w => maximum pixel width
1858 #style => 0, # render flags 1890 #style => 0, # render flags
1859 ellipsise => 3, # end 1891 ellipsise => 3, # end
1860 layout => (new CFPlus::Layout), 1892 layout => (new DC::Layout),
1861 fontsize => 1, 1893 fontsize => 1,
1862 align => -1, 1894 align => -1,
1863 valign => -1, 1895 valign => -1,
1864 padding_x => 2, 1896 padding_x => 2,
1865 padding_y => 2, 1897 padding_y => 2,
1866 can_events => 0, 1898 can_events => 0,
1867 %arg 1899 %arg
1868 ); 1900 );
1869 1901
1870 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1871 my $layout = new CFPlus::Layout; 1903 my $layout = new DC::Layout;
1872 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1873 $self->{template} = $layout; 1905 $self->{template} = $layout;
1874 } 1906 }
1875 1907
1876 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
1894 1926
1895 delete $self->{ox}; 1927 delete $self->{ox};
1896 $self->SUPER::realloc; 1928 $self->SUPER::realloc;
1897} 1929}
1898 1930
1931sub clear {
1932 my ($self) = @_;
1933
1934 $self->set_text ("");
1935}
1936
1899sub set_text { 1937sub set_text {
1900 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1901 1939
1902 return if $self->{text} eq "T$text"; 1940 return if $self->{text} eq "T$text";
1903 $self->{text} = "T$text"; 1941 $self->{text} = "T$text";
1926 1964
1927sub size_request { 1965sub size_request {
1928 my ($self) = @_; 1966 my ($self) = @_;
1929 1967
1930 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1931 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1932 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1933 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1934 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1935 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1936 1976
1937 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2009 : ($self->{w} - $size->[0]) * 0.5); 2049 : ($self->{w} - $size->[0]) * 0.5);
2010 2050
2011 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2012 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2013 : ($self->{h} - $size->[1]) * 0.5); 2053 : ($self->{h} - $size->[1]) * 0.5);
2054
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2014 }; 2056 };
2015 2057
2016# unless ($self->{list}) { 2058# unless ($self->{list}) {
2017# $self->{list} = CFPlus::OpenGL::glGenList; 2059# $self->{list} = DC::OpenGL::glGenList;
2018# CFPlus::OpenGL::glNewList $self->{list}; 2060# DC::OpenGL::glNewList $self->{list};
2019# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2020# CFPlus::OpenGL::glEndList; 2062# DC::OpenGL::glEndList;
2021# } 2063# }
2022# 2064#
2023# CFPlus::OpenGL::glCallList $self->{list}; 2065# DC::OpenGL::glCallList $self->{list};
2024 2066
2025 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2067 $self->{layout}->draw;
2026} 2068}
2027 2069
2028#sub destroy { 2070#sub destroy {
2029# my ($self) = @_; 2071# my ($self) = @_;
2030# 2072#
2031# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2073# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2032# 2074#
2033# $self->SUPER::destroy; 2075# $self->SUPER::destroy;
2034#} 2076#}
2035 2077
2036############################################################################# 2078#############################################################################
2037 2079
2038package CFPlus::UI::EntryBase; 2080package DC::UI::EntryBase;
2039 2081
2040our @ISA = CFPlus::UI::Label::; 2082our @ISA = DC::UI::Label::;
2041 2083
2042use CFPlus::OpenGL; 2084use DC::OpenGL;
2043 2085
2044sub new { 2086sub new {
2045 my $class = shift; 2087 my $class = shift;
2046 2088
2047 $class->SUPER::new ( 2089 $class->SUPER::new (
2048 fg => [1, 1, 1], 2090 fg => [1, 1, 1],
2049 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1],
2050 active_bg => [1, 1, 1, 0.5], 2093 active_bg => [0, 0, 1, .2],
2051 active_fg => [0, 0, 0], 2094 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0],
2052 can_hover => 1, 2096 can_hover => 1,
2053 can_focus => 1, 2097 can_focus => 1,
2054 valign => 0, 2098 valign => 0,
2055 can_events => 1, 2099 can_events => 1,
2056 ellipsise => 0, 2100 ellipsise => 0,
2101 padding_x => 4,
2102 padding_y => 2,
2057 #text => ... 2103 #text => ...
2058 #hidden => "*", 2104 #hidden => "*",
2059 @_ 2105 @_
2060 ) 2106 )
2061} 2107}
2112 2158
2113 if ($uni == 8) { 2159 if ($uni == 8) {
2114 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2160 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2115 } elsif ($uni == 127) { 2161 } elsif ($uni == 127) {
2116 substr $text, $self->{cursor}, 1, ""; 2162 substr $text, $self->{cursor}, 1, "";
2117 } elsif ($sym == CFPlus::SDLK_LEFT) { 2163 } elsif ($sym == DC::SDLK_LEFT) {
2118 --$self->{cursor} if $self->{cursor}; 2164 --$self->{cursor} if $self->{cursor};
2119 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2165 } elsif ($sym == DC::SDLK_RIGHT) {
2120 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2166 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2121 } elsif ($sym == CFPlus::SDLK_HOME) { 2167 } elsif ($sym == DC::SDLK_HOME) {
2122 # what a hack 2168 # what a hack
2123 $self->{cursor} = 2169 $self->{cursor} =
2124 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2170 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2125 ? length $1 2171 ? length $1
2126 : 0; 2172 : 0;
2127 } elsif ($sym == CFPlus::SDLK_END) { 2173 } elsif ($sym == DC::SDLK_END) {
2128 # uh, again 2174 # uh, again
2129 $self->{cursor} = 2175 $self->{cursor} =
2130 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2176 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2131 ? $self->{cursor} + length $1 2177 ? $self->{cursor} + length $1
2132 : length $self->{text}; 2178 : length $self->{text};
2196 glColor_premultiply @{$self->{bg}}; 2242 glColor_premultiply @{$self->{bg}};
2197 } 2243 }
2198 2244
2199 glEnable GL_BLEND; 2245 glEnable GL_BLEND;
2200 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2246 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2201 glBegin GL_QUADS;
2202 glVertex 0 , 0;
2203 glVertex 0 , $self->{h};
2204 glVertex $self->{w}, $self->{h}; 2247 glRect 0, 0, $self->{w}, $self->{h};
2205 glVertex $self->{w}, 0;
2206 glEnd;
2207 glDisable GL_BLEND; 2248 glDisable GL_BLEND;
2208 2249
2209 $self->SUPER::_draw; 2250 $self->SUPER::_draw;
2210 2251
2211 #TODO: force update every cursor change :( 2252 #TODO: force update every cursor change :(
2213 2254
2214 unless (exists $self->{cur_h}) { 2255 unless (exists $self->{cur_h}) {
2215 my $text = substr $self->{text}, 0, $self->{cursor}; 2256 my $text = substr $self->{text}, 0, $self->{cursor};
2216 utf8::encode $text; 2257 utf8::encode $text;
2217 2258
2218 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2259 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2219 } 2260 }
2220 2261
2262 glColor_premultiply @{$self->{active_fg}};
2221 glBegin GL_LINES; 2263 glBegin GL_LINES;
2222 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2264 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2223 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2265 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2224 glEnd; 2266 glEnd;
2267
2268 glLineWidth 3;
2269 glColor @{$self->{active_outline}};
2270 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2271 glLineWidth 1;
2272
2273 } else {
2274 glColor @{$self->{outline}};
2275 glBegin GL_LINE_STRIP;
2276 glVertex .5, $self->{h} * .5;
2277 glVertex .5, $self->{h} - 2.5;
2278 glVertex $self->{w} - .5, $self->{h} - 2.5;
2279 glVertex $self->{w} - .5, $self->{h} * .5;
2280 glEnd;
2225 } 2281 }
2226} 2282}
2227 2283
2228############################################################################# 2284#############################################################################
2229 2285
2230package CFPlus::UI::Entry; 2286package DC::UI::Entry;
2231 2287
2232our @ISA = CFPlus::UI::EntryBase::; 2288our @ISA = DC::UI::EntryBase::;
2233 2289
2234use CFPlus::OpenGL; 2290use DC::OpenGL;
2235 2291
2236sub invoke_key_down { 2292sub invoke_key_down {
2237 my ($self, $ev) = @_; 2293 my ($self, $ev) = @_;
2238 2294
2239 my $sym = $ev->{sym}; 2295 my $sym = $ev->{sym};
2245 $self->{history_pointer} = -1; 2301 $self->{history_pointer} = -1;
2246 $self->{history_saveback} = ''; 2302 $self->{history_saveback} = '';
2247 $self->emit (activate => $txt); 2303 $self->emit (activate => $txt);
2248 $self->update; 2304 $self->update;
2249 2305
2250 } elsif ($sym == CFPlus::SDLK_UP) { 2306 } elsif ($sym == DC::SDLK_UP) {
2251 if ($self->{history_pointer} < 0) { 2307 if ($self->{history_pointer} < 0) {
2252 $self->{history_saveback} = $self->get_text; 2308 $self->{history_saveback} = $self->get_text;
2253 } 2309 }
2254 if (@{$self->{history} || []} > 0) { 2310 if (@{$self->{history} || []} > 0) {
2255 $self->{history_pointer}++; 2311 $self->{history_pointer}++;
2257 $self->{history_pointer} = @{$self->{history} || []} - 1; 2313 $self->{history_pointer} = @{$self->{history} || []} - 1;
2258 } 2314 }
2259 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2315 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2260 } 2316 }
2261 2317
2262 } elsif ($sym == CFPlus::SDLK_DOWN) { 2318 } elsif ($sym == DC::SDLK_DOWN) {
2263 $self->{history_pointer}--; 2319 $self->{history_pointer}--;
2264 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2320 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2265 2321
2266 if ($self->{history_pointer} >= 0) { 2322 if ($self->{history_pointer} >= 0) {
2267 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2323 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2276 1 2332 1
2277} 2333}
2278 2334
2279############################################################################# 2335#############################################################################
2280 2336
2281package CFPlus::UI::TextEdit; 2337package DC::UI::TextEdit;
2282 2338
2283our @ISA = CFPlus::UI::EntryBase::; 2339our @ISA = DC::UI::EntryBase::;
2284 2340
2285use CFPlus::OpenGL; 2341use DC::OpenGL;
2342
2343sub new {
2344 my $class = shift;
2345
2346 $class->SUPER::new (
2347 padding_y => 4,
2348
2349 @_
2350 )
2351}
2286 2352
2287sub move_cursor_ver { 2353sub move_cursor_ver {
2288 my ($self, $dy) = @_; 2354 my ($self, $dy) = @_;
2289 2355
2290 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2356 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2291 2357
2292 $y += $dy; 2358 $line += $dy;
2293 2359
2294 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2360 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2295 $self->{cursor} = $index; 2361 $self->{cursor} = $index;
2296 delete $self->{cur_h}; 2362 delete $self->{cur_h};
2297 $self->update; 2363 $self->update;
2298 return; 2364 return;
2299 } 2365 }
2302sub invoke_key_down { 2368sub invoke_key_down {
2303 my ($self, $ev) = @_; 2369 my ($self, $ev) = @_;
2304 2370
2305 my $sym = $ev->{sym}; 2371 my $sym = $ev->{sym};
2306 2372
2307 if ($sym == CFPlus::SDLK_UP) { 2373 if ($sym == DC::SDLK_UP) {
2308 $self->move_cursor_ver (-1); 2374 $self->move_cursor_ver (-1);
2309 } elsif ($sym == CFPlus::SDLK_DOWN) { 2375 } elsif ($sym == DC::SDLK_DOWN) {
2310 $self->move_cursor_ver (+1); 2376 $self->move_cursor_ver (+1);
2311 } else { 2377 } else {
2312 return $self->SUPER::invoke_key_down ($ev) 2378 return $self->SUPER::invoke_key_down ($ev)
2313 } 2379 }
2314 2380
2315 1 2381 1
2316} 2382}
2317 2383
2318############################################################################# 2384#############################################################################
2319 2385
2320package CFPlus::UI::Button; 2386package DC::UI::ButtonBin;
2321 2387
2322our @ISA = CFPlus::UI::Label::; 2388our @ISA = DC::UI::Bin::;
2323 2389
2324use CFPlus::OpenGL; 2390use DC::OpenGL;
2325 2391
2326my @tex = 2392my @tex =
2327 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2393 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2328 qw(b1_button_inactive.png b1_button_active.png); 2394 qw(b1_button_inactive.png b1_button_active.png);
2329 2395
2330sub new { 2396sub new {
2331 my $class = shift; 2397 my $class = shift;
2332 2398
2333 $class->SUPER::new ( 2399 $class->SUPER::new (
2400 can_hover => 1,
2401 align => 0,
2402 valign => 0,
2403 can_events => 1,
2404 @_
2405 )
2406}
2407
2408sub invoke_button_up {
2409 my ($self, $ev, $x, $y) = @_;
2410
2411 $self->emit ("activate")
2412 if $x >= 0 && $x < $self->{w}
2413 && $y >= 0 && $y < $self->{h};
2414
2415 1
2416}
2417
2418sub _draw {
2419 my ($self) = @_;
2420
2421 glEnable GL_TEXTURE_2D;
2422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2423 glColor 0, 0, 0, 1;
2424
2425 my $tex = $tex[$GRAB == $self];
2426 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2427
2428 glDisable GL_TEXTURE_2D;
2429
2430 $self->SUPER::_draw;
2431}
2432
2433#############################################################################
2434
2435package DC::UI::Button;
2436
2437our @ISA = DC::UI::Label::;
2438
2439use DC::OpenGL;
2440
2441my @tex =
2442 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2443 qw(b1_button_inactive.png b1_button_active.png);
2444
2445sub new {
2446 my $class = shift;
2447
2448 $class->SUPER::new (
2334 padding_x => 4, 2449 padding_x => 8,
2335 padding_y => 4, 2450 padding_y => 4,
2336 fg => [1.0, 1.0, 1.0], 2451 fg => [1.0, 1.0, 1.0],
2337 active_fg => [0.8, 0.8, 0.8], 2452 active_fg => [0.8, 0.8, 0.8],
2338 can_hover => 1, 2453 can_hover => 1,
2339 align => 0, 2454 align => 0,
2370 $self->SUPER::_draw; 2485 $self->SUPER::_draw;
2371} 2486}
2372 2487
2373############################################################################# 2488#############################################################################
2374 2489
2375package CFPlus::UI::CheckBox; 2490package DC::UI::CheckBox;
2376 2491
2377our @ISA = CFPlus::UI::DrawBG::; 2492our @ISA = DC::UI::DrawBG::;
2378 2493
2379my @tex = 2494my @tex =
2380 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2495 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2381 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2496 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2382 2497
2383use CFPlus::OpenGL; 2498use DC::OpenGL;
2384 2499
2385sub new { 2500sub new {
2386 my $class = shift; 2501 my $class = shift;
2387 2502
2388 $class->SUPER::new ( 2503 $class->SUPER::new (
2428sub _draw { 2543sub _draw {
2429 my ($self) = @_; 2544 my ($self) = @_;
2430 2545
2431 $self->SUPER::_draw; 2546 $self->SUPER::_draw;
2432 2547
2433 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2548 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2434 2549
2435 my ($w, $h) = @$self{qw(w h)}; 2550 my ($w, $h) = @$self{qw(w h)};
2436 2551
2437 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2552 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2438 2553
2445 glDisable GL_TEXTURE_2D; 2560 glDisable GL_TEXTURE_2D;
2446} 2561}
2447 2562
2448############################################################################# 2563#############################################################################
2449 2564
2450package CFPlus::UI::Image; 2565package DC::UI::Image;
2451 2566
2452our @ISA = CFPlus::UI::Base::; 2567our @ISA = DC::UI::Base::;
2453 2568
2454use CFPlus::OpenGL; 2569use DC::OpenGL;
2455 2570
2456our %texture_cache; 2571our %texture_cache;
2457 2572
2458sub new { 2573sub new {
2459 my $class = shift; 2574 my $class = shift;
2460 2575
2461 my $self = $class->SUPER::new ( 2576 my $self = $class->SUPER::new (
2462 can_events => 0, 2577 can_events => 0,
2578 scale => 1,
2463 @_, 2579 @_,
2464 ); 2580 );
2465 2581
2466 $self->{path} || $self->{tex} 2582 $self->{path} || $self->{tex}
2467 or Carp::croak "'path' or 'tex' attributes required"; 2583 or Carp::croak "'path' or 'tex' attributes required";
2468 2584
2469 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2585 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2470 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2586 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1;
2471 2587
2472 CFPlus::weaken $texture_cache{$self->{path}}; 2588 DC::weaken $texture_cache{$self->{path}};
2473 2589
2474 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2590 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2475 2591
2476 $self 2592 $self
2477} 2593}
2478 2594
2479sub STORABLE_freeze { 2595sub STORABLE_freeze {
2480 my ($self, $cloning) = @_; 2596 my ($self, $cloning) = @_;
2481 2597
2482 $self->{path} 2598 $self->{path}
2483 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2599 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2484 2600
2485 $self->{path} 2601 $self->{path}
2486} 2602}
2487 2603
2488sub STORABLE_attach { 2604sub STORABLE_attach {
2492} 2608}
2493 2609
2494sub size_request { 2610sub size_request {
2495 my ($self) = @_; 2611 my ($self) = @_;
2496 2612
2497 ($self->{tex}{w}, $self->{tex}{h}) 2613 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2498} 2614}
2499 2615
2500sub _draw { 2616sub _draw {
2501 my ($self) = @_; 2617 my ($self) = @_;
2502 2618
2512 } 2628 }
2513 2629
2514 glEnable GL_TEXTURE_2D; 2630 glEnable GL_TEXTURE_2D;
2515 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2631 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2516 2632
2517 $tex->draw_quad (0, 0, $w, $h); 2633 $tex->draw_quad_alpha (0, 0, $w, $h);
2518 2634
2519 glDisable GL_TEXTURE_2D; 2635 glDisable GL_TEXTURE_2D;
2520} 2636}
2521 2637
2522############################################################################# 2638#############################################################################
2523 2639
2524package CFPlus::UI::ImageButton; 2640package DC::UI::ImageButton;
2525 2641
2526our @ISA = CFPlus::UI::Image::; 2642our @ISA = DC::UI::Image::;
2527 2643
2528use CFPlus::OpenGL; 2644use DC::OpenGL;
2529 2645
2530my %textures; 2646my %textures;
2531 2647
2532sub new { 2648sub new {
2533 my $class = shift; 2649 my $class = shift;
2543 can_events => 1, 2659 can_events => 1,
2544 @_ 2660 @_
2545 ); 2661 );
2546} 2662}
2547 2663
2664sub invoke_button_down {
2665 my ($self, $ev, $x, $y) = @_;
2666
2667 1
2668}
2669
2548sub invoke_button_up { 2670sub invoke_button_up {
2549 my ($self, $ev, $x, $y) = @_; 2671 my ($self, $ev, $x, $y) = @_;
2550 2672
2551 $self->emit ("activate") 2673 $self->emit ("activate")
2552 if $x >= 0 && $x < $self->{w} 2674 if $x >= 0 && $x < $self->{w}
2555 1 2677 1
2556} 2678}
2557 2679
2558############################################################################# 2680#############################################################################
2559 2681
2560package CFPlus::UI::VGauge; 2682package DC::UI::VGauge;
2561 2683
2562our @ISA = CFPlus::UI::Base::; 2684our @ISA = DC::UI::Base::;
2563 2685
2564use List::Util qw(min max); 2686use List::Util qw(min max);
2565 2687
2566use CFPlus::OpenGL; 2688use DC::OpenGL;
2567 2689
2568my %tex = ( 2690my %tex = (
2569 food => [ 2691 food => [
2570 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2692 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2571 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2693 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2572 ], 2694 ],
2573 grace => [ 2695 grace => [
2574 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2575 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2697 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2576 ], 2698 ],
2577 hp => [ 2699 hp => [
2578 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2579 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2701 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2580 ], 2702 ],
2581 mana => [ 2703 mana => [
2582 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2583 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2705 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2584 ], 2706 ],
2585); 2707);
2586 2708
2587# eg. VGauge->new (gauge => 'food'), default gauge: food 2709# eg. VGauge->new (gauge => 'food'), default gauge: food
2690 glDisable GL_TEXTURE_2D; 2812 glDisable GL_TEXTURE_2D;
2691} 2813}
2692 2814
2693############################################################################# 2815#############################################################################
2694 2816
2817package DC::UI::Progress;
2818
2819our @ISA = DC::UI::Label::;
2820
2821use DC::OpenGL;
2822
2823sub new {
2824 my ($class, %arg) = @_;
2825
2826 my $self = $class->SUPER::new (
2827 fg => [1, 1, 1],
2828 bg => [0, 0, 1, 0.2],
2829 bar => [0.7, 0.5, 0.1, 0.8],
2830 outline => [0.4, 0.3, 0],
2831 fontsize => 0.9,
2832 valign => 0,
2833 align => 0,
2834 can_events => 1,
2835 ellipsise => 1,
2836 label => "%d%%",
2837 %arg,
2838 );
2839
2840 $self->set_value ($arg{value} || -1);
2841
2842 $self
2843}
2844
2845sub set_label {
2846 my ($self, $label) = @_;
2847
2848 return if $self->{label} eq $label;
2849 $self->{label} = $label;
2850
2851 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2852}
2853
2854sub set_value {
2855 my ($self, $value) = @_;
2856
2857 if ($self->{value} ne $value) {
2858 $self->{value} = $value;
2859
2860 if ($value < 0) {
2861 $self->set_text ("-");
2862 } else {
2863 $self->set_text (sprintf $self->{label}, $value * 100);
2864 }
2865
2866 $self->update;
2867 }
2868}
2869
2870sub _draw {
2871 my ($self) = @_;
2872
2873 glEnable GL_BLEND;
2874 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2875
2876 if ($self->{value} >= 0) {
2877 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2878
2879 glColor_premultiply @{$self->{bar}};
2880 glRect 2, 2, $s, $self->{h} - 2;
2881 glColor_premultiply @{$self->{bg}};
2882 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2883 }
2884
2885 glColor_premultiply @{$self->{outline}};
2886 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2887
2888 glDisable GL_BLEND;
2889
2890 {
2891 local $self->{bg}; # do not draw background
2892 $self->SUPER::_draw;
2893 }
2894}
2895
2896#############################################################################
2897
2898package DC::UI::ExperienceProgress;
2899
2900our @ISA = DC::UI::Progress::;
2901
2902sub new {
2903 my ($class, %arg) = @_;
2904
2905 my $self = $class->SUPER::new (
2906 tooltip => sub {
2907 my ($self) = @_;
2908
2909 sprintf "level %d\n%s points\n%s next level\n%s to go",
2910 $self->{lvl},
2911 ::formsep ($self->{exp}),
2912 ::formsep ($self->{nxt}),
2913 ::formsep ($self->{nxt} - $self->{exp}),
2914 },
2915 %arg
2916 );
2917
2918 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2919 if $::CONN;
2920
2921 $self
2922}
2923
2924sub DESTROY {
2925 my ($self) = @_;
2926
2927 delete $::CONN->{on_exp_update}{$self+0}
2928 if $::CONN;
2929
2930 $self->SUPER::DESTROY;
2931}
2932
2933sub set_value {
2934 my ($self, $lvl, $exp) = @_;
2935
2936 $self->{lvl} = $lvl;
2937 $self->{exp} = $exp;
2938
2939 my $v = -1;
2940
2941 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2942 my $l0 = $table->[$lvl - 1];
2943 my $l1 = $table->[$lvl];
2944
2945 $self->{nxt} = $l1;
2946
2947 $v = ($exp - $l0) / ($l1 - $l0);
2948 }
2949
2950 $self->SUPER::set_value ($v);
2951}
2952
2953#############################################################################
2954
2695package CFPlus::UI::Gauge; 2955package DC::UI::Gauge;
2696 2956
2697our @ISA = CFPlus::UI::VBox::; 2957our @ISA = DC::UI::VBox::;
2698 2958
2699sub new { 2959sub new {
2700 my ($class, %arg) = @_; 2960 my ($class, %arg) = @_;
2701 2961
2702 my $self = $class->SUPER::new ( 2962 my $self = $class->SUPER::new (
2704 can_hover => 1, 2964 can_hover => 1,
2705 can_events => 1, 2965 can_events => 1,
2706 %arg, 2966 %arg,
2707 ); 2967 );
2708 2968
2709 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2969 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999");
2710 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2970 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2711 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2971 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999");
2712 2972
2713 $self 2973 $self
2714} 2974}
2715 2975
2716sub set_fontsize { 2976sub set_fontsize {
2737 $self->{value}->set_text ($val); 2997 $self->{value}->set_text ($val);
2738} 2998}
2739 2999
2740############################################################################# 3000#############################################################################
2741 3001
2742package CFPlus::UI::Slider; 3002package DC::UI::Slider;
2743 3003
2744use strict; 3004use strict;
2745 3005
2746use CFPlus::OpenGL; 3006use DC::OpenGL;
2747 3007
2748our @ISA = CFPlus::UI::DrawBG::; 3008our @ISA = DC::UI::DrawBG::;
2749 3009
2750my @tex = 3010my @tex =
2751 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3011 map { new_from_file DC::Texture DC::find_rcfile $_ }
2752 qw(s1_slider.png s1_slider_bg.png); 3012 qw(s1_slider.png s1_slider_bg.png);
2753 3013
2754sub new { 3014sub new {
2755 my $class = shift; 3015 my $class = shift;
2756 3016
2852sub invoke_mouse_wheel { 3112sub invoke_mouse_wheel {
2853 my ($self, $ev) = @_; 3113 my ($self, $ev) = @_;
2854 3114
2855 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3115 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2856 3116
2857 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3117 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2858 3118
2859 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3119 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2860 3120
2861 1 3121 1
2862} 3122}
2915 glDisable GL_TEXTURE_2D; 3175 glDisable GL_TEXTURE_2D;
2916} 3176}
2917 3177
2918############################################################################# 3178#############################################################################
2919 3179
2920package CFPlus::UI::ValSlider; 3180package DC::UI::ValSlider;
2921 3181
2922our @ISA = CFPlus::UI::HBox::; 3182our @ISA = DC::UI::HBox::;
2923 3183
2924sub new { 3184sub new {
2925 my ($class, %arg) = @_; 3185 my ($class, %arg) = @_;
2926 3186
2927 my $range = delete $arg{range}; 3187 my $range = delete $arg{range};
2928 3188
2929 my $self = $class->SUPER::new ( 3189 my $self = $class->SUPER::new (
2930 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3190 slider => (new DC::UI::Slider expand => 1, range => $range),
2931 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3191 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2932 to_value => sub { shift }, 3192 to_value => sub { shift },
2933 from_value => sub { shift }, 3193 from_value => sub { shift },
2934 %arg, 3194 %arg,
2935 ); 3195 );
2936 3196
2956sub set_range { shift->{slider}->set_range (@_) } 3216sub set_range { shift->{slider}->set_range (@_) }
2957sub set_value { shift->{slider}->set_value (@_) } 3217sub set_value { shift->{slider}->set_value (@_) }
2958 3218
2959############################################################################# 3219#############################################################################
2960 3220
2961package CFPlus::UI::TextScroller; 3221package DC::UI::TextScroller;
2962 3222
2963our @ISA = CFPlus::UI::HBox::; 3223our @ISA = DC::UI::HBox::;
2964 3224
2965use CFPlus::OpenGL; 3225use DC::OpenGL;
2966 3226
2967sub new { 3227sub new {
2968 my $class = shift; 3228 my $class = shift;
2969 3229
2970 my $self = $class->SUPER::new ( 3230 my $self = $class->SUPER::new (
2972 can_events => 1, 3232 can_events => 1,
2973 indent => 0, 3233 indent => 0,
2974 #font => default_font 3234 #font => default_font
2975 @_, 3235 @_,
2976 3236
2977 layout => (new CFPlus::Layout), 3237 layout => (new DC::Layout),
2978 par => [], 3238 par => [],
2979 max_par => 0, 3239 max_par => 0,
2980 height => 0, 3240 height => 0,
2981 children => [ 3241 children => [
2982 (new CFPlus::UI::Empty expand => 1), 3242 (new DC::UI::Empty expand => 1),
2983 (new CFPlus::UI::Slider vertical => 1), 3243 (new DC::UI::Slider vertical => 1),
2984 ], 3244 ],
2985 ); 3245 );
2986 3246
2987 $self->{children}[1]->connect (changed => sub { $self->update }); 3247 $self->{children}[1]->connect (changed => sub { $self->update });
2988 3248
2997} 3257}
2998 3258
2999sub size_request { 3259sub size_request {
3000 my ($self) = @_; 3260 my ($self) = @_;
3001 3261
3002 my ($empty, $slider) = @{ $self->{children} }; 3262 my ($empty, $slider) = $self->visible_children;
3003 3263
3004 local $self->{children} = [$empty, $slider]; 3264 local $self->{children} = [$empty, $slider];
3005 $self->SUPER::size_request 3265 $self->SUPER::size_request
3006} 3266}
3007 3267
3171 $ROOT->on_post_alloc ($self => sub { 3431 $ROOT->on_post_alloc ($self => sub {
3172 $self->force_uptodate; 3432 $self->force_uptodate;
3173 3433
3174 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3434 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3175 3435
3176 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3436 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3177 glClearColor 0, 0, 0, 0; 3437 glClearColor 0, 0, 0, 0;
3178 glClear GL_COLOR_BUFFER_BIT; 3438 glClear GL_COLOR_BUFFER_BIT;
3179 3439
3180 package CFPlus::UI::Base; 3440 package DC::UI::Base;
3181 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3441 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3182 (0, 0, $self->{w}, $self->{h}); 3442 (0, 0, $self->{w}, $self->{h});
3183 3443
3184 my $top = int $self->{children}[1]{range}[0]; 3444 my $top = int $self->{children}[1]{range}[0];
3185 3445
3196 3456
3197 if ($y0 < $y + $h && $y < $y1) { 3457 if ($y0 < $y + $h && $y < $y1) {
3198 my $layout = $self->get_layout ($para); 3458 my $layout = $self->get_layout ($para);
3199 3459
3200 $layout->render ($para->{indent}, $y - $y0); 3460 $layout->render ($para->{indent}, $y - $y0);
3461 $layout->draw;
3201 3462
3202 if (my @w = @{ $para->{widget} }) { 3463 if (my @w = @{ $para->{widget} }) {
3203 my @s = $layout->get_shapes; 3464 my @s = $layout->get_shapes;
3204 3465
3205 for (@w) { 3466 for (@w) {
3243 $self->{children}[1]->draw; 3504 $self->{children}[1]->draw;
3244} 3505}
3245 3506
3246############################################################################# 3507#############################################################################
3247 3508
3248package CFPlus::UI::Animator; 3509package DC::UI::Animator;
3249 3510
3250use CFPlus::OpenGL; 3511use DC::OpenGL;
3251 3512
3252our @ISA = CFPlus::UI::Bin::; 3513our @ISA = DC::UI::Bin::;
3253 3514
3254sub moveto { 3515sub moveto {
3255 my ($self, $x, $y) = @_; 3516 my ($self, $x, $y) = @_;
3256 3517
3257 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3518 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3285 glPopMatrix; 3546 glPopMatrix;
3286} 3547}
3287 3548
3288############################################################################# 3549#############################################################################
3289 3550
3290package CFPlus::UI::Flopper; 3551package DC::UI::Flopper;
3291 3552
3292our @ISA = CFPlus::UI::Button::; 3553our @ISA = DC::UI::Button::;
3293 3554
3294sub new { 3555sub new {
3295 my $class = shift; 3556 my $class = shift;
3296 3557
3297 my $self = $class->SUPER::new ( 3558 my $self = $class->SUPER::new (
3309 $self->{other}->toggle_visibility; 3570 $self->{other}->toggle_visibility;
3310} 3571}
3311 3572
3312############################################################################# 3573#############################################################################
3313 3574
3314package CFPlus::UI::Tooltip; 3575package DC::UI::Tooltip;
3315 3576
3316our @ISA = CFPlus::UI::Bin::; 3577our @ISA = DC::UI::Bin::;
3317 3578
3318use CFPlus::OpenGL; 3579use DC::OpenGL;
3319 3580
3320sub new { 3581sub new {
3321 my $class = shift; 3582 my $class = shift;
3322 3583
3323 $class->SUPER::new ( 3584 $class->SUPER::new (
3327} 3588}
3328 3589
3329sub set_tooltip_from { 3590sub set_tooltip_from {
3330 my ($self, $widget) = @_; 3591 my ($self, $widget) = @_;
3331 3592
3332 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3333 if $widget->{tooltip} =~ /^#(.*)$/;
3334
3335 my $tooltip = $widget->{tooltip}; 3593 my $tip = $widget->{tooltip};
3594 $tip = $tip->($widget) if "CODE" eq ref $tip;
3595
3596 $tip = DC::Pod::section_label tooltip => $1
3597 if $tip =~ /^#(.*)$/;
3336 3598
3337 if ($ENV{CFPLUS_DEBUG} & 2) { 3599 if ($ENV{CFPLUS_DEBUG} & 2) {
3338 $tooltip .= "\n\n" . (ref $widget) . "\n" 3600 $tip .= "\n\n" . (ref $widget) . "\n"
3339 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3601 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3340 . "req $widget->{req_w} $widget->{req_h}\n" 3602 . "req $widget->{req_w} $widget->{req_h}\n"
3341 . "visible $widget->{visible}"; 3603 . "visible $widget->{visible}";
3342 } 3604 }
3343 3605
3344 $tooltip =~ s/^\n+//; 3606 $tip =~ s/^\n+//;
3345 $tooltip =~ s/\n+$//; 3607 $tip =~ s/\n+$//;
3346 3608
3347 $self->add (new CFPlus::UI::Label 3609 $self->add (new DC::UI::Label
3348 markup => $tooltip, 3610 markup => $tip,
3349 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3611 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3350 fontsize => 0.8, 3612 fontsize => 0.8,
3351 style => 1, # FLAG_INVERSE 3613 style => 1, # FLAG_INVERSE
3352 ellipsise => 0, 3614 ellipsise => 0,
3353 font => ($widget->{tooltip_font} || $::FONT_PROP), 3615 font => ($widget->{tooltip_font} || $::FONT_PROP),
3391} 3653}
3392 3654
3393sub _draw { 3655sub _draw {
3394 my ($self) = @_; 3656 my ($self) = @_;
3395 3657
3396 glTranslate 0.375, 0.375;
3397
3398 my ($w, $h) = @$self{qw(w h)}; 3658 my ($w, $h) = @$self{qw(w h)};
3399 3659
3400 glColor 1, 0.8, 0.4; 3660 glColor 1, 0.8, 0.4;
3401 glBegin GL_QUADS; 3661 glRect 0, 0, $w, $h;
3402 glVertex 0 , 0;
3403 glVertex 0 , $h;
3404 glVertex $w, $h;
3405 glVertex $w, 0;
3406 glEnd;
3407 3662
3408 glColor 0, 0, 0; 3663 glColor 0, 0, 0;
3409 glBegin GL_LINE_LOOP; 3664 glRect_lineloop .5, .5, $w + .5, $h + .5;
3410 glVertex 0 , 0;
3411 glVertex 0 , $h;
3412 glVertex $w, $h;
3413 glVertex $w, 0;
3414 glEnd;
3415 3665
3416 glTranslate 2 - 0.375, 2 - 0.375; 3666 glTranslate 2, 2;
3417 3667
3418 $self->SUPER::_draw; 3668 $self->SUPER::_draw;
3419} 3669}
3420 3670
3421############################################################################# 3671#############################################################################
3422 3672
3423package CFPlus::UI::Face; 3673package DC::UI::Face;
3424 3674
3425our @ISA = CFPlus::UI::DrawBG::; 3675our @ISA = DC::UI::DrawBG::;
3426 3676
3427use CFPlus::OpenGL; 3677use DC::OpenGL;
3428 3678
3429sub new { 3679sub new {
3430 my $class = shift; 3680 my $class = shift;
3431 3681
3432 my $self = $class->SUPER::new ( 3682 my $self = $class->SUPER::new (
3436 can_events => 0, 3686 can_events => 0,
3437 @_, 3687 @_,
3438 ); 3688 );
3439 3689
3440 if ($self->{anim} && $self->{animspeed}) { 3690 if ($self->{anim} && $self->{animspeed}) {
3441 CFPlus::weaken (my $widget = $self); 3691 DC::weaken (my $widget = $self);
3442 3692
3443 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3693 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3444 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3694 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3445 $self->{timer} = Event->timer (
3446 parked => 1,
3447 cb => sub {
3448 return unless $::CONN && $widget; 3695 return unless $::CONN;
3449 3696
3697 my $w = $widget
3698 or return;
3699
3450 ++$widget->{frame}; 3700 ++$w->{frame};
3451 $widget->update_face; 3701 $w->update_face;
3702
3703 # somehow, $widget can go away
3452 $widget->update; 3704 $w->update;
3453
3454 $widget->update_timer; 3705 $w->update_timer;
3455 },
3456 ); 3706 };
3457 3707
3458 $self->update_face; 3708 $self->update_face;
3459 $self->update_timer; 3709 $self->update_timer;
3460 } 3710 }
3461 3711
3466 my ($self) = @_; 3716 my ($self) = @_;
3467 3717
3468 return unless $self->{timer}; 3718 return unless $self->{timer};
3469 3719
3470 if ($self->{visible}) { 3720 if ($self->{visible}) {
3471 $self->{timer}->at (
3472 $self->{anim_start}
3473 + $self->{animspeed}
3474 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3475 );
3476 $self->{timer}->start; 3721 $self->{timer}->start;
3477 } else { 3722 } else {
3478 $self->{timer}->stop; 3723 $self->{timer}->stop;
3479 } 3724 }
3480} 3725}
3481 3726
3482sub update_face { 3727sub update_face {
3483 my ($self) = @_; 3728 my ($self) = @_;
3484 3729
3485 return unless $::CONN; 3730 if ($::CONN) {
3486
3487 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3731 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3488 if ($anim && @$anim) { 3732 if ($anim && @$anim) {
3489 delete $self->{wait_face};
3490 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3733 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3734 delete $self->{face_change_cb};
3735
3491 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3736 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3737 unless ($tex->{name} || $tex->{loading}) {
3738 $tex->upload (sub { $self->reconfigure });
3739 }
3740 }
3741 }
3492 } 3742 }
3493 } 3743 }
3494} 3744}
3495 3745
3496sub size_request { 3746sub size_request {
3497 my ($self) = @_; 3747 my ($self) = @_;
3498 3748
3499 if ($::CONN) { 3749 if ($::CONN) {
3500 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3750 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3501 if (my $tex = $::CONN->{texture}[$faceid]) { 3751 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3502 $self->{tex} = $tex; 3752 if ($tex->{name}) {
3503 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3753 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3504 } else { 3754 } elsif (!$tex->{loading}) {
3505 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3755 $tex->upload (sub { $self->reconfigure });
3506 $self->realloc;
3507 }); 3756 }
3508 } 3757 }
3758
3759 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3509 } 3760 }
3510 } 3761 }
3511 3762
3512 ($self->{size_w} || 8, $self->{size_h} || 8) 3763 ($self->{size_w} || 8, $self->{size_h} || 8)
3513} 3764}
3551 $self->SUPER::destroy; 3802 $self->SUPER::destroy;
3552} 3803}
3553 3804
3554############################################################################# 3805#############################################################################
3555 3806
3556package CFPlus::UI::Buttonbar; 3807package DC::UI::Buttonbar;
3557 3808
3558our @ISA = CFPlus::UI::HBox::; 3809our @ISA = DC::UI::HBox::;
3559 3810
3560# TODO: should actually wrap buttons and other goodies. 3811# TODO: should actually wrap buttons and other goodies.
3561 3812
3562############################################################################# 3813#############################################################################
3563 3814
3564package CFPlus::UI::Menu; 3815package DC::UI::Menu;
3565 3816
3566our @ISA = CFPlus::UI::Toplevel::; 3817our @ISA = DC::UI::Toplevel::;
3567 3818
3568use CFPlus::OpenGL; 3819use DC::OpenGL;
3569 3820
3570sub new { 3821sub new {
3571 my $class = shift; 3822 my $class = shift;
3572 3823
3573 my $self = $class->SUPER::new ( 3824 my $self = $class->SUPER::new (
3574 items => [], 3825 items => [],
3575 z => 100, 3826 z => 100,
3576 @_, 3827 @_,
3577 ); 3828 );
3578 3829
3579 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3830 $self->add ($self->{vbox} = new DC::UI::VBox);
3580 3831
3581 for my $item (@{ $self->{items} }) { 3832 for my $item (@{ $self->{items} }) {
3582 my ($widget, $cb, $tooltip) = @$item; 3833 my ($widget, $cb, $tooltip) = @$item;
3583 3834
3584 # handle various types of items, only text for now 3835 # handle various types of items, only text for now
3585 if (!ref $widget) { 3836 if (!ref $widget) {
3586 if ($widget =~ /\t/) { 3837 if ($widget =~ /\t/) {
3587 my ($left, $right) = split /\t/, $widget, 2; 3838 my ($left, $right) = split /\t/, $widget, 2;
3588 3839
3589 $widget = new CFPlus::UI::HBox 3840 $widget = new DC::UI::HBox
3590 can_hover => 1, 3841 can_hover => 1,
3591 can_events => 1, 3842 can_events => 1,
3592 tooltip => $tooltip, 3843 tooltip => $tooltip,
3593 children => [ 3844 children => [
3594 (new CFPlus::UI::Label markup => $left, expand => 1), 3845 (new DC::UI::Label markup => $left, expand => 1),
3595 (new CFPlus::UI::Label markup => $right, align => +1), 3846 (new DC::UI::Label markup => $right, align => +1),
3596 ], 3847 ],
3597 ; 3848 ;
3598 3849
3599 } else { 3850 } else {
3600 $widget = new CFPlus::UI::Label 3851 $widget = new DC::UI::Label
3601 can_hover => 1, 3852 can_hover => 1,
3602 can_events => 1, 3853 can_events => 1,
3603 markup => $widget, 3854 markup => $widget,
3604 tooltip => $tooltip; 3855 tooltip => $tooltip;
3605 } 3856 }
3653 1 3904 1
3654} 3905}
3655 3906
3656############################################################################# 3907#############################################################################
3657 3908
3658package CFPlus::UI::Multiplexer; 3909package DC::UI::Multiplexer;
3659 3910
3660our @ISA = CFPlus::UI::Container::; 3911our @ISA = DC::UI::Container::;
3661 3912
3662sub new { 3913sub new {
3663 my $class = shift; 3914 my $class = shift;
3664 3915
3665 my $self = $class->SUPER::new ( 3916 my $self = $class->SUPER::new (
3726 $self->{current}->draw; 3977 $self->{current}->draw;
3727} 3978}
3728 3979
3729############################################################################# 3980#############################################################################
3730 3981
3731package CFPlus::UI::Notebook; 3982package DC::UI::Notebook;
3732 3983
3984use DC::OpenGL;
3985
3733our @ISA = CFPlus::UI::VBox::; 3986our @ISA = DC::UI::VBox::;
3734 3987
3735sub new { 3988sub new {
3736 my $class = shift; 3989 my $class = shift;
3737 3990
3738 my $self = $class->SUPER::new ( 3991 my $self = $class->SUPER::new (
3739 buttonbar => (new CFPlus::UI::Buttonbar), 3992 buttonbar => (new DC::UI::Buttonbar),
3740 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3993 multiplexer => (new DC::UI::Multiplexer expand => 1),
3994 active_outline => [.7, .7, 0.2],
3741 # filter => # will be put between multiplexer and $self 3995 # filter => # will be put between multiplexer and $self
3742 @_, 3996 @_,
3743 ); 3997 );
3744 3998
3745 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3999 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3746 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4000 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3747 4001
4002 {
4003 Scalar::Util::weaken (my $wself = $self);
4004
4005 $self->{multiplexer}->connect (c_add => sub {
4006 my ($mplex, $widgets) = @_;
4007
4008 for my $child (@$widgets) {
4009 Scalar::Util::weaken $child;
4010 $child->{c_tab_} ||= do {
4011 my $tab =
4012 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4013 ? $child->{c_tab}
4014 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4015
4016 $tab->connect (activate => sub {
4017 $wself->set_current_page ($child);
4018 });
4019
4020 $tab
4021 };
4022
4023 $self->{buttonbar}->add ($child->{c_tab_});
4024 }
4025 });
4026
4027 $self->{multiplexer}->connect (c_remove => sub {
4028 my ($mplex, $widgets) = @_;
4029
4030 for my $child (@$widgets) {
4031 $wself->{buttonbar}->remove ($child->{c_tab_});
4032 }
4033 });
4034 }
4035
3748 $self 4036 $self
3749} 4037}
3750 4038
3751sub add { 4039sub add {
4040 my ($self, @widgets) = @_;
4041
4042 $self->{multiplexer}->add (@widgets)
4043}
4044
4045sub remove {
4046 my ($self, @widgets) = @_;
4047
4048 $self->{multiplexer}->remove (@widgets)
4049}
4050
4051sub pages {
4052 my ($self) = @_;
4053 $self->{multiplexer}->children
4054}
4055
4056sub add_tab {
3752 my ($self, $title, $widget, $tooltip) = @_; 4057 my ($self, $title, $widget, $tooltip) = @_;
3753 4058
3754 CFPlus::weaken $self; 4059 $title = [$title, $tooltip] unless ref $title;
4060 $widget->{c_tab} = $title;
3755 4061
3756 unless (ref $title) {
3757 $title = new CFPlus::UI::Button
3758 markup => $title,
3759 tooltip => $tooltip,
3760 ;
3761 }
3762
3763 $title->connect (activate => sub { $self->set_current_page ($widget) });
3764 $self->{buttonbar}->add ($title);
3765
3766 $self->{multiplexer}->add ($widget); 4062 $self->add ($widget);
3767} 4063}
3768 4064
3769sub get_current_page { 4065sub get_current_page {
3770 my ($self) = @_; 4066 my ($self) = @_;
3771 4067
3777 4073
3778 $self->{multiplexer}->set_current_page ($page); 4074 $self->{multiplexer}->set_current_page ($page);
3779 $self->emit (page_changed => $self->{multiplexer}{current}); 4075 $self->emit (page_changed => $self->{multiplexer}{current});
3780} 4076}
3781 4077
4078sub _draw {
4079 my ($self) = @_;
4080
4081 $self->SUPER::_draw ();
4082
4083 if (my $cur = $self->{multiplexer}{current}) {
4084 if ($cur = $cur->{c_tab_}) {
4085 glTranslate $self->{buttonbar}{x} + $cur->{x},
4086 $self->{buttonbar}{y} + $cur->{y};
4087 glLineWidth 3;
4088 #glEnable GL_BLEND;
4089 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4090 glColor @{$self->{active_outline}};
4091 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4092 glLineWidth 1;
4093 #glDisable GL_BLEND;
4094 }
4095 }
4096}
4097
3782############################################################################# 4098#############################################################################
3783 4099
3784package CFPlus::UI::Selector; 4100package DC::UI::Selector;
3785 4101
3786use utf8; 4102use utf8;
3787 4103
3788our @ISA = CFPlus::UI::Button::; 4104our @ISA = DC::UI::Button::;
3789 4105
3790sub new { 4106sub new {
3791 my $class = shift; 4107 my $class = shift;
3792 4108
3793 my $self = $class->SUPER::new ( 4109 my $self = $class->SUPER::new (
3810 my ($value, $title, $tooltip) = @$_; 4126 my ($value, $title, $tooltip) = @$_;
3811 4127
3812 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4128 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3813 } 4129 }
3814 4130
3815 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4131 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3816} 4132}
3817 4133
3818sub _set_value { 4134sub _set_value {
3819 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
3820 4136
3821 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4137 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4138 $item ||= $self->{options}[0]
3822 or return; 4139 or return;
3823 4140
3824 $self->{value} = $item->[0]; 4141 $self->{value} = $item->[0];
3825 $self->set_markup ("$item->[1] ⇓"); 4142 $self->set_markup ("$item->[1] ⇓");
3826 $self->set_tooltip ($item->[2]); 4143# $self->set_tooltip ($item->[2]);
3827} 4144}
3828 4145
3829sub set_value { 4146sub set_value {
3830 my ($self, $value) = @_; 4147 my ($self, $value) = @_;
3831 4148
3833 4150
3834 $self->_set_value ($value); 4151 $self->_set_value ($value);
3835 $self->emit (changed => $value); 4152 $self->emit (changed => $value);
3836} 4153}
3837 4154
4155sub set_options {
4156 my ($self, $options) = @_;
4157
4158 $self->{options} = $options;
4159 $self->_set_value ($self->{value});
4160}
4161
3838############################################################################# 4162#############################################################################
3839 4163
3840package CFPlus::UI::Statusbox; 4164package DC::UI::Statusbox;
3841 4165
3842our @ISA = CFPlus::UI::VBox::; 4166our @ISA = DC::UI::VBox::;
3843 4167
3844sub new { 4168sub new {
3845 my $class = shift; 4169 my $class = shift;
3846 4170
3847 my $self = $class->SUPER::new ( 4171 my $self = $class->SUPER::new (
3848 fontsize => 0.8, 4172 fontsize => 0.8,
3849 @_, 4173 @_,
3850 ); 4174 );
3851 4175
3852 CFPlus::weaken (my $this = $self); 4176 DC::weaken (my $this = $self);
3853 4177
3854 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4178 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3855 4179
3856 $self 4180 $self
3857} 4181}
3858 4182
3859sub reorder { 4183sub reorder {
3860 my ($self) = @_; 4184 my ($self) = @_;
3861 my $NOW = Time::HiRes::time; 4185 my $NOW = Time::HiRes::time;
3862 4186
3863 # freeze display when hovering over any label 4187 # freeze display when hovering over any label
3864 return if $CFPlus::UI::TOOLTIP->{owner} 4188 return if $DC::UI::TOOLTIP->{owner}
3865 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4189 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3866 values %{ $self->{item} }; 4190 values %{ $self->{item} };
3867 4191
3868 while (my ($k, $v) = each %{ $self->{item} }) { 4192 while (my ($k, $v) = each %{ $self->{item} }) {
3869 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4193 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3870 } 4194 }
4195
4196 $self->{timer}->set (1, 1);
3871 4197
3872 my @widgets; 4198 my @widgets;
3873 4199
3874 my @items = sort { 4200 my @items = sort {
3875 $a->{pri} <=> $b->{pri} 4201 $a->{pri} <=> $b->{pri}
3876 or $b->{id} <=> $a->{id} 4202 or $b->{id} <=> $a->{id}
3877 } values %{ $self->{item} }; 4203 } values %{ $self->{item} };
3878
3879 $self->{timer}->interval (1);
3880 4204
3881 my $count = 10 + 1; 4205 my $count = 10 + 1;
3882 for my $item (@items) { 4206 for my $item (@items) {
3883 last unless --$count; 4207 last unless --$count;
3884 4208
3891 for ($short) { 4215 for ($short) {
3892 s/^\s+//; 4216 s/^\s+//;
3893 s/\s+/ /g; 4217 s/\s+/ /g;
3894 } 4218 }
3895 4219
3896 new CFPlus::UI::Label 4220 new DC::UI::Label
3897 markup => $short, 4221 markup => $short,
3898 tooltip => $item->{tooltip}, 4222 tooltip => $item->{tooltip},
3899 tooltip_font => $::FONT_PROP, 4223 tooltip_font => $::FONT_PROP,
3900 tooltip_width => 0.67, 4224 tooltip_width => 0.67,
3901 fontsize => $item->{fontsize} || $self->{fontsize}, 4225 fontsize => $item->{fontsize} || $self->{fontsize},
3908 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4232 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3909 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4233 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3910 $label->update; 4234 $label->update;
3911 $label->set_max_size (undef, $label->{req_h} * $diff) 4235 $label->set_max_size (undef, $label->{req_h} * $diff)
3912 if $diff < 1; 4236 if $diff < 1;
3913 $self->{timer}->interval (1/30); 4237 $self->{timer}->set (1/30, 1/30);
3914 } else { 4238 } else {
3915 $label->{fg}[3] = $item->{fg}[3] || 1; 4239 $label->{fg}[3] = $item->{fg}[3] || 1;
3916 } 4240 }
3917 4241
3918 push @widgets, $label; 4242 push @widgets, $label;
3980 $self->SUPER::destroy; 4304 $self->SUPER::destroy;
3981} 4305}
3982 4306
3983############################################################################# 4307#############################################################################
3984 4308
3985package CFPlus::UI::Root; 4309package DC::UI::Root;
3986 4310
3987our @ISA = CFPlus::UI::Container::; 4311our @ISA = DC::UI::Container::;
3988 4312
3989use List::Util qw(min max); 4313use List::Util qw(min max);
3990 4314
3991use CFPlus::OpenGL; 4315use DC::OpenGL;
3992 4316
3993sub new { 4317sub new {
3994 my $class = shift; 4318 my $class = shift;
3995 4319
3996 my $self = $class->SUPER::new ( 4320 my $self = $class->SUPER::new (
3997 visible => 1, 4321 visible => 1,
3998 @_, 4322 @_,
3999 ); 4323 );
4000 4324
4001 CFPlus::weaken ($self->{root} = $self); 4325 DC::weaken ($self->{root} = $self);
4002 4326
4003 $self 4327 $self
4004} 4328}
4005 4329
4006sub size_request { 4330sub size_request {
4054} 4378}
4055 4379
4056sub update { 4380sub update {
4057 my ($self) = @_; 4381 my ($self) = @_;
4058 4382
4059 $::WANT_REFRESH++; 4383 $::WANT_REFRESH = 1;
4060} 4384}
4061 4385
4062sub add { 4386sub add {
4063 my ($self, @children) = @_; 4387 my ($self, @children) = @_;
4064 4388
4209 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4533 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4210 glMatrixMode GL_MODELVIEW; 4534 glMatrixMode GL_MODELVIEW;
4211 glLoadIdentity; 4535 glLoadIdentity;
4212 4536
4213 { 4537 {
4214 package CFPlus::UI::Base; 4538 package DC::UI::Base;
4215 4539
4216 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4540 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4217 (0, 0, $self->{w}, $self->{h}); 4541 (0, 0, $self->{w}, $self->{h});
4218 4542
4219 $self->_draw; 4543 $self->_draw;
4220 } 4544 }
4221} 4545}
4222 4546
4223############################################################################# 4547#############################################################################
4224 4548
4225package CFPlus::UI; 4549package DC::UI;
4226 4550
4227$ROOT = new CFPlus::UI::Root; 4551$ROOT = new DC::UI::Root;
4228$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4552$TOOLTIP = new DC::UI::Tooltip z => 900;
4229 4553
42301 45541
4231 4555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines