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.445 by root, Sat Dec 8 19:17:02 2007 UTC vs.
Revision 1.477 by root, Sun Jan 11 23:50:27 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 ();
7 7
8use CFPlus; 8use Guard ();
9
10use DC;
9use CFPlus::Pod; 11use DC::Pod;
10use CFPlus::Texture; 12use DC::Texture;
11 13
12our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
13 15
14our $LAYOUT; 16our $LAYOUT;
15our $ROOT; 17our $ROOT;
16our $TOOLTIP; 18our $TOOLTIP;
17our $BUTTON_STATE; 19our $BUTTON_STATE;
18 20
19our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
20 22
21our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop; 24 $_[0]->stop;
23 25
24 if (!$GRAB) { 26 if (!$GRAB) {
25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
26 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
111 113
112 if ($GRAB) { 114 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 116 # mousewheel
115 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
117 119
118 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
119 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
120 122
121 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
189# call when resolution changes etc. 191# call when resolution changes etc.
190sub rescale_widgets { 192sub rescale_widgets {
191 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
192 194
193 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $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.]+$/;
196 $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.]+$/;
197 199
198 $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.]+$/;
199 $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};
211 reconfigure_widgets; 213 reconfigure_widgets;
212} 214}
213 215
214############################################################################# 216#############################################################################
215 217
216package CFPlus::UI::Event; 218package DC::UI::Event;
217 219
218sub xy { 220sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220} 222}
221 223
222############################################################################# 224#############################################################################
223 225
224package CFPlus::UI::Base; 226package DC::UI::Base;
225 227
226use strict; 228use strict;
227 229
228use CFPlus::OpenGL; 230use DC::OpenGL;
229 231
230sub new { 232sub new {
231 my $class = shift; 233 my $class = shift;
232 234
233 my $self = bless { 235 my $self = bless {
238 h => undef, 240 h => undef,
239 can_events => 1, 241 can_events => 1,
240 @_ 242 @_
241 }, $class; 243 }, $class;
242 244
243 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
244 246
245 for (keys %$self) { 247 for (keys %$self) {
246 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
247 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
248 } 250 }
249 } 251 }
250 252
251 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
252 $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};
253 $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};
254 $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};
255 $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};
256 258
257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
259 261
260 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
278sub show { 280sub show {
279 my ($self) = @_; 281 my ($self) = @_;
280 282
281 return if $self->{parent}; 283 return if $self->{parent};
282 284
283 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
284} 286}
285 287
286sub set_visible { 288sub set_visible {
287 my ($self) = @_; 289 my ($self) = @_;
288 290
289 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);
290 295
291 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
293 298
294 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
295 300
296 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
297 302
298 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
299} 304}
300 305
301sub set_invisible { 306sub set_invisible {
302 my ($self) = @_; 307 my ($self) = @_;
303 308
309 delete $self->{root}; 314 delete $self->{root};
310 315
311 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
312 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
313 318
314 $CFPlus::UI::TOOLTIP_WATCHER->invoke 319 $DC::UI::TOOLTIP_WATCHER->invoke
315 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
316 321
317 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
318 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
319} 324}
338sub hide { 343sub hide {
339 my ($self) = @_; 344 my ($self) = @_;
340 345
341 $self->set_invisible; 346 $self->set_invisible;
342 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
343 $self->{parent}->remove ($self) 351 $parent->remove ($self);
344 if $self->{parent}; 352 }
345} 353}
346 354
347sub move_abs { 355sub move_abs {
348 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
349 357
365 373
366# traverse the widget chain up to find the maximum "physical" size constraints 374# traverse the widget chain up to find the maximum "physical" size constraints
367sub get_max_wh { 375sub get_max_wh {
368 my ($self) = @_; 376 my ($self) = @_;
369 377
378 my ($w, $h) = @$self{qw(max_w max_h)};
379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
370 return $self->{parent}->get_max_wh 382 ? $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT) 383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
374} 390}
375 391
376sub size_request { 392sub size_request {
377 require Carp; 393 require Carp;
378 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
386 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
387 403
388 if ($self->{aspect}) { 404 if ($self->{aspect}) {
389 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
390 406
391 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
392 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
393 409
394 # use alignment to adjust x, y 410 # use alignment to adjust x, y
395 411
396 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
397 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
438 454
439 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
440 456
441 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
442 458
443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
444 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
445 $CFPlus::UI::TOOLTIP_WATCHER->invoke; 461 $DC::UI::TOOLTIP_WATCHER->invoke;
446 } 462 }
447} 463}
448 464
449# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
450sub coord2local { 466sub coord2local {
451 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
452 468
453 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
454 470
455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
456} 472}
457 473
458# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
459sub coord2global { 475sub coord2global {
460 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
461 477
462 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
463 479
464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
465} 481}
466 482
467sub invoke_focus_in { 483sub invoke_focus_in {
516sub connect { 532sub connect {
517 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
518 534
519 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
520 536
521 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
522 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
523 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
524 } 540 }
525} 541}
526 542
564# in .xs 580# in .xs
565 581
566sub set_parent { 582sub set_parent {
567 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
568 584
569 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
570 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
571} 587}
572 588
573sub realloc { 589sub realloc {
574 my ($self) = @_; 590 my ($self) = @_;
612} 628}
613 629
614sub DESTROY { 630sub DESTROY {
615 my ($self) = @_; 631 my ($self) = @_;
616 632
617 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
618 634
619 local $@; 635 local $@;
620 eval { $self->destroy }; 636 eval { $self->destroy };
621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
622 638
623 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
624} 640}
625 641
626############################################################################# 642#############################################################################
627 643
628package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
629 645
630our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
631 647
632use strict; 648use strict;
633use CFPlus::OpenGL; 649use DC::OpenGL;
634 650
635sub new { 651sub new {
636 my $class = shift; 652 my $class = shift;
637 653
638 $class->SUPER::new ( 654 $class->SUPER::new (
640 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
641 @_ 657 @_
642 ) 658 )
643} 659}
644 660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
645sub _draw { 668sub _draw {
646 my ($self) = @_; 669 my ($self) = @_;
647 670
648 my $color = $FOCUS == $self && $self->{active_bg} 671 my $color = $FOCUS == $self && $self->{active_bg}
649 ? $self->{active_bg} 672 ? $self->{active_bg}
660 } 683 }
661} 684}
662 685
663############################################################################# 686#############################################################################
664 687
665package CFPlus::UI::Empty; 688package DC::UI::Empty;
666 689
667our @ISA = CFPlus::UI::Base::; 690our @ISA = DC::UI::Base::;
668 691
669sub new { 692sub new {
670 my ($class, %arg) = @_; 693 my ($class, %arg) = @_;
671 $class->SUPER::new (can_events => 0, %arg); 694 $class->SUPER::new (can_events => 0, %arg);
672} 695}
679 702
680sub draw { } 703sub draw { }
681 704
682############################################################################# 705#############################################################################
683 706
684package CFPlus::UI::Container; 707package DC::UI::Container;
685 708
686our @ISA = CFPlus::UI::Base::; 709our @ISA = DC::UI::Base::;
687 710
688sub new { 711sub new {
689 my ($class, %arg) = @_; 712 my ($class, %arg) = @_;
690 713
691 my $children = delete $arg{children}; 714 my $children = delete $arg{children};
785 $_->draw for $self->visible_children; 808 $_->draw for $self->visible_children;
786} 809}
787 810
788############################################################################# 811#############################################################################
789 812
790package CFPlus::UI::Bin; 813package DC::UI::Bin;
791 814
792our @ISA = CFPlus::UI::Container::; 815our @ISA = DC::UI::Container::;
793 816
794sub new { 817sub new {
795 my ($class, %arg) = @_; 818 my ($class, %arg) = @_;
796 819
797 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 820 my $child = (delete $arg{child}) || new DC::UI::Empty::;
798 821
799 $class->SUPER::new (children => [$child], %arg) 822 $class->SUPER::new (children => [$child], %arg)
800} 823}
801 824
802sub add { 825sub add {
809sub remove { 832sub remove {
810 my ($self, $widget) = @_; 833 my ($self, $widget) = @_;
811 834
812 $self->SUPER::remove ($widget); 835 $self->SUPER::remove ($widget);
813 836
814 $self->{children} = [new CFPlus::UI::Empty] 837 $self->{children} = [new DC::UI::Empty]
815 unless @{$self->{children}}; 838 unless @{$self->{children}};
816} 839}
817 840
818sub child { $_[0]->{children}[0] } 841sub child { $_[0]->{children}[0] }
819 842
830} 853}
831 854
832############################################################################# 855#############################################################################
833# back-buffered drawing area 856# back-buffered drawing area
834 857
835package CFPlus::UI::Window; 858package DC::UI::Window;
836 859
837our @ISA = CFPlus::UI::Bin::; 860our @ISA = DC::UI::Bin::;
838 861
839use CFPlus::OpenGL; 862use DC::OpenGL;
840 863
841sub new { 864sub new {
842 my ($class, %arg) = @_; 865 my ($class, %arg) = @_;
843 866
844 my $self = $class->SUPER::new (%arg); 867 my $self = $class->SUPER::new (%arg);
866} 889}
867 890
868sub render_child { 891sub render_child {
869 my ($self) = @_; 892 my ($self) = @_;
870 893
871 $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 {
872 glClearColor 0, 0, 0, 0; 895 glClearColor 0, 0, 0, 0;
873 glClear GL_COLOR_BUFFER_BIT; 896 glClear GL_COLOR_BUFFER_BIT;
874 897
875 { 898 {
876 package CFPlus::UI::Base; 899 package DC::UI::Base;
877 900
878 local ($draw_x, $draw_y, $draw_w, $draw_h) = 901 local ($draw_x, $draw_y, $draw_w, $draw_h) =
879 (0, 0, $self->{w}, $self->{h}); 902 (0, 0, $self->{w}, $self->{h});
880 903
881 $self->_render; 904 $self->_render;
898 glDisable GL_TEXTURE_2D; 921 glDisable GL_TEXTURE_2D;
899} 922}
900 923
901############################################################################# 924#############################################################################
902 925
903package CFPlus::UI::ViewPort; 926package DC::UI::ViewPort;
904 927
905use List::Util qw(min max); 928use List::Util qw(min max);
906 929
907our @ISA = CFPlus::UI::Window::; 930our @ISA = DC::UI::Window::;
908 931
909sub new { 932sub new {
910 my $class = shift; 933 my $class = shift;
911 934
912 $class->SUPER::new ( 935 $class->SUPER::new (
996 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1019 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
997 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1020 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
998 ) { 1021 ) {
999 $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})
1000 } else { 1023 } else {
1001 $self->CFPlus::UI::Base::find_widget ($x, $y) 1024 $self->DC::UI::Base::find_widget ($x, $y)
1002 } 1025 }
1003} 1026}
1004 1027
1005sub _render { 1028sub _render {
1006 my ($self) = @_; 1029 my ($self) = @_;
1007 1030
1008 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};
1009 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};
1010 1033
1011 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1034 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1012 1035
1013 $self->SUPER::_render; 1036 $self->SUPER::_render;
1014} 1037}
1015 1038
1016############################################################################# 1039#############################################################################
1017 1040
1018package CFPlus::UI::ScrolledWindow; 1041package DC::UI::ScrolledWindow;
1019 1042
1020our @ISA = CFPlus::UI::Table::; 1043our @ISA = DC::UI::Table::;
1021 1044
1022sub new { 1045sub new {
1023 my ($class, %arg) = @_; 1046 my ($class, %arg) = @_;
1024 1047
1025 my $child = delete $arg{child}; 1048 my $child = delete $arg{child};
1026 1049
1027 my $self; 1050 my $self;
1028 1051
1029 my $hslider = new CFPlus::UI::Slider 1052 my $hslider = new DC::UI::Slider
1030 c_col => 0, 1053 c_col => 0,
1031 c_row => 1, 1054 c_row => 1,
1032 vertical => 0, 1055 vertical => 0,
1033 range => [0, 0, 1, 0.01], # HACK fix 1056 range => [0, 0, 1, 0.01], # HACK fix
1034 on_changed => sub { 1057 on_changed => sub {
1035 $self->{hpos} = $_[1]; 1058 $self->{hpos} = $_[1];
1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1059 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1037 }, 1060 },
1038 ; 1061 ;
1039 1062
1040 my $vslider = new CFPlus::UI::Slider 1063 my $vslider = new DC::UI::Slider
1041 c_col => 1, 1064 c_col => 1,
1042 c_row => 0, 1065 c_row => 0,
1043 vertical => 1, 1066 vertical => 1,
1044 range => [0, 0, 1, 0.01], # HACK fix 1067 range => [0, 0, 1, 0.01], # HACK fix
1045 on_changed => sub { 1068 on_changed => sub {
1057 col_expand => [1, 0], 1080 col_expand => [1, 0],
1058 row_expand => [1, 0], 1081 row_expand => [1, 0],
1059 %arg, 1082 %arg,
1060 ); 1083 );
1061 1084
1062 $self->{vp} = new CFPlus::UI::ViewPort 1085 $self->{vp} = new DC::UI::ViewPort
1063 c_col => 0, 1086 c_col => 0,
1064 c_row => 0, 1087 c_row => 0,
1065 expand => 1, 1088 expand => 1,
1066 scroll_x => $self->{scroll_x}, 1089 scroll_x => $self->{scroll_x},
1067 scroll_y => $self->{scroll_y}, 1090 scroll_y => $self->{scroll_y},
1193 $self->SUPER::invoke_size_allocate ($w, $h) 1216 $self->SUPER::invoke_size_allocate ($w, $h)
1194} 1217}
1195 1218
1196############################################################################# 1219#############################################################################
1197 1220
1198package CFPlus::UI::Frame; 1221package DC::UI::Frame;
1199 1222
1200our @ISA = CFPlus::UI::Bin::; 1223our @ISA = DC::UI::Bin::;
1201 1224
1202use CFPlus::OpenGL; 1225use DC::OpenGL;
1203 1226
1204sub new { 1227sub new {
1205 my $class = shift; 1228 my $class = shift;
1206 1229
1207 $class->SUPER::new ( 1230 $class->SUPER::new (
1226 $self->SUPER::_draw; 1249 $self->SUPER::_draw;
1227} 1250}
1228 1251
1229############################################################################# 1252#############################################################################
1230 1253
1231package CFPlus::UI::FancyFrame; 1254package DC::UI::FancyFrame;
1232 1255
1233our @ISA = CFPlus::UI::Bin::; 1256our @ISA = DC::UI::Bin::;
1234 1257
1235use CFPlus::OpenGL; 1258use DC::OpenGL;
1236 1259
1237sub new { 1260sub new {
1238 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1239 1262
1240 if ((exists $arg{label}) && !ref $arg{label}) { 1263 if ((exists $arg{label}) && !ref $arg{label}) {
1241 $arg{label} = new CFPlus::UI::Label 1264 $arg{label} = new DC::UI::Label
1242 align => 1, 1265 align => 1,
1243 valign => 0, 1266 valign => 0.5,
1244 text => $arg{label}, 1267 text => $arg{label},
1245 fontsize => ($arg{border} || 0.8) * 0.75; 1268 fontsize => ($arg{border} || 0.8) * 0.75;
1246 } 1269 }
1247 1270
1248 my $self = $class->SUPER::new ( 1271 my $self = $class->SUPER::new (
1249 # label => "", 1272 # label => "",
1250 fg => [0.6, 0.3, 0.1], 1273 fg => undef,
1251 border => 0.8, 1274 border => 0.8,
1252 style => 'single', 1275 style => 'single',
1253 %arg, 1276 %arg,
1254 ); 1277 );
1255 1278
1258 1281
1259sub add { 1282sub add {
1260 my ($self, @widgets) = @_; 1283 my ($self, @widgets) = @_;
1261 1284
1262 $self->SUPER::add (@widgets); 1285 $self->SUPER::add (@widgets);
1263 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1286 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1264} 1287}
1265 1288
1266sub border { 1289sub border {
1267 int $_[0]{border} * $::FONTSIZE 1290 int $_[0]{border} * $::FONTSIZE
1268} 1291}
1308 my $border = $self->border; 1331 my $border = $self->border;
1309 my ($w, $h) = ($self->{w}, $self->{h}); 1332 my ($w, $h) = ($self->{w}, $self->{h});
1310 1333
1311 $child->draw; 1334 $child->draw;
1312 1335
1313 glColor @{$self->{fg}}; 1336 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1314 glBegin GL_LINE_STRIP; 1337 glBegin GL_LINE_STRIP;
1315 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1338 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1317 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;
1318 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;
1326 } 1349 }
1327} 1350}
1328 1351
1329############################################################################# 1352#############################################################################
1330 1353
1331package CFPlus::UI::Toplevel; 1354package DC::UI::Toplevel;
1332 1355
1333our @ISA = CFPlus::UI::Bin::; 1356our @ISA = DC::UI::Bin::;
1334 1357
1335use CFPlus::OpenGL; 1358use DC::OpenGL;
1336 1359
1337my $bg = 1360my $bg =
1338 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1361 new_from_resource DC::Texture "d1_bg.png",
1339 mipmap => 1, wrap => 1; 1362 mipmap => 1, wrap => 1;
1340 1363
1341my @border = 1364my @border =
1342 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1365 map { new_from_resource DC::Texture $_, mipmap => 1 }
1343 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);
1344 1367
1345my @icon = 1368my @icon =
1346 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1369 map { new_from_resource DC::Texture $_, mipmap => 1 }
1347 qw(x1_move.png x1_resize.png); 1370 qw(x1_move.png x1_resize.png);
1348 1371
1349sub new { 1372sub new {
1350 my ($class, %arg) = @_; 1373 my ($class, %arg) = @_;
1351 1374
1352 my $self = $class->SUPER::new ( 1375 my $self = $class->SUPER::new (
1353 bg => [1, 1, 1, 1], 1376 bg => [1, 1, 1, 1],
1354 border_bg => [1, 1, 1, 1], 1377 border_bg => [1, 1, 1, 1],
1355 border => 0.6, 1378 border => 0.8,
1356 can_events => 1, 1379 can_events => 1,
1357 min_w => 64, 1380 min_w => 64,
1358 min_h => 32, 1381 min_h => 32,
1359 %arg, 1382 %arg,
1360 ); 1383 );
1361 1384
1362 $self->{title_widget} = new CFPlus::UI::Label 1385 $self->{title_widget} = new DC::UI::Label
1363 align => 0, 1386 align => 0.5,
1364 valign => 1, 1387 valign => 1,
1365 text => $self->{title}, 1388 text => $self->{title},
1366 fontsize => $self->{border}, 1389 fontsize => $self->{border},
1367 if exists $self->{title}; 1390 if exists $self->{title};
1368 1391
1369 if ($self->{has_close_button}) { 1392 if ($self->{has_close_button}) {
1370 $self->{close_button} = 1393 $self->{close_button} =
1371 new CFPlus::UI::ImageButton 1394 new DC::UI::ImageButton
1372 path => 'x1_close.png', 1395 path => 'x1_close.png',
1373 on_activate => sub { $self->emit ("delete") }; 1396 on_activate => sub { $self->emit ("delete") };
1374 1397
1375 $self->CFPlus::UI::Container::add ($self->{close_button}); 1398 $self->DC::UI::Container::add ($self->{close_button});
1376 } 1399 }
1377 1400
1378 $self 1401 $self
1379} 1402}
1380 1403
1381sub add { 1404sub add {
1382 my ($self, @widgets) = @_; 1405 my ($self, @widgets) = @_;
1383 1406
1384 $self->SUPER::add (@widgets); 1407 $self->SUPER::add (@widgets);
1385 $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};
1386 $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};
1387} 1410}
1388 1411
1389sub border { 1412sub border {
1390 int $_[0]{border} * $::FONTSIZE 1413 int $_[0]{border} * $::FONTSIZE
1391} 1414}
1529 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1530 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1531 1554
1532 my $border = $self->border; 1555 my $border = $self->border;
1533 1556
1557 if ($border) {
1534 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1535 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1536 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1537 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1538 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1539 1563
1540 # move 1564 # move
1541 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1542 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1543 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1545 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1546 1570
1547 # resize 1571 # resize
1548 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1550 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1551 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1553 1578
1554 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1555 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1556 1581
1557 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
1576 if $self->{close_button}; 1601 if $self->{close_button};
1577} 1602}
1578 1603
1579############################################################################# 1604#############################################################################
1580 1605
1581package CFPlus::UI::Table; 1606package DC::UI::Table;
1582 1607
1583our @ISA = CFPlus::UI::Container::; 1608our @ISA = DC::UI::Container::;
1584 1609
1585use List::Util qw(max sum); 1610use List::Util qw(max sum);
1586 1611
1587use CFPlus::OpenGL; 1612use DC::OpenGL;
1588 1613
1589sub new { 1614sub new {
1590 my $class = shift; 1615 my $class = shift;
1591 1616
1592 $class->SUPER::new ( 1617 $class->SUPER::new (
1636 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)};
1637 1662
1638 my $sw = sum @w[$c .. $c + $cs - 1]; 1663 my $sw = sum @w[$c .. $c + $cs - 1];
1639 1664
1640 if ($w > $sw) { 1665 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1666 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1642 } 1667 }
1643 } 1668 }
1644 1669
1645 # second pass, rows 1670 # second pass, rows
1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1671 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1647 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)};
1648 1673
1649 my $sh = sum @h[$r .. $r + $rs - 1]; 1674 my $sh = sum @h[$r .. $r + $rs - 1];
1650 1675
1651 if ($h > $sh) { 1676 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1677 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1653 } 1678 }
1654 } 1679 }
1655 1680
1656 (\@w, \@h) 1681 (\@w, \@h)
1657} 1682}
1680 @col_expand = (1) x @$ws unless @col_expand; 1705 @col_expand = (1) x @$ws unless @col_expand;
1681 my $col_expand = (sum @col_expand) || 1; 1706 my $col_expand = (sum @col_expand) || 1;
1682 1707
1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1708 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1684 1709
1685 CFPlus::UI::harmonize $ws; 1710 DC::UI::harmonize $ws;
1686 1711
1687 my @row_expand = @{$self->{row_expand}}; 1712 my @row_expand = @{$self->{row_expand}};
1688 @row_expand = (1) x @$ws unless @row_expand; 1713 @row_expand = (1) x @$ws unless @row_expand;
1689 my $row_expand = (sum @row_expand) || 1; 1714 my $row_expand = (sum @row_expand) || 1;
1690 1715
1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1716 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1692 1717
1693 CFPlus::UI::harmonize $hs; 1718 DC::UI::harmonize $hs;
1694 1719
1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1720 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1721 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1697 1722
1698 for my $widget ($self->children) { 1723 for my $widget ($self->children) {
1707 1 1732 1
1708} 1733}
1709 1734
1710############################################################################# 1735#############################################################################
1711 1736
1712package CFPlus::UI::Fixed; 1737package DC::UI::Fixed;
1713 1738
1714use List::Util qw(min max); 1739use List::Util qw(min max);
1715 1740
1716our @ISA = CFPlus::UI::Container::; 1741our @ISA = DC::UI::Container::;
1717 1742
1718sub _scale($$$) { 1743sub _scale($$$) {
1719 my ($rel, $val, $max) = @_; 1744 my ($rel, $val, $max) = @_;
1720 1745
1721 $rel ? $val * $max : $val 1746 $rel ? $val * $max : $val
1773 1 1798 1
1774} 1799}
1775 1800
1776############################################################################# 1801#############################################################################
1777 1802
1778package CFPlus::UI::Box; 1803package DC::UI::Box;
1779 1804
1780our @ISA = CFPlus::UI::Container::; 1805our @ISA = DC::UI::Container::;
1781 1806
1782sub size_request { 1807sub size_request {
1783 my ($self) = @_; 1808 my ($self) = @_;
1784 1809
1785 my @children = $self->visible_children; 1810 my @children = $self->visible_children;
1820 $req[$_] += $space * $children[$_]{expand} 1845 $req[$_] += $space * $children[$_]{expand}
1821 for 0 .. $#children; 1846 for 0 .. $#children;
1822 } 1847 }
1823 } 1848 }
1824 1849
1825 CFPlus::UI::harmonize \@req; 1850 DC::UI::harmonize \@req;
1826 1851
1827 my $pos = 0; 1852 my $pos = 0;
1828 for (0 .. $#children) { 1853 for (0 .. $#children) {
1829 my $alloc = $req[$_]; 1854 my $alloc = $req[$_];
1830 $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));
1835 1 1860 1
1836} 1861}
1837 1862
1838############################################################################# 1863#############################################################################
1839 1864
1840package CFPlus::UI::HBox; 1865package DC::UI::HBox;
1841 1866
1842our @ISA = CFPlus::UI::Box::; 1867our @ISA = DC::UI::Box::;
1843 1868
1844sub new { 1869sub new {
1845 my $class = shift; 1870 my $class = shift;
1846 1871
1847 $class->SUPER::new ( 1872 $class->SUPER::new (
1850 ) 1875 )
1851} 1876}
1852 1877
1853############################################################################# 1878#############################################################################
1854 1879
1855package CFPlus::UI::VBox; 1880package DC::UI::VBox;
1856 1881
1857our @ISA = CFPlus::UI::Box::; 1882our @ISA = DC::UI::Box::;
1858 1883
1859sub new { 1884sub new {
1860 my $class = shift; 1885 my $class = shift;
1861 1886
1862 $class->SUPER::new ( 1887 $class->SUPER::new (
1865 ) 1890 )
1866} 1891}
1867 1892
1868############################################################################# 1893#############################################################################
1869 1894
1870package CFPlus::UI::Label; 1895package DC::UI::Label;
1871 1896
1872our @ISA = CFPlus::UI::DrawBG::; 1897our @ISA = DC::UI::DrawBG::;
1873 1898
1874use CFPlus::OpenGL; 1899use DC::OpenGL;
1875 1900
1876sub new { 1901sub new {
1877 my ($class, %arg) = @_; 1902 my ($class, %arg) = @_;
1878 1903
1879 my $self = $class->SUPER::new ( 1904 my $self = $class->SUPER::new (
1884 #text => initial text 1909 #text => initial text
1885 #markup => initial narkup 1910 #markup => initial narkup
1886 #max_w => maximum pixel width 1911 #max_w => maximum pixel width
1887 #style => 0, # render flags 1912 #style => 0, # render flags
1888 ellipsise => 3, # end 1913 ellipsise => 3, # end
1889 layout => (new CFPlus::Layout), 1914 layout => (new DC::Layout),
1890 fontsize => 1, 1915 fontsize => 1,
1891 align => -1, 1916 align => 0.5,
1892 valign => -1, 1917 valign => 0.5,
1893 padding_x => 2, 1918 padding_x => 4,
1894 padding_y => 2, 1919 padding_y => 2,
1895 can_events => 0, 1920 can_events => 0,
1896 %arg 1921 %arg
1897 ); 1922 );
1898 1923
1899 if (exists $self->{template}) { 1924 if (exists $self->{template}) {
1900 my $layout = new CFPlus::Layout; 1925 my $layout = new DC::Layout;
1901 $layout->set_text (delete $self->{template}); 1926 $layout->set_text (delete $self->{template});
1902 $self->{template} = $layout; 1927 $self->{template} = $layout;
1903 } 1928 }
1904 1929
1905 if (exists $self->{markup}) { 1930 if (exists $self->{markup}) {
1964 1989
1965 $self->{size_req} ||= do { 1990 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh; 1991 my ($max_w, $max_h) = $self->get_max_wh;
1967 1992
1968 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1993 $self->{layout}->set_font ($self->{font}) if $self->{font};
1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1994 $self->{layout}->set_width ($max_w);
1970 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1995 $self->{layout}->set_ellipsise ($self->{ellipsise});
1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1996 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1973 1998
1974 my ($w, $h) = $self->{layout}->size; 1999 my ($w, $h) = $self->{layout}->size;
1975 2000
1976 if (exists $self->{template}) { 2001 if (exists $self->{template}) {
1977 $self->{template}->set_font ($self->{font}) if $self->{font}; 2002 $self->{template}->set_font ($self->{font}) if $self->{font};
1978 $self->{template}->set_width ($self->{max_w} || -1); 2003 $self->{template}->set_width ($max_w);
1979 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2004 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1980 2005
1981 my ($w2, $h2) = $self->{template}->size; 2006 my ($w2, $h2) = $self->{template}->size;
1982 2007
1983 $w = List::Util::max $w, $w2; 2008 $w = List::Util::max $w, $w2;
2039 2064
2040 [$self->{layout}->size] 2065 [$self->{layout}->size]
2041 }; 2066 };
2042 2067
2043 unless (exists $self->{ox}) { 2068 unless (exists $self->{ox}) {
2044 $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);
2045 : $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);
2046 : ($self->{w} - $size->[0]) * 0.5);
2047
2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2050 : ($self->{h} - $size->[1]) * 0.5);
2051 2071
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2053 }; 2073 };
2054 2074
2055# unless ($self->{list}) { 2075# unless ($self->{list}) {
2056# $self->{list} = CFPlus::OpenGL::glGenList; 2076# $self->{list} = DC::OpenGL::glGenList;
2057# CFPlus::OpenGL::glNewList $self->{list}; 2077# DC::OpenGL::glNewList $self->{list};
2058# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2078# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2059# CFPlus::OpenGL::glEndList; 2079# DC::OpenGL::glEndList;
2060# } 2080# }
2061# 2081#
2062# CFPlus::OpenGL::glCallList $self->{list}; 2082# DC::OpenGL::glCallList $self->{list};
2063 2083
2064 $self->{layout}->draw; 2084 $self->{layout}->draw;
2065} 2085}
2066 2086
2067#sub destroy { 2087#sub destroy {
2068# my ($self) = @_; 2088# my ($self) = @_;
2069# 2089#
2070# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2090# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2071# 2091#
2072# $self->SUPER::destroy; 2092# $self->SUPER::destroy;
2073#} 2093#}
2074 2094
2075############################################################################# 2095#############################################################################
2076 2096
2077package CFPlus::UI::EntryBase; 2097package DC::UI::EntryBase;
2078 2098
2079our @ISA = CFPlus::UI::Label::; 2099our @ISA = DC::UI::Label::;
2080 2100
2081use CFPlus::OpenGL; 2101use DC::OpenGL;
2082 2102
2083sub new { 2103sub new {
2084 my $class = shift; 2104 my $class = shift;
2085 2105
2086 $class->SUPER::new ( 2106 $class->SUPER::new (
2087 fg => [1, 1, 1], 2107 fg => [1, 1, 1],
2088 bg => [0, 0, 0, 0.2], 2108 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1], 2109 outline => undef,
2090 active_bg => [0, 0, 1, .2], 2110 active_bg => [0, 0, 1, .2],
2091 active_fg => [1, 1, 1], 2111 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0], 2112 active_outline => [1, 1, 0],
2093 can_hover => 1, 2113 can_hover => 1,
2094 can_focus => 1, 2114 can_focus => 1,
2115 align => 0,
2095 valign => 0, 2116 valign => 0.5,
2096 can_events => 1, 2117 can_events => 1,
2097 ellipsise => 0, 2118 ellipsise => 0,
2119 padding_x => 4,
2120 padding_y => 2,
2098 #text => ... 2121 #text => ...
2099 #hidden => "*", 2122 #hidden => "*",
2100 @_ 2123 @_
2101 ) 2124 )
2102} 2125}
2149 2172
2150 my $text = $self->get_text; 2173 my $text = $self->get_text;
2151 2174
2152 $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;
2153 2176
2154 if ($uni == 8) { 2177 if ($sym == DC::SDLK_BACKSPACE) {
2155 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2178 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2156 } elsif ($uni == 127) { 2179 } elsif ($sym == DC::SDLK_DELETE) {
2157 substr $text, $self->{cursor}, 1, ""; 2180 substr $text, $self->{cursor}, 1, "";
2158 } elsif ($sym == CFPlus::SDLK_LEFT) { 2181 } elsif ($sym == DC::SDLK_LEFT) {
2159 --$self->{cursor} if $self->{cursor}; 2182 --$self->{cursor} if $self->{cursor};
2160 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2183 } elsif ($sym == DC::SDLK_RIGHT) {
2161 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2184 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2162 } elsif ($sym == CFPlus::SDLK_HOME) { 2185 } elsif ($sym == DC::SDLK_HOME) {
2163 # what a hack 2186 # what a hack
2164 $self->{cursor} = 2187 $self->{cursor} =
2165 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2188 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2166 ? length $1 2189 ? length $1
2167 : 0; 2190 : 0;
2168 } elsif ($sym == CFPlus::SDLK_END) { 2191 } elsif ($sym == DC::SDLK_END) {
2169 # uh, again 2192 # uh, again
2170 $self->{cursor} = 2193 $self->{cursor} =
2171 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2194 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2172 ? $self->{cursor} + length $1 2195 ? $self->{cursor} + length $1
2173 : length $self->{text}; 2196 : length $self->{text};
2264 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2288 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2266 glLineWidth 1; 2289 glLineWidth 1;
2267 2290
2268 } else { 2291 } else {
2269 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2270 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2271 glVertex .5, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2272 glVertex .5, $self->{h} - 2.5; 2295 glVertex .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} - 2.5; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - .5, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
2276 } 2299 }
2277} 2300}
2278 2301
2279############################################################################# 2302#############################################################################
2280 2303
2281package CFPlus::UI::Entry; 2304package DC::UI::Entry;
2282 2305
2283our @ISA = CFPlus::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2284 2307
2285use 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
2286 2319
2287sub invoke_key_down { 2320sub invoke_key_down {
2288 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2289 2322
2290 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2296 $self->{history_pointer} = -1; 2329 $self->{history_pointer} = -1;
2297 $self->{history_saveback} = ''; 2330 $self->{history_saveback} = '';
2298 $self->emit (activate => $txt); 2331 $self->emit (activate => $txt);
2299 $self->update; 2332 $self->update;
2300 2333
2301 } elsif ($sym == CFPlus::SDLK_UP) { 2334 } elsif ($sym == DC::SDLK_UP) {
2302 if ($self->{history_pointer} < 0) { 2335 if ($self->{history_pointer} < 0) {
2303 $self->{history_saveback} = $self->get_text; 2336 $self->{history_saveback} = $self->get_text;
2304 } 2337 }
2305 if (@{$self->{history} || []} > 0) { 2338 if (@{$self->{history} || []} > 0) {
2306 $self->{history_pointer}++; 2339 $self->{history_pointer}++;
2308 $self->{history_pointer} = @{$self->{history} || []} - 1; 2341 $self->{history_pointer} = @{$self->{history} || []} - 1;
2309 } 2342 }
2310 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2343 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2311 } 2344 }
2312 2345
2313 } elsif ($sym == CFPlus::SDLK_DOWN) { 2346 } elsif ($sym == DC::SDLK_DOWN) {
2314 $self->{history_pointer}--; 2347 $self->{history_pointer}--;
2315 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2316 2349
2317 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2318 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2319 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2320 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2321 } 2357 }
2322 2358
2323 } else { 2359 } else {
2324 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2325 } 2361 }
2327 1 2363 1
2328} 2364}
2329 2365
2330############################################################################# 2366#############################################################################
2331 2367
2332package CFPlus::UI::TextEdit; 2368package DC::UI::TextEdit;
2333 2369
2334our @ISA = CFPlus::UI::EntryBase::; 2370our @ISA = DC::UI::EntryBase::;
2335 2371
2336use 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}
2337 2383
2338sub move_cursor_ver { 2384sub move_cursor_ver {
2339 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2340 2386
2341 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2342 2388
2343 $y += $dy; 2389 $line += $dy;
2344 2390
2345 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))) {
2346 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2347 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2348 $self->update; 2394 $self->update;
2349 return; 2395 return;
2350 } 2396 }
2353sub invoke_key_down { 2399sub invoke_key_down {
2354 my ($self, $ev) = @_; 2400 my ($self, $ev) = @_;
2355 2401
2356 my $sym = $ev->{sym}; 2402 my $sym = $ev->{sym};
2357 2403
2358 if ($sym == CFPlus::SDLK_UP) { 2404 if ($sym == DC::SDLK_UP) {
2359 $self->move_cursor_ver (-1); 2405 $self->move_cursor_ver (-1);
2360 } elsif ($sym == CFPlus::SDLK_DOWN) { 2406 } elsif ($sym == DC::SDLK_DOWN) {
2361 $self->move_cursor_ver (+1); 2407 $self->move_cursor_ver (+1);
2362 } else { 2408 } else {
2363 return $self->SUPER::invoke_key_down ($ev) 2409 return $self->SUPER::invoke_key_down ($ev)
2364 } 2410 }
2365 2411
2366 1 2412 1
2367} 2413}
2368 2414
2369############################################################################# 2415#############################################################################
2370 2416
2371package CFPlus::UI::ButtonBin; 2417package DC::UI::ButtonBin;
2372 2418
2373our @ISA = CFPlus::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2374 2420
2375use CFPlus::OpenGL; 2421use DC::OpenGL;
2376 2422
2377my @tex = 2423my @tex =
2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2379 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2380 2426
2381sub new { 2427sub new {
2382 my $class = shift; 2428 my $class = shift;
2383 2429
2384 $class->SUPER::new ( 2430 $class->SUPER::new (
2385 can_hover => 1, 2431 can_hover => 1,
2386 align => 0, 2432 align => 0.5,
2387 valign => 0, 2433 valign => 0.5,
2388 can_events => 1, 2434 can_events => 1,
2389 @_ 2435 @_
2390 ) 2436 )
2391} 2437}
2392 2438
2415 $self->SUPER::_draw; 2461 $self->SUPER::_draw;
2416} 2462}
2417 2463
2418############################################################################# 2464#############################################################################
2419 2465
2420package CFPlus::UI::Button; 2466package DC::UI::Button;
2421 2467
2422our @ISA = CFPlus::UI::Label::; 2468our @ISA = DC::UI::Label::;
2423 2469
2424use CFPlus::OpenGL; 2470use DC::OpenGL;
2425 2471
2426my @tex = 2472my @tex =
2427 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2428 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2429 2475
2430sub new { 2476sub new {
2431 my $class = shift; 2477 my $class = shift;
2432 2478
2433 $class->SUPER::new ( 2479 $class->SUPER::new (
2434 padding_x => 4, 2480 padding_x => 8,
2435 padding_y => 4, 2481 padding_y => 4,
2436 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2437 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2438 can_hover => 1, 2484 can_hover => 1,
2439 align => 0, 2485 align => 0.5,
2440 valign => 0, 2486 valign => 0.5,
2441 can_events => 1, 2487 can_events => 1,
2442 @_ 2488 @_
2443 ) 2489 )
2444} 2490}
2445 2491
2470 $self->SUPER::_draw; 2516 $self->SUPER::_draw;
2471} 2517}
2472 2518
2473############################################################################# 2519#############################################################################
2474 2520
2475package CFPlus::UI::CheckBox; 2521package DC::UI::CheckBox;
2476 2522
2477our @ISA = CFPlus::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2478 2524
2479my @tex = 2525my @tex =
2480 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2481 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2482 2528
2483use CFPlus::OpenGL; 2529use DC::OpenGL;
2484 2530
2485sub new { 2531sub new {
2486 my $class = shift; 2532 my $class = shift;
2487 2533
2488 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2489 padding_x => 2, 2536 padding_x => 2,
2490 padding_y => 2, 2537 padding_y => 2,
2491 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2492 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2493 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2499} 2546}
2500 2547
2501sub size_request { 2548sub size_request {
2502 my ($self) = @_; 2549 my ($self) = @_;
2503 2550
2504 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2505} 2552}
2506 2553
2507sub toggle { 2554sub toggle {
2508 my ($self) = @_; 2555 my ($self) = @_;
2509 2556
2545 glDisable GL_TEXTURE_2D; 2592 glDisable GL_TEXTURE_2D;
2546} 2593}
2547 2594
2548############################################################################# 2595#############################################################################
2549 2596
2550package CFPlus::UI::Image; 2597package DC::UI::Image;
2551 2598
2552our @ISA = CFPlus::UI::Base::; 2599our @ISA = DC::UI::Base::;
2553 2600
2554use CFPlus::OpenGL; 2601use DC::OpenGL;
2555 2602
2556our %texture_cache; 2603our %texture_cache;
2557 2604
2558sub new { 2605sub new {
2559 my $class = shift; 2606 my $class = shift;
2566 2613
2567 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2568 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2569 2616
2570 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2571 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2572 2619
2573 CFPlus::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2574 2621
2575 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2576 2623
2577 $self 2624 $self
2578} 2625}
2579 2626
2580sub STORABLE_freeze { 2627sub STORABLE_freeze {
2581 my ($self, $cloning) = @_; 2628 my ($self, $cloning) = @_;
2582 2629
2583 $self->{path} 2630 $self->{path}
2584 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";
2585 2632
2586 $self->{path} 2633 $self->{path}
2587} 2634}
2588 2635
2589sub STORABLE_attach { 2636sub STORABLE_attach {
2620 glDisable GL_TEXTURE_2D; 2667 glDisable GL_TEXTURE_2D;
2621} 2668}
2622 2669
2623############################################################################# 2670#############################################################################
2624 2671
2625package CFPlus::UI::ImageButton; 2672package DC::UI::ImageButton;
2626 2673
2627our @ISA = CFPlus::UI::Image::; 2674our @ISA = DC::UI::Image::;
2628 2675
2629use CFPlus::OpenGL; 2676use DC::OpenGL;
2630 2677
2631my %textures; 2678my %textures;
2632 2679
2633sub new { 2680sub new {
2634 my $class = shift; 2681 my $class = shift;
2637 padding_x => 4, 2684 padding_x => 4,
2638 padding_y => 4, 2685 padding_y => 4,
2639 fg => [1, 1, 1], 2686 fg => [1, 1, 1],
2640 active_fg => [0, 0, 1], 2687 active_fg => [0, 0, 1],
2641 can_hover => 1, 2688 can_hover => 1,
2642 align => 0, 2689 align => 0.5,
2643 valign => 0, 2690 valign => 0.5,
2644 can_events => 1, 2691 can_events => 1,
2645 @_ 2692 @_
2646 ); 2693 );
2647} 2694}
2648 2695
2662 1 2709 1
2663} 2710}
2664 2711
2665############################################################################# 2712#############################################################################
2666 2713
2667package CFPlus::UI::VGauge; 2714package DC::UI::VGauge;
2668 2715
2669our @ISA = CFPlus::UI::Base::; 2716our @ISA = DC::UI::Base::;
2670 2717
2671use List::Util qw(min max); 2718use List::Util qw(min max);
2672 2719
2673use CFPlus::OpenGL; 2720use DC::OpenGL;
2674 2721
2675my %tex = ( 2722my %tex = (
2676 food => [ 2723 food => [
2677 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2678 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2725 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2679 ], 2726 ],
2680 grace => [ 2727 grace => [
2681 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2728 map { new_from_resource DC::Texture $_, mipmap => 1 }
2682 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2729 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2683 ], 2730 ],
2684 hp => [ 2731 hp => [
2685 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2686 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2733 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2687 ], 2734 ],
2688 mana => [ 2735 mana => [
2689 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2736 map { new_from_resource DC::Texture $_, mipmap => 1 }
2690 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2737 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2691 ], 2738 ],
2692); 2739);
2693 2740
2694# eg. VGauge->new (gauge => 'food'), default gauge: food 2741# eg. VGauge->new (gauge => 'food'), default gauge: food
2797 glDisable GL_TEXTURE_2D; 2844 glDisable GL_TEXTURE_2D;
2798} 2845}
2799 2846
2800############################################################################# 2847#############################################################################
2801 2848
2802package CFPlus::UI::Progress; 2849package DC::UI::Progress;
2803 2850
2804our @ISA = CFPlus::UI::Label::; 2851our @ISA = DC::UI::Label::;
2805 2852
2806use CFPlus::OpenGL; 2853use DC::OpenGL;
2807 2854
2808sub new { 2855sub new {
2809 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2810 2857
2811 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2812 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2813 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2814 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2815 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2816 fontsize => 0.9, 2865 fontsize => 0.9,
2817 valign => 0, 2866 valign => 0.5,
2818 align => 0, 2867 align => 0.5,
2819 can_events => 1, 2868 can_events => 1,
2820 ellipsise => 1, 2869 ellipsise => 1,
2821 label => "%d%%", 2870 label => "%d%%",
2822 %arg, 2871 %arg,
2823 ); 2872 );
2831 my ($self, $label) = @_; 2880 my ($self, $label) = @_;
2832 2881
2833 return if $self->{label} eq $label; 2882 return if $self->{label} eq $label;
2834 $self->{label} = $label; 2883 $self->{label} = $label;
2835 2884
2836 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2885 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2837} 2886}
2838 2887
2839sub set_value { 2888sub set_value {
2840 my ($self, $value) = @_; 2889 my ($self, $value) = @_;
2841 2890
2856 my ($self) = @_; 2905 my ($self) = @_;
2857 2906
2858 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2860 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2861 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2862 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2863 2915
2864 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2865 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2866 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2868 } 2920 }
2869 2921
2870 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2872 2928
2873 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2874 2930
2875 { 2931 {
2876 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2878 } 2934 }
2879} 2935}
2880 2936
2881############################################################################# 2937#############################################################################
2882 2938
2883package CFPlus::UI::ExperienceProgress; 2939package DC::UI::ExperienceProgress;
2884 2940
2885our @ISA = CFPlus::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2886 2942
2887sub new { 2943sub new {
2888 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2889 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2890 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2891 tooltip => sub { 2950 tooltip => sub {
2892 my ($self) = @_; 2951 my ($self) = @_;
2893 2952
2894 sprintf "level %d\n%s points\n%s next level\n%s to go", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2954 $tt,
2895 $self->{lvl}, 2955 $self->{lvl},
2896 ::formsep ($self->{exp}), 2956 ::formsep ($self->{exp}),
2897 ::formsep ($self->{nxt}), 2957 ::formsep ($self->{nxt}),
2898 ::formsep ($self->{nxt} - $self->{exp}), 2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2899 }, 2960 },
2900 %arg
2901 ); 2961 );
2902 2962
2903 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2904 if $::CONN; 2964 if $::CONN;
2905 2965
2913 if $::CONN; 2973 if $::CONN;
2914 2974
2915 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2916} 2976}
2917 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2918sub set_value { 2992sub set_value {
2919 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2920 2994
2921 $self->{lvl} = $lvl; 2995 $self->{lvl} = $lvl;
2922 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2923 2997
2924 my $v = -1;
2925
2926 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2927 my $l0 = $table->[$lvl - 1];
2928 my $l1 = $table->[$lvl];
2929
2930 $self->{nxt} = $l1;
2931
2932 $v = ($exp - $l0) / ($l1 - $l0);
2933 }
2934
2935 $self->SUPER::set_value ($v); 2998 $self->SUPER::set_value ($self->_percent);
2936} 2999}
2937 3000
2938############################################################################# 3001#############################################################################
2939 3002
2940package CFPlus::UI::Gauge; 3003package DC::UI::Gauge;
2941 3004
2942our @ISA = CFPlus::UI::VBox::; 3005our @ISA = DC::UI::VBox::;
2943 3006
2944sub new { 3007sub new {
2945 my ($class, %arg) = @_; 3008 my ($class, %arg) = @_;
2946 3009
2947 my $self = $class->SUPER::new ( 3010 my $self = $class->SUPER::new (
2949 can_hover => 1, 3012 can_hover => 1,
2950 can_events => 1, 3013 can_events => 1,
2951 %arg, 3014 %arg,
2952 ); 3015 );
2953 3016
2954 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3017 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2955 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3018 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2956 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3019 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2957 3020
2958 $self 3021 $self
2959} 3022}
2960 3023
2961sub set_fontsize { 3024sub set_fontsize {
2982 $self->{value}->set_text ($val); 3045 $self->{value}->set_text ($val);
2983} 3046}
2984 3047
2985############################################################################# 3048#############################################################################
2986 3049
2987package CFPlus::UI::Slider; 3050package DC::UI::Slider;
2988 3051
2989use strict; 3052use strict;
2990 3053
2991use CFPlus::OpenGL; 3054use DC::OpenGL;
2992 3055
2993our @ISA = CFPlus::UI::DrawBG::; 3056our @ISA = DC::UI::DrawBG::;
2994 3057
2995my @tex = 3058my @tex =
2996 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3059 map { new_from_resource DC::Texture $_ }
2997 qw(s1_slider.png s1_slider_bg.png); 3060 qw(s1_slider.png s1_slider_bg.png);
2998 3061
2999sub new { 3062sub new {
3000 my $class = shift; 3063 my $class = shift;
3001 3064
3038sub set_value { 3101sub set_value {
3039 my ($self, $value) = @_; 3102 my ($self, $value) = @_;
3040 3103
3041 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3104 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3042 3105
3043 $hi = $lo + 1 if $hi <= $lo; 3106 $hi = $lo if $hi < $lo;
3044 3107
3045 $page = $hi - $lo if $page > $hi - $lo; 3108 $value = $hi - $page if $value > $hi - $page;
3046
3047 $value = $lo if $value < $lo; 3109 $value = $lo if $value < $lo;
3048 $value = $hi - $page if $value > $hi - $page;
3049 3110
3050 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3111 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3051 if $unit; 3112 if $unit;
3052 3113
3053 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3114 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3097sub invoke_mouse_wheel { 3158sub invoke_mouse_wheel {
3098 my ($self, $ev) = @_; 3159 my ($self, $ev) = @_;
3099 3160
3100 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3161 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3101 3162
3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3163 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3103 3164
3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3165 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3105 3166
3106 1 3167 1
3107} 3168}
3117 my ($self) = @_; 3178 my ($self) = @_;
3118 3179
3119 unless ($self->{knob_w}) { 3180 unless ($self->{knob_w}) {
3120 $self->set_value ($self->{range}[0]); 3181 $self->set_value ($self->{range}[0]);
3121 3182
3122 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3183 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3123 my $range = ($hi - $page - $lo) || 1e-100; 3184 my $range = ($hi - $page - $lo) || 1e-10;
3124 3185
3125 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3186 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3126 3187
3127 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3188 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3128 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3189 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3129 3190
3130 $value = ($value - $lo) / $range; 3191 $value = ($value - $lo) / $range;
3160 glDisable GL_TEXTURE_2D; 3221 glDisable GL_TEXTURE_2D;
3161} 3222}
3162 3223
3163############################################################################# 3224#############################################################################
3164 3225
3165package CFPlus::UI::ValSlider; 3226package DC::UI::ValSlider;
3166 3227
3167our @ISA = CFPlus::UI::HBox::; 3228our @ISA = DC::UI::HBox::;
3168 3229
3169sub new { 3230sub new {
3170 my ($class, %arg) = @_; 3231 my ($class, %arg) = @_;
3171 3232
3172 my $range = delete $arg{range}; 3233 my $range = delete $arg{range};
3173 3234
3174 my $self = $class->SUPER::new ( 3235 my $self = $class->SUPER::new (
3175 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3236 slider => (new DC::UI::Slider expand => 1, range => $range),
3176 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3237 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3177 to_value => sub { shift }, 3238 to_value => sub { shift },
3178 from_value => sub { shift }, 3239 from_value => sub { shift },
3179 %arg, 3240 %arg,
3180 ); 3241 );
3181 3242
3201sub set_range { shift->{slider}->set_range (@_) } 3262sub set_range { shift->{slider}->set_range (@_) }
3202sub set_value { shift->{slider}->set_value (@_) } 3263sub set_value { shift->{slider}->set_value (@_) }
3203 3264
3204############################################################################# 3265#############################################################################
3205 3266
3206package CFPlus::UI::TextScroller; 3267package DC::UI::TextScroller;
3207 3268
3208our @ISA = CFPlus::UI::HBox::; 3269our @ISA = DC::UI::HBox::;
3209 3270
3210use CFPlus::OpenGL; 3271use DC::OpenGL;
3211 3272
3212sub new { 3273sub new {
3213 my $class = shift; 3274 my $class = shift;
3214 3275
3215 my $self = $class->SUPER::new ( 3276 my $self = $class->SUPER::new (
3217 can_events => 1, 3278 can_events => 1,
3218 indent => 0, 3279 indent => 0,
3219 #font => default_font 3280 #font => default_font
3220 @_, 3281 @_,
3221 3282
3222 layout => (new CFPlus::Layout), 3283 layout => (new DC::Layout),
3223 par => [], 3284 par => [],
3224 max_par => 0, 3285 max_par => 0,
3225 height => 0, 3286 height => 0,
3226 children => [ 3287 children => [
3227 (new CFPlus::UI::Empty expand => 1), 3288 (new DC::UI::Empty expand => 1),
3228 (new CFPlus::UI::Slider vertical => 1), 3289 (new DC::UI::Slider vertical => 1),
3229 ], 3290 ],
3230 ); 3291 );
3231 3292
3232 $self->{children}[1]->connect (changed => sub { $self->update }); 3293 $self->{children}[1]->connect (changed => sub { $self->update });
3233 3294
3289 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3350 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3290 $layout->set_markup ($para->{markup}); 3351 $layout->set_markup ($para->{markup});
3291 3352
3292 $layout->set_shapes ( 3353 $layout->set_shapes (
3293 map 3354 map
3294 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3355 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3295 @{$para->{widget}} 3356 @{$para->{widget}}
3296 ); 3357 );
3297 3358
3298 $layout 3359 $layout
3299} 3360}
3416 $ROOT->on_post_alloc ($self => sub { 3477 $ROOT->on_post_alloc ($self => sub {
3417 $self->force_uptodate; 3478 $self->force_uptodate;
3418 3479
3419 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3480 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3420 3481
3421 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3482 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3422 glClearColor 0, 0, 0, 0; 3483 glClearColor 0, 0, 0, 0;
3423 glClear GL_COLOR_BUFFER_BIT; 3484 glClear GL_COLOR_BUFFER_BIT;
3424 3485
3425 package CFPlus::UI::Base; 3486 package DC::UI::Base;
3426 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3487 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3427 (0, 0, $self->{w}, $self->{h}); 3488 (0, 0, $self->{w}, $self->{h});
3428 3489
3429 my $top = int $self->{children}[1]{range}[0]; 3490 my $top = int $self->{children}[1]{range}[0];
3430 3491
3489 $self->{children}[1]->draw; 3550 $self->{children}[1]->draw;
3490} 3551}
3491 3552
3492############################################################################# 3553#############################################################################
3493 3554
3494package CFPlus::UI::Animator; 3555package DC::UI::Animator;
3495 3556
3496use CFPlus::OpenGL; 3557use DC::OpenGL;
3497 3558
3498our @ISA = CFPlus::UI::Bin::; 3559our @ISA = DC::UI::Bin::;
3499 3560
3500sub moveto { 3561sub moveto {
3501 my ($self, $x, $y) = @_; 3562 my ($self, $x, $y) = @_;
3502 3563
3503 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3564 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3531 glPopMatrix; 3592 glPopMatrix;
3532} 3593}
3533 3594
3534############################################################################# 3595#############################################################################
3535 3596
3536package CFPlus::UI::Flopper; 3597package DC::UI::Flopper;
3537 3598
3538our @ISA = CFPlus::UI::Button::; 3599our @ISA = DC::UI::Button::;
3539 3600
3540sub new { 3601sub new {
3541 my $class = shift; 3602 my $class = shift;
3542 3603
3543 my $self = $class->SUPER::new ( 3604 my $self = $class->SUPER::new (
3555 $self->{other}->toggle_visibility; 3616 $self->{other}->toggle_visibility;
3556} 3617}
3557 3618
3558############################################################################# 3619#############################################################################
3559 3620
3560package CFPlus::UI::Tooltip; 3621package DC::UI::Tooltip;
3561 3622
3562our @ISA = CFPlus::UI::Bin::; 3623our @ISA = DC::UI::Bin::;
3563 3624
3564use CFPlus::OpenGL; 3625use DC::OpenGL;
3565 3626
3566sub new { 3627sub new {
3567 my $class = shift; 3628 my $class = shift;
3568 3629
3569 $class->SUPER::new ( 3630 $class->SUPER::new (
3576 my ($self, $widget) = @_; 3637 my ($self, $widget) = @_;
3577 3638
3578 my $tip = $widget->{tooltip}; 3639 my $tip = $widget->{tooltip};
3579 $tip = $tip->($widget) if "CODE" eq ref $tip; 3640 $tip = $tip->($widget) if "CODE" eq ref $tip;
3580 3641
3581 $tip = CFPlus::Pod::section_label tooltip => $1 3642 $tip = DC::Pod::section_label tooltip => $1
3582 if $tip =~ /^#(.*)$/; 3643 if $tip =~ /^#(.*)$/;
3583 3644
3584 if ($ENV{CFPLUS_DEBUG} & 2) { 3645 if ($ENV{CFPLUS_DEBUG} & 2) {
3585 $tip .= "\n\n" . (ref $widget) . "\n" 3646 $tip .= "\n\n" . (ref $widget) . "\n"
3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3647 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3589 } 3650 }
3590 3651
3591 $tip =~ s/^\n+//; 3652 $tip =~ s/^\n+//;
3592 $tip =~ s/\n+$//; 3653 $tip =~ s/\n+$//;
3593 3654
3594 $self->add (new CFPlus::UI::Label 3655 $self->add (new DC::UI::Label
3656 fg => $DC::THEME{tooltip_fg},
3595 markup => $tip, 3657 markup => $tip,
3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3658 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3659 align => 0,
3597 fontsize => 0.8, 3660 fontsize => 0.8,
3598 style => 1, # FLAG_INVERSE 3661 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3599 ellipsise => 0, 3662 ellipsise => 0,
3600 font => ($widget->{tooltip_font} || $::FONT_PROP), 3663 font => ($widget->{tooltip_font} || $::FONT_PROP),
3601 ); 3664 );
3602} 3665}
3603 3666
3640sub _draw { 3703sub _draw {
3641 my ($self) = @_; 3704 my ($self) = @_;
3642 3705
3643 my ($w, $h) = @$self{qw(w h)}; 3706 my ($w, $h) = @$self{qw(w h)};
3644 3707
3645 glColor 1, 0.8, 0.4; 3708 glColor @{ $DC::THEME{tooltip_bg} };
3646 glRect 0, 0, $w, $h; 3709 glRect 0, 0, $w, $h;
3647 3710
3648 glColor 0, 0, 0; 3711 glColor @{ $DC::THEME{tooltip_border} };
3649 glRect_lineloop .5, .5, $w + .5, $h + .5; 3712 glRect_lineloop .5, .5, $w + .5, $h + .5;
3650 3713
3651 glTranslate 2, 2; 3714 glTranslate 2, 2;
3652 3715
3653 $self->SUPER::_draw; 3716 $self->SUPER::_draw;
3654} 3717}
3655 3718
3656############################################################################# 3719#############################################################################
3657 3720
3658package CFPlus::UI::Face; 3721package DC::UI::Face;
3659 3722
3660our @ISA = CFPlus::UI::DrawBG::; 3723our @ISA = DC::UI::DrawBG::;
3661 3724
3662use CFPlus::OpenGL; 3725use DC::OpenGL;
3663 3726
3664sub new { 3727sub new {
3665 my $class = shift; 3728 my $class = shift;
3666 3729
3667 my $self = $class->SUPER::new ( 3730 my $self = $class->SUPER::new (
3670 aspect => 1, 3733 aspect => 1,
3671 can_events => 0, 3734 can_events => 0,
3672 @_, 3735 @_,
3673 ); 3736 );
3674 3737
3675 if ($self->{anim} && $self->{animspeed}) {
3676 CFPlus::weaken (my $widget = $self);
3677
3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3681 return unless $::CONN;
3682
3683 my $w = $widget
3684 or return;
3685
3686 ++$w->{frame};
3687 $w->update_face;
3688
3689 # somehow, $widget can go away
3690 $w->update;
3691 $w->update_timer;
3692 };
3693
3694 $self->update_face;
3695 $self->update_timer; 3738 $self->update_anim;
3696 }
3697 3739
3698 $self 3740 $self
3699} 3741}
3700 3742
3701sub update_timer { 3743sub update_timer {
3702 my ($self) = @_; 3744 my ($self) = @_;
3703 3745
3704 return unless $self->{timer}; 3746 return unless $self->{timer};
3705 3747
3706 if ($self->{visible}) { 3748 if ($self->{visible}) {
3707 $self->{timer}->set (
3708 $self->{anim_start}
3709 + $self->{animspeed}
3710 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3711 );
3712 $self->{timer}->start; 3749 $self->{timer}->start;
3713 } else { 3750 } else {
3714 $self->{timer}->stop; 3751 $self->{timer}->stop;
3715 } 3752 }
3716} 3753}
3729 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3730 } 3767 }
3731 } 3768 }
3732 } 3769 }
3733 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3734 } 3799 }
3735} 3800}
3736 3801
3737sub size_request { 3802sub size_request {
3738 my ($self) = @_; 3803 my ($self) = @_;
3760 return unless $self->{visible}; 3825 return unless $self->{visible};
3761 3826
3762 $self->SUPER::update; 3827 $self->SUPER::update;
3763} 3828}
3764 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3765sub invoke_visibility_change { 3851sub invoke_visibility_change {
3766 my ($self) = @_; 3852 my ($self) = @_;
3767 3853
3768 $self->update_timer; 3854 $self->update_timer;
3769 3855
3793 $self->SUPER::destroy; 3879 $self->SUPER::destroy;
3794} 3880}
3795 3881
3796############################################################################# 3882#############################################################################
3797 3883
3798package CFPlus::UI::Buttonbar; 3884package DC::UI::Buttonbar;
3799 3885
3800our @ISA = CFPlus::UI::HBox::; 3886our @ISA = DC::UI::HBox::;
3801 3887
3802# TODO: should actually wrap buttons and other goodies. 3888# TODO: should actually wrap buttons and other goodies.
3803 3889
3804############################################################################# 3890#############################################################################
3805 3891
3806package CFPlus::UI::Menu; 3892package DC::UI::Menu;
3807 3893
3808our @ISA = CFPlus::UI::Toplevel::; 3894our @ISA = DC::UI::Toplevel::;
3809 3895
3810use CFPlus::OpenGL; 3896use DC::OpenGL;
3811 3897
3812sub new { 3898sub new {
3813 my $class = shift; 3899 my $class = shift;
3814 3900
3815 my $self = $class->SUPER::new ( 3901 my $self = $class->SUPER::new (
3816 items => [], 3902 items => [],
3817 z => 100, 3903 z => 100,
3818 @_, 3904 @_,
3819 ); 3905 );
3820 3906
3821 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3907 $self->add ($self->{vbox} = new DC::UI::VBox);
3822 3908
3823 for my $item (@{ $self->{items} }) { 3909 for my $item (@{ $self->{items} }) {
3824 my ($widget, $cb, $tooltip) = @$item; 3910 my ($widget, $cb, $tooltip) = @$item;
3825 3911
3826 # handle various types of items, only text for now 3912 # handle various types of items, only text for now
3827 if (!ref $widget) { 3913 if (!ref $widget) {
3828 if ($widget =~ /\t/) { 3914 if ($widget =~ /\t/) {
3829 my ($left, $right) = split /\t/, $widget, 2; 3915 my ($left, $right) = split /\t/, $widget, 2;
3830 3916
3831 $widget = new CFPlus::UI::HBox 3917 $widget = new DC::UI::HBox
3832 can_hover => 1, 3918 can_hover => 1,
3833 can_events => 1, 3919 can_events => 1,
3834 tooltip => $tooltip, 3920 tooltip => $tooltip,
3835 children => [ 3921 children => [
3836 (new CFPlus::UI::Label markup => $left, expand => 1), 3922 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3837 (new CFPlus::UI::Label markup => $right, align => +1), 3923 (new DC::UI::Label markup => $right, align => 1),
3838 ], 3924 ],
3839 ; 3925 ;
3840 3926
3841 } else { 3927 } else {
3842 $widget = new CFPlus::UI::Label 3928 $widget = new DC::UI::Label
3843 can_hover => 1, 3929 can_hover => 1,
3844 can_events => 1, 3930 can_events => 1,
3931 align => 0,
3845 markup => $widget, 3932 markup => $widget,
3846 tooltip => $tooltip; 3933 tooltip => $tooltip;
3847 } 3934 }
3848 } 3935 }
3849 3936
3864 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3865 $GRAB = $self; 3952 $GRAB = $self;
3866 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3867 3954
3868 $self->show; 3955 $self->show;
3869 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3870} 3965}
3871 3966
3872sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3873 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3874 3969
3895 1 3990 1
3896} 3991}
3897 3992
3898############################################################################# 3993#############################################################################
3899 3994
3900package CFPlus::UI::Multiplexer; 3995package DC::UI::Multiplexer;
3901 3996
3902our @ISA = CFPlus::UI::Container::; 3997our @ISA = DC::UI::Container::;
3903 3998
3904sub new { 3999sub new {
3905 my $class = shift; 4000 my $class = shift;
3906 4001
3907 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3908 @_, 4003 @_,
3909 ); 4004 );
3910 4005
3911 $self->{current} = $self->{children}[0] 4006 $self->set_current_page (0);
3912 if @{ $self->{children} };
3913 4007
3914 $self 4008 $self
3915} 4009}
3916 4010
3917sub add { 4011sub add {
3918 my ($self, @widgets) = @_; 4012 my ($self, @widgets) = @_;
3919 4013
3920 $self->SUPER::add (@widgets); 4014 $self->SUPER::add (@widgets);
3921 4015
3922 $self->{current} = $self->{children}[0] 4016 $self->set_current_page (0)
3923 if @{ $self->{children} }; 4017 if @widgets == @{ $self->{children} };
3924} 4018}
3925 4019
3926sub get_current_page { 4020sub get_current_page {
3927 my ($self) = @_; 4021 my ($self) = @_;
3928 4022
3934 4028
3935 my $widget = ref $page_or_widget 4029 my $widget = ref $page_or_widget
3936 ? $page_or_widget 4030 ? $page_or_widget
3937 : $self->{children}[$page_or_widget]; 4031 : $self->{children}[$page_or_widget];
3938 4032
4033 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4034
3939 $self->{current} = $widget; 4035 if (($self->{current} = $widget)) {
4036 $self->{current}->set_visible if $self->{current} && $self->{visible};
3940 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4037 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3941 4038
3942 $self->emit (page_changed => $self->{current}); 4039 $self->emit (page_changed => $self->{current});
4040 }
3943 4041
3944 $self->realloc; 4042 $self->realloc;
3945} 4043}
3946 4044
3947sub visible_children { 4045sub visible_children {
3948 $_[0]{current} 4046 $_[0]{current} || ()
3949} 4047}
3950 4048
3951sub size_request { 4049sub size_request {
3952 my ($self) = @_; 4050 my ($self) = @_;
3953 4051
4052 $self->{current}
3954 $self->{current}->size_request 4053 ? $self->{current}->size_request
4054 : (0, 0)
3955} 4055}
3956 4056
3957sub invoke_size_allocate { 4057sub invoke_size_allocate {
3958 my ($self, $w, $h) = @_; 4058 my ($self, $w, $h) = @_;
3959 4059
3960 $self->{current}->configure (0, 0, $w, $h); 4060 $self->{current}->configure (0, 0, $w, $h)
4061 if $self->{current};
3961 4062
3962 1 4063 1
3963} 4064}
3964 4065
3965sub _draw { 4066sub _draw {
3966 my ($self) = @_; 4067 my ($self) = @_;
3967 4068
3968 $self->{current}->draw; 4069 $self->{current}->draw
4070 if $self->{current};
3969} 4071}
3970 4072
3971############################################################################# 4073#############################################################################
3972 4074
3973package CFPlus::UI::Notebook; 4075package DC::UI::Notebook;
3974 4076
3975use CFPlus::OpenGL; 4077use DC::OpenGL;
3976 4078
3977our @ISA = CFPlus::UI::VBox::; 4079our @ISA = DC::UI::VBox::;
3978 4080
3979sub new { 4081sub new {
3980 my $class = shift; 4082 my $class = shift;
3981 4083
3982 my $self = $class->SUPER::new ( 4084 my $self = $class->SUPER::new (
3983 buttonbar => (new CFPlus::UI::Buttonbar), 4085 buttonbar => (new DC::UI::Buttonbar),
3984 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4086 multiplexer => (new DC::UI::Multiplexer expand => 1),
3985 active_outline => [.7, .7, 0.2], 4087 active_outline => [.7, .7, 0.2],
3986 # filter => # will be put between multiplexer and $self 4088 # filter => # will be put between multiplexer and $self
3987 @_, 4089 @_,
3988 ); 4090 );
3989 4091
3998 4100
3999 for my $child (@$widgets) { 4101 for my $child (@$widgets) {
4000 Scalar::Util::weaken $child; 4102 Scalar::Util::weaken $child;
4001 $child->{c_tab_} ||= do { 4103 $child->{c_tab_} ||= do {
4002 my $tab = 4104 my $tab =
4003 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4105 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4004 ? $child->{c_tab} 4106 ? $child->{c_tab}
4005 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4107 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4006 4108
4007 $tab->connect (activate => sub { 4109 $tab->connect (activate => sub {
4008 $wself->set_current_page ($child); 4110 $wself->set_current_page ($child);
4009 }); 4111 });
4010 4112
4040} 4142}
4041 4143
4042sub pages { 4144sub pages {
4043 my ($self) = @_; 4145 my ($self) = @_;
4044 $self->{multiplexer}->children 4146 $self->{multiplexer}->children
4147}
4148
4149sub page_index {
4150 my ($self, $widget) = @_;
4151
4152 my $i = 0;
4153 for ($self->pages) {
4154 if ($_ eq $widget) { return $i };
4155 $i++;
4156 }
4157
4158 undef
4045} 4159}
4046 4160
4047sub add_tab { 4161sub add_tab {
4048 my ($self, $title, $widget, $tooltip) = @_; 4162 my ($self, $title, $widget, $tooltip) = @_;
4049 4163
4086 } 4200 }
4087} 4201}
4088 4202
4089############################################################################# 4203#############################################################################
4090 4204
4091package CFPlus::UI::Selector; 4205package DC::UI::Selector;
4092 4206
4093use utf8; 4207use utf8;
4094 4208
4095our @ISA = CFPlus::UI::Button::; 4209our @ISA = DC::UI::Button::;
4096 4210
4097sub new { 4211sub new {
4098 my $class = shift; 4212 my $class = shift;
4099 4213
4100 my $self = $class->SUPER::new ( 4214 my $self = $class->SUPER::new (
4117 my ($value, $title, $tooltip) = @$_; 4231 my ($value, $title, $tooltip) = @$_;
4118 4232
4119 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4233 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4120 } 4234 }
4121 4235
4122 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4236 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4123} 4237}
4124 4238
4125sub _set_value { 4239sub _set_value {
4126 my ($self, $value) = @_; 4240 my ($self, $value) = @_;
4127 4241
4150 $self->_set_value ($self->{value}); 4264 $self->_set_value ($self->{value});
4151} 4265}
4152 4266
4153############################################################################# 4267#############################################################################
4154 4268
4155package CFPlus::UI::Statusbox; 4269package DC::UI::Statusbox;
4156 4270
4157our @ISA = CFPlus::UI::VBox::; 4271our @ISA = DC::UI::VBox::;
4158 4272
4159sub new { 4273sub new {
4160 my $class = shift; 4274 my $class = shift;
4161 4275
4162 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4163 fontsize => 0.8, 4277 fontsize => 0.8,
4164 @_, 4278 @_,
4165 ); 4279 );
4166 4280
4167 CFPlus::weaken (my $this = $self); 4281 DC::weaken (my $this = $self);
4168 4282
4169 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4283 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4170 4284
4171 $self 4285 $self
4172} 4286}
4173 4287
4174sub reorder { 4288sub reorder {
4175 my ($self) = @_; 4289 my ($self) = @_;
4176 my $NOW = Time::HiRes::time; 4290 my $NOW = EV::time;
4177 4291
4178 # freeze display when hovering over any label 4292 # freeze display when hovering over any label
4179 return if $CFPlus::UI::TOOLTIP->{owner} 4293 return if $DC::UI::TOOLTIP->{owner}
4180 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4294 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4181 values %{ $self->{item} }; 4295 values %{ $self->{item} };
4182 4296
4183 while (my ($k, $v) = each %{ $self->{item} }) { 4297 while (my ($k, $v) = each %{ $self->{item} }) {
4184 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4298 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4185 } 4299 }
4206 for ($short) { 4320 for ($short) {
4207 s/^\s+//; 4321 s/^\s+//;
4208 s/\s+/ /g; 4322 s/\s+/ /g;
4209 } 4323 }
4210 4324
4211 new CFPlus::UI::Label 4325 new DC::UI::Label
4212 markup => $short, 4326 markup => $short,
4213 tooltip => $item->{tooltip}, 4327 tooltip => $item->{tooltip},
4214 tooltip_font => $::FONT_PROP, 4328 tooltip_font => $::FONT_PROP,
4215 tooltip_width => 0.67, 4329 tooltip_width => 0.67,
4216 fontsize => $item->{fontsize} || $self->{fontsize}, 4330 fontsize => $item->{fontsize} || $self->{fontsize},
4217 max_w => $::WIDTH * 0.44, 4331 max_w => $::WIDTH * 0.44,
4332 align => 0,
4218 fg => [@{ $item->{fg} }], 4333 fg => [@{ $item->{fg} }],
4219 can_events => 1, 4334 can_events => 1,
4220 can_hover => 1 4335 can_hover => 1
4221 }; 4336 };
4222 4337
4230 $label->{fg}[3] = $item->{fg}[3] || 1; 4345 $label->{fg}[3] = $item->{fg}[3] || 1;
4231 } 4346 }
4232 4347
4233 push @widgets, $label; 4348 push @widgets, $label;
4234 } 4349 }
4350
4351 my $hash = join ",", @widgets;
4352 return if $hash eq $self->{last_widget_hash};
4353 $self->{last_widget_hash} = $hash;
4235 4354
4236 $self->clear; 4355 $self->clear;
4237 $self->SUPER::add (reverse @widgets); 4356 $self->SUPER::add (reverse @widgets);
4238} 4357}
4239 4358
4295 $self->SUPER::destroy; 4414 $self->SUPER::destroy;
4296} 4415}
4297 4416
4298############################################################################# 4417#############################################################################
4299 4418
4300package CFPlus::UI::Root; 4419package DC::UI::Root;
4301 4420
4302our @ISA = CFPlus::UI::Container::; 4421our @ISA = DC::UI::Container::;
4303 4422
4304use List::Util qw(min max); 4423use List::Util qw(min max);
4305 4424
4306use CFPlus::OpenGL; 4425use DC::OpenGL;
4307 4426
4308sub new { 4427sub new {
4309 my $class = shift; 4428 my $class = shift;
4310 4429
4311 my $self = $class->SUPER::new ( 4430 my $self = $class->SUPER::new (
4312 visible => 1, 4431 visible => 1,
4313 @_, 4432 @_,
4314 ); 4433 );
4315 4434
4316 CFPlus::weaken ($self->{root} = $self); 4435 DC::weaken ($self->{root} = $self);
4317 4436
4318 $self 4437 $self
4319} 4438}
4320 4439
4321sub size_request { 4440sub size_request {
4369} 4488}
4370 4489
4371sub update { 4490sub update {
4372 my ($self) = @_; 4491 my ($self) = @_;
4373 4492
4374 $::WANT_REFRESH->start; 4493 $::WANT_REFRESH = 1;
4375} 4494}
4376 4495
4377sub add { 4496sub add {
4378 my ($self, @children) = @_; 4497 my ($self, @children) = @_;
4379 4498
4446 4565
4447 delete $queue{$widget+0}; 4566 delete $queue{$widget+0};
4448 4567
4449 my ($w, $h) = $widget->size_request; 4568 my ($w, $h) = $widget->size_request;
4450 4569
4451 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4570 $w += $widget->{padding_x} * 2;
4452 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4571 $h += $widget->{padding_y} * 2;
4572
4573 $w = max $widget->{min_w}, $w;
4574 $h = max $widget->{min_h}, $h;
4453 4575
4454 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4576 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4455 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4577 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4456 4578
4457 $w = $widget->{force_w} if exists $widget->{force_w}; 4579 $w = $widget->{force_w} if exists $widget->{force_w};
4524 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4646 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4525 glMatrixMode GL_MODELVIEW; 4647 glMatrixMode GL_MODELVIEW;
4526 glLoadIdentity; 4648 glLoadIdentity;
4527 4649
4528 { 4650 {
4529 package CFPlus::UI::Base; 4651 package DC::UI::Base;
4530 4652
4531 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4653 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4532 (0, 0, $self->{w}, $self->{h}); 4654 (0, 0, $self->{w}, $self->{h});
4533 4655
4534 $self->_draw; 4656 $self->_draw;
4535 } 4657 }
4536} 4658}
4537 4659
4538############################################################################# 4660#############################################################################
4539 4661
4540package CFPlus::UI; 4662package DC::UI;
4541 4663
4542$ROOT = new CFPlus::UI::Root; 4664$ROOT = new DC::UI::Root;
4543$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4544 4666
45451 46671
4546

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines