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.447 by root, Sat Dec 22 06:50:45 2007 UTC vs.
Revision 1.483 by root, Wed Jan 4 11:23:23 2012 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
7 6
8use CFPlus; 7use AnyEvent ();
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 common::sense;
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 {
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 common::sense;
649
633use CFPlus::OpenGL; 650use DC::OpenGL;
634 651
635sub new { 652sub new {
636 my $class = shift; 653 my $class = shift;
637 654
638 $class->SUPER::new ( 655 $class->SUPER::new (
640 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
641 @_ 658 @_
642 ) 659 )
643} 660}
644 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
645sub _draw { 669sub _draw {
646 my ($self) = @_; 670 my ($self) = @_;
647 671
648 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
649 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
650 : $self->{bg}; 674 : $self->{bg};
651 675
652 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
653 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
654 678
660 } 684 }
661} 685}
662 686
663############################################################################# 687#############################################################################
664 688
665package CFPlus::UI::Empty; 689package DC::UI::Empty;
666 690
667our @ISA = CFPlus::UI::Base::; 691our @ISA = DC::UI::Base::;
668 692
669sub new { 693sub new {
670 my ($class, %arg) = @_; 694 my ($class, %arg) = @_;
671 $class->SUPER::new (can_events => 0, %arg); 695 $class->SUPER::new (can_events => 0, %arg);
672} 696}
679 703
680sub draw { } 704sub draw { }
681 705
682############################################################################# 706#############################################################################
683 707
684package CFPlus::UI::Container; 708package DC::UI::Container;
685 709
686our @ISA = CFPlus::UI::Base::; 710our @ISA = DC::UI::Base::;
687 711
688sub new { 712sub new {
689 my ($class, %arg) = @_; 713 my ($class, %arg) = @_;
690 714
691 my $children = delete $arg{children}; 715 my $children = delete $arg{children};
785 $_->draw for $self->visible_children; 809 $_->draw for $self->visible_children;
786} 810}
787 811
788############################################################################# 812#############################################################################
789 813
790package CFPlus::UI::Bin; 814package DC::UI::Bin;
791 815
792our @ISA = CFPlus::UI::Container::; 816our @ISA = DC::UI::Container::;
793 817
794sub new { 818sub new {
795 my ($class, %arg) = @_; 819 my ($class, %arg) = @_;
796 820
797 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 821 my $child = (delete $arg{child}) || new DC::UI::Empty::;
798 822
799 $class->SUPER::new (children => [$child], %arg) 823 $class->SUPER::new (children => [$child], %arg)
800} 824}
801 825
802sub add { 826sub add {
809sub remove { 833sub remove {
810 my ($self, $widget) = @_; 834 my ($self, $widget) = @_;
811 835
812 $self->SUPER::remove ($widget); 836 $self->SUPER::remove ($widget);
813 837
814 $self->{children} = [new CFPlus::UI::Empty] 838 $self->{children} = [new DC::UI::Empty]
815 unless @{$self->{children}}; 839 unless @{$self->{children}};
816} 840}
817 841
818sub child { $_[0]->{children}[0] } 842sub child { $_[0]->{children}[0] }
819 843
830} 854}
831 855
832############################################################################# 856#############################################################################
833# back-buffered drawing area 857# back-buffered drawing area
834 858
835package CFPlus::UI::Window; 859package DC::UI::Window;
836 860
837our @ISA = CFPlus::UI::Bin::; 861our @ISA = DC::UI::Bin::;
838 862
839use CFPlus::OpenGL; 863use DC::OpenGL;
840 864
841sub new { 865sub new {
842 my ($class, %arg) = @_; 866 my ($class, %arg) = @_;
843 867
844 my $self = $class->SUPER::new (%arg); 868 my $self = $class->SUPER::new (%arg);
866} 890}
867 891
868sub render_child { 892sub render_child {
869 my ($self) = @_; 893 my ($self) = @_;
870 894
871 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 895 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
872 glClearColor 0, 0, 0, 0; 896 glClearColor 0, 0, 0, 0;
873 glClear GL_COLOR_BUFFER_BIT; 897 glClear GL_COLOR_BUFFER_BIT;
874 898
875 { 899 {
876 package CFPlus::UI::Base; 900 package DC::UI::Base;
877 901
878 local ($draw_x, $draw_y, $draw_w, $draw_h) = 902 local ($draw_x, $draw_y, $draw_w, $draw_h) =
879 (0, 0, $self->{w}, $self->{h}); 903 (0, 0, $self->{w}, $self->{h});
880 904
881 $self->_render; 905 $self->_render;
898 glDisable GL_TEXTURE_2D; 922 glDisable GL_TEXTURE_2D;
899} 923}
900 924
901############################################################################# 925#############################################################################
902 926
903package CFPlus::UI::ViewPort; 927package DC::UI::ViewPort;
904 928
905use List::Util qw(min max); 929use List::Util qw(min max);
906 930
907our @ISA = CFPlus::UI::Window::; 931our @ISA = DC::UI::Window::;
908 932
909sub new { 933sub new {
910 my $class = shift; 934 my $class = shift;
911 935
912 $class->SUPER::new ( 936 $class->SUPER::new (
996 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
997 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
998 ) { 1022 ) {
999 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1023 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1000 } else { 1024 } else {
1001 $self->CFPlus::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
1002 } 1026 }
1003} 1027}
1004 1028
1005sub _render { 1029sub _render {
1006 my ($self) = @_; 1030 my ($self) = @_;
1007 1031
1008 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1032 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1009 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1033 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1010 1034
1011 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1012 1036
1013 $self->SUPER::_render; 1037 $self->SUPER::_render;
1014} 1038}
1015 1039
1016############################################################################# 1040#############################################################################
1017 1041
1018package CFPlus::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1019 1043
1020our @ISA = CFPlus::UI::Table::; 1044our @ISA = DC::UI::Table::;
1021 1045
1022sub new { 1046sub new {
1023 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1024 1048
1025 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1026 1050
1027 my $self; 1051 my $self;
1028 1052
1029 my $hslider = new CFPlus::UI::Slider 1053 my $hslider = new DC::UI::Slider
1030 c_col => 0, 1054 c_col => 0,
1031 c_row => 1, 1055 c_row => 1,
1032 vertical => 0, 1056 vertical => 0,
1033 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1034 on_changed => sub { 1058 on_changed => sub {
1035 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1037 }, 1061 },
1038 ; 1062 ;
1039 1063
1040 my $vslider = new CFPlus::UI::Slider 1064 my $vslider = new DC::UI::Slider
1041 c_col => 1, 1065 c_col => 1,
1042 c_row => 0, 1066 c_row => 0,
1043 vertical => 1, 1067 vertical => 1,
1044 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1045 on_changed => sub { 1069 on_changed => sub {
1057 col_expand => [1, 0], 1081 col_expand => [1, 0],
1058 row_expand => [1, 0], 1082 row_expand => [1, 0],
1059 %arg, 1083 %arg,
1060 ); 1084 );
1061 1085
1062 $self->{vp} = new CFPlus::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1063 c_col => 0, 1087 c_col => 0,
1064 c_row => 0, 1088 c_row => 0,
1065 expand => 1, 1089 expand => 1,
1066 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1067 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1193 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1194} 1218}
1195 1219
1196############################################################################# 1220#############################################################################
1197 1221
1198package CFPlus::UI::Frame; 1222package DC::UI::Frame;
1199 1223
1200our @ISA = CFPlus::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1201 1225
1202use CFPlus::OpenGL; 1226use DC::OpenGL;
1203 1227
1204sub new { 1228sub new {
1205 my $class = shift; 1229 my $class = shift;
1206 1230
1207 $class->SUPER::new ( 1231 $class->SUPER::new (
1226 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1227} 1251}
1228 1252
1229############################################################################# 1253#############################################################################
1230 1254
1231package CFPlus::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1232 1256
1233our @ISA = CFPlus::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1234 1258
1235use CFPlus::OpenGL; 1259use DC::OpenGL;
1236 1260
1237sub new { 1261sub new {
1238 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1239 1263
1240 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1241 $arg{label} = new CFPlus::UI::Label 1265 $arg{label} = new DC::UI::Label
1242 align => 1, 1266 align => 1,
1243 valign => 0, 1267 valign => 0.5,
1244 text => $arg{label}, 1268 text => $arg{label},
1245 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1246 } 1270 }
1247 1271
1248 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1249 # label => "", 1273 # label => "",
1250 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1251 border => 0.8, 1275 border => 0.8,
1252 style => 'single', 1276 style => 'single',
1253 %arg, 1277 %arg,
1254 ); 1278 );
1255 1279
1258 1282
1259sub add { 1283sub add {
1260 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1261 1285
1262 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1263 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1264} 1288}
1265 1289
1266sub border { 1290sub border {
1267 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1268} 1292}
1308 my $border = $self->border; 1332 my $border = $self->border;
1309 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1310 1334
1311 $child->draw; 1335 $child->draw;
1312 1336
1313 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1314 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1315 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1317 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1318 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1342 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1326 } 1350 }
1327} 1351}
1328 1352
1329############################################################################# 1353#############################################################################
1330 1354
1331package CFPlus::UI::Toplevel; 1355package DC::UI::Toplevel;
1332 1356
1333our @ISA = CFPlus::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1334 1358
1335use CFPlus::OpenGL; 1359use DC::OpenGL;
1336 1360
1337my $bg = 1361my $bg =
1338 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1339 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1340 1364
1341my @border = 1365my @border =
1342 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1366 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); 1367 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1344 1368
1345my @icon = 1369my @icon =
1346 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1370 map { new_from_resource DC::Texture $_, mipmap => 1 }
1347 qw(x1_move.png x1_resize.png); 1371 qw(x1_move.png x1_resize.png);
1348 1372
1349sub new { 1373sub new {
1350 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1351 1375
1352 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1353 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1354 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1355 border => 0.6, 1379 border => 0.8,
1356 can_events => 1, 1380 can_events => 1,
1357 min_w => 64, 1381 min_w => 64,
1358 min_h => 32, 1382 min_h => 32,
1359 %arg, 1383 %arg,
1360 ); 1384 );
1361 1385
1362 $self->{title_widget} = new CFPlus::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1363 align => 0, 1387 align => 0.5,
1364 valign => 1, 1388 valign => 1,
1365 text => $self->{title}, 1389 text => $self->{title},
1366 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1367 if exists $self->{title}; 1391 if exists $self->{title};
1368 1392
1369 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1370 $self->{close_button} = 1394 $self->{close_button} =
1371 new CFPlus::UI::ImageButton 1395 new DC::UI::ImageButton
1372 path => 'x1_close.png', 1396 path => 'x1_close.png',
1373 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1374 1398
1375 $self->CFPlus::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1376 } 1400 }
1377 1401
1378 $self 1402 $self
1379} 1403}
1380 1404
1381sub add { 1405sub add {
1382 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1383 1407
1384 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1385 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1409 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1386 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1410 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1387} 1411}
1388 1412
1389sub border { 1413sub border {
1390 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1391} 1415}
1529 glEnable GL_TEXTURE_2D; 1553 glEnable GL_TEXTURE_2D;
1530 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1554 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1531 1555
1532 my $border = $self->border; 1556 my $border = $self->border;
1533 1557
1558 if ($border) {
1534 glColor @{ $self->{border_bg} }; 1559 glColor @{ $self->{border_bg} };
1535 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1560 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1536 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1561 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1537 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1562 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1538 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1563 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1539 1564
1540 # move 1565 # move
1541 my $w2 = ($w - $border) * .5; 1566 my $w2 = ($w - $border) * .5;
1542 my $h2 = ($h - $border) * .5; 1567 my $h2 = ($h - $border) * .5;
1543 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1545 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1570 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1546 1571
1547 # resize 1572 # resize
1548 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1573 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1574 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1550 unless $self->{has_close_button}; 1575 unless $self->{has_close_button};
1551 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1577 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1578 }
1553 1579
1554 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1580 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1555 glColor @{ $self->{bg} }; 1581 glColor @{ $self->{bg} };
1556 1582
1557 # TODO: repeat texture not scale 1583 # TODO: repeat texture not scale
1576 if $self->{close_button}; 1602 if $self->{close_button};
1577} 1603}
1578 1604
1579############################################################################# 1605#############################################################################
1580 1606
1581package CFPlus::UI::Table; 1607package DC::UI::Table;
1582 1608
1583our @ISA = CFPlus::UI::Container::; 1609our @ISA = DC::UI::Container::;
1584 1610
1585use List::Util qw(max sum); 1611use List::Util qw(max sum);
1586 1612
1587use CFPlus::OpenGL; 1613use DC::OpenGL;
1588 1614
1589sub new { 1615sub new {
1590 my $class = shift; 1616 my $class = shift;
1591 1617
1592 $class->SUPER::new ( 1618 $class->SUPER::new (
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1662 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1637 1663
1638 my $sw = sum @w[$c .. $c + $cs - 1]; 1664 my $sw = sum @w[$c .. $c + $cs - 1];
1639 1665
1640 if ($w > $sw) { 1666 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1667 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1642 } 1668 }
1643 } 1669 }
1644 1670
1645 # second pass, rows 1671 # second pass, rows
1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1672 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1673 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1648 1674
1649 my $sh = sum @h[$r .. $r + $rs - 1]; 1675 my $sh = sum @h[$r .. $r + $rs - 1];
1650 1676
1651 if ($h > $sh) { 1677 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1678 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1653 } 1679 }
1654 } 1680 }
1655 1681
1656 (\@w, \@h) 1682 (\@w, \@h)
1657} 1683}
1680 @col_expand = (1) x @$ws unless @col_expand; 1706 @col_expand = (1) x @$ws unless @col_expand;
1681 my $col_expand = (sum @col_expand) || 1; 1707 my $col_expand = (sum @col_expand) || 1;
1682 1708
1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1709 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1684 1710
1685 CFPlus::UI::harmonize $ws; 1711 DC::UI::harmonize $ws;
1686 1712
1687 my @row_expand = @{$self->{row_expand}}; 1713 my @row_expand = @{$self->{row_expand}};
1688 @row_expand = (1) x @$ws unless @row_expand; 1714 @row_expand = (1) x @$ws unless @row_expand;
1689 my $row_expand = (sum @row_expand) || 1; 1715 my $row_expand = (sum @row_expand) || 1;
1690 1716
1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1717 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1692 1718
1693 CFPlus::UI::harmonize $hs; 1719 DC::UI::harmonize $hs;
1694 1720
1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1721 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1722 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1697 1723
1698 for my $widget ($self->children) { 1724 for my $widget ($self->children) {
1707 1 1733 1
1708} 1734}
1709 1735
1710############################################################################# 1736#############################################################################
1711 1737
1712package CFPlus::UI::Fixed; 1738package DC::UI::Fixed;
1713 1739
1714use List::Util qw(min max); 1740use List::Util qw(min max);
1715 1741
1716our @ISA = CFPlus::UI::Container::; 1742our @ISA = DC::UI::Container::;
1717 1743
1718sub _scale($$$) { 1744sub _scale($$$) {
1719 my ($rel, $val, $max) = @_; 1745 my ($rel, $val, $max) = @_;
1720 1746
1721 $rel ? $val * $max : $val 1747 $rel ? $val * $max : $val
1773 1 1799 1
1774} 1800}
1775 1801
1776############################################################################# 1802#############################################################################
1777 1803
1778package CFPlus::UI::Box; 1804package DC::UI::Box;
1779 1805
1780our @ISA = CFPlus::UI::Container::; 1806our @ISA = DC::UI::Container::;
1781 1807
1782sub size_request { 1808sub size_request {
1783 my ($self) = @_; 1809 my ($self) = @_;
1784 1810
1785 my @children = $self->visible_children; 1811 my @children = $self->visible_children;
1820 $req[$_] += $space * $children[$_]{expand} 1846 $req[$_] += $space * $children[$_]{expand}
1821 for 0 .. $#children; 1847 for 0 .. $#children;
1822 } 1848 }
1823 } 1849 }
1824 1850
1825 CFPlus::UI::harmonize \@req; 1851 DC::UI::harmonize \@req;
1826 1852
1827 my $pos = 0; 1853 my $pos = 0;
1828 for (0 .. $#children) { 1854 for (0 .. $#children) {
1829 my $alloc = $req[$_]; 1855 my $alloc = $req[$_];
1830 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1856 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1835 1 1861 1
1836} 1862}
1837 1863
1838############################################################################# 1864#############################################################################
1839 1865
1840package CFPlus::UI::HBox; 1866package DC::UI::HBox;
1841 1867
1842our @ISA = CFPlus::UI::Box::; 1868our @ISA = DC::UI::Box::;
1843 1869
1844sub new { 1870sub new {
1845 my $class = shift; 1871 my $class = shift;
1846 1872
1847 $class->SUPER::new ( 1873 $class->SUPER::new (
1850 ) 1876 )
1851} 1877}
1852 1878
1853############################################################################# 1879#############################################################################
1854 1880
1855package CFPlus::UI::VBox; 1881package DC::UI::VBox;
1856 1882
1857our @ISA = CFPlus::UI::Box::; 1883our @ISA = DC::UI::Box::;
1858 1884
1859sub new { 1885sub new {
1860 my $class = shift; 1886 my $class = shift;
1861 1887
1862 $class->SUPER::new ( 1888 $class->SUPER::new (
1865 ) 1891 )
1866} 1892}
1867 1893
1868############################################################################# 1894#############################################################################
1869 1895
1870package CFPlus::UI::Label; 1896package DC::UI::Label;
1871 1897
1872our @ISA = CFPlus::UI::DrawBG::; 1898our @ISA = DC::UI::DrawBG::;
1873 1899
1874use CFPlus::OpenGL; 1900use DC::OpenGL;
1875 1901
1876sub new { 1902sub new {
1877 my ($class, %arg) = @_; 1903 my ($class, %arg) = @_;
1878 1904
1879 my $self = $class->SUPER::new ( 1905 my $self = $class->SUPER::new (
1884 #text => initial text 1910 #text => initial text
1885 #markup => initial narkup 1911 #markup => initial narkup
1886 #max_w => maximum pixel width 1912 #max_w => maximum pixel width
1887 #style => 0, # render flags 1913 #style => 0, # render flags
1888 ellipsise => 3, # end 1914 ellipsise => 3, # end
1889 layout => (new CFPlus::Layout), 1915 layout => (new DC::Layout),
1890 fontsize => 1, 1916 fontsize => 1,
1891 align => -1, 1917 align => 0.5,
1892 valign => -1, 1918 valign => 0.5,
1893 padding_x => 2, 1919 padding_x => 4,
1894 padding_y => 2, 1920 padding_y => 2,
1895 can_events => 0, 1921 can_events => 0,
1896 %arg 1922 %arg
1897 ); 1923 );
1898 1924
1899 if (exists $self->{template}) { 1925 if (exists $self->{template}) {
1900 my $layout = new CFPlus::Layout; 1926 my $layout = new DC::Layout;
1901 $layout->set_text (delete $self->{template}); 1927 $layout->set_text (delete $self->{template});
1902 $self->{template} = $layout; 1928 $self->{template} = $layout;
1903 } 1929 }
1904 1930
1905 if (exists $self->{markup}) { 1931 if (exists $self->{markup}) {
1964 1990
1965 $self->{size_req} ||= do { 1991 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh; 1992 my ($max_w, $max_h) = $self->get_max_wh;
1967 1993
1968 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1994 $self->{layout}->set_font ($self->{font}) if $self->{font};
1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1995 $self->{layout}->set_width ($max_w);
1970 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1996 $self->{layout}->set_ellipsise ($self->{ellipsise});
1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1997 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1998 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1973 1999
1974 my ($w, $h) = $self->{layout}->size; 2000 my ($w, $h) = $self->{layout}->size;
1975 2001
1976 if (exists $self->{template}) { 2002 if (exists $self->{template}) {
1977 $self->{template}->set_font ($self->{font}) if $self->{font}; 2003 $self->{template}->set_font ($self->{font}) if $self->{font};
1978 $self->{template}->set_width ($self->{max_w} || -1); 2004 $self->{template}->set_width ($max_w);
1979 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2005 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1980 2006
1981 my ($w2, $h2) = $self->{template}->size; 2007 my ($w2, $h2) = $self->{template}->size;
1982 2008
1983 $w = List::Util::max $w, $w2; 2009 $w = List::Util::max $w, $w2;
2039 2065
2040 [$self->{layout}->size] 2066 [$self->{layout}->size]
2041 }; 2067 };
2042 2068
2043 unless (exists $self->{ox}) { 2069 unless (exists $self->{ox}) {
2044 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2070 $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} 2071 $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 2072
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2073 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2053 }; 2074 };
2054 2075
2055# unless ($self->{list}) { 2076# unless ($self->{list}) {
2056# $self->{list} = CFPlus::OpenGL::glGenList; 2077# $self->{list} = DC::OpenGL::glGenList;
2057# CFPlus::OpenGL::glNewList $self->{list}; 2078# DC::OpenGL::glNewList $self->{list};
2058# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2079# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2059# CFPlus::OpenGL::glEndList; 2080# DC::OpenGL::glEndList;
2060# } 2081# }
2061# 2082#
2062# CFPlus::OpenGL::glCallList $self->{list}; 2083# DC::OpenGL::glCallList $self->{list};
2063 2084
2064 $self->{layout}->draw; 2085 $self->{layout}->draw;
2065} 2086}
2066 2087
2067#sub destroy { 2088#sub destroy {
2068# my ($self) = @_; 2089# my ($self) = @_;
2069# 2090#
2070# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2091# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2071# 2092#
2072# $self->SUPER::destroy; 2093# $self->SUPER::destroy;
2073#} 2094#}
2074 2095
2075############################################################################# 2096#############################################################################
2076 2097
2077package CFPlus::UI::EntryBase; 2098package DC::UI::EntryBase;
2078 2099
2079our @ISA = CFPlus::UI::Label::; 2100our @ISA = DC::UI::Label::;
2080 2101
2081use CFPlus::OpenGL; 2102use DC::OpenGL;
2082 2103
2083sub new { 2104sub new {
2084 my $class = shift; 2105 my $class = shift;
2085 2106
2086 $class->SUPER::new ( 2107 $class->SUPER::new (
2087 fg => [1, 1, 1], 2108 fg => [1, 1, 1],
2088 bg => [0, 0, 0, 0.2], 2109 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1], 2110 outline => undef,
2090 active_bg => [0, 0, 1, .2], 2111 active_bg => [0, 0, 1, .2],
2091 active_fg => [1, 1, 1], 2112 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0], 2113 active_outline => [1, 1, 0],
2093 can_hover => 1, 2114 can_hover => 1,
2094 can_focus => 1, 2115 can_focus => 1,
2116 align => 0,
2095 valign => 0, 2117 valign => 0.5,
2096 can_events => 1, 2118 can_events => 1,
2097 ellipsise => 0, 2119 ellipsise => 0,
2120 padding_x => 4,
2121 padding_y => 2,
2098 #text => ... 2122 #text => ...
2099 #hidden => "*", 2123 #hidden => "*",
2100 @_ 2124 @_
2101 ) 2125 )
2102} 2126}
2149 2173
2150 my $text = $self->get_text; 2174 my $text = $self->get_text;
2151 2175
2152 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2176 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2153 2177
2154 if ($uni == 8) { 2178 if ($sym == DC::SDLK_BACKSPACE) {
2155 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2179 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2156 } elsif ($uni == 127) { 2180 } elsif ($sym == DC::SDLK_DELETE) {
2157 substr $text, $self->{cursor}, 1, ""; 2181 substr $text, $self->{cursor}, 1, "";
2158 } elsif ($sym == CFPlus::SDLK_LEFT) { 2182 } elsif ($sym == DC::SDLK_LEFT) {
2159 --$self->{cursor} if $self->{cursor}; 2183 --$self->{cursor} if $self->{cursor};
2160 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2184 } elsif ($sym == DC::SDLK_RIGHT) {
2161 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2185 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2162 } elsif ($sym == CFPlus::SDLK_HOME) { 2186 } elsif ($sym == DC::SDLK_HOME) {
2163 # what a hack 2187 # what a hack
2164 $self->{cursor} = 2188 $self->{cursor} =
2165 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2189 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2166 ? length $1 2190 ? length $1
2167 : 0; 2191 : 0;
2168 } elsif ($sym == CFPlus::SDLK_END) { 2192 } elsif ($sym == DC::SDLK_END) {
2169 # uh, again 2193 # uh, again
2170 $self->{cursor} = 2194 $self->{cursor} =
2171 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2195 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2172 ? $self->{cursor} + length $1 2196 ? $self->{cursor} + length $1
2173 : length $self->{text}; 2197 : length $self->{text};
2264 glColor @{$self->{active_outline}}; 2288 glColor @{$self->{active_outline}};
2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2289 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2266 glLineWidth 1; 2290 glLineWidth 1;
2267 2291
2268 } else { 2292 } else {
2269 glColor @{$self->{outline}}; 2293 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2270 glBegin GL_LINE_STRIP; 2294 glBegin GL_LINE_STRIP;
2271 glVertex .5, $self->{h} * .5; 2295 glVertex .5, $self->{h} * .5;
2272 glVertex .5, $self->{h} - 2.5; 2296 glVertex .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} - 2.5; 2297 glVertex $self->{w} - .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - .5, $self->{h} * .5; 2298 glVertex $self->{w} - .5, $self->{h} * .5;
2276 } 2300 }
2277} 2301}
2278 2302
2279############################################################################# 2303#############################################################################
2280 2304
2281package CFPlus::UI::Entry; 2305package DC::UI::Entry;
2282 2306
2283our @ISA = CFPlus::UI::EntryBase::; 2307our @ISA = DC::UI::EntryBase::;
2284 2308
2285use CFPlus::OpenGL; 2309use DC::OpenGL;
2310
2311sub new {
2312 my $class = shift;
2313
2314 $class->SUPER::new (
2315 history_pointer => -1,
2316 @_
2317 )
2318}
2319
2286 2320
2287sub invoke_key_down { 2321sub invoke_key_down {
2288 my ($self, $ev) = @_; 2322 my ($self, $ev) = @_;
2289 2323
2290 my $sym = $ev->{sym}; 2324 my $sym = $ev->{sym};
2296 $self->{history_pointer} = -1; 2330 $self->{history_pointer} = -1;
2297 $self->{history_saveback} = ''; 2331 $self->{history_saveback} = '';
2298 $self->emit (activate => $txt); 2332 $self->emit (activate => $txt);
2299 $self->update; 2333 $self->update;
2300 2334
2301 } elsif ($sym == CFPlus::SDLK_UP) { 2335 } elsif ($sym == DC::SDLK_UP) {
2302 if ($self->{history_pointer} < 0) { 2336 if ($self->{history_pointer} < 0) {
2303 $self->{history_saveback} = $self->get_text; 2337 $self->{history_saveback} = $self->get_text;
2304 } 2338 }
2305 if (@{$self->{history} || []} > 0) { 2339 if (@{$self->{history} || []} > 0) {
2306 $self->{history_pointer}++; 2340 $self->{history_pointer}++;
2308 $self->{history_pointer} = @{$self->{history} || []} - 1; 2342 $self->{history_pointer} = @{$self->{history} || []} - 1;
2309 } 2343 }
2310 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2344 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2311 } 2345 }
2312 2346
2313 } elsif ($sym == CFPlus::SDLK_DOWN) { 2347 } elsif ($sym == DC::SDLK_DOWN) {
2314 $self->{history_pointer}--; 2348 $self->{history_pointer}--;
2315 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2349 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2316 2350
2317 if ($self->{history_pointer} >= 0) { 2351 if ($self->{history_pointer} >= 0) {
2318 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2352 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2319 } else { 2353 } else {
2354 if (defined $self->{history_saveback}) {
2320 $self->set_text ($self->{history_saveback}); 2355 $self->set_text ($self->{history_saveback});
2356 $self->{history_saveback} = undef;
2357 }
2321 } 2358 }
2322 2359
2323 } else { 2360 } else {
2324 return $self->SUPER::invoke_key_down ($ev) 2361 return $self->SUPER::invoke_key_down ($ev)
2325 } 2362 }
2327 1 2364 1
2328} 2365}
2329 2366
2330############################################################################# 2367#############################################################################
2331 2368
2332package CFPlus::UI::TextEdit; 2369package DC::UI::TextEdit;
2333 2370
2334our @ISA = CFPlus::UI::EntryBase::; 2371our @ISA = DC::UI::EntryBase::;
2335 2372
2336use CFPlus::OpenGL; 2373use DC::OpenGL;
2374
2375sub new {
2376 my $class = shift;
2377
2378 $class->SUPER::new (
2379 padding_y => 4,
2380
2381 @_
2382 )
2383}
2337 2384
2338sub move_cursor_ver { 2385sub move_cursor_ver {
2339 my ($self, $dy) = @_; 2386 my ($self, $dy) = @_;
2340 2387
2341 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2388 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2342 2389
2343 $y += $dy; 2390 $line += $dy;
2344 2391
2345 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2392 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2346 $self->{cursor} = $index; 2393 $self->{cursor} = $index;
2347 delete $self->{cur_h}; 2394 delete $self->{cur_h};
2348 $self->update; 2395 $self->update;
2349 return; 2396 return;
2350 } 2397 }
2353sub invoke_key_down { 2400sub invoke_key_down {
2354 my ($self, $ev) = @_; 2401 my ($self, $ev) = @_;
2355 2402
2356 my $sym = $ev->{sym}; 2403 my $sym = $ev->{sym};
2357 2404
2358 if ($sym == CFPlus::SDLK_UP) { 2405 if ($sym == DC::SDLK_UP) {
2359 $self->move_cursor_ver (-1); 2406 $self->move_cursor_ver (-1);
2360 } elsif ($sym == CFPlus::SDLK_DOWN) { 2407 } elsif ($sym == DC::SDLK_DOWN) {
2361 $self->move_cursor_ver (+1); 2408 $self->move_cursor_ver (+1);
2362 } else { 2409 } else {
2363 return $self->SUPER::invoke_key_down ($ev) 2410 return $self->SUPER::invoke_key_down ($ev)
2364 } 2411 }
2365 2412
2366 1 2413 1
2367} 2414}
2368 2415
2369############################################################################# 2416#############################################################################
2370 2417
2371package CFPlus::UI::ButtonBin; 2418package DC::UI::ButtonBin;
2372 2419
2373our @ISA = CFPlus::UI::Bin::; 2420our @ISA = DC::UI::Bin::;
2374 2421
2375use CFPlus::OpenGL; 2422use DC::OpenGL;
2376 2423
2377my @tex = 2424my @tex =
2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2425 map { new_from_resource DC::Texture $_, mipmap => 1 }
2379 qw(b1_button_inactive.png b1_button_active.png); 2426 qw(b1_button_inactive.png b1_button_active.png);
2380 2427
2381sub new { 2428sub new {
2382 my $class = shift; 2429 my $class = shift;
2383 2430
2384 $class->SUPER::new ( 2431 $class->SUPER::new (
2385 can_hover => 1, 2432 can_hover => 1,
2386 align => 0, 2433 align => 0.5,
2387 valign => 0, 2434 valign => 0.5,
2388 can_events => 1, 2435 can_events => 1,
2389 @_ 2436 @_
2390 ) 2437 )
2391} 2438}
2392 2439
2415 $self->SUPER::_draw; 2462 $self->SUPER::_draw;
2416} 2463}
2417 2464
2418############################################################################# 2465#############################################################################
2419 2466
2420package CFPlus::UI::Button; 2467package DC::UI::Button;
2421 2468
2422our @ISA = CFPlus::UI::Label::; 2469our @ISA = DC::UI::Label::;
2423 2470
2424use CFPlus::OpenGL; 2471use DC::OpenGL;
2425 2472
2426my @tex = 2473my @tex =
2427 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2474 map { new_from_resource DC::Texture $_, mipmap => 1 }
2428 qw(b1_button_inactive.png b1_button_active.png); 2475 qw(b1_button_inactive.png b1_button_active.png);
2429 2476
2430sub new { 2477sub new {
2431 my $class = shift; 2478 my $class = shift;
2432 2479
2433 $class->SUPER::new ( 2480 $class->SUPER::new (
2434 padding_x => 4, 2481 padding_x => 8,
2435 padding_y => 4, 2482 padding_y => 4,
2436 fg => [1.0, 1.0, 1.0], 2483 fg => [1.0, 1.0, 1.0],
2437 active_fg => [0.8, 0.8, 0.8], 2484 active_fg => [0.8, 0.8, 0.8],
2438 can_hover => 1, 2485 can_hover => 1,
2439 align => 0, 2486 align => 0.5,
2440 valign => 0, 2487 valign => 0.5,
2441 can_events => 1, 2488 can_events => 1,
2442 @_ 2489 @_
2443 ) 2490 )
2444} 2491}
2445 2492
2470 $self->SUPER::_draw; 2517 $self->SUPER::_draw;
2471} 2518}
2472 2519
2473############################################################################# 2520#############################################################################
2474 2521
2475package CFPlus::UI::CheckBox; 2522package DC::UI::CheckBox;
2476 2523
2477our @ISA = CFPlus::UI::DrawBG::; 2524our @ISA = DC::UI::DrawBG::;
2478 2525
2479my @tex = 2526my @tex =
2480 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2527 map { new_from_resource DC::Texture $_, mipmap => 1 }
2481 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2528 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2482 2529
2483use CFPlus::OpenGL; 2530use DC::OpenGL;
2484 2531
2485sub new { 2532sub new {
2486 my $class = shift; 2533 my $class = shift;
2487 2534
2488 $class->SUPER::new ( 2535 $class->SUPER::new (
2536 fontsize => 1,
2489 padding_x => 2, 2537 padding_x => 2,
2490 padding_y => 2, 2538 padding_y => 2,
2491 fg => [1, 1, 1], 2539 fg => [1, 1, 1],
2492 active_fg => [1, 1, 0], 2540 active_fg => [1, 1, 0],
2493 bg => [0, 0, 0, 0.2], 2541 bg => [0, 0, 0, 0.2],
2499} 2547}
2500 2548
2501sub size_request { 2549sub size_request {
2502 my ($self) = @_; 2550 my ($self) = @_;
2503 2551
2504 (6) x 2 2552 ($self->{fontsize} * $::FONTSIZE) x 2
2505} 2553}
2506 2554
2507sub toggle { 2555sub toggle {
2508 my ($self) = @_; 2556 my ($self) = @_;
2509 2557
2545 glDisable GL_TEXTURE_2D; 2593 glDisable GL_TEXTURE_2D;
2546} 2594}
2547 2595
2548############################################################################# 2596#############################################################################
2549 2597
2550package CFPlus::UI::Image; 2598package DC::UI::Image;
2551 2599
2552our @ISA = CFPlus::UI::Base::; 2600our @ISA = DC::UI::DrawBG::;
2553 2601
2554use CFPlus::OpenGL; 2602use DC::OpenGL;
2555 2603
2556our %texture_cache; 2604our %texture_cache;
2557 2605
2558sub new { 2606sub new {
2559 my $class = shift; 2607 my $class = shift;
2566 2614
2567 $self->{path} || $self->{tex} 2615 $self->{path} || $self->{tex}
2568 or Carp::croak "'path' or 'tex' attributes required"; 2616 or Carp::croak "'path' or 'tex' attributes required";
2569 2617
2570 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2618 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2571 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2619 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2572 2620
2573 CFPlus::weaken $texture_cache{$self->{path}}; 2621 DC::weaken $texture_cache{$self->{path}};
2574 2622
2575 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2623 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2576 2624
2577 $self 2625 $self
2578} 2626}
2579 2627
2580sub STORABLE_freeze { 2628sub STORABLE_freeze {
2581 my ($self, $cloning) = @_; 2629 my ($self, $cloning) = @_;
2582 2630
2583 $self->{path} 2631 $self->{path}
2584 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2632 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2585 2633
2586 $self->{path} 2634 $self->{path}
2587} 2635}
2588 2636
2589sub STORABLE_attach { 2637sub STORABLE_attach {
2590 my ($self, $cloning, $path) = @_; 2638 my ($self, $cloning, $path) = @_;
2591 2639
2592 $self->new (path => $path) 2640 $self->new (path => $path)
2593} 2641}
2594 2642
2643sub set_texture {
2644 my ($self, $tex) = @_;
2645
2646 $self->{tex} = $tex;
2647 $self->update;
2648}
2649
2595sub size_request { 2650sub size_request {
2596 my ($self) = @_; 2651 my ($self) = @_;
2597 2652
2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2653 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2599} 2654}
2600 2655
2601sub _draw { 2656sub _draw {
2602 my ($self) = @_; 2657 my ($self) = @_;
2658
2659 $self->SUPER::_draw;
2603 2660
2604 my $tex = $self->{tex}; 2661 my $tex = $self->{tex};
2605 2662
2606 my ($w, $h) = ($self->{w}, $self->{h}); 2663 my ($w, $h) = ($self->{w}, $self->{h});
2607 2664
2620 glDisable GL_TEXTURE_2D; 2677 glDisable GL_TEXTURE_2D;
2621} 2678}
2622 2679
2623############################################################################# 2680#############################################################################
2624 2681
2625package CFPlus::UI::ImageButton; 2682package DC::UI::ImageButton;
2626 2683
2627our @ISA = CFPlus::UI::Image::; 2684our @ISA = DC::UI::Image::;
2628 2685
2629use CFPlus::OpenGL; 2686use DC::OpenGL;
2630
2631my %textures;
2632 2687
2633sub new { 2688sub new {
2634 my $class = shift; 2689 my $class = shift;
2635 2690
2636 my $self = $class->SUPER::new ( 2691 my $self = $class->SUPER::new (
2637 padding_x => 4, 2692 padding_x => 4,
2638 padding_y => 4, 2693 padding_y => 4,
2639 fg => [1, 1, 1], 2694 fg => [1, 1, 1],
2640 active_fg => [0, 0, 1], 2695 active_fg => [0, 0, 1],
2641 can_hover => 1, 2696 can_hover => 1,
2642 align => 0, 2697 align => 0.5,
2643 valign => 0, 2698 valign => 0.5,
2644 can_events => 1, 2699 can_events => 1,
2645 @_ 2700 @_
2646 ); 2701 );
2647} 2702}
2648 2703
2662 1 2717 1
2663} 2718}
2664 2719
2665############################################################################# 2720#############################################################################
2666 2721
2667package CFPlus::UI::VGauge; 2722package DC::UI::VGauge;
2668 2723
2669our @ISA = CFPlus::UI::Base::; 2724our @ISA = DC::UI::Base::;
2670 2725
2671use List::Util qw(min max); 2726use List::Util qw(min max);
2672 2727
2673use CFPlus::OpenGL; 2728use DC::OpenGL;
2674 2729
2675my %tex = ( 2730my %tex = (
2676 food => [ 2731 food => [
2677 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2678 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2733 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2679 ], 2734 ],
2680 grace => [ 2735 grace => [
2681 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2736 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/ 2737 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2683 ], 2738 ],
2684 hp => [ 2739 hp => [
2685 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2740 map { new_from_resource DC::Texture $_, mipmap => 1 }
2686 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2741 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2687 ], 2742 ],
2688 mana => [ 2743 mana => [
2689 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2744 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/ 2745 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2691 ], 2746 ],
2692); 2747);
2693 2748
2694# eg. VGauge->new (gauge => 'food'), default gauge: food 2749# eg. VGauge->new (gauge => 'food'), default gauge: food
2797 glDisable GL_TEXTURE_2D; 2852 glDisable GL_TEXTURE_2D;
2798} 2853}
2799 2854
2800############################################################################# 2855#############################################################################
2801 2856
2802package CFPlus::UI::Progress; 2857package DC::UI::Progress;
2803 2858
2804our @ISA = CFPlus::UI::Label::; 2859our @ISA = DC::UI::Label::;
2805 2860
2806use CFPlus::OpenGL; 2861use DC::OpenGL;
2807 2862
2808sub new { 2863sub new {
2809 my ($class, %arg) = @_; 2864 my ($class, %arg) = @_;
2810 2865
2811 my $self = $class->SUPER::new ( 2866 my $self = $class->SUPER::new (
2867 padding_x => 2,
2868 padding_y => 2,
2812 fg => [1, 1, 1], 2869 fg => [1, 1, 1],
2813 bg => [0, 0, 1, 0.2], 2870 bg => [0, 0, 1, 0.2],
2814 bar => [0.7, 0.5, 0.1, 0.8], 2871 bar => [0.7, 0.5, 0.1, 0.8],
2815 outline => [0.4, 0.3, 0], 2872 outline => [0.4, 0.3, 0],
2816 fontsize => 0.9, 2873 fontsize => 0.9,
2817 valign => 0, 2874 valign => 0.5,
2818 align => 0, 2875 align => 0.5,
2819 can_events => 1, 2876 can_events => 1,
2820 ellipsise => 1, 2877 ellipsise => 1,
2821 label => "%d%%", 2878 label => "%d%%",
2822 %arg, 2879 %arg,
2823 ); 2880 );
2831 my ($self, $label) = @_; 2888 my ($self, $label) = @_;
2832 2889
2833 return if $self->{label} eq $label; 2890 return if $self->{label} eq $label;
2834 $self->{label} = $label; 2891 $self->{label} = $label;
2835 2892
2836 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2893 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2837} 2894}
2838 2895
2839sub set_value { 2896sub set_value {
2840 my ($self, $value) = @_; 2897 my ($self, $value) = @_;
2841 2898
2856 my ($self) = @_; 2913 my ($self) = @_;
2857 2914
2858 glEnable GL_BLEND; 2915 glEnable GL_BLEND;
2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2916 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2860 2917
2918 my $px = $self->{padding_x};
2919 my $py = $self->{padding_y};
2920
2861 if ($self->{value} >= 0) { 2921 if ($self->{value} >= 0) {
2862 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2922 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2863 2923
2864 glColor_premultiply @{$self->{bar}}; 2924 glColor_premultiply @{$self->{bar}};
2865 glRect 2, 2, $s, $self->{h} - 2; 2925 glRect $px, $py, $s, $self->{h} - $py;
2866 glColor_premultiply @{$self->{bg}}; 2926 glColor_premultiply @{$self->{bg}};
2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2927 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2868 } 2928 }
2869 2929
2870 glColor_premultiply @{$self->{outline}}; 2930 glColor_premultiply @{$self->{outline}};
2931
2932 $px -= .5;
2933 $py -= .5;
2934
2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2935 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2872 2936
2873 glDisable GL_BLEND; 2937 glDisable GL_BLEND;
2874 2938
2875 { 2939 {
2876 local $self->{bg}; # do not draw background 2940 local $self->{bg}; # do not draw background
2878 } 2942 }
2879} 2943}
2880 2944
2881############################################################################# 2945#############################################################################
2882 2946
2883package CFPlus::UI::ExperienceProgress; 2947package DC::UI::ExperienceProgress;
2884 2948
2885our @ISA = CFPlus::UI::Progress::; 2949our @ISA = DC::UI::Progress::;
2886 2950
2887sub new { 2951sub new {
2888 my ($class, %arg) = @_; 2952 my ($class, %arg) = @_;
2889 2953
2954 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2955
2890 my $self = $class->SUPER::new ( 2956 my $self = $class->SUPER::new (
2957 %arg,
2891 tooltip => sub { 2958 tooltip => sub {
2892 my ($self) = @_; 2959 my ($self) = @_;
2893 2960
2894 sprintf "level %d\n%s points\n%s next level\n%s to go", 2961 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2962 $tt,
2895 $self->{lvl}, 2963 $self->{lvl},
2896 ::formsep ($self->{exp}), 2964 ::formsep ($self->{exp}),
2897 ::formsep ($self->{nxt}), 2965 ::formsep ($self->{nxt}),
2898 ::formsep ($self->{nxt} - $self->{exp}), 2966 ::formsep ($self->{nxt} - $self->{exp}),
2967 $self->_percent * 100,
2899 }, 2968 },
2900 %arg
2901 ); 2969 );
2902 2970
2903 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2971 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2904 if $::CONN; 2972 if $::CONN;
2905 2973
2913 if $::CONN; 2981 if $::CONN;
2914 2982
2915 $self->SUPER::DESTROY; 2983 $self->SUPER::DESTROY;
2916} 2984}
2917 2985
2986sub _percent {
2987 my ($self) = @_;
2988
2989 my $table = $::CONN && $::CONN->{exp_table}
2990 or return -1;
2991
2992 my $l0 = $table->[$self->{lvl} - 1];
2993 my $l1 = $table->[$self->{lvl}];
2994
2995 $self->{nxt} = $l1;
2996
2997 ($self->{exp} - $l0) / ($l1 - $l0)
2998}
2999
2918sub set_value { 3000sub set_value {
2919 my ($self, $lvl, $exp) = @_; 3001 my ($self, $lvl, $exp) = @_;
2920 3002
2921 $self->{lvl} = $lvl; 3003 $self->{lvl} = $lvl;
2922 $self->{exp} = $exp; 3004 $self->{exp} = $exp;
2923 3005
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); 3006 $self->SUPER::set_value ($self->_percent);
2936} 3007}
2937 3008
2938############################################################################# 3009#############################################################################
2939 3010
2940package CFPlus::UI::Gauge; 3011package DC::UI::Gauge;
2941 3012
2942our @ISA = CFPlus::UI::VBox::; 3013our @ISA = DC::UI::VBox::;
2943 3014
2944sub new { 3015sub new {
2945 my ($class, %arg) = @_; 3016 my ($class, %arg) = @_;
2946 3017
2947 my $self = $class->SUPER::new ( 3018 my $self = $class->SUPER::new (
2949 can_hover => 1, 3020 can_hover => 1,
2950 can_events => 1, 3021 can_events => 1,
2951 %arg, 3022 %arg,
2952 ); 3023 );
2953 3024
2954 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3025 $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); 3026 $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"); 3027 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2957 3028
2958 $self 3029 $self
2959} 3030}
2960 3031
2961sub set_fontsize { 3032sub set_fontsize {
2982 $self->{value}->set_text ($val); 3053 $self->{value}->set_text ($val);
2983} 3054}
2984 3055
2985############################################################################# 3056#############################################################################
2986 3057
2987package CFPlus::UI::Slider; 3058package DC::UI::Slider;
2988 3059
2989use strict; 3060use common::sense;
2990 3061
2991use CFPlus::OpenGL; 3062use DC::OpenGL;
2992 3063
2993our @ISA = CFPlus::UI::DrawBG::; 3064our @ISA = DC::UI::DrawBG::;
2994 3065
2995my @tex = 3066my @tex =
2996 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3067 map { new_from_resource DC::Texture $_ }
2997 qw(s1_slider.png s1_slider_bg.png); 3068 qw(s1_slider.png s1_slider_bg.png);
2998 3069
2999sub new { 3070sub new {
3000 my $class = shift; 3071 my $class = shift;
3001 3072
3038sub set_value { 3109sub set_value {
3039 my ($self, $value) = @_; 3110 my ($self, $value) = @_;
3040 3111
3041 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3112 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3042 3113
3043 $hi = $lo + 1 if $hi <= $lo; 3114 $hi = $lo if $hi < $lo;
3044 3115
3045 $page = $hi - $lo if $page > $hi - $lo; 3116 $value = $hi - $page if $value > $hi - $page;
3046
3047 $value = $lo if $value < $lo; 3117 $value = $lo if $value < $lo;
3048 $value = $hi - $page if $value > $hi - $page;
3049 3118
3050 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3119 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3051 if $unit; 3120 if $unit;
3052 3121
3053 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3122 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3082 if ($GRAB == $self) { 3151 if ($GRAB == $self) {
3083 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3152 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3084 3153
3085 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3154 my (undef, $lo, $hi, $page) = @{$self->{range}};
3086 3155
3087 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3156 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3088 3157
3089 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3158 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3090 } else { 3159 } else {
3091 return 0; 3160 return 0;
3092 } 3161 }
3097sub invoke_mouse_wheel { 3166sub invoke_mouse_wheel {
3098 my ($self, $ev) = @_; 3167 my ($self, $ev) = @_;
3099 3168
3100 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3169 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3101 3170
3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3171 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3103 3172
3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3173 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3105 3174
3106 1 3175 1
3107} 3176}
3117 my ($self) = @_; 3186 my ($self) = @_;
3118 3187
3119 unless ($self->{knob_w}) { 3188 unless ($self->{knob_w}) {
3120 $self->set_value ($self->{range}[0]); 3189 $self->set_value ($self->{range}[0]);
3121 3190
3122 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3191 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3123 my $range = ($hi - $page - $lo) || 1e-100; 3192 my $range = ($hi - $page - $lo) || 1e-10;
3124 3193
3125 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3194 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3126 3195
3127 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3196 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3128 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3197 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3129 3198
3130 $value = ($value - $lo) / $range; 3199 $value = ($value - $lo) / $range;
3160 glDisable GL_TEXTURE_2D; 3229 glDisable GL_TEXTURE_2D;
3161} 3230}
3162 3231
3163############################################################################# 3232#############################################################################
3164 3233
3165package CFPlus::UI::ValSlider; 3234package DC::UI::ValSlider;
3166 3235
3167our @ISA = CFPlus::UI::HBox::; 3236our @ISA = DC::UI::HBox::;
3168 3237
3169sub new { 3238sub new {
3170 my ($class, %arg) = @_; 3239 my ($class, %arg) = @_;
3171 3240
3172 my $range = delete $arg{range}; 3241 my $range = delete $arg{range};
3173 3242
3174 my $self = $class->SUPER::new ( 3243 my $self = $class->SUPER::new (
3175 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3244 slider => (new DC::UI::Slider expand => 1, range => $range),
3176 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3245 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3177 to_value => sub { shift }, 3246 to_value => sub { shift },
3178 from_value => sub { shift }, 3247 from_value => sub { shift },
3179 %arg, 3248 %arg,
3180 ); 3249 );
3181 3250
3201sub set_range { shift->{slider}->set_range (@_) } 3270sub set_range { shift->{slider}->set_range (@_) }
3202sub set_value { shift->{slider}->set_value (@_) } 3271sub set_value { shift->{slider}->set_value (@_) }
3203 3272
3204############################################################################# 3273#############################################################################
3205 3274
3206package CFPlus::UI::TextScroller; 3275package DC::UI::TextScroller;
3207 3276
3208our @ISA = CFPlus::UI::HBox::; 3277our @ISA = DC::UI::HBox::;
3209 3278
3210use CFPlus::OpenGL; 3279use DC::OpenGL;
3211 3280
3212sub new { 3281sub new {
3213 my $class = shift; 3282 my $class = shift;
3214 3283
3215 my $self = $class->SUPER::new ( 3284 my $self = $class->SUPER::new (
3217 can_events => 1, 3286 can_events => 1,
3218 indent => 0, 3287 indent => 0,
3219 #font => default_font 3288 #font => default_font
3220 @_, 3289 @_,
3221 3290
3222 layout => (new CFPlus::Layout), 3291 layout => (new DC::Layout),
3223 par => [],
3224 max_par => 0, 3292 max_par => 0,
3225 height => 0, 3293 height => 0,
3226 children => [ 3294 children => [
3227 (new CFPlus::UI::Empty expand => 1), 3295 (new DC::UI::Empty expand => 1),
3228 (new CFPlus::UI::Slider vertical => 1), 3296 (new DC::UI::Slider vertical => 1),
3229 ], 3297 ],
3230 ); 3298 );
3231 3299
3232 $self->{children}[1]->connect (changed => sub { $self->update }); 3300 $self->{children}[1]->connect (changed => sub { $self->update });
3301
3302 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3233 3303
3234 $self 3304 $self
3235} 3305}
3236 3306
3237sub set_fontsize { 3307sub set_fontsize {
3289 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3359 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3290 $layout->set_markup ($para->{markup}); 3360 $layout->set_markup ($para->{markup});
3291 3361
3292 $layout->set_shapes ( 3362 $layout->set_shapes (
3293 map 3363 map
3294 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3364 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3295 @{$para->{widget}} 3365 @{$para->{widget}}
3296 ); 3366 );
3297 3367
3298 $layout 3368 $layout
3299} 3369}
3416 $ROOT->on_post_alloc ($self => sub { 3486 $ROOT->on_post_alloc ($self => sub {
3417 $self->force_uptodate; 3487 $self->force_uptodate;
3418 3488
3419 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3489 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3420 3490
3421 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3491 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3422 glClearColor 0, 0, 0, 0; 3492 glClearColor 0, 0, 0, 0;
3423 glClear GL_COLOR_BUFFER_BIT; 3493 glClear GL_COLOR_BUFFER_BIT;
3424 3494
3425 package CFPlus::UI::Base; 3495 package DC::UI::Base;
3426 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3496 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3427 (0, 0, $self->{w}, $self->{h}); 3497 (0, 0, $self->{w}, $self->{h});
3428 3498
3429 my $top = int $self->{children}[1]{range}[0]; 3499 my $top = int $self->{children}[1]{range}[0];
3430 3500
3489 $self->{children}[1]->draw; 3559 $self->{children}[1]->draw;
3490} 3560}
3491 3561
3492############################################################################# 3562#############################################################################
3493 3563
3494package CFPlus::UI::Animator; 3564package DC::UI::Animator;
3495 3565
3496use CFPlus::OpenGL; 3566use DC::OpenGL;
3497 3567
3498our @ISA = CFPlus::UI::Bin::; 3568our @ISA = DC::UI::Bin::;
3499 3569
3500sub moveto { 3570sub moveto {
3501 my ($self, $x, $y) = @_; 3571 my ($self, $x, $y) = @_;
3502 3572
3503 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3573 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3531 glPopMatrix; 3601 glPopMatrix;
3532} 3602}
3533 3603
3534############################################################################# 3604#############################################################################
3535 3605
3536package CFPlus::UI::Flopper; 3606package DC::UI::Flopper;
3537 3607
3538our @ISA = CFPlus::UI::Button::; 3608our @ISA = DC::UI::Button::;
3539 3609
3540sub new { 3610sub new {
3541 my $class = shift; 3611 my $class = shift;
3542 3612
3543 my $self = $class->SUPER::new ( 3613 my $self = $class->SUPER::new (
3555 $self->{other}->toggle_visibility; 3625 $self->{other}->toggle_visibility;
3556} 3626}
3557 3627
3558############################################################################# 3628#############################################################################
3559 3629
3560package CFPlus::UI::Tooltip; 3630package DC::UI::Tooltip;
3561 3631
3562our @ISA = CFPlus::UI::Bin::; 3632our @ISA = DC::UI::Bin::;
3563 3633
3564use CFPlus::OpenGL; 3634use DC::OpenGL;
3565 3635
3566sub new { 3636sub new {
3567 my $class = shift; 3637 my $class = shift;
3568 3638
3569 $class->SUPER::new ( 3639 $class->SUPER::new (
3576 my ($self, $widget) = @_; 3646 my ($self, $widget) = @_;
3577 3647
3578 my $tip = $widget->{tooltip}; 3648 my $tip = $widget->{tooltip};
3579 $tip = $tip->($widget) if "CODE" eq ref $tip; 3649 $tip = $tip->($widget) if "CODE" eq ref $tip;
3580 3650
3581 $tip = CFPlus::Pod::section_label tooltip => $1 3651 $tip = DC::Pod::section_label tooltip => $1
3582 if $tip =~ /^#(.*)$/; 3652 if $tip =~ /^#(.*)$/;
3583 3653
3584 if ($ENV{CFPLUS_DEBUG} & 2) { 3654 if ($ENV{CFPLUS_DEBUG} & 2) {
3585 $tip .= "\n\n" . (ref $widget) . "\n" 3655 $tip .= "\n\n" . (ref $widget) . "\n"
3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3656 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3589 } 3659 }
3590 3660
3591 $tip =~ s/^\n+//; 3661 $tip =~ s/^\n+//;
3592 $tip =~ s/\n+$//; 3662 $tip =~ s/\n+$//;
3593 3663
3594 $self->add (new CFPlus::UI::Label 3664 $self->add (new DC::UI::Label
3665 fg => $DC::THEME{tooltip_fg},
3595 markup => $tip, 3666 markup => $tip,
3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3667 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3668 align => 0,
3597 fontsize => 0.8, 3669 fontsize => 0.8,
3598 style => 1, # FLAG_INVERSE 3670 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3599 ellipsise => 0, 3671 ellipsise => 0,
3600 font => ($widget->{tooltip_font} || $::FONT_PROP), 3672 font => ($widget->{tooltip_font} || $::FONT_PROP),
3601 ); 3673 );
3602} 3674}
3603 3675
3640sub _draw { 3712sub _draw {
3641 my ($self) = @_; 3713 my ($self) = @_;
3642 3714
3643 my ($w, $h) = @$self{qw(w h)}; 3715 my ($w, $h) = @$self{qw(w h)};
3644 3716
3645 glColor 1, 0.8, 0.4; 3717 glColor @{ $DC::THEME{tooltip_bg} };
3646 glRect 0, 0, $w, $h; 3718 glRect 0, 0, $w, $h;
3647 3719
3648 glColor 0, 0, 0; 3720 glColor @{ $DC::THEME{tooltip_border} };
3649 glRect_lineloop .5, .5, $w + .5, $h + .5; 3721 glRect_lineloop .5, .5, $w + .5, $h + .5;
3650 3722
3651 glTranslate 2, 2; 3723 glTranslate 2, 2;
3652 3724
3653 $self->SUPER::_draw; 3725 $self->SUPER::_draw;
3654} 3726}
3655 3727
3656############################################################################# 3728#############################################################################
3657 3729
3658package CFPlus::UI::Face; 3730package DC::UI::Face;
3659 3731
3660our @ISA = CFPlus::UI::DrawBG::; 3732our @ISA = DC::UI::DrawBG::;
3661 3733
3662use CFPlus::OpenGL; 3734use DC::OpenGL;
3663 3735
3664sub new { 3736sub new {
3665 my $class = shift; 3737 my $class = shift;
3666 3738
3667 my $self = $class->SUPER::new ( 3739 my $self = $class->SUPER::new (
3670 aspect => 1, 3742 aspect => 1,
3671 can_events => 0, 3743 can_events => 0,
3672 @_, 3744 @_,
3673 ); 3745 );
3674 3746
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; 3747 $self->update_anim;
3696 }
3697 3748
3698 $self 3749 $self
3699} 3750}
3700 3751
3701sub update_timer { 3752sub update_timer {
3702 my ($self) = @_; 3753 my ($self) = @_;
3703 3754
3704 return unless $self->{timer}; 3755 return unless $self->{timer};
3705 3756
3706 $self->{timer}->stop;
3707 if ($self->{visible}) { 3757 if ($self->{visible}) {
3708 $self->{timer}->set (
3709 $self->{anim_start}
3710 + $self->{animspeed}
3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3712 );
3713 $self->{timer}->start; 3758 $self->{timer}->start;
3759 } else {
3760 $self->{timer}->stop;
3714 } 3761 }
3715} 3762}
3716 3763
3717sub update_face { 3764sub update_face {
3718 my ($self) = @_; 3765 my ($self) = @_;
3728 $tex->upload (sub { $self->reconfigure }); 3775 $tex->upload (sub { $self->reconfigure });
3729 } 3776 }
3730 } 3777 }
3731 } 3778 }
3732 } 3779 }
3780 }
3781}
3782
3783sub update_anim {
3784 my ($self) = @_;
3785
3786 if ($self->{anim} && $self->{animspeed}) {
3787 DC::weaken (my $widget = $self);
3788
3789 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3790 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3791 return unless $::CONN;
3792
3793 my $w = $widget
3794 or return;
3795
3796 ++$w->{frame};
3797 $w->update_face;
3798
3799 # somehow, $widget can go away
3800 $w->update;
3801 $w->update_timer;
3802 };
3803
3804 $self->update_face;
3805 $self->update_timer;
3806 } else {
3807 delete $self->{timer};
3733 } 3808 }
3734} 3809}
3735 3810
3736sub size_request { 3811sub size_request {
3737 my ($self) = @_; 3812 my ($self) = @_;
3759 return unless $self->{visible}; 3834 return unless $self->{visible};
3760 3835
3761 $self->SUPER::update; 3836 $self->SUPER::update;
3762} 3837}
3763 3838
3839sub set_face {
3840 my ($self, $face) = @_;
3841
3842 $self->{face} = $face;
3843 $self->reconfigure;
3844}
3845
3846sub set_anim {
3847 my ($self, $anim) = @_;
3848
3849 $self->{anim} = $anim;
3850 $self->update_anim;
3851}
3852
3853sub set_animspeed {
3854 my ($self, $animspeed) = @_;
3855
3856 $self->{animspeed} = $animspeed;
3857 $self->update_anim;
3858}
3859
3764sub invoke_visibility_change { 3860sub invoke_visibility_change {
3765 my ($self) = @_; 3861 my ($self) = @_;
3766 3862
3767 $self->update_timer; 3863 $self->update_timer;
3768 3864
3792 $self->SUPER::destroy; 3888 $self->SUPER::destroy;
3793} 3889}
3794 3890
3795############################################################################# 3891#############################################################################
3796 3892
3797package CFPlus::UI::Buttonbar; 3893package DC::UI::Buttonbar;
3798 3894
3799our @ISA = CFPlus::UI::HBox::; 3895our @ISA = DC::UI::HBox::;
3800 3896
3801# TODO: should actually wrap buttons and other goodies. 3897# TODO: should actually wrap buttons and other goodies.
3802 3898
3803############################################################################# 3899#############################################################################
3804 3900
3805package CFPlus::UI::Menu; 3901package DC::UI::Menu;
3806 3902
3807our @ISA = CFPlus::UI::Toplevel::; 3903our @ISA = DC::UI::Toplevel::;
3808 3904
3809use CFPlus::OpenGL; 3905use DC::OpenGL;
3810 3906
3811sub new { 3907sub new {
3812 my $class = shift; 3908 my $class = shift;
3813 3909
3814 my $self = $class->SUPER::new ( 3910 my $self = $class->SUPER::new (
3815 items => [], 3911 items => [],
3816 z => 100, 3912 z => 100,
3817 @_, 3913 @_,
3818 ); 3914 );
3819 3915
3820 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3916 $self->add ($self->{vbox} = new DC::UI::VBox);
3821 3917
3822 for my $item (@{ $self->{items} }) { 3918 for my $item (@{ $self->{items} }) {
3823 my ($widget, $cb, $tooltip) = @$item; 3919 my ($widget, $cb, $tooltip) = @$item;
3824 3920
3825 # handle various types of items, only text for now 3921 # handle various types of items, only text for now
3826 if (!ref $widget) { 3922 if (!ref $widget) {
3827 if ($widget =~ /\t/) { 3923 if ($widget =~ /\t/) {
3828 my ($left, $right) = split /\t/, $widget, 2; 3924 my ($left, $right) = split /\t/, $widget, 2;
3829 3925
3830 $widget = new CFPlus::UI::HBox 3926 $widget = new DC::UI::HBox
3831 can_hover => 1, 3927 can_hover => 1,
3832 can_events => 1, 3928 can_events => 1,
3833 tooltip => $tooltip, 3929 tooltip => $tooltip,
3834 children => [ 3930 children => [
3835 (new CFPlus::UI::Label markup => $left, expand => 1), 3931 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3836 (new CFPlus::UI::Label markup => $right, align => +1), 3932 (new DC::UI::Label markup => $right, align => 1),
3837 ], 3933 ],
3838 ; 3934 ;
3839 3935
3840 } else { 3936 } else {
3841 $widget = new CFPlus::UI::Label 3937 $widget = new DC::UI::Label
3842 can_hover => 1, 3938 can_hover => 1,
3843 can_events => 1, 3939 can_events => 1,
3940 align => 0,
3844 markup => $widget, 3941 markup => $widget,
3845 tooltip => $tooltip; 3942 tooltip => $tooltip;
3846 } 3943 }
3847 } 3944 }
3848 3945
3863 # maybe save $GRAB? must be careful about events... 3960 # maybe save $GRAB? must be careful about events...
3864 $GRAB = $self; 3961 $GRAB = $self;
3865 $self->{button} = $ev->{button}; 3962 $self->{button} = $ev->{button};
3866 3963
3867 $self->show; 3964 $self->show;
3868 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3965
3966 my $x = $ev->{x};
3967 my $y = $ev->{y};
3968
3969 $self->{root}->on_post_alloc ($self => sub {
3970 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3971 });
3972
3973 1 # so it can be used inside event handlers
3869} 3974}
3870 3975
3871sub invoke_mouse_motion { 3976sub invoke_mouse_motion {
3872 my ($self, $ev, $x, $y) = @_; 3977 my ($self, $ev, $x, $y) = @_;
3873 3978
3894 1 3999 1
3895} 4000}
3896 4001
3897############################################################################# 4002#############################################################################
3898 4003
3899package CFPlus::UI::Multiplexer; 4004package DC::UI::Multiplexer;
3900 4005
3901our @ISA = CFPlus::UI::Container::; 4006our @ISA = DC::UI::Container::;
3902 4007
3903sub new { 4008sub new {
3904 my $class = shift; 4009 my $class = shift;
3905 4010
3906 my $self = $class->SUPER::new ( 4011 my $self = $class->SUPER::new (
3907 @_, 4012 @_,
3908 ); 4013 );
3909 4014
3910 $self->{current} = $self->{children}[0] 4015 $self->set_current_page (0);
3911 if @{ $self->{children} };
3912 4016
3913 $self 4017 $self
3914} 4018}
3915 4019
3916sub add { 4020sub add {
3917 my ($self, @widgets) = @_; 4021 my ($self, @widgets) = @_;
3918 4022
3919 $self->SUPER::add (@widgets); 4023 $self->SUPER::add (@widgets);
3920 4024
3921 $self->{current} = $self->{children}[0] 4025 $self->set_current_page (0)
3922 if @{ $self->{children} }; 4026 if @widgets == @{ $self->{children} };
3923} 4027}
3924 4028
3925sub get_current_page { 4029sub get_current_page {
3926 my ($self) = @_; 4030 my ($self) = @_;
3927 4031
3933 4037
3934 my $widget = ref $page_or_widget 4038 my $widget = ref $page_or_widget
3935 ? $page_or_widget 4039 ? $page_or_widget
3936 : $self->{children}[$page_or_widget]; 4040 : $self->{children}[$page_or_widget];
3937 4041
4042 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4043
3938 $self->{current} = $widget; 4044 if (($self->{current} = $widget)) {
4045 $self->{current}->set_visible if $self->{current} && $self->{visible};
3939 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4046 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3940 4047
3941 $self->emit (page_changed => $self->{current}); 4048 $self->emit (page_changed => $self->{current});
4049 }
3942 4050
3943 $self->realloc; 4051 $self->realloc;
3944} 4052}
3945 4053
3946sub visible_children { 4054sub visible_children {
3947 $_[0]{current} 4055 $_[0]{current} || ()
3948} 4056}
3949 4057
3950sub size_request { 4058sub size_request {
3951 my ($self) = @_; 4059 my ($self) = @_;
3952 4060
4061 $self->{current}
3953 $self->{current}->size_request 4062 ? $self->{current}->size_request
4063 : (0, 0)
3954} 4064}
3955 4065
3956sub invoke_size_allocate { 4066sub invoke_size_allocate {
3957 my ($self, $w, $h) = @_; 4067 my ($self, $w, $h) = @_;
3958 4068
3959 $self->{current}->configure (0, 0, $w, $h); 4069 $self->{current}->configure (0, 0, $w, $h)
4070 if $self->{current};
3960 4071
3961 1 4072 1
3962} 4073}
3963 4074
3964sub _draw { 4075sub _draw {
3965 my ($self) = @_; 4076 my ($self) = @_;
3966 4077
3967 $self->{current}->draw; 4078 $self->{current}->draw
4079 if $self->{current};
3968} 4080}
3969 4081
3970############################################################################# 4082#############################################################################
3971 4083
3972package CFPlus::UI::Notebook; 4084package DC::UI::Notebook;
3973 4085
3974use CFPlus::OpenGL; 4086use DC::OpenGL;
3975 4087
3976our @ISA = CFPlus::UI::VBox::; 4088our @ISA = DC::UI::VBox::;
3977 4089
3978sub new { 4090sub new {
3979 my $class = shift; 4091 my $class = shift;
3980 4092
3981 my $self = $class->SUPER::new ( 4093 my $self = $class->SUPER::new (
3982 buttonbar => (new CFPlus::UI::Buttonbar), 4094 buttonbar => (new DC::UI::Buttonbar),
3983 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4095 multiplexer => (new DC::UI::Multiplexer expand => 1),
3984 active_outline => [.7, .7, 0.2], 4096 active_outline => [.7, .7, 0.2],
3985 # filter => # will be put between multiplexer and $self 4097 # filter => # will be put between multiplexer and $self
3986 @_, 4098 @_,
3987 ); 4099 );
3988 4100
3997 4109
3998 for my $child (@$widgets) { 4110 for my $child (@$widgets) {
3999 Scalar::Util::weaken $child; 4111 Scalar::Util::weaken $child;
4000 $child->{c_tab_} ||= do { 4112 $child->{c_tab_} ||= do {
4001 my $tab = 4113 my $tab =
4002 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4114 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4003 ? $child->{c_tab} 4115 ? $child->{c_tab}
4004 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4116 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4005 4117
4006 $tab->connect (activate => sub { 4118 $tab->connect (activate => sub {
4007 $wself->set_current_page ($child); 4119 $wself->set_current_page ($child);
4008 }); 4120 });
4009 4121
4039} 4151}
4040 4152
4041sub pages { 4153sub pages {
4042 my ($self) = @_; 4154 my ($self) = @_;
4043 $self->{multiplexer}->children 4155 $self->{multiplexer}->children
4156}
4157
4158sub page_index {
4159 my ($self, $widget) = @_;
4160
4161 my $i = 0;
4162 for ($self->pages) {
4163 if ($_ eq $widget) { return $i };
4164 $i++;
4165 }
4166
4167 undef
4044} 4168}
4045 4169
4046sub add_tab { 4170sub add_tab {
4047 my ($self, $title, $widget, $tooltip) = @_; 4171 my ($self, $title, $widget, $tooltip) = @_;
4048 4172
4085 } 4209 }
4086} 4210}
4087 4211
4088############################################################################# 4212#############################################################################
4089 4213
4090package CFPlus::UI::Selector; 4214package DC::UI::Selector;
4091 4215
4092use utf8; 4216use utf8;
4093 4217
4094our @ISA = CFPlus::UI::Button::; 4218our @ISA = DC::UI::Button::;
4095 4219
4096sub new { 4220sub new {
4097 my $class = shift; 4221 my $class = shift;
4098 4222
4099 my $self = $class->SUPER::new ( 4223 my $self = $class->SUPER::new (
4116 my ($value, $title, $tooltip) = @$_; 4240 my ($value, $title, $tooltip) = @$_;
4117 4241
4118 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4242 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4119 } 4243 }
4120 4244
4121 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4245 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4122} 4246}
4123 4247
4124sub _set_value { 4248sub _set_value {
4125 my ($self, $value) = @_; 4249 my ($self, $value) = @_;
4126 4250
4149 $self->_set_value ($self->{value}); 4273 $self->_set_value ($self->{value});
4150} 4274}
4151 4275
4152############################################################################# 4276#############################################################################
4153 4277
4154package CFPlus::UI::Statusbox; 4278package DC::UI::Statusbox;
4155 4279
4156our @ISA = CFPlus::UI::VBox::; 4280our @ISA = DC::UI::VBox::;
4157 4281
4158sub new { 4282sub new {
4159 my $class = shift; 4283 my $class = shift;
4160 4284
4161 my $self = $class->SUPER::new ( 4285 my $self = $class->SUPER::new (
4162 fontsize => 0.8, 4286 fontsize => 0.8,
4163 @_, 4287 @_,
4164 ); 4288 );
4165 4289
4166 CFPlus::weaken (my $this = $self); 4290 DC::weaken (my $this = $self);
4167 4291
4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4292 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4169 4293
4170 $self 4294 $self
4171} 4295}
4172 4296
4173sub reorder { 4297sub reorder {
4174 my ($self) = @_; 4298 my ($self) = @_;
4175 my $NOW = Time::HiRes::time; 4299 my $NOW = AE::time;
4176 4300
4177 # freeze display when hovering over any label 4301 # freeze display when hovering over any label
4178 return if $CFPlus::UI::TOOLTIP->{owner} 4302 return if $DC::UI::TOOLTIP->{owner}
4179 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4303 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4180 values %{ $self->{item} }; 4304 values %{ $self->{item} };
4181 4305
4182 while (my ($k, $v) = each %{ $self->{item} }) { 4306 while (my ($k, $v) = each %{ $self->{item} }) {
4183 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4307 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4184 } 4308 }
4309
4310 $self->{timer}->set (1, 1);
4185 4311
4186 my @widgets; 4312 my @widgets;
4187 4313
4188 my @items = sort { 4314 my @items = sort {
4189 $a->{pri} <=> $b->{pri} 4315 $a->{pri} <=> $b->{pri}
4203 for ($short) { 4329 for ($short) {
4204 s/^\s+//; 4330 s/^\s+//;
4205 s/\s+/ /g; 4331 s/\s+/ /g;
4206 } 4332 }
4207 4333
4208 new CFPlus::UI::Label 4334 new DC::UI::Label
4209 markup => $short, 4335 markup => $short,
4210 tooltip => $item->{tooltip}, 4336 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4337 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4338 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4339 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4340 max_w => $::WIDTH * 0.44,
4341 align => 0,
4215 fg => [@{ $item->{fg} }], 4342 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4343 can_events => 1,
4217 can_hover => 1 4344 can_hover => 1
4218 }; 4345 };
4219 4346
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4354 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4355 }
4229 4356
4230 push @widgets, $label; 4357 push @widgets, $label;
4231 } 4358 }
4359
4360 my $hash = join ",", @widgets;
4361 return if $hash eq $self->{last_widget_hash};
4362 $self->{last_widget_hash} = $hash;
4232 4363
4233 $self->clear; 4364 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4365 $self->SUPER::add (reverse @widgets);
4235} 4366}
4236 4367
4272 $ROOT->on_refresh (reorder => sub { 4403 $ROOT->on_refresh (reorder => sub {
4273 $self->reorder; 4404 $self->reorder;
4274 }); 4405 });
4275} 4406}
4276 4407
4408sub clr_group {
4409 my ($self, $group) = @_;
4410
4411 if (delete $self->{item}{$group}) {
4412 $ROOT->on_refresh (reorder => sub {
4413 $self->reorder;
4414 });
4415 }
4416}
4417
4277sub reconfigure { 4418sub reconfigure {
4278 my ($self) = @_; 4419 my ($self) = @_;
4279 4420
4280 delete $_->{label} 4421 delete $_->{label}
4281 for values %{ $self->{item} || {} }; 4422 for values %{ $self->{item} || {} };
4292 $self->SUPER::destroy; 4433 $self->SUPER::destroy;
4293} 4434}
4294 4435
4295############################################################################# 4436#############################################################################
4296 4437
4297package CFPlus::UI::Root; 4438package DC::UI::Root;
4298 4439
4299our @ISA = CFPlus::UI::Container::; 4440our @ISA = DC::UI::Container::;
4300 4441
4301use List::Util qw(min max); 4442use List::Util qw(min max);
4302 4443
4303use CFPlus::OpenGL; 4444use DC::OpenGL;
4304 4445
4305sub new { 4446sub new {
4306 my $class = shift; 4447 my $class = shift;
4307 4448
4308 my $self = $class->SUPER::new ( 4449 my $self = $class->SUPER::new (
4309 visible => 1, 4450 visible => 1,
4310 @_, 4451 @_,
4311 ); 4452 );
4312 4453
4313 CFPlus::weaken ($self->{root} = $self); 4454 DC::weaken ($self->{root} = $self);
4314 4455
4315 $self 4456 $self
4316} 4457}
4317 4458
4318sub size_request { 4459sub size_request {
4366} 4507}
4367 4508
4368sub update { 4509sub update {
4369 my ($self) = @_; 4510 my ($self) = @_;
4370 4511
4371 $::WANT_REFRESH->start; 4512 $::WANT_REFRESH = 1;
4372} 4513}
4373 4514
4374sub add { 4515sub add {
4375 my ($self, @children) = @_; 4516 my ($self, @children) = @_;
4376 4517
4443 4584
4444 delete $queue{$widget+0}; 4585 delete $queue{$widget+0};
4445 4586
4446 my ($w, $h) = $widget->size_request; 4587 my ($w, $h) = $widget->size_request;
4447 4588
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4589 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4590 $h += $widget->{padding_y} * 2;
4591
4592 $w = max $widget->{min_w}, $w;
4593 $h = max $widget->{min_h}, $h;
4450 4594
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4595 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4596 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4597
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4598 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4665 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4666 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4667 glLoadIdentity;
4524 4668
4525 { 4669 {
4526 package CFPlus::UI::Base; 4670 package DC::UI::Base;
4527 4671
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4672 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4673 (0, 0, $self->{w}, $self->{h});
4530 4674
4531 $self->_draw; 4675 $self->_draw;
4532 } 4676 }
4533} 4677}
4534 4678
4535############################################################################# 4679#############################################################################
4536 4680
4537package CFPlus::UI; 4681package DC::UI;
4538 4682
4539$ROOT = new CFPlus::UI::Root; 4683$ROOT = new DC::UI::Root;
4540$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4684$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4685
45421 46861
4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines