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.440 by root, Sun Sep 16 18:27:48 2007 UTC vs.
Revision 1.473 by root, Sun Jan 11 03:19:47 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 43 }
43 44
44 $TOOLTIP->hide; 45 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
47}); 48};
48 49
49sub get_layout { 50sub get_layout {
50 my $layout; 51 my $layout;
51 52
52 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
88 89
89 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 92
92 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
93 } 94 }
94} 95}
95 96
96sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
97 my ($ev) = @_; 98 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
104 105
105 $GRAB = $widget; 106 $GRAB = $widget;
106 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
107 108
108 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
109 } 110 }
110 111
111 if ($GRAB) { 112 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 114 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 117
117 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
119 120
120 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 140 $grab->update if $grab;
140 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
141 142
142 check_hover $widget; 143 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
144 } 145 }
145} 146}
146 147
147sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
148 my ($ev) = @_; 149 my ($ev) = @_;
188# call when resolution changes etc. 189# call when resolution changes etc.
189sub rescale_widgets { 190sub rescale_widgets {
190 my ($sx, $sy) = @_; 191 my ($sx, $sy) = @_;
191 192
192 for my $widget (values %WIDGET) { 193 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 194 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 197
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 211 reconfigure_widgets;
211} 212}
212 213
213############################################################################# 214#############################################################################
214 215
215package CFPlus::UI::Event; 216package DC::UI::Event;
216 217
217sub xy { 218sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 220}
220 221
221############################################################################# 222#############################################################################
222 223
223package CFPlus::UI::Base; 224package DC::UI::Base;
224 225
225use strict; 226use strict;
226 227
227use CFPlus::OpenGL; 228use DC::OpenGL;
228 229
229sub new { 230sub new {
230 my $class = shift; 231 my $class = shift;
231 232
232 my $self = bless { 233 my $self = bless {
237 h => undef, 238 h => undef,
238 can_events => 1, 239 can_events => 1,
239 @_ 240 @_
240 }, $class; 241 }, $class;
241 242
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 244
244 for (keys %$self) { 245 for (keys %$self) {
245 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
247 } 248 }
248 } 249 }
249 250
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 256
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 259
259 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
277sub show { 278sub show {
278 my ($self) = @_; 279 my ($self) = @_;
279 280
280 return if $self->{parent}; 281 return if $self->{parent};
281 282
282 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
283} 284}
284 285
285sub set_visible { 286sub set_visible {
286 my ($self) = @_; 287 my ($self) = @_;
287 288
288 return if $self->{visible}; 289 return if $self->{visible};
290
291 $self->{parent} && $self->{parent}{root}#d#
292 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 293
290 $self->{root} = $self->{parent}{root}; 294 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 295 $self->{visible} = $self->{parent}{visible} + 1;
292 296
293 $self->emit (visibility_change => 1); 297 $self->emit (visibility_change => 1);
294 298
295 $self->realloc if !exists $self->{req_w}; 299 $self->realloc if !exists $self->{req_w};
296 300
297 $_->set_visible for $self->children; 301 $_->set_visible for $self->visible_children;
298} 302}
299 303
300sub set_invisible { 304sub set_invisible {
301 my ($self) = @_; 305 my ($self) = @_;
302 306
308 delete $self->{root}; 312 delete $self->{root};
309 313
310 undef $GRAB if $GRAB == $self; 314 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 315 undef $HOVER if $HOVER == $self;
312 316
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 317 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 318 if $TOOLTIP->{owner} == $self;
315 319
316 $self->emit ("focus_out"); 320 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 321 $self->emit (visibility_change => 0);
318} 322}
337sub hide { 341sub hide {
338 my ($self) = @_; 342 my ($self) = @_;
339 343
340 $self->set_invisible; 344 $self->set_invisible;
341 345
346 # extra $parent copy for 5.8.8+ bug workaround
347 # (otherwise $_[0] in remove gets freed
348 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 349 $parent->remove ($self);
343 if $self->{parent}; 350 }
344} 351}
345 352
346sub move_abs { 353sub move_abs {
347 my ($self, $x, $y, $z) = @_; 354 my ($self, $x, $y, $z) = @_;
348 355
364 371
365# traverse the widget chain up to find the maximum "physical" size constraints 372# traverse the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh { 373sub get_max_wh {
367 my ($self) = @_; 374 my ($self) = @_;
368 375
376 my ($w, $h) = @$self{qw(max_w max_h)};
377
378 if ($w <= 0 || $h <= 0) {
379 my ($mw, $mh) = $self->{parent}
369 return $self->{parent}->get_max_wh 380 ? $self->{parent}->get_max_wh
370 if $self->{parent};
371
372 ($::WIDTH, $::HEIGHT) 381 : ($::WIDTH, $::HEIGHT);
382
383 $w = $mw if $w <= 0;
384 $h = $mh if $h <= 0;
385 }
386
387 ($w, $h)
373} 388}
374 389
375sub size_request { 390sub size_request {
376 require Carp; 391 require Carp;
377 Carp::confess "size_request is abstract"; 392 Carp::confess "size_request is abstract";
385 my ($self, $x, $y, $w, $h) = @_; 400 my ($self, $x, $y, $w, $h) = @_;
386 401
387 if ($self->{aspect}) { 402 if ($self->{aspect}) {
388 my ($ow, $oh) = ($w, $h); 403 my ($ow, $oh) = ($w, $h);
389 404
390 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 405 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
391 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 406 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
392 407
393 # use alignment to adjust x, y 408 # use alignment to adjust x, y
394 409
395 $x += int 0.5 * ($ow - $w); 410 $x += int 0.5 * ($ow - $w);
396 $y += int 0.5 * ($oh - $h); 411 $y += int 0.5 * ($oh - $h);
437 452
438 return if $self->{tooltip} eq $tooltip; 453 return if $self->{tooltip} eq $tooltip;
439 454
440 $self->{tooltip} = $tooltip; 455 $self->{tooltip} = $tooltip;
441 456
442 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 457 if ($DC::UI::TOOLTIP->{owner} == $self) {
443 delete $CFPlus::UI::TOOLTIP->{owner}; 458 delete $DC::UI::TOOLTIP->{owner};
444 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 459 $DC::UI::TOOLTIP_WATCHER->invoke;
445 } 460 }
446} 461}
447 462
448# translate global coordinates to local coordinate system 463# translate global coordinates to local coordinate system
449sub coord2local { 464sub coord2local {
450 my ($self, $x, $y) = @_; 465 my ($self, $x, $y) = @_;
451 466
452 Carp::confess unless $self->{parent};#d# 467 return (undef, undef) unless $self->{parent};
453 468
454 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 469 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
455} 470}
456 471
457# translate local coordinates to global coordinate system 472# translate local coordinates to global coordinate system
458sub coord2global { 473sub coord2global {
459 my ($self, $x, $y) = @_; 474 my ($self, $x, $y) = @_;
460 475
461 Carp::confess unless $self->{parent};#d# 476 return (undef, undef) unless $self->{parent};
462 477
463 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 478 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
464} 479}
465 480
466sub invoke_focus_in { 481sub invoke_focus_in {
515sub connect { 530sub connect {
516 my ($self, $signal, $cb) = @_; 531 my ($self, $signal, $cb) = @_;
517 532
518 push @{ $self->{signal_cb}{$signal} }, $cb; 533 push @{ $self->{signal_cb}{$signal} }, $cb;
519 534
520 defined wantarray and CFPlus::guard { 535 defined wantarray and DC::guard {
521 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
522 @{ $self->{signal_cb}{$signal} }; 537 @{ $self->{signal_cb}{$signal} };
523 } 538 }
524} 539}
525 540
563# in .xs 578# in .xs
564 579
565sub set_parent { 580sub set_parent {
566 my ($self, $parent) = @_; 581 my ($self, $parent) = @_;
567 582
568 CFPlus::weaken ($self->{parent} = $parent); 583 DC::weaken ($self->{parent} = $parent);
569 $self->set_visible if $parent->{visible}; 584 $self->set_visible if $parent->{visible};
570} 585}
571 586
572sub realloc { 587sub realloc {
573 my ($self) = @_; 588 my ($self) = @_;
611} 626}
612 627
613sub DESTROY { 628sub DESTROY {
614 my ($self) = @_; 629 my ($self) = @_;
615 630
616 return if CFPlus::in_destruct; 631 return if DC::in_destruct;
617 632
618 local $@; 633 local $@;
619 eval { $self->destroy }; 634 eval { $self->destroy };
620 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 635 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
621 636
622 delete $WIDGET{$self+0}; 637 delete $WIDGET{$self+0};
623} 638}
624 639
625############################################################################# 640#############################################################################
626 641
627package CFPlus::UI::DrawBG; 642package DC::UI::DrawBG;
628 643
629our @ISA = CFPlus::UI::Base::; 644our @ISA = DC::UI::Base::;
630 645
631use strict; 646use strict;
632use CFPlus::OpenGL; 647use DC::OpenGL;
633 648
634sub new { 649sub new {
635 my $class = shift; 650 my $class = shift;
636 651
637 $class->SUPER::new ( 652 $class->SUPER::new (
659 } 674 }
660} 675}
661 676
662############################################################################# 677#############################################################################
663 678
664package CFPlus::UI::Empty; 679package DC::UI::Empty;
665 680
666our @ISA = CFPlus::UI::Base::; 681our @ISA = DC::UI::Base::;
667 682
668sub new { 683sub new {
669 my ($class, %arg) = @_; 684 my ($class, %arg) = @_;
670 $class->SUPER::new (can_events => 0, %arg); 685 $class->SUPER::new (can_events => 0, %arg);
671} 686}
678 693
679sub draw { } 694sub draw { }
680 695
681############################################################################# 696#############################################################################
682 697
683package CFPlus::UI::Container; 698package DC::UI::Container;
684 699
685our @ISA = CFPlus::UI::Base::; 700our @ISA = DC::UI::Base::;
686 701
687sub new { 702sub new {
688 my ($class, %arg) = @_; 703 my ($class, %arg) = @_;
689 704
690 my $children = delete $arg{children}; 705 my $children = delete $arg{children};
784 $_->draw for $self->visible_children; 799 $_->draw for $self->visible_children;
785} 800}
786 801
787############################################################################# 802#############################################################################
788 803
789package CFPlus::UI::Bin; 804package DC::UI::Bin;
790 805
791our @ISA = CFPlus::UI::Container::; 806our @ISA = DC::UI::Container::;
792 807
793sub new { 808sub new {
794 my ($class, %arg) = @_; 809 my ($class, %arg) = @_;
795 810
796 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 811 my $child = (delete $arg{child}) || new DC::UI::Empty::;
797 812
798 $class->SUPER::new (children => [$child], %arg) 813 $class->SUPER::new (children => [$child], %arg)
799} 814}
800 815
801sub add { 816sub add {
808sub remove { 823sub remove {
809 my ($self, $widget) = @_; 824 my ($self, $widget) = @_;
810 825
811 $self->SUPER::remove ($widget); 826 $self->SUPER::remove ($widget);
812 827
813 $self->{children} = [new CFPlus::UI::Empty] 828 $self->{children} = [new DC::UI::Empty]
814 unless @{$self->{children}}; 829 unless @{$self->{children}};
815} 830}
816 831
817sub child { $_[0]->{children}[0] } 832sub child { $_[0]->{children}[0] }
818 833
829} 844}
830 845
831############################################################################# 846#############################################################################
832# back-buffered drawing area 847# back-buffered drawing area
833 848
834package CFPlus::UI::Window; 849package DC::UI::Window;
835 850
836our @ISA = CFPlus::UI::Bin::; 851our @ISA = DC::UI::Bin::;
837 852
838use CFPlus::OpenGL; 853use DC::OpenGL;
839 854
840sub new { 855sub new {
841 my ($class, %arg) = @_; 856 my ($class, %arg) = @_;
842 857
843 my $self = $class->SUPER::new (%arg); 858 my $self = $class->SUPER::new (%arg);
865} 880}
866 881
867sub render_child { 882sub render_child {
868 my ($self) = @_; 883 my ($self) = @_;
869 884
870 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 885 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
871 glClearColor 0, 0, 0, 0; 886 glClearColor 0, 0, 0, 0;
872 glClear GL_COLOR_BUFFER_BIT; 887 glClear GL_COLOR_BUFFER_BIT;
873 888
874 { 889 {
875 package CFPlus::UI::Base; 890 package DC::UI::Base;
876 891
877 local ($draw_x, $draw_y, $draw_w, $draw_h) = 892 local ($draw_x, $draw_y, $draw_w, $draw_h) =
878 (0, 0, $self->{w}, $self->{h}); 893 (0, 0, $self->{w}, $self->{h});
879 894
880 $self->_render; 895 $self->_render;
897 glDisable GL_TEXTURE_2D; 912 glDisable GL_TEXTURE_2D;
898} 913}
899 914
900############################################################################# 915#############################################################################
901 916
902package CFPlus::UI::ViewPort; 917package DC::UI::ViewPort;
903 918
904use List::Util qw(min max); 919use List::Util qw(min max);
905 920
906our @ISA = CFPlus::UI::Window::; 921our @ISA = DC::UI::Window::;
907 922
908sub new { 923sub new {
909 my $class = shift; 924 my $class = shift;
910 925
911 $class->SUPER::new ( 926 $class->SUPER::new (
995 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
996 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
997 ) { 1012 ) {
998 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1013 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
999 } else { 1014 } else {
1000 $self->CFPlus::UI::Base::find_widget ($x, $y) 1015 $self->DC::UI::Base::find_widget ($x, $y)
1001 } 1016 }
1002} 1017}
1003 1018
1004sub _render { 1019sub _render {
1005 my ($self) = @_; 1020 my ($self) = @_;
1006 1021
1007 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1022 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1008 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1023 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1009 1024
1010 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1025 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1011 1026
1012 $self->SUPER::_render; 1027 $self->SUPER::_render;
1013} 1028}
1014 1029
1015############################################################################# 1030#############################################################################
1016 1031
1017package CFPlus::UI::ScrolledWindow; 1032package DC::UI::ScrolledWindow;
1018 1033
1019our @ISA = CFPlus::UI::Table::; 1034our @ISA = DC::UI::Table::;
1020 1035
1021sub new { 1036sub new {
1022 my ($class, %arg) = @_; 1037 my ($class, %arg) = @_;
1023 1038
1024 my $child = delete $arg{child}; 1039 my $child = delete $arg{child};
1025 1040
1026 my $self; 1041 my $self;
1027 1042
1028 my $hslider = new CFPlus::UI::Slider 1043 my $hslider = new DC::UI::Slider
1029 c_col => 0, 1044 c_col => 0,
1030 c_row => 1, 1045 c_row => 1,
1031 vertical => 0, 1046 vertical => 0,
1032 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1033 on_changed => sub { 1048 on_changed => sub {
1034 $self->{hpos} = $_[1]; 1049 $self->{hpos} = $_[1];
1035 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1036 }, 1051 },
1037 ; 1052 ;
1038 1053
1039 my $vslider = new CFPlus::UI::Slider 1054 my $vslider = new DC::UI::Slider
1040 c_col => 1, 1055 c_col => 1,
1041 c_row => 0, 1056 c_row => 0,
1042 vertical => 1, 1057 vertical => 1,
1043 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1044 on_changed => sub { 1059 on_changed => sub {
1056 col_expand => [1, 0], 1071 col_expand => [1, 0],
1057 row_expand => [1, 0], 1072 row_expand => [1, 0],
1058 %arg, 1073 %arg,
1059 ); 1074 );
1060 1075
1061 $self->{vp} = new CFPlus::UI::ViewPort 1076 $self->{vp} = new DC::UI::ViewPort
1062 c_col => 0, 1077 c_col => 0,
1063 c_row => 0, 1078 c_row => 0,
1064 expand => 1, 1079 expand => 1,
1065 scroll_x => $self->{scroll_x}, 1080 scroll_x => $self->{scroll_x},
1066 scroll_y => $self->{scroll_y}, 1081 scroll_y => $self->{scroll_y},
1192 $self->SUPER::invoke_size_allocate ($w, $h) 1207 $self->SUPER::invoke_size_allocate ($w, $h)
1193} 1208}
1194 1209
1195############################################################################# 1210#############################################################################
1196 1211
1197package CFPlus::UI::Frame; 1212package DC::UI::Frame;
1198 1213
1199our @ISA = CFPlus::UI::Bin::; 1214our @ISA = DC::UI::Bin::;
1200 1215
1201use CFPlus::OpenGL; 1216use DC::OpenGL;
1202 1217
1203sub new { 1218sub new {
1204 my $class = shift; 1219 my $class = shift;
1205 1220
1206 $class->SUPER::new ( 1221 $class->SUPER::new (
1225 $self->SUPER::_draw; 1240 $self->SUPER::_draw;
1226} 1241}
1227 1242
1228############################################################################# 1243#############################################################################
1229 1244
1230package CFPlus::UI::FancyFrame; 1245package DC::UI::FancyFrame;
1231 1246
1232our @ISA = CFPlus::UI::Bin::; 1247our @ISA = DC::UI::Bin::;
1233 1248
1234use CFPlus::OpenGL; 1249use DC::OpenGL;
1235 1250
1236sub new { 1251sub new {
1237 my ($class, %arg) = @_; 1252 my ($class, %arg) = @_;
1238 1253
1239 if ((exists $arg{label}) && !ref $arg{label}) { 1254 if ((exists $arg{label}) && !ref $arg{label}) {
1240 $arg{label} = new CFPlus::UI::Label 1255 $arg{label} = new DC::UI::Label
1241 align => 1, 1256 align => 1,
1242 valign => 0, 1257 valign => 0.5,
1243 text => $arg{label}, 1258 text => $arg{label},
1244 fontsize => ($arg{border} || 0.8) * 0.75; 1259 fontsize => ($arg{border} || 0.8) * 0.75;
1245 } 1260 }
1246 1261
1247 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1248 # label => "", 1263 # label => "",
1249 fg => [0.6, 0.3, 0.1], 1264 fg => undef,
1250 border => 0.8, 1265 border => 0.8,
1251 style => 'single', 1266 style => 'single',
1252 %arg, 1267 %arg,
1253 ); 1268 );
1254 1269
1257 1272
1258sub add { 1273sub add {
1259 my ($self, @widgets) = @_; 1274 my ($self, @widgets) = @_;
1260 1275
1261 $self->SUPER::add (@widgets); 1276 $self->SUPER::add (@widgets);
1262 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1277 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1263} 1278}
1264 1279
1265sub border { 1280sub border {
1266 int $_[0]{border} * $::FONTSIZE 1281 int $_[0]{border} * $::FONTSIZE
1267} 1282}
1307 my $border = $self->border; 1322 my $border = $self->border;
1308 my ($w, $h) = ($self->{w}, $self->{h}); 1323 my ($w, $h) = ($self->{w}, $self->{h});
1309 1324
1310 $child->draw; 1325 $child->draw;
1311 1326
1312 glColor @{$self->{fg}}; 1327 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1313 glBegin GL_LINE_STRIP; 1328 glBegin GL_LINE_STRIP;
1314 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1329 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1315 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1330 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1331 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1317 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1332 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1325 } 1340 }
1326} 1341}
1327 1342
1328############################################################################# 1343#############################################################################
1329 1344
1330package CFPlus::UI::Toplevel; 1345package DC::UI::Toplevel;
1331 1346
1332our @ISA = CFPlus::UI::Bin::; 1347our @ISA = DC::UI::Bin::;
1333 1348
1334use CFPlus::OpenGL; 1349use DC::OpenGL;
1335 1350
1336my $bg = 1351my $bg =
1337 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1352 new_from_resource DC::Texture "d1_bg.png",
1338 mipmap => 1, wrap => 1; 1353 mipmap => 1, wrap => 1;
1339 1354
1340my @border = 1355my @border =
1341 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1356 map { new_from_resource DC::Texture $_, mipmap => 1 }
1342 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1357 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1343 1358
1344my @icon = 1359my @icon =
1345 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1360 map { new_from_resource DC::Texture $_, mipmap => 1 }
1346 qw(x1_move.png x1_resize.png); 1361 qw(x1_move.png x1_resize.png);
1347 1362
1348sub new { 1363sub new {
1349 my ($class, %arg) = @_; 1364 my ($class, %arg) = @_;
1350 1365
1351 my $self = $class->SUPER::new ( 1366 my $self = $class->SUPER::new (
1352 bg => [1, 1, 1, 1], 1367 bg => [1, 1, 1, 1],
1353 border_bg => [1, 1, 1, 1], 1368 border_bg => [1, 1, 1, 1],
1354 border => 0.6, 1369 border => 0.8,
1355 can_events => 1, 1370 can_events => 1,
1356 min_w => 64, 1371 min_w => 64,
1357 min_h => 32, 1372 min_h => 32,
1358 %arg, 1373 %arg,
1359 ); 1374 );
1360 1375
1361 $self->{title_widget} = new CFPlus::UI::Label 1376 $self->{title_widget} = new DC::UI::Label
1362 align => 0, 1377 align => 0.5,
1363 valign => 1, 1378 valign => 1,
1364 text => $self->{title}, 1379 text => $self->{title},
1365 fontsize => $self->{border}, 1380 fontsize => $self->{border},
1366 if exists $self->{title}; 1381 if exists $self->{title};
1367 1382
1368 if ($self->{has_close_button}) { 1383 if ($self->{has_close_button}) {
1369 $self->{close_button} = 1384 $self->{close_button} =
1370 new CFPlus::UI::ImageButton 1385 new DC::UI::ImageButton
1371 path => 'x1_close.png', 1386 path => 'x1_close.png',
1372 on_activate => sub { $self->emit ("delete") }; 1387 on_activate => sub { $self->emit ("delete") };
1373 1388
1374 $self->CFPlus::UI::Container::add ($self->{close_button}); 1389 $self->DC::UI::Container::add ($self->{close_button});
1375 } 1390 }
1376 1391
1377 $self 1392 $self
1378} 1393}
1379 1394
1380sub add { 1395sub add {
1381 my ($self, @widgets) = @_; 1396 my ($self, @widgets) = @_;
1382 1397
1383 $self->SUPER::add (@widgets); 1398 $self->SUPER::add (@widgets);
1384 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1399 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1385 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1400 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1386} 1401}
1387 1402
1388sub border { 1403sub border {
1389 int $_[0]{border} * $::FONTSIZE 1404 int $_[0]{border} * $::FONTSIZE
1390} 1405}
1528 glEnable GL_TEXTURE_2D; 1543 glEnable GL_TEXTURE_2D;
1529 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1544 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1530 1545
1531 my $border = $self->border; 1546 my $border = $self->border;
1532 1547
1548 if ($border) {
1533 glColor @{ $self->{border_bg} }; 1549 glColor @{ $self->{border_bg} };
1534 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1550 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1535 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1551 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1536 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1552 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1537 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1553 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1538 1554
1539 # move 1555 # move
1540 my $w2 = ($w - $border) * .5; 1556 my $w2 = ($w - $border) * .5;
1541 my $h2 = ($h - $border) * .5; 1557 my $h2 = ($h - $border) * .5;
1542 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1558 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1543 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1559 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1560 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1545 1561
1546 # resize 1562 # resize
1547 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1563 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1548 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1564 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1549 unless $self->{has_close_button}; 1565 unless $self->{has_close_button};
1550 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1566 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1551 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1567 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1568 }
1552 1569
1553 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1570 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1554 glColor @{ $self->{bg} }; 1571 glColor @{ $self->{bg} };
1555 1572
1556 # TODO: repeat texture not scale 1573 # TODO: repeat texture not scale
1575 if $self->{close_button}; 1592 if $self->{close_button};
1576} 1593}
1577 1594
1578############################################################################# 1595#############################################################################
1579 1596
1580package CFPlus::UI::Table; 1597package DC::UI::Table;
1581 1598
1582our @ISA = CFPlus::UI::Container::; 1599our @ISA = DC::UI::Container::;
1583 1600
1584use List::Util qw(max sum); 1601use List::Util qw(max sum);
1585 1602
1586use CFPlus::OpenGL; 1603use DC::OpenGL;
1587 1604
1588sub new { 1605sub new {
1589 my $class = shift; 1606 my $class = shift;
1590 1607
1591 $class->SUPER::new ( 1608 $class->SUPER::new (
1635 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1652 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1636 1653
1637 my $sw = sum @w[$c .. $c + $cs - 1]; 1654 my $sw = sum @w[$c .. $c + $cs - 1];
1638 1655
1639 if ($w > $sw) { 1656 if ($w > $sw) {
1640 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1657 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1641 } 1658 }
1642 } 1659 }
1643 1660
1644 # second pass, rows 1661 # second pass, rows
1645 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1662 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1646 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1663 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1647 1664
1648 my $sh = sum @h[$r .. $r + $rs - 1]; 1665 my $sh = sum @h[$r .. $r + $rs - 1];
1649 1666
1650 if ($h > $sh) { 1667 if ($h > $sh) {
1651 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1668 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1652 } 1669 }
1653 } 1670 }
1654 1671
1655 (\@w, \@h) 1672 (\@w, \@h)
1656} 1673}
1679 @col_expand = (1) x @$ws unless @col_expand; 1696 @col_expand = (1) x @$ws unless @col_expand;
1680 my $col_expand = (sum @col_expand) || 1; 1697 my $col_expand = (sum @col_expand) || 1;
1681 1698
1682 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1699 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1683 1700
1684 CFPlus::UI::harmonize $ws; 1701 DC::UI::harmonize $ws;
1685 1702
1686 my @row_expand = @{$self->{row_expand}}; 1703 my @row_expand = @{$self->{row_expand}};
1687 @row_expand = (1) x @$ws unless @row_expand; 1704 @row_expand = (1) x @$ws unless @row_expand;
1688 my $row_expand = (sum @row_expand) || 1; 1705 my $row_expand = (sum @row_expand) || 1;
1689 1706
1690 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1707 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1691 1708
1692 CFPlus::UI::harmonize $hs; 1709 DC::UI::harmonize $hs;
1693 1710
1694 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1711 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1695 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1712 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1696 1713
1697 for my $widget ($self->children) { 1714 for my $widget ($self->children) {
1706 1 1723 1
1707} 1724}
1708 1725
1709############################################################################# 1726#############################################################################
1710 1727
1711package CFPlus::UI::Fixed; 1728package DC::UI::Fixed;
1712 1729
1713use List::Util qw(min max); 1730use List::Util qw(min max);
1714 1731
1715our @ISA = CFPlus::UI::Container::; 1732our @ISA = DC::UI::Container::;
1716 1733
1717sub _scale($$$) { 1734sub _scale($$$) {
1718 my ($rel, $val, $max) = @_; 1735 my ($rel, $val, $max) = @_;
1719 1736
1720 $rel ? $val * $max : $val 1737 $rel ? $val * $max : $val
1772 1 1789 1
1773} 1790}
1774 1791
1775############################################################################# 1792#############################################################################
1776 1793
1777package CFPlus::UI::Box; 1794package DC::UI::Box;
1778 1795
1779our @ISA = CFPlus::UI::Container::; 1796our @ISA = DC::UI::Container::;
1780 1797
1781sub size_request { 1798sub size_request {
1782 my ($self) = @_; 1799 my ($self) = @_;
1783 1800
1784 my @children = $self->visible_children; 1801 my @children = $self->visible_children;
1819 $req[$_] += $space * $children[$_]{expand} 1836 $req[$_] += $space * $children[$_]{expand}
1820 for 0 .. $#children; 1837 for 0 .. $#children;
1821 } 1838 }
1822 } 1839 }
1823 1840
1824 CFPlus::UI::harmonize \@req; 1841 DC::UI::harmonize \@req;
1825 1842
1826 my $pos = 0; 1843 my $pos = 0;
1827 for (0 .. $#children) { 1844 for (0 .. $#children) {
1828 my $alloc = $req[$_]; 1845 my $alloc = $req[$_];
1829 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1846 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1834 1 1851 1
1835} 1852}
1836 1853
1837############################################################################# 1854#############################################################################
1838 1855
1839package CFPlus::UI::HBox; 1856package DC::UI::HBox;
1840 1857
1841our @ISA = CFPlus::UI::Box::; 1858our @ISA = DC::UI::Box::;
1842 1859
1843sub new { 1860sub new {
1844 my $class = shift; 1861 my $class = shift;
1845 1862
1846 $class->SUPER::new ( 1863 $class->SUPER::new (
1849 ) 1866 )
1850} 1867}
1851 1868
1852############################################################################# 1869#############################################################################
1853 1870
1854package CFPlus::UI::VBox; 1871package DC::UI::VBox;
1855 1872
1856our @ISA = CFPlus::UI::Box::; 1873our @ISA = DC::UI::Box::;
1857 1874
1858sub new { 1875sub new {
1859 my $class = shift; 1876 my $class = shift;
1860 1877
1861 $class->SUPER::new ( 1878 $class->SUPER::new (
1864 ) 1881 )
1865} 1882}
1866 1883
1867############################################################################# 1884#############################################################################
1868 1885
1869package CFPlus::UI::Label; 1886package DC::UI::Label;
1870 1887
1871our @ISA = CFPlus::UI::DrawBG::; 1888our @ISA = DC::UI::DrawBG::;
1872 1889
1873use CFPlus::OpenGL; 1890use DC::OpenGL;
1874 1891
1875sub new { 1892sub new {
1876 my ($class, %arg) = @_; 1893 my ($class, %arg) = @_;
1877 1894
1878 my $self = $class->SUPER::new ( 1895 my $self = $class->SUPER::new (
1883 #text => initial text 1900 #text => initial text
1884 #markup => initial narkup 1901 #markup => initial narkup
1885 #max_w => maximum pixel width 1902 #max_w => maximum pixel width
1886 #style => 0, # render flags 1903 #style => 0, # render flags
1887 ellipsise => 3, # end 1904 ellipsise => 3, # end
1888 layout => (new CFPlus::Layout), 1905 layout => (new DC::Layout),
1889 fontsize => 1, 1906 fontsize => 1,
1890 align => -1, 1907 align => 0.5,
1891 valign => -1, 1908 valign => 0.5,
1892 padding_x => 2, 1909 padding_x => 4,
1893 padding_y => 2, 1910 padding_y => 2,
1894 can_events => 0, 1911 can_events => 0,
1895 %arg 1912 %arg
1896 ); 1913 );
1897 1914
1898 if (exists $self->{template}) { 1915 if (exists $self->{template}) {
1899 my $layout = new CFPlus::Layout; 1916 my $layout = new DC::Layout;
1900 $layout->set_text (delete $self->{template}); 1917 $layout->set_text (delete $self->{template});
1901 $self->{template} = $layout; 1918 $self->{template} = $layout;
1902 } 1919 }
1903 1920
1904 if (exists $self->{markup}) { 1921 if (exists $self->{markup}) {
1963 1980
1964 $self->{size_req} ||= do { 1981 $self->{size_req} ||= do {
1965 my ($max_w, $max_h) = $self->get_max_wh; 1982 my ($max_w, $max_h) = $self->get_max_wh;
1966 1983
1967 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1984 $self->{layout}->set_font ($self->{font}) if $self->{font};
1968 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1985 $self->{layout}->set_width ($max_w);
1969 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1986 $self->{layout}->set_ellipsise ($self->{ellipsise});
1970 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1971 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1972 1989
1973 my ($w, $h) = $self->{layout}->size; 1990 my ($w, $h) = $self->{layout}->size;
1974 1991
1975 if (exists $self->{template}) { 1992 if (exists $self->{template}) {
1976 $self->{template}->set_font ($self->{font}) if $self->{font}; 1993 $self->{template}->set_font ($self->{font}) if $self->{font};
1977 $self->{template}->set_width ($self->{max_w} || -1); 1994 $self->{template}->set_width ($max_w);
1978 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1995 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1979 1996
1980 my ($w2, $h2) = $self->{template}->size; 1997 my ($w2, $h2) = $self->{template}->size;
1981 1998
1982 $w = List::Util::max $w, $w2; 1999 $w = List::Util::max $w, $w2;
2038 2055
2039 [$self->{layout}->size] 2056 [$self->{layout}->size]
2040 }; 2057 };
2041 2058
2042 unless (exists $self->{ox}) { 2059 unless (exists $self->{ox}) {
2043 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2060 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2044 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2061 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2045 : ($self->{w} - $size->[0]) * 0.5);
2046
2047 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2048 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2049 : ($self->{h} - $size->[1]) * 0.5);
2050 2062
2051 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2063 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052 }; 2064 };
2053 2065
2054# unless ($self->{list}) { 2066# unless ($self->{list}) {
2055# $self->{list} = CFPlus::OpenGL::glGenList; 2067# $self->{list} = DC::OpenGL::glGenList;
2056# CFPlus::OpenGL::glNewList $self->{list}; 2068# DC::OpenGL::glNewList $self->{list};
2057# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2069# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2058# CFPlus::OpenGL::glEndList; 2070# DC::OpenGL::glEndList;
2059# } 2071# }
2060# 2072#
2061# CFPlus::OpenGL::glCallList $self->{list}; 2073# DC::OpenGL::glCallList $self->{list};
2062 2074
2063 $self->{layout}->draw; 2075 $self->{layout}->draw;
2064} 2076}
2065 2077
2066#sub destroy { 2078#sub destroy {
2067# my ($self) = @_; 2079# my ($self) = @_;
2068# 2080#
2069# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2081# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2070# 2082#
2071# $self->SUPER::destroy; 2083# $self->SUPER::destroy;
2072#} 2084#}
2073 2085
2074############################################################################# 2086#############################################################################
2075 2087
2076package CFPlus::UI::EntryBase; 2088package DC::UI::EntryBase;
2077 2089
2078our @ISA = CFPlus::UI::Label::; 2090our @ISA = DC::UI::Label::;
2079 2091
2080use CFPlus::OpenGL; 2092use DC::OpenGL;
2081 2093
2082sub new { 2094sub new {
2083 my $class = shift; 2095 my $class = shift;
2084 2096
2085 $class->SUPER::new ( 2097 $class->SUPER::new (
2086 fg => [1, 1, 1], 2098 fg => [1, 1, 1],
2087 bg => [0, 0, 0, 0.2], 2099 bg => [0, 0, 0, 0.2],
2088 outline => [0.6, 0.3, 0.1], 2100 outline => undef,
2089 active_bg => [0, 0, 1, .2], 2101 active_bg => [0, 0, 1, .2],
2090 active_fg => [1, 1, 1], 2102 active_fg => [1, 1, 1],
2091 active_outline => [1, 1, 0], 2103 active_outline => [1, 1, 0],
2092 can_hover => 1, 2104 can_hover => 1,
2093 can_focus => 1, 2105 can_focus => 1,
2106 align => 0,
2094 valign => 0, 2107 valign => 0.5,
2095 can_events => 1, 2108 can_events => 1,
2096 ellipsise => 0, 2109 ellipsise => 0,
2110 padding_x => 4,
2111 padding_y => 2,
2097 #text => ... 2112 #text => ...
2098 #hidden => "*", 2113 #hidden => "*",
2099 @_ 2114 @_
2100 ) 2115 )
2101} 2116}
2148 2163
2149 my $text = $self->get_text; 2164 my $text = $self->get_text;
2150 2165
2151 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2166 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2152 2167
2153 if ($uni == 8) { 2168 if ($sym == DC::SDLK_BACKSPACE) {
2154 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2169 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2155 } elsif ($uni == 127) { 2170 } elsif ($sym == DC::SDLK_DELETE) {
2156 substr $text, $self->{cursor}, 1, ""; 2171 substr $text, $self->{cursor}, 1, "";
2157 } elsif ($sym == CFPlus::SDLK_LEFT) { 2172 } elsif ($sym == DC::SDLK_LEFT) {
2158 --$self->{cursor} if $self->{cursor}; 2173 --$self->{cursor} if $self->{cursor};
2159 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2174 } elsif ($sym == DC::SDLK_RIGHT) {
2160 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2175 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2161 } elsif ($sym == CFPlus::SDLK_HOME) { 2176 } elsif ($sym == DC::SDLK_HOME) {
2162 # what a hack 2177 # what a hack
2163 $self->{cursor} = 2178 $self->{cursor} =
2164 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2179 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2165 ? length $1 2180 ? length $1
2166 : 0; 2181 : 0;
2167 } elsif ($sym == CFPlus::SDLK_END) { 2182 } elsif ($sym == DC::SDLK_END) {
2168 # uh, again 2183 # uh, again
2169 $self->{cursor} = 2184 $self->{cursor} =
2170 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2185 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2171 ? $self->{cursor} + length $1 2186 ? $self->{cursor} + length $1
2172 : length $self->{text}; 2187 : length $self->{text};
2263 glColor @{$self->{active_outline}}; 2278 glColor @{$self->{active_outline}};
2264 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2279 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2265 glLineWidth 1; 2280 glLineWidth 1;
2266 2281
2267 } else { 2282 } else {
2268 glColor @{$self->{outline}}; 2283 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2269 glBegin GL_LINE_STRIP; 2284 glBegin GL_LINE_STRIP;
2270 glVertex .5, $self->{h} * .5; 2285 glVertex .5, $self->{h} * .5;
2271 glVertex .5, $self->{h} - 2.5; 2286 glVertex .5, $self->{h} - 2.5;
2272 glVertex $self->{w} - .5, $self->{h} - 2.5; 2287 glVertex $self->{w} - .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} * .5; 2288 glVertex $self->{w} - .5, $self->{h} * .5;
2275 } 2290 }
2276} 2291}
2277 2292
2278############################################################################# 2293#############################################################################
2279 2294
2280package CFPlus::UI::Entry; 2295package DC::UI::Entry;
2281 2296
2282our @ISA = CFPlus::UI::EntryBase::; 2297our @ISA = DC::UI::EntryBase::;
2283 2298
2284use CFPlus::OpenGL; 2299use DC::OpenGL;
2300
2301sub new {
2302 my $class = shift;
2303
2304 $class->SUPER::new (
2305 history_pointer => -1,
2306 @_
2307 )
2308}
2309
2285 2310
2286sub invoke_key_down { 2311sub invoke_key_down {
2287 my ($self, $ev) = @_; 2312 my ($self, $ev) = @_;
2288 2313
2289 my $sym = $ev->{sym}; 2314 my $sym = $ev->{sym};
2295 $self->{history_pointer} = -1; 2320 $self->{history_pointer} = -1;
2296 $self->{history_saveback} = ''; 2321 $self->{history_saveback} = '';
2297 $self->emit (activate => $txt); 2322 $self->emit (activate => $txt);
2298 $self->update; 2323 $self->update;
2299 2324
2300 } elsif ($sym == CFPlus::SDLK_UP) { 2325 } elsif ($sym == DC::SDLK_UP) {
2301 if ($self->{history_pointer} < 0) { 2326 if ($self->{history_pointer} < 0) {
2302 $self->{history_saveback} = $self->get_text; 2327 $self->{history_saveback} = $self->get_text;
2303 } 2328 }
2304 if (@{$self->{history} || []} > 0) { 2329 if (@{$self->{history} || []} > 0) {
2305 $self->{history_pointer}++; 2330 $self->{history_pointer}++;
2307 $self->{history_pointer} = @{$self->{history} || []} - 1; 2332 $self->{history_pointer} = @{$self->{history} || []} - 1;
2308 } 2333 }
2309 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2334 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2310 } 2335 }
2311 2336
2312 } elsif ($sym == CFPlus::SDLK_DOWN) { 2337 } elsif ($sym == DC::SDLK_DOWN) {
2313 $self->{history_pointer}--; 2338 $self->{history_pointer}--;
2314 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2339 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2315 2340
2316 if ($self->{history_pointer} >= 0) { 2341 if ($self->{history_pointer} >= 0) {
2317 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2318 } else { 2343 } else {
2344 if (defined $self->{history_saveback}) {
2319 $self->set_text ($self->{history_saveback}); 2345 $self->set_text ($self->{history_saveback});
2346 $self->{history_saveback} = undef;
2347 }
2320 } 2348 }
2321 2349
2322 } else { 2350 } else {
2323 return $self->SUPER::invoke_key_down ($ev) 2351 return $self->SUPER::invoke_key_down ($ev)
2324 } 2352 }
2326 1 2354 1
2327} 2355}
2328 2356
2329############################################################################# 2357#############################################################################
2330 2358
2331package CFPlus::UI::TextEdit; 2359package DC::UI::TextEdit;
2332 2360
2333our @ISA = CFPlus::UI::EntryBase::; 2361our @ISA = DC::UI::EntryBase::;
2334 2362
2335use CFPlus::OpenGL; 2363use DC::OpenGL;
2364
2365sub new {
2366 my $class = shift;
2367
2368 $class->SUPER::new (
2369 padding_y => 4,
2370
2371 @_
2372 )
2373}
2336 2374
2337sub move_cursor_ver { 2375sub move_cursor_ver {
2338 my ($self, $dy) = @_; 2376 my ($self, $dy) = @_;
2339 2377
2340 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2378 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2341 2379
2342 $y += $dy; 2380 $line += $dy;
2343 2381
2344 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2382 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2345 $self->{cursor} = $index; 2383 $self->{cursor} = $index;
2346 delete $self->{cur_h}; 2384 delete $self->{cur_h};
2347 $self->update; 2385 $self->update;
2348 return; 2386 return;
2349 } 2387 }
2352sub invoke_key_down { 2390sub invoke_key_down {
2353 my ($self, $ev) = @_; 2391 my ($self, $ev) = @_;
2354 2392
2355 my $sym = $ev->{sym}; 2393 my $sym = $ev->{sym};
2356 2394
2357 if ($sym == CFPlus::SDLK_UP) { 2395 if ($sym == DC::SDLK_UP) {
2358 $self->move_cursor_ver (-1); 2396 $self->move_cursor_ver (-1);
2359 } elsif ($sym == CFPlus::SDLK_DOWN) { 2397 } elsif ($sym == DC::SDLK_DOWN) {
2360 $self->move_cursor_ver (+1); 2398 $self->move_cursor_ver (+1);
2361 } else { 2399 } else {
2362 return $self->SUPER::invoke_key_down ($ev) 2400 return $self->SUPER::invoke_key_down ($ev)
2363 } 2401 }
2364 2402
2365 1 2403 1
2366} 2404}
2367 2405
2368############################################################################# 2406#############################################################################
2369 2407
2370package CFPlus::UI::ButtonBin; 2408package DC::UI::ButtonBin;
2371 2409
2372our @ISA = CFPlus::UI::Bin::; 2410our @ISA = DC::UI::Bin::;
2373 2411
2374use CFPlus::OpenGL; 2412use DC::OpenGL;
2375 2413
2376my @tex = 2414my @tex =
2377 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2415 map { new_from_resource DC::Texture $_, mipmap => 1 }
2378 qw(b1_button_inactive.png b1_button_active.png); 2416 qw(b1_button_inactive.png b1_button_active.png);
2379 2417
2380sub new { 2418sub new {
2381 my $class = shift; 2419 my $class = shift;
2382 2420
2383 $class->SUPER::new ( 2421 $class->SUPER::new (
2384 can_hover => 1, 2422 can_hover => 1,
2385 align => 0, 2423 align => 0.5,
2386 valign => 0, 2424 valign => 0.5,
2387 can_events => 1, 2425 can_events => 1,
2388 @_ 2426 @_
2389 ) 2427 )
2390} 2428}
2391 2429
2414 $self->SUPER::_draw; 2452 $self->SUPER::_draw;
2415} 2453}
2416 2454
2417############################################################################# 2455#############################################################################
2418 2456
2419package CFPlus::UI::Button; 2457package DC::UI::Button;
2420 2458
2421our @ISA = CFPlus::UI::Label::; 2459our @ISA = DC::UI::Label::;
2422 2460
2423use CFPlus::OpenGL; 2461use DC::OpenGL;
2424 2462
2425my @tex = 2463my @tex =
2426 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2464 map { new_from_resource DC::Texture $_, mipmap => 1 }
2427 qw(b1_button_inactive.png b1_button_active.png); 2465 qw(b1_button_inactive.png b1_button_active.png);
2428 2466
2429sub new { 2467sub new {
2430 my $class = shift; 2468 my $class = shift;
2431 2469
2432 $class->SUPER::new ( 2470 $class->SUPER::new (
2433 padding_x => 4, 2471 padding_x => 8,
2434 padding_y => 4, 2472 padding_y => 4,
2435 fg => [1.0, 1.0, 1.0], 2473 fg => [1.0, 1.0, 1.0],
2436 active_fg => [0.8, 0.8, 0.8], 2474 active_fg => [0.8, 0.8, 0.8],
2437 can_hover => 1, 2475 can_hover => 1,
2438 align => 0, 2476 align => 0.5,
2439 valign => 0, 2477 valign => 0.5,
2440 can_events => 1, 2478 can_events => 1,
2441 @_ 2479 @_
2442 ) 2480 )
2443} 2481}
2444 2482
2469 $self->SUPER::_draw; 2507 $self->SUPER::_draw;
2470} 2508}
2471 2509
2472############################################################################# 2510#############################################################################
2473 2511
2474package CFPlus::UI::CheckBox; 2512package DC::UI::CheckBox;
2475 2513
2476our @ISA = CFPlus::UI::DrawBG::; 2514our @ISA = DC::UI::DrawBG::;
2477 2515
2478my @tex = 2516my @tex =
2479 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2517 map { new_from_resource DC::Texture $_, mipmap => 1 }
2480 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2518 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2481 2519
2482use CFPlus::OpenGL; 2520use DC::OpenGL;
2483 2521
2484sub new { 2522sub new {
2485 my $class = shift; 2523 my $class = shift;
2486 2524
2487 $class->SUPER::new ( 2525 $class->SUPER::new (
2544 glDisable GL_TEXTURE_2D; 2582 glDisable GL_TEXTURE_2D;
2545} 2583}
2546 2584
2547############################################################################# 2585#############################################################################
2548 2586
2549package CFPlus::UI::Image; 2587package DC::UI::Image;
2550 2588
2551our @ISA = CFPlus::UI::Base::; 2589our @ISA = DC::UI::Base::;
2552 2590
2553use CFPlus::OpenGL; 2591use DC::OpenGL;
2554 2592
2555our %texture_cache; 2593our %texture_cache;
2556 2594
2557sub new { 2595sub new {
2558 my $class = shift; 2596 my $class = shift;
2565 2603
2566 $self->{path} || $self->{tex} 2604 $self->{path} || $self->{tex}
2567 or Carp::croak "'path' or 'tex' attributes required"; 2605 or Carp::croak "'path' or 'tex' attributes required";
2568 2606
2569 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2607 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2570 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2608 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2571 2609
2572 CFPlus::weaken $texture_cache{$self->{path}}; 2610 DC::weaken $texture_cache{$self->{path}};
2573 2611
2574 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2612 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2575 2613
2576 $self 2614 $self
2577} 2615}
2578 2616
2579sub STORABLE_freeze { 2617sub STORABLE_freeze {
2580 my ($self, $cloning) = @_; 2618 my ($self, $cloning) = @_;
2581 2619
2582 $self->{path} 2620 $self->{path}
2583 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2621 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2584 2622
2585 $self->{path} 2623 $self->{path}
2586} 2624}
2587 2625
2588sub STORABLE_attach { 2626sub STORABLE_attach {
2619 glDisable GL_TEXTURE_2D; 2657 glDisable GL_TEXTURE_2D;
2620} 2658}
2621 2659
2622############################################################################# 2660#############################################################################
2623 2661
2624package CFPlus::UI::ImageButton; 2662package DC::UI::ImageButton;
2625 2663
2626our @ISA = CFPlus::UI::Image::; 2664our @ISA = DC::UI::Image::;
2627 2665
2628use CFPlus::OpenGL; 2666use DC::OpenGL;
2629 2667
2630my %textures; 2668my %textures;
2631 2669
2632sub new { 2670sub new {
2633 my $class = shift; 2671 my $class = shift;
2636 padding_x => 4, 2674 padding_x => 4,
2637 padding_y => 4, 2675 padding_y => 4,
2638 fg => [1, 1, 1], 2676 fg => [1, 1, 1],
2639 active_fg => [0, 0, 1], 2677 active_fg => [0, 0, 1],
2640 can_hover => 1, 2678 can_hover => 1,
2641 align => 0, 2679 align => 0.5,
2642 valign => 0, 2680 valign => 0.5,
2643 can_events => 1, 2681 can_events => 1,
2644 @_ 2682 @_
2645 ); 2683 );
2646} 2684}
2647 2685
2661 1 2699 1
2662} 2700}
2663 2701
2664############################################################################# 2702#############################################################################
2665 2703
2666package CFPlus::UI::VGauge; 2704package DC::UI::VGauge;
2667 2705
2668our @ISA = CFPlus::UI::Base::; 2706our @ISA = DC::UI::Base::;
2669 2707
2670use List::Util qw(min max); 2708use List::Util qw(min max);
2671 2709
2672use CFPlus::OpenGL; 2710use DC::OpenGL;
2673 2711
2674my %tex = ( 2712my %tex = (
2675 food => [ 2713 food => [
2676 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2714 map { new_from_resource DC::Texture $_, mipmap => 1 }
2677 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2715 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2678 ], 2716 ],
2679 grace => [ 2717 grace => [
2680 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2718 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2719 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2682 ], 2720 ],
2683 hp => [ 2721 hp => [
2684 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2722 map { new_from_resource DC::Texture $_, mipmap => 1 }
2685 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2723 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2686 ], 2724 ],
2687 mana => [ 2725 mana => [
2688 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2726 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2727 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2690 ], 2728 ],
2691); 2729);
2692 2730
2693# eg. VGauge->new (gauge => 'food'), default gauge: food 2731# eg. VGauge->new (gauge => 'food'), default gauge: food
2796 glDisable GL_TEXTURE_2D; 2834 glDisable GL_TEXTURE_2D;
2797} 2835}
2798 2836
2799############################################################################# 2837#############################################################################
2800 2838
2801package CFPlus::UI::Progress; 2839package DC::UI::Progress;
2802 2840
2803our @ISA = CFPlus::UI::Label::; 2841our @ISA = DC::UI::Label::;
2804 2842
2805use CFPlus::OpenGL; 2843use DC::OpenGL;
2806 2844
2807sub new { 2845sub new {
2808 my ($class, %arg) = @_; 2846 my ($class, %arg) = @_;
2809 2847
2810 my $self = $class->SUPER::new ( 2848 my $self = $class->SUPER::new (
2849 padding_x => 2,
2850 padding_y => 2,
2811 fg => [1, 1, 1], 2851 fg => [1, 1, 1],
2812 bg => [0, 0, 1, 0.2], 2852 bg => [0, 0, 1, 0.2],
2813 bar => [0.7, 0.5, 0.1, 0.8], 2853 bar => [0.7, 0.5, 0.1, 0.8],
2814 outline => [0.4, 0.3, 0], 2854 outline => [0.4, 0.3, 0],
2815 fontsize => 0.9, 2855 fontsize => 0.9,
2816 valign => 0, 2856 valign => 0.5,
2817 align => 0, 2857 align => 0.5,
2818 can_events => 1, 2858 can_events => 1,
2819 ellipsise => 1, 2859 ellipsise => 1,
2820 label => "%d%%", 2860 label => "%d%%",
2821 %arg, 2861 %arg,
2822 ); 2862 );
2830 my ($self, $label) = @_; 2870 my ($self, $label) = @_;
2831 2871
2832 return if $self->{label} eq $label; 2872 return if $self->{label} eq $label;
2833 $self->{label} = $label; 2873 $self->{label} = $label;
2834 2874
2835 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2875 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2836} 2876}
2837 2877
2838sub set_value { 2878sub set_value {
2839 my ($self, $value) = @_; 2879 my ($self, $value) = @_;
2840 2880
2855 my ($self) = @_; 2895 my ($self) = @_;
2856 2896
2857 glEnable GL_BLEND; 2897 glEnable GL_BLEND;
2858 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2898 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2859 2899
2900 my $px = $self->{padding_x};
2901 my $py = $self->{padding_y};
2902
2860 if ($self->{value} >= 0) { 2903 if ($self->{value} >= 0) {
2861 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2904 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2862 2905
2863 glColor_premultiply @{$self->{bar}}; 2906 glColor_premultiply @{$self->{bar}};
2864 glRect 2, 2, $s, $self->{h} - 2; 2907 glRect $px, $py, $s, $self->{h} - $py;
2865 glColor_premultiply @{$self->{bg}}; 2908 glColor_premultiply @{$self->{bg}};
2866 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2909 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2867 } 2910 }
2868 2911
2869 glColor_premultiply @{$self->{outline}}; 2912 glColor_premultiply @{$self->{outline}};
2913
2914 $px -= .5;
2915 $py -= .5;
2916
2870 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2917 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2871 2918
2872 glDisable GL_BLEND; 2919 glDisable GL_BLEND;
2873 2920
2874 { 2921 {
2875 local $self->{bg}; # do not draw background 2922 local $self->{bg}; # do not draw background
2877 } 2924 }
2878} 2925}
2879 2926
2880############################################################################# 2927#############################################################################
2881 2928
2882package CFPlus::UI::ExperienceProgress; 2929package DC::UI::ExperienceProgress;
2883 2930
2884our @ISA = CFPlus::UI::Progress::; 2931our @ISA = DC::UI::Progress::;
2885 2932
2886sub new { 2933sub new {
2887 my ($class, %arg) = @_; 2934 my ($class, %arg) = @_;
2888 2935
2936 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2937
2889 my $self = $class->SUPER::new ( 2938 my $self = $class->SUPER::new (
2939 %arg,
2890 tooltip => sub { 2940 tooltip => sub {
2891 my ($self) = @_; 2941 my ($self) = @_;
2892 2942
2893 sprintf "level %d\n%s points\n%s next level\n%s to go", 2943 sprintf "%slevel %d\n%s points\n%s next level\n%s to go",
2944 $tt,
2894 $self->{lvl}, 2945 $self->{lvl},
2895 ::formsep ($self->{exp}), 2946 ::formsep ($self->{exp}),
2896 ::formsep ($self->{nxt}), 2947 ::formsep ($self->{nxt}),
2897 ::formsep ($self->{nxt} - $self->{exp}), 2948 ::formsep ($self->{nxt} - $self->{exp}),
2898 }, 2949 },
2899 %arg
2900 ); 2950 );
2901 2951
2902 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2952 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2903 if $::CONN; 2953 if $::CONN;
2904 2954
2934 $self->SUPER::set_value ($v); 2984 $self->SUPER::set_value ($v);
2935} 2985}
2936 2986
2937############################################################################# 2987#############################################################################
2938 2988
2939package CFPlus::UI::Gauge; 2989package DC::UI::Gauge;
2940 2990
2941our @ISA = CFPlus::UI::VBox::; 2991our @ISA = DC::UI::VBox::;
2942 2992
2943sub new { 2993sub new {
2944 my ($class, %arg) = @_; 2994 my ($class, %arg) = @_;
2945 2995
2946 my $self = $class->SUPER::new ( 2996 my $self = $class->SUPER::new (
2948 can_hover => 1, 2998 can_hover => 1,
2949 can_events => 1, 2999 can_events => 1,
2950 %arg, 3000 %arg,
2951 ); 3001 );
2952 3002
2953 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3003 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2954 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3004 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2955 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3005 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2956 3006
2957 $self 3007 $self
2958} 3008}
2959 3009
2960sub set_fontsize { 3010sub set_fontsize {
2981 $self->{value}->set_text ($val); 3031 $self->{value}->set_text ($val);
2982} 3032}
2983 3033
2984############################################################################# 3034#############################################################################
2985 3035
2986package CFPlus::UI::Slider; 3036package DC::UI::Slider;
2987 3037
2988use strict; 3038use strict;
2989 3039
2990use CFPlus::OpenGL; 3040use DC::OpenGL;
2991 3041
2992our @ISA = CFPlus::UI::DrawBG::; 3042our @ISA = DC::UI::DrawBG::;
2993 3043
2994my @tex = 3044my @tex =
2995 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3045 map { new_from_resource DC::Texture $_ }
2996 qw(s1_slider.png s1_slider_bg.png); 3046 qw(s1_slider.png s1_slider_bg.png);
2997 3047
2998sub new { 3048sub new {
2999 my $class = shift; 3049 my $class = shift;
3000 3050
3037sub set_value { 3087sub set_value {
3038 my ($self, $value) = @_; 3088 my ($self, $value) = @_;
3039 3089
3040 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3090 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3041 3091
3042 $hi = $lo + 1 if $hi <= $lo; 3092 $hi = $lo if $hi < $lo;
3043 3093
3044 $page = $hi - $lo if $page > $hi - $lo; 3094 $value = $hi - $page if $value > $hi - $page;
3045
3046 $value = $lo if $value < $lo; 3095 $value = $lo if $value < $lo;
3047 $value = $hi - $page if $value > $hi - $page;
3048 3096
3049 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3097 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3050 if $unit; 3098 if $unit;
3051 3099
3052 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3100 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3096sub invoke_mouse_wheel { 3144sub invoke_mouse_wheel {
3097 my ($self, $ev) = @_; 3145 my ($self, $ev) = @_;
3098 3146
3099 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3147 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3100 3148
3101 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3149 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3102 3150
3103 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3151 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3104 3152
3105 1 3153 1
3106} 3154}
3116 my ($self) = @_; 3164 my ($self) = @_;
3117 3165
3118 unless ($self->{knob_w}) { 3166 unless ($self->{knob_w}) {
3119 $self->set_value ($self->{range}[0]); 3167 $self->set_value ($self->{range}[0]);
3120 3168
3121 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3169 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3122 my $range = ($hi - $page - $lo) || 1e-100; 3170 my $range = ($hi - $page - $lo) || 1e-10;
3123 3171
3124 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3172 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3125 3173
3126 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3174 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3127 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3175 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3128 3176
3129 $value = ($value - $lo) / $range; 3177 $value = ($value - $lo) / $range;
3159 glDisable GL_TEXTURE_2D; 3207 glDisable GL_TEXTURE_2D;
3160} 3208}
3161 3209
3162############################################################################# 3210#############################################################################
3163 3211
3164package CFPlus::UI::ValSlider; 3212package DC::UI::ValSlider;
3165 3213
3166our @ISA = CFPlus::UI::HBox::; 3214our @ISA = DC::UI::HBox::;
3167 3215
3168sub new { 3216sub new {
3169 my ($class, %arg) = @_; 3217 my ($class, %arg) = @_;
3170 3218
3171 my $range = delete $arg{range}; 3219 my $range = delete $arg{range};
3172 3220
3173 my $self = $class->SUPER::new ( 3221 my $self = $class->SUPER::new (
3174 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3222 slider => (new DC::UI::Slider expand => 1, range => $range),
3175 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3223 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3176 to_value => sub { shift }, 3224 to_value => sub { shift },
3177 from_value => sub { shift }, 3225 from_value => sub { shift },
3178 %arg, 3226 %arg,
3179 ); 3227 );
3180 3228
3200sub set_range { shift->{slider}->set_range (@_) } 3248sub set_range { shift->{slider}->set_range (@_) }
3201sub set_value { shift->{slider}->set_value (@_) } 3249sub set_value { shift->{slider}->set_value (@_) }
3202 3250
3203############################################################################# 3251#############################################################################
3204 3252
3205package CFPlus::UI::TextScroller; 3253package DC::UI::TextScroller;
3206 3254
3207our @ISA = CFPlus::UI::HBox::; 3255our @ISA = DC::UI::HBox::;
3208 3256
3209use CFPlus::OpenGL; 3257use DC::OpenGL;
3210 3258
3211sub new { 3259sub new {
3212 my $class = shift; 3260 my $class = shift;
3213 3261
3214 my $self = $class->SUPER::new ( 3262 my $self = $class->SUPER::new (
3216 can_events => 1, 3264 can_events => 1,
3217 indent => 0, 3265 indent => 0,
3218 #font => default_font 3266 #font => default_font
3219 @_, 3267 @_,
3220 3268
3221 layout => (new CFPlus::Layout), 3269 layout => (new DC::Layout),
3222 par => [], 3270 par => [],
3223 max_par => 0, 3271 max_par => 0,
3224 height => 0, 3272 height => 0,
3225 children => [ 3273 children => [
3226 (new CFPlus::UI::Empty expand => 1), 3274 (new DC::UI::Empty expand => 1),
3227 (new CFPlus::UI::Slider vertical => 1), 3275 (new DC::UI::Slider vertical => 1),
3228 ], 3276 ],
3229 ); 3277 );
3230 3278
3231 $self->{children}[1]->connect (changed => sub { $self->update }); 3279 $self->{children}[1]->connect (changed => sub { $self->update });
3232 3280
3288 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3336 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3289 $layout->set_markup ($para->{markup}); 3337 $layout->set_markup ($para->{markup});
3290 3338
3291 $layout->set_shapes ( 3339 $layout->set_shapes (
3292 map 3340 map
3293 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3341 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3294 @{$para->{widget}} 3342 @{$para->{widget}}
3295 ); 3343 );
3296 3344
3297 $layout 3345 $layout
3298} 3346}
3415 $ROOT->on_post_alloc ($self => sub { 3463 $ROOT->on_post_alloc ($self => sub {
3416 $self->force_uptodate; 3464 $self->force_uptodate;
3417 3465
3418 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3466 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3419 3467
3420 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3468 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3421 glClearColor 0, 0, 0, 0; 3469 glClearColor 0, 0, 0, 0;
3422 glClear GL_COLOR_BUFFER_BIT; 3470 glClear GL_COLOR_BUFFER_BIT;
3423 3471
3424 package CFPlus::UI::Base; 3472 package DC::UI::Base;
3425 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3473 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3426 (0, 0, $self->{w}, $self->{h}); 3474 (0, 0, $self->{w}, $self->{h});
3427 3475
3428 my $top = int $self->{children}[1]{range}[0]; 3476 my $top = int $self->{children}[1]{range}[0];
3429 3477
3488 $self->{children}[1]->draw; 3536 $self->{children}[1]->draw;
3489} 3537}
3490 3538
3491############################################################################# 3539#############################################################################
3492 3540
3493package CFPlus::UI::Animator; 3541package DC::UI::Animator;
3494 3542
3495use CFPlus::OpenGL; 3543use DC::OpenGL;
3496 3544
3497our @ISA = CFPlus::UI::Bin::; 3545our @ISA = DC::UI::Bin::;
3498 3546
3499sub moveto { 3547sub moveto {
3500 my ($self, $x, $y) = @_; 3548 my ($self, $x, $y) = @_;
3501 3549
3502 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3550 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3530 glPopMatrix; 3578 glPopMatrix;
3531} 3579}
3532 3580
3533############################################################################# 3581#############################################################################
3534 3582
3535package CFPlus::UI::Flopper; 3583package DC::UI::Flopper;
3536 3584
3537our @ISA = CFPlus::UI::Button::; 3585our @ISA = DC::UI::Button::;
3538 3586
3539sub new { 3587sub new {
3540 my $class = shift; 3588 my $class = shift;
3541 3589
3542 my $self = $class->SUPER::new ( 3590 my $self = $class->SUPER::new (
3554 $self->{other}->toggle_visibility; 3602 $self->{other}->toggle_visibility;
3555} 3603}
3556 3604
3557############################################################################# 3605#############################################################################
3558 3606
3559package CFPlus::UI::Tooltip; 3607package DC::UI::Tooltip;
3560 3608
3561our @ISA = CFPlus::UI::Bin::; 3609our @ISA = DC::UI::Bin::;
3562 3610
3563use CFPlus::OpenGL; 3611use DC::OpenGL;
3564 3612
3565sub new { 3613sub new {
3566 my $class = shift; 3614 my $class = shift;
3567 3615
3568 $class->SUPER::new ( 3616 $class->SUPER::new (
3575 my ($self, $widget) = @_; 3623 my ($self, $widget) = @_;
3576 3624
3577 my $tip = $widget->{tooltip}; 3625 my $tip = $widget->{tooltip};
3578 $tip = $tip->($widget) if "CODE" eq ref $tip; 3626 $tip = $tip->($widget) if "CODE" eq ref $tip;
3579 3627
3580 $tip = CFPlus::Pod::section_label tooltip => $1 3628 $tip = DC::Pod::section_label tooltip => $1
3581 if $tip =~ /^#(.*)$/; 3629 if $tip =~ /^#(.*)$/;
3582 3630
3583 if ($ENV{CFPLUS_DEBUG} & 2) { 3631 if ($ENV{CFPLUS_DEBUG} & 2) {
3584 $tip .= "\n\n" . (ref $widget) . "\n" 3632 $tip .= "\n\n" . (ref $widget) . "\n"
3585 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3633 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3588 } 3636 }
3589 3637
3590 $tip =~ s/^\n+//; 3638 $tip =~ s/^\n+//;
3591 $tip =~ s/\n+$//; 3639 $tip =~ s/\n+$//;
3592 3640
3593 $self->add (new CFPlus::UI::Label 3641 $self->add (new DC::UI::Label
3642 fg => $DC::THEME{tooltip_fg},
3594 markup => $tip, 3643 markup => $tip,
3595 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3644 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3645 align => 0,
3596 fontsize => 0.8, 3646 fontsize => 0.8,
3597 style => 1, # FLAG_INVERSE 3647 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3598 ellipsise => 0, 3648 ellipsise => 0,
3599 font => ($widget->{tooltip_font} || $::FONT_PROP), 3649 font => ($widget->{tooltip_font} || $::FONT_PROP),
3600 ); 3650 );
3601} 3651}
3602 3652
3639sub _draw { 3689sub _draw {
3640 my ($self) = @_; 3690 my ($self) = @_;
3641 3691
3642 my ($w, $h) = @$self{qw(w h)}; 3692 my ($w, $h) = @$self{qw(w h)};
3643 3693
3644 glColor 1, 0.8, 0.4; 3694 glColor @{ $DC::THEME{tooltip_bg} };
3645 glRect 0, 0, $w, $h; 3695 glRect 0, 0, $w, $h;
3646 3696
3647 glColor 0, 0, 0; 3697 glColor @{ $DC::THEME{tooltip_border} };
3648 glRect_lineloop .5, .5, $w + .5, $h + .5; 3698 glRect_lineloop .5, .5, $w + .5, $h + .5;
3649 3699
3650 glTranslate 2, 2; 3700 glTranslate 2, 2;
3651 3701
3652 $self->SUPER::_draw; 3702 $self->SUPER::_draw;
3653} 3703}
3654 3704
3655############################################################################# 3705#############################################################################
3656 3706
3657package CFPlus::UI::Face; 3707package DC::UI::Face;
3658 3708
3659our @ISA = CFPlus::UI::DrawBG::; 3709our @ISA = DC::UI::DrawBG::;
3660 3710
3661use CFPlus::OpenGL; 3711use DC::OpenGL;
3662 3712
3663sub new { 3713sub new {
3664 my $class = shift; 3714 my $class = shift;
3665 3715
3666 my $self = $class->SUPER::new ( 3716 my $self = $class->SUPER::new (
3670 can_events => 0, 3720 can_events => 0,
3671 @_, 3721 @_,
3672 ); 3722 );
3673 3723
3674 if ($self->{anim} && $self->{animspeed}) { 3724 if ($self->{anim} && $self->{animspeed}) {
3675 CFPlus::weaken (my $widget = $self); 3725 DC::weaken (my $widget = $self);
3676 3726
3677 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3727 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3678 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3728 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3679 $self->{timer} = Event->timer (
3680 parked => 1,
3681 cb => sub {
3682 return unless $::CONN; 3729 return unless $::CONN;
3683 3730
3684 my $w = $widget 3731 my $w = $widget
3685 or return; 3732 or return;
3686 3733
3687 ++$w->{frame}; 3734 ++$w->{frame};
3688 $w->update_face; 3735 $w->update_face;
3689 3736
3690 # somehow, $widget can go away 3737 # somehow, $widget can go away
3691 $w->update; 3738 $w->update;
3692 $w->update_timer; 3739 $w->update_timer;
3693 },
3694 ); 3740 };
3695 3741
3696 $self->update_face; 3742 $self->update_face;
3697 $self->update_timer; 3743 $self->update_timer;
3698 } 3744 }
3699 3745
3704 my ($self) = @_; 3750 my ($self) = @_;
3705 3751
3706 return unless $self->{timer}; 3752 return unless $self->{timer};
3707 3753
3708 if ($self->{visible}) { 3754 if ($self->{visible}) {
3709 $self->{timer}->at (
3710 $self->{anim_start}
3711 + $self->{animspeed}
3712 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3713 );
3714 $self->{timer}->start; 3755 $self->{timer}->start;
3715 } else { 3756 } else {
3716 $self->{timer}->stop; 3757 $self->{timer}->stop;
3717 } 3758 }
3718} 3759}
3795 $self->SUPER::destroy; 3836 $self->SUPER::destroy;
3796} 3837}
3797 3838
3798############################################################################# 3839#############################################################################
3799 3840
3800package CFPlus::UI::Buttonbar; 3841package DC::UI::Buttonbar;
3801 3842
3802our @ISA = CFPlus::UI::HBox::; 3843our @ISA = DC::UI::HBox::;
3803 3844
3804# TODO: should actually wrap buttons and other goodies. 3845# TODO: should actually wrap buttons and other goodies.
3805 3846
3806############################################################################# 3847#############################################################################
3807 3848
3808package CFPlus::UI::Menu; 3849package DC::UI::Menu;
3809 3850
3810our @ISA = CFPlus::UI::Toplevel::; 3851our @ISA = DC::UI::Toplevel::;
3811 3852
3812use CFPlus::OpenGL; 3853use DC::OpenGL;
3813 3854
3814sub new { 3855sub new {
3815 my $class = shift; 3856 my $class = shift;
3816 3857
3817 my $self = $class->SUPER::new ( 3858 my $self = $class->SUPER::new (
3818 items => [], 3859 items => [],
3819 z => 100, 3860 z => 100,
3820 @_, 3861 @_,
3821 ); 3862 );
3822 3863
3823 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3864 $self->add ($self->{vbox} = new DC::UI::VBox);
3824 3865
3825 for my $item (@{ $self->{items} }) { 3866 for my $item (@{ $self->{items} }) {
3826 my ($widget, $cb, $tooltip) = @$item; 3867 my ($widget, $cb, $tooltip) = @$item;
3827 3868
3828 # handle various types of items, only text for now 3869 # handle various types of items, only text for now
3829 if (!ref $widget) { 3870 if (!ref $widget) {
3830 if ($widget =~ /\t/) { 3871 if ($widget =~ /\t/) {
3831 my ($left, $right) = split /\t/, $widget, 2; 3872 my ($left, $right) = split /\t/, $widget, 2;
3832 3873
3833 $widget = new CFPlus::UI::HBox 3874 $widget = new DC::UI::HBox
3834 can_hover => 1, 3875 can_hover => 1,
3835 can_events => 1, 3876 can_events => 1,
3836 tooltip => $tooltip, 3877 tooltip => $tooltip,
3837 children => [ 3878 children => [
3838 (new CFPlus::UI::Label markup => $left, expand => 1), 3879 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3839 (new CFPlus::UI::Label markup => $right, align => +1), 3880 (new DC::UI::Label markup => $right, align => 1),
3840 ], 3881 ],
3841 ; 3882 ;
3842 3883
3843 } else { 3884 } else {
3844 $widget = new CFPlus::UI::Label 3885 $widget = new DC::UI::Label
3845 can_hover => 1, 3886 can_hover => 1,
3846 can_events => 1, 3887 can_events => 1,
3888 align => 0,
3847 markup => $widget, 3889 markup => $widget,
3848 tooltip => $tooltip; 3890 tooltip => $tooltip;
3849 } 3891 }
3850 } 3892 }
3851 3893
3867 $GRAB = $self; 3909 $GRAB = $self;
3868 $self->{button} = $ev->{button}; 3910 $self->{button} = $ev->{button};
3869 3911
3870 $self->show; 3912 $self->show;
3871 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3913 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
3914
3915 1 # so it can be used inside event handlers
3872} 3916}
3873 3917
3874sub invoke_mouse_motion { 3918sub invoke_mouse_motion {
3875 my ($self, $ev, $x, $y) = @_; 3919 my ($self, $ev, $x, $y) = @_;
3876 3920
3897 1 3941 1
3898} 3942}
3899 3943
3900############################################################################# 3944#############################################################################
3901 3945
3902package CFPlus::UI::Multiplexer; 3946package DC::UI::Multiplexer;
3903 3947
3904our @ISA = CFPlus::UI::Container::; 3948our @ISA = DC::UI::Container::;
3905 3949
3906sub new { 3950sub new {
3907 my $class = shift; 3951 my $class = shift;
3908 3952
3909 my $self = $class->SUPER::new ( 3953 my $self = $class->SUPER::new (
3910 @_, 3954 @_,
3911 ); 3955 );
3912 3956
3913 $self->{current} = $self->{children}[0] 3957 $self->set_current_page (0);
3914 if @{ $self->{children} };
3915 3958
3916 $self 3959 $self
3917} 3960}
3918 3961
3919sub add { 3962sub add {
3920 my ($self, @widgets) = @_; 3963 my ($self, @widgets) = @_;
3921 3964
3922 $self->SUPER::add (@widgets); 3965 $self->SUPER::add (@widgets);
3923 3966
3924 $self->{current} = $self->{children}[0] 3967 $self->set_current_page (0)
3925 if @{ $self->{children} }; 3968 if @widgets == @{ $self->{children} };
3926} 3969}
3927 3970
3928sub get_current_page { 3971sub get_current_page {
3929 my ($self) = @_; 3972 my ($self) = @_;
3930 3973
3936 3979
3937 my $widget = ref $page_or_widget 3980 my $widget = ref $page_or_widget
3938 ? $page_or_widget 3981 ? $page_or_widget
3939 : $self->{children}[$page_or_widget]; 3982 : $self->{children}[$page_or_widget];
3940 3983
3984 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3985
3941 $self->{current} = $widget; 3986 if (($self->{current} = $widget)) {
3987 $self->{current}->set_visible if $self->{current} && $self->{visible};
3942 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3988 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3943 3989
3944 $self->emit (page_changed => $self->{current}); 3990 $self->emit (page_changed => $self->{current});
3991 }
3945 3992
3946 $self->realloc; 3993 $self->realloc;
3947} 3994}
3948 3995
3949sub visible_children { 3996sub visible_children {
3950 $_[0]{current} 3997 $_[0]{current} || ()
3951} 3998}
3952 3999
3953sub size_request { 4000sub size_request {
3954 my ($self) = @_; 4001 my ($self) = @_;
3955 4002
4003 $self->{current}
3956 $self->{current}->size_request 4004 ? $self->{current}->size_request
4005 : (0, 0)
3957} 4006}
3958 4007
3959sub invoke_size_allocate { 4008sub invoke_size_allocate {
3960 my ($self, $w, $h) = @_; 4009 my ($self, $w, $h) = @_;
3961 4010
3962 $self->{current}->configure (0, 0, $w, $h); 4011 $self->{current}->configure (0, 0, $w, $h)
4012 if $self->{current};
3963 4013
3964 1 4014 1
3965} 4015}
3966 4016
3967sub _draw { 4017sub _draw {
3968 my ($self) = @_; 4018 my ($self) = @_;
3969 4019
3970 $self->{current}->draw; 4020 $self->{current}->draw
4021 if $self->{current};
3971} 4022}
3972 4023
3973############################################################################# 4024#############################################################################
3974 4025
3975package CFPlus::UI::Notebook; 4026package DC::UI::Notebook;
3976 4027
3977use CFPlus::OpenGL; 4028use DC::OpenGL;
3978 4029
3979our @ISA = CFPlus::UI::VBox::; 4030our @ISA = DC::UI::VBox::;
3980 4031
3981sub new { 4032sub new {
3982 my $class = shift; 4033 my $class = shift;
3983 4034
3984 my $self = $class->SUPER::new ( 4035 my $self = $class->SUPER::new (
3985 buttonbar => (new CFPlus::UI::Buttonbar), 4036 buttonbar => (new DC::UI::Buttonbar),
3986 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4037 multiplexer => (new DC::UI::Multiplexer expand => 1),
3987 active_outline => [.7, .7, 0.2], 4038 active_outline => [.7, .7, 0.2],
3988 # filter => # will be put between multiplexer and $self 4039 # filter => # will be put between multiplexer and $self
3989 @_, 4040 @_,
3990 ); 4041 );
3991 4042
4000 4051
4001 for my $child (@$widgets) { 4052 for my $child (@$widgets) {
4002 Scalar::Util::weaken $child; 4053 Scalar::Util::weaken $child;
4003 $child->{c_tab_} ||= do { 4054 $child->{c_tab_} ||= do {
4004 my $tab = 4055 my $tab =
4005 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4056 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4006 ? $child->{c_tab} 4057 ? $child->{c_tab}
4007 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4058 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4008 4059
4009 $tab->connect (activate => sub { 4060 $tab->connect (activate => sub {
4010 $wself->set_current_page ($child); 4061 $wself->set_current_page ($child);
4011 }); 4062 });
4012 4063
4042} 4093}
4043 4094
4044sub pages { 4095sub pages {
4045 my ($self) = @_; 4096 my ($self) = @_;
4046 $self->{multiplexer}->children 4097 $self->{multiplexer}->children
4098}
4099
4100sub page_index {
4101 my ($self, $widget) = @_;
4102
4103 my $i = 0;
4104 for ($self->pages) {
4105 if ($_ eq $widget) { return $i };
4106 $i++;
4107 }
4108
4109 undef
4047} 4110}
4048 4111
4049sub add_tab { 4112sub add_tab {
4050 my ($self, $title, $widget, $tooltip) = @_; 4113 my ($self, $title, $widget, $tooltip) = @_;
4051 4114
4088 } 4151 }
4089} 4152}
4090 4153
4091############################################################################# 4154#############################################################################
4092 4155
4093package CFPlus::UI::Selector; 4156package DC::UI::Selector;
4094 4157
4095use utf8; 4158use utf8;
4096 4159
4097our @ISA = CFPlus::UI::Button::; 4160our @ISA = DC::UI::Button::;
4098 4161
4099sub new { 4162sub new {
4100 my $class = shift; 4163 my $class = shift;
4101 4164
4102 my $self = $class->SUPER::new ( 4165 my $self = $class->SUPER::new (
4119 my ($value, $title, $tooltip) = @$_; 4182 my ($value, $title, $tooltip) = @$_;
4120 4183
4121 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4184 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4122 } 4185 }
4123 4186
4124 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4187 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4125} 4188}
4126 4189
4127sub _set_value { 4190sub _set_value {
4128 my ($self, $value) = @_; 4191 my ($self, $value) = @_;
4129 4192
4130 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4193 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4194 $item ||= $self->{options}[0]
4131 or return; 4195 or return;
4132 4196
4133 $self->{value} = $item->[0]; 4197 $self->{value} = $item->[0];
4134 $self->set_markup ("$item->[1] ⇓"); 4198 $self->set_markup ("$item->[1] ⇓");
4135 $self->set_tooltip ($item->[2]); 4199# $self->set_tooltip ($item->[2]);
4136} 4200}
4137 4201
4138sub set_value { 4202sub set_value {
4139 my ($self, $value) = @_; 4203 my ($self, $value) = @_;
4140 4204
4142 4206
4143 $self->_set_value ($value); 4207 $self->_set_value ($value);
4144 $self->emit (changed => $value); 4208 $self->emit (changed => $value);
4145} 4209}
4146 4210
4211sub set_options {
4212 my ($self, $options) = @_;
4213
4214 $self->{options} = $options;
4215 $self->_set_value ($self->{value});
4216}
4217
4147############################################################################# 4218#############################################################################
4148 4219
4149package CFPlus::UI::Statusbox; 4220package DC::UI::Statusbox;
4150 4221
4151our @ISA = CFPlus::UI::VBox::; 4222our @ISA = DC::UI::VBox::;
4152 4223
4153sub new { 4224sub new {
4154 my $class = shift; 4225 my $class = shift;
4155 4226
4156 my $self = $class->SUPER::new ( 4227 my $self = $class->SUPER::new (
4157 fontsize => 0.8, 4228 fontsize => 0.8,
4158 @_, 4229 @_,
4159 ); 4230 );
4160 4231
4161 CFPlus::weaken (my $this = $self); 4232 DC::weaken (my $this = $self);
4162 4233
4163 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4234 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4164 4235
4165 $self 4236 $self
4166} 4237}
4167 4238
4168sub reorder { 4239sub reorder {
4169 my ($self) = @_; 4240 my ($self) = @_;
4170 my $NOW = Time::HiRes::time; 4241 my $NOW = EV::time;
4171 4242
4172 # freeze display when hovering over any label 4243 # freeze display when hovering over any label
4173 return if $CFPlus::UI::TOOLTIP->{owner} 4244 return if $DC::UI::TOOLTIP->{owner}
4174 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4245 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4175 values %{ $self->{item} }; 4246 values %{ $self->{item} };
4176 4247
4177 while (my ($k, $v) = each %{ $self->{item} }) { 4248 while (my ($k, $v) = each %{ $self->{item} }) {
4178 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4249 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4179 } 4250 }
4251
4252 $self->{timer}->set (1, 1);
4180 4253
4181 my @widgets; 4254 my @widgets;
4182 4255
4183 my @items = sort { 4256 my @items = sort {
4184 $a->{pri} <=> $b->{pri} 4257 $a->{pri} <=> $b->{pri}
4185 or $b->{id} <=> $a->{id} 4258 or $b->{id} <=> $a->{id}
4186 } values %{ $self->{item} }; 4259 } values %{ $self->{item} };
4187
4188 $self->{timer}->interval (1);
4189 4260
4190 my $count = 10 + 1; 4261 my $count = 10 + 1;
4191 for my $item (@items) { 4262 for my $item (@items) {
4192 last unless --$count; 4263 last unless --$count;
4193 4264
4200 for ($short) { 4271 for ($short) {
4201 s/^\s+//; 4272 s/^\s+//;
4202 s/\s+/ /g; 4273 s/\s+/ /g;
4203 } 4274 }
4204 4275
4205 new CFPlus::UI::Label 4276 new DC::UI::Label
4206 markup => $short, 4277 markup => $short,
4207 tooltip => $item->{tooltip}, 4278 tooltip => $item->{tooltip},
4208 tooltip_font => $::FONT_PROP, 4279 tooltip_font => $::FONT_PROP,
4209 tooltip_width => 0.67, 4280 tooltip_width => 0.67,
4210 fontsize => $item->{fontsize} || $self->{fontsize}, 4281 fontsize => $item->{fontsize} || $self->{fontsize},
4211 max_w => $::WIDTH * 0.44, 4282 max_w => $::WIDTH * 0.44,
4283 align => 0,
4212 fg => [@{ $item->{fg} }], 4284 fg => [@{ $item->{fg} }],
4213 can_events => 1, 4285 can_events => 1,
4214 can_hover => 1 4286 can_hover => 1
4215 }; 4287 };
4216 4288
4217 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4289 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4290 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4219 $label->update; 4291 $label->update;
4220 $label->set_max_size (undef, $label->{req_h} * $diff) 4292 $label->set_max_size (undef, $label->{req_h} * $diff)
4221 if $diff < 1; 4293 if $diff < 1;
4222 $self->{timer}->interval (1/30); 4294 $self->{timer}->set (1/30, 1/30);
4223 } else { 4295 } else {
4224 $label->{fg}[3] = $item->{fg}[3] || 1; 4296 $label->{fg}[3] = $item->{fg}[3] || 1;
4225 } 4297 }
4226 4298
4227 push @widgets, $label; 4299 push @widgets, $label;
4228 } 4300 }
4301
4302 my $hash = join ",", @widgets;
4303 return if $hash eq $self->{last_widget_hash};
4304 $self->{last_widget_hash} = $hash;
4229 4305
4230 $self->clear; 4306 $self->clear;
4231 $self->SUPER::add (reverse @widgets); 4307 $self->SUPER::add (reverse @widgets);
4232} 4308}
4233 4309
4289 $self->SUPER::destroy; 4365 $self->SUPER::destroy;
4290} 4366}
4291 4367
4292############################################################################# 4368#############################################################################
4293 4369
4294package CFPlus::UI::Root; 4370package DC::UI::Root;
4295 4371
4296our @ISA = CFPlus::UI::Container::; 4372our @ISA = DC::UI::Container::;
4297 4373
4298use List::Util qw(min max); 4374use List::Util qw(min max);
4299 4375
4300use CFPlus::OpenGL; 4376use DC::OpenGL;
4301 4377
4302sub new { 4378sub new {
4303 my $class = shift; 4379 my $class = shift;
4304 4380
4305 my $self = $class->SUPER::new ( 4381 my $self = $class->SUPER::new (
4306 visible => 1, 4382 visible => 1,
4307 @_, 4383 @_,
4308 ); 4384 );
4309 4385
4310 CFPlus::weaken ($self->{root} = $self); 4386 DC::weaken ($self->{root} = $self);
4311 4387
4312 $self 4388 $self
4313} 4389}
4314 4390
4315sub size_request { 4391sub size_request {
4363} 4439}
4364 4440
4365sub update { 4441sub update {
4366 my ($self) = @_; 4442 my ($self) = @_;
4367 4443
4368 $::WANT_REFRESH->start; 4444 $::WANT_REFRESH = 1;
4369} 4445}
4370 4446
4371sub add { 4447sub add {
4372 my ($self, @children) = @_; 4448 my ($self, @children) = @_;
4373 4449
4440 4516
4441 delete $queue{$widget+0}; 4517 delete $queue{$widget+0};
4442 4518
4443 my ($w, $h) = $widget->size_request; 4519 my ($w, $h) = $widget->size_request;
4444 4520
4445 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4521 $w += $widget->{padding_x} * 2;
4446 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4522 $h += $widget->{padding_y} * 2;
4523
4524 $w = max $widget->{min_w}, $w;
4525 $h = max $widget->{min_h}, $h;
4447 4526
4448 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4527 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4449 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4528 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4450 4529
4451 $w = $widget->{force_w} if exists $widget->{force_w}; 4530 $w = $widget->{force_w} if exists $widget->{force_w};
4518 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4597 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4519 glMatrixMode GL_MODELVIEW; 4598 glMatrixMode GL_MODELVIEW;
4520 glLoadIdentity; 4599 glLoadIdentity;
4521 4600
4522 { 4601 {
4523 package CFPlus::UI::Base; 4602 package DC::UI::Base;
4524 4603
4525 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4604 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4526 (0, 0, $self->{w}, $self->{h}); 4605 (0, 0, $self->{w}, $self->{h});
4527 4606
4528 $self->_draw; 4607 $self->_draw;
4529 } 4608 }
4530} 4609}
4531 4610
4532############################################################################# 4611#############################################################################
4533 4612
4534package CFPlus::UI; 4613package DC::UI;
4535 4614
4536$ROOT = new CFPlus::UI::Root; 4615$ROOT = new DC::UI::Root;
4537$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4616$TOOLTIP = new DC::UI::Tooltip z => 900;
4538 4617
45391 46181
4540

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines