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.436 by root, Wed Aug 22 21:40:57 2007 UTC vs.
Revision 1.478 by root, Tue Dec 22 00:35:44 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use Guard ();
9
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 strict;
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};
269 $self->emit ("destroy"); 272 $self->emit ("destroy");
270 %$self = (); 273 %$self = ();
271} 274}
272 275
273sub TO_JSON { 276sub TO_JSON {
274 { __w_ => $_[0]{s_id} } 277 { "\fw" => $_[0]{s_id} }
275} 278}
276 279
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
360 $self->{force_h} = $h; 369 $self->{force_h} = $h;
361 370
362 $self->realloc; 371 $self->realloc;
363} 372}
364 373
374# traverse the widget chain up to find the maximum "physical" size constraints
375sub get_max_wh {
376 my ($self) = @_;
377
378 my ($w, $h) = @$self{qw(max_w max_h)};
379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
382 ? $self->{parent}->get_max_wh
383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
390}
391
365sub size_request { 392sub size_request {
366 require Carp; 393 require Carp;
367 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
368} 395}
369 396
375 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
376 403
377 if ($self->{aspect}) { 404 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
379 406
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 409
383 # use alignment to adjust x, y 410 # use alignment to adjust x, y
384 411
385 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
427 454
428 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
429 456
430 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
431 458
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 462 }
436} 463}
437 464
438# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
439sub coord2local { 466sub coord2local {
440 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
441 468
442 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
443 470
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 472}
446 473
447# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
448sub coord2global { 475sub coord2global {
449 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
450 477
451 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
452 479
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 481}
455 482
456sub invoke_focus_in { 483sub invoke_focus_in {
505sub connect { 532sub connect {
506 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
507 534
508 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
509 536
510 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
513 } 540 }
514} 541}
515 542
553# in .xs 580# in .xs
554 581
555sub set_parent { 582sub set_parent {
556 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
557 584
558 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
559 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
560} 587}
561 588
562sub realloc { 589sub realloc {
563 my ($self) = @_; 590 my ($self) = @_;
601} 628}
602 629
603sub DESTROY { 630sub DESTROY {
604 my ($self) = @_; 631 my ($self) = @_;
605 632
606 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
607 634
608 local $@; 635 local $@;
609 eval { $self->destroy }; 636 eval { $self->destroy };
610 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
611 638
612 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
613} 640}
614 641
615############################################################################# 642#############################################################################
616 643
617package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
618 645
619our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
620 647
621use strict; 648use strict;
622use CFPlus::OpenGL; 649use DC::OpenGL;
623 650
624sub new { 651sub new {
625 my $class = shift; 652 my $class = shift;
626 653
627 $class->SUPER::new ( 654 $class->SUPER::new (
629 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
630 @_ 657 @_
631 ) 658 )
632} 659}
633 660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
634sub _draw { 668sub _draw {
635 my ($self) = @_; 669 my ($self) = @_;
636 670
637 my $color = $FOCUS == $self && $self->{active_bg} 671 my $color = $FOCUS == $self
638 ? $self->{active_bg} 672 ? $self->{active_bg} || $self->{bg}
639 : $self->{bg}; 673 : $self->{bg};
640 674
641 if ($color && (@$color < 4 || $color->[3])) { 675 if ($color && (@$color < 4 || $color->[3])) {
642 my ($w, $h) = @$self{qw(w h)}; 676 my ($w, $h) = @$self{qw(w h)};
643 677
649 } 683 }
650} 684}
651 685
652############################################################################# 686#############################################################################
653 687
654package CFPlus::UI::Empty; 688package DC::UI::Empty;
655 689
656our @ISA = CFPlus::UI::Base::; 690our @ISA = DC::UI::Base::;
657 691
658sub new { 692sub new {
659 my ($class, %arg) = @_; 693 my ($class, %arg) = @_;
660 $class->SUPER::new (can_events => 0, %arg); 694 $class->SUPER::new (can_events => 0, %arg);
661} 695}
668 702
669sub draw { } 703sub draw { }
670 704
671############################################################################# 705#############################################################################
672 706
673package CFPlus::UI::Container; 707package DC::UI::Container;
674 708
675our @ISA = CFPlus::UI::Base::; 709our @ISA = DC::UI::Base::;
676 710
677sub new { 711sub new {
678 my ($class, %arg) = @_; 712 my ($class, %arg) = @_;
679 713
680 my $children = delete $arg{children}; 714 my $children = delete $arg{children};
774 $_->draw for $self->visible_children; 808 $_->draw for $self->visible_children;
775} 809}
776 810
777############################################################################# 811#############################################################################
778 812
779package CFPlus::UI::Bin; 813package DC::UI::Bin;
780 814
781our @ISA = CFPlus::UI::Container::; 815our @ISA = DC::UI::Container::;
782 816
783sub new { 817sub new {
784 my ($class, %arg) = @_; 818 my ($class, %arg) = @_;
785 819
786 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 820 my $child = (delete $arg{child}) || new DC::UI::Empty::;
787 821
788 $class->SUPER::new (children => [$child], %arg) 822 $class->SUPER::new (children => [$child], %arg)
789} 823}
790 824
791sub add { 825sub add {
798sub remove { 832sub remove {
799 my ($self, $widget) = @_; 833 my ($self, $widget) = @_;
800 834
801 $self->SUPER::remove ($widget); 835 $self->SUPER::remove ($widget);
802 836
803 $self->{children} = [new CFPlus::UI::Empty] 837 $self->{children} = [new DC::UI::Empty]
804 unless @{$self->{children}}; 838 unless @{$self->{children}};
805} 839}
806 840
807sub child { $_[0]->{children}[0] } 841sub child { $_[0]->{children}[0] }
808 842
817 851
818 1 852 1
819} 853}
820 854
821############################################################################# 855#############################################################################
822
823# back-buffered drawing area 856# back-buffered drawing area
824 857
825package CFPlus::UI::Window; 858package DC::UI::Window;
826 859
827our @ISA = CFPlus::UI::Bin::; 860our @ISA = DC::UI::Bin::;
828 861
829use CFPlus::OpenGL; 862use DC::OpenGL;
830 863
831sub new { 864sub new {
832 my ($class, %arg) = @_; 865 my ($class, %arg) = @_;
833 866
834 my $self = $class->SUPER::new (%arg); 867 my $self = $class->SUPER::new (%arg);
856} 889}
857 890
858sub render_child { 891sub render_child {
859 my ($self) = @_; 892 my ($self) = @_;
860 893
861 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 894 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
862 glClearColor 0, 0, 0, 0; 895 glClearColor 0, 0, 0, 0;
863 glClear GL_COLOR_BUFFER_BIT; 896 glClear GL_COLOR_BUFFER_BIT;
864 897
865 { 898 {
866 package CFPlus::UI::Base; 899 package DC::UI::Base;
867 900
868 local ($draw_x, $draw_y, $draw_w, $draw_h) = 901 local ($draw_x, $draw_y, $draw_w, $draw_h) =
869 (0, 0, $self->{w}, $self->{h}); 902 (0, 0, $self->{w}, $self->{h});
870 903
871 $self->_render; 904 $self->_render;
888 glDisable GL_TEXTURE_2D; 921 glDisable GL_TEXTURE_2D;
889} 922}
890 923
891############################################################################# 924#############################################################################
892 925
893package CFPlus::UI::ViewPort; 926package DC::UI::ViewPort;
894 927
895use List::Util qw(min max); 928use List::Util qw(min max);
896 929
897our @ISA = CFPlus::UI::Window::; 930our @ISA = DC::UI::Window::;
898 931
899sub new { 932sub new {
900 my $class = shift; 933 my $class = shift;
901 934
902 $class->SUPER::new ( 935 $class->SUPER::new (
986 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1019 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
987 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1020 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
988 ) { 1021 ) {
989 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1022 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
990 } else { 1023 } else {
991 $self->CFPlus::UI::Base::find_widget ($x, $y) 1024 $self->DC::UI::Base::find_widget ($x, $y)
992 } 1025 }
993} 1026}
994 1027
995sub _render { 1028sub _render {
996 my ($self) = @_; 1029 my ($self) = @_;
997 1030
998 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1031 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
999 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1032 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1000 1033
1001 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1034 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1002 1035
1003 $self->SUPER::_render; 1036 $self->SUPER::_render;
1004} 1037}
1005 1038
1006############################################################################# 1039#############################################################################
1007 1040
1008package CFPlus::UI::ScrolledWindow; 1041package DC::UI::ScrolledWindow;
1009 1042
1010our @ISA = CFPlus::UI::Table::; 1043our @ISA = DC::UI::Table::;
1011 1044
1012sub new { 1045sub new {
1013 my ($class, %arg) = @_; 1046 my ($class, %arg) = @_;
1014 1047
1015 my $child = delete $arg{child}; 1048 my $child = delete $arg{child};
1016 1049
1017 my $self; 1050 my $self;
1018 1051
1019 my $hslider = new CFPlus::UI::Slider 1052 my $hslider = new DC::UI::Slider
1020 c_col => 0, 1053 c_col => 0,
1021 c_row => 1, 1054 c_row => 1,
1022 vertical => 0, 1055 vertical => 0,
1023 range => [0, 0, 1, 0.01], # HACK fix 1056 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1057 on_changed => sub {
1025 $self->{hpos} = $_[1]; 1058 $self->{hpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1059 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1027 }, 1060 },
1028 ; 1061 ;
1029 1062
1030 my $vslider = new CFPlus::UI::Slider 1063 my $vslider = new DC::UI::Slider
1031 c_col => 1, 1064 c_col => 1,
1032 c_row => 0, 1065 c_row => 0,
1033 vertical => 1, 1066 vertical => 1,
1034 range => [0, 0, 1, 0.01], # HACK fix 1067 range => [0, 0, 1, 0.01], # HACK fix
1035 on_changed => sub { 1068 on_changed => sub {
1047 col_expand => [1, 0], 1080 col_expand => [1, 0],
1048 row_expand => [1, 0], 1081 row_expand => [1, 0],
1049 %arg, 1082 %arg,
1050 ); 1083 );
1051 1084
1052 $self->{vp} = new CFPlus::UI::ViewPort 1085 $self->{vp} = new DC::UI::ViewPort
1053 c_col => 0, 1086 c_col => 0,
1054 c_row => 0, 1087 c_row => 0,
1055 expand => 1, 1088 expand => 1,
1056 scroll_x => $self->{scroll_x}, 1089 scroll_x => $self->{scroll_x},
1057 scroll_y => $self->{scroll_y}, 1090 scroll_y => $self->{scroll_y},
1183 $self->SUPER::invoke_size_allocate ($w, $h) 1216 $self->SUPER::invoke_size_allocate ($w, $h)
1184} 1217}
1185 1218
1186############################################################################# 1219#############################################################################
1187 1220
1188package CFPlus::UI::Frame; 1221package DC::UI::Frame;
1189 1222
1190our @ISA = CFPlus::UI::Bin::; 1223our @ISA = DC::UI::Bin::;
1191 1224
1192use CFPlus::OpenGL; 1225use DC::OpenGL;
1193 1226
1194sub new { 1227sub new {
1195 my $class = shift; 1228 my $class = shift;
1196 1229
1197 $class->SUPER::new ( 1230 $class->SUPER::new (
1216 $self->SUPER::_draw; 1249 $self->SUPER::_draw;
1217} 1250}
1218 1251
1219############################################################################# 1252#############################################################################
1220 1253
1221package CFPlus::UI::FancyFrame; 1254package DC::UI::FancyFrame;
1222 1255
1223our @ISA = CFPlus::UI::Bin::; 1256our @ISA = DC::UI::Bin::;
1224 1257
1225use CFPlus::OpenGL; 1258use DC::OpenGL;
1226 1259
1227sub new { 1260sub new {
1228 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1229 1262
1230 if ((exists $arg{label}) && !ref $arg{label}) { 1263 if ((exists $arg{label}) && !ref $arg{label}) {
1231 $arg{label} = new CFPlus::UI::Label 1264 $arg{label} = new DC::UI::Label
1232 align => 1, 1265 align => 1,
1233 valign => 0, 1266 valign => 0.5,
1234 text => $arg{label}, 1267 text => $arg{label},
1235 fontsize => ($arg{border} || 0.8) * 0.75; 1268 fontsize => ($arg{border} || 0.8) * 0.75;
1236 } 1269 }
1237 1270
1238 my $self = $class->SUPER::new ( 1271 my $self = $class->SUPER::new (
1239 # label => "", 1272 # label => "",
1240 fg => [0.6, 0.3, 0.1], 1273 fg => undef,
1241 border => 0.8, 1274 border => 0.8,
1242 style => 'single', 1275 style => 'single',
1243 %arg, 1276 %arg,
1244 ); 1277 );
1245 1278
1248 1281
1249sub add { 1282sub add {
1250 my ($self, @widgets) = @_; 1283 my ($self, @widgets) = @_;
1251 1284
1252 $self->SUPER::add (@widgets); 1285 $self->SUPER::add (@widgets);
1253 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1286 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1254} 1287}
1255 1288
1256sub border { 1289sub border {
1257 int $_[0]{border} * $::FONTSIZE 1290 int $_[0]{border} * $::FONTSIZE
1258} 1291}
1298 my $border = $self->border; 1331 my $border = $self->border;
1299 my ($w, $h) = ($self->{w}, $self->{h}); 1332 my ($w, $h) = ($self->{w}, $self->{h});
1300 1333
1301 $child->draw; 1334 $child->draw;
1302 1335
1303 glColor @{$self->{fg}}; 1336 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1304 glBegin GL_LINE_STRIP; 1337 glBegin GL_LINE_STRIP;
1305 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1338 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1306 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1307 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1308 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1316 } 1349 }
1317} 1350}
1318 1351
1319############################################################################# 1352#############################################################################
1320 1353
1321package CFPlus::UI::Toplevel; 1354package DC::UI::Toplevel;
1322 1355
1323our @ISA = CFPlus::UI::Bin::; 1356our @ISA = DC::UI::Bin::;
1324 1357
1325use CFPlus::OpenGL; 1358use DC::OpenGL;
1326 1359
1327my $bg = 1360my $bg =
1328 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1361 new_from_resource DC::Texture "d1_bg.png",
1329 mipmap => 1, wrap => 1; 1362 mipmap => 1, wrap => 1;
1330 1363
1331my @border = 1364my @border =
1332 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1365 map { new_from_resource DC::Texture $_, mipmap => 1 }
1333 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1366 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1334 1367
1335my @icon = 1368my @icon =
1336 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1369 map { new_from_resource DC::Texture $_, mipmap => 1 }
1337 qw(x1_move.png x1_resize.png); 1370 qw(x1_move.png x1_resize.png);
1338 1371
1339sub new { 1372sub new {
1340 my ($class, %arg) = @_; 1373 my ($class, %arg) = @_;
1341 1374
1342 my $self = $class->SUPER::new ( 1375 my $self = $class->SUPER::new (
1343 bg => [1, 1, 1, 1], 1376 bg => [1, 1, 1, 1],
1344 border_bg => [1, 1, 1, 1], 1377 border_bg => [1, 1, 1, 1],
1345 border => 0.6, 1378 border => 0.8,
1346 can_events => 1, 1379 can_events => 1,
1347 min_w => 64, 1380 min_w => 64,
1348 min_h => 32, 1381 min_h => 32,
1349 %arg, 1382 %arg,
1350 ); 1383 );
1351 1384
1352 $self->{title_widget} = new CFPlus::UI::Label 1385 $self->{title_widget} = new DC::UI::Label
1353 align => 0, 1386 align => 0.5,
1354 valign => 1, 1387 valign => 1,
1355 text => $self->{title}, 1388 text => $self->{title},
1356 fontsize => $self->{border}, 1389 fontsize => $self->{border},
1357 if exists $self->{title}; 1390 if exists $self->{title};
1358 1391
1359 if ($self->{has_close_button}) { 1392 if ($self->{has_close_button}) {
1360 $self->{close_button} = 1393 $self->{close_button} =
1361 new CFPlus::UI::ImageButton 1394 new DC::UI::ImageButton
1362 path => 'x1_close.png', 1395 path => 'x1_close.png',
1363 on_activate => sub { $self->emit ("delete") }; 1396 on_activate => sub { $self->emit ("delete") };
1364 1397
1365 $self->CFPlus::UI::Container::add ($self->{close_button}); 1398 $self->DC::UI::Container::add ($self->{close_button});
1366 } 1399 }
1367 1400
1368 $self 1401 $self
1369} 1402}
1370 1403
1371sub add { 1404sub add {
1372 my ($self, @widgets) = @_; 1405 my ($self, @widgets) = @_;
1373 1406
1374 $self->SUPER::add (@widgets); 1407 $self->SUPER::add (@widgets);
1375 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1408 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1376 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1409 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1377} 1410}
1378 1411
1379sub border { 1412sub border {
1380 int $_[0]{border} * $::FONTSIZE 1413 int $_[0]{border} * $::FONTSIZE
1414}
1415
1416sub get_max_wh {
1417 my ($self) = @_;
1418
1419 return ($self->{w}, $self->{h})
1420 if $self->{visible} && $self->{w};
1421
1422 $self->SUPER::get_max_wh
1381} 1423}
1382 1424
1383sub size_request { 1425sub size_request {
1384 my ($self) = @_; 1426 my ($self) = @_;
1385 1427
1510 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1512 1554
1513 my $border = $self->border; 1555 my $border = $self->border;
1514 1556
1557 if ($border) {
1515 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1516 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1517 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1518 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1519 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1520 1563
1521 # move 1564 # move
1522 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1523 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1524 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1525 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1526 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1527 1570
1528 # resize 1571 # resize
1529 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1530 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1531 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1532 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1533 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1534 1578
1535 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1536 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1537 1581
1538 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
1557 if $self->{close_button}; 1601 if $self->{close_button};
1558} 1602}
1559 1603
1560############################################################################# 1604#############################################################################
1561 1605
1562package CFPlus::UI::Table; 1606package DC::UI::Table;
1563 1607
1564our @ISA = CFPlus::UI::Container::; 1608our @ISA = DC::UI::Container::;
1565 1609
1566use List::Util qw(max sum); 1610use List::Util qw(max sum);
1567 1611
1568use CFPlus::OpenGL; 1612use DC::OpenGL;
1569 1613
1570sub new { 1614sub new {
1571 my $class = shift; 1615 my $class = shift;
1572 1616
1573 $class->SUPER::new ( 1617 $class->SUPER::new (
1617 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1661 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1618 1662
1619 my $sw = sum @w[$c .. $c + $cs - 1]; 1663 my $sw = sum @w[$c .. $c + $cs - 1];
1620 1664
1621 if ($w > $sw) { 1665 if ($w > $sw) {
1622 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1666 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1623 } 1667 }
1624 } 1668 }
1625 1669
1626 # second pass, rows 1670 # second pass, rows
1627 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1671 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1628 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1672 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1629 1673
1630 my $sh = sum @h[$r .. $r + $rs - 1]; 1674 my $sh = sum @h[$r .. $r + $rs - 1];
1631 1675
1632 if ($h > $sh) { 1676 if ($h > $sh) {
1633 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1677 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1634 } 1678 }
1635 } 1679 }
1636 1680
1637 (\@w, \@h) 1681 (\@w, \@h)
1638} 1682}
1661 @col_expand = (1) x @$ws unless @col_expand; 1705 @col_expand = (1) x @$ws unless @col_expand;
1662 my $col_expand = (sum @col_expand) || 1; 1706 my $col_expand = (sum @col_expand) || 1;
1663 1707
1664 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1708 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1665 1709
1666 CFPlus::UI::harmonize $ws; 1710 DC::UI::harmonize $ws;
1667 1711
1668 my @row_expand = @{$self->{row_expand}}; 1712 my @row_expand = @{$self->{row_expand}};
1669 @row_expand = (1) x @$ws unless @row_expand; 1713 @row_expand = (1) x @$ws unless @row_expand;
1670 my $row_expand = (sum @row_expand) || 1; 1714 my $row_expand = (sum @row_expand) || 1;
1671 1715
1672 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1716 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1673 1717
1674 CFPlus::UI::harmonize $hs; 1718 DC::UI::harmonize $hs;
1675 1719
1676 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1720 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1677 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1721 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1678 1722
1679 for my $widget ($self->children) { 1723 for my $widget ($self->children) {
1688 1 1732 1
1689} 1733}
1690 1734
1691############################################################################# 1735#############################################################################
1692 1736
1693package CFPlus::UI::Fixed; 1737package DC::UI::Fixed;
1694 1738
1695use List::Util qw(min max); 1739use List::Util qw(min max);
1696 1740
1697our @ISA = CFPlus::UI::Container::; 1741our @ISA = DC::UI::Container::;
1698 1742
1699sub _scale($$$) { 1743sub _scale($$$) {
1700 my ($rel, $val, $max) = @_; 1744 my ($rel, $val, $max) = @_;
1701 1745
1702 $rel ? $val * $max : $val 1746 $rel ? $val * $max : $val
1754 1 1798 1
1755} 1799}
1756 1800
1757############################################################################# 1801#############################################################################
1758 1802
1759package CFPlus::UI::Box; 1803package DC::UI::Box;
1760 1804
1761our @ISA = CFPlus::UI::Container::; 1805our @ISA = DC::UI::Container::;
1762 1806
1763sub size_request { 1807sub size_request {
1764 my ($self) = @_; 1808 my ($self) = @_;
1765 1809
1766 my @children = $self->visible_children; 1810 my @children = $self->visible_children;
1801 $req[$_] += $space * $children[$_]{expand} 1845 $req[$_] += $space * $children[$_]{expand}
1802 for 0 .. $#children; 1846 for 0 .. $#children;
1803 } 1847 }
1804 } 1848 }
1805 1849
1806 CFPlus::UI::harmonize \@req; 1850 DC::UI::harmonize \@req;
1807 1851
1808 my $pos = 0; 1852 my $pos = 0;
1809 for (0 .. $#children) { 1853 for (0 .. $#children) {
1810 my $alloc = $req[$_]; 1854 my $alloc = $req[$_];
1811 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1855 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1816 1 1860 1
1817} 1861}
1818 1862
1819############################################################################# 1863#############################################################################
1820 1864
1821package CFPlus::UI::HBox; 1865package DC::UI::HBox;
1822 1866
1823our @ISA = CFPlus::UI::Box::; 1867our @ISA = DC::UI::Box::;
1824 1868
1825sub new { 1869sub new {
1826 my $class = shift; 1870 my $class = shift;
1827 1871
1828 $class->SUPER::new ( 1872 $class->SUPER::new (
1831 ) 1875 )
1832} 1876}
1833 1877
1834############################################################################# 1878#############################################################################
1835 1879
1836package CFPlus::UI::VBox; 1880package DC::UI::VBox;
1837 1881
1838our @ISA = CFPlus::UI::Box::; 1882our @ISA = DC::UI::Box::;
1839 1883
1840sub new { 1884sub new {
1841 my $class = shift; 1885 my $class = shift;
1842 1886
1843 $class->SUPER::new ( 1887 $class->SUPER::new (
1846 ) 1890 )
1847} 1891}
1848 1892
1849############################################################################# 1893#############################################################################
1850 1894
1851package CFPlus::UI::Label; 1895package DC::UI::Label;
1852 1896
1853our @ISA = CFPlus::UI::DrawBG::; 1897our @ISA = DC::UI::DrawBG::;
1854 1898
1855use CFPlus::OpenGL; 1899use DC::OpenGL;
1856 1900
1857sub new { 1901sub new {
1858 my ($class, %arg) = @_; 1902 my ($class, %arg) = @_;
1859 1903
1860 my $self = $class->SUPER::new ( 1904 my $self = $class->SUPER::new (
1865 #text => initial text 1909 #text => initial text
1866 #markup => initial narkup 1910 #markup => initial narkup
1867 #max_w => maximum pixel width 1911 #max_w => maximum pixel width
1868 #style => 0, # render flags 1912 #style => 0, # render flags
1869 ellipsise => 3, # end 1913 ellipsise => 3, # end
1870 layout => (new CFPlus::Layout), 1914 layout => (new DC::Layout),
1871 fontsize => 1, 1915 fontsize => 1,
1872 align => -1, 1916 align => 0.5,
1873 valign => -1, 1917 valign => 0.5,
1874 padding_x => 2, 1918 padding_x => 4,
1875 padding_y => 2, 1919 padding_y => 2,
1876 can_events => 0, 1920 can_events => 0,
1877 %arg 1921 %arg
1878 ); 1922 );
1879 1923
1880 if (exists $self->{template}) { 1924 if (exists $self->{template}) {
1881 my $layout = new CFPlus::Layout; 1925 my $layout = new DC::Layout;
1882 $layout->set_text (delete $self->{template}); 1926 $layout->set_text (delete $self->{template});
1883 $self->{template} = $layout; 1927 $self->{template} = $layout;
1884 } 1928 }
1885 1929
1886 if (exists $self->{markup}) { 1930 if (exists $self->{markup}) {
1942 1986
1943sub size_request { 1987sub size_request {
1944 my ($self) = @_; 1988 my ($self) = @_;
1945 1989
1946 $self->{size_req} ||= do { 1990 $self->{size_req} ||= do {
1991 my ($max_w, $max_h) = $self->get_max_wh;
1992
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1993 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1994 $self->{layout}->set_width ($max_w);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1995 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1996 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1998
1953 my ($w, $h) = $self->{layout}->size; 1999 my ($w, $h) = $self->{layout}->size;
1954 2000
1955 if (exists $self->{template}) { 2001 if (exists $self->{template}) {
1956 $self->{template}->set_font ($self->{font}) if $self->{font}; 2002 $self->{template}->set_font ($self->{font}) if $self->{font};
1957 $self->{template}->set_width ($self->{max_w} || -1); 2003 $self->{template}->set_width ($max_w);
1958 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2004 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1959 2005
1960 my ($w2, $h2) = $self->{template}->size; 2006 my ($w2, $h2) = $self->{template}->size;
1961 2007
1962 $w = List::Util::max $w, $w2; 2008 $w = List::Util::max $w, $w2;
2018 2064
2019 [$self->{layout}->size] 2065 [$self->{layout}->size]
2020 }; 2066 };
2021 2067
2022 unless (exists $self->{ox}) { 2068 unless (exists $self->{ox}) {
2023 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2069 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2024 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2070 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2025 : ($self->{w} - $size->[0]) * 0.5);
2026
2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2029 : ($self->{h} - $size->[1]) * 0.5);
2030 2071
2031 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2032 }; 2073 };
2033 2074
2034# unless ($self->{list}) { 2075# unless ($self->{list}) {
2035# $self->{list} = CFPlus::OpenGL::glGenList; 2076# $self->{list} = DC::OpenGL::glGenList;
2036# CFPlus::OpenGL::glNewList $self->{list}; 2077# DC::OpenGL::glNewList $self->{list};
2037# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2078# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2038# CFPlus::OpenGL::glEndList; 2079# DC::OpenGL::glEndList;
2039# } 2080# }
2040# 2081#
2041# CFPlus::OpenGL::glCallList $self->{list}; 2082# DC::OpenGL::glCallList $self->{list};
2042 2083
2043 $self->{layout}->draw; 2084 $self->{layout}->draw;
2044} 2085}
2045 2086
2046#sub destroy { 2087#sub destroy {
2047# my ($self) = @_; 2088# my ($self) = @_;
2048# 2089#
2049# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2090# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2050# 2091#
2051# $self->SUPER::destroy; 2092# $self->SUPER::destroy;
2052#} 2093#}
2053 2094
2054############################################################################# 2095#############################################################################
2055 2096
2056package CFPlus::UI::EntryBase; 2097package DC::UI::EntryBase;
2057 2098
2058our @ISA = CFPlus::UI::Label::; 2099our @ISA = DC::UI::Label::;
2059 2100
2060use CFPlus::OpenGL; 2101use DC::OpenGL;
2061 2102
2062sub new { 2103sub new {
2063 my $class = shift; 2104 my $class = shift;
2064 2105
2065 $class->SUPER::new ( 2106 $class->SUPER::new (
2066 fg => [1, 1, 1], 2107 fg => [1, 1, 1],
2067 bg => [0, 0, 0, 0.2], 2108 bg => [0, 0, 0, 0.2],
2068 outline => [0.6, 0.3, 0.1], 2109 outline => undef,
2069 active_bg => [0, 0, 1, .2], 2110 active_bg => [0, 0, 1, .2],
2070 active_fg => [1, 1, 1], 2111 active_fg => [1, 1, 1],
2071 active_outline => [1, 1, 0], 2112 active_outline => [1, 1, 0],
2072 can_hover => 1, 2113 can_hover => 1,
2073 can_focus => 1, 2114 can_focus => 1,
2115 align => 0,
2074 valign => 0, 2116 valign => 0.5,
2075 can_events => 1, 2117 can_events => 1,
2076 ellipsise => 0, 2118 ellipsise => 0,
2119 padding_x => 4,
2120 padding_y => 2,
2077 #text => ... 2121 #text => ...
2078 #hidden => "*", 2122 #hidden => "*",
2079 @_ 2123 @_
2080 ) 2124 )
2081} 2125}
2128 2172
2129 my $text = $self->get_text; 2173 my $text = $self->get_text;
2130 2174
2131 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2175 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2132 2176
2133 if ($uni == 8) { 2177 if ($sym == DC::SDLK_BACKSPACE) {
2134 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2178 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2135 } elsif ($uni == 127) { 2179 } elsif ($sym == DC::SDLK_DELETE) {
2136 substr $text, $self->{cursor}, 1, ""; 2180 substr $text, $self->{cursor}, 1, "";
2137 } elsif ($sym == CFPlus::SDLK_LEFT) { 2181 } elsif ($sym == DC::SDLK_LEFT) {
2138 --$self->{cursor} if $self->{cursor}; 2182 --$self->{cursor} if $self->{cursor};
2139 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2183 } elsif ($sym == DC::SDLK_RIGHT) {
2140 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2184 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2141 } elsif ($sym == CFPlus::SDLK_HOME) { 2185 } elsif ($sym == DC::SDLK_HOME) {
2142 # what a hack 2186 # what a hack
2143 $self->{cursor} = 2187 $self->{cursor} =
2144 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2188 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2145 ? length $1 2189 ? length $1
2146 : 0; 2190 : 0;
2147 } elsif ($sym == CFPlus::SDLK_END) { 2191 } elsif ($sym == DC::SDLK_END) {
2148 # uh, again 2192 # uh, again
2149 $self->{cursor} = 2193 $self->{cursor} =
2150 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2194 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2151 ? $self->{cursor} + length $1 2195 ? $self->{cursor} + length $1
2152 : length $self->{text}; 2196 : length $self->{text};
2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2277 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2234 } 2278 }
2235 2279
2236 glColor_premultiply @{$self->{active_fg}}; 2280 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2281 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2282 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2283 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2284 glEnd;
2241 2285
2242 glLineWidth 3; 2286 glLineWidth 3;
2243 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2288 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2245 glLineWidth 1; 2289 glLineWidth 1;
2246 2290
2247 } else { 2291 } else {
2248 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3; 2295 glVertex .5, $self->{h} - 2.5;
2253 glVertex $self->{w} - 1, $self->{h} - 3; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2254 glVertex $self->{w} - 1, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
2255 glEnd; 2298 glEnd;
2256 } 2299 }
2257} 2300}
2258 2301
2259############################################################################# 2302#############################################################################
2260 2303
2261package CFPlus::UI::Entry; 2304package DC::UI::Entry;
2262 2305
2263our @ISA = CFPlus::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2264 2307
2265use CFPlus::OpenGL; 2308use DC::OpenGL;
2309
2310sub new {
2311 my $class = shift;
2312
2313 $class->SUPER::new (
2314 history_pointer => -1,
2315 @_
2316 )
2317}
2318
2266 2319
2267sub invoke_key_down { 2320sub invoke_key_down {
2268 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2269 2322
2270 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2276 $self->{history_pointer} = -1; 2329 $self->{history_pointer} = -1;
2277 $self->{history_saveback} = ''; 2330 $self->{history_saveback} = '';
2278 $self->emit (activate => $txt); 2331 $self->emit (activate => $txt);
2279 $self->update; 2332 $self->update;
2280 2333
2281 } elsif ($sym == CFPlus::SDLK_UP) { 2334 } elsif ($sym == DC::SDLK_UP) {
2282 if ($self->{history_pointer} < 0) { 2335 if ($self->{history_pointer} < 0) {
2283 $self->{history_saveback} = $self->get_text; 2336 $self->{history_saveback} = $self->get_text;
2284 } 2337 }
2285 if (@{$self->{history} || []} > 0) { 2338 if (@{$self->{history} || []} > 0) {
2286 $self->{history_pointer}++; 2339 $self->{history_pointer}++;
2288 $self->{history_pointer} = @{$self->{history} || []} - 1; 2341 $self->{history_pointer} = @{$self->{history} || []} - 1;
2289 } 2342 }
2290 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2343 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2291 } 2344 }
2292 2345
2293 } elsif ($sym == CFPlus::SDLK_DOWN) { 2346 } elsif ($sym == DC::SDLK_DOWN) {
2294 $self->{history_pointer}--; 2347 $self->{history_pointer}--;
2295 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2296 2349
2297 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2299 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2300 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2301 } 2357 }
2302 2358
2303 } else { 2359 } else {
2304 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2305 } 2361 }
2307 1 2363 1
2308} 2364}
2309 2365
2310############################################################################# 2366#############################################################################
2311 2367
2312package CFPlus::UI::TextEdit; 2368package DC::UI::TextEdit;
2313 2369
2314our @ISA = CFPlus::UI::EntryBase::; 2370our @ISA = DC::UI::EntryBase::;
2315 2371
2316use CFPlus::OpenGL; 2372use DC::OpenGL;
2373
2374sub new {
2375 my $class = shift;
2376
2377 $class->SUPER::new (
2378 padding_y => 4,
2379
2380 @_
2381 )
2382}
2317 2383
2318sub move_cursor_ver { 2384sub move_cursor_ver {
2319 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2320 2386
2321 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2322 2388
2323 $y += $dy; 2389 $line += $dy;
2324 2390
2325 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2391 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2326 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2327 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2328 $self->update; 2394 $self->update;
2329 return; 2395 return;
2330 } 2396 }
2333sub invoke_key_down { 2399sub invoke_key_down {
2334 my ($self, $ev) = @_; 2400 my ($self, $ev) = @_;
2335 2401
2336 my $sym = $ev->{sym}; 2402 my $sym = $ev->{sym};
2337 2403
2338 if ($sym == CFPlus::SDLK_UP) { 2404 if ($sym == DC::SDLK_UP) {
2339 $self->move_cursor_ver (-1); 2405 $self->move_cursor_ver (-1);
2340 } elsif ($sym == CFPlus::SDLK_DOWN) { 2406 } elsif ($sym == DC::SDLK_DOWN) {
2341 $self->move_cursor_ver (+1); 2407 $self->move_cursor_ver (+1);
2342 } else { 2408 } else {
2343 return $self->SUPER::invoke_key_down ($ev) 2409 return $self->SUPER::invoke_key_down ($ev)
2344 } 2410 }
2345 2411
2346 1 2412 1
2347} 2413}
2348 2414
2349############################################################################# 2415#############################################################################
2350 2416
2351package CFPlus::UI::ButtonBin; 2417package DC::UI::ButtonBin;
2352 2418
2353our @ISA = CFPlus::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2354 2420
2355use CFPlus::OpenGL; 2421use DC::OpenGL;
2356 2422
2357my @tex = 2423my @tex =
2358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2359 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2360 2426
2361sub new { 2427sub new {
2362 my $class = shift; 2428 my $class = shift;
2363 2429
2364 $class->SUPER::new ( 2430 $class->SUPER::new (
2365 can_hover => 1, 2431 can_hover => 1,
2366 align => 0, 2432 align => 0.5,
2367 valign => 0, 2433 valign => 0.5,
2368 can_events => 1, 2434 can_events => 1,
2369 @_ 2435 @_
2370 ) 2436 )
2371} 2437}
2372 2438
2395 $self->SUPER::_draw; 2461 $self->SUPER::_draw;
2396} 2462}
2397 2463
2398############################################################################# 2464#############################################################################
2399 2465
2400package CFPlus::UI::Button; 2466package DC::UI::Button;
2401 2467
2402our @ISA = CFPlus::UI::Label::; 2468our @ISA = DC::UI::Label::;
2403 2469
2404use CFPlus::OpenGL; 2470use DC::OpenGL;
2405 2471
2406my @tex = 2472my @tex =
2407 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2408 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2409 2475
2410sub new { 2476sub new {
2411 my $class = shift; 2477 my $class = shift;
2412 2478
2413 $class->SUPER::new ( 2479 $class->SUPER::new (
2414 padding_x => 4, 2480 padding_x => 8,
2415 padding_y => 4, 2481 padding_y => 4,
2416 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2417 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2418 can_hover => 1, 2484 can_hover => 1,
2419 align => 0, 2485 align => 0.5,
2420 valign => 0, 2486 valign => 0.5,
2421 can_events => 1, 2487 can_events => 1,
2422 @_ 2488 @_
2423 ) 2489 )
2424} 2490}
2425 2491
2450 $self->SUPER::_draw; 2516 $self->SUPER::_draw;
2451} 2517}
2452 2518
2453############################################################################# 2519#############################################################################
2454 2520
2455package CFPlus::UI::CheckBox; 2521package DC::UI::CheckBox;
2456 2522
2457our @ISA = CFPlus::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2458 2524
2459my @tex = 2525my @tex =
2460 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2461 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2462 2528
2463use CFPlus::OpenGL; 2529use DC::OpenGL;
2464 2530
2465sub new { 2531sub new {
2466 my $class = shift; 2532 my $class = shift;
2467 2533
2468 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2469 padding_x => 2, 2536 padding_x => 2,
2470 padding_y => 2, 2537 padding_y => 2,
2471 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2472 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2473 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2479} 2546}
2480 2547
2481sub size_request { 2548sub size_request {
2482 my ($self) = @_; 2549 my ($self) = @_;
2483 2550
2484 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2485} 2552}
2486 2553
2487sub toggle { 2554sub toggle {
2488 my ($self) = @_; 2555 my ($self) = @_;
2489 2556
2508sub _draw { 2575sub _draw {
2509 my ($self) = @_; 2576 my ($self) = @_;
2510 2577
2511 $self->SUPER::_draw; 2578 $self->SUPER::_draw;
2512 2579
2513 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2580 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2514 2581
2515 my ($w, $h) = @$self{qw(w h)}; 2582 my ($w, $h) = @$self{qw(w h)};
2516 2583
2517 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2584 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2518 2585
2525 glDisable GL_TEXTURE_2D; 2592 glDisable GL_TEXTURE_2D;
2526} 2593}
2527 2594
2528############################################################################# 2595#############################################################################
2529 2596
2530package CFPlus::UI::Image; 2597package DC::UI::Image;
2531 2598
2532our @ISA = CFPlus::UI::Base::; 2599our @ISA = DC::UI::DrawBG::;
2533 2600
2534use CFPlus::OpenGL; 2601use DC::OpenGL;
2535 2602
2536our %texture_cache; 2603our %texture_cache;
2537 2604
2538sub new { 2605sub new {
2539 my $class = shift; 2606 my $class = shift;
2546 2613
2547 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2549 2616
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2552 2619
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2554 2621
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2623
2557 $self 2624 $self
2558} 2625}
2559 2626
2560sub STORABLE_freeze { 2627sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2628 my ($self, $cloning) = @_;
2562 2629
2563 $self->{path} 2630 $self->{path}
2564 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2631 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2565 2632
2566 $self->{path} 2633 $self->{path}
2567} 2634}
2568 2635
2569sub STORABLE_attach { 2636sub STORABLE_attach {
2570 my ($self, $cloning, $path) = @_; 2637 my ($self, $cloning, $path) = @_;
2571 2638
2572 $self->new (path => $path) 2639 $self->new (path => $path)
2573} 2640}
2574 2641
2642sub set_texture {
2643 my ($self, $tex) = @_;
2644
2645 $self->{tex} = $tex;
2646 $self->update;
2647}
2648
2575sub size_request { 2649sub size_request {
2576 my ($self) = @_; 2650 my ($self) = @_;
2577 2651
2578 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2652 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2579} 2653}
2580 2654
2581sub _draw { 2655sub _draw {
2582 my ($self) = @_; 2656 my ($self) = @_;
2657
2658 $self->SUPER::_draw;
2583 2659
2584 my $tex = $self->{tex}; 2660 my $tex = $self->{tex};
2585 2661
2586 my ($w, $h) = ($self->{w}, $self->{h}); 2662 my ($w, $h) = ($self->{w}, $self->{h});
2587 2663
2600 glDisable GL_TEXTURE_2D; 2676 glDisable GL_TEXTURE_2D;
2601} 2677}
2602 2678
2603############################################################################# 2679#############################################################################
2604 2680
2605package CFPlus::UI::ImageButton; 2681package DC::UI::ImageButton;
2606 2682
2607our @ISA = CFPlus::UI::Image::; 2683our @ISA = DC::UI::Image::;
2608 2684
2609use CFPlus::OpenGL; 2685use DC::OpenGL;
2610
2611my %textures;
2612 2686
2613sub new { 2687sub new {
2614 my $class = shift; 2688 my $class = shift;
2615 2689
2616 my $self = $class->SUPER::new ( 2690 my $self = $class->SUPER::new (
2617 padding_x => 4, 2691 padding_x => 4,
2618 padding_y => 4, 2692 padding_y => 4,
2619 fg => [1, 1, 1], 2693 fg => [1, 1, 1],
2620 active_fg => [0, 0, 1], 2694 active_fg => [0, 0, 1],
2621 can_hover => 1, 2695 can_hover => 1,
2622 align => 0, 2696 align => 0.5,
2623 valign => 0, 2697 valign => 0.5,
2624 can_events => 1, 2698 can_events => 1,
2625 @_ 2699 @_
2626 ); 2700 );
2627} 2701}
2628 2702
2642 1 2716 1
2643} 2717}
2644 2718
2645############################################################################# 2719#############################################################################
2646 2720
2647package CFPlus::UI::VGauge; 2721package DC::UI::VGauge;
2648 2722
2649our @ISA = CFPlus::UI::Base::; 2723our @ISA = DC::UI::Base::;
2650 2724
2651use List::Util qw(min max); 2725use List::Util qw(min max);
2652 2726
2653use CFPlus::OpenGL; 2727use DC::OpenGL;
2654 2728
2655my %tex = ( 2729my %tex = (
2656 food => [ 2730 food => [
2657 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2731 map { new_from_resource DC::Texture $_, mipmap => 1 }
2658 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2732 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2659 ], 2733 ],
2660 grace => [ 2734 grace => [
2661 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2735 map { new_from_resource DC::Texture $_, mipmap => 1 }
2662 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2736 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2663 ], 2737 ],
2664 hp => [ 2738 hp => [
2665 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2739 map { new_from_resource DC::Texture $_, mipmap => 1 }
2666 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2740 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2667 ], 2741 ],
2668 mana => [ 2742 mana => [
2669 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2743 map { new_from_resource DC::Texture $_, mipmap => 1 }
2670 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2744 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2671 ], 2745 ],
2672); 2746);
2673 2747
2674# eg. VGauge->new (gauge => 'food'), default gauge: food 2748# eg. VGauge->new (gauge => 'food'), default gauge: food
2777 glDisable GL_TEXTURE_2D; 2851 glDisable GL_TEXTURE_2D;
2778} 2852}
2779 2853
2780############################################################################# 2854#############################################################################
2781 2855
2782package CFPlus::UI::Progress; 2856package DC::UI::Progress;
2783 2857
2784our @ISA = CFPlus::UI::Label::; 2858our @ISA = DC::UI::Label::;
2785 2859
2786use CFPlus::OpenGL; 2860use DC::OpenGL;
2787 2861
2788sub new { 2862sub new {
2789 my ($class, %arg) = @_; 2863 my ($class, %arg) = @_;
2790 2864
2791 my $self = $class->SUPER::new ( 2865 my $self = $class->SUPER::new (
2866 padding_x => 2,
2867 padding_y => 2,
2792 fg => [1, 1, 1], 2868 fg => [1, 1, 1],
2793 bg => [0, 0, 1, 0.2], 2869 bg => [0, 0, 1, 0.2],
2794 bar => [0.7, 0.5, 0.1, 0.8], 2870 bar => [0.7, 0.5, 0.1, 0.8],
2795 outline => [0.4, 0.3, 0], 2871 outline => [0.4, 0.3, 0],
2796 fontsize => 0.9, 2872 fontsize => 0.9,
2797 valign => 0, 2873 valign => 0.5,
2798 align => 0, 2874 align => 0.5,
2799 can_events => 1, 2875 can_events => 1,
2800 ellipsise => 1, 2876 ellipsise => 1,
2801 label => "%d%%", 2877 label => "%d%%",
2802 %arg, 2878 %arg,
2803 ); 2879 );
2811 my ($self, $label) = @_; 2887 my ($self, $label) = @_;
2812 2888
2813 return if $self->{label} eq $label; 2889 return if $self->{label} eq $label;
2814 $self->{label} = $label; 2890 $self->{label} = $label;
2815 2891
2816 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2892 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2817} 2893}
2818 2894
2819sub set_value { 2895sub set_value {
2820 my ($self, $value) = @_; 2896 my ($self, $value) = @_;
2821 2897
2836 my ($self) = @_; 2912 my ($self) = @_;
2837 2913
2838 glEnable GL_BLEND; 2914 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2915 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840 2916
2917 my $px = $self->{padding_x};
2918 my $py = $self->{padding_y};
2919
2841 if ($self->{value} >= 0) { 2920 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2921 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2843 2922
2844 glColor_premultiply @{$self->{bar}}; 2923 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2; 2924 glRect $px, $py, $s, $self->{h} - $py;
2846 glColor_premultiply @{$self->{bg}}; 2925 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2926 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2848 } 2927 }
2849 2928
2850 glColor_premultiply @{$self->{outline}}; 2929 glColor_premultiply @{$self->{outline}};
2930
2931 $px -= .5;
2932 $py -= .5;
2933
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2934 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2852 2935
2853 glDisable GL_BLEND; 2936 glDisable GL_BLEND;
2854 2937
2855 { 2938 {
2856 local $self->{bg}; # do not draw background 2939 local $self->{bg}; # do not draw background
2858 } 2941 }
2859} 2942}
2860 2943
2861############################################################################# 2944#############################################################################
2862 2945
2863package CFPlus::UI::ExperienceProgress; 2946package DC::UI::ExperienceProgress;
2864 2947
2865our @ISA = CFPlus::UI::Progress::; 2948our @ISA = DC::UI::Progress::;
2866 2949
2867sub new { 2950sub new {
2868 my ($class, %arg) = @_; 2951 my ($class, %arg) = @_;
2869 2952
2953 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2954
2870 my $self = $class->SUPER::new ( 2955 my $self = $class->SUPER::new (
2956 %arg,
2871 tooltip => sub { 2957 tooltip => sub {
2872 my ($self) = @_; 2958 my ($self) = @_;
2873 2959
2874 sprintf "level %d\n%s points\n%s next level\n%s to go", 2960 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2961 $tt,
2875 $self->{lvl}, 2962 $self->{lvl},
2876 ::formsep ($self->{exp}), 2963 ::formsep ($self->{exp}),
2877 ::formsep ($self->{nxt}), 2964 ::formsep ($self->{nxt}),
2878 ::formsep ($self->{nxt} - $self->{exp}), 2965 ::formsep ($self->{nxt} - $self->{exp}),
2966 $self->_percent * 100,
2879 }, 2967 },
2880 %arg
2881 ); 2968 );
2882 2969
2883 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2970 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2884 if $::CONN; 2971 if $::CONN;
2885 2972
2893 if $::CONN; 2980 if $::CONN;
2894 2981
2895 $self->SUPER::DESTROY; 2982 $self->SUPER::DESTROY;
2896} 2983}
2897 2984
2985sub _percent {
2986 my ($self) = @_;
2987
2988 my $table = $::CONN && $::CONN->{exp_table}
2989 or return -1;
2990
2991 my $l0 = $table->[$self->{lvl} - 1];
2992 my $l1 = $table->[$self->{lvl}];
2993
2994 $self->{nxt} = $l1;
2995
2996 ($self->{exp} - $l0) / ($l1 - $l0)
2997}
2998
2898sub set_value { 2999sub set_value {
2899 my ($self, $lvl, $exp) = @_; 3000 my ($self, $lvl, $exp) = @_;
2900 3001
2901 $self->{lvl} = $lvl; 3002 $self->{lvl} = $lvl;
2902 $self->{exp} = $exp; 3003 $self->{exp} = $exp;
2903 3004
2904 my $v = -1;
2905
2906 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2907 my $l0 = $table->[$lvl - 1];
2908 my $l1 = $table->[$lvl];
2909
2910 $self->{nxt} = $l1;
2911
2912 $v = ($exp - $l0) / ($l1 - $l0);
2913 }
2914
2915 $self->SUPER::set_value ($v); 3005 $self->SUPER::set_value ($self->_percent);
2916} 3006}
2917 3007
2918############################################################################# 3008#############################################################################
2919 3009
2920package CFPlus::UI::Gauge; 3010package DC::UI::Gauge;
2921 3011
2922our @ISA = CFPlus::UI::VBox::; 3012our @ISA = DC::UI::VBox::;
2923 3013
2924sub new { 3014sub new {
2925 my ($class, %arg) = @_; 3015 my ($class, %arg) = @_;
2926 3016
2927 my $self = $class->SUPER::new ( 3017 my $self = $class->SUPER::new (
2929 can_hover => 1, 3019 can_hover => 1,
2930 can_events => 1, 3020 can_events => 1,
2931 %arg, 3021 %arg,
2932 ); 3022 );
2933 3023
2934 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3024 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2935 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3025 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2936 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3026 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2937 3027
2938 $self 3028 $self
2939} 3029}
2940 3030
2941sub set_fontsize { 3031sub set_fontsize {
2962 $self->{value}->set_text ($val); 3052 $self->{value}->set_text ($val);
2963} 3053}
2964 3054
2965############################################################################# 3055#############################################################################
2966 3056
2967package CFPlus::UI::Slider; 3057package DC::UI::Slider;
2968 3058
2969use strict; 3059use strict;
2970 3060
2971use CFPlus::OpenGL; 3061use DC::OpenGL;
2972 3062
2973our @ISA = CFPlus::UI::DrawBG::; 3063our @ISA = DC::UI::DrawBG::;
2974 3064
2975my @tex = 3065my @tex =
2976 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3066 map { new_from_resource DC::Texture $_ }
2977 qw(s1_slider.png s1_slider_bg.png); 3067 qw(s1_slider.png s1_slider_bg.png);
2978 3068
2979sub new { 3069sub new {
2980 my $class = shift; 3070 my $class = shift;
2981 3071
3018sub set_value { 3108sub set_value {
3019 my ($self, $value) = @_; 3109 my ($self, $value) = @_;
3020 3110
3021 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3111 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3022 3112
3023 $hi = $lo + 1 if $hi <= $lo; 3113 $hi = $lo if $hi < $lo;
3024 3114
3025 $page = $hi - $lo if $page > $hi - $lo; 3115 $value = $hi - $page if $value > $hi - $page;
3026
3027 $value = $lo if $value < $lo; 3116 $value = $lo if $value < $lo;
3028 $value = $hi - $page if $value > $hi - $page;
3029 3117
3030 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3118 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3031 if $unit; 3119 if $unit;
3032 3120
3033 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3121 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3077sub invoke_mouse_wheel { 3165sub invoke_mouse_wheel {
3078 my ($self, $ev) = @_; 3166 my ($self, $ev) = @_;
3079 3167
3080 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3168 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3081 3169
3082 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3170 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3083 3171
3084 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3172 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3085 3173
3086 1 3174 1
3087} 3175}
3097 my ($self) = @_; 3185 my ($self) = @_;
3098 3186
3099 unless ($self->{knob_w}) { 3187 unless ($self->{knob_w}) {
3100 $self->set_value ($self->{range}[0]); 3188 $self->set_value ($self->{range}[0]);
3101 3189
3102 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3190 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3103 my $range = ($hi - $page - $lo) || 1e-100; 3191 my $range = ($hi - $page - $lo) || 1e-10;
3104 3192
3105 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3193 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3106 3194
3107 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3195 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3108 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3196 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3109 3197
3110 $value = ($value - $lo) / $range; 3198 $value = ($value - $lo) / $range;
3140 glDisable GL_TEXTURE_2D; 3228 glDisable GL_TEXTURE_2D;
3141} 3229}
3142 3230
3143############################################################################# 3231#############################################################################
3144 3232
3145package CFPlus::UI::ValSlider; 3233package DC::UI::ValSlider;
3146 3234
3147our @ISA = CFPlus::UI::HBox::; 3235our @ISA = DC::UI::HBox::;
3148 3236
3149sub new { 3237sub new {
3150 my ($class, %arg) = @_; 3238 my ($class, %arg) = @_;
3151 3239
3152 my $range = delete $arg{range}; 3240 my $range = delete $arg{range};
3153 3241
3154 my $self = $class->SUPER::new ( 3242 my $self = $class->SUPER::new (
3155 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3243 slider => (new DC::UI::Slider expand => 1, range => $range),
3156 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3244 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3157 to_value => sub { shift }, 3245 to_value => sub { shift },
3158 from_value => sub { shift }, 3246 from_value => sub { shift },
3159 %arg, 3247 %arg,
3160 ); 3248 );
3161 3249
3181sub set_range { shift->{slider}->set_range (@_) } 3269sub set_range { shift->{slider}->set_range (@_) }
3182sub set_value { shift->{slider}->set_value (@_) } 3270sub set_value { shift->{slider}->set_value (@_) }
3183 3271
3184############################################################################# 3272#############################################################################
3185 3273
3186package CFPlus::UI::TextScroller; 3274package DC::UI::TextScroller;
3187 3275
3188our @ISA = CFPlus::UI::HBox::; 3276our @ISA = DC::UI::HBox::;
3189 3277
3190use CFPlus::OpenGL; 3278use DC::OpenGL;
3191 3279
3192sub new { 3280sub new {
3193 my $class = shift; 3281 my $class = shift;
3194 3282
3195 my $self = $class->SUPER::new ( 3283 my $self = $class->SUPER::new (
3197 can_events => 1, 3285 can_events => 1,
3198 indent => 0, 3286 indent => 0,
3199 #font => default_font 3287 #font => default_font
3200 @_, 3288 @_,
3201 3289
3202 layout => (new CFPlus::Layout), 3290 layout => (new DC::Layout),
3203 par => [], 3291 par => [],
3204 max_par => 0, 3292 max_par => 0,
3205 height => 0, 3293 height => 0,
3206 children => [ 3294 children => [
3207 (new CFPlus::UI::Empty expand => 1), 3295 (new DC::UI::Empty expand => 1),
3208 (new CFPlus::UI::Slider vertical => 1), 3296 (new DC::UI::Slider vertical => 1),
3209 ], 3297 ],
3210 ); 3298 );
3211 3299
3212 $self->{children}[1]->connect (changed => sub { $self->update }); 3300 $self->{children}[1]->connect (changed => sub { $self->update });
3213 3301
3269 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3357 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3270 $layout->set_markup ($para->{markup}); 3358 $layout->set_markup ($para->{markup});
3271 3359
3272 $layout->set_shapes ( 3360 $layout->set_shapes (
3273 map 3361 map
3274 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3362 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3275 @{$para->{widget}} 3363 @{$para->{widget}}
3276 ); 3364 );
3277 3365
3278 $layout 3366 $layout
3279} 3367}
3396 $ROOT->on_post_alloc ($self => sub { 3484 $ROOT->on_post_alloc ($self => sub {
3397 $self->force_uptodate; 3485 $self->force_uptodate;
3398 3486
3399 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3487 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3400 3488
3401 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3489 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3402 glClearColor 0, 0, 0, 0; 3490 glClearColor 0, 0, 0, 0;
3403 glClear GL_COLOR_BUFFER_BIT; 3491 glClear GL_COLOR_BUFFER_BIT;
3404 3492
3405 package CFPlus::UI::Base; 3493 package DC::UI::Base;
3406 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3494 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3407 (0, 0, $self->{w}, $self->{h}); 3495 (0, 0, $self->{w}, $self->{h});
3408 3496
3409 my $top = int $self->{children}[1]{range}[0]; 3497 my $top = int $self->{children}[1]{range}[0];
3410 3498
3469 $self->{children}[1]->draw; 3557 $self->{children}[1]->draw;
3470} 3558}
3471 3559
3472############################################################################# 3560#############################################################################
3473 3561
3474package CFPlus::UI::Animator; 3562package DC::UI::Animator;
3475 3563
3476use CFPlus::OpenGL; 3564use DC::OpenGL;
3477 3565
3478our @ISA = CFPlus::UI::Bin::; 3566our @ISA = DC::UI::Bin::;
3479 3567
3480sub moveto { 3568sub moveto {
3481 my ($self, $x, $y) = @_; 3569 my ($self, $x, $y) = @_;
3482 3570
3483 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3571 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3511 glPopMatrix; 3599 glPopMatrix;
3512} 3600}
3513 3601
3514############################################################################# 3602#############################################################################
3515 3603
3516package CFPlus::UI::Flopper; 3604package DC::UI::Flopper;
3517 3605
3518our @ISA = CFPlus::UI::Button::; 3606our @ISA = DC::UI::Button::;
3519 3607
3520sub new { 3608sub new {
3521 my $class = shift; 3609 my $class = shift;
3522 3610
3523 my $self = $class->SUPER::new ( 3611 my $self = $class->SUPER::new (
3535 $self->{other}->toggle_visibility; 3623 $self->{other}->toggle_visibility;
3536} 3624}
3537 3625
3538############################################################################# 3626#############################################################################
3539 3627
3540package CFPlus::UI::Tooltip; 3628package DC::UI::Tooltip;
3541 3629
3542our @ISA = CFPlus::UI::Bin::; 3630our @ISA = DC::UI::Bin::;
3543 3631
3544use CFPlus::OpenGL; 3632use DC::OpenGL;
3545 3633
3546sub new { 3634sub new {
3547 my $class = shift; 3635 my $class = shift;
3548 3636
3549 $class->SUPER::new ( 3637 $class->SUPER::new (
3556 my ($self, $widget) = @_; 3644 my ($self, $widget) = @_;
3557 3645
3558 my $tip = $widget->{tooltip}; 3646 my $tip = $widget->{tooltip};
3559 $tip = $tip->($widget) if "CODE" eq ref $tip; 3647 $tip = $tip->($widget) if "CODE" eq ref $tip;
3560 3648
3561 $tip = CFPlus::Pod::section_label tooltip => $1 3649 $tip = DC::Pod::section_label tooltip => $1
3562 if $tip =~ /^#(.*)$/; 3650 if $tip =~ /^#(.*)$/;
3563 3651
3564 if ($ENV{CFPLUS_DEBUG} & 2) { 3652 if ($ENV{CFPLUS_DEBUG} & 2) {
3565 $tip .= "\n\n" . (ref $widget) . "\n" 3653 $tip .= "\n\n" . (ref $widget) . "\n"
3566 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3654 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3569 } 3657 }
3570 3658
3571 $tip =~ s/^\n+//; 3659 $tip =~ s/^\n+//;
3572 $tip =~ s/\n+$//; 3660 $tip =~ s/\n+$//;
3573 3661
3574 $self->add (new CFPlus::UI::Label 3662 $self->add (new DC::UI::Label
3663 fg => $DC::THEME{tooltip_fg},
3575 markup => $tip, 3664 markup => $tip,
3576 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3665 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3666 align => 0,
3577 fontsize => 0.8, 3667 fontsize => 0.8,
3578 style => 1, # FLAG_INVERSE 3668 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3579 ellipsise => 0, 3669 ellipsise => 0,
3580 font => ($widget->{tooltip_font} || $::FONT_PROP), 3670 font => ($widget->{tooltip_font} || $::FONT_PROP),
3581 ); 3671 );
3582} 3672}
3583 3673
3618} 3708}
3619 3709
3620sub _draw { 3710sub _draw {
3621 my ($self) = @_; 3711 my ($self) = @_;
3622 3712
3623 glTranslate 0.375, 0.375;
3624
3625 my ($w, $h) = @$self{qw(w h)}; 3713 my ($w, $h) = @$self{qw(w h)};
3626 3714
3627 glColor 1, 0.8, 0.4; 3715 glColor @{ $DC::THEME{tooltip_bg} };
3628 glRect 0, 0, $w, $h; 3716 glRect 0, 0, $w, $h;
3629 3717
3630 glColor 0, 0, 0; 3718 glColor @{ $DC::THEME{tooltip_border} };
3631 glRect_lineloop 0, 0, $w, $h; 3719 glRect_lineloop .5, .5, $w + .5, $h + .5;
3632 3720
3633 glTranslate 2 - 0.375, 2 - 0.375; 3721 glTranslate 2, 2;
3634 3722
3635 $self->SUPER::_draw; 3723 $self->SUPER::_draw;
3636} 3724}
3637 3725
3638############################################################################# 3726#############################################################################
3639 3727
3640package CFPlus::UI::Face; 3728package DC::UI::Face;
3641 3729
3642our @ISA = CFPlus::UI::DrawBG::; 3730our @ISA = DC::UI::DrawBG::;
3643 3731
3644use CFPlus::OpenGL; 3732use DC::OpenGL;
3645 3733
3646sub new { 3734sub new {
3647 my $class = shift; 3735 my $class = shift;
3648 3736
3649 my $self = $class->SUPER::new ( 3737 my $self = $class->SUPER::new (
3652 aspect => 1, 3740 aspect => 1,
3653 can_events => 0, 3741 can_events => 0,
3654 @_, 3742 @_,
3655 ); 3743 );
3656 3744
3657 if ($self->{anim} && $self->{animspeed}) {
3658 CFPlus::weaken (my $widget = $self);
3659
3660 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3661 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3662 $self->{timer} = Event->timer (
3663 parked => 1,
3664 cb => sub {
3665 return unless $::CONN;
3666
3667 my $w = $widget
3668 or return;
3669
3670 ++$w->{frame};
3671 $w->update_face;
3672
3673 # somehow, $widget can go away
3674 $w->update;
3675 $w->update_timer;
3676 },
3677 );
3678
3679 $self->update_face;
3680 $self->update_timer; 3745 $self->update_anim;
3681 }
3682 3746
3683 $self 3747 $self
3684} 3748}
3685 3749
3686sub update_timer { 3750sub update_timer {
3687 my ($self) = @_; 3751 my ($self) = @_;
3688 3752
3689 return unless $self->{timer}; 3753 return unless $self->{timer};
3690 3754
3691 if ($self->{visible}) { 3755 if ($self->{visible}) {
3692 $self->{timer}->at (
3693 $self->{anim_start}
3694 + $self->{animspeed}
3695 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3696 );
3697 $self->{timer}->start; 3756 $self->{timer}->start;
3698 } else { 3757 } else {
3699 $self->{timer}->stop; 3758 $self->{timer}->stop;
3700 } 3759 }
3701} 3760}
3714 $tex->upload (sub { $self->reconfigure }); 3773 $tex->upload (sub { $self->reconfigure });
3715 } 3774 }
3716 } 3775 }
3717 } 3776 }
3718 } 3777 }
3778 }
3779}
3780
3781sub update_anim {
3782 my ($self) = @_;
3783
3784 if ($self->{anim} && $self->{animspeed}) {
3785 DC::weaken (my $widget = $self);
3786
3787 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3788 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3789 return unless $::CONN;
3790
3791 my $w = $widget
3792 or return;
3793
3794 ++$w->{frame};
3795 $w->update_face;
3796
3797 # somehow, $widget can go away
3798 $w->update;
3799 $w->update_timer;
3800 };
3801
3802 $self->update_face;
3803 $self->update_timer;
3804 } else {
3805 delete $self->{timer};
3719 } 3806 }
3720} 3807}
3721 3808
3722sub size_request { 3809sub size_request {
3723 my ($self) = @_; 3810 my ($self) = @_;
3745 return unless $self->{visible}; 3832 return unless $self->{visible};
3746 3833
3747 $self->SUPER::update; 3834 $self->SUPER::update;
3748} 3835}
3749 3836
3837sub set_face {
3838 my ($self, $face) = @_;
3839
3840 $self->{face} = $face;
3841 $self->reconfigure;
3842}
3843
3844sub set_anim {
3845 my ($self, $anim) = @_;
3846
3847 $self->{anim} = $anim;
3848 $self->update_anim;
3849}
3850
3851sub set_animspeed {
3852 my ($self, $animspeed) = @_;
3853
3854 $self->{animspeed} = $animspeed;
3855 $self->update_anim;
3856}
3857
3750sub invoke_visibility_change { 3858sub invoke_visibility_change {
3751 my ($self) = @_; 3859 my ($self) = @_;
3752 3860
3753 $self->update_timer; 3861 $self->update_timer;
3754 3862
3778 $self->SUPER::destroy; 3886 $self->SUPER::destroy;
3779} 3887}
3780 3888
3781############################################################################# 3889#############################################################################
3782 3890
3783package CFPlus::UI::Buttonbar; 3891package DC::UI::Buttonbar;
3784 3892
3785our @ISA = CFPlus::UI::HBox::; 3893our @ISA = DC::UI::HBox::;
3786 3894
3787# TODO: should actually wrap buttons and other goodies. 3895# TODO: should actually wrap buttons and other goodies.
3788 3896
3789############################################################################# 3897#############################################################################
3790 3898
3791package CFPlus::UI::Menu; 3899package DC::UI::Menu;
3792 3900
3793our @ISA = CFPlus::UI::Toplevel::; 3901our @ISA = DC::UI::Toplevel::;
3794 3902
3795use CFPlus::OpenGL; 3903use DC::OpenGL;
3796 3904
3797sub new { 3905sub new {
3798 my $class = shift; 3906 my $class = shift;
3799 3907
3800 my $self = $class->SUPER::new ( 3908 my $self = $class->SUPER::new (
3801 items => [], 3909 items => [],
3802 z => 100, 3910 z => 100,
3803 @_, 3911 @_,
3804 ); 3912 );
3805 3913
3806 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3914 $self->add ($self->{vbox} = new DC::UI::VBox);
3807 3915
3808 for my $item (@{ $self->{items} }) { 3916 for my $item (@{ $self->{items} }) {
3809 my ($widget, $cb, $tooltip) = @$item; 3917 my ($widget, $cb, $tooltip) = @$item;
3810 3918
3811 # handle various types of items, only text for now 3919 # handle various types of items, only text for now
3812 if (!ref $widget) { 3920 if (!ref $widget) {
3813 if ($widget =~ /\t/) { 3921 if ($widget =~ /\t/) {
3814 my ($left, $right) = split /\t/, $widget, 2; 3922 my ($left, $right) = split /\t/, $widget, 2;
3815 3923
3816 $widget = new CFPlus::UI::HBox 3924 $widget = new DC::UI::HBox
3817 can_hover => 1, 3925 can_hover => 1,
3818 can_events => 1, 3926 can_events => 1,
3819 tooltip => $tooltip, 3927 tooltip => $tooltip,
3820 children => [ 3928 children => [
3821 (new CFPlus::UI::Label markup => $left, expand => 1), 3929 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3822 (new CFPlus::UI::Label markup => $right, align => +1), 3930 (new DC::UI::Label markup => $right, align => 1),
3823 ], 3931 ],
3824 ; 3932 ;
3825 3933
3826 } else { 3934 } else {
3827 $widget = new CFPlus::UI::Label 3935 $widget = new DC::UI::Label
3828 can_hover => 1, 3936 can_hover => 1,
3829 can_events => 1, 3937 can_events => 1,
3938 align => 0,
3830 markup => $widget, 3939 markup => $widget,
3831 tooltip => $tooltip; 3940 tooltip => $tooltip;
3832 } 3941 }
3833 } 3942 }
3834 3943
3849 # maybe save $GRAB? must be careful about events... 3958 # maybe save $GRAB? must be careful about events...
3850 $GRAB = $self; 3959 $GRAB = $self;
3851 $self->{button} = $ev->{button}; 3960 $self->{button} = $ev->{button};
3852 3961
3853 $self->show; 3962 $self->show;
3854 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3963
3964 my $x = $ev->{x};
3965 my $y = $ev->{y};
3966
3967 $self->{root}->on_post_alloc ($self => sub {
3968 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3969 });
3970
3971 1 # so it can be used inside event handlers
3855} 3972}
3856 3973
3857sub invoke_mouse_motion { 3974sub invoke_mouse_motion {
3858 my ($self, $ev, $x, $y) = @_; 3975 my ($self, $ev, $x, $y) = @_;
3859 3976
3880 1 3997 1
3881} 3998}
3882 3999
3883############################################################################# 4000#############################################################################
3884 4001
3885package CFPlus::UI::Multiplexer; 4002package DC::UI::Multiplexer;
3886 4003
3887our @ISA = CFPlus::UI::Container::; 4004our @ISA = DC::UI::Container::;
3888 4005
3889sub new { 4006sub new {
3890 my $class = shift; 4007 my $class = shift;
3891 4008
3892 my $self = $class->SUPER::new ( 4009 my $self = $class->SUPER::new (
3893 @_, 4010 @_,
3894 ); 4011 );
3895 4012
3896 $self->{current} = $self->{children}[0] 4013 $self->set_current_page (0);
3897 if @{ $self->{children} };
3898 4014
3899 $self 4015 $self
3900} 4016}
3901 4017
3902sub add { 4018sub add {
3903 my ($self, @widgets) = @_; 4019 my ($self, @widgets) = @_;
3904 4020
3905 $self->SUPER::add (@widgets); 4021 $self->SUPER::add (@widgets);
3906 4022
3907 $self->{current} = $self->{children}[0] 4023 $self->set_current_page (0)
3908 if @{ $self->{children} }; 4024 if @widgets == @{ $self->{children} };
3909} 4025}
3910 4026
3911sub get_current_page { 4027sub get_current_page {
3912 my ($self) = @_; 4028 my ($self) = @_;
3913 4029
3919 4035
3920 my $widget = ref $page_or_widget 4036 my $widget = ref $page_or_widget
3921 ? $page_or_widget 4037 ? $page_or_widget
3922 : $self->{children}[$page_or_widget]; 4038 : $self->{children}[$page_or_widget];
3923 4039
4040 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4041
3924 $self->{current} = $widget; 4042 if (($self->{current} = $widget)) {
4043 $self->{current}->set_visible if $self->{current} && $self->{visible};
3925 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4044 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3926 4045
3927 $self->emit (page_changed => $self->{current}); 4046 $self->emit (page_changed => $self->{current});
4047 }
3928 4048
3929 $self->realloc; 4049 $self->realloc;
3930} 4050}
3931 4051
3932sub visible_children { 4052sub visible_children {
3933 $_[0]{current} 4053 $_[0]{current} || ()
3934} 4054}
3935 4055
3936sub size_request { 4056sub size_request {
3937 my ($self) = @_; 4057 my ($self) = @_;
3938 4058
4059 $self->{current}
3939 $self->{current}->size_request 4060 ? $self->{current}->size_request
4061 : (0, 0)
3940} 4062}
3941 4063
3942sub invoke_size_allocate { 4064sub invoke_size_allocate {
3943 my ($self, $w, $h) = @_; 4065 my ($self, $w, $h) = @_;
3944 4066
3945 $self->{current}->configure (0, 0, $w, $h); 4067 $self->{current}->configure (0, 0, $w, $h)
4068 if $self->{current};
3946 4069
3947 1 4070 1
3948} 4071}
3949 4072
3950sub _draw { 4073sub _draw {
3951 my ($self) = @_; 4074 my ($self) = @_;
3952 4075
3953 $self->{current}->draw; 4076 $self->{current}->draw
4077 if $self->{current};
3954} 4078}
3955 4079
3956############################################################################# 4080#############################################################################
3957 4081
3958package CFPlus::UI::Notebook; 4082package DC::UI::Notebook;
3959 4083
3960use CFPlus::OpenGL; 4084use DC::OpenGL;
3961 4085
3962our @ISA = CFPlus::UI::VBox::; 4086our @ISA = DC::UI::VBox::;
3963 4087
3964sub new { 4088sub new {
3965 my $class = shift; 4089 my $class = shift;
3966 4090
3967 my $self = $class->SUPER::new ( 4091 my $self = $class->SUPER::new (
3968 buttonbar => (new CFPlus::UI::Buttonbar), 4092 buttonbar => (new DC::UI::Buttonbar),
3969 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4093 multiplexer => (new DC::UI::Multiplexer expand => 1),
3970 active_outline => [1, 1, 0], 4094 active_outline => [.7, .7, 0.2],
3971 # filter => # will be put between multiplexer and $self 4095 # filter => # will be put between multiplexer and $self
3972 @_, 4096 @_,
3973 ); 4097 );
3974 4098
3975 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4099 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3983 4107
3984 for my $child (@$widgets) { 4108 for my $child (@$widgets) {
3985 Scalar::Util::weaken $child; 4109 Scalar::Util::weaken $child;
3986 $child->{c_tab_} ||= do { 4110 $child->{c_tab_} ||= do {
3987 my $tab = 4111 my $tab =
3988 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4112 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3989 ? $child->{c_tab} 4113 ? $child->{c_tab}
3990 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4114 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3991 4115
3992 $tab->connect (activate => sub { 4116 $tab->connect (activate => sub {
3993 $wself->set_current_page ($child); 4117 $wself->set_current_page ($child);
3994 }); 4118 });
3995 4119
4027sub pages { 4151sub pages {
4028 my ($self) = @_; 4152 my ($self) = @_;
4029 $self->{multiplexer}->children 4153 $self->{multiplexer}->children
4030} 4154}
4031 4155
4156sub page_index {
4157 my ($self, $widget) = @_;
4158
4159 my $i = 0;
4160 for ($self->pages) {
4161 if ($_ eq $widget) { return $i };
4162 $i++;
4163 }
4164
4165 undef
4166}
4167
4032sub add_tab { 4168sub add_tab {
4033 my ($self, $title, $widget, $tooltip) = @_; 4169 my ($self, $title, $widget, $tooltip) = @_;
4034 4170
4035 $title = [$title, $tooltip] unless ref $title; 4171 $title = [$title, $tooltip] unless ref $title;
4036 $widget->{c_tab} = $title; 4172 $widget->{c_tab} = $title;
4056 4192
4057 $self->SUPER::_draw (); 4193 $self->SUPER::_draw ();
4058 4194
4059 if (my $cur = $self->{multiplexer}{current}) { 4195 if (my $cur = $self->{multiplexer}{current}) {
4060 if ($cur = $cur->{c_tab_}) { 4196 if ($cur = $cur->{c_tab_}) {
4061 glTranslate $cur->{x}, $cur->{y}; 4197 glTranslate $self->{buttonbar}{x} + $cur->{x},
4198 $self->{buttonbar}{y} + $cur->{y};
4062 glLineWidth 3; 4199 glLineWidth 3;
4200 #glEnable GL_BLEND;
4201 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4063 glColor @{$self->{active_outline}}; 4202 glColor @{$self->{active_outline}};
4064 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4203 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4065 glLineWidth 1; 4204 glLineWidth 1;
4205 #glDisable GL_BLEND;
4066 } 4206 }
4067 } 4207 }
4068} 4208}
4069 4209
4070############################################################################# 4210#############################################################################
4071 4211
4072package CFPlus::UI::Selector; 4212package DC::UI::Selector;
4073 4213
4074use utf8; 4214use utf8;
4075 4215
4076our @ISA = CFPlus::UI::Button::; 4216our @ISA = DC::UI::Button::;
4077 4217
4078sub new { 4218sub new {
4079 my $class = shift; 4219 my $class = shift;
4080 4220
4081 my $self = $class->SUPER::new ( 4221 my $self = $class->SUPER::new (
4098 my ($value, $title, $tooltip) = @$_; 4238 my ($value, $title, $tooltip) = @$_;
4099 4239
4100 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4240 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4101 } 4241 }
4102 4242
4103 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4243 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4104} 4244}
4105 4245
4106sub _set_value { 4246sub _set_value {
4107 my ($self, $value) = @_; 4247 my ($self, $value) = @_;
4108 4248
4109 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4249 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4250 $item ||= $self->{options}[0]
4110 or return; 4251 or return;
4111 4252
4112 $self->{value} = $item->[0]; 4253 $self->{value} = $item->[0];
4113 $self->set_markup ("$item->[1] ⇓"); 4254 $self->set_markup ("$item->[1] ⇓");
4114 $self->set_tooltip ($item->[2]); 4255# $self->set_tooltip ($item->[2]);
4115} 4256}
4116 4257
4117sub set_value { 4258sub set_value {
4118 my ($self, $value) = @_; 4259 my ($self, $value) = @_;
4119 4260
4121 4262
4122 $self->_set_value ($value); 4263 $self->_set_value ($value);
4123 $self->emit (changed => $value); 4264 $self->emit (changed => $value);
4124} 4265}
4125 4266
4267sub set_options {
4268 my ($self, $options) = @_;
4269
4270 $self->{options} = $options;
4271 $self->_set_value ($self->{value});
4272}
4273
4126############################################################################# 4274#############################################################################
4127 4275
4128package CFPlus::UI::Statusbox; 4276package DC::UI::Statusbox;
4129 4277
4130our @ISA = CFPlus::UI::VBox::; 4278our @ISA = DC::UI::VBox::;
4131 4279
4132sub new { 4280sub new {
4133 my $class = shift; 4281 my $class = shift;
4134 4282
4135 my $self = $class->SUPER::new ( 4283 my $self = $class->SUPER::new (
4136 fontsize => 0.8, 4284 fontsize => 0.8,
4137 @_, 4285 @_,
4138 ); 4286 );
4139 4287
4140 CFPlus::weaken (my $this = $self); 4288 DC::weaken (my $this = $self);
4141 4289
4142 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4290 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4143 4291
4144 $self 4292 $self
4145} 4293}
4146 4294
4147sub reorder { 4295sub reorder {
4148 my ($self) = @_; 4296 my ($self) = @_;
4149 my $NOW = Time::HiRes::time; 4297 my $NOW = EV::time;
4150 4298
4151 # freeze display when hovering over any label 4299 # freeze display when hovering over any label
4152 return if $CFPlus::UI::TOOLTIP->{owner} 4300 return if $DC::UI::TOOLTIP->{owner}
4153 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4301 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4154 values %{ $self->{item} }; 4302 values %{ $self->{item} };
4155 4303
4156 while (my ($k, $v) = each %{ $self->{item} }) { 4304 while (my ($k, $v) = each %{ $self->{item} }) {
4157 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4305 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4158 } 4306 }
4307
4308 $self->{timer}->set (1, 1);
4159 4309
4160 my @widgets; 4310 my @widgets;
4161 4311
4162 my @items = sort { 4312 my @items = sort {
4163 $a->{pri} <=> $b->{pri} 4313 $a->{pri} <=> $b->{pri}
4164 or $b->{id} <=> $a->{id} 4314 or $b->{id} <=> $a->{id}
4165 } values %{ $self->{item} }; 4315 } values %{ $self->{item} };
4166
4167 $self->{timer}->interval (1);
4168 4316
4169 my $count = 10 + 1; 4317 my $count = 10 + 1;
4170 for my $item (@items) { 4318 for my $item (@items) {
4171 last unless --$count; 4319 last unless --$count;
4172 4320
4179 for ($short) { 4327 for ($short) {
4180 s/^\s+//; 4328 s/^\s+//;
4181 s/\s+/ /g; 4329 s/\s+/ /g;
4182 } 4330 }
4183 4331
4184 new CFPlus::UI::Label 4332 new DC::UI::Label
4185 markup => $short, 4333 markup => $short,
4186 tooltip => $item->{tooltip}, 4334 tooltip => $item->{tooltip},
4187 tooltip_font => $::FONT_PROP, 4335 tooltip_font => $::FONT_PROP,
4188 tooltip_width => 0.67, 4336 tooltip_width => 0.67,
4189 fontsize => $item->{fontsize} || $self->{fontsize}, 4337 fontsize => $item->{fontsize} || $self->{fontsize},
4190 max_w => $::WIDTH * 0.44, 4338 max_w => $::WIDTH * 0.44,
4339 align => 0,
4191 fg => [@{ $item->{fg} }], 4340 fg => [@{ $item->{fg} }],
4192 can_events => 1, 4341 can_events => 1,
4193 can_hover => 1 4342 can_hover => 1
4194 }; 4343 };
4195 4344
4196 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4345 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4197 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4346 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4198 $label->update; 4347 $label->update;
4199 $label->set_max_size (undef, $label->{req_h} * $diff) 4348 $label->set_max_size (undef, $label->{req_h} * $diff)
4200 if $diff < 1; 4349 if $diff < 1;
4201 $self->{timer}->interval (1/30); 4350 $self->{timer}->set (1/30, 1/30);
4202 } else { 4351 } else {
4203 $label->{fg}[3] = $item->{fg}[3] || 1; 4352 $label->{fg}[3] = $item->{fg}[3] || 1;
4204 } 4353 }
4205 4354
4206 push @widgets, $label; 4355 push @widgets, $label;
4207 } 4356 }
4357
4358 my $hash = join ",", @widgets;
4359 return if $hash eq $self->{last_widget_hash};
4360 $self->{last_widget_hash} = $hash;
4208 4361
4209 $self->clear; 4362 $self->clear;
4210 $self->SUPER::add (reverse @widgets); 4363 $self->SUPER::add (reverse @widgets);
4211} 4364}
4212 4365
4268 $self->SUPER::destroy; 4421 $self->SUPER::destroy;
4269} 4422}
4270 4423
4271############################################################################# 4424#############################################################################
4272 4425
4273package CFPlus::UI::Root; 4426package DC::UI::Root;
4274 4427
4275our @ISA = CFPlus::UI::Container::; 4428our @ISA = DC::UI::Container::;
4276 4429
4277use List::Util qw(min max); 4430use List::Util qw(min max);
4278 4431
4279use CFPlus::OpenGL; 4432use DC::OpenGL;
4280 4433
4281sub new { 4434sub new {
4282 my $class = shift; 4435 my $class = shift;
4283 4436
4284 my $self = $class->SUPER::new ( 4437 my $self = $class->SUPER::new (
4285 visible => 1, 4438 visible => 1,
4286 @_, 4439 @_,
4287 ); 4440 );
4288 4441
4289 CFPlus::weaken ($self->{root} = $self); 4442 DC::weaken ($self->{root} = $self);
4290 4443
4291 $self 4444 $self
4292} 4445}
4293 4446
4294sub size_request { 4447sub size_request {
4342} 4495}
4343 4496
4344sub update { 4497sub update {
4345 my ($self) = @_; 4498 my ($self) = @_;
4346 4499
4347 $::WANT_REFRESH->start; 4500 $::WANT_REFRESH = 1;
4348} 4501}
4349 4502
4350sub add { 4503sub add {
4351 my ($self, @children) = @_; 4504 my ($self, @children) = @_;
4352 4505
4419 4572
4420 delete $queue{$widget+0}; 4573 delete $queue{$widget+0};
4421 4574
4422 my ($w, $h) = $widget->size_request; 4575 my ($w, $h) = $widget->size_request;
4423 4576
4424 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4577 $w += $widget->{padding_x} * 2;
4425 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4578 $h += $widget->{padding_y} * 2;
4579
4580 $w = max $widget->{min_w}, $w;
4581 $h = max $widget->{min_h}, $h;
4426 4582
4427 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4583 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4428 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4584 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4429 4585
4430 $w = $widget->{force_w} if exists $widget->{force_w}; 4586 $w = $widget->{force_w} if exists $widget->{force_w};
4497 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4653 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4498 glMatrixMode GL_MODELVIEW; 4654 glMatrixMode GL_MODELVIEW;
4499 glLoadIdentity; 4655 glLoadIdentity;
4500 4656
4501 { 4657 {
4502 package CFPlus::UI::Base; 4658 package DC::UI::Base;
4503 4659
4504 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4660 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4505 (0, 0, $self->{w}, $self->{h}); 4661 (0, 0, $self->{w}, $self->{h});
4506 4662
4507 $self->_draw; 4663 $self->_draw;
4508 } 4664 }
4509} 4665}
4510 4666
4511############################################################################# 4667#############################################################################
4512 4668
4513package CFPlus::UI; 4669package DC::UI;
4514 4670
4515$ROOT = new CFPlus::UI::Root; 4671$ROOT = new DC::UI::Root;
4516$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4672$TOOLTIP = new DC::UI::Tooltip z => 900;
4517 4673
45181 46741
4519

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines