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.420 by root, Wed Jul 25 15:50:17 2007 UTC vs.
Revision 1.479 by root, Thu Feb 4 20:01:09 2010 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
7use Event;
8 6
9use CFPlus; 7use Guard ();
8
9use DC;
10use CFPlus::Pod; 10use DC::Pod;
11use CFPlus::Texture; 11use DC::Texture;
12 12
13our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
14 14
15our $LAYOUT; 15our $LAYOUT;
16our $ROOT; 16our $ROOT;
17our $TOOLTIP; 17our $TOOLTIP;
18our $BUTTON_STATE; 18our $BUTTON_STATE;
19 19
20our %WIDGET; # all widgets, weak-referenced 20our %WIDGET; # all widgets, weak-referenced
21 21
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
23 $_[0]->stop;
24
23 if (!$GRAB) { 25 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 26 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 27 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 28 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 29 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 32 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 33 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 34
33 return if $ENV{CFPLUS_DEBUG} & 8; 35 return if $ENV{CFPLUS_DEBUG} & 8;
34 36
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 37 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 38 $TOOLTIP->show;
41 } 39 }
42 40
43 return; 41 return;
46 } 44 }
47 45
48 $TOOLTIP->hide; 46 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 47 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 48 delete $TOOLTIP->{owner};
51}); 49};
52 50
53sub get_layout { 51sub get_layout {
54 my $layout; 52 my $layout;
55 53
56 for (grep { $_->{name} } values %WIDGET) { 54 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 89 my $hover = $HOVER; $HOVER = $widget;
92 90
93 $hover->update if $hover && $hover->{can_hover}; 91 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 92 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 93
96 $TOOLTIP_WATCHER->start; 94 $TOOLTIP_WATCHER->again;
97 } 95 }
98} 96}
99 97
100sub feed_sdl_button_down_event { 98sub feed_sdl_button_down_event {
101 my ($ev) = @_; 99 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 105 my $widget = $ROOT->find_widget ($x, $y);
108 106
109 $GRAB = $widget; 107 $GRAB = $widget;
110 $GRAB->update if $GRAB; 108 $GRAB->update if $GRAB;
111 109
112 $TOOLTIP_WATCHER->cb->(); 110 $TOOLTIP_WATCHER->invoke;
113 } 111 }
114 112
115 if ($GRAB) { 113 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 114 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 115 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 116 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 117 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 118
121 $ev->{dx} = $shift ? $delta : 0; 119 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 120 $ev->{dy} = $shift ? 0 : $delta;
123 121
124 $GRAB->emit (mouse_wheel => $ev); 122 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 140 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 141 $grab->update if $grab;
144 $GRAB->update if $GRAB; 142 $GRAB->update if $GRAB;
145 143
146 check_hover $widget; 144 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 145 $TOOLTIP_WATCHER->invoke;
148 } 146 }
149} 147}
150 148
151sub feed_sdl_motion_event { 149sub feed_sdl_motion_event {
152 my ($ev) = @_; 150 my ($ev) = @_;
192# call when resolution changes etc. 190# call when resolution changes etc.
193sub rescale_widgets { 191sub rescale_widgets {
194 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
195 193
196 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
197 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
198 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 196 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 197 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
200 198
201 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 199 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
202 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 200 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
214 reconfigure_widgets; 212 reconfigure_widgets;
215} 213}
216 214
217############################################################################# 215#############################################################################
218 216
219package CFPlus::UI::Event; 217package DC::UI::Event;
220 218
221sub xy { 219sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 220 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 221}
224 222
225############################################################################# 223#############################################################################
226 224
227package CFPlus::UI::Base; 225package DC::UI::Base;
228 226
229use strict; 227use strict;
230 228
231use CFPlus::OpenGL; 229use DC::OpenGL;
232 230
233sub new { 231sub new {
234 my $class = shift; 232 my $class = shift;
235 233
236 my $self = bless { 234 my $self = bless {
241 h => undef, 239 h => undef,
242 can_events => 1, 240 can_events => 1,
243 @_ 241 @_
244 }, $class; 242 }, $class;
245 243
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 244 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 245
248 for (keys %$self) { 246 for (keys %$self) {
249 if (/^on_(.*)$/) { 247 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 248 $self->connect ($1 => delete $self->{$_});
251 } 249 }
252 } 250 }
253 251
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 252 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 253 $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}; 254 $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}; 255 $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}; 256 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
259 257
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 258 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 259 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 260
263 $self->show if $layout->{show}; 261 $self->show if $layout->{show};
273 $self->emit ("destroy"); 271 $self->emit ("destroy");
274 %$self = (); 272 %$self = ();
275} 273}
276 274
277sub TO_JSON { 275sub TO_JSON {
278 { __w_ => $_[0]{s_id} } 276 { "\fw" => $_[0]{s_id} }
279} 277}
280 278
281sub show { 279sub show {
282 my ($self) = @_; 280 my ($self) = @_;
283 281
284 return if $self->{parent}; 282 return if $self->{parent};
285 283
286 $CFPlus::UI::ROOT->add ($self); 284 $DC::UI::ROOT->add ($self);
287} 285}
288 286
289sub set_visible { 287sub set_visible {
290 my ($self) = @_; 288 my ($self) = @_;
291 289
292 return if $self->{visible}; 290 return if $self->{visible};
291
292 $self->{parent} && $self->{parent}{root}#d#
293 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
293 294
294 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
295 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
296 297
297 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
298 299
299 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
300 301
301 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
302} 303}
303 304
304sub set_invisible { 305sub set_invisible {
305 my ($self) = @_; 306 my ($self) = @_;
306 307
312 delete $self->{root}; 313 delete $self->{root};
313 314
314 undef $GRAB if $GRAB == $self; 315 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 316 undef $HOVER if $HOVER == $self;
316 317
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 318 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 319 if $TOOLTIP->{owner} == $self;
319 320
320 $self->emit ("focus_out"); 321 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 322 $self->emit (visibility_change => 0);
322} 323}
341sub hide { 342sub hide {
342 my ($self) = @_; 343 my ($self) = @_;
343 344
344 $self->set_invisible; 345 $self->set_invisible;
345 346
347 # extra $parent copy for 5.8.8+ bug workaround
348 # (otherwise $_[0] in remove gets freed
349 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 350 $parent->remove ($self);
347 if $self->{parent}; 351 }
348} 352}
349 353
350sub move_abs { 354sub move_abs {
351 my ($self, $x, $y, $z) = @_; 355 my ($self, $x, $y, $z) = @_;
352 356
364 $self->{force_h} = $h; 368 $self->{force_h} = $h;
365 369
366 $self->realloc; 370 $self->realloc;
367} 371}
368 372
373# traverse the widget chain up to find the maximum "physical" size constraints
374sub get_max_wh {
375 my ($self) = @_;
376
377 my ($w, $h) = @$self{qw(max_w max_h)};
378
379 if ($w <= 0 || $h <= 0) {
380 my ($mw, $mh) = $self->{parent}
381 ? $self->{parent}->get_max_wh
382 : ($::WIDTH, $::HEIGHT);
383
384 $w = $mw if $w <= 0;
385 $h = $mh if $h <= 0;
386 }
387
388 ($w, $h)
389}
390
369sub size_request { 391sub size_request {
370 require Carp; 392 require Carp;
371 Carp::confess "size_request is abstract"; 393 Carp::confess "size_request is abstract";
372} 394}
373 395
379 my ($self, $x, $y, $w, $h) = @_; 401 my ($self, $x, $y, $w, $h) = @_;
380 402
381 if ($self->{aspect}) { 403 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 404 my ($ow, $oh) = ($w, $h);
383 405
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 406 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 407 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 408
387 # use alignment to adjust x, y 409 # use alignment to adjust x, y
388 410
389 $x += int 0.5 * ($ow - $w); 411 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 412 $y += int 0.5 * ($oh - $h);
431 453
432 return if $self->{tooltip} eq $tooltip; 454 return if $self->{tooltip} eq $tooltip;
433 455
434 $self->{tooltip} = $tooltip; 456 $self->{tooltip} = $tooltip;
435 457
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 458 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 459 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 460 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 461 }
440} 462}
441 463
442# translate global coordinates to local coordinate system 464# translate global coordinates to local coordinate system
443sub coord2local { 465sub coord2local {
444 my ($self, $x, $y) = @_; 466 my ($self, $x, $y) = @_;
445 467
446 Carp::confess unless $self->{parent};#d# 468 return (undef, undef) unless $self->{parent};
447 469
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 470 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 471}
450 472
451# translate local coordinates to global coordinate system 473# translate local coordinates to global coordinate system
452sub coord2global { 474sub coord2global {
453 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
454 476
455 Carp::confess unless $self->{parent};#d# 477 return (undef, undef) unless $self->{parent};
456 478
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 479 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 480}
459 481
460sub invoke_focus_in { 482sub invoke_focus_in {
509sub connect { 531sub connect {
510 my ($self, $signal, $cb) = @_; 532 my ($self, $signal, $cb) = @_;
511 533
512 push @{ $self->{signal_cb}{$signal} }, $cb; 534 push @{ $self->{signal_cb}{$signal} }, $cb;
513 535
514 defined wantarray and CFPlus::guard { 536 defined wantarray and Guard::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 537 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 538 @{ $self->{signal_cb}{$signal} };
517 } 539 }
518} 540}
519 541
557# in .xs 579# in .xs
558 580
559sub set_parent { 581sub set_parent {
560 my ($self, $parent) = @_; 582 my ($self, $parent) = @_;
561 583
562 CFPlus::weaken ($self->{parent} = $parent); 584 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 585 $self->set_visible if $parent->{visible};
564} 586}
565 587
566sub realloc { 588sub realloc {
567 my ($self) = @_; 589 my ($self) = @_;
605} 627}
606 628
607sub DESTROY { 629sub DESTROY {
608 my ($self) = @_; 630 my ($self) = @_;
609 631
610 return if CFPlus::in_destruct; 632 return if DC::in_destruct;
611 633
634 local $@;
612 eval { $self->destroy }; 635 eval { $self->destroy };
613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614 637
615 delete $WIDGET{$self+0}; 638 delete $WIDGET{$self+0};
616} 639}
617 640
618############################################################################# 641#############################################################################
619 642
620package CFPlus::UI::DrawBG; 643package DC::UI::DrawBG;
621 644
622our @ISA = CFPlus::UI::Base::; 645our @ISA = DC::UI::Base::;
623 646
624use strict; 647use strict;
625use CFPlus::OpenGL; 648use DC::OpenGL;
626 649
627sub new { 650sub new {
628 my $class = shift; 651 my $class = shift;
629
630 # range [value, low, high, page]
631 652
632 $class->SUPER::new ( 653 $class->SUPER::new (
633 #bg => [0, 0, 0, 0.2], 654 #bg => [0, 0, 0, 0.2],
634 #active_bg => [1, 1, 1, 0.5], 655 #active_bg => [1, 1, 1, 0.5],
635 @_ 656 @_
636 ) 657 )
637} 658}
638 659
660sub set_bg {
661 my ($self, $bg) = @_;
662
663 $self->{bg} = $bg;
664 $self->update;
665}
666
639sub _draw { 667sub _draw {
640 my ($self) = @_; 668 my ($self) = @_;
641 669
642 my $color = $FOCUS == $self && $self->{active_bg} 670 my $color = $FOCUS == $self
643 ? $self->{active_bg} 671 ? $self->{active_bg} || $self->{bg}
644 : $self->{bg}; 672 : $self->{bg};
645 673
646 if ($color && (@$color < 4 || $color->[3])) { 674 if ($color && (@$color < 4 || $color->[3])) {
647 my ($w, $h) = @$self{qw(w h)}; 675 my ($w, $h) = @$self{qw(w h)};
648 676
649 glEnable GL_BLEND; 677 glEnable GL_BLEND;
650 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 678 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
651 glColor_premultiply @$color; 679 glColor_premultiply @$color;
652
653 glBegin GL_QUADS;
654 glVertex 0 , 0;
655 glVertex 0 , $h;
656 glVertex $w, $h; 680 glRect 0, 0, $w, $h;
657 glVertex $w, 0;
658 glEnd;
659
660 glDisable GL_BLEND; 681 glDisable GL_BLEND;
661 } 682 }
662} 683}
663 684
664############################################################################# 685#############################################################################
665 686
666package CFPlus::UI::Empty; 687package DC::UI::Empty;
667 688
668our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
669 690
670sub new { 691sub new {
671 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
672 $class->SUPER::new (can_events => 0, %arg); 693 $class->SUPER::new (can_events => 0, %arg);
673} 694}
680 701
681sub draw { } 702sub draw { }
682 703
683############################################################################# 704#############################################################################
684 705
685package CFPlus::UI::Container; 706package DC::UI::Container;
686 707
687our @ISA = CFPlus::UI::Base::; 708our @ISA = DC::UI::Base::;
688 709
689sub new { 710sub new {
690 my ($class, %arg) = @_; 711 my ($class, %arg) = @_;
691 712
692 my $children = delete $arg{children}; 713 my $children = delete $arg{children};
786 $_->draw for $self->visible_children; 807 $_->draw for $self->visible_children;
787} 808}
788 809
789############################################################################# 810#############################################################################
790 811
791package CFPlus::UI::Bin; 812package DC::UI::Bin;
792 813
793our @ISA = CFPlus::UI::Container::; 814our @ISA = DC::UI::Container::;
794 815
795sub new { 816sub new {
796 my ($class, %arg) = @_; 817 my ($class, %arg) = @_;
797 818
798 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 819 my $child = (delete $arg{child}) || new DC::UI::Empty::;
799 820
800 $class->SUPER::new (children => [$child], %arg) 821 $class->SUPER::new (children => [$child], %arg)
801} 822}
802 823
803sub add { 824sub add {
810sub remove { 831sub remove {
811 my ($self, $widget) = @_; 832 my ($self, $widget) = @_;
812 833
813 $self->SUPER::remove ($widget); 834 $self->SUPER::remove ($widget);
814 835
815 $self->{children} = [new CFPlus::UI::Empty] 836 $self->{children} = [new DC::UI::Empty]
816 unless @{$self->{children}}; 837 unless @{$self->{children}};
817} 838}
818 839
819sub child { $_[0]->{children}[0] } 840sub child { $_[0]->{children}[0] }
820 841
829 850
830 1 851 1
831} 852}
832 853
833############################################################################# 854#############################################################################
834
835# back-buffered drawing area 855# back-buffered drawing area
836 856
837package CFPlus::UI::Window; 857package DC::UI::Window;
838 858
839our @ISA = CFPlus::UI::Bin::; 859our @ISA = DC::UI::Bin::;
840 860
841use CFPlus::OpenGL; 861use DC::OpenGL;
842 862
843sub new { 863sub new {
844 my ($class, %arg) = @_; 864 my ($class, %arg) = @_;
845 865
846 my $self = $class->SUPER::new (%arg); 866 my $self = $class->SUPER::new (%arg);
868} 888}
869 889
870sub render_child { 890sub render_child {
871 my ($self) = @_; 891 my ($self) = @_;
872 892
873 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 893 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
874 glClearColor 0, 0, 0, 0; 894 glClearColor 0, 0, 0, 0;
875 glClear GL_COLOR_BUFFER_BIT; 895 glClear GL_COLOR_BUFFER_BIT;
876 896
877 { 897 {
878 package CFPlus::UI::Base; 898 package DC::UI::Base;
879 899
880 local ($draw_x, $draw_y, $draw_w, $draw_h) = 900 local ($draw_x, $draw_y, $draw_w, $draw_h) =
881 (0, 0, $self->{w}, $self->{h}); 901 (0, 0, $self->{w}, $self->{h});
882 902
883 $self->_render; 903 $self->_render;
900 glDisable GL_TEXTURE_2D; 920 glDisable GL_TEXTURE_2D;
901} 921}
902 922
903############################################################################# 923#############################################################################
904 924
905package CFPlus::UI::ViewPort; 925package DC::UI::ViewPort;
906 926
907use List::Util qw(min max); 927use List::Util qw(min max);
908 928
909our @ISA = CFPlus::UI::Window::; 929our @ISA = DC::UI::Window::;
910 930
911sub new { 931sub new {
912 my $class = shift; 932 my $class = shift;
913 933
914 $class->SUPER::new ( 934 $class->SUPER::new (
998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
999 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1000 ) { 1020 ) {
1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1002 } else { 1022 } else {
1003 $self->CFPlus::UI::Base::find_widget ($x, $y) 1023 $self->DC::UI::Base::find_widget ($x, $y)
1004 } 1024 }
1005} 1025}
1006 1026
1007sub _render { 1027sub _render {
1008 my ($self) = @_; 1028 my ($self) = @_;
1009 1029
1010 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1030 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1011 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1031 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1012 1032
1013 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1033 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1014 1034
1015 $self->SUPER::_render; 1035 $self->SUPER::_render;
1016} 1036}
1017 1037
1018############################################################################# 1038#############################################################################
1019 1039
1020package CFPlus::UI::ScrolledWindow; 1040package DC::UI::ScrolledWindow;
1021 1041
1022our @ISA = CFPlus::UI::Table::; 1042our @ISA = DC::UI::Table::;
1023 1043
1024sub new { 1044sub new {
1025 my ($class, %arg) = @_; 1045 my ($class, %arg) = @_;
1026 1046
1027 my $child = delete $arg{child}; 1047 my $child = delete $arg{child};
1028 1048
1029 my $self; 1049 my $self;
1030 1050
1031 my $hslider = new CFPlus::UI::Slider 1051 my $hslider = new DC::UI::Slider
1032 col => 0, 1052 c_col => 0,
1033 row => 1, 1053 c_row => 1,
1034 vertical => 0, 1054 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix 1055 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub { 1056 on_changed => sub {
1037 $self->{hpos} = $_[1]; 1057 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 }, 1059 },
1040 ; 1060 ;
1041 1061
1042 my $vslider = new CFPlus::UI::Slider 1062 my $vslider = new DC::UI::Slider
1043 col => 1, 1063 c_col => 1,
1044 row => 0, 1064 c_row => 0,
1045 vertical => 1, 1065 vertical => 1,
1046 range => [0, 0, 1, 0.01], # HACK fix 1066 range => [0, 0, 1, 0.01], # HACK fix
1047 on_changed => sub { 1067 on_changed => sub {
1048 $self->{vpos} = $_[1]; 1068 $self->{vpos} = $_[1];
1049 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 col_expand => [1, 0], 1079 col_expand => [1, 0],
1060 row_expand => [1, 0], 1080 row_expand => [1, 0],
1061 %arg, 1081 %arg,
1062 ); 1082 );
1063 1083
1064 $self->{vp} = new CFPlus::UI::ViewPort 1084 $self->{vp} = new DC::UI::ViewPort
1065 col => 0, 1085 c_col => 0,
1066 row => 0, 1086 c_row => 0,
1067 expand => 1, 1087 expand => 1,
1068 scroll_x => $self->{scroll_x}, 1088 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y}, 1089 scroll_y => $self->{scroll_y},
1070 on_changed => sub { 1090 on_changed => sub {
1071 my ($vp, $x, $y) = @_; 1091 my ($vp, $x, $y) = @_;
1195 $self->SUPER::invoke_size_allocate ($w, $h) 1215 $self->SUPER::invoke_size_allocate ($w, $h)
1196} 1216}
1197 1217
1198############################################################################# 1218#############################################################################
1199 1219
1200package CFPlus::UI::Frame; 1220package DC::UI::Frame;
1201 1221
1202our @ISA = CFPlus::UI::Bin::; 1222our @ISA = DC::UI::Bin::;
1203 1223
1204use CFPlus::OpenGL; 1224use DC::OpenGL;
1205 1225
1206sub new { 1226sub new {
1207 my $class = shift; 1227 my $class = shift;
1208 1228
1209 $class->SUPER::new ( 1229 $class->SUPER::new (
1219 my ($w, $h) = @$self{qw(w h)}; 1239 my ($w, $h) = @$self{qw(w h)};
1220 1240
1221 glEnable GL_BLEND; 1241 glEnable GL_BLEND;
1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1242 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1223 glColor_premultiply @{ $self->{bg} }; 1243 glColor_premultiply @{ $self->{bg} };
1224
1225 glBegin GL_QUADS;
1226 glVertex 0 , 0;
1227 glVertex 0 , $h;
1228 glVertex $w, $h; 1244 glRect 0, 0, $w, $h;
1229 glVertex $w, 0;
1230 glEnd;
1231
1232 glDisable GL_BLEND; 1245 glDisable GL_BLEND;
1233 } 1246 }
1234 1247
1235 $self->SUPER::_draw; 1248 $self->SUPER::_draw;
1236} 1249}
1237 1250
1238############################################################################# 1251#############################################################################
1239 1252
1240package CFPlus::UI::FancyFrame; 1253package DC::UI::FancyFrame;
1241 1254
1242our @ISA = CFPlus::UI::Bin::; 1255our @ISA = DC::UI::Bin::;
1243 1256
1244use CFPlus::OpenGL; 1257use DC::OpenGL;
1245 1258
1246sub new { 1259sub new {
1247 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1248 1261
1249 if ((exists $arg{label}) && !ref $arg{label}) { 1262 if ((exists $arg{label}) && !ref $arg{label}) {
1250 $arg{label} = new CFPlus::UI::Label 1263 $arg{label} = new DC::UI::Label
1251 align => 1, 1264 align => 1,
1252 valign => 0, 1265 valign => 0.5,
1253 text => $arg{label}, 1266 text => $arg{label},
1254 fontsize => ($arg{border} || 0.8) * 0.75; 1267 fontsize => ($arg{border} || 0.8) * 0.75;
1255 } 1268 }
1256 1269
1257 my $self = $class->SUPER::new ( 1270 my $self = $class->SUPER::new (
1258 # label => "", 1271 # label => "",
1259 fg => [0.6, 0.3, 0.1], 1272 fg => undef,
1260 border => 0.8, 1273 border => 0.8,
1261 style => 'single', 1274 style => 'single',
1262 %arg, 1275 %arg,
1263 ); 1276 );
1264 1277
1267 1280
1268sub add { 1281sub add {
1269 my ($self, @widgets) = @_; 1282 my ($self, @widgets) = @_;
1270 1283
1271 $self->SUPER::add (@widgets); 1284 $self->SUPER::add (@widgets);
1272 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1285 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1273} 1286}
1274 1287
1275sub border { 1288sub border {
1276 int $_[0]{border} * $::FONTSIZE 1289 int $_[0]{border} * $::FONTSIZE
1277} 1290}
1317 my $border = $self->border; 1330 my $border = $self->border;
1318 my ($w, $h) = ($self->{w}, $self->{h}); 1331 my ($w, $h) = ($self->{w}, $self->{h});
1319 1332
1320 $child->draw; 1333 $child->draw;
1321 1334
1322 glColor @{$self->{fg}}; 1335 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1323 glBegin GL_LINE_STRIP; 1336 glBegin GL_LINE_STRIP;
1324 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1337 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1325 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1338 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1326 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1327 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1340 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1335 } 1348 }
1336} 1349}
1337 1350
1338############################################################################# 1351#############################################################################
1339 1352
1340package CFPlus::UI::Toplevel; 1353package DC::UI::Toplevel;
1341 1354
1342our @ISA = CFPlus::UI::Bin::; 1355our @ISA = DC::UI::Bin::;
1343 1356
1344use CFPlus::OpenGL; 1357use DC::OpenGL;
1345 1358
1346my $bg = 1359my $bg =
1347 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1360 new_from_resource DC::Texture "d1_bg.png",
1348 mipmap => 1, wrap => 1; 1361 mipmap => 1, wrap => 1;
1349 1362
1350my @border = 1363my @border =
1351 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1364 map { new_from_resource DC::Texture $_, mipmap => 1 }
1352 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1365 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1353 1366
1354my @icon = 1367my @icon =
1355 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1368 map { new_from_resource DC::Texture $_, mipmap => 1 }
1356 qw(x1_move.png x1_resize.png); 1369 qw(x1_move.png x1_resize.png);
1357 1370
1358sub new { 1371sub new {
1359 my ($class, %arg) = @_; 1372 my ($class, %arg) = @_;
1360 1373
1361 my $self = $class->SUPER::new ( 1374 my $self = $class->SUPER::new (
1362 bg => [1, 1, 1, 1], 1375 bg => [1, 1, 1, 1],
1363 border_bg => [1, 1, 1, 1], 1376 border_bg => [1, 1, 1, 1],
1364 border => 0.6, 1377 border => 0.8,
1365 can_events => 1, 1378 can_events => 1,
1366 min_w => 64, 1379 min_w => 64,
1367 min_h => 32, 1380 min_h => 32,
1368 %arg, 1381 %arg,
1369 ); 1382 );
1370 1383
1371 $self->{title_widget} = new CFPlus::UI::Label 1384 $self->{title_widget} = new DC::UI::Label
1372 align => 0, 1385 align => 0.5,
1373 valign => 1, 1386 valign => 1,
1374 text => $self->{title}, 1387 text => $self->{title},
1375 fontsize => $self->{border}, 1388 fontsize => $self->{border},
1376 if exists $self->{title}; 1389 if exists $self->{title};
1377 1390
1378 if ($self->{has_close_button}) { 1391 if ($self->{has_close_button}) {
1379 $self->{close_button} = 1392 $self->{close_button} =
1380 new CFPlus::UI::ImageButton 1393 new DC::UI::ImageButton
1381 path => 'x1_close.png', 1394 path => 'x1_close.png',
1382 on_activate => sub { $self->emit ("delete") }; 1395 on_activate => sub { $self->emit ("delete") };
1383 1396
1384 $self->CFPlus::UI::Container::add ($self->{close_button}); 1397 $self->DC::UI::Container::add ($self->{close_button});
1385 } 1398 }
1386 1399
1387 $self 1400 $self
1388} 1401}
1389 1402
1390sub add { 1403sub add {
1391 my ($self, @widgets) = @_; 1404 my ($self, @widgets) = @_;
1392 1405
1393 $self->SUPER::add (@widgets); 1406 $self->SUPER::add (@widgets);
1394 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1407 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1395 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1408 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1396} 1409}
1397 1410
1398sub border { 1411sub border {
1399 int $_[0]{border} * $::FONTSIZE 1412 int $_[0]{border} * $::FONTSIZE
1413}
1414
1415sub get_max_wh {
1416 my ($self) = @_;
1417
1418 return ($self->{w}, $self->{h})
1419 if $self->{visible} && $self->{w};
1420
1421 $self->SUPER::get_max_wh
1400} 1422}
1401 1423
1402sub size_request { 1424sub size_request {
1403 my ($self) = @_; 1425 my ($self) = @_;
1404 1426
1529 glEnable GL_TEXTURE_2D; 1551 glEnable GL_TEXTURE_2D;
1530 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1531 1553
1532 my $border = $self->border; 1554 my $border = $self->border;
1533 1555
1556 if ($border) {
1534 glColor @{ $self->{border_bg} }; 1557 glColor @{ $self->{border_bg} };
1535 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1558 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1536 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1559 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1537 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1560 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1538 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1561 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1539 1562
1540 # move 1563 # move
1541 my $w2 = ($w - $border) * .5; 1564 my $w2 = ($w - $border) * .5;
1542 my $h2 = ($h - $border) * .5; 1565 my $h2 = ($h - $border) * .5;
1543 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1566 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1545 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1546 1569
1547 # resize 1570 # resize
1548 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1571 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1572 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1550 unless $self->{has_close_button}; 1573 unless $self->{has_close_button};
1551 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1574 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1576 }
1553 1577
1554 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1578 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1555 glColor @{ $self->{bg} }; 1579 glColor @{ $self->{bg} };
1556 1580
1557 # TODO: repeat texture not scale 1581 # TODO: repeat texture not scale
1576 if $self->{close_button}; 1600 if $self->{close_button};
1577} 1601}
1578 1602
1579############################################################################# 1603#############################################################################
1580 1604
1581package CFPlus::UI::Table; 1605package DC::UI::Table;
1582 1606
1583our @ISA = CFPlus::UI::Container::; 1607our @ISA = DC::UI::Container::;
1584 1608
1585use List::Util qw(max sum); 1609use List::Util qw(max sum);
1586 1610
1587use CFPlus::OpenGL; 1611use DC::OpenGL;
1588 1612
1589sub new { 1613sub new {
1590 my $class = shift; 1614 my $class = shift;
1591 1615
1592 $class->SUPER::new ( 1616 $class->SUPER::new (
1598 1622
1599sub add { 1623sub add {
1600 my ($self, @widgets) = @_; 1624 my ($self, @widgets) = @_;
1601 1625
1602 for my $child (@widgets) { 1626 for my $child (@widgets) {
1603 $child->{rowspan} ||= 1; 1627 $child->{c_rowspan} ||= 1;
1604 $child->{colspan} ||= 1; 1628 $child->{c_colspan} ||= 1;
1605 } 1629 }
1606 1630
1607 $self->SUPER::add (@widgets); 1631 $self->SUPER::add (@widgets);
1608} 1632}
1609 1633
1613 my @widgets; 1637 my @widgets;
1614 1638
1615 while (@_) { 1639 while (@_) {
1616 my ($col, $row, $child) = splice @_, 0, 3, (); 1640 my ($col, $row, $child) = splice @_, 0, 3, ();
1617 1641
1618 $child->{row} = $row; 1642 $child->{c_row} = $row;
1619 $child->{col} = $col; 1643 $child->{c_col} = $col;
1620 1644
1621 push @widgets, $child; 1645 push @widgets, $child;
1622 } 1646 }
1623 1647
1624 $self->add (@widgets); 1648 $self->add (@widgets);
1630 my (@w, @h); 1654 my (@w, @h);
1631 1655
1632 my @children = $self->children; 1656 my @children = $self->children;
1633 1657
1634 # first pass, columns 1658 # first pass, columns
1635 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1659 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1636 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1660 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1637 1661
1638 my $sw = sum @w[$c .. $c + $cs - 1]; 1662 my $sw = sum @w[$c .. $c + $cs - 1];
1639 1663
1640 if ($w > $sw) { 1664 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1665 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1642 } 1666 }
1643 } 1667 }
1644 1668
1645 # second pass, rows 1669 # second pass, rows
1646 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1670 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1647 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1671 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1648 1672
1649 my $sh = sum @h[$r .. $r + $rs - 1]; 1673 my $sh = sum @h[$r .. $r + $rs - 1];
1650 1674
1651 if ($h > $sh) { 1675 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1676 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1653 } 1677 }
1654 } 1678 }
1655 1679
1656 (\@w, \@h) 1680 (\@w, \@h)
1657} 1681}
1680 @col_expand = (1) x @$ws unless @col_expand; 1704 @col_expand = (1) x @$ws unless @col_expand;
1681 my $col_expand = (sum @col_expand) || 1; 1705 my $col_expand = (sum @col_expand) || 1;
1682 1706
1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1707 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1684 1708
1685 CFPlus::UI::harmonize $ws; 1709 DC::UI::harmonize $ws;
1686 1710
1687 my @row_expand = @{$self->{row_expand}}; 1711 my @row_expand = @{$self->{row_expand}};
1688 @row_expand = (1) x @$ws unless @row_expand; 1712 @row_expand = (1) x @$ws unless @row_expand;
1689 my $row_expand = (sum @row_expand) || 1; 1713 my $row_expand = (sum @row_expand) || 1;
1690 1714
1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1715 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1692 1716
1693 CFPlus::UI::harmonize $hs; 1717 DC::UI::harmonize $hs;
1694 1718
1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1719 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1720 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1697 1721
1698 for my $widget ($self->children) { 1722 for my $widget ($self->children) {
1699 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1723 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1700 1724
1701 $widget->configure ( 1725 $widget->configure (
1702 $x[$c], $y[$r], 1726 $x[$c], $y[$r],
1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1727 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1704 ); 1728 );
1707 1 1731 1
1708} 1732}
1709 1733
1710############################################################################# 1734#############################################################################
1711 1735
1712package CFPlus::UI::Fixed; 1736package DC::UI::Fixed;
1713 1737
1714use List::Util qw(min max); 1738use List::Util qw(min max);
1715 1739
1716our @ISA = CFPlus::UI::Container::; 1740our @ISA = DC::UI::Container::;
1717 1741
1718sub _scale($$$) { 1742sub _scale($$$) {
1719 my ($rel, $val, $max) = @_; 1743 my ($rel, $val, $max) = @_;
1720 1744
1721 $rel ? $val * $max : $val 1745 $rel ? $val * $max : $val
1773 1 1797 1
1774} 1798}
1775 1799
1776############################################################################# 1800#############################################################################
1777 1801
1778package CFPlus::UI::Box; 1802package DC::UI::Box;
1779 1803
1780our @ISA = CFPlus::UI::Container::; 1804our @ISA = DC::UI::Container::;
1781 1805
1782sub size_request { 1806sub size_request {
1783 my ($self) = @_; 1807 my ($self) = @_;
1784 1808
1785 my @children = $self->visible_children; 1809 my @children = $self->visible_children;
1820 $req[$_] += $space * $children[$_]{expand} 1844 $req[$_] += $space * $children[$_]{expand}
1821 for 0 .. $#children; 1845 for 0 .. $#children;
1822 } 1846 }
1823 } 1847 }
1824 1848
1825 CFPlus::UI::harmonize \@req; 1849 DC::UI::harmonize \@req;
1826 1850
1827 my $pos = 0; 1851 my $pos = 0;
1828 for (0 .. $#children) { 1852 for (0 .. $#children) {
1829 my $alloc = $req[$_]; 1853 my $alloc = $req[$_];
1830 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1854 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1835 1 1859 1
1836} 1860}
1837 1861
1838############################################################################# 1862#############################################################################
1839 1863
1840package CFPlus::UI::HBox; 1864package DC::UI::HBox;
1841 1865
1842our @ISA = CFPlus::UI::Box::; 1866our @ISA = DC::UI::Box::;
1843 1867
1844sub new { 1868sub new {
1845 my $class = shift; 1869 my $class = shift;
1846 1870
1847 $class->SUPER::new ( 1871 $class->SUPER::new (
1850 ) 1874 )
1851} 1875}
1852 1876
1853############################################################################# 1877#############################################################################
1854 1878
1855package CFPlus::UI::VBox; 1879package DC::UI::VBox;
1856 1880
1857our @ISA = CFPlus::UI::Box::; 1881our @ISA = DC::UI::Box::;
1858 1882
1859sub new { 1883sub new {
1860 my $class = shift; 1884 my $class = shift;
1861 1885
1862 $class->SUPER::new ( 1886 $class->SUPER::new (
1865 ) 1889 )
1866} 1890}
1867 1891
1868############################################################################# 1892#############################################################################
1869 1893
1870package CFPlus::UI::Label; 1894package DC::UI::Label;
1871 1895
1872our @ISA = CFPlus::UI::DrawBG::; 1896our @ISA = DC::UI::DrawBG::;
1873 1897
1874use CFPlus::OpenGL; 1898use DC::OpenGL;
1875 1899
1876sub new { 1900sub new {
1877 my ($class, %arg) = @_; 1901 my ($class, %arg) = @_;
1878 1902
1879 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1884 #text => initial text 1908 #text => initial text
1885 #markup => initial narkup 1909 #markup => initial narkup
1886 #max_w => maximum pixel width 1910 #max_w => maximum pixel width
1887 #style => 0, # render flags 1911 #style => 0, # render flags
1888 ellipsise => 3, # end 1912 ellipsise => 3, # end
1889 layout => (new CFPlus::Layout), 1913 layout => (new DC::Layout),
1890 fontsize => 1, 1914 fontsize => 1,
1891 align => -1, 1915 align => 0.5,
1892 valign => -1, 1916 valign => 0.5,
1893 padding_x => 2, 1917 padding_x => 4,
1894 padding_y => 2, 1918 padding_y => 2,
1895 can_events => 0, 1919 can_events => 0,
1896 %arg 1920 %arg
1897 ); 1921 );
1898 1922
1899 if (exists $self->{template}) { 1923 if (exists $self->{template}) {
1900 my $layout = new CFPlus::Layout; 1924 my $layout = new DC::Layout;
1901 $layout->set_text (delete $self->{template}); 1925 $layout->set_text (delete $self->{template});
1902 $self->{template} = $layout; 1926 $self->{template} = $layout;
1903 } 1927 }
1904 1928
1905 if (exists $self->{markup}) { 1929 if (exists $self->{markup}) {
1923 1947
1924 delete $self->{ox}; 1948 delete $self->{ox};
1925 $self->SUPER::realloc; 1949 $self->SUPER::realloc;
1926} 1950}
1927 1951
1952sub clear {
1953 my ($self) = @_;
1954
1955 $self->set_text ("");
1956}
1957
1928sub set_text { 1958sub set_text {
1929 my ($self, $text) = @_; 1959 my ($self, $text) = @_;
1930 1960
1931 return if $self->{text} eq "T$text"; 1961 return if $self->{text} eq "T$text";
1932 $self->{text} = "T$text"; 1962 $self->{text} = "T$text";
1955 1985
1956sub size_request { 1986sub size_request {
1957 my ($self) = @_; 1987 my ($self) = @_;
1958 1988
1959 $self->{size_req} ||= do { 1989 $self->{size_req} ||= do {
1990 my ($max_w, $max_h) = $self->get_max_wh;
1991
1960 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1992 $self->{layout}->set_font ($self->{font}) if $self->{font};
1961 $self->{layout}->set_width ($self->{max_w} || -1); 1993 $self->{layout}->set_width ($max_w);
1962 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1994 $self->{layout}->set_ellipsise ($self->{ellipsise});
1963 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1995 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1964 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1996 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1965 1997
1966 my ($w, $h) = $self->{layout}->size; 1998 my ($w, $h) = $self->{layout}->size;
1967 1999
1968 if (exists $self->{template}) { 2000 if (exists $self->{template}) {
1969 $self->{template}->set_font ($self->{font}) if $self->{font}; 2001 $self->{template}->set_font ($self->{font}) if $self->{font};
1970 $self->{template}->set_width ($self->{max_w} || -1); 2002 $self->{template}->set_width ($max_w);
1971 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2003 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1972 2004
1973 my ($w2, $h2) = $self->{template}->size; 2005 my ($w2, $h2) = $self->{template}->size;
1974 2006
1975 $w = List::Util::max $w, $w2; 2007 $w = List::Util::max $w, $w2;
2031 2063
2032 [$self->{layout}->size] 2064 [$self->{layout}->size]
2033 }; 2065 };
2034 2066
2035 unless (exists $self->{ox}) { 2067 unless (exists $self->{ox}) {
2036 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2068 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2037 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2069 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2038 : ($self->{w} - $size->[0]) * 0.5);
2039 2070
2040 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2071 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2041 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2042 : ($self->{h} - $size->[1]) * 0.5);
2043 }; 2072 };
2044 2073
2045# unless ($self->{list}) { 2074# unless ($self->{list}) {
2046# $self->{list} = CFPlus::OpenGL::glGenList; 2075# $self->{list} = DC::OpenGL::glGenList;
2047# CFPlus::OpenGL::glNewList $self->{list}; 2076# DC::OpenGL::glNewList $self->{list};
2048# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2077# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2049# CFPlus::OpenGL::glEndList; 2078# DC::OpenGL::glEndList;
2050# } 2079# }
2051# 2080#
2052# CFPlus::OpenGL::glCallList $self->{list}; 2081# DC::OpenGL::glCallList $self->{list};
2053 2082
2054 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2083 $self->{layout}->draw;
2055} 2084}
2056 2085
2057#sub destroy { 2086#sub destroy {
2058# my ($self) = @_; 2087# my ($self) = @_;
2059# 2088#
2060# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2089# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2061# 2090#
2062# $self->SUPER::destroy; 2091# $self->SUPER::destroy;
2063#} 2092#}
2064 2093
2065############################################################################# 2094#############################################################################
2066 2095
2067package CFPlus::UI::EntryBase; 2096package DC::UI::EntryBase;
2068 2097
2069our @ISA = CFPlus::UI::Label::; 2098our @ISA = DC::UI::Label::;
2070 2099
2071use CFPlus::OpenGL; 2100use DC::OpenGL;
2072 2101
2073sub new { 2102sub new {
2074 my $class = shift; 2103 my $class = shift;
2075 2104
2076 $class->SUPER::new ( 2105 $class->SUPER::new (
2077 fg => [1, 1, 1], 2106 fg => [1, 1, 1],
2078 bg => [0, 0, 0, 0.2], 2107 bg => [0, 0, 0, 0.2],
2079 outline => [0.6, 0.3, 0.1], 2108 outline => undef,
2080 active_bg => [0, 0, 1, .2], 2109 active_bg => [0, 0, 1, .2],
2081 active_fg => [1, 1, 1], 2110 active_fg => [1, 1, 1],
2082 active_outline => [1, 1, 0], 2111 active_outline => [1, 1, 0],
2083 can_hover => 1, 2112 can_hover => 1,
2084 can_focus => 1, 2113 can_focus => 1,
2114 align => 0,
2085 valign => 0, 2115 valign => 0.5,
2086 can_events => 1, 2116 can_events => 1,
2087 ellipsise => 0, 2117 ellipsise => 0,
2118 padding_x => 4,
2119 padding_y => 2,
2088 #text => ... 2120 #text => ...
2089 #hidden => "*", 2121 #hidden => "*",
2090 @_ 2122 @_
2091 ) 2123 )
2092} 2124}
2139 2171
2140 my $text = $self->get_text; 2172 my $text = $self->get_text;
2141 2173
2142 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2174 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2143 2175
2144 if ($uni == 8) { 2176 if ($sym == DC::SDLK_BACKSPACE) {
2145 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2177 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2146 } elsif ($uni == 127) { 2178 } elsif ($sym == DC::SDLK_DELETE) {
2147 substr $text, $self->{cursor}, 1, ""; 2179 substr $text, $self->{cursor}, 1, "";
2148 } elsif ($sym == CFPlus::SDLK_LEFT) { 2180 } elsif ($sym == DC::SDLK_LEFT) {
2149 --$self->{cursor} if $self->{cursor}; 2181 --$self->{cursor} if $self->{cursor};
2150 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2182 } elsif ($sym == DC::SDLK_RIGHT) {
2151 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2183 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2152 } elsif ($sym == CFPlus::SDLK_HOME) { 2184 } elsif ($sym == DC::SDLK_HOME) {
2153 # what a hack 2185 # what a hack
2154 $self->{cursor} = 2186 $self->{cursor} =
2155 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2187 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2156 ? length $1 2188 ? length $1
2157 : 0; 2189 : 0;
2158 } elsif ($sym == CFPlus::SDLK_END) { 2190 } elsif ($sym == DC::SDLK_END) {
2159 # uh, again 2191 # uh, again
2160 $self->{cursor} = 2192 $self->{cursor} =
2161 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2193 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2162 ? $self->{cursor} + length $1 2194 ? $self->{cursor} + length $1
2163 : length $self->{text}; 2195 : length $self->{text};
2227 glColor_premultiply @{$self->{bg}}; 2259 glColor_premultiply @{$self->{bg}};
2228 } 2260 }
2229 2261
2230 glEnable GL_BLEND; 2262 glEnable GL_BLEND;
2231 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2263 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2232 glBegin GL_QUADS;
2233 glVertex 0 , 0;
2234 glVertex 0 , $self->{h};
2235 glVertex $self->{w}, $self->{h}; 2264 glRect 0, 0, $self->{w}, $self->{h};
2236 glVertex $self->{w}, 0;
2237 glEnd;
2238 glDisable GL_BLEND; 2265 glDisable GL_BLEND;
2239 2266
2240 $self->SUPER::_draw; 2267 $self->SUPER::_draw;
2241 2268
2242 #TODO: force update every cursor change :( 2269 #TODO: force update every cursor change :(
2249 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2276 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2250 } 2277 }
2251 2278
2252 glColor_premultiply @{$self->{active_fg}}; 2279 glColor_premultiply @{$self->{active_fg}};
2253 glBegin GL_LINES; 2280 glBegin GL_LINES;
2254 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2281 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2255 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2282 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2256 glEnd; 2283 glEnd;
2257 2284
2258 glLineWidth 3; 2285 glLineWidth 3;
2259 glColor @{$self->{active_outline}}; 2286 glColor @{$self->{active_outline}};
2287 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2288 glLineWidth 1;
2289
2290 } else {
2291 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2260 glBegin GL_LINE_LOOP; 2292 glBegin GL_LINE_STRIP;
2261 glVertex 0, 0; 2293 glVertex .5, $self->{h} * .5;
2262 glVertex $self->{w} - 1, 0;
2263 glVertex $self->{w} - 1, $self->{h} - 1;
2264 glVertex 0, $self->{h} - 1; 2294 glVertex .5, $self->{h} - 2.5;
2295 glVertex $self->{w} - .5, $self->{h} - 2.5;
2296 glVertex $self->{w} - .5, $self->{h} * .5;
2265 glEnd; 2297 glEnd;
2266 glLineWidth 1;
2267
2268 } else {
2269 glColor @{$self->{outline}};
2270 glTranslate .375, .375;
2271 glBegin GL_LINE_STRIP;
2272 glVertex 0, $self->{h} * .5;
2273 glVertex 0, $self->{h} - 3;
2274 glVertex $self->{w} - 1, $self->{h} - 3;
2275 glVertex $self->{w} - 1, $self->{h} * .5;
2276 glEnd;
2277 } 2298 }
2278} 2299}
2279 2300
2280############################################################################# 2301#############################################################################
2281 2302
2282package CFPlus::UI::Entry; 2303package DC::UI::Entry;
2283 2304
2284our @ISA = CFPlus::UI::EntryBase::; 2305our @ISA = DC::UI::EntryBase::;
2285 2306
2286use CFPlus::OpenGL; 2307use DC::OpenGL;
2308
2309sub new {
2310 my $class = shift;
2311
2312 $class->SUPER::new (
2313 history_pointer => -1,
2314 @_
2315 )
2316}
2317
2287 2318
2288sub invoke_key_down { 2319sub invoke_key_down {
2289 my ($self, $ev) = @_; 2320 my ($self, $ev) = @_;
2290 2321
2291 my $sym = $ev->{sym}; 2322 my $sym = $ev->{sym};
2297 $self->{history_pointer} = -1; 2328 $self->{history_pointer} = -1;
2298 $self->{history_saveback} = ''; 2329 $self->{history_saveback} = '';
2299 $self->emit (activate => $txt); 2330 $self->emit (activate => $txt);
2300 $self->update; 2331 $self->update;
2301 2332
2302 } elsif ($sym == CFPlus::SDLK_UP) { 2333 } elsif ($sym == DC::SDLK_UP) {
2303 if ($self->{history_pointer} < 0) { 2334 if ($self->{history_pointer} < 0) {
2304 $self->{history_saveback} = $self->get_text; 2335 $self->{history_saveback} = $self->get_text;
2305 } 2336 }
2306 if (@{$self->{history} || []} > 0) { 2337 if (@{$self->{history} || []} > 0) {
2307 $self->{history_pointer}++; 2338 $self->{history_pointer}++;
2309 $self->{history_pointer} = @{$self->{history} || []} - 1; 2340 $self->{history_pointer} = @{$self->{history} || []} - 1;
2310 } 2341 }
2311 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2312 } 2343 }
2313 2344
2314 } elsif ($sym == CFPlus::SDLK_DOWN) { 2345 } elsif ($sym == DC::SDLK_DOWN) {
2315 $self->{history_pointer}--; 2346 $self->{history_pointer}--;
2316 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2347 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2317 2348
2318 if ($self->{history_pointer} >= 0) { 2349 if ($self->{history_pointer} >= 0) {
2319 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2350 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2320 } else { 2351 } else {
2352 if (defined $self->{history_saveback}) {
2321 $self->set_text ($self->{history_saveback}); 2353 $self->set_text ($self->{history_saveback});
2354 $self->{history_saveback} = undef;
2355 }
2322 } 2356 }
2323 2357
2324 } else { 2358 } else {
2325 return $self->SUPER::invoke_key_down ($ev) 2359 return $self->SUPER::invoke_key_down ($ev)
2326 } 2360 }
2328 1 2362 1
2329} 2363}
2330 2364
2331############################################################################# 2365#############################################################################
2332 2366
2333package CFPlus::UI::TextEdit; 2367package DC::UI::TextEdit;
2334 2368
2335our @ISA = CFPlus::UI::EntryBase::; 2369our @ISA = DC::UI::EntryBase::;
2336 2370
2337use CFPlus::OpenGL; 2371use DC::OpenGL;
2372
2373sub new {
2374 my $class = shift;
2375
2376 $class->SUPER::new (
2377 padding_y => 4,
2378
2379 @_
2380 )
2381}
2338 2382
2339sub move_cursor_ver { 2383sub move_cursor_ver {
2340 my ($self, $dy) = @_; 2384 my ($self, $dy) = @_;
2341 2385
2342 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2386 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2343 2387
2344 $y += $dy; 2388 $line += $dy;
2345 2389
2346 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2390 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2347 $self->{cursor} = $index; 2391 $self->{cursor} = $index;
2348 delete $self->{cur_h}; 2392 delete $self->{cur_h};
2349 $self->update; 2393 $self->update;
2350 return; 2394 return;
2351 } 2395 }
2354sub invoke_key_down { 2398sub invoke_key_down {
2355 my ($self, $ev) = @_; 2399 my ($self, $ev) = @_;
2356 2400
2357 my $sym = $ev->{sym}; 2401 my $sym = $ev->{sym};
2358 2402
2359 if ($sym == CFPlus::SDLK_UP) { 2403 if ($sym == DC::SDLK_UP) {
2360 $self->move_cursor_ver (-1); 2404 $self->move_cursor_ver (-1);
2361 } elsif ($sym == CFPlus::SDLK_DOWN) { 2405 } elsif ($sym == DC::SDLK_DOWN) {
2362 $self->move_cursor_ver (+1); 2406 $self->move_cursor_ver (+1);
2363 } else { 2407 } else {
2364 return $self->SUPER::invoke_key_down ($ev) 2408 return $self->SUPER::invoke_key_down ($ev)
2365 } 2409 }
2366 2410
2367 1 2411 1
2368} 2412}
2369 2413
2370############################################################################# 2414#############################################################################
2371 2415
2372package CFPlus::UI::ButtonBin; 2416package DC::UI::ButtonBin;
2373 2417
2374our @ISA = CFPlus::UI::Bin::; 2418our @ISA = DC::UI::Bin::;
2375 2419
2376use CFPlus::OpenGL; 2420use DC::OpenGL;
2377 2421
2378my @tex = 2422my @tex =
2379 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2423 map { new_from_resource DC::Texture $_, mipmap => 1 }
2380 qw(b1_button_inactive.png b1_button_active.png); 2424 qw(b1_button_inactive.png b1_button_active.png);
2381 2425
2382sub new { 2426sub new {
2383 my $class = shift; 2427 my $class = shift;
2384 2428
2385 $class->SUPER::new ( 2429 $class->SUPER::new (
2386 can_hover => 1, 2430 can_hover => 1,
2387 align => 0, 2431 align => 0.5,
2388 valign => 0, 2432 valign => 0.5,
2389 can_events => 1, 2433 can_events => 1,
2390 @_ 2434 @_
2391 ) 2435 )
2392} 2436}
2393 2437
2416 $self->SUPER::_draw; 2460 $self->SUPER::_draw;
2417} 2461}
2418 2462
2419############################################################################# 2463#############################################################################
2420 2464
2421package CFPlus::UI::Button; 2465package DC::UI::Button;
2422 2466
2423our @ISA = CFPlus::UI::Label::; 2467our @ISA = DC::UI::Label::;
2424 2468
2425use CFPlus::OpenGL; 2469use DC::OpenGL;
2426 2470
2427my @tex = 2471my @tex =
2428 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2472 map { new_from_resource DC::Texture $_, mipmap => 1 }
2429 qw(b1_button_inactive.png b1_button_active.png); 2473 qw(b1_button_inactive.png b1_button_active.png);
2430 2474
2431sub new { 2475sub new {
2432 my $class = shift; 2476 my $class = shift;
2433 2477
2434 $class->SUPER::new ( 2478 $class->SUPER::new (
2435 padding_x => 4, 2479 padding_x => 8,
2436 padding_y => 4, 2480 padding_y => 4,
2437 fg => [1.0, 1.0, 1.0], 2481 fg => [1.0, 1.0, 1.0],
2438 active_fg => [0.8, 0.8, 0.8], 2482 active_fg => [0.8, 0.8, 0.8],
2439 can_hover => 1, 2483 can_hover => 1,
2440 align => 0, 2484 align => 0.5,
2441 valign => 0, 2485 valign => 0.5,
2442 can_events => 1, 2486 can_events => 1,
2443 @_ 2487 @_
2444 ) 2488 )
2445} 2489}
2446 2490
2471 $self->SUPER::_draw; 2515 $self->SUPER::_draw;
2472} 2516}
2473 2517
2474############################################################################# 2518#############################################################################
2475 2519
2476package CFPlus::UI::CheckBox; 2520package DC::UI::CheckBox;
2477 2521
2478our @ISA = CFPlus::UI::DrawBG::; 2522our @ISA = DC::UI::DrawBG::;
2479 2523
2480my @tex = 2524my @tex =
2481 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2525 map { new_from_resource DC::Texture $_, mipmap => 1 }
2482 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2526 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2483 2527
2484use CFPlus::OpenGL; 2528use DC::OpenGL;
2485 2529
2486sub new { 2530sub new {
2487 my $class = shift; 2531 my $class = shift;
2488 2532
2489 $class->SUPER::new ( 2533 $class->SUPER::new (
2534 fontsize => 1,
2490 padding_x => 2, 2535 padding_x => 2,
2491 padding_y => 2, 2536 padding_y => 2,
2492 fg => [1, 1, 1], 2537 fg => [1, 1, 1],
2493 active_fg => [1, 1, 0], 2538 active_fg => [1, 1, 0],
2494 bg => [0, 0, 0, 0.2], 2539 bg => [0, 0, 0, 0.2],
2500} 2545}
2501 2546
2502sub size_request { 2547sub size_request {
2503 my ($self) = @_; 2548 my ($self) = @_;
2504 2549
2505 (6) x 2 2550 ($self->{fontsize} * $::FONTSIZE) x 2
2506} 2551}
2507 2552
2508sub toggle { 2553sub toggle {
2509 my ($self) = @_; 2554 my ($self) = @_;
2510 2555
2529sub _draw { 2574sub _draw {
2530 my ($self) = @_; 2575 my ($self) = @_;
2531 2576
2532 $self->SUPER::_draw; 2577 $self->SUPER::_draw;
2533 2578
2534 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2579 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2535 2580
2536 my ($w, $h) = @$self{qw(w h)}; 2581 my ($w, $h) = @$self{qw(w h)};
2537 2582
2538 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2583 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2539 2584
2546 glDisable GL_TEXTURE_2D; 2591 glDisable GL_TEXTURE_2D;
2547} 2592}
2548 2593
2549############################################################################# 2594#############################################################################
2550 2595
2551package CFPlus::UI::Image; 2596package DC::UI::Image;
2552 2597
2553our @ISA = CFPlus::UI::Base::; 2598our @ISA = DC::UI::DrawBG::;
2554 2599
2555use CFPlus::OpenGL; 2600use DC::OpenGL;
2556 2601
2557our %texture_cache; 2602our %texture_cache;
2558 2603
2559sub new { 2604sub new {
2560 my $class = shift; 2605 my $class = shift;
2567 2612
2568 $self->{path} || $self->{tex} 2613 $self->{path} || $self->{tex}
2569 or Carp::croak "'path' or 'tex' attributes required"; 2614 or Carp::croak "'path' or 'tex' attributes required";
2570 2615
2571 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2616 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2572 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2617 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2573 2618
2574 CFPlus::weaken $texture_cache{$self->{path}}; 2619 DC::weaken $texture_cache{$self->{path}};
2575 2620
2576 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2621 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2577 2622
2578 $self 2623 $self
2579} 2624}
2580 2625
2581sub STORABLE_freeze { 2626sub STORABLE_freeze {
2582 my ($self, $cloning) = @_; 2627 my ($self, $cloning) = @_;
2583 2628
2584 $self->{path} 2629 $self->{path}
2585 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2630 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2586 2631
2587 $self->{path} 2632 $self->{path}
2588} 2633}
2589 2634
2590sub STORABLE_attach { 2635sub STORABLE_attach {
2591 my ($self, $cloning, $path) = @_; 2636 my ($self, $cloning, $path) = @_;
2592 2637
2593 $self->new (path => $path) 2638 $self->new (path => $path)
2594} 2639}
2595 2640
2641sub set_texture {
2642 my ($self, $tex) = @_;
2643
2644 $self->{tex} = $tex;
2645 $self->update;
2646}
2647
2596sub size_request { 2648sub size_request {
2597 my ($self) = @_; 2649 my ($self) = @_;
2598 2650
2599 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2651 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2600} 2652}
2601 2653
2602sub _draw { 2654sub _draw {
2603 my ($self) = @_; 2655 my ($self) = @_;
2656
2657 $self->SUPER::_draw;
2604 2658
2605 my $tex = $self->{tex}; 2659 my $tex = $self->{tex};
2606 2660
2607 my ($w, $h) = ($self->{w}, $self->{h}); 2661 my ($w, $h) = ($self->{w}, $self->{h});
2608 2662
2621 glDisable GL_TEXTURE_2D; 2675 glDisable GL_TEXTURE_2D;
2622} 2676}
2623 2677
2624############################################################################# 2678#############################################################################
2625 2679
2626package CFPlus::UI::ImageButton; 2680package DC::UI::ImageButton;
2627 2681
2628our @ISA = CFPlus::UI::Image::; 2682our @ISA = DC::UI::Image::;
2629 2683
2630use CFPlus::OpenGL; 2684use DC::OpenGL;
2631
2632my %textures;
2633 2685
2634sub new { 2686sub new {
2635 my $class = shift; 2687 my $class = shift;
2636 2688
2637 my $self = $class->SUPER::new ( 2689 my $self = $class->SUPER::new (
2638 padding_x => 4, 2690 padding_x => 4,
2639 padding_y => 4, 2691 padding_y => 4,
2640 fg => [1, 1, 1], 2692 fg => [1, 1, 1],
2641 active_fg => [0, 0, 1], 2693 active_fg => [0, 0, 1],
2642 can_hover => 1, 2694 can_hover => 1,
2643 align => 0, 2695 align => 0.5,
2644 valign => 0, 2696 valign => 0.5,
2645 can_events => 1, 2697 can_events => 1,
2646 @_ 2698 @_
2647 ); 2699 );
2648} 2700}
2649 2701
2702sub invoke_button_down {
2703 my ($self, $ev, $x, $y) = @_;
2704
2705 1
2706}
2707
2650sub invoke_button_up { 2708sub invoke_button_up {
2651 my ($self, $ev, $x, $y) = @_; 2709 my ($self, $ev, $x, $y) = @_;
2652 2710
2653 $self->emit ("activate") 2711 $self->emit ("activate")
2654 if $x >= 0 && $x < $self->{w} 2712 if $x >= 0 && $x < $self->{w}
2657 1 2715 1
2658} 2716}
2659 2717
2660############################################################################# 2718#############################################################################
2661 2719
2662package CFPlus::UI::VGauge; 2720package DC::UI::VGauge;
2663 2721
2664our @ISA = CFPlus::UI::Base::; 2722our @ISA = DC::UI::Base::;
2665 2723
2666use List::Util qw(min max); 2724use List::Util qw(min max);
2667 2725
2668use CFPlus::OpenGL; 2726use DC::OpenGL;
2669 2727
2670my %tex = ( 2728my %tex = (
2671 food => [ 2729 food => [
2672 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2730 map { new_from_resource DC::Texture $_, mipmap => 1 }
2673 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2731 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2674 ], 2732 ],
2675 grace => [ 2733 grace => [
2676 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2677 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2735 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2678 ], 2736 ],
2679 hp => [ 2737 hp => [
2680 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2739 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2682 ], 2740 ],
2683 mana => [ 2741 mana => [
2684 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2685 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2743 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2686 ], 2744 ],
2687); 2745);
2688 2746
2689# eg. VGauge->new (gauge => 'food'), default gauge: food 2747# eg. VGauge->new (gauge => 'food'), default gauge: food
2792 glDisable GL_TEXTURE_2D; 2850 glDisable GL_TEXTURE_2D;
2793} 2851}
2794 2852
2795############################################################################# 2853#############################################################################
2796 2854
2855package DC::UI::Progress;
2856
2857our @ISA = DC::UI::Label::;
2858
2859use DC::OpenGL;
2860
2861sub new {
2862 my ($class, %arg) = @_;
2863
2864 my $self = $class->SUPER::new (
2865 padding_x => 2,
2866 padding_y => 2,
2867 fg => [1, 1, 1],
2868 bg => [0, 0, 1, 0.2],
2869 bar => [0.7, 0.5, 0.1, 0.8],
2870 outline => [0.4, 0.3, 0],
2871 fontsize => 0.9,
2872 valign => 0.5,
2873 align => 0.5,
2874 can_events => 1,
2875 ellipsise => 1,
2876 label => "%d%%",
2877 %arg,
2878 );
2879
2880 $self->set_value ($arg{value} || -1);
2881
2882 $self
2883}
2884
2885sub set_label {
2886 my ($self, $label) = @_;
2887
2888 return if $self->{label} eq $label;
2889 $self->{label} = $label;
2890
2891 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2892}
2893
2894sub set_value {
2895 my ($self, $value) = @_;
2896
2897 if ($self->{value} ne $value) {
2898 $self->{value} = $value;
2899
2900 if ($value < 0) {
2901 $self->set_text ("-");
2902 } else {
2903 $self->set_text (sprintf $self->{label}, $value * 100);
2904 }
2905
2906 $self->update;
2907 }
2908}
2909
2910sub _draw {
2911 my ($self) = @_;
2912
2913 glEnable GL_BLEND;
2914 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2915
2916 my $px = $self->{padding_x};
2917 my $py = $self->{padding_y};
2918
2919 if ($self->{value} >= 0) {
2920 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2921
2922 glColor_premultiply @{$self->{bar}};
2923 glRect $px, $py, $s, $self->{h} - $py;
2924 glColor_premultiply @{$self->{bg}};
2925 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2926 }
2927
2928 glColor_premultiply @{$self->{outline}};
2929
2930 $px -= .5;
2931 $py -= .5;
2932
2933 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2934
2935 glDisable GL_BLEND;
2936
2937 {
2938 local $self->{bg}; # do not draw background
2939 $self->SUPER::_draw;
2940 }
2941}
2942
2943#############################################################################
2944
2945package DC::UI::ExperienceProgress;
2946
2947our @ISA = DC::UI::Progress::;
2948
2949sub new {
2950 my ($class, %arg) = @_;
2951
2952 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2953
2954 my $self = $class->SUPER::new (
2955 %arg,
2956 tooltip => sub {
2957 my ($self) = @_;
2958
2959 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2960 $tt,
2961 $self->{lvl},
2962 ::formsep ($self->{exp}),
2963 ::formsep ($self->{nxt}),
2964 ::formsep ($self->{nxt} - $self->{exp}),
2965 $self->_percent * 100,
2966 },
2967 );
2968
2969 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2970 if $::CONN;
2971
2972 $self
2973}
2974
2975sub DESTROY {
2976 my ($self) = @_;
2977
2978 delete $::CONN->{on_exp_update}{$self+0}
2979 if $::CONN;
2980
2981 $self->SUPER::DESTROY;
2982}
2983
2984sub _percent {
2985 my ($self) = @_;
2986
2987 my $table = $::CONN && $::CONN->{exp_table}
2988 or return -1;
2989
2990 my $l0 = $table->[$self->{lvl} - 1];
2991 my $l1 = $table->[$self->{lvl}];
2992
2993 $self->{nxt} = $l1;
2994
2995 ($self->{exp} - $l0) / ($l1 - $l0)
2996}
2997
2998sub set_value {
2999 my ($self, $lvl, $exp) = @_;
3000
3001 $self->{lvl} = $lvl;
3002 $self->{exp} = $exp;
3003
3004 $self->SUPER::set_value ($self->_percent);
3005}
3006
3007#############################################################################
3008
2797package CFPlus::UI::Gauge; 3009package DC::UI::Gauge;
2798 3010
2799our @ISA = CFPlus::UI::VBox::; 3011our @ISA = DC::UI::VBox::;
2800 3012
2801sub new { 3013sub new {
2802 my ($class, %arg) = @_; 3014 my ($class, %arg) = @_;
2803 3015
2804 my $self = $class->SUPER::new ( 3016 my $self = $class->SUPER::new (
2806 can_hover => 1, 3018 can_hover => 1,
2807 can_events => 1, 3019 can_events => 1,
2808 %arg, 3020 %arg,
2809 ); 3021 );
2810 3022
2811 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3023 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2812 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3024 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2813 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3025 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2814 3026
2815 $self 3027 $self
2816} 3028}
2817 3029
2818sub set_fontsize { 3030sub set_fontsize {
2839 $self->{value}->set_text ($val); 3051 $self->{value}->set_text ($val);
2840} 3052}
2841 3053
2842############################################################################# 3054#############################################################################
2843 3055
2844package CFPlus::UI::Slider; 3056package DC::UI::Slider;
2845 3057
2846use strict; 3058use strict;
2847 3059
2848use CFPlus::OpenGL; 3060use DC::OpenGL;
2849 3061
2850our @ISA = CFPlus::UI::DrawBG::; 3062our @ISA = DC::UI::DrawBG::;
2851 3063
2852my @tex = 3064my @tex =
2853 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3065 map { new_from_resource DC::Texture $_ }
2854 qw(s1_slider.png s1_slider_bg.png); 3066 qw(s1_slider.png s1_slider_bg.png);
2855 3067
2856sub new { 3068sub new {
2857 my $class = shift; 3069 my $class = shift;
2858 3070
2895sub set_value { 3107sub set_value {
2896 my ($self, $value) = @_; 3108 my ($self, $value) = @_;
2897 3109
2898 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2899 3111
2900 $hi = $lo + 1 if $hi <= $lo; 3112 $hi = $lo if $hi < $lo;
2901 3113
2902 $page = $hi - $lo if $page > $hi - $lo; 3114 $value = $hi - $page if $value > $hi - $page;
2903
2904 $value = $lo if $value < $lo; 3115 $value = $lo if $value < $lo;
2905 $value = $hi - $page if $value > $hi - $page;
2906 3116
2907 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3117 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2908 if $unit; 3118 if $unit;
2909 3119
2910 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3120 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2954sub invoke_mouse_wheel { 3164sub invoke_mouse_wheel {
2955 my ($self, $ev) = @_; 3165 my ($self, $ev) = @_;
2956 3166
2957 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3167 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2958 3168
2959 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3169 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2960 3170
2961 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3171 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2962 3172
2963 1 3173 1
2964} 3174}
2974 my ($self) = @_; 3184 my ($self) = @_;
2975 3185
2976 unless ($self->{knob_w}) { 3186 unless ($self->{knob_w}) {
2977 $self->set_value ($self->{range}[0]); 3187 $self->set_value ($self->{range}[0]);
2978 3188
2979 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3189 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2980 my $range = ($hi - $page - $lo) || 1e-100; 3190 my $range = ($hi - $page - $lo) || 1e-10;
2981 3191
2982 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3192 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
2983 3193
2984 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3194 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2985 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3195 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2986 3196
2987 $value = ($value - $lo) / $range; 3197 $value = ($value - $lo) / $range;
3017 glDisable GL_TEXTURE_2D; 3227 glDisable GL_TEXTURE_2D;
3018} 3228}
3019 3229
3020############################################################################# 3230#############################################################################
3021 3231
3022package CFPlus::UI::ValSlider; 3232package DC::UI::ValSlider;
3023 3233
3024our @ISA = CFPlus::UI::HBox::; 3234our @ISA = DC::UI::HBox::;
3025 3235
3026sub new { 3236sub new {
3027 my ($class, %arg) = @_; 3237 my ($class, %arg) = @_;
3028 3238
3029 my $range = delete $arg{range}; 3239 my $range = delete $arg{range};
3030 3240
3031 my $self = $class->SUPER::new ( 3241 my $self = $class->SUPER::new (
3032 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3242 slider => (new DC::UI::Slider expand => 1, range => $range),
3033 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3243 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3034 to_value => sub { shift }, 3244 to_value => sub { shift },
3035 from_value => sub { shift }, 3245 from_value => sub { shift },
3036 %arg, 3246 %arg,
3037 ); 3247 );
3038 3248
3058sub set_range { shift->{slider}->set_range (@_) } 3268sub set_range { shift->{slider}->set_range (@_) }
3059sub set_value { shift->{slider}->set_value (@_) } 3269sub set_value { shift->{slider}->set_value (@_) }
3060 3270
3061############################################################################# 3271#############################################################################
3062 3272
3063package CFPlus::UI::TextScroller; 3273package DC::UI::TextScroller;
3064 3274
3065our @ISA = CFPlus::UI::HBox::; 3275our @ISA = DC::UI::HBox::;
3066 3276
3067use CFPlus::OpenGL; 3277use DC::OpenGL;
3068 3278
3069sub new { 3279sub new {
3070 my $class = shift; 3280 my $class = shift;
3071 3281
3072 my $self = $class->SUPER::new ( 3282 my $self = $class->SUPER::new (
3074 can_events => 1, 3284 can_events => 1,
3075 indent => 0, 3285 indent => 0,
3076 #font => default_font 3286 #font => default_font
3077 @_, 3287 @_,
3078 3288
3079 layout => (new CFPlus::Layout), 3289 layout => (new DC::Layout),
3080 par => [], 3290 par => [],
3081 max_par => 0, 3291 max_par => 0,
3082 height => 0, 3292 height => 0,
3083 children => [ 3293 children => [
3084 (new CFPlus::UI::Empty expand => 1), 3294 (new DC::UI::Empty expand => 1),
3085 (new CFPlus::UI::Slider vertical => 1), 3295 (new DC::UI::Slider vertical => 1),
3086 ], 3296 ],
3087 ); 3297 );
3088 3298
3089 $self->{children}[1]->connect (changed => sub { $self->update }); 3299 $self->{children}[1]->connect (changed => sub { $self->update });
3090 3300
3146 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3356 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3147 $layout->set_markup ($para->{markup}); 3357 $layout->set_markup ($para->{markup});
3148 3358
3149 $layout->set_shapes ( 3359 $layout->set_shapes (
3150 map 3360 map
3151 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3361 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3152 @{$para->{widget}} 3362 @{$para->{widget}}
3153 ); 3363 );
3154 3364
3155 $layout 3365 $layout
3156} 3366}
3273 $ROOT->on_post_alloc ($self => sub { 3483 $ROOT->on_post_alloc ($self => sub {
3274 $self->force_uptodate; 3484 $self->force_uptodate;
3275 3485
3276 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3486 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3277 3487
3278 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3488 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3279 glClearColor 0, 0, 0, 0; 3489 glClearColor 0, 0, 0, 0;
3280 glClear GL_COLOR_BUFFER_BIT; 3490 glClear GL_COLOR_BUFFER_BIT;
3281 3491
3282 package CFPlus::UI::Base; 3492 package DC::UI::Base;
3283 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3493 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3284 (0, 0, $self->{w}, $self->{h}); 3494 (0, 0, $self->{w}, $self->{h});
3285 3495
3286 my $top = int $self->{children}[1]{range}[0]; 3496 my $top = int $self->{children}[1]{range}[0];
3287 3497
3298 3508
3299 if ($y0 < $y + $h && $y < $y1) { 3509 if ($y0 < $y + $h && $y < $y1) {
3300 my $layout = $self->get_layout ($para); 3510 my $layout = $self->get_layout ($para);
3301 3511
3302 $layout->render ($para->{indent}, $y - $y0); 3512 $layout->render ($para->{indent}, $y - $y0);
3513 $layout->draw;
3303 3514
3304 if (my @w = @{ $para->{widget} }) { 3515 if (my @w = @{ $para->{widget} }) {
3305 my @s = $layout->get_shapes; 3516 my @s = $layout->get_shapes;
3306 3517
3307 for (@w) { 3518 for (@w) {
3345 $self->{children}[1]->draw; 3556 $self->{children}[1]->draw;
3346} 3557}
3347 3558
3348############################################################################# 3559#############################################################################
3349 3560
3350package CFPlus::UI::Animator; 3561package DC::UI::Animator;
3351 3562
3352use CFPlus::OpenGL; 3563use DC::OpenGL;
3353 3564
3354our @ISA = CFPlus::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3355 3566
3356sub moveto { 3567sub moveto {
3357 my ($self, $x, $y) = @_; 3568 my ($self, $x, $y) = @_;
3358 3569
3359 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3570 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3387 glPopMatrix; 3598 glPopMatrix;
3388} 3599}
3389 3600
3390############################################################################# 3601#############################################################################
3391 3602
3392package CFPlus::UI::Flopper; 3603package DC::UI::Flopper;
3393 3604
3394our @ISA = CFPlus::UI::Button::; 3605our @ISA = DC::UI::Button::;
3395 3606
3396sub new { 3607sub new {
3397 my $class = shift; 3608 my $class = shift;
3398 3609
3399 my $self = $class->SUPER::new ( 3610 my $self = $class->SUPER::new (
3411 $self->{other}->toggle_visibility; 3622 $self->{other}->toggle_visibility;
3412} 3623}
3413 3624
3414############################################################################# 3625#############################################################################
3415 3626
3416package CFPlus::UI::Tooltip; 3627package DC::UI::Tooltip;
3417 3628
3418our @ISA = CFPlus::UI::Bin::; 3629our @ISA = DC::UI::Bin::;
3419 3630
3420use CFPlus::OpenGL; 3631use DC::OpenGL;
3421 3632
3422sub new { 3633sub new {
3423 my $class = shift; 3634 my $class = shift;
3424 3635
3425 $class->SUPER::new ( 3636 $class->SUPER::new (
3429} 3640}
3430 3641
3431sub set_tooltip_from { 3642sub set_tooltip_from {
3432 my ($self, $widget) = @_; 3643 my ($self, $widget) = @_;
3433 3644
3434 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3435 if $widget->{tooltip} =~ /^#(.*)$/;
3436
3437 my $tooltip = $widget->{tooltip}; 3645 my $tip = $widget->{tooltip};
3646 $tip = $tip->($widget) if "CODE" eq ref $tip;
3647
3648 $tip = DC::Pod::section_label tooltip => $1
3649 if $tip =~ /^#(.*)$/;
3438 3650
3439 if ($ENV{CFPLUS_DEBUG} & 2) { 3651 if ($ENV{CFPLUS_DEBUG} & 2) {
3440 $tooltip .= "\n\n" . (ref $widget) . "\n" 3652 $tip .= "\n\n" . (ref $widget) . "\n"
3441 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3653 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3442 . "req $widget->{req_w} $widget->{req_h}\n" 3654 . "req $widget->{req_w} $widget->{req_h}\n"
3443 . "visible $widget->{visible}"; 3655 . "visible $widget->{visible}";
3444 } 3656 }
3445 3657
3446 $tooltip =~ s/^\n+//; 3658 $tip =~ s/^\n+//;
3447 $tooltip =~ s/\n+$//; 3659 $tip =~ s/\n+$//;
3448 3660
3449 $self->add (new CFPlus::UI::Label 3661 $self->add (new DC::UI::Label
3662 fg => $DC::THEME{tooltip_fg},
3450 markup => $tooltip, 3663 markup => $tip,
3451 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3664 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3665 align => 0,
3452 fontsize => 0.8, 3666 fontsize => 0.8,
3453 style => 1, # FLAG_INVERSE 3667 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3454 ellipsise => 0, 3668 ellipsise => 0,
3455 font => ($widget->{tooltip_font} || $::FONT_PROP), 3669 font => ($widget->{tooltip_font} || $::FONT_PROP),
3456 ); 3670 );
3457} 3671}
3458 3672
3493} 3707}
3494 3708
3495sub _draw { 3709sub _draw {
3496 my ($self) = @_; 3710 my ($self) = @_;
3497 3711
3498 glTranslate 0.375, 0.375;
3499
3500 my ($w, $h) = @$self{qw(w h)}; 3712 my ($w, $h) = @$self{qw(w h)};
3501 3713
3502 glColor 1, 0.8, 0.4; 3714 glColor @{ $DC::THEME{tooltip_bg} };
3503 glRect 0, 0, $w, $h; 3715 glRect 0, 0, $w, $h;
3504 3716
3505 glColor 0, 0, 0; 3717 glColor @{ $DC::THEME{tooltip_border} };
3506 glBegin GL_LINE_LOOP; 3718 glRect_lineloop .5, .5, $w + .5, $h + .5;
3507 glVertex 0 , 0;
3508 glVertex 0 , $h;
3509 glVertex $w, $h;
3510 glVertex $w, 0;
3511 glEnd;
3512 3719
3513 glTranslate 2 - 0.375, 2 - 0.375; 3720 glTranslate 2, 2;
3514 3721
3515 $self->SUPER::_draw; 3722 $self->SUPER::_draw;
3516} 3723}
3517 3724
3518############################################################################# 3725#############################################################################
3519 3726
3520package CFPlus::UI::Face; 3727package DC::UI::Face;
3521 3728
3522our @ISA = CFPlus::UI::DrawBG::; 3729our @ISA = DC::UI::DrawBG::;
3523 3730
3524use CFPlus::OpenGL; 3731use DC::OpenGL;
3525 3732
3526sub new { 3733sub new {
3527 my $class = shift; 3734 my $class = shift;
3528 3735
3529 my $self = $class->SUPER::new ( 3736 my $self = $class->SUPER::new (
3532 aspect => 1, 3739 aspect => 1,
3533 can_events => 0, 3740 can_events => 0,
3534 @_, 3741 @_,
3535 ); 3742 );
3536 3743
3537 if ($self->{anim} && $self->{animspeed}) {
3538 CFPlus::weaken (my $widget = $self);
3539
3540 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3541 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3542 $self->{timer} = Event->timer (
3543 parked => 1,
3544 cb => sub {
3545 return unless $::CONN && $widget;
3546
3547 ++$widget->{frame};
3548 $widget->update_face;
3549 $widget->update;
3550
3551 $widget->update_timer;
3552 },
3553 );
3554
3555 $self->update_face;
3556 $self->update_timer; 3744 $self->update_anim;
3557 }
3558 3745
3559 $self 3746 $self
3560} 3747}
3561 3748
3562sub update_timer { 3749sub update_timer {
3563 my ($self) = @_; 3750 my ($self) = @_;
3564 3751
3565 return unless $self->{timer}; 3752 return unless $self->{timer};
3566 3753
3567 if ($self->{visible}) { 3754 if ($self->{visible}) {
3568 $self->{timer}->at (
3569 $self->{anim_start}
3570 + $self->{animspeed}
3571 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3572 );
3573 $self->{timer}->start; 3755 $self->{timer}->start;
3574 } else { 3756 } else {
3575 $self->{timer}->stop; 3757 $self->{timer}->stop;
3576 } 3758 }
3577} 3759}
3578 3760
3579sub update_face { 3761sub update_face {
3580 my ($self) = @_; 3762 my ($self) = @_;
3581 3763
3582 return unless $::CONN;
3583
3584 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3585 if ($anim && @$anim) {
3586 delete $self->{wait_face};
3587 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3588 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3589 }
3590 }
3591}
3592
3593sub size_request {
3594 my ($self) = @_;
3595
3596 if ($::CONN) { 3764 if ($::CONN) {
3597 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3598 if (my $tex = $::CONN->{texture}[$faceid]) { 3766 if ($anim && @$anim) {
3599 $self->{tex} = $tex; 3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3600 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3768 delete $self->{face_change_cb};
3601 } else { 3769
3602 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3770 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3603 $self->realloc; 3771 unless ($tex->{name} || $tex->{loading}) {
3772 $tex->upload (sub { $self->reconfigure });
3773 }
3604 }); 3774 }
3605 } 3775 }
3606 } 3776 }
3607 } 3777 }
3778}
3779
3780sub update_anim {
3781 my ($self) = @_;
3782
3783 if ($self->{anim} && $self->{animspeed}) {
3784 DC::weaken (my $widget = $self);
3785
3786 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3787 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3788 return unless $::CONN;
3789
3790 my $w = $widget
3791 or return;
3792
3793 ++$w->{frame};
3794 $w->update_face;
3795
3796 # somehow, $widget can go away
3797 $w->update;
3798 $w->update_timer;
3799 };
3800
3801 $self->update_face;
3802 $self->update_timer;
3803 } else {
3804 delete $self->{timer};
3805 }
3806}
3807
3808sub size_request {
3809 my ($self) = @_;
3810
3811 if ($::CONN) {
3812 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3813 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3814 if ($tex->{name}) {
3815 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3816 } elsif (!$tex->{loading}) {
3817 $tex->upload (sub { $self->reconfigure });
3818 }
3819 }
3820
3821 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3822 }
3823 }
3608 3824
3609 ($self->{size_w} || 8, $self->{size_h} || 8) 3825 ($self->{size_w} || 8, $self->{size_h} || 8)
3610} 3826}
3611 3827
3612sub update { 3828sub update {
3613 my ($self) = @_; 3829 my ($self) = @_;
3614 3830
3615 return unless $self->{visible}; 3831 return unless $self->{visible};
3616 3832
3617 $self->SUPER::update; 3833 $self->SUPER::update;
3834}
3835
3836sub set_face {
3837 my ($self, $face) = @_;
3838
3839 $self->{face} = $face;
3840 $self->reconfigure;
3841}
3842
3843sub set_anim {
3844 my ($self, $anim) = @_;
3845
3846 $self->{anim} = $anim;
3847 $self->update_anim;
3848}
3849
3850sub set_animspeed {
3851 my ($self, $animspeed) = @_;
3852
3853 $self->{animspeed} = $animspeed;
3854 $self->update_anim;
3618} 3855}
3619 3856
3620sub invoke_visibility_change { 3857sub invoke_visibility_change {
3621 my ($self) = @_; 3858 my ($self) = @_;
3622 3859
3648 $self->SUPER::destroy; 3885 $self->SUPER::destroy;
3649} 3886}
3650 3887
3651############################################################################# 3888#############################################################################
3652 3889
3653package CFPlus::UI::Buttonbar; 3890package DC::UI::Buttonbar;
3654 3891
3655our @ISA = CFPlus::UI::HBox::; 3892our @ISA = DC::UI::HBox::;
3656 3893
3657# TODO: should actually wrap buttons and other goodies. 3894# TODO: should actually wrap buttons and other goodies.
3658 3895
3659############################################################################# 3896#############################################################################
3660 3897
3661package CFPlus::UI::Menu; 3898package DC::UI::Menu;
3662 3899
3663our @ISA = CFPlus::UI::Toplevel::; 3900our @ISA = DC::UI::Toplevel::;
3664 3901
3665use CFPlus::OpenGL; 3902use DC::OpenGL;
3666 3903
3667sub new { 3904sub new {
3668 my $class = shift; 3905 my $class = shift;
3669 3906
3670 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3671 items => [], 3908 items => [],
3672 z => 100, 3909 z => 100,
3673 @_, 3910 @_,
3674 ); 3911 );
3675 3912
3676 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3913 $self->add ($self->{vbox} = new DC::UI::VBox);
3677 3914
3678 for my $item (@{ $self->{items} }) { 3915 for my $item (@{ $self->{items} }) {
3679 my ($widget, $cb, $tooltip) = @$item; 3916 my ($widget, $cb, $tooltip) = @$item;
3680 3917
3681 # handle various types of items, only text for now 3918 # handle various types of items, only text for now
3682 if (!ref $widget) { 3919 if (!ref $widget) {
3683 if ($widget =~ /\t/) { 3920 if ($widget =~ /\t/) {
3684 my ($left, $right) = split /\t/, $widget, 2; 3921 my ($left, $right) = split /\t/, $widget, 2;
3685 3922
3686 $widget = new CFPlus::UI::HBox 3923 $widget = new DC::UI::HBox
3687 can_hover => 1, 3924 can_hover => 1,
3688 can_events => 1, 3925 can_events => 1,
3689 tooltip => $tooltip, 3926 tooltip => $tooltip,
3690 children => [ 3927 children => [
3691 (new CFPlus::UI::Label markup => $left, expand => 1), 3928 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3692 (new CFPlus::UI::Label markup => $right, align => +1), 3929 (new DC::UI::Label markup => $right, align => 1),
3693 ], 3930 ],
3694 ; 3931 ;
3695 3932
3696 } else { 3933 } else {
3697 $widget = new CFPlus::UI::Label 3934 $widget = new DC::UI::Label
3698 can_hover => 1, 3935 can_hover => 1,
3699 can_events => 1, 3936 can_events => 1,
3937 align => 0,
3700 markup => $widget, 3938 markup => $widget,
3701 tooltip => $tooltip; 3939 tooltip => $tooltip;
3702 } 3940 }
3703 } 3941 }
3704 3942
3719 # maybe save $GRAB? must be careful about events... 3957 # maybe save $GRAB? must be careful about events...
3720 $GRAB = $self; 3958 $GRAB = $self;
3721 $self->{button} = $ev->{button}; 3959 $self->{button} = $ev->{button};
3722 3960
3723 $self->show; 3961 $self->show;
3724 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3962
3963 my $x = $ev->{x};
3964 my $y = $ev->{y};
3965
3966 $self->{root}->on_post_alloc ($self => sub {
3967 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3968 });
3969
3970 1 # so it can be used inside event handlers
3725} 3971}
3726 3972
3727sub invoke_mouse_motion { 3973sub invoke_mouse_motion {
3728 my ($self, $ev, $x, $y) = @_; 3974 my ($self, $ev, $x, $y) = @_;
3729 3975
3750 1 3996 1
3751} 3997}
3752 3998
3753############################################################################# 3999#############################################################################
3754 4000
3755package CFPlus::UI::Multiplexer; 4001package DC::UI::Multiplexer;
3756 4002
3757our @ISA = CFPlus::UI::Container::; 4003our @ISA = DC::UI::Container::;
3758 4004
3759sub new { 4005sub new {
3760 my $class = shift; 4006 my $class = shift;
3761 4007
3762 my $self = $class->SUPER::new ( 4008 my $self = $class->SUPER::new (
3763 @_, 4009 @_,
3764 ); 4010 );
3765 4011
3766 $self->{current} = $self->{children}[0] 4012 $self->set_current_page (0);
3767 if @{ $self->{children} };
3768 4013
3769 $self 4014 $self
3770} 4015}
3771 4016
3772sub add { 4017sub add {
3773 my ($self, @widgets) = @_; 4018 my ($self, @widgets) = @_;
3774 4019
3775 $self->SUPER::add (@widgets); 4020 $self->SUPER::add (@widgets);
3776 4021
3777 $self->{current} = $self->{children}[0] 4022 $self->set_current_page (0)
3778 if @{ $self->{children} }; 4023 if @widgets == @{ $self->{children} };
3779} 4024}
3780 4025
3781sub get_current_page { 4026sub get_current_page {
3782 my ($self) = @_; 4027 my ($self) = @_;
3783 4028
3789 4034
3790 my $widget = ref $page_or_widget 4035 my $widget = ref $page_or_widget
3791 ? $page_or_widget 4036 ? $page_or_widget
3792 : $self->{children}[$page_or_widget]; 4037 : $self->{children}[$page_or_widget];
3793 4038
4039 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4040
3794 $self->{current} = $widget; 4041 if (($self->{current} = $widget)) {
4042 $self->{current}->set_visible if $self->{current} && $self->{visible};
3795 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4043 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3796 4044
3797 $self->emit (page_changed => $self->{current}); 4045 $self->emit (page_changed => $self->{current});
4046 }
3798 4047
3799 $self->realloc; 4048 $self->realloc;
3800} 4049}
3801 4050
3802sub visible_children { 4051sub visible_children {
3803 $_[0]{current} 4052 $_[0]{current} || ()
3804} 4053}
3805 4054
3806sub size_request { 4055sub size_request {
3807 my ($self) = @_; 4056 my ($self) = @_;
3808 4057
4058 $self->{current}
3809 $self->{current}->size_request 4059 ? $self->{current}->size_request
4060 : (0, 0)
3810} 4061}
3811 4062
3812sub invoke_size_allocate { 4063sub invoke_size_allocate {
3813 my ($self, $w, $h) = @_; 4064 my ($self, $w, $h) = @_;
3814 4065
3815 $self->{current}->configure (0, 0, $w, $h); 4066 $self->{current}->configure (0, 0, $w, $h)
4067 if $self->{current};
3816 4068
3817 1 4069 1
3818} 4070}
3819 4071
3820sub _draw { 4072sub _draw {
3821 my ($self) = @_; 4073 my ($self) = @_;
3822 4074
3823 $self->{current}->draw; 4075 $self->{current}->draw
4076 if $self->{current};
3824} 4077}
3825 4078
3826############################################################################# 4079#############################################################################
3827 4080
3828package CFPlus::UI::Notebook; 4081package DC::UI::Notebook;
3829 4082
4083use DC::OpenGL;
4084
3830our @ISA = CFPlus::UI::VBox::; 4085our @ISA = DC::UI::VBox::;
3831 4086
3832sub new { 4087sub new {
3833 my $class = shift; 4088 my $class = shift;
3834 4089
3835 my $self = $class->SUPER::new ( 4090 my $self = $class->SUPER::new (
3836 buttonbar => (new CFPlus::UI::Buttonbar), 4091 buttonbar => (new DC::UI::Buttonbar),
3837 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4092 multiplexer => (new DC::UI::Multiplexer expand => 1),
4093 active_outline => [.7, .7, 0.2],
3838 # filter => # will be put between multiplexer and $self 4094 # filter => # will be put between multiplexer and $self
3839 @_, 4095 @_,
3840 ); 4096 );
3841 4097
3842 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4098 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3850 4106
3851 for my $child (@$widgets) { 4107 for my $child (@$widgets) {
3852 Scalar::Util::weaken $child; 4108 Scalar::Util::weaken $child;
3853 $child->{c_tab_} ||= do { 4109 $child->{c_tab_} ||= do {
3854 my $tab = 4110 my $tab =
3855 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4111 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3856 ? $child->{c_tab} 4112 ? $child->{c_tab}
3857 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4113 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3858 4114
3859 $tab->connect (activate => sub { 4115 $tab->connect (activate => sub {
3860 $wself->set_current_page ($child); 4116 $wself->set_current_page ($child);
3861 }); 4117 });
3862 4118
3894sub pages { 4150sub pages {
3895 my ($self) = @_; 4151 my ($self) = @_;
3896 $self->{multiplexer}->children 4152 $self->{multiplexer}->children
3897} 4153}
3898 4154
4155sub page_index {
4156 my ($self, $widget) = @_;
4157
4158 my $i = 0;
4159 for ($self->pages) {
4160 if ($_ eq $widget) { return $i };
4161 $i++;
4162 }
4163
4164 undef
4165}
4166
3899sub add_tab { 4167sub add_tab {
3900 my ($self, $title, $widget, $tooltip) = @_; 4168 my ($self, $title, $widget, $tooltip) = @_;
3901 4169
3902 $title = [$title, $tooltip] unless ref $title; 4170 $title = [$title, $tooltip] unless ref $title;
3903 $widget->{c_tab} = $title; 4171 $widget->{c_tab} = $title;
3916 4184
3917 $self->{multiplexer}->set_current_page ($page); 4185 $self->{multiplexer}->set_current_page ($page);
3918 $self->emit (page_changed => $self->{multiplexer}{current}); 4186 $self->emit (page_changed => $self->{multiplexer}{current});
3919} 4187}
3920 4188
4189sub _draw {
4190 my ($self) = @_;
4191
4192 $self->SUPER::_draw ();
4193
4194 if (my $cur = $self->{multiplexer}{current}) {
4195 if ($cur = $cur->{c_tab_}) {
4196 glTranslate $self->{buttonbar}{x} + $cur->{x},
4197 $self->{buttonbar}{y} + $cur->{y};
4198 glLineWidth 3;
4199 #glEnable GL_BLEND;
4200 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4201 glColor @{$self->{active_outline}};
4202 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4203 glLineWidth 1;
4204 #glDisable GL_BLEND;
4205 }
4206 }
4207}
4208
3921############################################################################# 4209#############################################################################
3922 4210
3923package CFPlus::UI::Selector; 4211package DC::UI::Selector;
3924 4212
3925use utf8; 4213use utf8;
3926 4214
3927our @ISA = CFPlus::UI::Button::; 4215our @ISA = DC::UI::Button::;
3928 4216
3929sub new { 4217sub new {
3930 my $class = shift; 4218 my $class = shift;
3931 4219
3932 my $self = $class->SUPER::new ( 4220 my $self = $class->SUPER::new (
3949 my ($value, $title, $tooltip) = @$_; 4237 my ($value, $title, $tooltip) = @$_;
3950 4238
3951 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4239 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3952 } 4240 }
3953 4241
3954 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4242 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3955} 4243}
3956 4244
3957sub _set_value { 4245sub _set_value {
3958 my ($self, $value) = @_; 4246 my ($self, $value) = @_;
3959 4247
3960 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4248 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4249 $item ||= $self->{options}[0]
3961 or return; 4250 or return;
3962 4251
3963 $self->{value} = $item->[0]; 4252 $self->{value} = $item->[0];
3964 $self->set_markup ("$item->[1] ⇓"); 4253 $self->set_markup ("$item->[1] ⇓");
3965 $self->set_tooltip ($item->[2]); 4254# $self->set_tooltip ($item->[2]);
3966} 4255}
3967 4256
3968sub set_value { 4257sub set_value {
3969 my ($self, $value) = @_; 4258 my ($self, $value) = @_;
3970 4259
3972 4261
3973 $self->_set_value ($value); 4262 $self->_set_value ($value);
3974 $self->emit (changed => $value); 4263 $self->emit (changed => $value);
3975} 4264}
3976 4265
4266sub set_options {
4267 my ($self, $options) = @_;
4268
4269 $self->{options} = $options;
4270 $self->_set_value ($self->{value});
4271}
4272
3977############################################################################# 4273#############################################################################
3978 4274
3979package CFPlus::UI::Statusbox; 4275package DC::UI::Statusbox;
3980 4276
3981our @ISA = CFPlus::UI::VBox::; 4277our @ISA = DC::UI::VBox::;
3982 4278
3983sub new { 4279sub new {
3984 my $class = shift; 4280 my $class = shift;
3985 4281
3986 my $self = $class->SUPER::new ( 4282 my $self = $class->SUPER::new (
3987 fontsize => 0.8, 4283 fontsize => 0.8,
3988 @_, 4284 @_,
3989 ); 4285 );
3990 4286
3991 CFPlus::weaken (my $this = $self); 4287 DC::weaken (my $this = $self);
3992 4288
3993 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4289 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3994 4290
3995 $self 4291 $self
3996} 4292}
3997 4293
3998sub reorder { 4294sub reorder {
3999 my ($self) = @_; 4295 my ($self) = @_;
4000 my $NOW = Time::HiRes::time; 4296 my $NOW = EV::time;
4001 4297
4002 # freeze display when hovering over any label 4298 # freeze display when hovering over any label
4003 return if $CFPlus::UI::TOOLTIP->{owner} 4299 return if $DC::UI::TOOLTIP->{owner}
4004 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4300 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4005 values %{ $self->{item} }; 4301 values %{ $self->{item} };
4006 4302
4007 while (my ($k, $v) = each %{ $self->{item} }) { 4303 while (my ($k, $v) = each %{ $self->{item} }) {
4008 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4304 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4009 } 4305 }
4306
4307 $self->{timer}->set (1, 1);
4010 4308
4011 my @widgets; 4309 my @widgets;
4012 4310
4013 my @items = sort { 4311 my @items = sort {
4014 $a->{pri} <=> $b->{pri} 4312 $a->{pri} <=> $b->{pri}
4015 or $b->{id} <=> $a->{id} 4313 or $b->{id} <=> $a->{id}
4016 } values %{ $self->{item} }; 4314 } values %{ $self->{item} };
4017
4018 $self->{timer}->interval (1);
4019 4315
4020 my $count = 10 + 1; 4316 my $count = 10 + 1;
4021 for my $item (@items) { 4317 for my $item (@items) {
4022 last unless --$count; 4318 last unless --$count;
4023 4319
4030 for ($short) { 4326 for ($short) {
4031 s/^\s+//; 4327 s/^\s+//;
4032 s/\s+/ /g; 4328 s/\s+/ /g;
4033 } 4329 }
4034 4330
4035 new CFPlus::UI::Label 4331 new DC::UI::Label
4036 markup => $short, 4332 markup => $short,
4037 tooltip => $item->{tooltip}, 4333 tooltip => $item->{tooltip},
4038 tooltip_font => $::FONT_PROP, 4334 tooltip_font => $::FONT_PROP,
4039 tooltip_width => 0.67, 4335 tooltip_width => 0.67,
4040 fontsize => $item->{fontsize} || $self->{fontsize}, 4336 fontsize => $item->{fontsize} || $self->{fontsize},
4041 max_w => $::WIDTH * 0.44, 4337 max_w => $::WIDTH * 0.44,
4338 align => 0,
4042 fg => [@{ $item->{fg} }], 4339 fg => [@{ $item->{fg} }],
4043 can_events => 1, 4340 can_events => 1,
4044 can_hover => 1 4341 can_hover => 1
4045 }; 4342 };
4046 4343
4047 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4344 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4048 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4345 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4049 $label->update; 4346 $label->update;
4050 $label->set_max_size (undef, $label->{req_h} * $diff) 4347 $label->set_max_size (undef, $label->{req_h} * $diff)
4051 if $diff < 1; 4348 if $diff < 1;
4052 $self->{timer}->interval (1/30); 4349 $self->{timer}->set (1/30, 1/30);
4053 } else { 4350 } else {
4054 $label->{fg}[3] = $item->{fg}[3] || 1; 4351 $label->{fg}[3] = $item->{fg}[3] || 1;
4055 } 4352 }
4056 4353
4057 push @widgets, $label; 4354 push @widgets, $label;
4058 } 4355 }
4356
4357 my $hash = join ",", @widgets;
4358 return if $hash eq $self->{last_widget_hash};
4359 $self->{last_widget_hash} = $hash;
4059 4360
4060 $self->clear; 4361 $self->clear;
4061 $self->SUPER::add (reverse @widgets); 4362 $self->SUPER::add (reverse @widgets);
4062} 4363}
4063 4364
4119 $self->SUPER::destroy; 4420 $self->SUPER::destroy;
4120} 4421}
4121 4422
4122############################################################################# 4423#############################################################################
4123 4424
4124package CFPlus::UI::Root; 4425package DC::UI::Root;
4125 4426
4126our @ISA = CFPlus::UI::Container::; 4427our @ISA = DC::UI::Container::;
4127 4428
4128use List::Util qw(min max); 4429use List::Util qw(min max);
4129 4430
4130use CFPlus::OpenGL; 4431use DC::OpenGL;
4131 4432
4132sub new { 4433sub new {
4133 my $class = shift; 4434 my $class = shift;
4134 4435
4135 my $self = $class->SUPER::new ( 4436 my $self = $class->SUPER::new (
4136 visible => 1, 4437 visible => 1,
4137 @_, 4438 @_,
4138 ); 4439 );
4139 4440
4140 CFPlus::weaken ($self->{root} = $self); 4441 DC::weaken ($self->{root} = $self);
4141 4442
4142 $self 4443 $self
4143} 4444}
4144 4445
4145sub size_request { 4446sub size_request {
4193} 4494}
4194 4495
4195sub update { 4496sub update {
4196 my ($self) = @_; 4497 my ($self) = @_;
4197 4498
4198 $::WANT_REFRESH++; 4499 $::WANT_REFRESH = 1;
4199} 4500}
4200 4501
4201sub add { 4502sub add {
4202 my ($self, @children) = @_; 4503 my ($self, @children) = @_;
4203 4504
4270 4571
4271 delete $queue{$widget+0}; 4572 delete $queue{$widget+0};
4272 4573
4273 my ($w, $h) = $widget->size_request; 4574 my ($w, $h) = $widget->size_request;
4274 4575
4275 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4576 $w += $widget->{padding_x} * 2;
4276 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4577 $h += $widget->{padding_y} * 2;
4578
4579 $w = max $widget->{min_w}, $w;
4580 $h = max $widget->{min_h}, $h;
4277 4581
4278 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4582 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4279 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4583 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4280 4584
4281 $w = $widget->{force_w} if exists $widget->{force_w}; 4585 $w = $widget->{force_w} if exists $widget->{force_w};
4348 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4652 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4349 glMatrixMode GL_MODELVIEW; 4653 glMatrixMode GL_MODELVIEW;
4350 glLoadIdentity; 4654 glLoadIdentity;
4351 4655
4352 { 4656 {
4353 package CFPlus::UI::Base; 4657 package DC::UI::Base;
4354 4658
4355 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4659 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4356 (0, 0, $self->{w}, $self->{h}); 4660 (0, 0, $self->{w}, $self->{h});
4357 4661
4358 $self->_draw; 4662 $self->_draw;
4359 } 4663 }
4360} 4664}
4361 4665
4362############################################################################# 4666#############################################################################
4363 4667
4364package CFPlus::UI; 4668package DC::UI;
4365 4669
4366$ROOT = new CFPlus::UI::Root; 4670$ROOT = new DC::UI::Root;
4367$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4671$TOOLTIP = new DC::UI::Tooltip z => 900;
4368 4672
43691 46731
4370

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines