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.439 by root, Sun Sep 16 13:04:36 2007 UTC vs.
Revision 1.484 by root, Sun Jan 8 07:58:39 2012 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
6
7use Event; 7use AnyEvent ();
8use Guard ();
8 9
9use CFPlus; 10use DC;
10use CFPlus::Pod; 11use DC::Pod;
11use CFPlus::Texture; 12use DC::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
17our $TOOLTIP; 18our $TOOLTIP;
18our $BUTTON_STATE; 19our $BUTTON_STATE;
19 20
20our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
21 22
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
24 $_[0]->stop;
25
23 if (!$GRAB) { 26 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 29 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 30 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 45 }
43 46
44 $TOOLTIP->hide; 47 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 48 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 49 delete $TOOLTIP->{owner};
47}); 50};
48 51
49sub get_layout { 52sub get_layout {
50 my $layout; 53 my $layout;
51 54
52 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
88 91
89 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 94
92 $TOOLTIP_WATCHER->start; 95 $TOOLTIP_WATCHER->again;
93 } 96 }
94} 97}
95 98
96sub feed_sdl_button_down_event { 99sub feed_sdl_button_down_event {
97 my ($ev) = @_; 100 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 106 my $widget = $ROOT->find_widget ($x, $y);
104 107
105 $GRAB = $widget; 108 $GRAB = $widget;
106 $GRAB->update if $GRAB; 109 $GRAB->update if $GRAB;
107 110
108 $TOOLTIP_WATCHER->cb->(); 111 $TOOLTIP_WATCHER->invoke;
109 } 112 }
110 113
111 if ($GRAB) { 114 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 116 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 119
117 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
119 122
120 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 141 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 142 $grab->update if $grab;
140 $GRAB->update if $GRAB; 143 $GRAB->update if $GRAB;
141 144
142 check_hover $widget; 145 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 146 $TOOLTIP_WATCHER->invoke;
144 } 147 }
145} 148}
146 149
147sub feed_sdl_motion_event { 150sub feed_sdl_motion_event {
148 my ($ev) = @_; 151 my ($ev) = @_;
188# call when resolution changes etc. 191# call when resolution changes etc.
189sub rescale_widgets { 192sub rescale_widgets {
190 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
191 194
192 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 199
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 213 reconfigure_widgets;
211} 214}
212 215
213############################################################################# 216#############################################################################
214 217
215package CFPlus::UI::Event; 218package DC::UI::Event;
216 219
217sub xy { 220sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 222}
220 223
221############################################################################# 224#############################################################################
222 225
223package CFPlus::UI::Base; 226package DC::UI::Base;
224 227
225use strict; 228use common::sense;
226 229
227use CFPlus::OpenGL; 230use DC::OpenGL;
228 231
229sub new { 232sub new {
230 my $class = shift; 233 my $class = shift;
231 234
232 my $self = bless { 235 my $self = bless {
237 h => undef, 240 h => undef,
238 can_events => 1, 241 can_events => 1,
239 @_ 242 @_
240 }, $class; 243 }, $class;
241 244
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 246
244 for (keys %$self) { 247 for (keys %$self) {
245 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
247 } 250 }
248 } 251 }
249 252
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 258
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 261
259 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
277sub show { 280sub show {
278 my ($self) = @_; 281 my ($self) = @_;
279 282
280 return if $self->{parent}; 283 return if $self->{parent};
281 284
282 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
283} 286}
284 287
285sub set_visible { 288sub set_visible {
286 my ($self) = @_; 289 my ($self) = @_;
287 290
288 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 295
290 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
292 298
293 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
294 300
295 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
296 302
297 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
298} 304}
299 305
300sub set_invisible { 306sub set_invisible {
301 my ($self) = @_; 307 my ($self) = @_;
302 308
308 delete $self->{root}; 314 delete $self->{root};
309 315
310 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
312 318
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 319 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
315 321
316 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
318} 324}
337sub hide { 343sub hide {
338 my ($self) = @_; 344 my ($self) = @_;
339 345
340 $self->set_invisible; 346 $self->set_invisible;
341 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 351 $parent->remove ($self);
343 if $self->{parent}; 352 }
344} 353}
345 354
346sub move_abs { 355sub move_abs {
347 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
348 357
354} 363}
355 364
356sub set_size { 365sub set_size {
357 my ($self, $w, $h) = @_; 366 my ($self, $w, $h) = @_;
358 367
359 $self->{force_w} = $w; 368 $self->{force_w} = List::Util::min $w, ($self->{max_w} || $::WIDTH );
360 $self->{force_h} = $h; 369 $self->{force_h} = List::Util::min $h, ($self->{max_h} || $::HEIGHT);
361 370
362 $self->realloc; 371 $self->realloc;
363} 372}
364 373
365# travsle the widget chain up to find the maximum "physical" size constraints 374# traverse the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh { 375sub get_max_wh {
367 my ($self) = @_; 376 my ($self) = @_;
368 377
369 return ($self->{alloc_w}, $self->{alloc_h}) 378 my ($w, $h) = @$self{qw(max_w max_h)};
370 if $self->{alloc_w} && !$self->{root}{realloc}{$self+0};
371 379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
372 return $self->{parent}->get_max_wh 382 ? $self->{parent}->get_max_wh
373 if $self->{parent};
374
375 ($::WIDTH, $::HEIGHT) 383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
376} 390}
377 391
378sub size_request { 392sub size_request {
379 require Carp; 393 require Carp;
380 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
388 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
389 403
390 if ($self->{aspect}) { 404 if ($self->{aspect}) {
391 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
392 406
393 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
394 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
395 409
396 # use alignment to adjust x, y 410 # use alignment to adjust x, y
397 411
398 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
399 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
440 454
441 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
442 456
443 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
444 458
445 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
446 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
447 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
448 } 462 }
449} 463}
450 464
451# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
452sub coord2local { 466sub coord2local {
453 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
454 468
455 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
456 470
457 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
458} 472}
459 473
460# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
461sub coord2global { 475sub coord2global {
462 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
463 477
464 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
465 479
466 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
467} 481}
468 482
469sub invoke_focus_in { 483sub invoke_focus_in {
518sub connect { 532sub connect {
519 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
520 534
521 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
522 536
523 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
524 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
525 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
526 } 540 }
527} 541}
528 542
566# in .xs 580# in .xs
567 581
568sub set_parent { 582sub set_parent {
569 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
570 584
571 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
572 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
573} 587}
574 588
575sub realloc { 589sub realloc {
576 my ($self) = @_; 590 my ($self) = @_;
614} 628}
615 629
616sub DESTROY { 630sub DESTROY {
617 my ($self) = @_; 631 my ($self) = @_;
618 632
619 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
620 634
621 local $@; 635 local $@;
622 eval { $self->destroy }; 636 eval { $self->destroy };
623 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
624 638
625 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
626} 640}
627 641
628############################################################################# 642#############################################################################
629 643
630package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
631 645
632our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
633 647
634use strict; 648use common::sense;
649
635use CFPlus::OpenGL; 650use DC::OpenGL;
636 651
637sub new { 652sub new {
638 my $class = shift; 653 my $class = shift;
639 654
640 $class->SUPER::new ( 655 $class->SUPER::new (
642 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
643 @_ 658 @_
644 ) 659 )
645} 660}
646 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
647sub _draw { 669sub _draw {
648 my ($self) = @_; 670 my ($self) = @_;
649 671
650 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
651 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
652 : $self->{bg}; 674 : $self->{bg};
653 675
654 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
655 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
656 678
662 } 684 }
663} 685}
664 686
665############################################################################# 687#############################################################################
666 688
667package CFPlus::UI::Empty; 689package DC::UI::Empty;
668 690
669our @ISA = CFPlus::UI::Base::; 691our @ISA = DC::UI::Base::;
670 692
671sub new { 693sub new {
672 my ($class, %arg) = @_; 694 my ($class, %arg) = @_;
673 $class->SUPER::new (can_events => 0, %arg); 695 $class->SUPER::new (can_events => 0, %arg);
674} 696}
681 703
682sub draw { } 704sub draw { }
683 705
684############################################################################# 706#############################################################################
685 707
686package CFPlus::UI::Container; 708package DC::UI::Container;
687 709
688our @ISA = CFPlus::UI::Base::; 710our @ISA = DC::UI::Base::;
689 711
690sub new { 712sub new {
691 my ($class, %arg) = @_; 713 my ($class, %arg) = @_;
692 714
693 my $children = delete $arg{children}; 715 my $children = delete $arg{children};
787 $_->draw for $self->visible_children; 809 $_->draw for $self->visible_children;
788} 810}
789 811
790############################################################################# 812#############################################################################
791 813
792package CFPlus::UI::Bin; 814package DC::UI::Bin;
793 815
794our @ISA = CFPlus::UI::Container::; 816our @ISA = DC::UI::Container::;
795 817
796sub new { 818sub new {
797 my ($class, %arg) = @_; 819 my ($class, %arg) = @_;
798 820
799 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 821 my $child = (delete $arg{child}) || new DC::UI::Empty::;
800 822
801 $class->SUPER::new (children => [$child], %arg) 823 $class->SUPER::new (children => [$child], %arg)
802} 824}
803 825
804sub add { 826sub add {
811sub remove { 833sub remove {
812 my ($self, $widget) = @_; 834 my ($self, $widget) = @_;
813 835
814 $self->SUPER::remove ($widget); 836 $self->SUPER::remove ($widget);
815 837
816 $self->{children} = [new CFPlus::UI::Empty] 838 $self->{children} = [new DC::UI::Empty]
817 unless @{$self->{children}}; 839 unless @{$self->{children}};
818} 840}
819 841
820sub child { $_[0]->{children}[0] } 842sub child { $_[0]->{children}[0] }
821 843
832} 854}
833 855
834############################################################################# 856#############################################################################
835# back-buffered drawing area 857# back-buffered drawing area
836 858
837package CFPlus::UI::Window; 859package DC::UI::Window;
838 860
839our @ISA = CFPlus::UI::Bin::; 861our @ISA = DC::UI::Bin::;
840 862
841use CFPlus::OpenGL; 863use DC::OpenGL;
842 864
843sub new { 865sub new {
844 my ($class, %arg) = @_; 866 my ($class, %arg) = @_;
845 867
846 my $self = $class->SUPER::new (%arg); 868 my $self = $class->SUPER::new (%arg);
868} 890}
869 891
870sub render_child { 892sub render_child {
871 my ($self) = @_; 893 my ($self) = @_;
872 894
873 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 895 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
874 glClearColor 0, 0, 0, 0; 896 glClearColor 0, 0, 0, 0;
875 glClear GL_COLOR_BUFFER_BIT; 897 glClear GL_COLOR_BUFFER_BIT;
876 898
877 { 899 {
878 package CFPlus::UI::Base; 900 package DC::UI::Base;
879 901
880 local ($draw_x, $draw_y, $draw_w, $draw_h) = 902 local ($draw_x, $draw_y, $draw_w, $draw_h) =
881 (0, 0, $self->{w}, $self->{h}); 903 (0, 0, $self->{w}, $self->{h});
882 904
883 $self->_render; 905 $self->_render;
900 glDisable GL_TEXTURE_2D; 922 glDisable GL_TEXTURE_2D;
901} 923}
902 924
903############################################################################# 925#############################################################################
904 926
905package CFPlus::UI::ViewPort; 927package DC::UI::ViewPort;
906 928
907use List::Util qw(min max); 929use List::Util qw(min max);
908 930
909our @ISA = CFPlus::UI::Window::; 931our @ISA = DC::UI::Window::;
910 932
911sub new { 933sub new {
912 my $class = shift; 934 my $class = shift;
913 935
914 $class->SUPER::new ( 936 $class->SUPER::new (
998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
999 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1000 ) { 1022 ) {
1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1023 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1002 } else { 1024 } else {
1003 $self->CFPlus::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
1004 } 1026 }
1005} 1027}
1006 1028
1007sub _render { 1029sub _render {
1008 my ($self) = @_; 1030 my ($self) = @_;
1009 1031
1010 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1032 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}; 1033 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1012 1034
1013 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1014 1036
1015 $self->SUPER::_render; 1037 $self->SUPER::_render;
1016} 1038}
1017 1039
1018############################################################################# 1040#############################################################################
1019 1041
1020package CFPlus::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1021 1043
1022our @ISA = CFPlus::UI::Table::; 1044our @ISA = DC::UI::Table::;
1023 1045
1024sub new { 1046sub new {
1025 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1026 1048
1027 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1028 1050
1029 my $self; 1051 my $self;
1030 1052
1031 my $hslider = new CFPlus::UI::Slider 1053 my $hslider = new DC::UI::Slider
1032 c_col => 0, 1054 c_col => 0,
1033 c_row => 1, 1055 c_row => 1,
1034 vertical => 0, 1056 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub { 1058 on_changed => sub {
1037 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 }, 1061 },
1040 ; 1062 ;
1041 1063
1042 my $vslider = new CFPlus::UI::Slider 1064 my $vslider = new DC::UI::Slider
1043 c_col => 1, 1065 c_col => 1,
1044 c_row => 0, 1066 c_row => 0,
1045 vertical => 1, 1067 vertical => 1,
1046 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1047 on_changed => sub { 1069 on_changed => sub {
1059 col_expand => [1, 0], 1081 col_expand => [1, 0],
1060 row_expand => [1, 0], 1082 row_expand => [1, 0],
1061 %arg, 1083 %arg,
1062 ); 1084 );
1063 1085
1064 $self->{vp} = new CFPlus::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1065 c_col => 0, 1087 c_col => 0,
1066 c_row => 0, 1088 c_row => 0,
1067 expand => 1, 1089 expand => 1,
1068 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1195 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1196} 1218}
1197 1219
1198############################################################################# 1220#############################################################################
1199 1221
1200package CFPlus::UI::Frame; 1222package DC::UI::Frame;
1201 1223
1202our @ISA = CFPlus::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1203 1225
1204use CFPlus::OpenGL; 1226use DC::OpenGL;
1205 1227
1206sub new { 1228sub new {
1207 my $class = shift; 1229 my $class = shift;
1208 1230
1209 $class->SUPER::new ( 1231 $class->SUPER::new (
1228 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1229} 1251}
1230 1252
1231############################################################################# 1253#############################################################################
1232 1254
1233package CFPlus::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1234 1256
1235our @ISA = CFPlus::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1236 1258
1237use CFPlus::OpenGL; 1259use DC::OpenGL;
1238 1260
1239sub new { 1261sub new {
1240 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1241 1263
1242 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1243 $arg{label} = new CFPlus::UI::Label 1265 $arg{label} = new DC::UI::Label
1244 align => 1, 1266 align => 1,
1245 valign => 0, 1267 valign => 0.5,
1246 text => $arg{label}, 1268 text => $arg{label},
1247 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1248 } 1270 }
1249 1271
1250 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1251 # label => "", 1273 # label => "",
1252 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1253 border => 0.8, 1275 border => 0.8,
1254 style => 'single', 1276 style => 'single',
1255 %arg, 1277 %arg,
1256 ); 1278 );
1257 1279
1260 1282
1261sub add { 1283sub add {
1262 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1263 1285
1264 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1265 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1266} 1288}
1267 1289
1268sub border { 1290sub border {
1269 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1270} 1292}
1310 my $border = $self->border; 1332 my $border = $self->border;
1311 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1312 1334
1313 $child->draw; 1335 $child->draw;
1314 1336
1315 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1316 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1317 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1318 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1320 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1342 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1328 } 1350 }
1329} 1351}
1330 1352
1331############################################################################# 1353#############################################################################
1332 1354
1333package CFPlus::UI::Toplevel; 1355package DC::UI::Toplevel;
1334 1356
1335our @ISA = CFPlus::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1336 1358
1337use CFPlus::OpenGL; 1359use DC::OpenGL;
1338 1360
1339my $bg = 1361my $bg =
1340 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1341 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1342 1364
1343my @border = 1365my @border =
1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1345 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1367 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1346 1368
1347my @icon = 1369my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1370 map { new_from_resource DC::Texture $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png); 1371 qw(x1_move.png x1_resize.png);
1350 1372
1351sub new { 1373sub new {
1352 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1353 1375
1354 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1355 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1356 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1357 border => 0.6, 1379 border => 0.8,
1358 can_events => 1, 1380 can_events => 1,
1359 min_w => 64, 1381 min_w => 64,
1360 min_h => 32, 1382 min_h => 32,
1361 %arg, 1383 %arg,
1362 ); 1384 );
1363 1385
1364 $self->{title_widget} = new CFPlus::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1365 align => 0, 1387 align => 0.5,
1366 valign => 1, 1388 valign => 1,
1367 text => $self->{title}, 1389 text => $self->{title},
1368 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1369 if exists $self->{title}; 1391 if exists $self->{title};
1370 1392
1371 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1372 $self->{close_button} = 1394 $self->{close_button} =
1373 new CFPlus::UI::ImageButton 1395 new DC::UI::ImageButton
1374 path => 'x1_close.png', 1396 path => 'x1_close.png',
1375 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1376 1398
1377 $self->CFPlus::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1378 } 1400 }
1379 1401
1380 $self 1402 $self
1381} 1403}
1382 1404
1383sub add { 1405sub add {
1384 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1385 1407
1386 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1387 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1409 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1388 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1410 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1389} 1411}
1390 1412
1391sub border { 1413sub border {
1392 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1415}
1416
1417sub get_max_wh {
1418 my ($self) = @_;
1419
1420 return ($self->{w}, $self->{h})
1421 if $self->{visible} && $self->{w};
1422
1423 $self->SUPER::get_max_wh
1393} 1424}
1394 1425
1395sub size_request { 1426sub size_request {
1396 my ($self) = @_; 1427 my ($self) = @_;
1397 1428
1443 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1444 1475
1445 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1446 my $border = $self->border; 1477 my $border = $self->border;
1447 1478
1448 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); 1479 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); # left-right
1449 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); 1480 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); # top-down
1450 1481
1451 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1452 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1453 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1454 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1455 1486
1456 my $mx = $x < $border; 1487 my $mx = $x < $border;
1460 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1461 1492
1462 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1463 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1464 1495
1496 $self->set_size (
1465 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1466 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1467 1500
1468 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1469 $self->realloc; 1502 $self->realloc;
1470 }; 1503 };
1471 1504
1472 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1473 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1474 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1475 1508
1476 $self->{motion} = sub { 1509 $self->{motion} = sub {
1477 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1522 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1523 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1524 1557
1525 my $border = $self->border; 1558 my $border = $self->border;
1526 1559
1560 if ($border) {
1527 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1528 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1529 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1530 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1531 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1565 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1532 1566
1533 # move 1567 # move
1534 my $w2 = ($w - $border) * .5; 1568 my $w2 = ($w - $border) * .5;
1535 my $h2 = ($h - $border) * .5; 1569 my $h2 = ($h - $border) * .5;
1536 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1570 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1537 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1571 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1538 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1572 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1539 1573
1540 # resize 1574 # resize
1541 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1542 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1576 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1543 unless $self->{has_close_button}; 1577 unless $self->{has_close_button};
1544 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1578 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1545 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1579 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1580 }
1546 1581
1547 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1548 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1549 1584
1550 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
1569 if $self->{close_button}; 1604 if $self->{close_button};
1570} 1605}
1571 1606
1572############################################################################# 1607#############################################################################
1573 1608
1574package CFPlus::UI::Table; 1609package DC::UI::Table;
1575 1610
1576our @ISA = CFPlus::UI::Container::; 1611our @ISA = DC::UI::Container::;
1577 1612
1578use List::Util qw(max sum); 1613use List::Util qw(max sum);
1579 1614
1580use CFPlus::OpenGL; 1615use DC::OpenGL;
1581 1616
1582sub new { 1617sub new {
1583 my $class = shift; 1618 my $class = shift;
1584 1619
1585 $class->SUPER::new ( 1620 $class->SUPER::new (
1629 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1664 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1630 1665
1631 my $sw = sum @w[$c .. $c + $cs - 1]; 1666 my $sw = sum @w[$c .. $c + $cs - 1];
1632 1667
1633 if ($w > $sw) { 1668 if ($w > $sw) {
1634 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1669 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1635 } 1670 }
1636 } 1671 }
1637 1672
1638 # second pass, rows 1673 # second pass, rows
1639 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1674 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1640 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1675 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1641 1676
1642 my $sh = sum @h[$r .. $r + $rs - 1]; 1677 my $sh = sum @h[$r .. $r + $rs - 1];
1643 1678
1644 if ($h > $sh) { 1679 if ($h > $sh) {
1645 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1680 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1646 } 1681 }
1647 } 1682 }
1648 1683
1649 (\@w, \@h) 1684 (\@w, \@h)
1650} 1685}
1673 @col_expand = (1) x @$ws unless @col_expand; 1708 @col_expand = (1) x @$ws unless @col_expand;
1674 my $col_expand = (sum @col_expand) || 1; 1709 my $col_expand = (sum @col_expand) || 1;
1675 1710
1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1711 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1677 1712
1678 CFPlus::UI::harmonize $ws; 1713 DC::UI::harmonize $ws;
1679 1714
1680 my @row_expand = @{$self->{row_expand}}; 1715 my @row_expand = @{$self->{row_expand}};
1681 @row_expand = (1) x @$ws unless @row_expand; 1716 @row_expand = (1) x @$ws unless @row_expand;
1682 my $row_expand = (sum @row_expand) || 1; 1717 my $row_expand = (sum @row_expand) || 1;
1683 1718
1684 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1719 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1685 1720
1686 CFPlus::UI::harmonize $hs; 1721 DC::UI::harmonize $hs;
1687 1722
1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1723 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1724 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1690 1725
1691 for my $widget ($self->children) { 1726 for my $widget ($self->children) {
1700 1 1735 1
1701} 1736}
1702 1737
1703############################################################################# 1738#############################################################################
1704 1739
1705package CFPlus::UI::Fixed; 1740package DC::UI::Fixed;
1706 1741
1707use List::Util qw(min max); 1742use List::Util qw(min max);
1708 1743
1709our @ISA = CFPlus::UI::Container::; 1744our @ISA = DC::UI::Container::;
1710 1745
1711sub _scale($$$) { 1746sub _scale($$$) {
1712 my ($rel, $val, $max) = @_; 1747 my ($rel, $val, $max) = @_;
1713 1748
1714 $rel ? $val * $max : $val 1749 $rel ? $val * $max : $val
1766 1 1801 1
1767} 1802}
1768 1803
1769############################################################################# 1804#############################################################################
1770 1805
1771package CFPlus::UI::Box; 1806package DC::UI::Box;
1772 1807
1773our @ISA = CFPlus::UI::Container::; 1808our @ISA = DC::UI::Container::;
1774 1809
1775sub size_request { 1810sub size_request {
1776 my ($self) = @_; 1811 my ($self) = @_;
1777 1812
1778 my @children = $self->visible_children; 1813 my @children = $self->visible_children;
1813 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1814 for 0 .. $#children; 1849 for 0 .. $#children;
1815 } 1850 }
1816 } 1851 }
1817 1852
1818 CFPlus::UI::harmonize \@req; 1853 DC::UI::harmonize \@req;
1819 1854
1820 my $pos = 0; 1855 my $pos = 0;
1821 for (0 .. $#children) { 1856 for (0 .. $#children) {
1822 my $alloc = $req[$_]; 1857 my $alloc = $req[$_];
1823 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1858 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1828 1 1863 1
1829} 1864}
1830 1865
1831############################################################################# 1866#############################################################################
1832 1867
1833package CFPlus::UI::HBox; 1868package DC::UI::HBox;
1834 1869
1835our @ISA = CFPlus::UI::Box::; 1870our @ISA = DC::UI::Box::;
1836 1871
1837sub new { 1872sub new {
1838 my $class = shift; 1873 my $class = shift;
1839 1874
1840 $class->SUPER::new ( 1875 $class->SUPER::new (
1843 ) 1878 )
1844} 1879}
1845 1880
1846############################################################################# 1881#############################################################################
1847 1882
1848package CFPlus::UI::VBox; 1883package DC::UI::VBox;
1849 1884
1850our @ISA = CFPlus::UI::Box::; 1885our @ISA = DC::UI::Box::;
1851 1886
1852sub new { 1887sub new {
1853 my $class = shift; 1888 my $class = shift;
1854 1889
1855 $class->SUPER::new ( 1890 $class->SUPER::new (
1858 ) 1893 )
1859} 1894}
1860 1895
1861############################################################################# 1896#############################################################################
1862 1897
1863package CFPlus::UI::Label; 1898package DC::UI::Label;
1864 1899
1865our @ISA = CFPlus::UI::DrawBG::; 1900our @ISA = DC::UI::DrawBG::;
1866 1901
1867use CFPlus::OpenGL; 1902use DC::OpenGL;
1868 1903
1869sub new { 1904sub new {
1870 my ($class, %arg) = @_; 1905 my ($class, %arg) = @_;
1871 1906
1872 my $self = $class->SUPER::new ( 1907 my $self = $class->SUPER::new (
1877 #text => initial text 1912 #text => initial text
1878 #markup => initial narkup 1913 #markup => initial narkup
1879 #max_w => maximum pixel width 1914 #max_w => maximum pixel width
1880 #style => 0, # render flags 1915 #style => 0, # render flags
1881 ellipsise => 3, # end 1916 ellipsise => 3, # end
1882 layout => (new CFPlus::Layout), 1917 layout => (new DC::Layout),
1883 fontsize => 1, 1918 fontsize => 1,
1884 align => -1, 1919 align => 0.5,
1885 valign => -1, 1920 valign => 0.5,
1886 padding_x => 2, 1921 padding_x => 4,
1887 padding_y => 2, 1922 padding_y => 2,
1888 can_events => 0, 1923 can_events => 0,
1889 %arg 1924 %arg
1890 ); 1925 );
1891 1926
1892 if (exists $self->{template}) { 1927 if (exists $self->{template}) {
1893 my $layout = new CFPlus::Layout; 1928 my $layout = new DC::Layout;
1894 $layout->set_text (delete $self->{template}); 1929 $layout->set_text (delete $self->{template});
1895 $self->{template} = $layout; 1930 $self->{template} = $layout;
1896 } 1931 }
1897 1932
1898 if (exists $self->{markup}) { 1933 if (exists $self->{markup}) {
1957 1992
1958 $self->{size_req} ||= do { 1993 $self->{size_req} ||= do {
1959 my ($max_w, $max_h) = $self->get_max_wh; 1994 my ($max_w, $max_h) = $self->get_max_wh;
1960 1995
1961 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
1962 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1997 $self->{layout}->set_width ($max_w);
1963 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
1964 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1965 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2000 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1966 2001
1967 my ($w, $h) = $self->{layout}->size; 2002 my ($w, $h) = $self->{layout}->size;
1968 2003
1969 if (exists $self->{template}) { 2004 if (exists $self->{template}) {
1970 $self->{template}->set_font ($self->{font}) if $self->{font}; 2005 $self->{template}->set_font ($self->{font}) if $self->{font};
1971 $self->{template}->set_width ($self->{max_w} || -1); 2006 $self->{template}->set_width ($max_w);
1972 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2007 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1973 2008
1974 my ($w2, $h2) = $self->{template}->size; 2009 my ($w2, $h2) = $self->{template}->size;
1975 2010
1976 $w = List::Util::max $w, $w2; 2011 $w = List::Util::max $w, $w2;
2032 2067
2033 [$self->{layout}->size] 2068 [$self->{layout}->size]
2034 }; 2069 };
2035 2070
2036 unless (exists $self->{ox}) { 2071 unless (exists $self->{ox}) {
2037 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2072 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2038 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2073 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2039 : ($self->{w} - $size->[0]) * 0.5);
2040
2041 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2042 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2043 : ($self->{h} - $size->[1]) * 0.5);
2044 2074
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2075 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2046 }; 2076 };
2047 2077
2048# unless ($self->{list}) { 2078# unless ($self->{list}) {
2049# $self->{list} = CFPlus::OpenGL::glGenList; 2079# $self->{list} = DC::OpenGL::glGenList;
2050# CFPlus::OpenGL::glNewList $self->{list}; 2080# DC::OpenGL::glNewList $self->{list};
2051# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2081# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052# CFPlus::OpenGL::glEndList; 2082# DC::OpenGL::glEndList;
2053# } 2083# }
2054# 2084#
2055# CFPlus::OpenGL::glCallList $self->{list}; 2085# DC::OpenGL::glCallList $self->{list};
2056 2086
2057 $self->{layout}->draw; 2087 $self->{layout}->draw;
2058} 2088}
2059 2089
2060#sub destroy { 2090#sub destroy {
2061# my ($self) = @_; 2091# my ($self) = @_;
2062# 2092#
2063# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2093# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2064# 2094#
2065# $self->SUPER::destroy; 2095# $self->SUPER::destroy;
2066#} 2096#}
2067 2097
2068############################################################################# 2098#############################################################################
2069 2099
2070package CFPlus::UI::EntryBase; 2100package DC::UI::EntryBase;
2071 2101
2072our @ISA = CFPlus::UI::Label::; 2102our @ISA = DC::UI::Label::;
2073 2103
2074use CFPlus::OpenGL; 2104use DC::OpenGL;
2075 2105
2076sub new { 2106sub new {
2077 my $class = shift; 2107 my $class = shift;
2078 2108
2079 $class->SUPER::new ( 2109 $class->SUPER::new (
2080 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2081 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2082 outline => [0.6, 0.3, 0.1], 2112 outline => undef,
2083 active_bg => [0, 0, 1, .2], 2113 active_bg => [0, 0, 1, .2],
2084 active_fg => [1, 1, 1], 2114 active_fg => [1, 1, 1],
2085 active_outline => [1, 1, 0], 2115 active_outline => [1, 1, 0],
2086 can_hover => 1, 2116 can_hover => 1,
2087 can_focus => 1, 2117 can_focus => 1,
2118 align => 0,
2088 valign => 0, 2119 valign => 0.5,
2089 can_events => 1, 2120 can_events => 1,
2090 ellipsise => 0, 2121 ellipsise => 0,
2122 padding_x => 4,
2123 padding_y => 2,
2091 #text => ... 2124 #text => ...
2092 #hidden => "*", 2125 #hidden => "*",
2093 @_ 2126 @_
2094 ) 2127 )
2095} 2128}
2142 2175
2143 my $text = $self->get_text; 2176 my $text = $self->get_text;
2144 2177
2145 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2178 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2146 2179
2147 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2149 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2150 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2151 } elsif ($sym == CFPlus::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2152 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2153 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2154 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2155 } elsif ($sym == CFPlus::SDLK_HOME) { 2188 } elsif ($sym == DC::SDLK_HOME) {
2156 # what a hack 2189 # what a hack
2157 $self->{cursor} = 2190 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2191 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1 2192 ? length $1
2160 : 0; 2193 : 0;
2161 } elsif ($sym == CFPlus::SDLK_END) { 2194 } elsif ($sym == DC::SDLK_END) {
2162 # uh, again 2195 # uh, again
2163 $self->{cursor} = 2196 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2197 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2165 ? $self->{cursor} + length $1 2198 ? $self->{cursor} + length $1
2166 : length $self->{text}; 2199 : length $self->{text};
2257 glColor @{$self->{active_outline}}; 2290 glColor @{$self->{active_outline}};
2258 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2291 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2259 glLineWidth 1; 2292 glLineWidth 1;
2260 2293
2261 } else { 2294 } else {
2262 glColor @{$self->{outline}}; 2295 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2263 glBegin GL_LINE_STRIP; 2296 glBegin GL_LINE_STRIP;
2264 glVertex .5, $self->{h} * .5; 2297 glVertex .5, $self->{h} * .5;
2265 glVertex .5, $self->{h} - 2.5; 2298 glVertex .5, $self->{h} - 2.5;
2266 glVertex $self->{w} - .5, $self->{h} - 2.5; 2299 glVertex $self->{w} - .5, $self->{h} - 2.5;
2267 glVertex $self->{w} - .5, $self->{h} * .5; 2300 glVertex $self->{w} - .5, $self->{h} * .5;
2269 } 2302 }
2270} 2303}
2271 2304
2272############################################################################# 2305#############################################################################
2273 2306
2274package CFPlus::UI::Entry; 2307package DC::UI::Entry;
2275 2308
2276our @ISA = CFPlus::UI::EntryBase::; 2309our @ISA = DC::UI::EntryBase::;
2277 2310
2278use CFPlus::OpenGL; 2311use DC::OpenGL;
2312
2313sub new {
2314 my $class = shift;
2315
2316 $class->SUPER::new (
2317 history_pointer => -1,
2318 @_
2319 )
2320}
2321
2279 2322
2280sub invoke_key_down { 2323sub invoke_key_down {
2281 my ($self, $ev) = @_; 2324 my ($self, $ev) = @_;
2282 2325
2283 my $sym = $ev->{sym}; 2326 my $sym = $ev->{sym};
2289 $self->{history_pointer} = -1; 2332 $self->{history_pointer} = -1;
2290 $self->{history_saveback} = ''; 2333 $self->{history_saveback} = '';
2291 $self->emit (activate => $txt); 2334 $self->emit (activate => $txt);
2292 $self->update; 2335 $self->update;
2293 2336
2294 } elsif ($sym == CFPlus::SDLK_UP) { 2337 } elsif ($sym == DC::SDLK_UP) {
2295 if ($self->{history_pointer} < 0) { 2338 if ($self->{history_pointer} < 0) {
2296 $self->{history_saveback} = $self->get_text; 2339 $self->{history_saveback} = $self->get_text;
2297 } 2340 }
2298 if (@{$self->{history} || []} > 0) { 2341 if (@{$self->{history} || []} > 0) {
2299 $self->{history_pointer}++; 2342 $self->{history_pointer}++;
2301 $self->{history_pointer} = @{$self->{history} || []} - 1; 2344 $self->{history_pointer} = @{$self->{history} || []} - 1;
2302 } 2345 }
2303 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2346 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2304 } 2347 }
2305 2348
2306 } elsif ($sym == CFPlus::SDLK_DOWN) { 2349 } elsif ($sym == DC::SDLK_DOWN) {
2307 $self->{history_pointer}--; 2350 $self->{history_pointer}--;
2308 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2351 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2309 2352
2310 if ($self->{history_pointer} >= 0) { 2353 if ($self->{history_pointer} >= 0) {
2311 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2354 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2312 } else { 2355 } else {
2356 if (defined $self->{history_saveback}) {
2313 $self->set_text ($self->{history_saveback}); 2357 $self->set_text ($self->{history_saveback});
2358 $self->{history_saveback} = undef;
2359 }
2314 } 2360 }
2315 2361
2316 } else { 2362 } else {
2317 return $self->SUPER::invoke_key_down ($ev) 2363 return $self->SUPER::invoke_key_down ($ev)
2318 } 2364 }
2320 1 2366 1
2321} 2367}
2322 2368
2323############################################################################# 2369#############################################################################
2324 2370
2325package CFPlus::UI::TextEdit; 2371package DC::UI::TextEdit;
2326 2372
2327our @ISA = CFPlus::UI::EntryBase::; 2373our @ISA = DC::UI::EntryBase::;
2328 2374
2329use CFPlus::OpenGL; 2375use DC::OpenGL;
2376
2377sub new {
2378 my $class = shift;
2379
2380 $class->SUPER::new (
2381 padding_y => 4,
2382
2383 @_
2384 )
2385}
2330 2386
2331sub move_cursor_ver { 2387sub move_cursor_ver {
2332 my ($self, $dy) = @_; 2388 my ($self, $dy) = @_;
2333 2389
2334 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2390 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2335 2391
2336 $y += $dy; 2392 $line += $dy;
2337 2393
2338 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2394 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2339 $self->{cursor} = $index; 2395 $self->{cursor} = $index;
2340 delete $self->{cur_h}; 2396 delete $self->{cur_h};
2341 $self->update; 2397 $self->update;
2342 return; 2398 return;
2343 } 2399 }
2346sub invoke_key_down { 2402sub invoke_key_down {
2347 my ($self, $ev) = @_; 2403 my ($self, $ev) = @_;
2348 2404
2349 my $sym = $ev->{sym}; 2405 my $sym = $ev->{sym};
2350 2406
2351 if ($sym == CFPlus::SDLK_UP) { 2407 if ($sym == DC::SDLK_UP) {
2352 $self->move_cursor_ver (-1); 2408 $self->move_cursor_ver (-1);
2353 } elsif ($sym == CFPlus::SDLK_DOWN) { 2409 } elsif ($sym == DC::SDLK_DOWN) {
2354 $self->move_cursor_ver (+1); 2410 $self->move_cursor_ver (+1);
2355 } else { 2411 } else {
2356 return $self->SUPER::invoke_key_down ($ev) 2412 return $self->SUPER::invoke_key_down ($ev)
2357 } 2413 }
2358 2414
2359 1 2415 1
2360} 2416}
2361 2417
2362############################################################################# 2418#############################################################################
2363 2419
2364package CFPlus::UI::ButtonBin; 2420package DC::UI::ButtonBin;
2365 2421
2366our @ISA = CFPlus::UI::Bin::; 2422our @ISA = DC::UI::Bin::;
2367 2423
2368use CFPlus::OpenGL; 2424use DC::OpenGL;
2369 2425
2370my @tex = 2426my @tex =
2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2372 qw(b1_button_inactive.png b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2373 2429
2374sub new { 2430sub new {
2375 my $class = shift; 2431 my $class = shift;
2376 2432
2377 $class->SUPER::new ( 2433 $class->SUPER::new (
2378 can_hover => 1, 2434 can_hover => 1,
2379 align => 0, 2435 align => 0.5,
2380 valign => 0, 2436 valign => 0.5,
2381 can_events => 1, 2437 can_events => 1,
2382 @_ 2438 @_
2383 ) 2439 )
2384} 2440}
2385 2441
2408 $self->SUPER::_draw; 2464 $self->SUPER::_draw;
2409} 2465}
2410 2466
2411############################################################################# 2467#############################################################################
2412 2468
2413package CFPlus::UI::Button; 2469package DC::UI::Button;
2414 2470
2415our @ISA = CFPlus::UI::Label::; 2471our @ISA = DC::UI::Label::;
2416 2472
2417use CFPlus::OpenGL; 2473use DC::OpenGL;
2418 2474
2419my @tex = 2475my @tex =
2420 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2421 qw(b1_button_inactive.png b1_button_active.png); 2477 qw(b1_button_inactive.png b1_button_active.png);
2422 2478
2423sub new { 2479sub new {
2424 my $class = shift; 2480 my $class = shift;
2425 2481
2426 $class->SUPER::new ( 2482 $class->SUPER::new (
2427 padding_x => 4, 2483 padding_x => 8,
2428 padding_y => 4, 2484 padding_y => 4,
2429 fg => [1.0, 1.0, 1.0], 2485 fg => [1.0, 1.0, 1.0],
2430 active_fg => [0.8, 0.8, 0.8], 2486 active_fg => [0.8, 0.8, 0.8],
2431 can_hover => 1, 2487 can_hover => 1,
2432 align => 0, 2488 align => 0.5,
2433 valign => 0, 2489 valign => 0.5,
2434 can_events => 1, 2490 can_events => 1,
2435 @_ 2491 @_
2436 ) 2492 )
2437} 2493}
2438 2494
2463 $self->SUPER::_draw; 2519 $self->SUPER::_draw;
2464} 2520}
2465 2521
2466############################################################################# 2522#############################################################################
2467 2523
2468package CFPlus::UI::CheckBox; 2524package DC::UI::CheckBox;
2469 2525
2470our @ISA = CFPlus::UI::DrawBG::; 2526our @ISA = DC::UI::DrawBG::;
2471 2527
2472my @tex = 2528my @tex =
2473 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2529 map { new_from_resource DC::Texture $_, mipmap => 1 }
2474 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2530 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2475 2531
2476use CFPlus::OpenGL; 2532use DC::OpenGL;
2477 2533
2478sub new { 2534sub new {
2479 my $class = shift; 2535 my $class = shift;
2480 2536
2481 $class->SUPER::new ( 2537 $class->SUPER::new (
2538 fontsize => 1,
2482 padding_x => 2, 2539 padding_x => 2,
2483 padding_y => 2, 2540 padding_y => 2,
2484 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2485 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2486 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2492} 2549}
2493 2550
2494sub size_request { 2551sub size_request {
2495 my ($self) = @_; 2552 my ($self) = @_;
2496 2553
2497 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2498} 2555}
2499 2556
2500sub toggle { 2557sub toggle {
2501 my ($self) = @_; 2558 my ($self) = @_;
2502 2559
2538 glDisable GL_TEXTURE_2D; 2595 glDisable GL_TEXTURE_2D;
2539} 2596}
2540 2597
2541############################################################################# 2598#############################################################################
2542 2599
2543package CFPlus::UI::Image; 2600package DC::UI::Image;
2544 2601
2545our @ISA = CFPlus::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2546 2603
2547use CFPlus::OpenGL; 2604use DC::OpenGL;
2548 2605
2549our %texture_cache; 2606our %texture_cache;
2550 2607
2551sub new { 2608sub new {
2552 my $class = shift; 2609 my $class = shift;
2559 2616
2560 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2561 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2562 2619
2563 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2564 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2565 2622
2566 CFPlus::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2567 2624
2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2569 2626
2570 $self 2627 $self
2571} 2628}
2572 2629
2573sub STORABLE_freeze { 2630sub STORABLE_freeze {
2574 my ($self, $cloning) = @_; 2631 my ($self, $cloning) = @_;
2575 2632
2576 $self->{path} 2633 $self->{path}
2577 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2634 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2578 2635
2579 $self->{path} 2636 $self->{path}
2580} 2637}
2581 2638
2582sub STORABLE_attach { 2639sub STORABLE_attach {
2583 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2584 2641
2585 $self->new (path => $path) 2642 $self->new (path => $path)
2586} 2643}
2587 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2588sub size_request { 2652sub size_request {
2589 my ($self) = @_; 2653 my ($self) = @_;
2590 2654
2591 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2655 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2592} 2656}
2593 2657
2594sub _draw { 2658sub _draw {
2595 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2596 2662
2597 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2598 2664
2599 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2600 2666
2613 glDisable GL_TEXTURE_2D; 2679 glDisable GL_TEXTURE_2D;
2614} 2680}
2615 2681
2616############################################################################# 2682#############################################################################
2617 2683
2618package CFPlus::UI::ImageButton; 2684package DC::UI::ImageButton;
2619 2685
2620our @ISA = CFPlus::UI::Image::; 2686our @ISA = DC::UI::Image::;
2621 2687
2622use CFPlus::OpenGL; 2688use DC::OpenGL;
2623
2624my %textures;
2625 2689
2626sub new { 2690sub new {
2627 my $class = shift; 2691 my $class = shift;
2628 2692
2629 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2630 padding_x => 4, 2694 padding_x => 4,
2631 padding_y => 4, 2695 padding_y => 4,
2632 fg => [1, 1, 1], 2696 fg => [1, 1, 1],
2633 active_fg => [0, 0, 1], 2697 active_fg => [0, 0, 1],
2634 can_hover => 1, 2698 can_hover => 1,
2635 align => 0, 2699 align => 0.5,
2636 valign => 0, 2700 valign => 0.5,
2637 can_events => 1, 2701 can_events => 1,
2638 @_ 2702 @_
2639 ); 2703 );
2640} 2704}
2641 2705
2655 1 2719 1
2656} 2720}
2657 2721
2658############################################################################# 2722#############################################################################
2659 2723
2660package CFPlus::UI::VGauge; 2724package DC::UI::VGauge;
2661 2725
2662our @ISA = CFPlus::UI::Base::; 2726our @ISA = DC::UI::Base::;
2663 2727
2664use List::Util qw(min max); 2728use List::Util qw(min max);
2665 2729
2666use CFPlus::OpenGL; 2730use DC::OpenGL;
2667 2731
2668my %tex = ( 2732my %tex = (
2669 food => [ 2733 food => [
2670 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2671 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2672 ], 2736 ],
2673 grace => [ 2737 grace => [
2674 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2675 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2739 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2676 ], 2740 ],
2677 hp => [ 2741 hp => [
2678 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2679 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2680 ], 2744 ],
2681 mana => [ 2745 mana => [
2682 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2683 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2747 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2684 ], 2748 ],
2685); 2749);
2686 2750
2687# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
2790 glDisable GL_TEXTURE_2D; 2854 glDisable GL_TEXTURE_2D;
2791} 2855}
2792 2856
2793############################################################################# 2857#############################################################################
2794 2858
2795package CFPlus::UI::Progress; 2859package DC::UI::Progress;
2796 2860
2797our @ISA = CFPlus::UI::Label::; 2861our @ISA = DC::UI::Label::;
2798 2862
2799use CFPlus::OpenGL; 2863use DC::OpenGL;
2800 2864
2801sub new { 2865sub new {
2802 my ($class, %arg) = @_; 2866 my ($class, %arg) = @_;
2803 2867
2804 my $self = $class->SUPER::new ( 2868 my $self = $class->SUPER::new (
2869 padding_x => 2,
2870 padding_y => 2,
2805 fg => [1, 1, 1], 2871 fg => [1, 1, 1],
2806 bg => [0, 0, 1, 0.2], 2872 bg => [0, 0, 1, 0.2],
2807 bar => [0.7, 0.5, 0.1, 0.8], 2873 bar => [0.7, 0.5, 0.1, 0.8],
2808 outline => [0.4, 0.3, 0], 2874 outline => [0.4, 0.3, 0],
2809 fontsize => 0.9, 2875 fontsize => 0.9,
2810 valign => 0, 2876 valign => 0.5,
2811 align => 0, 2877 align => 0.5,
2812 can_events => 1, 2878 can_events => 1,
2813 ellipsise => 1, 2879 ellipsise => 1,
2814 label => "%d%%", 2880 label => "%d%%",
2815 %arg, 2881 %arg,
2816 ); 2882 );
2824 my ($self, $label) = @_; 2890 my ($self, $label) = @_;
2825 2891
2826 return if $self->{label} eq $label; 2892 return if $self->{label} eq $label;
2827 $self->{label} = $label; 2893 $self->{label} = $label;
2828 2894
2829 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2895 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2830} 2896}
2831 2897
2832sub set_value { 2898sub set_value {
2833 my ($self, $value) = @_; 2899 my ($self, $value) = @_;
2834 2900
2849 my ($self) = @_; 2915 my ($self) = @_;
2850 2916
2851 glEnable GL_BLEND; 2917 glEnable GL_BLEND;
2852 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2918 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2853 2919
2920 my $px = $self->{padding_x};
2921 my $py = $self->{padding_y};
2922
2854 if ($self->{value} >= 0) { 2923 if ($self->{value} >= 0) {
2855 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2924 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2856 2925
2857 glColor_premultiply @{$self->{bar}}; 2926 glColor_premultiply @{$self->{bar}};
2858 glRect 2, 2, $s, $self->{h} - 2; 2927 glRect $px, $py, $s, $self->{h} - $py;
2859 glColor_premultiply @{$self->{bg}}; 2928 glColor_premultiply @{$self->{bg}};
2860 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2929 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2861 } 2930 }
2862 2931
2863 glColor_premultiply @{$self->{outline}}; 2932 glColor_premultiply @{$self->{outline}};
2933
2934 $px -= .5;
2935 $py -= .5;
2936
2864 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2937 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2865 2938
2866 glDisable GL_BLEND; 2939 glDisable GL_BLEND;
2867 2940
2868 { 2941 {
2869 local $self->{bg}; # do not draw background 2942 local $self->{bg}; # do not draw background
2871 } 2944 }
2872} 2945}
2873 2946
2874############################################################################# 2947#############################################################################
2875 2948
2876package CFPlus::UI::ExperienceProgress; 2949package DC::UI::ExperienceProgress;
2877 2950
2878our @ISA = CFPlus::UI::Progress::; 2951our @ISA = DC::UI::Progress::;
2879 2952
2880sub new { 2953sub new {
2881 my ($class, %arg) = @_; 2954 my ($class, %arg) = @_;
2882 2955
2956 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2957
2883 my $self = $class->SUPER::new ( 2958 my $self = $class->SUPER::new (
2959 %arg,
2884 tooltip => sub { 2960 tooltip => sub {
2885 my ($self) = @_; 2961 my ($self) = @_;
2886 2962
2887 sprintf "level %d\n%s points\n%s next level\n%s to go", 2963 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2964 $tt,
2888 $self->{lvl}, 2965 $self->{lvl},
2889 ::formsep ($self->{exp}), 2966 ::formsep ($self->{exp}),
2890 ::formsep ($self->{nxt}), 2967 ::formsep ($self->{nxt}),
2891 ::formsep ($self->{nxt} - $self->{exp}), 2968 ::formsep ($self->{nxt} - $self->{exp}),
2969 $self->_percent * 100,
2892 }, 2970 },
2893 %arg
2894 ); 2971 );
2895 2972
2896 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2973 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2897 if $::CONN; 2974 if $::CONN;
2898 2975
2906 if $::CONN; 2983 if $::CONN;
2907 2984
2908 $self->SUPER::DESTROY; 2985 $self->SUPER::DESTROY;
2909} 2986}
2910 2987
2988sub _percent {
2989 my ($self) = @_;
2990
2991 my $table = $::CONN && $::CONN->{exp_table}
2992 or return -1;
2993
2994 my $l0 = $table->[$self->{lvl} - 1];
2995 my $l1 = $table->[$self->{lvl}];
2996
2997 $self->{nxt} = $l1;
2998
2999 ($self->{exp} - $l0) / ($l1 - $l0)
3000}
3001
2911sub set_value { 3002sub set_value {
2912 my ($self, $lvl, $exp) = @_; 3003 my ($self, $lvl, $exp) = @_;
2913 3004
2914 $self->{lvl} = $lvl; 3005 $self->{lvl} = $lvl;
2915 $self->{exp} = $exp; 3006 $self->{exp} = $exp;
2916 3007
2917 my $v = -1;
2918
2919 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2920 my $l0 = $table->[$lvl - 1];
2921 my $l1 = $table->[$lvl];
2922
2923 $self->{nxt} = $l1;
2924
2925 $v = ($exp - $l0) / ($l1 - $l0);
2926 }
2927
2928 $self->SUPER::set_value ($v); 3008 $self->SUPER::set_value ($self->_percent);
2929} 3009}
2930 3010
2931############################################################################# 3011#############################################################################
2932 3012
2933package CFPlus::UI::Gauge; 3013package DC::UI::Gauge;
2934 3014
2935our @ISA = CFPlus::UI::VBox::; 3015our @ISA = DC::UI::VBox::;
2936 3016
2937sub new { 3017sub new {
2938 my ($class, %arg) = @_; 3018 my ($class, %arg) = @_;
2939 3019
2940 my $self = $class->SUPER::new ( 3020 my $self = $class->SUPER::new (
2942 can_hover => 1, 3022 can_hover => 1,
2943 can_events => 1, 3023 can_events => 1,
2944 %arg, 3024 %arg,
2945 ); 3025 );
2946 3026
2947 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3027 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2948 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3028 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2949 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3029 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2950 3030
2951 $self 3031 $self
2952} 3032}
2953 3033
2954sub set_fontsize { 3034sub set_fontsize {
2975 $self->{value}->set_text ($val); 3055 $self->{value}->set_text ($val);
2976} 3056}
2977 3057
2978############################################################################# 3058#############################################################################
2979 3059
2980package CFPlus::UI::Slider; 3060package DC::UI::Slider;
2981 3061
2982use strict; 3062use common::sense;
2983 3063
2984use CFPlus::OpenGL; 3064use DC::OpenGL;
2985 3065
2986our @ISA = CFPlus::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
2987 3067
2988my @tex = 3068my @tex =
2989 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
2990 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
2991 3071
2992sub new { 3072sub new {
2993 my $class = shift; 3073 my $class = shift;
2994 3074
3031sub set_value { 3111sub set_value {
3032 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
3033 3113
3034 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3035 3115
3036 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
3037 3117
3038 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
3039
3040 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
3041 $value = $hi - $page if $value > $hi - $page;
3042 3120
3043 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3044 if $unit; 3122 if $unit;
3045 3123
3046 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3075 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
3076 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3077 3155
3078 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
3079 3157
3080 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3081 3159
3082 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3083 } else { 3161 } else {
3084 return 0; 3162 return 0;
3085 } 3163 }
3090sub invoke_mouse_wheel { 3168sub invoke_mouse_wheel {
3091 my ($self, $ev) = @_; 3169 my ($self, $ev) = @_;
3092 3170
3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3171 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3094 3172
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3173 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3096 3174
3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3175 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3098 3176
3099 1 3177 1
3100} 3178}
3110 my ($self) = @_; 3188 my ($self) = @_;
3111 3189
3112 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
3113 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
3114 3192
3115 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3116 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
3117 3195
3118 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3196 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3119 3197
3120 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3198 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3121 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3122 3200
3123 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
3153 glDisable GL_TEXTURE_2D; 3231 glDisable GL_TEXTURE_2D;
3154} 3232}
3155 3233
3156############################################################################# 3234#############################################################################
3157 3235
3158package CFPlus::UI::ValSlider; 3236package DC::UI::ValSlider;
3159 3237
3160our @ISA = CFPlus::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
3161 3239
3162sub new { 3240sub new {
3163 my ($class, %arg) = @_; 3241 my ($class, %arg) = @_;
3164 3242
3165 my $range = delete $arg{range}; 3243 my $range = delete $arg{range};
3166 3244
3167 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
3168 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3246 slider => (new DC::UI::Slider expand => 1, range => $range),
3169 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3247 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3170 to_value => sub { shift }, 3248 to_value => sub { shift },
3171 from_value => sub { shift }, 3249 from_value => sub { shift },
3172 %arg, 3250 %arg,
3173 ); 3251 );
3174 3252
3194sub set_range { shift->{slider}->set_range (@_) } 3272sub set_range { shift->{slider}->set_range (@_) }
3195sub set_value { shift->{slider}->set_value (@_) } 3273sub set_value { shift->{slider}->set_value (@_) }
3196 3274
3197############################################################################# 3275#############################################################################
3198 3276
3199package CFPlus::UI::TextScroller; 3277package DC::UI::TextScroller;
3200 3278
3201our @ISA = CFPlus::UI::HBox::; 3279our @ISA = DC::UI::HBox::;
3202 3280
3203use CFPlus::OpenGL; 3281use DC::OpenGL;
3204 3282
3205sub new { 3283sub new {
3206 my $class = shift; 3284 my $class = shift;
3207 3285
3208 my $self = $class->SUPER::new ( 3286 my $self = $class->SUPER::new (
3210 can_events => 1, 3288 can_events => 1,
3211 indent => 0, 3289 indent => 0,
3212 #font => default_font 3290 #font => default_font
3213 @_, 3291 @_,
3214 3292
3215 layout => (new CFPlus::Layout), 3293 layout => (new DC::Layout),
3216 par => [],
3217 max_par => 0, 3294 max_par => 0,
3218 height => 0, 3295 height => 0,
3219 children => [ 3296 children => [
3220 (new CFPlus::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3221 (new CFPlus::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3222 ], 3299 ],
3223 ); 3300 );
3224 3301
3225 $self->{children}[1]->connect (changed => sub { $self->update }); 3302 $self->{children}[1]->connect (changed => sub { $self->update });
3303
3304 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3226 3305
3227 $self 3306 $self
3228} 3307}
3229 3308
3230sub set_fontsize { 3309sub set_fontsize {
3282 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3361 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3283 $layout->set_markup ($para->{markup}); 3362 $layout->set_markup ($para->{markup});
3284 3363
3285 $layout->set_shapes ( 3364 $layout->set_shapes (
3286 map 3365 map
3287 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3366 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3288 @{$para->{widget}} 3367 @{$para->{widget}}
3289 ); 3368 );
3290 3369
3291 $layout 3370 $layout
3292} 3371}
3409 $ROOT->on_post_alloc ($self => sub { 3488 $ROOT->on_post_alloc ($self => sub {
3410 $self->force_uptodate; 3489 $self->force_uptodate;
3411 3490
3412 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3491 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3413 3492
3414 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3493 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3415 glClearColor 0, 0, 0, 0; 3494 glClearColor 0, 0, 0, 0;
3416 glClear GL_COLOR_BUFFER_BIT; 3495 glClear GL_COLOR_BUFFER_BIT;
3417 3496
3418 package CFPlus::UI::Base; 3497 package DC::UI::Base;
3419 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3498 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3420 (0, 0, $self->{w}, $self->{h}); 3499 (0, 0, $self->{w}, $self->{h});
3421 3500
3422 my $top = int $self->{children}[1]{range}[0]; 3501 my $top = int $self->{children}[1]{range}[0];
3423 3502
3482 $self->{children}[1]->draw; 3561 $self->{children}[1]->draw;
3483} 3562}
3484 3563
3485############################################################################# 3564#############################################################################
3486 3565
3487package CFPlus::UI::Animator; 3566package DC::UI::Animator;
3488 3567
3489use CFPlus::OpenGL; 3568use DC::OpenGL;
3490 3569
3491our @ISA = CFPlus::UI::Bin::; 3570our @ISA = DC::UI::Bin::;
3492 3571
3493sub moveto { 3572sub moveto {
3494 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3495 3574
3496 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3524 glPopMatrix; 3603 glPopMatrix;
3525} 3604}
3526 3605
3527############################################################################# 3606#############################################################################
3528 3607
3529package CFPlus::UI::Flopper; 3608package DC::UI::Flopper;
3530 3609
3531our @ISA = CFPlus::UI::Button::; 3610our @ISA = DC::UI::Button::;
3532 3611
3533sub new { 3612sub new {
3534 my $class = shift; 3613 my $class = shift;
3535 3614
3536 my $self = $class->SUPER::new ( 3615 my $self = $class->SUPER::new (
3548 $self->{other}->toggle_visibility; 3627 $self->{other}->toggle_visibility;
3549} 3628}
3550 3629
3551############################################################################# 3630#############################################################################
3552 3631
3553package CFPlus::UI::Tooltip; 3632package DC::UI::Tooltip;
3554 3633
3555our @ISA = CFPlus::UI::Bin::; 3634our @ISA = DC::UI::Bin::;
3556 3635
3557use CFPlus::OpenGL; 3636use DC::OpenGL;
3558 3637
3559sub new { 3638sub new {
3560 my $class = shift; 3639 my $class = shift;
3561 3640
3562 $class->SUPER::new ( 3641 $class->SUPER::new (
3569 my ($self, $widget) = @_; 3648 my ($self, $widget) = @_;
3570 3649
3571 my $tip = $widget->{tooltip}; 3650 my $tip = $widget->{tooltip};
3572 $tip = $tip->($widget) if "CODE" eq ref $tip; 3651 $tip = $tip->($widget) if "CODE" eq ref $tip;
3573 3652
3574 $tip = CFPlus::Pod::section_label tooltip => $1 3653 $tip = DC::Pod::section_label tooltip => $1
3575 if $tip =~ /^#(.*)$/; 3654 if $tip =~ /^#(.*)$/;
3576 3655
3577 if ($ENV{CFPLUS_DEBUG} & 2) { 3656 if ($ENV{CFPLUS_DEBUG} & 2) {
3578 $tip .= "\n\n" . (ref $widget) . "\n" 3657 $tip .= "\n\n" . (ref $widget) . "\n"
3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3658 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3582 } 3661 }
3583 3662
3584 $tip =~ s/^\n+//; 3663 $tip =~ s/^\n+//;
3585 $tip =~ s/\n+$//; 3664 $tip =~ s/\n+$//;
3586 3665
3587 $self->add (new CFPlus::UI::Label 3666 $self->add (new DC::UI::Label
3667 fg => $DC::THEME{tooltip_fg},
3588 markup => $tip, 3668 markup => $tip,
3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3669 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3670 align => 0,
3590 fontsize => 0.8, 3671 fontsize => 0.8,
3591 style => 1, # FLAG_INVERSE 3672 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3592 ellipsise => 0, 3673 ellipsise => 0,
3593 font => ($widget->{tooltip_font} || $::FONT_PROP), 3674 font => ($widget->{tooltip_font} || $::FONT_PROP),
3594 ); 3675 );
3595} 3676}
3596 3677
3633sub _draw { 3714sub _draw {
3634 my ($self) = @_; 3715 my ($self) = @_;
3635 3716
3636 my ($w, $h) = @$self{qw(w h)}; 3717 my ($w, $h) = @$self{qw(w h)};
3637 3718
3638 glColor 1, 0.8, 0.4; 3719 glColor @{ $DC::THEME{tooltip_bg} };
3639 glRect 0, 0, $w, $h; 3720 glRect 0, 0, $w, $h;
3640 3721
3641 glColor 0, 0, 0; 3722 glColor @{ $DC::THEME{tooltip_border} };
3642 glRect_lineloop .5, .5, $w + .5, $h + .5; 3723 glRect_lineloop .5, .5, $w + .5, $h + .5;
3643 3724
3644 glTranslate 2, 2; 3725 glTranslate 2, 2;
3645 3726
3646 $self->SUPER::_draw; 3727 $self->SUPER::_draw;
3647} 3728}
3648 3729
3649############################################################################# 3730#############################################################################
3650 3731
3651package CFPlus::UI::Face; 3732package DC::UI::Face;
3652 3733
3653our @ISA = CFPlus::UI::DrawBG::; 3734our @ISA = DC::UI::DrawBG::;
3654 3735
3655use CFPlus::OpenGL; 3736use DC::OpenGL;
3656 3737
3657sub new { 3738sub new {
3658 my $class = shift; 3739 my $class = shift;
3659 3740
3660 my $self = $class->SUPER::new ( 3741 my $self = $class->SUPER::new (
3663 aspect => 1, 3744 aspect => 1,
3664 can_events => 0, 3745 can_events => 0,
3665 @_, 3746 @_,
3666 ); 3747 );
3667 3748
3668 if ($self->{anim} && $self->{animspeed}) {
3669 CFPlus::weaken (my $widget = $self);
3670
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3673 $self->{timer} = Event->timer (
3674 parked => 1,
3675 cb => sub {
3676 return unless $::CONN;
3677
3678 my $w = $widget
3679 or return;
3680
3681 ++$w->{frame};
3682 $w->update_face;
3683
3684 # somehow, $widget can go away
3685 $w->update;
3686 $w->update_timer;
3687 },
3688 );
3689
3690 $self->update_face;
3691 $self->update_timer; 3749 $self->update_anim;
3692 }
3693 3750
3694 $self 3751 $self
3695} 3752}
3696 3753
3697sub update_timer { 3754sub update_timer {
3698 my ($self) = @_; 3755 my ($self) = @_;
3699 3756
3700 return unless $self->{timer}; 3757 return unless $self->{timer};
3701 3758
3702 if ($self->{visible}) { 3759 if ($self->{visible}) {
3703 $self->{timer}->at (
3704 $self->{anim_start}
3705 + $self->{animspeed}
3706 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3707 );
3708 $self->{timer}->start; 3760 $self->{timer}->start;
3709 } else { 3761 } else {
3710 $self->{timer}->stop; 3762 $self->{timer}->stop;
3711 } 3763 }
3712} 3764}
3725 $tex->upload (sub { $self->reconfigure }); 3777 $tex->upload (sub { $self->reconfigure });
3726 } 3778 }
3727 } 3779 }
3728 } 3780 }
3729 } 3781 }
3782 }
3783}
3784
3785sub update_anim {
3786 my ($self) = @_;
3787
3788 if ($self->{anim} && $self->{animspeed}) {
3789 DC::weaken (my $widget = $self);
3790
3791 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3792 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3793 return unless $::CONN;
3794
3795 my $w = $widget
3796 or return;
3797
3798 ++$w->{frame};
3799 $w->update_face;
3800
3801 # somehow, $widget can go away
3802 $w->update;
3803 $w->update_timer;
3804 };
3805
3806 $self->update_face;
3807 $self->update_timer;
3808 } else {
3809 delete $self->{timer};
3730 } 3810 }
3731} 3811}
3732 3812
3733sub size_request { 3813sub size_request {
3734 my ($self) = @_; 3814 my ($self) = @_;
3756 return unless $self->{visible}; 3836 return unless $self->{visible};
3757 3837
3758 $self->SUPER::update; 3838 $self->SUPER::update;
3759} 3839}
3760 3840
3841sub set_face {
3842 my ($self, $face) = @_;
3843
3844 $self->{face} = $face;
3845 $self->reconfigure;
3846}
3847
3848sub set_anim {
3849 my ($self, $anim) = @_;
3850
3851 $self->{anim} = $anim;
3852 $self->update_anim;
3853}
3854
3855sub set_animspeed {
3856 my ($self, $animspeed) = @_;
3857
3858 $self->{animspeed} = $animspeed;
3859 $self->update_anim;
3860}
3861
3761sub invoke_visibility_change { 3862sub invoke_visibility_change {
3762 my ($self) = @_; 3863 my ($self) = @_;
3763 3864
3764 $self->update_timer; 3865 $self->update_timer;
3765 3866
3789 $self->SUPER::destroy; 3890 $self->SUPER::destroy;
3790} 3891}
3791 3892
3792############################################################################# 3893#############################################################################
3793 3894
3794package CFPlus::UI::Buttonbar; 3895package DC::UI::Buttonbar;
3795 3896
3796our @ISA = CFPlus::UI::HBox::; 3897our @ISA = DC::UI::HBox::;
3797 3898
3798# TODO: should actually wrap buttons and other goodies. 3899# TODO: should actually wrap buttons and other goodies.
3799 3900
3800############################################################################# 3901#############################################################################
3801 3902
3802package CFPlus::UI::Menu; 3903package DC::UI::Menu;
3803 3904
3804our @ISA = CFPlus::UI::Toplevel::; 3905our @ISA = DC::UI::Toplevel::;
3805 3906
3806use CFPlus::OpenGL; 3907use DC::OpenGL;
3807 3908
3808sub new { 3909sub new {
3809 my $class = shift; 3910 my $class = shift;
3810 3911
3811 my $self = $class->SUPER::new ( 3912 my $self = $class->SUPER::new (
3812 items => [], 3913 items => [],
3813 z => 100, 3914 z => 100,
3814 @_, 3915 @_,
3815 ); 3916 );
3816 3917
3817 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3918 $self->add ($self->{vbox} = new DC::UI::VBox);
3818 3919
3819 for my $item (@{ $self->{items} }) { 3920 for my $item (@{ $self->{items} }) {
3820 my ($widget, $cb, $tooltip) = @$item; 3921 my ($widget, $cb, $tooltip) = @$item;
3821 3922
3822 # handle various types of items, only text for now 3923 # handle various types of items, only text for now
3823 if (!ref $widget) { 3924 if (!ref $widget) {
3824 if ($widget =~ /\t/) { 3925 if ($widget =~ /\t/) {
3825 my ($left, $right) = split /\t/, $widget, 2; 3926 my ($left, $right) = split /\t/, $widget, 2;
3826 3927
3827 $widget = new CFPlus::UI::HBox 3928 $widget = new DC::UI::HBox
3828 can_hover => 1, 3929 can_hover => 1,
3829 can_events => 1, 3930 can_events => 1,
3830 tooltip => $tooltip, 3931 tooltip => $tooltip,
3831 children => [ 3932 children => [
3832 (new CFPlus::UI::Label markup => $left, expand => 1), 3933 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3833 (new CFPlus::UI::Label markup => $right, align => +1), 3934 (new DC::UI::Label markup => $right, align => 1),
3834 ], 3935 ],
3835 ; 3936 ;
3836 3937
3837 } else { 3938 } else {
3838 $widget = new CFPlus::UI::Label 3939 $widget = new DC::UI::Label
3839 can_hover => 1, 3940 can_hover => 1,
3840 can_events => 1, 3941 can_events => 1,
3942 align => 0,
3841 markup => $widget, 3943 markup => $widget,
3842 tooltip => $tooltip; 3944 tooltip => $tooltip;
3843 } 3945 }
3844 } 3946 }
3845 3947
3860 # maybe save $GRAB? must be careful about events... 3962 # maybe save $GRAB? must be careful about events...
3861 $GRAB = $self; 3963 $GRAB = $self;
3862 $self->{button} = $ev->{button}; 3964 $self->{button} = $ev->{button};
3863 3965
3864 $self->show; 3966 $self->show;
3865 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3967
3968 my $x = $ev->{x};
3969 my $y = $ev->{y};
3970
3971 $self->{root}->on_post_alloc ($self => sub {
3972 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3973 });
3974
3975 1 # so it can be used inside event handlers
3866} 3976}
3867 3977
3868sub invoke_mouse_motion { 3978sub invoke_mouse_motion {
3869 my ($self, $ev, $x, $y) = @_; 3979 my ($self, $ev, $x, $y) = @_;
3870 3980
3891 1 4001 1
3892} 4002}
3893 4003
3894############################################################################# 4004#############################################################################
3895 4005
3896package CFPlus::UI::Multiplexer; 4006package DC::UI::Multiplexer;
3897 4007
3898our @ISA = CFPlus::UI::Container::; 4008our @ISA = DC::UI::Container::;
3899 4009
3900sub new { 4010sub new {
3901 my $class = shift; 4011 my $class = shift;
3902 4012
3903 my $self = $class->SUPER::new ( 4013 my $self = $class->SUPER::new (
3904 @_, 4014 @_,
3905 ); 4015 );
3906 4016
3907 $self->{current} = $self->{children}[0] 4017 $self->set_current_page (0);
3908 if @{ $self->{children} };
3909 4018
3910 $self 4019 $self
3911} 4020}
3912 4021
3913sub add { 4022sub add {
3914 my ($self, @widgets) = @_; 4023 my ($self, @widgets) = @_;
3915 4024
3916 $self->SUPER::add (@widgets); 4025 $self->SUPER::add (@widgets);
3917 4026
3918 $self->{current} = $self->{children}[0] 4027 $self->set_current_page (0)
3919 if @{ $self->{children} }; 4028 if @widgets == @{ $self->{children} };
3920} 4029}
3921 4030
3922sub get_current_page { 4031sub get_current_page {
3923 my ($self) = @_; 4032 my ($self) = @_;
3924 4033
3930 4039
3931 my $widget = ref $page_or_widget 4040 my $widget = ref $page_or_widget
3932 ? $page_or_widget 4041 ? $page_or_widget
3933 : $self->{children}[$page_or_widget]; 4042 : $self->{children}[$page_or_widget];
3934 4043
4044 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4045
3935 $self->{current} = $widget; 4046 if (($self->{current} = $widget)) {
4047 $self->{current}->set_visible if $self->{current} && $self->{visible};
3936 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4048 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3937 4049
3938 $self->emit (page_changed => $self->{current}); 4050 $self->emit (page_changed => $self->{current});
4051 }
3939 4052
3940 $self->realloc; 4053 $self->realloc;
3941} 4054}
3942 4055
3943sub visible_children { 4056sub visible_children {
3944 $_[0]{current} 4057 $_[0]{current} || ()
3945} 4058}
3946 4059
3947sub size_request { 4060sub size_request {
3948 my ($self) = @_; 4061 my ($self) = @_;
3949 4062
4063 $self->{current}
3950 $self->{current}->size_request 4064 ? $self->{current}->size_request
4065 : (0, 0)
3951} 4066}
3952 4067
3953sub invoke_size_allocate { 4068sub invoke_size_allocate {
3954 my ($self, $w, $h) = @_; 4069 my ($self, $w, $h) = @_;
3955 4070
3956 $self->{current}->configure (0, 0, $w, $h); 4071 $self->{current}->configure (0, 0, $w, $h)
4072 if $self->{current};
3957 4073
3958 1 4074 1
3959} 4075}
3960 4076
3961sub _draw { 4077sub _draw {
3962 my ($self) = @_; 4078 my ($self) = @_;
3963 4079
3964 $self->{current}->draw; 4080 $self->{current}->draw
4081 if $self->{current};
3965} 4082}
3966 4083
3967############################################################################# 4084#############################################################################
3968 4085
3969package CFPlus::UI::Notebook; 4086package DC::UI::Notebook;
3970 4087
3971use CFPlus::OpenGL; 4088use DC::OpenGL;
3972 4089
3973our @ISA = CFPlus::UI::VBox::; 4090our @ISA = DC::UI::VBox::;
3974 4091
3975sub new { 4092sub new {
3976 my $class = shift; 4093 my $class = shift;
3977 4094
3978 my $self = $class->SUPER::new ( 4095 my $self = $class->SUPER::new (
3979 buttonbar => (new CFPlus::UI::Buttonbar), 4096 buttonbar => (new DC::UI::Buttonbar),
3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4097 multiplexer => (new DC::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2], 4098 active_outline => [.7, .7, 0.2],
3982 # filter => # will be put between multiplexer and $self 4099 # filter => # will be put between multiplexer and $self
3983 @_, 4100 @_,
3984 ); 4101 );
3985 4102
3994 4111
3995 for my $child (@$widgets) { 4112 for my $child (@$widgets) {
3996 Scalar::Util::weaken $child; 4113 Scalar::Util::weaken $child;
3997 $child->{c_tab_} ||= do { 4114 $child->{c_tab_} ||= do {
3998 my $tab = 4115 my $tab =
3999 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4116 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4000 ? $child->{c_tab} 4117 ? $child->{c_tab}
4001 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4118 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4002 4119
4003 $tab->connect (activate => sub { 4120 $tab->connect (activate => sub {
4004 $wself->set_current_page ($child); 4121 $wself->set_current_page ($child);
4005 }); 4122 });
4006 4123
4036} 4153}
4037 4154
4038sub pages { 4155sub pages {
4039 my ($self) = @_; 4156 my ($self) = @_;
4040 $self->{multiplexer}->children 4157 $self->{multiplexer}->children
4158}
4159
4160sub page_index {
4161 my ($self, $widget) = @_;
4162
4163 my $i = 0;
4164 for ($self->pages) {
4165 if ($_ eq $widget) { return $i };
4166 $i++;
4167 }
4168
4169 undef
4041} 4170}
4042 4171
4043sub add_tab { 4172sub add_tab {
4044 my ($self, $title, $widget, $tooltip) = @_; 4173 my ($self, $title, $widget, $tooltip) = @_;
4045 4174
4082 } 4211 }
4083} 4212}
4084 4213
4085############################################################################# 4214#############################################################################
4086 4215
4087package CFPlus::UI::Selector; 4216package DC::UI::Selector;
4088 4217
4089use utf8; 4218use utf8;
4090 4219
4091our @ISA = CFPlus::UI::Button::; 4220our @ISA = DC::UI::Button::;
4092 4221
4093sub new { 4222sub new {
4094 my $class = shift; 4223 my $class = shift;
4095 4224
4096 my $self = $class->SUPER::new ( 4225 my $self = $class->SUPER::new (
4113 my ($value, $title, $tooltip) = @$_; 4242 my ($value, $title, $tooltip) = @$_;
4114 4243
4115 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4244 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4116 } 4245 }
4117 4246
4118 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4247 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4119} 4248}
4120 4249
4121sub _set_value { 4250sub _set_value {
4122 my ($self, $value) = @_; 4251 my ($self, $value) = @_;
4123 4252
4124 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4253 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4254 $item ||= $self->{options}[0]
4125 or return; 4255 or return;
4126 4256
4127 $self->{value} = $item->[0]; 4257 $self->{value} = $item->[0];
4128 $self->set_markup ("$item->[1] ⇓"); 4258 $self->set_markup ("$item->[1] ⇓");
4129 $self->set_tooltip ($item->[2]); 4259# $self->set_tooltip ($item->[2]);
4130} 4260}
4131 4261
4132sub set_value { 4262sub set_value {
4133 my ($self, $value) = @_; 4263 my ($self, $value) = @_;
4134 4264
4136 4266
4137 $self->_set_value ($value); 4267 $self->_set_value ($value);
4138 $self->emit (changed => $value); 4268 $self->emit (changed => $value);
4139} 4269}
4140 4270
4271sub set_options {
4272 my ($self, $options) = @_;
4273
4274 $self->{options} = $options;
4275 $self->_set_value ($self->{value});
4276}
4277
4141############################################################################# 4278#############################################################################
4142 4279
4143package CFPlus::UI::Statusbox; 4280package DC::UI::Statusbox;
4144 4281
4145our @ISA = CFPlus::UI::VBox::; 4282our @ISA = DC::UI::VBox::;
4146 4283
4147sub new { 4284sub new {
4148 my $class = shift; 4285 my $class = shift;
4149 4286
4150 my $self = $class->SUPER::new ( 4287 my $self = $class->SUPER::new (
4151 fontsize => 0.8, 4288 fontsize => 0.8,
4152 @_, 4289 @_,
4153 ); 4290 );
4154 4291
4155 CFPlus::weaken (my $this = $self); 4292 DC::weaken (my $this = $self);
4156 4293
4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4294 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4158 4295
4159 $self 4296 $self
4160} 4297}
4161 4298
4162sub reorder { 4299sub reorder {
4163 my ($self) = @_; 4300 my ($self) = @_;
4164 my $NOW = Time::HiRes::time; 4301 my $NOW = AE::time;
4165 4302
4166 # freeze display when hovering over any label 4303 # freeze display when hovering over any label
4167 return if $CFPlus::UI::TOOLTIP->{owner} 4304 return if $DC::UI::TOOLTIP->{owner}
4168 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4305 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4169 values %{ $self->{item} }; 4306 values %{ $self->{item} };
4170 4307
4171 while (my ($k, $v) = each %{ $self->{item} }) { 4308 while (my ($k, $v) = each %{ $self->{item} }) {
4172 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4309 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4173 } 4310 }
4311
4312 $self->{timer}->set (1, 1);
4174 4313
4175 my @widgets; 4314 my @widgets;
4176 4315
4177 my @items = sort { 4316 my @items = sort {
4178 $a->{pri} <=> $b->{pri} 4317 $a->{pri} <=> $b->{pri}
4179 or $b->{id} <=> $a->{id} 4318 or $b->{id} <=> $a->{id}
4180 } values %{ $self->{item} }; 4319 } values %{ $self->{item} };
4181
4182 $self->{timer}->interval (1);
4183 4320
4184 my $count = 10 + 1; 4321 my $count = 10 + 1;
4185 for my $item (@items) { 4322 for my $item (@items) {
4186 last unless --$count; 4323 last unless --$count;
4187 4324
4194 for ($short) { 4331 for ($short) {
4195 s/^\s+//; 4332 s/^\s+//;
4196 s/\s+/ /g; 4333 s/\s+/ /g;
4197 } 4334 }
4198 4335
4199 new CFPlus::UI::Label 4336 new DC::UI::Label
4200 markup => $short, 4337 markup => $short,
4201 tooltip => $item->{tooltip}, 4338 tooltip => $item->{tooltip},
4202 tooltip_font => $::FONT_PROP, 4339 tooltip_font => $::FONT_PROP,
4203 tooltip_width => 0.67, 4340 tooltip_width => 0.67,
4204 fontsize => $item->{fontsize} || $self->{fontsize}, 4341 fontsize => $item->{fontsize} || $self->{fontsize},
4205 max_w => $::WIDTH * 0.44, 4342 max_w => $::WIDTH * 0.44,
4343 align => 0,
4206 fg => [@{ $item->{fg} }], 4344 fg => [@{ $item->{fg} }],
4207 can_events => 1, 4345 can_events => 1,
4208 can_hover => 1 4346 can_hover => 1
4209 }; 4347 };
4210 4348
4211 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4349 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4212 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4350 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4213 $label->update; 4351 $label->update;
4214 $label->set_max_size (undef, $label->{req_h} * $diff) 4352 $label->set_max_size (undef, $label->{req_h} * $diff)
4215 if $diff < 1; 4353 if $diff < 1;
4216 $self->{timer}->interval (1/30); 4354 $self->{timer}->set (1/30, 1/30);
4217 } else { 4355 } else {
4218 $label->{fg}[3] = $item->{fg}[3] || 1; 4356 $label->{fg}[3] = $item->{fg}[3] || 1;
4219 } 4357 }
4220 4358
4221 push @widgets, $label; 4359 push @widgets, $label;
4222 } 4360 }
4361
4362 my $hash = join ",", @widgets;
4363 return if $hash eq $self->{last_widget_hash};
4364 $self->{last_widget_hash} = $hash;
4223 4365
4224 $self->clear; 4366 $self->clear;
4225 $self->SUPER::add (reverse @widgets); 4367 $self->SUPER::add (reverse @widgets);
4226} 4368}
4227 4369
4263 $ROOT->on_refresh (reorder => sub { 4405 $ROOT->on_refresh (reorder => sub {
4264 $self->reorder; 4406 $self->reorder;
4265 }); 4407 });
4266} 4408}
4267 4409
4410sub clr_group {
4411 my ($self, $group) = @_;
4412
4413 if (delete $self->{item}{$group}) {
4414 $ROOT->on_refresh (reorder => sub {
4415 $self->reorder;
4416 });
4417 }
4418}
4419
4268sub reconfigure { 4420sub reconfigure {
4269 my ($self) = @_; 4421 my ($self) = @_;
4270 4422
4271 delete $_->{label} 4423 delete $_->{label}
4272 for values %{ $self->{item} || {} }; 4424 for values %{ $self->{item} || {} };
4283 $self->SUPER::destroy; 4435 $self->SUPER::destroy;
4284} 4436}
4285 4437
4286############################################################################# 4438#############################################################################
4287 4439
4288package CFPlus::UI::Root; 4440package DC::UI::Root;
4289 4441
4290our @ISA = CFPlus::UI::Container::; 4442our @ISA = DC::UI::Container::;
4291 4443
4292use List::Util qw(min max); 4444use List::Util qw(min max);
4293 4445
4294use CFPlus::OpenGL; 4446use DC::OpenGL;
4295 4447
4296sub new { 4448sub new {
4297 my $class = shift; 4449 my $class = shift;
4298 4450
4299 my $self = $class->SUPER::new ( 4451 my $self = $class->SUPER::new (
4300 visible => 1, 4452 visible => 1,
4301 @_, 4453 @_,
4302 ); 4454 );
4303 4455
4304 CFPlus::weaken ($self->{root} = $self); 4456 DC::weaken ($self->{root} = $self);
4305 4457
4306 $self 4458 $self
4307} 4459}
4308 4460
4309sub size_request { 4461sub size_request {
4357} 4509}
4358 4510
4359sub update { 4511sub update {
4360 my ($self) = @_; 4512 my ($self) = @_;
4361 4513
4362 $::WANT_REFRESH->start; 4514 $::WANT_REFRESH = 1;
4363} 4515}
4364 4516
4365sub add { 4517sub add {
4366 my ($self, @children) = @_; 4518 my ($self, @children) = @_;
4367 4519
4434 4586
4435 delete $queue{$widget+0}; 4587 delete $queue{$widget+0};
4436 4588
4437 my ($w, $h) = $widget->size_request; 4589 my ($w, $h) = $widget->size_request;
4438 4590
4439 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4591 $w += $widget->{padding_x} * 2;
4440 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4592 $h += $widget->{padding_y} * 2;
4593
4594 $w = max $widget->{min_w}, $w;
4595 $h = max $widget->{min_h}, $h;
4441 4596
4442 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4597 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4443 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4598 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4444 4599
4445 $w = $widget->{force_w} if exists $widget->{force_w}; 4600 $w = $widget->{force_w} if exists $widget->{force_w};
4512 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4667 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4513 glMatrixMode GL_MODELVIEW; 4668 glMatrixMode GL_MODELVIEW;
4514 glLoadIdentity; 4669 glLoadIdentity;
4515 4670
4516 { 4671 {
4517 package CFPlus::UI::Base; 4672 package DC::UI::Base;
4518 4673
4519 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4674 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4520 (0, 0, $self->{w}, $self->{h}); 4675 (0, 0, $self->{w}, $self->{h});
4521 4676
4522 $self->_draw; 4677 $self->_draw;
4523 } 4678 }
4524} 4679}
4525 4680
4526############################################################################# 4681#############################################################################
4527 4682
4528package CFPlus::UI; 4683package DC::UI;
4529 4684
4530$ROOT = new CFPlus::UI::Root; 4685$ROOT = new DC::UI::Root;
4531$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4686$TOOLTIP = new DC::UI::Tooltip z => 900;
4532 4687
45331 46881
4534

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines