ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.439 by root, Sun Sep 16 13:04:36 2007 UTC vs.
Revision 1.476 by root, Sun Jan 11 23:45:52 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use Guard ();
9
10use DC;
10use CFPlus::Pod; 11use DC::Pod;
11use CFPlus::Texture; 12use DC::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
17our $TOOLTIP; 18our $TOOLTIP;
18our $BUTTON_STATE; 19our $BUTTON_STATE;
19 20
20our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
21 22
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
24 $_[0]->stop;
25
23 if (!$GRAB) { 26 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 29 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 30 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 45 }
43 46
44 $TOOLTIP->hide; 47 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 48 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 49 delete $TOOLTIP->{owner};
47}); 50};
48 51
49sub get_layout { 52sub get_layout {
50 my $layout; 53 my $layout;
51 54
52 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
88 91
89 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 94
92 $TOOLTIP_WATCHER->start; 95 $TOOLTIP_WATCHER->again;
93 } 96 }
94} 97}
95 98
96sub feed_sdl_button_down_event { 99sub feed_sdl_button_down_event {
97 my ($ev) = @_; 100 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 106 my $widget = $ROOT->find_widget ($x, $y);
104 107
105 $GRAB = $widget; 108 $GRAB = $widget;
106 $GRAB->update if $GRAB; 109 $GRAB->update if $GRAB;
107 110
108 $TOOLTIP_WATCHER->cb->(); 111 $TOOLTIP_WATCHER->invoke;
109 } 112 }
110 113
111 if ($GRAB) { 114 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 116 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 119
117 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
119 122
120 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 141 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 142 $grab->update if $grab;
140 $GRAB->update if $GRAB; 143 $GRAB->update if $GRAB;
141 144
142 check_hover $widget; 145 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 146 $TOOLTIP_WATCHER->invoke;
144 } 147 }
145} 148}
146 149
147sub feed_sdl_motion_event { 150sub feed_sdl_motion_event {
148 my ($ev) = @_; 151 my ($ev) = @_;
188# call when resolution changes etc. 191# call when resolution changes etc.
189sub rescale_widgets { 192sub rescale_widgets {
190 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
191 194
192 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 199
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 213 reconfigure_widgets;
211} 214}
212 215
213############################################################################# 216#############################################################################
214 217
215package CFPlus::UI::Event; 218package DC::UI::Event;
216 219
217sub xy { 220sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 222}
220 223
221############################################################################# 224#############################################################################
222 225
223package CFPlus::UI::Base; 226package DC::UI::Base;
224 227
225use strict; 228use strict;
226 229
227use CFPlus::OpenGL; 230use DC::OpenGL;
228 231
229sub new { 232sub new {
230 my $class = shift; 233 my $class = shift;
231 234
232 my $self = bless { 235 my $self = bless {
237 h => undef, 240 h => undef,
238 can_events => 1, 241 can_events => 1,
239 @_ 242 @_
240 }, $class; 243 }, $class;
241 244
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 246
244 for (keys %$self) { 247 for (keys %$self) {
245 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
247 } 250 }
248 } 251 }
249 252
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 258
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 261
259 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
277sub show { 280sub show {
278 my ($self) = @_; 281 my ($self) = @_;
279 282
280 return if $self->{parent}; 283 return if $self->{parent};
281 284
282 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
283} 286}
284 287
285sub set_visible { 288sub set_visible {
286 my ($self) = @_; 289 my ($self) = @_;
287 290
288 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 295
290 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
292 298
293 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
294 300
295 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
296 302
297 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
298} 304}
299 305
300sub set_invisible { 306sub set_invisible {
301 my ($self) = @_; 307 my ($self) = @_;
302 308
308 delete $self->{root}; 314 delete $self->{root};
309 315
310 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
312 318
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 319 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
315 321
316 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
318} 324}
337sub hide { 343sub hide {
338 my ($self) = @_; 344 my ($self) = @_;
339 345
340 $self->set_invisible; 346 $self->set_invisible;
341 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 351 $parent->remove ($self);
343 if $self->{parent}; 352 }
344} 353}
345 354
346sub move_abs { 355sub move_abs {
347 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
348 357
360 $self->{force_h} = $h; 369 $self->{force_h} = $h;
361 370
362 $self->realloc; 371 $self->realloc;
363} 372}
364 373
365# travsle the widget chain up to find the maximum "physical" size constraints 374# traverse the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh { 375sub get_max_wh {
367 my ($self) = @_; 376 my ($self) = @_;
368 377
369 return ($self->{alloc_w}, $self->{alloc_h}) 378 my ($w, $h) = @$self{qw(max_w max_h)};
370 if $self->{alloc_w} && !$self->{root}{realloc}{$self+0};
371 379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
372 return $self->{parent}->get_max_wh 382 ? $self->{parent}->get_max_wh
373 if $self->{parent};
374
375 ($::WIDTH, $::HEIGHT) 383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
376} 390}
377 391
378sub size_request { 392sub size_request {
379 require Carp; 393 require Carp;
380 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
388 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
389 403
390 if ($self->{aspect}) { 404 if ($self->{aspect}) {
391 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
392 406
393 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
394 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
395 409
396 # use alignment to adjust x, y 410 # use alignment to adjust x, y
397 411
398 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
399 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
440 454
441 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
442 456
443 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
444 458
445 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
446 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
447 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
448 } 462 }
449} 463}
450 464
451# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
452sub coord2local { 466sub coord2local {
453 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
454 468
455 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
456 470
457 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
458} 472}
459 473
460# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
461sub coord2global { 475sub coord2global {
462 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
463 477
464 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
465 479
466 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
467} 481}
468 482
469sub invoke_focus_in { 483sub invoke_focus_in {
518sub connect { 532sub connect {
519 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
520 534
521 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
522 536
523 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
524 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
525 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
526 } 540 }
527} 541}
528 542
566# in .xs 580# in .xs
567 581
568sub set_parent { 582sub set_parent {
569 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
570 584
571 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
572 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
573} 587}
574 588
575sub realloc { 589sub realloc {
576 my ($self) = @_; 590 my ($self) = @_;
614} 628}
615 629
616sub DESTROY { 630sub DESTROY {
617 my ($self) = @_; 631 my ($self) = @_;
618 632
619 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
620 634
621 local $@; 635 local $@;
622 eval { $self->destroy }; 636 eval { $self->destroy };
623 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
624 638
625 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
626} 640}
627 641
628############################################################################# 642#############################################################################
629 643
630package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
631 645
632our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
633 647
634use strict; 648use strict;
635use CFPlus::OpenGL; 649use DC::OpenGL;
636 650
637sub new { 651sub new {
638 my $class = shift; 652 my $class = shift;
639 653
640 $class->SUPER::new ( 654 $class->SUPER::new (
662 } 676 }
663} 677}
664 678
665############################################################################# 679#############################################################################
666 680
667package CFPlus::UI::Empty; 681package DC::UI::Empty;
668 682
669our @ISA = CFPlus::UI::Base::; 683our @ISA = DC::UI::Base::;
670 684
671sub new { 685sub new {
672 my ($class, %arg) = @_; 686 my ($class, %arg) = @_;
673 $class->SUPER::new (can_events => 0, %arg); 687 $class->SUPER::new (can_events => 0, %arg);
674} 688}
681 695
682sub draw { } 696sub draw { }
683 697
684############################################################################# 698#############################################################################
685 699
686package CFPlus::UI::Container; 700package DC::UI::Container;
687 701
688our @ISA = CFPlus::UI::Base::; 702our @ISA = DC::UI::Base::;
689 703
690sub new { 704sub new {
691 my ($class, %arg) = @_; 705 my ($class, %arg) = @_;
692 706
693 my $children = delete $arg{children}; 707 my $children = delete $arg{children};
787 $_->draw for $self->visible_children; 801 $_->draw for $self->visible_children;
788} 802}
789 803
790############################################################################# 804#############################################################################
791 805
792package CFPlus::UI::Bin; 806package DC::UI::Bin;
793 807
794our @ISA = CFPlus::UI::Container::; 808our @ISA = DC::UI::Container::;
795 809
796sub new { 810sub new {
797 my ($class, %arg) = @_; 811 my ($class, %arg) = @_;
798 812
799 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 813 my $child = (delete $arg{child}) || new DC::UI::Empty::;
800 814
801 $class->SUPER::new (children => [$child], %arg) 815 $class->SUPER::new (children => [$child], %arg)
802} 816}
803 817
804sub add { 818sub add {
811sub remove { 825sub remove {
812 my ($self, $widget) = @_; 826 my ($self, $widget) = @_;
813 827
814 $self->SUPER::remove ($widget); 828 $self->SUPER::remove ($widget);
815 829
816 $self->{children} = [new CFPlus::UI::Empty] 830 $self->{children} = [new DC::UI::Empty]
817 unless @{$self->{children}}; 831 unless @{$self->{children}};
818} 832}
819 833
820sub child { $_[0]->{children}[0] } 834sub child { $_[0]->{children}[0] }
821 835
832} 846}
833 847
834############################################################################# 848#############################################################################
835# back-buffered drawing area 849# back-buffered drawing area
836 850
837package CFPlus::UI::Window; 851package DC::UI::Window;
838 852
839our @ISA = CFPlus::UI::Bin::; 853our @ISA = DC::UI::Bin::;
840 854
841use CFPlus::OpenGL; 855use DC::OpenGL;
842 856
843sub new { 857sub new {
844 my ($class, %arg) = @_; 858 my ($class, %arg) = @_;
845 859
846 my $self = $class->SUPER::new (%arg); 860 my $self = $class->SUPER::new (%arg);
868} 882}
869 883
870sub render_child { 884sub render_child {
871 my ($self) = @_; 885 my ($self) = @_;
872 886
873 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 887 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
874 glClearColor 0, 0, 0, 0; 888 glClearColor 0, 0, 0, 0;
875 glClear GL_COLOR_BUFFER_BIT; 889 glClear GL_COLOR_BUFFER_BIT;
876 890
877 { 891 {
878 package CFPlus::UI::Base; 892 package DC::UI::Base;
879 893
880 local ($draw_x, $draw_y, $draw_w, $draw_h) = 894 local ($draw_x, $draw_y, $draw_w, $draw_h) =
881 (0, 0, $self->{w}, $self->{h}); 895 (0, 0, $self->{w}, $self->{h});
882 896
883 $self->_render; 897 $self->_render;
900 glDisable GL_TEXTURE_2D; 914 glDisable GL_TEXTURE_2D;
901} 915}
902 916
903############################################################################# 917#############################################################################
904 918
905package CFPlus::UI::ViewPort; 919package DC::UI::ViewPort;
906 920
907use List::Util qw(min max); 921use List::Util qw(min max);
908 922
909our @ISA = CFPlus::UI::Window::; 923our @ISA = DC::UI::Window::;
910 924
911sub new { 925sub new {
912 my $class = shift; 926 my $class = shift;
913 927
914 $class->SUPER::new ( 928 $class->SUPER::new (
998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1012 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
999 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1013 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1000 ) { 1014 ) {
1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1015 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1002 } else { 1016 } else {
1003 $self->CFPlus::UI::Base::find_widget ($x, $y) 1017 $self->DC::UI::Base::find_widget ($x, $y)
1004 } 1018 }
1005} 1019}
1006 1020
1007sub _render { 1021sub _render {
1008 my ($self) = @_; 1022 my ($self) = @_;
1009 1023
1010 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1024 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1011 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1025 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1012 1026
1013 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1027 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1014 1028
1015 $self->SUPER::_render; 1029 $self->SUPER::_render;
1016} 1030}
1017 1031
1018############################################################################# 1032#############################################################################
1019 1033
1020package CFPlus::UI::ScrolledWindow; 1034package DC::UI::ScrolledWindow;
1021 1035
1022our @ISA = CFPlus::UI::Table::; 1036our @ISA = DC::UI::Table::;
1023 1037
1024sub new { 1038sub new {
1025 my ($class, %arg) = @_; 1039 my ($class, %arg) = @_;
1026 1040
1027 my $child = delete $arg{child}; 1041 my $child = delete $arg{child};
1028 1042
1029 my $self; 1043 my $self;
1030 1044
1031 my $hslider = new CFPlus::UI::Slider 1045 my $hslider = new DC::UI::Slider
1032 c_col => 0, 1046 c_col => 0,
1033 c_row => 1, 1047 c_row => 1,
1034 vertical => 0, 1048 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix 1049 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub { 1050 on_changed => sub {
1037 $self->{hpos} = $_[1]; 1051 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 }, 1053 },
1040 ; 1054 ;
1041 1055
1042 my $vslider = new CFPlus::UI::Slider 1056 my $vslider = new DC::UI::Slider
1043 c_col => 1, 1057 c_col => 1,
1044 c_row => 0, 1058 c_row => 0,
1045 vertical => 1, 1059 vertical => 1,
1046 range => [0, 0, 1, 0.01], # HACK fix 1060 range => [0, 0, 1, 0.01], # HACK fix
1047 on_changed => sub { 1061 on_changed => sub {
1059 col_expand => [1, 0], 1073 col_expand => [1, 0],
1060 row_expand => [1, 0], 1074 row_expand => [1, 0],
1061 %arg, 1075 %arg,
1062 ); 1076 );
1063 1077
1064 $self->{vp} = new CFPlus::UI::ViewPort 1078 $self->{vp} = new DC::UI::ViewPort
1065 c_col => 0, 1079 c_col => 0,
1066 c_row => 0, 1080 c_row => 0,
1067 expand => 1, 1081 expand => 1,
1068 scroll_x => $self->{scroll_x}, 1082 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y}, 1083 scroll_y => $self->{scroll_y},
1195 $self->SUPER::invoke_size_allocate ($w, $h) 1209 $self->SUPER::invoke_size_allocate ($w, $h)
1196} 1210}
1197 1211
1198############################################################################# 1212#############################################################################
1199 1213
1200package CFPlus::UI::Frame; 1214package DC::UI::Frame;
1201 1215
1202our @ISA = CFPlus::UI::Bin::; 1216our @ISA = DC::UI::Bin::;
1203 1217
1204use CFPlus::OpenGL; 1218use DC::OpenGL;
1205 1219
1206sub new { 1220sub new {
1207 my $class = shift; 1221 my $class = shift;
1208 1222
1209 $class->SUPER::new ( 1223 $class->SUPER::new (
1228 $self->SUPER::_draw; 1242 $self->SUPER::_draw;
1229} 1243}
1230 1244
1231############################################################################# 1245#############################################################################
1232 1246
1233package CFPlus::UI::FancyFrame; 1247package DC::UI::FancyFrame;
1234 1248
1235our @ISA = CFPlus::UI::Bin::; 1249our @ISA = DC::UI::Bin::;
1236 1250
1237use CFPlus::OpenGL; 1251use DC::OpenGL;
1238 1252
1239sub new { 1253sub new {
1240 my ($class, %arg) = @_; 1254 my ($class, %arg) = @_;
1241 1255
1242 if ((exists $arg{label}) && !ref $arg{label}) { 1256 if ((exists $arg{label}) && !ref $arg{label}) {
1243 $arg{label} = new CFPlus::UI::Label 1257 $arg{label} = new DC::UI::Label
1244 align => 1, 1258 align => 1,
1245 valign => 0, 1259 valign => 0.5,
1246 text => $arg{label}, 1260 text => $arg{label},
1247 fontsize => ($arg{border} || 0.8) * 0.75; 1261 fontsize => ($arg{border} || 0.8) * 0.75;
1248 } 1262 }
1249 1263
1250 my $self = $class->SUPER::new ( 1264 my $self = $class->SUPER::new (
1251 # label => "", 1265 # label => "",
1252 fg => [0.6, 0.3, 0.1], 1266 fg => undef,
1253 border => 0.8, 1267 border => 0.8,
1254 style => 'single', 1268 style => 'single',
1255 %arg, 1269 %arg,
1256 ); 1270 );
1257 1271
1260 1274
1261sub add { 1275sub add {
1262 my ($self, @widgets) = @_; 1276 my ($self, @widgets) = @_;
1263 1277
1264 $self->SUPER::add (@widgets); 1278 $self->SUPER::add (@widgets);
1265 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1279 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1266} 1280}
1267 1281
1268sub border { 1282sub border {
1269 int $_[0]{border} * $::FONTSIZE 1283 int $_[0]{border} * $::FONTSIZE
1270} 1284}
1310 my $border = $self->border; 1324 my $border = $self->border;
1311 my ($w, $h) = ($self->{w}, $self->{h}); 1325 my ($w, $h) = ($self->{w}, $self->{h});
1312 1326
1313 $child->draw; 1327 $child->draw;
1314 1328
1315 glColor @{$self->{fg}}; 1329 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1316 glBegin GL_LINE_STRIP; 1330 glBegin GL_LINE_STRIP;
1317 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1331 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1318 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1332 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1333 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1320 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1334 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1328 } 1342 }
1329} 1343}
1330 1344
1331############################################################################# 1345#############################################################################
1332 1346
1333package CFPlus::UI::Toplevel; 1347package DC::UI::Toplevel;
1334 1348
1335our @ISA = CFPlus::UI::Bin::; 1349our @ISA = DC::UI::Bin::;
1336 1350
1337use CFPlus::OpenGL; 1351use DC::OpenGL;
1338 1352
1339my $bg = 1353my $bg =
1340 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1354 new_from_resource DC::Texture "d1_bg.png",
1341 mipmap => 1, wrap => 1; 1355 mipmap => 1, wrap => 1;
1342 1356
1343my @border = 1357my @border =
1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1358 map { new_from_resource DC::Texture $_, mipmap => 1 }
1345 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1359 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1346 1360
1347my @icon = 1361my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1362 map { new_from_resource DC::Texture $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png); 1363 qw(x1_move.png x1_resize.png);
1350 1364
1351sub new { 1365sub new {
1352 my ($class, %arg) = @_; 1366 my ($class, %arg) = @_;
1353 1367
1354 my $self = $class->SUPER::new ( 1368 my $self = $class->SUPER::new (
1355 bg => [1, 1, 1, 1], 1369 bg => [1, 1, 1, 1],
1356 border_bg => [1, 1, 1, 1], 1370 border_bg => [1, 1, 1, 1],
1357 border => 0.6, 1371 border => 0.8,
1358 can_events => 1, 1372 can_events => 1,
1359 min_w => 64, 1373 min_w => 64,
1360 min_h => 32, 1374 min_h => 32,
1361 %arg, 1375 %arg,
1362 ); 1376 );
1363 1377
1364 $self->{title_widget} = new CFPlus::UI::Label 1378 $self->{title_widget} = new DC::UI::Label
1365 align => 0, 1379 align => 0.5,
1366 valign => 1, 1380 valign => 1,
1367 text => $self->{title}, 1381 text => $self->{title},
1368 fontsize => $self->{border}, 1382 fontsize => $self->{border},
1369 if exists $self->{title}; 1383 if exists $self->{title};
1370 1384
1371 if ($self->{has_close_button}) { 1385 if ($self->{has_close_button}) {
1372 $self->{close_button} = 1386 $self->{close_button} =
1373 new CFPlus::UI::ImageButton 1387 new DC::UI::ImageButton
1374 path => 'x1_close.png', 1388 path => 'x1_close.png',
1375 on_activate => sub { $self->emit ("delete") }; 1389 on_activate => sub { $self->emit ("delete") };
1376 1390
1377 $self->CFPlus::UI::Container::add ($self->{close_button}); 1391 $self->DC::UI::Container::add ($self->{close_button});
1378 } 1392 }
1379 1393
1380 $self 1394 $self
1381} 1395}
1382 1396
1383sub add { 1397sub add {
1384 my ($self, @widgets) = @_; 1398 my ($self, @widgets) = @_;
1385 1399
1386 $self->SUPER::add (@widgets); 1400 $self->SUPER::add (@widgets);
1387 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1401 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1388 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1402 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1389} 1403}
1390 1404
1391sub border { 1405sub border {
1392 int $_[0]{border} * $::FONTSIZE 1406 int $_[0]{border} * $::FONTSIZE
1407}
1408
1409sub get_max_wh {
1410 my ($self) = @_;
1411
1412 return ($self->{w}, $self->{h})
1413 if $self->{visible} && $self->{w};
1414
1415 $self->SUPER::get_max_wh
1393} 1416}
1394 1417
1395sub size_request { 1418sub size_request {
1396 my ($self) = @_; 1419 my ($self) = @_;
1397 1420
1522 glEnable GL_TEXTURE_2D; 1545 glEnable GL_TEXTURE_2D;
1523 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1546 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1524 1547
1525 my $border = $self->border; 1548 my $border = $self->border;
1526 1549
1550 if ($border) {
1527 glColor @{ $self->{border_bg} }; 1551 glColor @{ $self->{border_bg} };
1528 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1552 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1529 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1553 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1530 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1554 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1531 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1555 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1532 1556
1533 # move 1557 # move
1534 my $w2 = ($w - $border) * .5; 1558 my $w2 = ($w - $border) * .5;
1535 my $h2 = ($h - $border) * .5; 1559 my $h2 = ($h - $border) * .5;
1536 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1560 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1537 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1561 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1538 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1562 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1539 1563
1540 # resize 1564 # resize
1541 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1565 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1542 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1566 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1543 unless $self->{has_close_button}; 1567 unless $self->{has_close_button};
1544 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1568 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1545 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1569 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1570 }
1546 1571
1547 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1572 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1548 glColor @{ $self->{bg} }; 1573 glColor @{ $self->{bg} };
1549 1574
1550 # TODO: repeat texture not scale 1575 # TODO: repeat texture not scale
1569 if $self->{close_button}; 1594 if $self->{close_button};
1570} 1595}
1571 1596
1572############################################################################# 1597#############################################################################
1573 1598
1574package CFPlus::UI::Table; 1599package DC::UI::Table;
1575 1600
1576our @ISA = CFPlus::UI::Container::; 1601our @ISA = DC::UI::Container::;
1577 1602
1578use List::Util qw(max sum); 1603use List::Util qw(max sum);
1579 1604
1580use CFPlus::OpenGL; 1605use DC::OpenGL;
1581 1606
1582sub new { 1607sub new {
1583 my $class = shift; 1608 my $class = shift;
1584 1609
1585 $class->SUPER::new ( 1610 $class->SUPER::new (
1629 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1654 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1630 1655
1631 my $sw = sum @w[$c .. $c + $cs - 1]; 1656 my $sw = sum @w[$c .. $c + $cs - 1];
1632 1657
1633 if ($w > $sw) { 1658 if ($w > $sw) {
1634 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1659 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1635 } 1660 }
1636 } 1661 }
1637 1662
1638 # second pass, rows 1663 # second pass, rows
1639 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1664 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1640 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1665 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1641 1666
1642 my $sh = sum @h[$r .. $r + $rs - 1]; 1667 my $sh = sum @h[$r .. $r + $rs - 1];
1643 1668
1644 if ($h > $sh) { 1669 if ($h > $sh) {
1645 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1670 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1646 } 1671 }
1647 } 1672 }
1648 1673
1649 (\@w, \@h) 1674 (\@w, \@h)
1650} 1675}
1673 @col_expand = (1) x @$ws unless @col_expand; 1698 @col_expand = (1) x @$ws unless @col_expand;
1674 my $col_expand = (sum @col_expand) || 1; 1699 my $col_expand = (sum @col_expand) || 1;
1675 1700
1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1701 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1677 1702
1678 CFPlus::UI::harmonize $ws; 1703 DC::UI::harmonize $ws;
1679 1704
1680 my @row_expand = @{$self->{row_expand}}; 1705 my @row_expand = @{$self->{row_expand}};
1681 @row_expand = (1) x @$ws unless @row_expand; 1706 @row_expand = (1) x @$ws unless @row_expand;
1682 my $row_expand = (sum @row_expand) || 1; 1707 my $row_expand = (sum @row_expand) || 1;
1683 1708
1684 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1709 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1685 1710
1686 CFPlus::UI::harmonize $hs; 1711 DC::UI::harmonize $hs;
1687 1712
1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1713 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1714 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1690 1715
1691 for my $widget ($self->children) { 1716 for my $widget ($self->children) {
1700 1 1725 1
1701} 1726}
1702 1727
1703############################################################################# 1728#############################################################################
1704 1729
1705package CFPlus::UI::Fixed; 1730package DC::UI::Fixed;
1706 1731
1707use List::Util qw(min max); 1732use List::Util qw(min max);
1708 1733
1709our @ISA = CFPlus::UI::Container::; 1734our @ISA = DC::UI::Container::;
1710 1735
1711sub _scale($$$) { 1736sub _scale($$$) {
1712 my ($rel, $val, $max) = @_; 1737 my ($rel, $val, $max) = @_;
1713 1738
1714 $rel ? $val * $max : $val 1739 $rel ? $val * $max : $val
1766 1 1791 1
1767} 1792}
1768 1793
1769############################################################################# 1794#############################################################################
1770 1795
1771package CFPlus::UI::Box; 1796package DC::UI::Box;
1772 1797
1773our @ISA = CFPlus::UI::Container::; 1798our @ISA = DC::UI::Container::;
1774 1799
1775sub size_request { 1800sub size_request {
1776 my ($self) = @_; 1801 my ($self) = @_;
1777 1802
1778 my @children = $self->visible_children; 1803 my @children = $self->visible_children;
1813 $req[$_] += $space * $children[$_]{expand} 1838 $req[$_] += $space * $children[$_]{expand}
1814 for 0 .. $#children; 1839 for 0 .. $#children;
1815 } 1840 }
1816 } 1841 }
1817 1842
1818 CFPlus::UI::harmonize \@req; 1843 DC::UI::harmonize \@req;
1819 1844
1820 my $pos = 0; 1845 my $pos = 0;
1821 for (0 .. $#children) { 1846 for (0 .. $#children) {
1822 my $alloc = $req[$_]; 1847 my $alloc = $req[$_];
1823 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1848 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1828 1 1853 1
1829} 1854}
1830 1855
1831############################################################################# 1856#############################################################################
1832 1857
1833package CFPlus::UI::HBox; 1858package DC::UI::HBox;
1834 1859
1835our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1836 1861
1837sub new { 1862sub new {
1838 my $class = shift; 1863 my $class = shift;
1839 1864
1840 $class->SUPER::new ( 1865 $class->SUPER::new (
1843 ) 1868 )
1844} 1869}
1845 1870
1846############################################################################# 1871#############################################################################
1847 1872
1848package CFPlus::UI::VBox; 1873package DC::UI::VBox;
1849 1874
1850our @ISA = CFPlus::UI::Box::; 1875our @ISA = DC::UI::Box::;
1851 1876
1852sub new { 1877sub new {
1853 my $class = shift; 1878 my $class = shift;
1854 1879
1855 $class->SUPER::new ( 1880 $class->SUPER::new (
1858 ) 1883 )
1859} 1884}
1860 1885
1861############################################################################# 1886#############################################################################
1862 1887
1863package CFPlus::UI::Label; 1888package DC::UI::Label;
1864 1889
1865our @ISA = CFPlus::UI::DrawBG::; 1890our @ISA = DC::UI::DrawBG::;
1866 1891
1867use CFPlus::OpenGL; 1892use DC::OpenGL;
1868 1893
1869sub new { 1894sub new {
1870 my ($class, %arg) = @_; 1895 my ($class, %arg) = @_;
1871 1896
1872 my $self = $class->SUPER::new ( 1897 my $self = $class->SUPER::new (
1877 #text => initial text 1902 #text => initial text
1878 #markup => initial narkup 1903 #markup => initial narkup
1879 #max_w => maximum pixel width 1904 #max_w => maximum pixel width
1880 #style => 0, # render flags 1905 #style => 0, # render flags
1881 ellipsise => 3, # end 1906 ellipsise => 3, # end
1882 layout => (new CFPlus::Layout), 1907 layout => (new DC::Layout),
1883 fontsize => 1, 1908 fontsize => 1,
1884 align => -1, 1909 align => 0.5,
1885 valign => -1, 1910 valign => 0.5,
1886 padding_x => 2, 1911 padding_x => 4,
1887 padding_y => 2, 1912 padding_y => 2,
1888 can_events => 0, 1913 can_events => 0,
1889 %arg 1914 %arg
1890 ); 1915 );
1891 1916
1892 if (exists $self->{template}) { 1917 if (exists $self->{template}) {
1893 my $layout = new CFPlus::Layout; 1918 my $layout = new DC::Layout;
1894 $layout->set_text (delete $self->{template}); 1919 $layout->set_text (delete $self->{template});
1895 $self->{template} = $layout; 1920 $self->{template} = $layout;
1896 } 1921 }
1897 1922
1898 if (exists $self->{markup}) { 1923 if (exists $self->{markup}) {
1957 1982
1958 $self->{size_req} ||= do { 1983 $self->{size_req} ||= do {
1959 my ($max_w, $max_h) = $self->get_max_wh; 1984 my ($max_w, $max_h) = $self->get_max_wh;
1960 1985
1961 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1986 $self->{layout}->set_font ($self->{font}) if $self->{font};
1962 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1987 $self->{layout}->set_width ($max_w);
1963 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1988 $self->{layout}->set_ellipsise ($self->{ellipsise});
1964 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1989 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1965 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1990 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1966 1991
1967 my ($w, $h) = $self->{layout}->size; 1992 my ($w, $h) = $self->{layout}->size;
1968 1993
1969 if (exists $self->{template}) { 1994 if (exists $self->{template}) {
1970 $self->{template}->set_font ($self->{font}) if $self->{font}; 1995 $self->{template}->set_font ($self->{font}) if $self->{font};
1971 $self->{template}->set_width ($self->{max_w} || -1); 1996 $self->{template}->set_width ($max_w);
1972 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1973 1998
1974 my ($w2, $h2) = $self->{template}->size; 1999 my ($w2, $h2) = $self->{template}->size;
1975 2000
1976 $w = List::Util::max $w, $w2; 2001 $w = List::Util::max $w, $w2;
2032 2057
2033 [$self->{layout}->size] 2058 [$self->{layout}->size]
2034 }; 2059 };
2035 2060
2036 unless (exists $self->{ox}) { 2061 unless (exists $self->{ox}) {
2037 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2062 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2038 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2063 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2039 : ($self->{w} - $size->[0]) * 0.5);
2040
2041 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2042 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2043 : ($self->{h} - $size->[1]) * 0.5);
2044 2064
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2065 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2046 }; 2066 };
2047 2067
2048# unless ($self->{list}) { 2068# unless ($self->{list}) {
2049# $self->{list} = CFPlus::OpenGL::glGenList; 2069# $self->{list} = DC::OpenGL::glGenList;
2050# CFPlus::OpenGL::glNewList $self->{list}; 2070# DC::OpenGL::glNewList $self->{list};
2051# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2071# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052# CFPlus::OpenGL::glEndList; 2072# DC::OpenGL::glEndList;
2053# } 2073# }
2054# 2074#
2055# CFPlus::OpenGL::glCallList $self->{list}; 2075# DC::OpenGL::glCallList $self->{list};
2056 2076
2057 $self->{layout}->draw; 2077 $self->{layout}->draw;
2058} 2078}
2059 2079
2060#sub destroy { 2080#sub destroy {
2061# my ($self) = @_; 2081# my ($self) = @_;
2062# 2082#
2063# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2083# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2064# 2084#
2065# $self->SUPER::destroy; 2085# $self->SUPER::destroy;
2066#} 2086#}
2067 2087
2068############################################################################# 2088#############################################################################
2069 2089
2070package CFPlus::UI::EntryBase; 2090package DC::UI::EntryBase;
2071 2091
2072our @ISA = CFPlus::UI::Label::; 2092our @ISA = DC::UI::Label::;
2073 2093
2074use CFPlus::OpenGL; 2094use DC::OpenGL;
2075 2095
2076sub new { 2096sub new {
2077 my $class = shift; 2097 my $class = shift;
2078 2098
2079 $class->SUPER::new ( 2099 $class->SUPER::new (
2080 fg => [1, 1, 1], 2100 fg => [1, 1, 1],
2081 bg => [0, 0, 0, 0.2], 2101 bg => [0, 0, 0, 0.2],
2082 outline => [0.6, 0.3, 0.1], 2102 outline => undef,
2083 active_bg => [0, 0, 1, .2], 2103 active_bg => [0, 0, 1, .2],
2084 active_fg => [1, 1, 1], 2104 active_fg => [1, 1, 1],
2085 active_outline => [1, 1, 0], 2105 active_outline => [1, 1, 0],
2086 can_hover => 1, 2106 can_hover => 1,
2087 can_focus => 1, 2107 can_focus => 1,
2108 align => 0,
2088 valign => 0, 2109 valign => 0.5,
2089 can_events => 1, 2110 can_events => 1,
2090 ellipsise => 0, 2111 ellipsise => 0,
2112 padding_x => 4,
2113 padding_y => 2,
2091 #text => ... 2114 #text => ...
2092 #hidden => "*", 2115 #hidden => "*",
2093 @_ 2116 @_
2094 ) 2117 )
2095} 2118}
2142 2165
2143 my $text = $self->get_text; 2166 my $text = $self->get_text;
2144 2167
2145 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2168 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2146 2169
2147 if ($uni == 8) { 2170 if ($sym == DC::SDLK_BACKSPACE) {
2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2171 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2149 } elsif ($uni == 127) { 2172 } elsif ($sym == DC::SDLK_DELETE) {
2150 substr $text, $self->{cursor}, 1, ""; 2173 substr $text, $self->{cursor}, 1, "";
2151 } elsif ($sym == CFPlus::SDLK_LEFT) { 2174 } elsif ($sym == DC::SDLK_LEFT) {
2152 --$self->{cursor} if $self->{cursor}; 2175 --$self->{cursor} if $self->{cursor};
2153 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2176 } elsif ($sym == DC::SDLK_RIGHT) {
2154 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2177 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2155 } elsif ($sym == CFPlus::SDLK_HOME) { 2178 } elsif ($sym == DC::SDLK_HOME) {
2156 # what a hack 2179 # what a hack
2157 $self->{cursor} = 2180 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2181 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1 2182 ? length $1
2160 : 0; 2183 : 0;
2161 } elsif ($sym == CFPlus::SDLK_END) { 2184 } elsif ($sym == DC::SDLK_END) {
2162 # uh, again 2185 # uh, again
2163 $self->{cursor} = 2186 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2187 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2165 ? $self->{cursor} + length $1 2188 ? $self->{cursor} + length $1
2166 : length $self->{text}; 2189 : length $self->{text};
2257 glColor @{$self->{active_outline}}; 2280 glColor @{$self->{active_outline}};
2258 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2281 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2259 glLineWidth 1; 2282 glLineWidth 1;
2260 2283
2261 } else { 2284 } else {
2262 glColor @{$self->{outline}}; 2285 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2263 glBegin GL_LINE_STRIP; 2286 glBegin GL_LINE_STRIP;
2264 glVertex .5, $self->{h} * .5; 2287 glVertex .5, $self->{h} * .5;
2265 glVertex .5, $self->{h} - 2.5; 2288 glVertex .5, $self->{h} - 2.5;
2266 glVertex $self->{w} - .5, $self->{h} - 2.5; 2289 glVertex $self->{w} - .5, $self->{h} - 2.5;
2267 glVertex $self->{w} - .5, $self->{h} * .5; 2290 glVertex $self->{w} - .5, $self->{h} * .5;
2269 } 2292 }
2270} 2293}
2271 2294
2272############################################################################# 2295#############################################################################
2273 2296
2274package CFPlus::UI::Entry; 2297package DC::UI::Entry;
2275 2298
2276our @ISA = CFPlus::UI::EntryBase::; 2299our @ISA = DC::UI::EntryBase::;
2277 2300
2278use CFPlus::OpenGL; 2301use DC::OpenGL;
2302
2303sub new {
2304 my $class = shift;
2305
2306 $class->SUPER::new (
2307 history_pointer => -1,
2308 @_
2309 )
2310}
2311
2279 2312
2280sub invoke_key_down { 2313sub invoke_key_down {
2281 my ($self, $ev) = @_; 2314 my ($self, $ev) = @_;
2282 2315
2283 my $sym = $ev->{sym}; 2316 my $sym = $ev->{sym};
2289 $self->{history_pointer} = -1; 2322 $self->{history_pointer} = -1;
2290 $self->{history_saveback} = ''; 2323 $self->{history_saveback} = '';
2291 $self->emit (activate => $txt); 2324 $self->emit (activate => $txt);
2292 $self->update; 2325 $self->update;
2293 2326
2294 } elsif ($sym == CFPlus::SDLK_UP) { 2327 } elsif ($sym == DC::SDLK_UP) {
2295 if ($self->{history_pointer} < 0) { 2328 if ($self->{history_pointer} < 0) {
2296 $self->{history_saveback} = $self->get_text; 2329 $self->{history_saveback} = $self->get_text;
2297 } 2330 }
2298 if (@{$self->{history} || []} > 0) { 2331 if (@{$self->{history} || []} > 0) {
2299 $self->{history_pointer}++; 2332 $self->{history_pointer}++;
2301 $self->{history_pointer} = @{$self->{history} || []} - 1; 2334 $self->{history_pointer} = @{$self->{history} || []} - 1;
2302 } 2335 }
2303 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2336 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2304 } 2337 }
2305 2338
2306 } elsif ($sym == CFPlus::SDLK_DOWN) { 2339 } elsif ($sym == DC::SDLK_DOWN) {
2307 $self->{history_pointer}--; 2340 $self->{history_pointer}--;
2308 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2341 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2309 2342
2310 if ($self->{history_pointer} >= 0) { 2343 if ($self->{history_pointer} >= 0) {
2311 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2344 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2312 } else { 2345 } else {
2346 if (defined $self->{history_saveback}) {
2313 $self->set_text ($self->{history_saveback}); 2347 $self->set_text ($self->{history_saveback});
2348 $self->{history_saveback} = undef;
2349 }
2314 } 2350 }
2315 2351
2316 } else { 2352 } else {
2317 return $self->SUPER::invoke_key_down ($ev) 2353 return $self->SUPER::invoke_key_down ($ev)
2318 } 2354 }
2320 1 2356 1
2321} 2357}
2322 2358
2323############################################################################# 2359#############################################################################
2324 2360
2325package CFPlus::UI::TextEdit; 2361package DC::UI::TextEdit;
2326 2362
2327our @ISA = CFPlus::UI::EntryBase::; 2363our @ISA = DC::UI::EntryBase::;
2328 2364
2329use CFPlus::OpenGL; 2365use DC::OpenGL;
2366
2367sub new {
2368 my $class = shift;
2369
2370 $class->SUPER::new (
2371 padding_y => 4,
2372
2373 @_
2374 )
2375}
2330 2376
2331sub move_cursor_ver { 2377sub move_cursor_ver {
2332 my ($self, $dy) = @_; 2378 my ($self, $dy) = @_;
2333 2379
2334 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2380 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2335 2381
2336 $y += $dy; 2382 $line += $dy;
2337 2383
2338 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2384 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2339 $self->{cursor} = $index; 2385 $self->{cursor} = $index;
2340 delete $self->{cur_h}; 2386 delete $self->{cur_h};
2341 $self->update; 2387 $self->update;
2342 return; 2388 return;
2343 } 2389 }
2346sub invoke_key_down { 2392sub invoke_key_down {
2347 my ($self, $ev) = @_; 2393 my ($self, $ev) = @_;
2348 2394
2349 my $sym = $ev->{sym}; 2395 my $sym = $ev->{sym};
2350 2396
2351 if ($sym == CFPlus::SDLK_UP) { 2397 if ($sym == DC::SDLK_UP) {
2352 $self->move_cursor_ver (-1); 2398 $self->move_cursor_ver (-1);
2353 } elsif ($sym == CFPlus::SDLK_DOWN) { 2399 } elsif ($sym == DC::SDLK_DOWN) {
2354 $self->move_cursor_ver (+1); 2400 $self->move_cursor_ver (+1);
2355 } else { 2401 } else {
2356 return $self->SUPER::invoke_key_down ($ev) 2402 return $self->SUPER::invoke_key_down ($ev)
2357 } 2403 }
2358 2404
2359 1 2405 1
2360} 2406}
2361 2407
2362############################################################################# 2408#############################################################################
2363 2409
2364package CFPlus::UI::ButtonBin; 2410package DC::UI::ButtonBin;
2365 2411
2366our @ISA = CFPlus::UI::Bin::; 2412our @ISA = DC::UI::Bin::;
2367 2413
2368use CFPlus::OpenGL; 2414use DC::OpenGL;
2369 2415
2370my @tex = 2416my @tex =
2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2417 map { new_from_resource DC::Texture $_, mipmap => 1 }
2372 qw(b1_button_inactive.png b1_button_active.png); 2418 qw(b1_button_inactive.png b1_button_active.png);
2373 2419
2374sub new { 2420sub new {
2375 my $class = shift; 2421 my $class = shift;
2376 2422
2377 $class->SUPER::new ( 2423 $class->SUPER::new (
2378 can_hover => 1, 2424 can_hover => 1,
2379 align => 0, 2425 align => 0.5,
2380 valign => 0, 2426 valign => 0.5,
2381 can_events => 1, 2427 can_events => 1,
2382 @_ 2428 @_
2383 ) 2429 )
2384} 2430}
2385 2431
2408 $self->SUPER::_draw; 2454 $self->SUPER::_draw;
2409} 2455}
2410 2456
2411############################################################################# 2457#############################################################################
2412 2458
2413package CFPlus::UI::Button; 2459package DC::UI::Button;
2414 2460
2415our @ISA = CFPlus::UI::Label::; 2461our @ISA = DC::UI::Label::;
2416 2462
2417use CFPlus::OpenGL; 2463use DC::OpenGL;
2418 2464
2419my @tex = 2465my @tex =
2420 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2466 map { new_from_resource DC::Texture $_, mipmap => 1 }
2421 qw(b1_button_inactive.png b1_button_active.png); 2467 qw(b1_button_inactive.png b1_button_active.png);
2422 2468
2423sub new { 2469sub new {
2424 my $class = shift; 2470 my $class = shift;
2425 2471
2426 $class->SUPER::new ( 2472 $class->SUPER::new (
2427 padding_x => 4, 2473 padding_x => 8,
2428 padding_y => 4, 2474 padding_y => 4,
2429 fg => [1.0, 1.0, 1.0], 2475 fg => [1.0, 1.0, 1.0],
2430 active_fg => [0.8, 0.8, 0.8], 2476 active_fg => [0.8, 0.8, 0.8],
2431 can_hover => 1, 2477 can_hover => 1,
2432 align => 0, 2478 align => 0.5,
2433 valign => 0, 2479 valign => 0.5,
2434 can_events => 1, 2480 can_events => 1,
2435 @_ 2481 @_
2436 ) 2482 )
2437} 2483}
2438 2484
2463 $self->SUPER::_draw; 2509 $self->SUPER::_draw;
2464} 2510}
2465 2511
2466############################################################################# 2512#############################################################################
2467 2513
2468package CFPlus::UI::CheckBox; 2514package DC::UI::CheckBox;
2469 2515
2470our @ISA = CFPlus::UI::DrawBG::; 2516our @ISA = DC::UI::DrawBG::;
2471 2517
2472my @tex = 2518my @tex =
2473 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2519 map { new_from_resource DC::Texture $_, mipmap => 1 }
2474 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2520 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2475 2521
2476use CFPlus::OpenGL; 2522use DC::OpenGL;
2477 2523
2478sub new { 2524sub new {
2479 my $class = shift; 2525 my $class = shift;
2480 2526
2481 $class->SUPER::new ( 2527 $class->SUPER::new (
2528 fontsize => 1,
2482 padding_x => 2, 2529 padding_x => 2,
2483 padding_y => 2, 2530 padding_y => 2,
2484 fg => [1, 1, 1], 2531 fg => [1, 1, 1],
2485 active_fg => [1, 1, 0], 2532 active_fg => [1, 1, 0],
2486 bg => [0, 0, 0, 0.2], 2533 bg => [0, 0, 0, 0.2],
2492} 2539}
2493 2540
2494sub size_request { 2541sub size_request {
2495 my ($self) = @_; 2542 my ($self) = @_;
2496 2543
2497 (6) x 2 2544 ($self->{fontsize} * $::FONTSIZE) x 2
2498} 2545}
2499 2546
2500sub toggle { 2547sub toggle {
2501 my ($self) = @_; 2548 my ($self) = @_;
2502 2549
2538 glDisable GL_TEXTURE_2D; 2585 glDisable GL_TEXTURE_2D;
2539} 2586}
2540 2587
2541############################################################################# 2588#############################################################################
2542 2589
2543package CFPlus::UI::Image; 2590package DC::UI::Image;
2544 2591
2545our @ISA = CFPlus::UI::Base::; 2592our @ISA = DC::UI::Base::;
2546 2593
2547use CFPlus::OpenGL; 2594use DC::OpenGL;
2548 2595
2549our %texture_cache; 2596our %texture_cache;
2550 2597
2551sub new { 2598sub new {
2552 my $class = shift; 2599 my $class = shift;
2559 2606
2560 $self->{path} || $self->{tex} 2607 $self->{path} || $self->{tex}
2561 or Carp::croak "'path' or 'tex' attributes required"; 2608 or Carp::croak "'path' or 'tex' attributes required";
2562 2609
2563 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2610 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2564 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2611 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2565 2612
2566 CFPlus::weaken $texture_cache{$self->{path}}; 2613 DC::weaken $texture_cache{$self->{path}};
2567 2614
2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2615 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2569 2616
2570 $self 2617 $self
2571} 2618}
2572 2619
2573sub STORABLE_freeze { 2620sub STORABLE_freeze {
2574 my ($self, $cloning) = @_; 2621 my ($self, $cloning) = @_;
2575 2622
2576 $self->{path} 2623 $self->{path}
2577 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2624 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2578 2625
2579 $self->{path} 2626 $self->{path}
2580} 2627}
2581 2628
2582sub STORABLE_attach { 2629sub STORABLE_attach {
2613 glDisable GL_TEXTURE_2D; 2660 glDisable GL_TEXTURE_2D;
2614} 2661}
2615 2662
2616############################################################################# 2663#############################################################################
2617 2664
2618package CFPlus::UI::ImageButton; 2665package DC::UI::ImageButton;
2619 2666
2620our @ISA = CFPlus::UI::Image::; 2667our @ISA = DC::UI::Image::;
2621 2668
2622use CFPlus::OpenGL; 2669use DC::OpenGL;
2623 2670
2624my %textures; 2671my %textures;
2625 2672
2626sub new { 2673sub new {
2627 my $class = shift; 2674 my $class = shift;
2630 padding_x => 4, 2677 padding_x => 4,
2631 padding_y => 4, 2678 padding_y => 4,
2632 fg => [1, 1, 1], 2679 fg => [1, 1, 1],
2633 active_fg => [0, 0, 1], 2680 active_fg => [0, 0, 1],
2634 can_hover => 1, 2681 can_hover => 1,
2635 align => 0, 2682 align => 0.5,
2636 valign => 0, 2683 valign => 0.5,
2637 can_events => 1, 2684 can_events => 1,
2638 @_ 2685 @_
2639 ); 2686 );
2640} 2687}
2641 2688
2655 1 2702 1
2656} 2703}
2657 2704
2658############################################################################# 2705#############################################################################
2659 2706
2660package CFPlus::UI::VGauge; 2707package DC::UI::VGauge;
2661 2708
2662our @ISA = CFPlus::UI::Base::; 2709our @ISA = DC::UI::Base::;
2663 2710
2664use List::Util qw(min max); 2711use List::Util qw(min max);
2665 2712
2666use CFPlus::OpenGL; 2713use DC::OpenGL;
2667 2714
2668my %tex = ( 2715my %tex = (
2669 food => [ 2716 food => [
2670 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2717 map { new_from_resource DC::Texture $_, mipmap => 1 }
2671 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2718 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2672 ], 2719 ],
2673 grace => [ 2720 grace => [
2674 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2721 map { new_from_resource DC::Texture $_, mipmap => 1 }
2675 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2722 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2676 ], 2723 ],
2677 hp => [ 2724 hp => [
2678 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2725 map { new_from_resource DC::Texture $_, mipmap => 1 }
2679 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2726 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2680 ], 2727 ],
2681 mana => [ 2728 mana => [
2682 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2729 map { new_from_resource DC::Texture $_, mipmap => 1 }
2683 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2730 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2684 ], 2731 ],
2685); 2732);
2686 2733
2687# eg. VGauge->new (gauge => 'food'), default gauge: food 2734# eg. VGauge->new (gauge => 'food'), default gauge: food
2790 glDisable GL_TEXTURE_2D; 2837 glDisable GL_TEXTURE_2D;
2791} 2838}
2792 2839
2793############################################################################# 2840#############################################################################
2794 2841
2795package CFPlus::UI::Progress; 2842package DC::UI::Progress;
2796 2843
2797our @ISA = CFPlus::UI::Label::; 2844our @ISA = DC::UI::Label::;
2798 2845
2799use CFPlus::OpenGL; 2846use DC::OpenGL;
2800 2847
2801sub new { 2848sub new {
2802 my ($class, %arg) = @_; 2849 my ($class, %arg) = @_;
2803 2850
2804 my $self = $class->SUPER::new ( 2851 my $self = $class->SUPER::new (
2852 padding_x => 2,
2853 padding_y => 2,
2805 fg => [1, 1, 1], 2854 fg => [1, 1, 1],
2806 bg => [0, 0, 1, 0.2], 2855 bg => [0, 0, 1, 0.2],
2807 bar => [0.7, 0.5, 0.1, 0.8], 2856 bar => [0.7, 0.5, 0.1, 0.8],
2808 outline => [0.4, 0.3, 0], 2857 outline => [0.4, 0.3, 0],
2809 fontsize => 0.9, 2858 fontsize => 0.9,
2810 valign => 0, 2859 valign => 0.5,
2811 align => 0, 2860 align => 0.5,
2812 can_events => 1, 2861 can_events => 1,
2813 ellipsise => 1, 2862 ellipsise => 1,
2814 label => "%d%%", 2863 label => "%d%%",
2815 %arg, 2864 %arg,
2816 ); 2865 );
2824 my ($self, $label) = @_; 2873 my ($self, $label) = @_;
2825 2874
2826 return if $self->{label} eq $label; 2875 return if $self->{label} eq $label;
2827 $self->{label} = $label; 2876 $self->{label} = $label;
2828 2877
2829 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2878 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2830} 2879}
2831 2880
2832sub set_value { 2881sub set_value {
2833 my ($self, $value) = @_; 2882 my ($self, $value) = @_;
2834 2883
2849 my ($self) = @_; 2898 my ($self) = @_;
2850 2899
2851 glEnable GL_BLEND; 2900 glEnable GL_BLEND;
2852 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2901 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2853 2902
2903 my $px = $self->{padding_x};
2904 my $py = $self->{padding_y};
2905
2854 if ($self->{value} >= 0) { 2906 if ($self->{value} >= 0) {
2855 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2907 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2856 2908
2857 glColor_premultiply @{$self->{bar}}; 2909 glColor_premultiply @{$self->{bar}};
2858 glRect 2, 2, $s, $self->{h} - 2; 2910 glRect $px, $py, $s, $self->{h} - $py;
2859 glColor_premultiply @{$self->{bg}}; 2911 glColor_premultiply @{$self->{bg}};
2860 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2912 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2861 } 2913 }
2862 2914
2863 glColor_premultiply @{$self->{outline}}; 2915 glColor_premultiply @{$self->{outline}};
2916
2917 $px -= .5;
2918 $py -= .5;
2919
2864 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2920 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2865 2921
2866 glDisable GL_BLEND; 2922 glDisable GL_BLEND;
2867 2923
2868 { 2924 {
2869 local $self->{bg}; # do not draw background 2925 local $self->{bg}; # do not draw background
2871 } 2927 }
2872} 2928}
2873 2929
2874############################################################################# 2930#############################################################################
2875 2931
2876package CFPlus::UI::ExperienceProgress; 2932package DC::UI::ExperienceProgress;
2877 2933
2878our @ISA = CFPlus::UI::Progress::; 2934our @ISA = DC::UI::Progress::;
2879 2935
2880sub new { 2936sub new {
2881 my ($class, %arg) = @_; 2937 my ($class, %arg) = @_;
2882 2938
2939 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2940
2883 my $self = $class->SUPER::new ( 2941 my $self = $class->SUPER::new (
2942 %arg,
2884 tooltip => sub { 2943 tooltip => sub {
2885 my ($self) = @_; 2944 my ($self) = @_;
2886 2945
2887 sprintf "level %d\n%s points\n%s next level\n%s to go", 2946 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2947 $tt,
2888 $self->{lvl}, 2948 $self->{lvl},
2889 ::formsep ($self->{exp}), 2949 ::formsep ($self->{exp}),
2890 ::formsep ($self->{nxt}), 2950 ::formsep ($self->{nxt}),
2891 ::formsep ($self->{nxt} - $self->{exp}), 2951 ::formsep ($self->{nxt} - $self->{exp}),
2952 $self->_percent * 100,
2892 }, 2953 },
2893 %arg
2894 ); 2954 );
2895 2955
2896 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2956 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2897 if $::CONN; 2957 if $::CONN;
2898 2958
2906 if $::CONN; 2966 if $::CONN;
2907 2967
2908 $self->SUPER::DESTROY; 2968 $self->SUPER::DESTROY;
2909} 2969}
2910 2970
2971sub _percent {
2972 my ($self) = @_;
2973
2974 my $table = $::CONN && $::CONN->{exp_table}
2975 or return -1;
2976
2977 my $l0 = $table->[$self->{lvl} - 1];
2978 my $l1 = $table->[$self->{lvl}];
2979
2980 $self->{nxt} = $l1;
2981
2982 ($self->{exp} - $l0) / ($l1 - $l0)
2983}
2984
2911sub set_value { 2985sub set_value {
2912 my ($self, $lvl, $exp) = @_; 2986 my ($self, $lvl, $exp) = @_;
2913 2987
2914 $self->{lvl} = $lvl; 2988 $self->{lvl} = $lvl;
2915 $self->{exp} = $exp; 2989 $self->{exp} = $exp;
2916 2990
2917 my $v = -1;
2918
2919 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2920 my $l0 = $table->[$lvl - 1];
2921 my $l1 = $table->[$lvl];
2922
2923 $self->{nxt} = $l1;
2924
2925 $v = ($exp - $l0) / ($l1 - $l0);
2926 }
2927
2928 $self->SUPER::set_value ($v); 2991 $self->SUPER::set_value ($self->_percent);
2929} 2992}
2930 2993
2931############################################################################# 2994#############################################################################
2932 2995
2933package CFPlus::UI::Gauge; 2996package DC::UI::Gauge;
2934 2997
2935our @ISA = CFPlus::UI::VBox::; 2998our @ISA = DC::UI::VBox::;
2936 2999
2937sub new { 3000sub new {
2938 my ($class, %arg) = @_; 3001 my ($class, %arg) = @_;
2939 3002
2940 my $self = $class->SUPER::new ( 3003 my $self = $class->SUPER::new (
2942 can_hover => 1, 3005 can_hover => 1,
2943 can_events => 1, 3006 can_events => 1,
2944 %arg, 3007 %arg,
2945 ); 3008 );
2946 3009
2947 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3010 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2948 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3011 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2949 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3012 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2950 3013
2951 $self 3014 $self
2952} 3015}
2953 3016
2954sub set_fontsize { 3017sub set_fontsize {
2975 $self->{value}->set_text ($val); 3038 $self->{value}->set_text ($val);
2976} 3039}
2977 3040
2978############################################################################# 3041#############################################################################
2979 3042
2980package CFPlus::UI::Slider; 3043package DC::UI::Slider;
2981 3044
2982use strict; 3045use strict;
2983 3046
2984use CFPlus::OpenGL; 3047use DC::OpenGL;
2985 3048
2986our @ISA = CFPlus::UI::DrawBG::; 3049our @ISA = DC::UI::DrawBG::;
2987 3050
2988my @tex = 3051my @tex =
2989 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3052 map { new_from_resource DC::Texture $_ }
2990 qw(s1_slider.png s1_slider_bg.png); 3053 qw(s1_slider.png s1_slider_bg.png);
2991 3054
2992sub new { 3055sub new {
2993 my $class = shift; 3056 my $class = shift;
2994 3057
3031sub set_value { 3094sub set_value {
3032 my ($self, $value) = @_; 3095 my ($self, $value) = @_;
3033 3096
3034 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3097 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3035 3098
3036 $hi = $lo + 1 if $hi <= $lo; 3099 $hi = $lo if $hi < $lo;
3037 3100
3038 $page = $hi - $lo if $page > $hi - $lo; 3101 $value = $hi - $page if $value > $hi - $page;
3039
3040 $value = $lo if $value < $lo; 3102 $value = $lo if $value < $lo;
3041 $value = $hi - $page if $value > $hi - $page;
3042 3103
3043 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3104 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3044 if $unit; 3105 if $unit;
3045 3106
3046 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3107 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3090sub invoke_mouse_wheel { 3151sub invoke_mouse_wheel {
3091 my ($self, $ev) = @_; 3152 my ($self, $ev) = @_;
3092 3153
3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3154 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3094 3155
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3156 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3096 3157
3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3158 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3098 3159
3099 1 3160 1
3100} 3161}
3110 my ($self) = @_; 3171 my ($self) = @_;
3111 3172
3112 unless ($self->{knob_w}) { 3173 unless ($self->{knob_w}) {
3113 $self->set_value ($self->{range}[0]); 3174 $self->set_value ($self->{range}[0]);
3114 3175
3115 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3176 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3116 my $range = ($hi - $page - $lo) || 1e-100; 3177 my $range = ($hi - $page - $lo) || 1e-10;
3117 3178
3118 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3179 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3119 3180
3120 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3181 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3121 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3182 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3122 3183
3123 $value = ($value - $lo) / $range; 3184 $value = ($value - $lo) / $range;
3153 glDisable GL_TEXTURE_2D; 3214 glDisable GL_TEXTURE_2D;
3154} 3215}
3155 3216
3156############################################################################# 3217#############################################################################
3157 3218
3158package CFPlus::UI::ValSlider; 3219package DC::UI::ValSlider;
3159 3220
3160our @ISA = CFPlus::UI::HBox::; 3221our @ISA = DC::UI::HBox::;
3161 3222
3162sub new { 3223sub new {
3163 my ($class, %arg) = @_; 3224 my ($class, %arg) = @_;
3164 3225
3165 my $range = delete $arg{range}; 3226 my $range = delete $arg{range};
3166 3227
3167 my $self = $class->SUPER::new ( 3228 my $self = $class->SUPER::new (
3168 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3229 slider => (new DC::UI::Slider expand => 1, range => $range),
3169 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3230 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3170 to_value => sub { shift }, 3231 to_value => sub { shift },
3171 from_value => sub { shift }, 3232 from_value => sub { shift },
3172 %arg, 3233 %arg,
3173 ); 3234 );
3174 3235
3194sub set_range { shift->{slider}->set_range (@_) } 3255sub set_range { shift->{slider}->set_range (@_) }
3195sub set_value { shift->{slider}->set_value (@_) } 3256sub set_value { shift->{slider}->set_value (@_) }
3196 3257
3197############################################################################# 3258#############################################################################
3198 3259
3199package CFPlus::UI::TextScroller; 3260package DC::UI::TextScroller;
3200 3261
3201our @ISA = CFPlus::UI::HBox::; 3262our @ISA = DC::UI::HBox::;
3202 3263
3203use CFPlus::OpenGL; 3264use DC::OpenGL;
3204 3265
3205sub new { 3266sub new {
3206 my $class = shift; 3267 my $class = shift;
3207 3268
3208 my $self = $class->SUPER::new ( 3269 my $self = $class->SUPER::new (
3210 can_events => 1, 3271 can_events => 1,
3211 indent => 0, 3272 indent => 0,
3212 #font => default_font 3273 #font => default_font
3213 @_, 3274 @_,
3214 3275
3215 layout => (new CFPlus::Layout), 3276 layout => (new DC::Layout),
3216 par => [], 3277 par => [],
3217 max_par => 0, 3278 max_par => 0,
3218 height => 0, 3279 height => 0,
3219 children => [ 3280 children => [
3220 (new CFPlus::UI::Empty expand => 1), 3281 (new DC::UI::Empty expand => 1),
3221 (new CFPlus::UI::Slider vertical => 1), 3282 (new DC::UI::Slider vertical => 1),
3222 ], 3283 ],
3223 ); 3284 );
3224 3285
3225 $self->{children}[1]->connect (changed => sub { $self->update }); 3286 $self->{children}[1]->connect (changed => sub { $self->update });
3226 3287
3282 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3343 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3283 $layout->set_markup ($para->{markup}); 3344 $layout->set_markup ($para->{markup});
3284 3345
3285 $layout->set_shapes ( 3346 $layout->set_shapes (
3286 map 3347 map
3287 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3348 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3288 @{$para->{widget}} 3349 @{$para->{widget}}
3289 ); 3350 );
3290 3351
3291 $layout 3352 $layout
3292} 3353}
3409 $ROOT->on_post_alloc ($self => sub { 3470 $ROOT->on_post_alloc ($self => sub {
3410 $self->force_uptodate; 3471 $self->force_uptodate;
3411 3472
3412 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3473 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3413 3474
3414 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3475 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3415 glClearColor 0, 0, 0, 0; 3476 glClearColor 0, 0, 0, 0;
3416 glClear GL_COLOR_BUFFER_BIT; 3477 glClear GL_COLOR_BUFFER_BIT;
3417 3478
3418 package CFPlus::UI::Base; 3479 package DC::UI::Base;
3419 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3480 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3420 (0, 0, $self->{w}, $self->{h}); 3481 (0, 0, $self->{w}, $self->{h});
3421 3482
3422 my $top = int $self->{children}[1]{range}[0]; 3483 my $top = int $self->{children}[1]{range}[0];
3423 3484
3482 $self->{children}[1]->draw; 3543 $self->{children}[1]->draw;
3483} 3544}
3484 3545
3485############################################################################# 3546#############################################################################
3486 3547
3487package CFPlus::UI::Animator; 3548package DC::UI::Animator;
3488 3549
3489use CFPlus::OpenGL; 3550use DC::OpenGL;
3490 3551
3491our @ISA = CFPlus::UI::Bin::; 3552our @ISA = DC::UI::Bin::;
3492 3553
3493sub moveto { 3554sub moveto {
3494 my ($self, $x, $y) = @_; 3555 my ($self, $x, $y) = @_;
3495 3556
3496 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3557 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3524 glPopMatrix; 3585 glPopMatrix;
3525} 3586}
3526 3587
3527############################################################################# 3588#############################################################################
3528 3589
3529package CFPlus::UI::Flopper; 3590package DC::UI::Flopper;
3530 3591
3531our @ISA = CFPlus::UI::Button::; 3592our @ISA = DC::UI::Button::;
3532 3593
3533sub new { 3594sub new {
3534 my $class = shift; 3595 my $class = shift;
3535 3596
3536 my $self = $class->SUPER::new ( 3597 my $self = $class->SUPER::new (
3548 $self->{other}->toggle_visibility; 3609 $self->{other}->toggle_visibility;
3549} 3610}
3550 3611
3551############################################################################# 3612#############################################################################
3552 3613
3553package CFPlus::UI::Tooltip; 3614package DC::UI::Tooltip;
3554 3615
3555our @ISA = CFPlus::UI::Bin::; 3616our @ISA = DC::UI::Bin::;
3556 3617
3557use CFPlus::OpenGL; 3618use DC::OpenGL;
3558 3619
3559sub new { 3620sub new {
3560 my $class = shift; 3621 my $class = shift;
3561 3622
3562 $class->SUPER::new ( 3623 $class->SUPER::new (
3569 my ($self, $widget) = @_; 3630 my ($self, $widget) = @_;
3570 3631
3571 my $tip = $widget->{tooltip}; 3632 my $tip = $widget->{tooltip};
3572 $tip = $tip->($widget) if "CODE" eq ref $tip; 3633 $tip = $tip->($widget) if "CODE" eq ref $tip;
3573 3634
3574 $tip = CFPlus::Pod::section_label tooltip => $1 3635 $tip = DC::Pod::section_label tooltip => $1
3575 if $tip =~ /^#(.*)$/; 3636 if $tip =~ /^#(.*)$/;
3576 3637
3577 if ($ENV{CFPLUS_DEBUG} & 2) { 3638 if ($ENV{CFPLUS_DEBUG} & 2) {
3578 $tip .= "\n\n" . (ref $widget) . "\n" 3639 $tip .= "\n\n" . (ref $widget) . "\n"
3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3640 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3582 } 3643 }
3583 3644
3584 $tip =~ s/^\n+//; 3645 $tip =~ s/^\n+//;
3585 $tip =~ s/\n+$//; 3646 $tip =~ s/\n+$//;
3586 3647
3587 $self->add (new CFPlus::UI::Label 3648 $self->add (new DC::UI::Label
3649 fg => $DC::THEME{tooltip_fg},
3588 markup => $tip, 3650 markup => $tip,
3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3651 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3652 align => 0,
3590 fontsize => 0.8, 3653 fontsize => 0.8,
3591 style => 1, # FLAG_INVERSE 3654 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3592 ellipsise => 0, 3655 ellipsise => 0,
3593 font => ($widget->{tooltip_font} || $::FONT_PROP), 3656 font => ($widget->{tooltip_font} || $::FONT_PROP),
3594 ); 3657 );
3595} 3658}
3596 3659
3633sub _draw { 3696sub _draw {
3634 my ($self) = @_; 3697 my ($self) = @_;
3635 3698
3636 my ($w, $h) = @$self{qw(w h)}; 3699 my ($w, $h) = @$self{qw(w h)};
3637 3700
3638 glColor 1, 0.8, 0.4; 3701 glColor @{ $DC::THEME{tooltip_bg} };
3639 glRect 0, 0, $w, $h; 3702 glRect 0, 0, $w, $h;
3640 3703
3641 glColor 0, 0, 0; 3704 glColor @{ $DC::THEME{tooltip_border} };
3642 glRect_lineloop .5, .5, $w + .5, $h + .5; 3705 glRect_lineloop .5, .5, $w + .5, $h + .5;
3643 3706
3644 glTranslate 2, 2; 3707 glTranslate 2, 2;
3645 3708
3646 $self->SUPER::_draw; 3709 $self->SUPER::_draw;
3647} 3710}
3648 3711
3649############################################################################# 3712#############################################################################
3650 3713
3651package CFPlus::UI::Face; 3714package DC::UI::Face;
3652 3715
3653our @ISA = CFPlus::UI::DrawBG::; 3716our @ISA = DC::UI::DrawBG::;
3654 3717
3655use CFPlus::OpenGL; 3718use DC::OpenGL;
3656 3719
3657sub new { 3720sub new {
3658 my $class = shift; 3721 my $class = shift;
3659 3722
3660 my $self = $class->SUPER::new ( 3723 my $self = $class->SUPER::new (
3663 aspect => 1, 3726 aspect => 1,
3664 can_events => 0, 3727 can_events => 0,
3665 @_, 3728 @_,
3666 ); 3729 );
3667 3730
3668 if ($self->{anim} && $self->{animspeed}) {
3669 CFPlus::weaken (my $widget = $self);
3670
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3673 $self->{timer} = Event->timer (
3674 parked => 1,
3675 cb => sub {
3676 return unless $::CONN;
3677
3678 my $w = $widget
3679 or return;
3680
3681 ++$w->{frame};
3682 $w->update_face;
3683
3684 # somehow, $widget can go away
3685 $w->update;
3686 $w->update_timer;
3687 },
3688 );
3689
3690 $self->update_face;
3691 $self->update_timer; 3731 $self->update_anim;
3692 }
3693 3732
3694 $self 3733 $self
3695} 3734}
3696 3735
3697sub update_timer { 3736sub update_timer {
3698 my ($self) = @_; 3737 my ($self) = @_;
3699 3738
3700 return unless $self->{timer}; 3739 return unless $self->{timer};
3701 3740
3702 if ($self->{visible}) { 3741 if ($self->{visible}) {
3703 $self->{timer}->at (
3704 $self->{anim_start}
3705 + $self->{animspeed}
3706 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3707 );
3708 $self->{timer}->start; 3742 $self->{timer}->start;
3709 } else { 3743 } else {
3710 $self->{timer}->stop; 3744 $self->{timer}->stop;
3711 } 3745 }
3712} 3746}
3725 $tex->upload (sub { $self->reconfigure }); 3759 $tex->upload (sub { $self->reconfigure });
3726 } 3760 }
3727 } 3761 }
3728 } 3762 }
3729 } 3763 }
3764 }
3765}
3766
3767sub update_anim {
3768 my ($self) = @_;
3769
3770 if ($self->{anim} && $self->{animspeed}) {
3771 DC::weaken (my $widget = $self);
3772
3773 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3774 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3775 return unless $::CONN;
3776
3777 my $w = $widget
3778 or return;
3779
3780 ++$w->{frame};
3781 $w->update_face;
3782
3783 # somehow, $widget can go away
3784 $w->update;
3785 $w->update_timer;
3786 };
3787
3788 $self->update_face;
3789 $self->update_timer;
3790 } else {
3791 delete $self->{timer};
3730 } 3792 }
3731} 3793}
3732 3794
3733sub size_request { 3795sub size_request {
3734 my ($self) = @_; 3796 my ($self) = @_;
3756 return unless $self->{visible}; 3818 return unless $self->{visible};
3757 3819
3758 $self->SUPER::update; 3820 $self->SUPER::update;
3759} 3821}
3760 3822
3823sub set_face {
3824 my ($self, $face) = @_;
3825
3826 $self->{face} = $face;
3827 $self->reconfigure;
3828}
3829
3830sub set_anim {
3831 my ($self, $anim) = @_;
3832
3833 $self->{anim} = $anim;
3834 $self->update_anim;
3835}
3836
3837sub set_animspeed {
3838 my ($self, $animspeed) = @_;
3839
3840 $self->{animspeed} = $animspeed;
3841 $self->update_anim;
3842}
3843
3761sub invoke_visibility_change { 3844sub invoke_visibility_change {
3762 my ($self) = @_; 3845 my ($self) = @_;
3763 3846
3764 $self->update_timer; 3847 $self->update_timer;
3765 3848
3789 $self->SUPER::destroy; 3872 $self->SUPER::destroy;
3790} 3873}
3791 3874
3792############################################################################# 3875#############################################################################
3793 3876
3794package CFPlus::UI::Buttonbar; 3877package DC::UI::Buttonbar;
3795 3878
3796our @ISA = CFPlus::UI::HBox::; 3879our @ISA = DC::UI::HBox::;
3797 3880
3798# TODO: should actually wrap buttons and other goodies. 3881# TODO: should actually wrap buttons and other goodies.
3799 3882
3800############################################################################# 3883#############################################################################
3801 3884
3802package CFPlus::UI::Menu; 3885package DC::UI::Menu;
3803 3886
3804our @ISA = CFPlus::UI::Toplevel::; 3887our @ISA = DC::UI::Toplevel::;
3805 3888
3806use CFPlus::OpenGL; 3889use DC::OpenGL;
3807 3890
3808sub new { 3891sub new {
3809 my $class = shift; 3892 my $class = shift;
3810 3893
3811 my $self = $class->SUPER::new ( 3894 my $self = $class->SUPER::new (
3812 items => [], 3895 items => [],
3813 z => 100, 3896 z => 100,
3814 @_, 3897 @_,
3815 ); 3898 );
3816 3899
3817 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3900 $self->add ($self->{vbox} = new DC::UI::VBox);
3818 3901
3819 for my $item (@{ $self->{items} }) { 3902 for my $item (@{ $self->{items} }) {
3820 my ($widget, $cb, $tooltip) = @$item; 3903 my ($widget, $cb, $tooltip) = @$item;
3821 3904
3822 # handle various types of items, only text for now 3905 # handle various types of items, only text for now
3823 if (!ref $widget) { 3906 if (!ref $widget) {
3824 if ($widget =~ /\t/) { 3907 if ($widget =~ /\t/) {
3825 my ($left, $right) = split /\t/, $widget, 2; 3908 my ($left, $right) = split /\t/, $widget, 2;
3826 3909
3827 $widget = new CFPlus::UI::HBox 3910 $widget = new DC::UI::HBox
3828 can_hover => 1, 3911 can_hover => 1,
3829 can_events => 1, 3912 can_events => 1,
3830 tooltip => $tooltip, 3913 tooltip => $tooltip,
3831 children => [ 3914 children => [
3832 (new CFPlus::UI::Label markup => $left, expand => 1), 3915 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3833 (new CFPlus::UI::Label markup => $right, align => +1), 3916 (new DC::UI::Label markup => $right, align => 1),
3834 ], 3917 ],
3835 ; 3918 ;
3836 3919
3837 } else { 3920 } else {
3838 $widget = new CFPlus::UI::Label 3921 $widget = new DC::UI::Label
3839 can_hover => 1, 3922 can_hover => 1,
3840 can_events => 1, 3923 can_events => 1,
3924 align => 0,
3841 markup => $widget, 3925 markup => $widget,
3842 tooltip => $tooltip; 3926 tooltip => $tooltip;
3843 } 3927 }
3844 } 3928 }
3845 3929
3860 # maybe save $GRAB? must be careful about events... 3944 # maybe save $GRAB? must be careful about events...
3861 $GRAB = $self; 3945 $GRAB = $self;
3862 $self->{button} = $ev->{button}; 3946 $self->{button} = $ev->{button};
3863 3947
3864 $self->show; 3948 $self->show;
3865 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3949
3950 my $x = $ev->{x};
3951 my $y = $ev->{y};
3952
3953 $self->{root}->on_post_alloc ($self => sub {
3954 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3955 });
3956
3957 1 # so it can be used inside event handlers
3866} 3958}
3867 3959
3868sub invoke_mouse_motion { 3960sub invoke_mouse_motion {
3869 my ($self, $ev, $x, $y) = @_; 3961 my ($self, $ev, $x, $y) = @_;
3870 3962
3891 1 3983 1
3892} 3984}
3893 3985
3894############################################################################# 3986#############################################################################
3895 3987
3896package CFPlus::UI::Multiplexer; 3988package DC::UI::Multiplexer;
3897 3989
3898our @ISA = CFPlus::UI::Container::; 3990our @ISA = DC::UI::Container::;
3899 3991
3900sub new { 3992sub new {
3901 my $class = shift; 3993 my $class = shift;
3902 3994
3903 my $self = $class->SUPER::new ( 3995 my $self = $class->SUPER::new (
3904 @_, 3996 @_,
3905 ); 3997 );
3906 3998
3907 $self->{current} = $self->{children}[0] 3999 $self->set_current_page (0);
3908 if @{ $self->{children} };
3909 4000
3910 $self 4001 $self
3911} 4002}
3912 4003
3913sub add { 4004sub add {
3914 my ($self, @widgets) = @_; 4005 my ($self, @widgets) = @_;
3915 4006
3916 $self->SUPER::add (@widgets); 4007 $self->SUPER::add (@widgets);
3917 4008
3918 $self->{current} = $self->{children}[0] 4009 $self->set_current_page (0)
3919 if @{ $self->{children} }; 4010 if @widgets == @{ $self->{children} };
3920} 4011}
3921 4012
3922sub get_current_page { 4013sub get_current_page {
3923 my ($self) = @_; 4014 my ($self) = @_;
3924 4015
3930 4021
3931 my $widget = ref $page_or_widget 4022 my $widget = ref $page_or_widget
3932 ? $page_or_widget 4023 ? $page_or_widget
3933 : $self->{children}[$page_or_widget]; 4024 : $self->{children}[$page_or_widget];
3934 4025
4026 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4027
3935 $self->{current} = $widget; 4028 if (($self->{current} = $widget)) {
4029 $self->{current}->set_visible if $self->{current} && $self->{visible};
3936 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4030 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3937 4031
3938 $self->emit (page_changed => $self->{current}); 4032 $self->emit (page_changed => $self->{current});
4033 }
3939 4034
3940 $self->realloc; 4035 $self->realloc;
3941} 4036}
3942 4037
3943sub visible_children { 4038sub visible_children {
3944 $_[0]{current} 4039 $_[0]{current} || ()
3945} 4040}
3946 4041
3947sub size_request { 4042sub size_request {
3948 my ($self) = @_; 4043 my ($self) = @_;
3949 4044
4045 $self->{current}
3950 $self->{current}->size_request 4046 ? $self->{current}->size_request
4047 : (0, 0)
3951} 4048}
3952 4049
3953sub invoke_size_allocate { 4050sub invoke_size_allocate {
3954 my ($self, $w, $h) = @_; 4051 my ($self, $w, $h) = @_;
3955 4052
3956 $self->{current}->configure (0, 0, $w, $h); 4053 $self->{current}->configure (0, 0, $w, $h)
4054 if $self->{current};
3957 4055
3958 1 4056 1
3959} 4057}
3960 4058
3961sub _draw { 4059sub _draw {
3962 my ($self) = @_; 4060 my ($self) = @_;
3963 4061
3964 $self->{current}->draw; 4062 $self->{current}->draw
4063 if $self->{current};
3965} 4064}
3966 4065
3967############################################################################# 4066#############################################################################
3968 4067
3969package CFPlus::UI::Notebook; 4068package DC::UI::Notebook;
3970 4069
3971use CFPlus::OpenGL; 4070use DC::OpenGL;
3972 4071
3973our @ISA = CFPlus::UI::VBox::; 4072our @ISA = DC::UI::VBox::;
3974 4073
3975sub new { 4074sub new {
3976 my $class = shift; 4075 my $class = shift;
3977 4076
3978 my $self = $class->SUPER::new ( 4077 my $self = $class->SUPER::new (
3979 buttonbar => (new CFPlus::UI::Buttonbar), 4078 buttonbar => (new DC::UI::Buttonbar),
3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4079 multiplexer => (new DC::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2], 4080 active_outline => [.7, .7, 0.2],
3982 # filter => # will be put between multiplexer and $self 4081 # filter => # will be put between multiplexer and $self
3983 @_, 4082 @_,
3984 ); 4083 );
3985 4084
3994 4093
3995 for my $child (@$widgets) { 4094 for my $child (@$widgets) {
3996 Scalar::Util::weaken $child; 4095 Scalar::Util::weaken $child;
3997 $child->{c_tab_} ||= do { 4096 $child->{c_tab_} ||= do {
3998 my $tab = 4097 my $tab =
3999 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4098 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4000 ? $child->{c_tab} 4099 ? $child->{c_tab}
4001 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4100 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4002 4101
4003 $tab->connect (activate => sub { 4102 $tab->connect (activate => sub {
4004 $wself->set_current_page ($child); 4103 $wself->set_current_page ($child);
4005 }); 4104 });
4006 4105
4036} 4135}
4037 4136
4038sub pages { 4137sub pages {
4039 my ($self) = @_; 4138 my ($self) = @_;
4040 $self->{multiplexer}->children 4139 $self->{multiplexer}->children
4140}
4141
4142sub page_index {
4143 my ($self, $widget) = @_;
4144
4145 my $i = 0;
4146 for ($self->pages) {
4147 if ($_ eq $widget) { return $i };
4148 $i++;
4149 }
4150
4151 undef
4041} 4152}
4042 4153
4043sub add_tab { 4154sub add_tab {
4044 my ($self, $title, $widget, $tooltip) = @_; 4155 my ($self, $title, $widget, $tooltip) = @_;
4045 4156
4082 } 4193 }
4083} 4194}
4084 4195
4085############################################################################# 4196#############################################################################
4086 4197
4087package CFPlus::UI::Selector; 4198package DC::UI::Selector;
4088 4199
4089use utf8; 4200use utf8;
4090 4201
4091our @ISA = CFPlus::UI::Button::; 4202our @ISA = DC::UI::Button::;
4092 4203
4093sub new { 4204sub new {
4094 my $class = shift; 4205 my $class = shift;
4095 4206
4096 my $self = $class->SUPER::new ( 4207 my $self = $class->SUPER::new (
4113 my ($value, $title, $tooltip) = @$_; 4224 my ($value, $title, $tooltip) = @$_;
4114 4225
4115 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4226 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4116 } 4227 }
4117 4228
4118 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4229 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4119} 4230}
4120 4231
4121sub _set_value { 4232sub _set_value {
4122 my ($self, $value) = @_; 4233 my ($self, $value) = @_;
4123 4234
4124 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4235 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4236 $item ||= $self->{options}[0]
4125 or return; 4237 or return;
4126 4238
4127 $self->{value} = $item->[0]; 4239 $self->{value} = $item->[0];
4128 $self->set_markup ("$item->[1] ⇓"); 4240 $self->set_markup ("$item->[1] ⇓");
4129 $self->set_tooltip ($item->[2]); 4241# $self->set_tooltip ($item->[2]);
4130} 4242}
4131 4243
4132sub set_value { 4244sub set_value {
4133 my ($self, $value) = @_; 4245 my ($self, $value) = @_;
4134 4246
4136 4248
4137 $self->_set_value ($value); 4249 $self->_set_value ($value);
4138 $self->emit (changed => $value); 4250 $self->emit (changed => $value);
4139} 4251}
4140 4252
4253sub set_options {
4254 my ($self, $options) = @_;
4255
4256 $self->{options} = $options;
4257 $self->_set_value ($self->{value});
4258}
4259
4141############################################################################# 4260#############################################################################
4142 4261
4143package CFPlus::UI::Statusbox; 4262package DC::UI::Statusbox;
4144 4263
4145our @ISA = CFPlus::UI::VBox::; 4264our @ISA = DC::UI::VBox::;
4146 4265
4147sub new { 4266sub new {
4148 my $class = shift; 4267 my $class = shift;
4149 4268
4150 my $self = $class->SUPER::new ( 4269 my $self = $class->SUPER::new (
4151 fontsize => 0.8, 4270 fontsize => 0.8,
4152 @_, 4271 @_,
4153 ); 4272 );
4154 4273
4155 CFPlus::weaken (my $this = $self); 4274 DC::weaken (my $this = $self);
4156 4275
4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4276 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4158 4277
4159 $self 4278 $self
4160} 4279}
4161 4280
4162sub reorder { 4281sub reorder {
4163 my ($self) = @_; 4282 my ($self) = @_;
4164 my $NOW = Time::HiRes::time; 4283 my $NOW = EV::time;
4165 4284
4166 # freeze display when hovering over any label 4285 # freeze display when hovering over any label
4167 return if $CFPlus::UI::TOOLTIP->{owner} 4286 return if $DC::UI::TOOLTIP->{owner}
4168 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4287 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4169 values %{ $self->{item} }; 4288 values %{ $self->{item} };
4170 4289
4171 while (my ($k, $v) = each %{ $self->{item} }) { 4290 while (my ($k, $v) = each %{ $self->{item} }) {
4172 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4291 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4173 } 4292 }
4293
4294 $self->{timer}->set (1, 1);
4174 4295
4175 my @widgets; 4296 my @widgets;
4176 4297
4177 my @items = sort { 4298 my @items = sort {
4178 $a->{pri} <=> $b->{pri} 4299 $a->{pri} <=> $b->{pri}
4179 or $b->{id} <=> $a->{id} 4300 or $b->{id} <=> $a->{id}
4180 } values %{ $self->{item} }; 4301 } values %{ $self->{item} };
4181
4182 $self->{timer}->interval (1);
4183 4302
4184 my $count = 10 + 1; 4303 my $count = 10 + 1;
4185 for my $item (@items) { 4304 for my $item (@items) {
4186 last unless --$count; 4305 last unless --$count;
4187 4306
4194 for ($short) { 4313 for ($short) {
4195 s/^\s+//; 4314 s/^\s+//;
4196 s/\s+/ /g; 4315 s/\s+/ /g;
4197 } 4316 }
4198 4317
4199 new CFPlus::UI::Label 4318 new DC::UI::Label
4200 markup => $short, 4319 markup => $short,
4201 tooltip => $item->{tooltip}, 4320 tooltip => $item->{tooltip},
4202 tooltip_font => $::FONT_PROP, 4321 tooltip_font => $::FONT_PROP,
4203 tooltip_width => 0.67, 4322 tooltip_width => 0.67,
4204 fontsize => $item->{fontsize} || $self->{fontsize}, 4323 fontsize => $item->{fontsize} || $self->{fontsize},
4205 max_w => $::WIDTH * 0.44, 4324 max_w => $::WIDTH * 0.44,
4325 align => 0,
4206 fg => [@{ $item->{fg} }], 4326 fg => [@{ $item->{fg} }],
4207 can_events => 1, 4327 can_events => 1,
4208 can_hover => 1 4328 can_hover => 1
4209 }; 4329 };
4210 4330
4211 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4331 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4212 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4332 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4213 $label->update; 4333 $label->update;
4214 $label->set_max_size (undef, $label->{req_h} * $diff) 4334 $label->set_max_size (undef, $label->{req_h} * $diff)
4215 if $diff < 1; 4335 if $diff < 1;
4216 $self->{timer}->interval (1/30); 4336 $self->{timer}->set (1/30, 1/30);
4217 } else { 4337 } else {
4218 $label->{fg}[3] = $item->{fg}[3] || 1; 4338 $label->{fg}[3] = $item->{fg}[3] || 1;
4219 } 4339 }
4220 4340
4221 push @widgets, $label; 4341 push @widgets, $label;
4222 } 4342 }
4343
4344 my $hash = join ",", @widgets;
4345 return if $hash eq $self->{last_widget_hash};
4346 $self->{last_widget_hash} = $hash;
4223 4347
4224 $self->clear; 4348 $self->clear;
4225 $self->SUPER::add (reverse @widgets); 4349 $self->SUPER::add (reverse @widgets);
4226} 4350}
4227 4351
4283 $self->SUPER::destroy; 4407 $self->SUPER::destroy;
4284} 4408}
4285 4409
4286############################################################################# 4410#############################################################################
4287 4411
4288package CFPlus::UI::Root; 4412package DC::UI::Root;
4289 4413
4290our @ISA = CFPlus::UI::Container::; 4414our @ISA = DC::UI::Container::;
4291 4415
4292use List::Util qw(min max); 4416use List::Util qw(min max);
4293 4417
4294use CFPlus::OpenGL; 4418use DC::OpenGL;
4295 4419
4296sub new { 4420sub new {
4297 my $class = shift; 4421 my $class = shift;
4298 4422
4299 my $self = $class->SUPER::new ( 4423 my $self = $class->SUPER::new (
4300 visible => 1, 4424 visible => 1,
4301 @_, 4425 @_,
4302 ); 4426 );
4303 4427
4304 CFPlus::weaken ($self->{root} = $self); 4428 DC::weaken ($self->{root} = $self);
4305 4429
4306 $self 4430 $self
4307} 4431}
4308 4432
4309sub size_request { 4433sub size_request {
4357} 4481}
4358 4482
4359sub update { 4483sub update {
4360 my ($self) = @_; 4484 my ($self) = @_;
4361 4485
4362 $::WANT_REFRESH->start; 4486 $::WANT_REFRESH = 1;
4363} 4487}
4364 4488
4365sub add { 4489sub add {
4366 my ($self, @children) = @_; 4490 my ($self, @children) = @_;
4367 4491
4434 4558
4435 delete $queue{$widget+0}; 4559 delete $queue{$widget+0};
4436 4560
4437 my ($w, $h) = $widget->size_request; 4561 my ($w, $h) = $widget->size_request;
4438 4562
4439 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4563 $w += $widget->{padding_x} * 2;
4440 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4564 $h += $widget->{padding_y} * 2;
4565
4566 $w = max $widget->{min_w}, $w;
4567 $h = max $widget->{min_h}, $h;
4441 4568
4442 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4569 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4443 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4570 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4444 4571
4445 $w = $widget->{force_w} if exists $widget->{force_w}; 4572 $w = $widget->{force_w} if exists $widget->{force_w};
4512 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4639 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4513 glMatrixMode GL_MODELVIEW; 4640 glMatrixMode GL_MODELVIEW;
4514 glLoadIdentity; 4641 glLoadIdentity;
4515 4642
4516 { 4643 {
4517 package CFPlus::UI::Base; 4644 package DC::UI::Base;
4518 4645
4519 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4646 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4520 (0, 0, $self->{w}, $self->{h}); 4647 (0, 0, $self->{w}, $self->{h});
4521 4648
4522 $self->_draw; 4649 $self->_draw;
4523 } 4650 }
4524} 4651}
4525 4652
4526############################################################################# 4653#############################################################################
4527 4654
4528package CFPlus::UI; 4655package DC::UI;
4529 4656
4530$ROOT = new CFPlus::UI::Root; 4657$ROOT = new DC::UI::Root;
4531$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4658$TOOLTIP = new DC::UI::Tooltip z => 900;
4532 4659
45331 46601
4534

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines