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.422 by root, Thu Aug 2 14:35:21 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 c_col => 0, 1052 c_col => 0,
1033 c_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 c_col => 1, 1063 c_col => 1,
1044 c_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 {
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 c_col => 0, 1085 c_col => 0,
1066 c_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},
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 (
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_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->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1670 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_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) {
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}) {
1961 1985
1962sub size_request { 1986sub size_request {
1963 my ($self) = @_; 1987 my ($self) = @_;
1964 1988
1965 $self->{size_req} ||= do { 1989 $self->{size_req} ||= do {
1990 my ($max_w, $max_h) = $self->get_max_wh;
1991
1966 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1992 $self->{layout}->set_font ($self->{font}) if $self->{font};
1967 $self->{layout}->set_width ($self->{max_w} || -1); 1993 $self->{layout}->set_width ($max_w);
1968 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1994 $self->{layout}->set_ellipsise ($self->{ellipsise});
1969 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1995 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1970 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1996 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1971 1997
1972 my ($w, $h) = $self->{layout}->size; 1998 my ($w, $h) = $self->{layout}->size;
1973 1999
1974 if (exists $self->{template}) { 2000 if (exists $self->{template}) {
1975 $self->{template}->set_font ($self->{font}) if $self->{font}; 2001 $self->{template}->set_font ($self->{font}) if $self->{font};
1976 $self->{template}->set_width ($self->{max_w} || -1); 2002 $self->{template}->set_width ($max_w);
1977 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2003 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1978 2004
1979 my ($w2, $h2) = $self->{template}->size; 2005 my ($w2, $h2) = $self->{template}->size;
1980 2006
1981 $w = List::Util::max $w, $w2; 2007 $w = List::Util::max $w, $w2;
2037 2063
2038 [$self->{layout}->size] 2064 [$self->{layout}->size]
2039 }; 2065 };
2040 2066
2041 unless (exists $self->{ox}) { 2067 unless (exists $self->{ox}) {
2042 $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);
2043 : $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);
2044 : ($self->{w} - $size->[0]) * 0.5);
2045 2070
2046 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2071 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2047 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2048 : ($self->{h} - $size->[1]) * 0.5);
2049 }; 2072 };
2050 2073
2051# unless ($self->{list}) { 2074# unless ($self->{list}) {
2052# $self->{list} = CFPlus::OpenGL::glGenList; 2075# $self->{list} = DC::OpenGL::glGenList;
2053# CFPlus::OpenGL::glNewList $self->{list}; 2076# DC::OpenGL::glNewList $self->{list};
2054# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2077# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2055# CFPlus::OpenGL::glEndList; 2078# DC::OpenGL::glEndList;
2056# } 2079# }
2057# 2080#
2058# CFPlus::OpenGL::glCallList $self->{list}; 2081# DC::OpenGL::glCallList $self->{list};
2059 2082
2060 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2083 $self->{layout}->draw;
2061} 2084}
2062 2085
2063#sub destroy { 2086#sub destroy {
2064# my ($self) = @_; 2087# my ($self) = @_;
2065# 2088#
2066# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2089# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2067# 2090#
2068# $self->SUPER::destroy; 2091# $self->SUPER::destroy;
2069#} 2092#}
2070 2093
2071############################################################################# 2094#############################################################################
2072 2095
2073package CFPlus::UI::EntryBase; 2096package DC::UI::EntryBase;
2074 2097
2075our @ISA = CFPlus::UI::Label::; 2098our @ISA = DC::UI::Label::;
2076 2099
2077use CFPlus::OpenGL; 2100use DC::OpenGL;
2078 2101
2079sub new { 2102sub new {
2080 my $class = shift; 2103 my $class = shift;
2081 2104
2082 $class->SUPER::new ( 2105 $class->SUPER::new (
2083 fg => [1, 1, 1], 2106 fg => [1, 1, 1],
2084 bg => [0, 0, 0, 0.2], 2107 bg => [0, 0, 0, 0.2],
2085 outline => [0.6, 0.3, 0.1], 2108 outline => undef,
2086 active_bg => [0, 0, 1, .2], 2109 active_bg => [0, 0, 1, .2],
2087 active_fg => [1, 1, 1], 2110 active_fg => [1, 1, 1],
2088 active_outline => [1, 1, 0], 2111 active_outline => [1, 1, 0],
2089 can_hover => 1, 2112 can_hover => 1,
2090 can_focus => 1, 2113 can_focus => 1,
2114 align => 0,
2091 valign => 0, 2115 valign => 0.5,
2092 can_events => 1, 2116 can_events => 1,
2093 ellipsise => 0, 2117 ellipsise => 0,
2118 padding_x => 4,
2119 padding_y => 2,
2094 #text => ... 2120 #text => ...
2095 #hidden => "*", 2121 #hidden => "*",
2096 @_ 2122 @_
2097 ) 2123 )
2098} 2124}
2145 2171
2146 my $text = $self->get_text; 2172 my $text = $self->get_text;
2147 2173
2148 $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;
2149 2175
2150 if ($uni == 8) { 2176 if ($sym == DC::SDLK_BACKSPACE) {
2151 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2177 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2152 } elsif ($uni == 127) { 2178 } elsif ($sym == DC::SDLK_DELETE) {
2153 substr $text, $self->{cursor}, 1, ""; 2179 substr $text, $self->{cursor}, 1, "";
2154 } elsif ($sym == CFPlus::SDLK_LEFT) { 2180 } elsif ($sym == DC::SDLK_LEFT) {
2155 --$self->{cursor} if $self->{cursor}; 2181 --$self->{cursor} if $self->{cursor};
2156 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2182 } elsif ($sym == DC::SDLK_RIGHT) {
2157 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2183 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2158 } elsif ($sym == CFPlus::SDLK_HOME) { 2184 } elsif ($sym == DC::SDLK_HOME) {
2159 # what a hack 2185 # what a hack
2160 $self->{cursor} = 2186 $self->{cursor} =
2161 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2187 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2162 ? length $1 2188 ? length $1
2163 : 0; 2189 : 0;
2164 } elsif ($sym == CFPlus::SDLK_END) { 2190 } elsif ($sym == DC::SDLK_END) {
2165 # uh, again 2191 # uh, again
2166 $self->{cursor} = 2192 $self->{cursor} =
2167 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2193 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2168 ? $self->{cursor} + length $1 2194 ? $self->{cursor} + length $1
2169 : length $self->{text}; 2195 : length $self->{text};
2233 glColor_premultiply @{$self->{bg}}; 2259 glColor_premultiply @{$self->{bg}};
2234 } 2260 }
2235 2261
2236 glEnable GL_BLEND; 2262 glEnable GL_BLEND;
2237 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2263 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2238 glBegin GL_QUADS;
2239 glVertex 0 , 0;
2240 glVertex 0 , $self->{h};
2241 glVertex $self->{w}, $self->{h}; 2264 glRect 0, 0, $self->{w}, $self->{h};
2242 glVertex $self->{w}, 0;
2243 glEnd;
2244 glDisable GL_BLEND; 2265 glDisable GL_BLEND;
2245 2266
2246 $self->SUPER::_draw; 2267 $self->SUPER::_draw;
2247 2268
2248 #TODO: force update every cursor change :( 2269 #TODO: force update every cursor change :(
2255 @$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);
2256 } 2277 }
2257 2278
2258 glColor_premultiply @{$self->{active_fg}}; 2279 glColor_premultiply @{$self->{active_fg}};
2259 glBegin GL_LINES; 2280 glBegin GL_LINES;
2260 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};
2261 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};
2262 glEnd; 2283 glEnd;
2263 2284
2264 glLineWidth 3; 2285 glLineWidth 3;
2265 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}};
2266 glBegin GL_LINE_LOOP; 2292 glBegin GL_LINE_STRIP;
2267 glVertex 0, 0; 2293 glVertex .5, $self->{h} * .5;
2268 glVertex $self->{w} - 1, 0;
2269 glVertex $self->{w} - 1, $self->{h} - 1;
2270 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;
2271 glEnd; 2297 glEnd;
2272 glLineWidth 1;
2273
2274 } else {
2275 glColor @{$self->{outline}};
2276 glTranslate .375, .375;
2277 glBegin GL_LINE_STRIP;
2278 glVertex 0, $self->{h} * .5;
2279 glVertex 0, $self->{h} - 3;
2280 glVertex $self->{w} - 1, $self->{h} - 3;
2281 glVertex $self->{w} - 1, $self->{h} * .5;
2282 glEnd;
2283 } 2298 }
2284} 2299}
2285 2300
2286############################################################################# 2301#############################################################################
2287 2302
2288package CFPlus::UI::Entry; 2303package DC::UI::Entry;
2289 2304
2290our @ISA = CFPlus::UI::EntryBase::; 2305our @ISA = DC::UI::EntryBase::;
2291 2306
2292use 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
2293 2318
2294sub invoke_key_down { 2319sub invoke_key_down {
2295 my ($self, $ev) = @_; 2320 my ($self, $ev) = @_;
2296 2321
2297 my $sym = $ev->{sym}; 2322 my $sym = $ev->{sym};
2303 $self->{history_pointer} = -1; 2328 $self->{history_pointer} = -1;
2304 $self->{history_saveback} = ''; 2329 $self->{history_saveback} = '';
2305 $self->emit (activate => $txt); 2330 $self->emit (activate => $txt);
2306 $self->update; 2331 $self->update;
2307 2332
2308 } elsif ($sym == CFPlus::SDLK_UP) { 2333 } elsif ($sym == DC::SDLK_UP) {
2309 if ($self->{history_pointer} < 0) { 2334 if ($self->{history_pointer} < 0) {
2310 $self->{history_saveback} = $self->get_text; 2335 $self->{history_saveback} = $self->get_text;
2311 } 2336 }
2312 if (@{$self->{history} || []} > 0) { 2337 if (@{$self->{history} || []} > 0) {
2313 $self->{history_pointer}++; 2338 $self->{history_pointer}++;
2315 $self->{history_pointer} = @{$self->{history} || []} - 1; 2340 $self->{history_pointer} = @{$self->{history} || []} - 1;
2316 } 2341 }
2317 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2318 } 2343 }
2319 2344
2320 } elsif ($sym == CFPlus::SDLK_DOWN) { 2345 } elsif ($sym == DC::SDLK_DOWN) {
2321 $self->{history_pointer}--; 2346 $self->{history_pointer}--;
2322 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2347 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2323 2348
2324 if ($self->{history_pointer} >= 0) { 2349 if ($self->{history_pointer} >= 0) {
2325 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2350 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2326 } else { 2351 } else {
2352 if (defined $self->{history_saveback}) {
2327 $self->set_text ($self->{history_saveback}); 2353 $self->set_text ($self->{history_saveback});
2354 $self->{history_saveback} = undef;
2355 }
2328 } 2356 }
2329 2357
2330 } else { 2358 } else {
2331 return $self->SUPER::invoke_key_down ($ev) 2359 return $self->SUPER::invoke_key_down ($ev)
2332 } 2360 }
2334 1 2362 1
2335} 2363}
2336 2364
2337############################################################################# 2365#############################################################################
2338 2366
2339package CFPlus::UI::TextEdit; 2367package DC::UI::TextEdit;
2340 2368
2341our @ISA = CFPlus::UI::EntryBase::; 2369our @ISA = DC::UI::EntryBase::;
2342 2370
2343use 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}
2344 2382
2345sub move_cursor_ver { 2383sub move_cursor_ver {
2346 my ($self, $dy) = @_; 2384 my ($self, $dy) = @_;
2347 2385
2348 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2386 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2349 2387
2350 $y += $dy; 2388 $line += $dy;
2351 2389
2352 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))) {
2353 $self->{cursor} = $index; 2391 $self->{cursor} = $index;
2354 delete $self->{cur_h}; 2392 delete $self->{cur_h};
2355 $self->update; 2393 $self->update;
2356 return; 2394 return;
2357 } 2395 }
2360sub invoke_key_down { 2398sub invoke_key_down {
2361 my ($self, $ev) = @_; 2399 my ($self, $ev) = @_;
2362 2400
2363 my $sym = $ev->{sym}; 2401 my $sym = $ev->{sym};
2364 2402
2365 if ($sym == CFPlus::SDLK_UP) { 2403 if ($sym == DC::SDLK_UP) {
2366 $self->move_cursor_ver (-1); 2404 $self->move_cursor_ver (-1);
2367 } elsif ($sym == CFPlus::SDLK_DOWN) { 2405 } elsif ($sym == DC::SDLK_DOWN) {
2368 $self->move_cursor_ver (+1); 2406 $self->move_cursor_ver (+1);
2369 } else { 2407 } else {
2370 return $self->SUPER::invoke_key_down ($ev) 2408 return $self->SUPER::invoke_key_down ($ev)
2371 } 2409 }
2372 2410
2373 1 2411 1
2374} 2412}
2375 2413
2376############################################################################# 2414#############################################################################
2377 2415
2378package CFPlus::UI::ButtonBin; 2416package DC::UI::ButtonBin;
2379 2417
2380our @ISA = CFPlus::UI::Bin::; 2418our @ISA = DC::UI::Bin::;
2381 2419
2382use CFPlus::OpenGL; 2420use DC::OpenGL;
2383 2421
2384my @tex = 2422my @tex =
2385 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2423 map { new_from_resource DC::Texture $_, mipmap => 1 }
2386 qw(b1_button_inactive.png b1_button_active.png); 2424 qw(b1_button_inactive.png b1_button_active.png);
2387 2425
2388sub new { 2426sub new {
2389 my $class = shift; 2427 my $class = shift;
2390 2428
2391 $class->SUPER::new ( 2429 $class->SUPER::new (
2392 can_hover => 1, 2430 can_hover => 1,
2393 align => 0, 2431 align => 0.5,
2394 valign => 0, 2432 valign => 0.5,
2395 can_events => 1, 2433 can_events => 1,
2396 @_ 2434 @_
2397 ) 2435 )
2398} 2436}
2399 2437
2422 $self->SUPER::_draw; 2460 $self->SUPER::_draw;
2423} 2461}
2424 2462
2425############################################################################# 2463#############################################################################
2426 2464
2427package CFPlus::UI::Button; 2465package DC::UI::Button;
2428 2466
2429our @ISA = CFPlus::UI::Label::; 2467our @ISA = DC::UI::Label::;
2430 2468
2431use CFPlus::OpenGL; 2469use DC::OpenGL;
2432 2470
2433my @tex = 2471my @tex =
2434 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2472 map { new_from_resource DC::Texture $_, mipmap => 1 }
2435 qw(b1_button_inactive.png b1_button_active.png); 2473 qw(b1_button_inactive.png b1_button_active.png);
2436 2474
2437sub new { 2475sub new {
2438 my $class = shift; 2476 my $class = shift;
2439 2477
2440 $class->SUPER::new ( 2478 $class->SUPER::new (
2441 padding_x => 4, 2479 padding_x => 8,
2442 padding_y => 4, 2480 padding_y => 4,
2443 fg => [1.0, 1.0, 1.0], 2481 fg => [1.0, 1.0, 1.0],
2444 active_fg => [0.8, 0.8, 0.8], 2482 active_fg => [0.8, 0.8, 0.8],
2445 can_hover => 1, 2483 can_hover => 1,
2446 align => 0, 2484 align => 0.5,
2447 valign => 0, 2485 valign => 0.5,
2448 can_events => 1, 2486 can_events => 1,
2449 @_ 2487 @_
2450 ) 2488 )
2451} 2489}
2452 2490
2477 $self->SUPER::_draw; 2515 $self->SUPER::_draw;
2478} 2516}
2479 2517
2480############################################################################# 2518#############################################################################
2481 2519
2482package CFPlus::UI::CheckBox; 2520package DC::UI::CheckBox;
2483 2521
2484our @ISA = CFPlus::UI::DrawBG::; 2522our @ISA = DC::UI::DrawBG::;
2485 2523
2486my @tex = 2524my @tex =
2487 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2525 map { new_from_resource DC::Texture $_, mipmap => 1 }
2488 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2526 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2489 2527
2490use CFPlus::OpenGL; 2528use DC::OpenGL;
2491 2529
2492sub new { 2530sub new {
2493 my $class = shift; 2531 my $class = shift;
2494 2532
2495 $class->SUPER::new ( 2533 $class->SUPER::new (
2534 fontsize => 1,
2496 padding_x => 2, 2535 padding_x => 2,
2497 padding_y => 2, 2536 padding_y => 2,
2498 fg => [1, 1, 1], 2537 fg => [1, 1, 1],
2499 active_fg => [1, 1, 0], 2538 active_fg => [1, 1, 0],
2500 bg => [0, 0, 0, 0.2], 2539 bg => [0, 0, 0, 0.2],
2506} 2545}
2507 2546
2508sub size_request { 2547sub size_request {
2509 my ($self) = @_; 2548 my ($self) = @_;
2510 2549
2511 (6) x 2 2550 ($self->{fontsize} * $::FONTSIZE) x 2
2512} 2551}
2513 2552
2514sub toggle { 2553sub toggle {
2515 my ($self) = @_; 2554 my ($self) = @_;
2516 2555
2535sub _draw { 2574sub _draw {
2536 my ($self) = @_; 2575 my ($self) = @_;
2537 2576
2538 $self->SUPER::_draw; 2577 $self->SUPER::_draw;
2539 2578
2540 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2579 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2541 2580
2542 my ($w, $h) = @$self{qw(w h)}; 2581 my ($w, $h) = @$self{qw(w h)};
2543 2582
2544 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;
2545 2584
2552 glDisable GL_TEXTURE_2D; 2591 glDisable GL_TEXTURE_2D;
2553} 2592}
2554 2593
2555############################################################################# 2594#############################################################################
2556 2595
2557package CFPlus::UI::Image; 2596package DC::UI::Image;
2558 2597
2559our @ISA = CFPlus::UI::Base::; 2598our @ISA = DC::UI::DrawBG::;
2560 2599
2561use CFPlus::OpenGL; 2600use DC::OpenGL;
2562 2601
2563our %texture_cache; 2602our %texture_cache;
2564 2603
2565sub new { 2604sub new {
2566 my $class = shift; 2605 my $class = shift;
2573 2612
2574 $self->{path} || $self->{tex} 2613 $self->{path} || $self->{tex}
2575 or Carp::croak "'path' or 'tex' attributes required"; 2614 or Carp::croak "'path' or 'tex' attributes required";
2576 2615
2577 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2616 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2578 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2617 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2579 2618
2580 CFPlus::weaken $texture_cache{$self->{path}}; 2619 DC::weaken $texture_cache{$self->{path}};
2581 2620
2582 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2621 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2583 2622
2584 $self 2623 $self
2585} 2624}
2586 2625
2587sub STORABLE_freeze { 2626sub STORABLE_freeze {
2588 my ($self, $cloning) = @_; 2627 my ($self, $cloning) = @_;
2589 2628
2590 $self->{path} 2629 $self->{path}
2591 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";
2592 2631
2593 $self->{path} 2632 $self->{path}
2594} 2633}
2595 2634
2596sub STORABLE_attach { 2635sub STORABLE_attach {
2597 my ($self, $cloning, $path) = @_; 2636 my ($self, $cloning, $path) = @_;
2598 2637
2599 $self->new (path => $path) 2638 $self->new (path => $path)
2600} 2639}
2601 2640
2641sub set_texture {
2642 my ($self, $tex) = @_;
2643
2644 $self->{tex} = $tex;
2645 $self->update;
2646}
2647
2602sub size_request { 2648sub size_request {
2603 my ($self) = @_; 2649 my ($self) = @_;
2604 2650
2605 (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})
2606} 2652}
2607 2653
2608sub _draw { 2654sub _draw {
2609 my ($self) = @_; 2655 my ($self) = @_;
2656
2657 $self->SUPER::_draw;
2610 2658
2611 my $tex = $self->{tex}; 2659 my $tex = $self->{tex};
2612 2660
2613 my ($w, $h) = ($self->{w}, $self->{h}); 2661 my ($w, $h) = ($self->{w}, $self->{h});
2614 2662
2627 glDisable GL_TEXTURE_2D; 2675 glDisable GL_TEXTURE_2D;
2628} 2676}
2629 2677
2630############################################################################# 2678#############################################################################
2631 2679
2632package CFPlus::UI::ImageButton; 2680package DC::UI::ImageButton;
2633 2681
2634our @ISA = CFPlus::UI::Image::; 2682our @ISA = DC::UI::Image::;
2635 2683
2636use CFPlus::OpenGL; 2684use DC::OpenGL;
2637
2638my %textures;
2639 2685
2640sub new { 2686sub new {
2641 my $class = shift; 2687 my $class = shift;
2642 2688
2643 my $self = $class->SUPER::new ( 2689 my $self = $class->SUPER::new (
2644 padding_x => 4, 2690 padding_x => 4,
2645 padding_y => 4, 2691 padding_y => 4,
2646 fg => [1, 1, 1], 2692 fg => [1, 1, 1],
2647 active_fg => [0, 0, 1], 2693 active_fg => [0, 0, 1],
2648 can_hover => 1, 2694 can_hover => 1,
2649 align => 0, 2695 align => 0.5,
2650 valign => 0, 2696 valign => 0.5,
2651 can_events => 1, 2697 can_events => 1,
2652 @_ 2698 @_
2653 ); 2699 );
2654} 2700}
2655 2701
2702sub invoke_button_down {
2703 my ($self, $ev, $x, $y) = @_;
2704
2705 1
2706}
2707
2656sub invoke_button_up { 2708sub invoke_button_up {
2657 my ($self, $ev, $x, $y) = @_; 2709 my ($self, $ev, $x, $y) = @_;
2658 2710
2659 $self->emit ("activate") 2711 $self->emit ("activate")
2660 if $x >= 0 && $x < $self->{w} 2712 if $x >= 0 && $x < $self->{w}
2663 1 2715 1
2664} 2716}
2665 2717
2666############################################################################# 2718#############################################################################
2667 2719
2668package CFPlus::UI::VGauge; 2720package DC::UI::VGauge;
2669 2721
2670our @ISA = CFPlus::UI::Base::; 2722our @ISA = DC::UI::Base::;
2671 2723
2672use List::Util qw(min max); 2724use List::Util qw(min max);
2673 2725
2674use CFPlus::OpenGL; 2726use DC::OpenGL;
2675 2727
2676my %tex = ( 2728my %tex = (
2677 food => [ 2729 food => [
2678 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2730 map { new_from_resource DC::Texture $_, mipmap => 1 }
2679 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2731 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2680 ], 2732 ],
2681 grace => [ 2733 grace => [
2682 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2683 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/
2684 ], 2736 ],
2685 hp => [ 2737 hp => [
2686 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2687 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2739 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2688 ], 2740 ],
2689 mana => [ 2741 mana => [
2690 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2691 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/
2692 ], 2744 ],
2693); 2745);
2694 2746
2695# eg. VGauge->new (gauge => 'food'), default gauge: food 2747# eg. VGauge->new (gauge => 'food'), default gauge: food
2798 glDisable GL_TEXTURE_2D; 2850 glDisable GL_TEXTURE_2D;
2799} 2851}
2800 2852
2801############################################################################# 2853#############################################################################
2802 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
2803package CFPlus::UI::Gauge; 3009package DC::UI::Gauge;
2804 3010
2805our @ISA = CFPlus::UI::VBox::; 3011our @ISA = DC::UI::VBox::;
2806 3012
2807sub new { 3013sub new {
2808 my ($class, %arg) = @_; 3014 my ($class, %arg) = @_;
2809 3015
2810 my $self = $class->SUPER::new ( 3016 my $self = $class->SUPER::new (
2812 can_hover => 1, 3018 can_hover => 1,
2813 can_events => 1, 3019 can_events => 1,
2814 %arg, 3020 %arg,
2815 ); 3021 );
2816 3022
2817 $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");
2818 $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);
2819 $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");
2820 3026
2821 $self 3027 $self
2822} 3028}
2823 3029
2824sub set_fontsize { 3030sub set_fontsize {
2845 $self->{value}->set_text ($val); 3051 $self->{value}->set_text ($val);
2846} 3052}
2847 3053
2848############################################################################# 3054#############################################################################
2849 3055
2850package CFPlus::UI::Slider; 3056package DC::UI::Slider;
2851 3057
2852use strict; 3058use strict;
2853 3059
2854use CFPlus::OpenGL; 3060use DC::OpenGL;
2855 3061
2856our @ISA = CFPlus::UI::DrawBG::; 3062our @ISA = DC::UI::DrawBG::;
2857 3063
2858my @tex = 3064my @tex =
2859 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3065 map { new_from_resource DC::Texture $_ }
2860 qw(s1_slider.png s1_slider_bg.png); 3066 qw(s1_slider.png s1_slider_bg.png);
2861 3067
2862sub new { 3068sub new {
2863 my $class = shift; 3069 my $class = shift;
2864 3070
2901sub set_value { 3107sub set_value {
2902 my ($self, $value) = @_; 3108 my ($self, $value) = @_;
2903 3109
2904 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2905 3111
2906 $hi = $lo + 1 if $hi <= $lo; 3112 $hi = $lo if $hi < $lo;
2907 3113
2908 $page = $hi - $lo if $page > $hi - $lo; 3114 $value = $hi - $page if $value > $hi - $page;
2909
2910 $value = $lo if $value < $lo; 3115 $value = $lo if $value < $lo;
2911 $value = $hi - $page if $value > $hi - $page;
2912 3116
2913 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3117 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2914 if $unit; 3118 if $unit;
2915 3119
2916 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3120 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2960sub invoke_mouse_wheel { 3164sub invoke_mouse_wheel {
2961 my ($self, $ev) = @_; 3165 my ($self, $ev) = @_;
2962 3166
2963 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3167 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2964 3168
2965 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3169 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2966 3170
2967 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3171 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2968 3172
2969 1 3173 1
2970} 3174}
2980 my ($self) = @_; 3184 my ($self) = @_;
2981 3185
2982 unless ($self->{knob_w}) { 3186 unless ($self->{knob_w}) {
2983 $self->set_value ($self->{range}[0]); 3187 $self->set_value ($self->{range}[0]);
2984 3188
2985 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3189 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2986 my $range = ($hi - $page - $lo) || 1e-100; 3190 my $range = ($hi - $page - $lo) || 1e-10;
2987 3191
2988 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};
2989 3193
2990 $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;
2991 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3195 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2992 3196
2993 $value = ($value - $lo) / $range; 3197 $value = ($value - $lo) / $range;
3023 glDisable GL_TEXTURE_2D; 3227 glDisable GL_TEXTURE_2D;
3024} 3228}
3025 3229
3026############################################################################# 3230#############################################################################
3027 3231
3028package CFPlus::UI::ValSlider; 3232package DC::UI::ValSlider;
3029 3233
3030our @ISA = CFPlus::UI::HBox::; 3234our @ISA = DC::UI::HBox::;
3031 3235
3032sub new { 3236sub new {
3033 my ($class, %arg) = @_; 3237 my ($class, %arg) = @_;
3034 3238
3035 my $range = delete $arg{range}; 3239 my $range = delete $arg{range};
3036 3240
3037 my $self = $class->SUPER::new ( 3241 my $self = $class->SUPER::new (
3038 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3242 slider => (new DC::UI::Slider expand => 1, range => $range),
3039 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3243 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3040 to_value => sub { shift }, 3244 to_value => sub { shift },
3041 from_value => sub { shift }, 3245 from_value => sub { shift },
3042 %arg, 3246 %arg,
3043 ); 3247 );
3044 3248
3064sub set_range { shift->{slider}->set_range (@_) } 3268sub set_range { shift->{slider}->set_range (@_) }
3065sub set_value { shift->{slider}->set_value (@_) } 3269sub set_value { shift->{slider}->set_value (@_) }
3066 3270
3067############################################################################# 3271#############################################################################
3068 3272
3069package CFPlus::UI::TextScroller; 3273package DC::UI::TextScroller;
3070 3274
3071our @ISA = CFPlus::UI::HBox::; 3275our @ISA = DC::UI::HBox::;
3072 3276
3073use CFPlus::OpenGL; 3277use DC::OpenGL;
3074 3278
3075sub new { 3279sub new {
3076 my $class = shift; 3280 my $class = shift;
3077 3281
3078 my $self = $class->SUPER::new ( 3282 my $self = $class->SUPER::new (
3080 can_events => 1, 3284 can_events => 1,
3081 indent => 0, 3285 indent => 0,
3082 #font => default_font 3286 #font => default_font
3083 @_, 3287 @_,
3084 3288
3085 layout => (new CFPlus::Layout), 3289 layout => (new DC::Layout),
3086 par => [], 3290 par => [],
3087 max_par => 0, 3291 max_par => 0,
3088 height => 0, 3292 height => 0,
3089 children => [ 3293 children => [
3090 (new CFPlus::UI::Empty expand => 1), 3294 (new DC::UI::Empty expand => 1),
3091 (new CFPlus::UI::Slider vertical => 1), 3295 (new DC::UI::Slider vertical => 1),
3092 ], 3296 ],
3093 ); 3297 );
3094 3298
3095 $self->{children}[1]->connect (changed => sub { $self->update }); 3299 $self->{children}[1]->connect (changed => sub { $self->update });
3096 3300
3152 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3356 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3153 $layout->set_markup ($para->{markup}); 3357 $layout->set_markup ($para->{markup});
3154 3358
3155 $layout->set_shapes ( 3359 $layout->set_shapes (
3156 map 3360 map
3157 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3361 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3158 @{$para->{widget}} 3362 @{$para->{widget}}
3159 ); 3363 );
3160 3364
3161 $layout 3365 $layout
3162} 3366}
3279 $ROOT->on_post_alloc ($self => sub { 3483 $ROOT->on_post_alloc ($self => sub {
3280 $self->force_uptodate; 3484 $self->force_uptodate;
3281 3485
3282 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3486 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3283 3487
3284 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3488 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3285 glClearColor 0, 0, 0, 0; 3489 glClearColor 0, 0, 0, 0;
3286 glClear GL_COLOR_BUFFER_BIT; 3490 glClear GL_COLOR_BUFFER_BIT;
3287 3491
3288 package CFPlus::UI::Base; 3492 package DC::UI::Base;
3289 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3493 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3290 (0, 0, $self->{w}, $self->{h}); 3494 (0, 0, $self->{w}, $self->{h});
3291 3495
3292 my $top = int $self->{children}[1]{range}[0]; 3496 my $top = int $self->{children}[1]{range}[0];
3293 3497
3304 3508
3305 if ($y0 < $y + $h && $y < $y1) { 3509 if ($y0 < $y + $h && $y < $y1) {
3306 my $layout = $self->get_layout ($para); 3510 my $layout = $self->get_layout ($para);
3307 3511
3308 $layout->render ($para->{indent}, $y - $y0); 3512 $layout->render ($para->{indent}, $y - $y0);
3513 $layout->draw;
3309 3514
3310 if (my @w = @{ $para->{widget} }) { 3515 if (my @w = @{ $para->{widget} }) {
3311 my @s = $layout->get_shapes; 3516 my @s = $layout->get_shapes;
3312 3517
3313 for (@w) { 3518 for (@w) {
3351 $self->{children}[1]->draw; 3556 $self->{children}[1]->draw;
3352} 3557}
3353 3558
3354############################################################################# 3559#############################################################################
3355 3560
3356package CFPlus::UI::Animator; 3561package DC::UI::Animator;
3357 3562
3358use CFPlus::OpenGL; 3563use DC::OpenGL;
3359 3564
3360our @ISA = CFPlus::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3361 3566
3362sub moveto { 3567sub moveto {
3363 my ($self, $x, $y) = @_; 3568 my ($self, $x, $y) = @_;
3364 3569
3365 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3570 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3393 glPopMatrix; 3598 glPopMatrix;
3394} 3599}
3395 3600
3396############################################################################# 3601#############################################################################
3397 3602
3398package CFPlus::UI::Flopper; 3603package DC::UI::Flopper;
3399 3604
3400our @ISA = CFPlus::UI::Button::; 3605our @ISA = DC::UI::Button::;
3401 3606
3402sub new { 3607sub new {
3403 my $class = shift; 3608 my $class = shift;
3404 3609
3405 my $self = $class->SUPER::new ( 3610 my $self = $class->SUPER::new (
3417 $self->{other}->toggle_visibility; 3622 $self->{other}->toggle_visibility;
3418} 3623}
3419 3624
3420############################################################################# 3625#############################################################################
3421 3626
3422package CFPlus::UI::Tooltip; 3627package DC::UI::Tooltip;
3423 3628
3424our @ISA = CFPlus::UI::Bin::; 3629our @ISA = DC::UI::Bin::;
3425 3630
3426use CFPlus::OpenGL; 3631use DC::OpenGL;
3427 3632
3428sub new { 3633sub new {
3429 my $class = shift; 3634 my $class = shift;
3430 3635
3431 $class->SUPER::new ( 3636 $class->SUPER::new (
3435} 3640}
3436 3641
3437sub set_tooltip_from { 3642sub set_tooltip_from {
3438 my ($self, $widget) = @_; 3643 my ($self, $widget) = @_;
3439 3644
3440 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3441 if $widget->{tooltip} =~ /^#(.*)$/;
3442
3443 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 =~ /^#(.*)$/;
3444 3650
3445 if ($ENV{CFPLUS_DEBUG} & 2) { 3651 if ($ENV{CFPLUS_DEBUG} & 2) {
3446 $tooltip .= "\n\n" . (ref $widget) . "\n" 3652 $tip .= "\n\n" . (ref $widget) . "\n"
3447 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3653 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3448 . "req $widget->{req_w} $widget->{req_h}\n" 3654 . "req $widget->{req_w} $widget->{req_h}\n"
3449 . "visible $widget->{visible}"; 3655 . "visible $widget->{visible}";
3450 } 3656 }
3451 3657
3452 $tooltip =~ s/^\n+//; 3658 $tip =~ s/^\n+//;
3453 $tooltip =~ s/\n+$//; 3659 $tip =~ s/\n+$//;
3454 3660
3455 $self->add (new CFPlus::UI::Label 3661 $self->add (new DC::UI::Label
3662 fg => $DC::THEME{tooltip_fg},
3456 markup => $tooltip, 3663 markup => $tip,
3457 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3664 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3665 align => 0,
3458 fontsize => 0.8, 3666 fontsize => 0.8,
3459 style => 1, # FLAG_INVERSE 3667 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3460 ellipsise => 0, 3668 ellipsise => 0,
3461 font => ($widget->{tooltip_font} || $::FONT_PROP), 3669 font => ($widget->{tooltip_font} || $::FONT_PROP),
3462 ); 3670 );
3463} 3671}
3464 3672
3499} 3707}
3500 3708
3501sub _draw { 3709sub _draw {
3502 my ($self) = @_; 3710 my ($self) = @_;
3503 3711
3504 glTranslate 0.375, 0.375;
3505
3506 my ($w, $h) = @$self{qw(w h)}; 3712 my ($w, $h) = @$self{qw(w h)};
3507 3713
3508 glColor 1, 0.8, 0.4; 3714 glColor @{ $DC::THEME{tooltip_bg} };
3509 glRect 0, 0, $w, $h; 3715 glRect 0, 0, $w, $h;
3510 3716
3511 glColor 0, 0, 0; 3717 glColor @{ $DC::THEME{tooltip_border} };
3512 glBegin GL_LINE_LOOP; 3718 glRect_lineloop .5, .5, $w + .5, $h + .5;
3513 glVertex 0 , 0;
3514 glVertex 0 , $h;
3515 glVertex $w, $h;
3516 glVertex $w, 0;
3517 glEnd;
3518 3719
3519 glTranslate 2 - 0.375, 2 - 0.375; 3720 glTranslate 2, 2;
3520 3721
3521 $self->SUPER::_draw; 3722 $self->SUPER::_draw;
3522} 3723}
3523 3724
3524############################################################################# 3725#############################################################################
3525 3726
3526package CFPlus::UI::Face; 3727package DC::UI::Face;
3527 3728
3528our @ISA = CFPlus::UI::DrawBG::; 3729our @ISA = DC::UI::DrawBG::;
3529 3730
3530use CFPlus::OpenGL; 3731use DC::OpenGL;
3531 3732
3532sub new { 3733sub new {
3533 my $class = shift; 3734 my $class = shift;
3534 3735
3535 my $self = $class->SUPER::new ( 3736 my $self = $class->SUPER::new (
3538 aspect => 1, 3739 aspect => 1,
3539 can_events => 0, 3740 can_events => 0,
3540 @_, 3741 @_,
3541 ); 3742 );
3542 3743
3543 if ($self->{anim} && $self->{animspeed}) {
3544 CFPlus::weaken (my $widget = $self);
3545
3546 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3547 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3548 $self->{timer} = Event->timer (
3549 parked => 1,
3550 cb => sub {
3551 return unless $::CONN && $widget;
3552
3553 ++$widget->{frame};
3554 $widget->update_face;
3555 $widget->update;
3556
3557 $widget->update_timer;
3558 },
3559 );
3560
3561 $self->update_face;
3562 $self->update_timer; 3744 $self->update_anim;
3563 }
3564 3745
3565 $self 3746 $self
3566} 3747}
3567 3748
3568sub update_timer { 3749sub update_timer {
3569 my ($self) = @_; 3750 my ($self) = @_;
3570 3751
3571 return unless $self->{timer}; 3752 return unless $self->{timer};
3572 3753
3573 if ($self->{visible}) { 3754 if ($self->{visible}) {
3574 $self->{timer}->at (
3575 $self->{anim_start}
3576 + $self->{animspeed}
3577 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3578 );
3579 $self->{timer}->start; 3755 $self->{timer}->start;
3580 } else { 3756 } else {
3581 $self->{timer}->stop; 3757 $self->{timer}->stop;
3582 } 3758 }
3583} 3759}
3584 3760
3585sub update_face { 3761sub update_face {
3586 my ($self) = @_; 3762 my ($self) = @_;
3587 3763
3588 return unless $::CONN;
3589
3590 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3591 if ($anim && @$anim) {
3592 delete $self->{wait_face};
3593 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3594 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3595 }
3596 }
3597}
3598
3599sub size_request {
3600 my ($self) = @_;
3601
3602 if ($::CONN) { 3764 if ($::CONN) {
3603 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3604 if (my $tex = $::CONN->{texture}[$faceid]) { 3766 if ($anim && @$anim) {
3605 $self->{tex} = $tex; 3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3606 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3768 delete $self->{face_change_cb};
3607 } else { 3769
3608 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3770 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3609 $self->realloc; 3771 unless ($tex->{name} || $tex->{loading}) {
3772 $tex->upload (sub { $self->reconfigure });
3773 }
3610 }); 3774 }
3611 } 3775 }
3612 } 3776 }
3613 } 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 }
3614 3824
3615 ($self->{size_w} || 8, $self->{size_h} || 8) 3825 ($self->{size_w} || 8, $self->{size_h} || 8)
3616} 3826}
3617 3827
3618sub update { 3828sub update {
3619 my ($self) = @_; 3829 my ($self) = @_;
3620 3830
3621 return unless $self->{visible}; 3831 return unless $self->{visible};
3622 3832
3623 $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;
3624} 3855}
3625 3856
3626sub invoke_visibility_change { 3857sub invoke_visibility_change {
3627 my ($self) = @_; 3858 my ($self) = @_;
3628 3859
3654 $self->SUPER::destroy; 3885 $self->SUPER::destroy;
3655} 3886}
3656 3887
3657############################################################################# 3888#############################################################################
3658 3889
3659package CFPlus::UI::Buttonbar; 3890package DC::UI::Buttonbar;
3660 3891
3661our @ISA = CFPlus::UI::HBox::; 3892our @ISA = DC::UI::HBox::;
3662 3893
3663# TODO: should actually wrap buttons and other goodies. 3894# TODO: should actually wrap buttons and other goodies.
3664 3895
3665############################################################################# 3896#############################################################################
3666 3897
3667package CFPlus::UI::Menu; 3898package DC::UI::Menu;
3668 3899
3669our @ISA = CFPlus::UI::Toplevel::; 3900our @ISA = DC::UI::Toplevel::;
3670 3901
3671use CFPlus::OpenGL; 3902use DC::OpenGL;
3672 3903
3673sub new { 3904sub new {
3674 my $class = shift; 3905 my $class = shift;
3675 3906
3676 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3677 items => [], 3908 items => [],
3678 z => 100, 3909 z => 100,
3679 @_, 3910 @_,
3680 ); 3911 );
3681 3912
3682 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3913 $self->add ($self->{vbox} = new DC::UI::VBox);
3683 3914
3684 for my $item (@{ $self->{items} }) { 3915 for my $item (@{ $self->{items} }) {
3685 my ($widget, $cb, $tooltip) = @$item; 3916 my ($widget, $cb, $tooltip) = @$item;
3686 3917
3687 # handle various types of items, only text for now 3918 # handle various types of items, only text for now
3688 if (!ref $widget) { 3919 if (!ref $widget) {
3689 if ($widget =~ /\t/) { 3920 if ($widget =~ /\t/) {
3690 my ($left, $right) = split /\t/, $widget, 2; 3921 my ($left, $right) = split /\t/, $widget, 2;
3691 3922
3692 $widget = new CFPlus::UI::HBox 3923 $widget = new DC::UI::HBox
3693 can_hover => 1, 3924 can_hover => 1,
3694 can_events => 1, 3925 can_events => 1,
3695 tooltip => $tooltip, 3926 tooltip => $tooltip,
3696 children => [ 3927 children => [
3697 (new CFPlus::UI::Label markup => $left, expand => 1), 3928 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3698 (new CFPlus::UI::Label markup => $right, align => +1), 3929 (new DC::UI::Label markup => $right, align => 1),
3699 ], 3930 ],
3700 ; 3931 ;
3701 3932
3702 } else { 3933 } else {
3703 $widget = new CFPlus::UI::Label 3934 $widget = new DC::UI::Label
3704 can_hover => 1, 3935 can_hover => 1,
3705 can_events => 1, 3936 can_events => 1,
3937 align => 0,
3706 markup => $widget, 3938 markup => $widget,
3707 tooltip => $tooltip; 3939 tooltip => $tooltip;
3708 } 3940 }
3709 } 3941 }
3710 3942
3725 # maybe save $GRAB? must be careful about events... 3957 # maybe save $GRAB? must be careful about events...
3726 $GRAB = $self; 3958 $GRAB = $self;
3727 $self->{button} = $ev->{button}; 3959 $self->{button} = $ev->{button};
3728 3960
3729 $self->show; 3961 $self->show;
3730 $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
3731} 3971}
3732 3972
3733sub invoke_mouse_motion { 3973sub invoke_mouse_motion {
3734 my ($self, $ev, $x, $y) = @_; 3974 my ($self, $ev, $x, $y) = @_;
3735 3975
3756 1 3996 1
3757} 3997}
3758 3998
3759############################################################################# 3999#############################################################################
3760 4000
3761package CFPlus::UI::Multiplexer; 4001package DC::UI::Multiplexer;
3762 4002
3763our @ISA = CFPlus::UI::Container::; 4003our @ISA = DC::UI::Container::;
3764 4004
3765sub new { 4005sub new {
3766 my $class = shift; 4006 my $class = shift;
3767 4007
3768 my $self = $class->SUPER::new ( 4008 my $self = $class->SUPER::new (
3769 @_, 4009 @_,
3770 ); 4010 );
3771 4011
3772 $self->{current} = $self->{children}[0] 4012 $self->set_current_page (0);
3773 if @{ $self->{children} };
3774 4013
3775 $self 4014 $self
3776} 4015}
3777 4016
3778sub add { 4017sub add {
3779 my ($self, @widgets) = @_; 4018 my ($self, @widgets) = @_;
3780 4019
3781 $self->SUPER::add (@widgets); 4020 $self->SUPER::add (@widgets);
3782 4021
3783 $self->{current} = $self->{children}[0] 4022 $self->set_current_page (0)
3784 if @{ $self->{children} }; 4023 if @widgets == @{ $self->{children} };
3785} 4024}
3786 4025
3787sub get_current_page { 4026sub get_current_page {
3788 my ($self) = @_; 4027 my ($self) = @_;
3789 4028
3795 4034
3796 my $widget = ref $page_or_widget 4035 my $widget = ref $page_or_widget
3797 ? $page_or_widget 4036 ? $page_or_widget
3798 : $self->{children}[$page_or_widget]; 4037 : $self->{children}[$page_or_widget];
3799 4038
4039 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4040
3800 $self->{current} = $widget; 4041 if (($self->{current} = $widget)) {
4042 $self->{current}->set_visible if $self->{current} && $self->{visible};
3801 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4043 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3802 4044
3803 $self->emit (page_changed => $self->{current}); 4045 $self->emit (page_changed => $self->{current});
4046 }
3804 4047
3805 $self->realloc; 4048 $self->realloc;
3806} 4049}
3807 4050
3808sub visible_children { 4051sub visible_children {
3809 $_[0]{current} 4052 $_[0]{current} || ()
3810} 4053}
3811 4054
3812sub size_request { 4055sub size_request {
3813 my ($self) = @_; 4056 my ($self) = @_;
3814 4057
4058 $self->{current}
3815 $self->{current}->size_request 4059 ? $self->{current}->size_request
4060 : (0, 0)
3816} 4061}
3817 4062
3818sub invoke_size_allocate { 4063sub invoke_size_allocate {
3819 my ($self, $w, $h) = @_; 4064 my ($self, $w, $h) = @_;
3820 4065
3821 $self->{current}->configure (0, 0, $w, $h); 4066 $self->{current}->configure (0, 0, $w, $h)
4067 if $self->{current};
3822 4068
3823 1 4069 1
3824} 4070}
3825 4071
3826sub _draw { 4072sub _draw {
3827 my ($self) = @_; 4073 my ($self) = @_;
3828 4074
3829 $self->{current}->draw; 4075 $self->{current}->draw
4076 if $self->{current};
3830} 4077}
3831 4078
3832############################################################################# 4079#############################################################################
3833 4080
3834package CFPlus::UI::Notebook; 4081package DC::UI::Notebook;
3835 4082
4083use DC::OpenGL;
4084
3836our @ISA = CFPlus::UI::VBox::; 4085our @ISA = DC::UI::VBox::;
3837 4086
3838sub new { 4087sub new {
3839 my $class = shift; 4088 my $class = shift;
3840 4089
3841 my $self = $class->SUPER::new ( 4090 my $self = $class->SUPER::new (
3842 buttonbar => (new CFPlus::UI::Buttonbar), 4091 buttonbar => (new DC::UI::Buttonbar),
3843 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4092 multiplexer => (new DC::UI::Multiplexer expand => 1),
4093 active_outline => [.7, .7, 0.2],
3844 # filter => # will be put between multiplexer and $self 4094 # filter => # will be put between multiplexer and $self
3845 @_, 4095 @_,
3846 ); 4096 );
3847 4097
3848 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4098 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3856 4106
3857 for my $child (@$widgets) { 4107 for my $child (@$widgets) {
3858 Scalar::Util::weaken $child; 4108 Scalar::Util::weaken $child;
3859 $child->{c_tab_} ||= do { 4109 $child->{c_tab_} ||= do {
3860 my $tab = 4110 my $tab =
3861 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4111 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3862 ? $child->{c_tab} 4112 ? $child->{c_tab}
3863 : 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];
3864 4114
3865 $tab->connect (activate => sub { 4115 $tab->connect (activate => sub {
3866 $wself->set_current_page ($child); 4116 $wself->set_current_page ($child);
3867 }); 4117 });
3868 4118
3900sub pages { 4150sub pages {
3901 my ($self) = @_; 4151 my ($self) = @_;
3902 $self->{multiplexer}->children 4152 $self->{multiplexer}->children
3903} 4153}
3904 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
3905sub add_tab { 4167sub add_tab {
3906 my ($self, $title, $widget, $tooltip) = @_; 4168 my ($self, $title, $widget, $tooltip) = @_;
3907 4169
3908 $title = [$title, $tooltip] unless ref $title; 4170 $title = [$title, $tooltip] unless ref $title;
3909 $widget->{c_tab} = $title; 4171 $widget->{c_tab} = $title;
3922 4184
3923 $self->{multiplexer}->set_current_page ($page); 4185 $self->{multiplexer}->set_current_page ($page);
3924 $self->emit (page_changed => $self->{multiplexer}{current}); 4186 $self->emit (page_changed => $self->{multiplexer}{current});
3925} 4187}
3926 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
3927############################################################################# 4209#############################################################################
3928 4210
3929package CFPlus::UI::Selector; 4211package DC::UI::Selector;
3930 4212
3931use utf8; 4213use utf8;
3932 4214
3933our @ISA = CFPlus::UI::Button::; 4215our @ISA = DC::UI::Button::;
3934 4216
3935sub new { 4217sub new {
3936 my $class = shift; 4218 my $class = shift;
3937 4219
3938 my $self = $class->SUPER::new ( 4220 my $self = $class->SUPER::new (
3955 my ($value, $title, $tooltip) = @$_; 4237 my ($value, $title, $tooltip) = @$_;
3956 4238
3957 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4239 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3958 } 4240 }
3959 4241
3960 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4242 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3961} 4243}
3962 4244
3963sub _set_value { 4245sub _set_value {
3964 my ($self, $value) = @_; 4246 my ($self, $value) = @_;
3965 4247
3966 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4248 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4249 $item ||= $self->{options}[0]
3967 or return; 4250 or return;
3968 4251
3969 $self->{value} = $item->[0]; 4252 $self->{value} = $item->[0];
3970 $self->set_markup ("$item->[1] ⇓"); 4253 $self->set_markup ("$item->[1] ⇓");
3971 $self->set_tooltip ($item->[2]); 4254# $self->set_tooltip ($item->[2]);
3972} 4255}
3973 4256
3974sub set_value { 4257sub set_value {
3975 my ($self, $value) = @_; 4258 my ($self, $value) = @_;
3976 4259
3978 4261
3979 $self->_set_value ($value); 4262 $self->_set_value ($value);
3980 $self->emit (changed => $value); 4263 $self->emit (changed => $value);
3981} 4264}
3982 4265
4266sub set_options {
4267 my ($self, $options) = @_;
4268
4269 $self->{options} = $options;
4270 $self->_set_value ($self->{value});
4271}
4272
3983############################################################################# 4273#############################################################################
3984 4274
3985package CFPlus::UI::Statusbox; 4275package DC::UI::Statusbox;
3986 4276
3987our @ISA = CFPlus::UI::VBox::; 4277our @ISA = DC::UI::VBox::;
3988 4278
3989sub new { 4279sub new {
3990 my $class = shift; 4280 my $class = shift;
3991 4281
3992 my $self = $class->SUPER::new ( 4282 my $self = $class->SUPER::new (
3993 fontsize => 0.8, 4283 fontsize => 0.8,
3994 @_, 4284 @_,
3995 ); 4285 );
3996 4286
3997 CFPlus::weaken (my $this = $self); 4287 DC::weaken (my $this = $self);
3998 4288
3999 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4289 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4000 4290
4001 $self 4291 $self
4002} 4292}
4003 4293
4004sub reorder { 4294sub reorder {
4005 my ($self) = @_; 4295 my ($self) = @_;
4006 my $NOW = Time::HiRes::time; 4296 my $NOW = EV::time;
4007 4297
4008 # freeze display when hovering over any label 4298 # freeze display when hovering over any label
4009 return if $CFPlus::UI::TOOLTIP->{owner} 4299 return if $DC::UI::TOOLTIP->{owner}
4010 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4300 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4011 values %{ $self->{item} }; 4301 values %{ $self->{item} };
4012 4302
4013 while (my ($k, $v) = each %{ $self->{item} }) { 4303 while (my ($k, $v) = each %{ $self->{item} }) {
4014 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4304 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4015 } 4305 }
4306
4307 $self->{timer}->set (1, 1);
4016 4308
4017 my @widgets; 4309 my @widgets;
4018 4310
4019 my @items = sort { 4311 my @items = sort {
4020 $a->{pri} <=> $b->{pri} 4312 $a->{pri} <=> $b->{pri}
4021 or $b->{id} <=> $a->{id} 4313 or $b->{id} <=> $a->{id}
4022 } values %{ $self->{item} }; 4314 } values %{ $self->{item} };
4023
4024 $self->{timer}->interval (1);
4025 4315
4026 my $count = 10 + 1; 4316 my $count = 10 + 1;
4027 for my $item (@items) { 4317 for my $item (@items) {
4028 last unless --$count; 4318 last unless --$count;
4029 4319
4036 for ($short) { 4326 for ($short) {
4037 s/^\s+//; 4327 s/^\s+//;
4038 s/\s+/ /g; 4328 s/\s+/ /g;
4039 } 4329 }
4040 4330
4041 new CFPlus::UI::Label 4331 new DC::UI::Label
4042 markup => $short, 4332 markup => $short,
4043 tooltip => $item->{tooltip}, 4333 tooltip => $item->{tooltip},
4044 tooltip_font => $::FONT_PROP, 4334 tooltip_font => $::FONT_PROP,
4045 tooltip_width => 0.67, 4335 tooltip_width => 0.67,
4046 fontsize => $item->{fontsize} || $self->{fontsize}, 4336 fontsize => $item->{fontsize} || $self->{fontsize},
4047 max_w => $::WIDTH * 0.44, 4337 max_w => $::WIDTH * 0.44,
4338 align => 0,
4048 fg => [@{ $item->{fg} }], 4339 fg => [@{ $item->{fg} }],
4049 can_events => 1, 4340 can_events => 1,
4050 can_hover => 1 4341 can_hover => 1
4051 }; 4342 };
4052 4343
4053 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4344 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4054 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4345 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4055 $label->update; 4346 $label->update;
4056 $label->set_max_size (undef, $label->{req_h} * $diff) 4347 $label->set_max_size (undef, $label->{req_h} * $diff)
4057 if $diff < 1; 4348 if $diff < 1;
4058 $self->{timer}->interval (1/30); 4349 $self->{timer}->set (1/30, 1/30);
4059 } else { 4350 } else {
4060 $label->{fg}[3] = $item->{fg}[3] || 1; 4351 $label->{fg}[3] = $item->{fg}[3] || 1;
4061 } 4352 }
4062 4353
4063 push @widgets, $label; 4354 push @widgets, $label;
4064 } 4355 }
4356
4357 my $hash = join ",", @widgets;
4358 return if $hash eq $self->{last_widget_hash};
4359 $self->{last_widget_hash} = $hash;
4065 4360
4066 $self->clear; 4361 $self->clear;
4067 $self->SUPER::add (reverse @widgets); 4362 $self->SUPER::add (reverse @widgets);
4068} 4363}
4069 4364
4125 $self->SUPER::destroy; 4420 $self->SUPER::destroy;
4126} 4421}
4127 4422
4128############################################################################# 4423#############################################################################
4129 4424
4130package CFPlus::UI::Root; 4425package DC::UI::Root;
4131 4426
4132our @ISA = CFPlus::UI::Container::; 4427our @ISA = DC::UI::Container::;
4133 4428
4134use List::Util qw(min max); 4429use List::Util qw(min max);
4135 4430
4136use CFPlus::OpenGL; 4431use DC::OpenGL;
4137 4432
4138sub new { 4433sub new {
4139 my $class = shift; 4434 my $class = shift;
4140 4435
4141 my $self = $class->SUPER::new ( 4436 my $self = $class->SUPER::new (
4142 visible => 1, 4437 visible => 1,
4143 @_, 4438 @_,
4144 ); 4439 );
4145 4440
4146 CFPlus::weaken ($self->{root} = $self); 4441 DC::weaken ($self->{root} = $self);
4147 4442
4148 $self 4443 $self
4149} 4444}
4150 4445
4151sub size_request { 4446sub size_request {
4199} 4494}
4200 4495
4201sub update { 4496sub update {
4202 my ($self) = @_; 4497 my ($self) = @_;
4203 4498
4204 $::WANT_REFRESH++; 4499 $::WANT_REFRESH = 1;
4205} 4500}
4206 4501
4207sub add { 4502sub add {
4208 my ($self, @children) = @_; 4503 my ($self, @children) = @_;
4209 4504
4276 4571
4277 delete $queue{$widget+0}; 4572 delete $queue{$widget+0};
4278 4573
4279 my ($w, $h) = $widget->size_request; 4574 my ($w, $h) = $widget->size_request;
4280 4575
4281 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4576 $w += $widget->{padding_x} * 2;
4282 $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;
4283 4581
4284 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4582 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4285 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4583 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4286 4584
4287 $w = $widget->{force_w} if exists $widget->{force_w}; 4585 $w = $widget->{force_w} if exists $widget->{force_w};
4354 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4652 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4355 glMatrixMode GL_MODELVIEW; 4653 glMatrixMode GL_MODELVIEW;
4356 glLoadIdentity; 4654 glLoadIdentity;
4357 4655
4358 { 4656 {
4359 package CFPlus::UI::Base; 4657 package DC::UI::Base;
4360 4658
4361 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4659 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4362 (0, 0, $self->{w}, $self->{h}); 4660 (0, 0, $self->{w}, $self->{h});
4363 4661
4364 $self->_draw; 4662 $self->_draw;
4365 } 4663 }
4366} 4664}
4367 4665
4368############################################################################# 4666#############################################################################
4369 4667
4370package CFPlus::UI; 4668package DC::UI;
4371 4669
4372$ROOT = new CFPlus::UI::Root; 4670$ROOT = new DC::UI::Root;
4373$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4671$TOOLTIP = new DC::UI::Tooltip z => 900;
4374 4672
43751 46731
4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines