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.477 by root, Sun Jan 11 23:50:27 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
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 (
642 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
643 @_ 657 @_
644 ) 658 )
645} 659}
646 660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
647sub _draw { 668sub _draw {
648 my ($self) = @_; 669 my ($self) = @_;
649 670
650 my $color = $FOCUS == $self && $self->{active_bg} 671 my $color = $FOCUS == $self && $self->{active_bg}
651 ? $self->{active_bg} 672 ? $self->{active_bg}
662 } 683 }
663} 684}
664 685
665############################################################################# 686#############################################################################
666 687
667package CFPlus::UI::Empty; 688package DC::UI::Empty;
668 689
669our @ISA = CFPlus::UI::Base::; 690our @ISA = DC::UI::Base::;
670 691
671sub new { 692sub new {
672 my ($class, %arg) = @_; 693 my ($class, %arg) = @_;
673 $class->SUPER::new (can_events => 0, %arg); 694 $class->SUPER::new (can_events => 0, %arg);
674} 695}
681 702
682sub draw { } 703sub draw { }
683 704
684############################################################################# 705#############################################################################
685 706
686package CFPlus::UI::Container; 707package DC::UI::Container;
687 708
688our @ISA = CFPlus::UI::Base::; 709our @ISA = DC::UI::Base::;
689 710
690sub new { 711sub new {
691 my ($class, %arg) = @_; 712 my ($class, %arg) = @_;
692 713
693 my $children = delete $arg{children}; 714 my $children = delete $arg{children};
787 $_->draw for $self->visible_children; 808 $_->draw for $self->visible_children;
788} 809}
789 810
790############################################################################# 811#############################################################################
791 812
792package CFPlus::UI::Bin; 813package DC::UI::Bin;
793 814
794our @ISA = CFPlus::UI::Container::; 815our @ISA = DC::UI::Container::;
795 816
796sub new { 817sub new {
797 my ($class, %arg) = @_; 818 my ($class, %arg) = @_;
798 819
799 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 820 my $child = (delete $arg{child}) || new DC::UI::Empty::;
800 821
801 $class->SUPER::new (children => [$child], %arg) 822 $class->SUPER::new (children => [$child], %arg)
802} 823}
803 824
804sub add { 825sub add {
811sub remove { 832sub remove {
812 my ($self, $widget) = @_; 833 my ($self, $widget) = @_;
813 834
814 $self->SUPER::remove ($widget); 835 $self->SUPER::remove ($widget);
815 836
816 $self->{children} = [new CFPlus::UI::Empty] 837 $self->{children} = [new DC::UI::Empty]
817 unless @{$self->{children}}; 838 unless @{$self->{children}};
818} 839}
819 840
820sub child { $_[0]->{children}[0] } 841sub child { $_[0]->{children}[0] }
821 842
832} 853}
833 854
834############################################################################# 855#############################################################################
835# back-buffered drawing area 856# back-buffered drawing area
836 857
837package CFPlus::UI::Window; 858package DC::UI::Window;
838 859
839our @ISA = CFPlus::UI::Bin::; 860our @ISA = DC::UI::Bin::;
840 861
841use CFPlus::OpenGL; 862use DC::OpenGL;
842 863
843sub new { 864sub new {
844 my ($class, %arg) = @_; 865 my ($class, %arg) = @_;
845 866
846 my $self = $class->SUPER::new (%arg); 867 my $self = $class->SUPER::new (%arg);
868} 889}
869 890
870sub render_child { 891sub render_child {
871 my ($self) = @_; 892 my ($self) = @_;
872 893
873 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 894 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
874 glClearColor 0, 0, 0, 0; 895 glClearColor 0, 0, 0, 0;
875 glClear GL_COLOR_BUFFER_BIT; 896 glClear GL_COLOR_BUFFER_BIT;
876 897
877 { 898 {
878 package CFPlus::UI::Base; 899 package DC::UI::Base;
879 900
880 local ($draw_x, $draw_y, $draw_w, $draw_h) = 901 local ($draw_x, $draw_y, $draw_w, $draw_h) =
881 (0, 0, $self->{w}, $self->{h}); 902 (0, 0, $self->{w}, $self->{h});
882 903
883 $self->_render; 904 $self->_render;
900 glDisable GL_TEXTURE_2D; 921 glDisable GL_TEXTURE_2D;
901} 922}
902 923
903############################################################################# 924#############################################################################
904 925
905package CFPlus::UI::ViewPort; 926package DC::UI::ViewPort;
906 927
907use List::Util qw(min max); 928use List::Util qw(min max);
908 929
909our @ISA = CFPlus::UI::Window::; 930our @ISA = DC::UI::Window::;
910 931
911sub new { 932sub new {
912 my $class = shift; 933 my $class = shift;
913 934
914 $class->SUPER::new ( 935 $class->SUPER::new (
998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1019 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
999 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1020 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1000 ) { 1021 ) {
1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1022 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1002 } else { 1023 } else {
1003 $self->CFPlus::UI::Base::find_widget ($x, $y) 1024 $self->DC::UI::Base::find_widget ($x, $y)
1004 } 1025 }
1005} 1026}
1006 1027
1007sub _render { 1028sub _render {
1008 my ($self) = @_; 1029 my ($self) = @_;
1009 1030
1010 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1031 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1011 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1032 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1012 1033
1013 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1034 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1014 1035
1015 $self->SUPER::_render; 1036 $self->SUPER::_render;
1016} 1037}
1017 1038
1018############################################################################# 1039#############################################################################
1019 1040
1020package CFPlus::UI::ScrolledWindow; 1041package DC::UI::ScrolledWindow;
1021 1042
1022our @ISA = CFPlus::UI::Table::; 1043our @ISA = DC::UI::Table::;
1023 1044
1024sub new { 1045sub new {
1025 my ($class, %arg) = @_; 1046 my ($class, %arg) = @_;
1026 1047
1027 my $child = delete $arg{child}; 1048 my $child = delete $arg{child};
1028 1049
1029 my $self; 1050 my $self;
1030 1051
1031 my $hslider = new CFPlus::UI::Slider 1052 my $hslider = new DC::UI::Slider
1032 c_col => 0, 1053 c_col => 0,
1033 c_row => 1, 1054 c_row => 1,
1034 vertical => 0, 1055 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix 1056 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub { 1057 on_changed => sub {
1037 $self->{hpos} = $_[1]; 1058 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1059 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 }, 1060 },
1040 ; 1061 ;
1041 1062
1042 my $vslider = new CFPlus::UI::Slider 1063 my $vslider = new DC::UI::Slider
1043 c_col => 1, 1064 c_col => 1,
1044 c_row => 0, 1065 c_row => 0,
1045 vertical => 1, 1066 vertical => 1,
1046 range => [0, 0, 1, 0.01], # HACK fix 1067 range => [0, 0, 1, 0.01], # HACK fix
1047 on_changed => sub { 1068 on_changed => sub {
1059 col_expand => [1, 0], 1080 col_expand => [1, 0],
1060 row_expand => [1, 0], 1081 row_expand => [1, 0],
1061 %arg, 1082 %arg,
1062 ); 1083 );
1063 1084
1064 $self->{vp} = new CFPlus::UI::ViewPort 1085 $self->{vp} = new DC::UI::ViewPort
1065 c_col => 0, 1086 c_col => 0,
1066 c_row => 0, 1087 c_row => 0,
1067 expand => 1, 1088 expand => 1,
1068 scroll_x => $self->{scroll_x}, 1089 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y}, 1090 scroll_y => $self->{scroll_y},
1195 $self->SUPER::invoke_size_allocate ($w, $h) 1216 $self->SUPER::invoke_size_allocate ($w, $h)
1196} 1217}
1197 1218
1198############################################################################# 1219#############################################################################
1199 1220
1200package CFPlus::UI::Frame; 1221package DC::UI::Frame;
1201 1222
1202our @ISA = CFPlus::UI::Bin::; 1223our @ISA = DC::UI::Bin::;
1203 1224
1204use CFPlus::OpenGL; 1225use DC::OpenGL;
1205 1226
1206sub new { 1227sub new {
1207 my $class = shift; 1228 my $class = shift;
1208 1229
1209 $class->SUPER::new ( 1230 $class->SUPER::new (
1228 $self->SUPER::_draw; 1249 $self->SUPER::_draw;
1229} 1250}
1230 1251
1231############################################################################# 1252#############################################################################
1232 1253
1233package CFPlus::UI::FancyFrame; 1254package DC::UI::FancyFrame;
1234 1255
1235our @ISA = CFPlus::UI::Bin::; 1256our @ISA = DC::UI::Bin::;
1236 1257
1237use CFPlus::OpenGL; 1258use DC::OpenGL;
1238 1259
1239sub new { 1260sub new {
1240 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1241 1262
1242 if ((exists $arg{label}) && !ref $arg{label}) { 1263 if ((exists $arg{label}) && !ref $arg{label}) {
1243 $arg{label} = new CFPlus::UI::Label 1264 $arg{label} = new DC::UI::Label
1244 align => 1, 1265 align => 1,
1245 valign => 0, 1266 valign => 0.5,
1246 text => $arg{label}, 1267 text => $arg{label},
1247 fontsize => ($arg{border} || 0.8) * 0.75; 1268 fontsize => ($arg{border} || 0.8) * 0.75;
1248 } 1269 }
1249 1270
1250 my $self = $class->SUPER::new ( 1271 my $self = $class->SUPER::new (
1251 # label => "", 1272 # label => "",
1252 fg => [0.6, 0.3, 0.1], 1273 fg => undef,
1253 border => 0.8, 1274 border => 0.8,
1254 style => 'single', 1275 style => 'single',
1255 %arg, 1276 %arg,
1256 ); 1277 );
1257 1278
1260 1281
1261sub add { 1282sub add {
1262 my ($self, @widgets) = @_; 1283 my ($self, @widgets) = @_;
1263 1284
1264 $self->SUPER::add (@widgets); 1285 $self->SUPER::add (@widgets);
1265 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1286 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1266} 1287}
1267 1288
1268sub border { 1289sub border {
1269 int $_[0]{border} * $::FONTSIZE 1290 int $_[0]{border} * $::FONTSIZE
1270} 1291}
1310 my $border = $self->border; 1331 my $border = $self->border;
1311 my ($w, $h) = ($self->{w}, $self->{h}); 1332 my ($w, $h) = ($self->{w}, $self->{h});
1312 1333
1313 $child->draw; 1334 $child->draw;
1314 1335
1315 glColor @{$self->{fg}}; 1336 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1316 glBegin GL_LINE_STRIP; 1337 glBegin GL_LINE_STRIP;
1317 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1338 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1318 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1320 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1328 } 1349 }
1329} 1350}
1330 1351
1331############################################################################# 1352#############################################################################
1332 1353
1333package CFPlus::UI::Toplevel; 1354package DC::UI::Toplevel;
1334 1355
1335our @ISA = CFPlus::UI::Bin::; 1356our @ISA = DC::UI::Bin::;
1336 1357
1337use CFPlus::OpenGL; 1358use DC::OpenGL;
1338 1359
1339my $bg = 1360my $bg =
1340 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1361 new_from_resource DC::Texture "d1_bg.png",
1341 mipmap => 1, wrap => 1; 1362 mipmap => 1, wrap => 1;
1342 1363
1343my @border = 1364my @border =
1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1365 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); 1366 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1346 1367
1347my @icon = 1368my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1369 map { new_from_resource DC::Texture $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png); 1370 qw(x1_move.png x1_resize.png);
1350 1371
1351sub new { 1372sub new {
1352 my ($class, %arg) = @_; 1373 my ($class, %arg) = @_;
1353 1374
1354 my $self = $class->SUPER::new ( 1375 my $self = $class->SUPER::new (
1355 bg => [1, 1, 1, 1], 1376 bg => [1, 1, 1, 1],
1356 border_bg => [1, 1, 1, 1], 1377 border_bg => [1, 1, 1, 1],
1357 border => 0.6, 1378 border => 0.8,
1358 can_events => 1, 1379 can_events => 1,
1359 min_w => 64, 1380 min_w => 64,
1360 min_h => 32, 1381 min_h => 32,
1361 %arg, 1382 %arg,
1362 ); 1383 );
1363 1384
1364 $self->{title_widget} = new CFPlus::UI::Label 1385 $self->{title_widget} = new DC::UI::Label
1365 align => 0, 1386 align => 0.5,
1366 valign => 1, 1387 valign => 1,
1367 text => $self->{title}, 1388 text => $self->{title},
1368 fontsize => $self->{border}, 1389 fontsize => $self->{border},
1369 if exists $self->{title}; 1390 if exists $self->{title};
1370 1391
1371 if ($self->{has_close_button}) { 1392 if ($self->{has_close_button}) {
1372 $self->{close_button} = 1393 $self->{close_button} =
1373 new CFPlus::UI::ImageButton 1394 new DC::UI::ImageButton
1374 path => 'x1_close.png', 1395 path => 'x1_close.png',
1375 on_activate => sub { $self->emit ("delete") }; 1396 on_activate => sub { $self->emit ("delete") };
1376 1397
1377 $self->CFPlus::UI::Container::add ($self->{close_button}); 1398 $self->DC::UI::Container::add ($self->{close_button});
1378 } 1399 }
1379 1400
1380 $self 1401 $self
1381} 1402}
1382 1403
1383sub add { 1404sub add {
1384 my ($self, @widgets) = @_; 1405 my ($self, @widgets) = @_;
1385 1406
1386 $self->SUPER::add (@widgets); 1407 $self->SUPER::add (@widgets);
1387 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1408 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1388 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1409 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1389} 1410}
1390 1411
1391sub border { 1412sub border {
1392 int $_[0]{border} * $::FONTSIZE 1413 int $_[0]{border} * $::FONTSIZE
1414}
1415
1416sub get_max_wh {
1417 my ($self) = @_;
1418
1419 return ($self->{w}, $self->{h})
1420 if $self->{visible} && $self->{w};
1421
1422 $self->SUPER::get_max_wh
1393} 1423}
1394 1424
1395sub size_request { 1425sub size_request {
1396 my ($self) = @_; 1426 my ($self) = @_;
1397 1427
1522 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1523 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1524 1554
1525 my $border = $self->border; 1555 my $border = $self->border;
1526 1556
1557 if ($border) {
1527 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1528 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1529 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1530 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1531 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1532 1563
1533 # move 1564 # move
1534 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1535 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1536 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1537 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1538 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1539 1570
1540 # resize 1571 # resize
1541 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1542 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1543 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1544 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1545 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1546 1578
1547 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1548 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1549 1581
1550 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
1569 if $self->{close_button}; 1601 if $self->{close_button};
1570} 1602}
1571 1603
1572############################################################################# 1604#############################################################################
1573 1605
1574package CFPlus::UI::Table; 1606package DC::UI::Table;
1575 1607
1576our @ISA = CFPlus::UI::Container::; 1608our @ISA = DC::UI::Container::;
1577 1609
1578use List::Util qw(max sum); 1610use List::Util qw(max sum);
1579 1611
1580use CFPlus::OpenGL; 1612use DC::OpenGL;
1581 1613
1582sub new { 1614sub new {
1583 my $class = shift; 1615 my $class = shift;
1584 1616
1585 $class->SUPER::new ( 1617 $class->SUPER::new (
1629 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1661 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1630 1662
1631 my $sw = sum @w[$c .. $c + $cs - 1]; 1663 my $sw = sum @w[$c .. $c + $cs - 1];
1632 1664
1633 if ($w > $sw) { 1665 if ($w > $sw) {
1634 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1666 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1635 } 1667 }
1636 } 1668 }
1637 1669
1638 # second pass, rows 1670 # second pass, rows
1639 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1671 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1640 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1672 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1641 1673
1642 my $sh = sum @h[$r .. $r + $rs - 1]; 1674 my $sh = sum @h[$r .. $r + $rs - 1];
1643 1675
1644 if ($h > $sh) { 1676 if ($h > $sh) {
1645 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1677 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1646 } 1678 }
1647 } 1679 }
1648 1680
1649 (\@w, \@h) 1681 (\@w, \@h)
1650} 1682}
1673 @col_expand = (1) x @$ws unless @col_expand; 1705 @col_expand = (1) x @$ws unless @col_expand;
1674 my $col_expand = (sum @col_expand) || 1; 1706 my $col_expand = (sum @col_expand) || 1;
1675 1707
1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1708 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1677 1709
1678 CFPlus::UI::harmonize $ws; 1710 DC::UI::harmonize $ws;
1679 1711
1680 my @row_expand = @{$self->{row_expand}}; 1712 my @row_expand = @{$self->{row_expand}};
1681 @row_expand = (1) x @$ws unless @row_expand; 1713 @row_expand = (1) x @$ws unless @row_expand;
1682 my $row_expand = (sum @row_expand) || 1; 1714 my $row_expand = (sum @row_expand) || 1;
1683 1715
1684 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1716 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1685 1717
1686 CFPlus::UI::harmonize $hs; 1718 DC::UI::harmonize $hs;
1687 1719
1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1720 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1721 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1690 1722
1691 for my $widget ($self->children) { 1723 for my $widget ($self->children) {
1700 1 1732 1
1701} 1733}
1702 1734
1703############################################################################# 1735#############################################################################
1704 1736
1705package CFPlus::UI::Fixed; 1737package DC::UI::Fixed;
1706 1738
1707use List::Util qw(min max); 1739use List::Util qw(min max);
1708 1740
1709our @ISA = CFPlus::UI::Container::; 1741our @ISA = DC::UI::Container::;
1710 1742
1711sub _scale($$$) { 1743sub _scale($$$) {
1712 my ($rel, $val, $max) = @_; 1744 my ($rel, $val, $max) = @_;
1713 1745
1714 $rel ? $val * $max : $val 1746 $rel ? $val * $max : $val
1766 1 1798 1
1767} 1799}
1768 1800
1769############################################################################# 1801#############################################################################
1770 1802
1771package CFPlus::UI::Box; 1803package DC::UI::Box;
1772 1804
1773our @ISA = CFPlus::UI::Container::; 1805our @ISA = DC::UI::Container::;
1774 1806
1775sub size_request { 1807sub size_request {
1776 my ($self) = @_; 1808 my ($self) = @_;
1777 1809
1778 my @children = $self->visible_children; 1810 my @children = $self->visible_children;
1813 $req[$_] += $space * $children[$_]{expand} 1845 $req[$_] += $space * $children[$_]{expand}
1814 for 0 .. $#children; 1846 for 0 .. $#children;
1815 } 1847 }
1816 } 1848 }
1817 1849
1818 CFPlus::UI::harmonize \@req; 1850 DC::UI::harmonize \@req;
1819 1851
1820 my $pos = 0; 1852 my $pos = 0;
1821 for (0 .. $#children) { 1853 for (0 .. $#children) {
1822 my $alloc = $req[$_]; 1854 my $alloc = $req[$_];
1823 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1855 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1828 1 1860 1
1829} 1861}
1830 1862
1831############################################################################# 1863#############################################################################
1832 1864
1833package CFPlus::UI::HBox; 1865package DC::UI::HBox;
1834 1866
1835our @ISA = CFPlus::UI::Box::; 1867our @ISA = DC::UI::Box::;
1836 1868
1837sub new { 1869sub new {
1838 my $class = shift; 1870 my $class = shift;
1839 1871
1840 $class->SUPER::new ( 1872 $class->SUPER::new (
1843 ) 1875 )
1844} 1876}
1845 1877
1846############################################################################# 1878#############################################################################
1847 1879
1848package CFPlus::UI::VBox; 1880package DC::UI::VBox;
1849 1881
1850our @ISA = CFPlus::UI::Box::; 1882our @ISA = DC::UI::Box::;
1851 1883
1852sub new { 1884sub new {
1853 my $class = shift; 1885 my $class = shift;
1854 1886
1855 $class->SUPER::new ( 1887 $class->SUPER::new (
1858 ) 1890 )
1859} 1891}
1860 1892
1861############################################################################# 1893#############################################################################
1862 1894
1863package CFPlus::UI::Label; 1895package DC::UI::Label;
1864 1896
1865our @ISA = CFPlus::UI::DrawBG::; 1897our @ISA = DC::UI::DrawBG::;
1866 1898
1867use CFPlus::OpenGL; 1899use DC::OpenGL;
1868 1900
1869sub new { 1901sub new {
1870 my ($class, %arg) = @_; 1902 my ($class, %arg) = @_;
1871 1903
1872 my $self = $class->SUPER::new ( 1904 my $self = $class->SUPER::new (
1877 #text => initial text 1909 #text => initial text
1878 #markup => initial narkup 1910 #markup => initial narkup
1879 #max_w => maximum pixel width 1911 #max_w => maximum pixel width
1880 #style => 0, # render flags 1912 #style => 0, # render flags
1881 ellipsise => 3, # end 1913 ellipsise => 3, # end
1882 layout => (new CFPlus::Layout), 1914 layout => (new DC::Layout),
1883 fontsize => 1, 1915 fontsize => 1,
1884 align => -1, 1916 align => 0.5,
1885 valign => -1, 1917 valign => 0.5,
1886 padding_x => 2, 1918 padding_x => 4,
1887 padding_y => 2, 1919 padding_y => 2,
1888 can_events => 0, 1920 can_events => 0,
1889 %arg 1921 %arg
1890 ); 1922 );
1891 1923
1892 if (exists $self->{template}) { 1924 if (exists $self->{template}) {
1893 my $layout = new CFPlus::Layout; 1925 my $layout = new DC::Layout;
1894 $layout->set_text (delete $self->{template}); 1926 $layout->set_text (delete $self->{template});
1895 $self->{template} = $layout; 1927 $self->{template} = $layout;
1896 } 1928 }
1897 1929
1898 if (exists $self->{markup}) { 1930 if (exists $self->{markup}) {
1957 1989
1958 $self->{size_req} ||= do { 1990 $self->{size_req} ||= do {
1959 my ($max_w, $max_h) = $self->get_max_wh; 1991 my ($max_w, $max_h) = $self->get_max_wh;
1960 1992
1961 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1993 $self->{layout}->set_font ($self->{font}) if $self->{font};
1962 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1994 $self->{layout}->set_width ($max_w);
1963 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1995 $self->{layout}->set_ellipsise ($self->{ellipsise});
1964 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1996 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1965 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1966 1998
1967 my ($w, $h) = $self->{layout}->size; 1999 my ($w, $h) = $self->{layout}->size;
1968 2000
1969 if (exists $self->{template}) { 2001 if (exists $self->{template}) {
1970 $self->{template}->set_font ($self->{font}) if $self->{font}; 2002 $self->{template}->set_font ($self->{font}) if $self->{font};
1971 $self->{template}->set_width ($self->{max_w} || -1); 2003 $self->{template}->set_width ($max_w);
1972 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2004 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1973 2005
1974 my ($w2, $h2) = $self->{template}->size; 2006 my ($w2, $h2) = $self->{template}->size;
1975 2007
1976 $w = List::Util::max $w, $w2; 2008 $w = List::Util::max $w, $w2;
2032 2064
2033 [$self->{layout}->size] 2065 [$self->{layout}->size]
2034 }; 2066 };
2035 2067
2036 unless (exists $self->{ox}) { 2068 unless (exists $self->{ox}) {
2037 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2069 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2038 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2070 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
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 2071
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2046 }; 2073 };
2047 2074
2048# unless ($self->{list}) { 2075# unless ($self->{list}) {
2049# $self->{list} = CFPlus::OpenGL::glGenList; 2076# $self->{list} = DC::OpenGL::glGenList;
2050# CFPlus::OpenGL::glNewList $self->{list}; 2077# DC::OpenGL::glNewList $self->{list};
2051# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2078# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052# CFPlus::OpenGL::glEndList; 2079# DC::OpenGL::glEndList;
2053# } 2080# }
2054# 2081#
2055# CFPlus::OpenGL::glCallList $self->{list}; 2082# DC::OpenGL::glCallList $self->{list};
2056 2083
2057 $self->{layout}->draw; 2084 $self->{layout}->draw;
2058} 2085}
2059 2086
2060#sub destroy { 2087#sub destroy {
2061# my ($self) = @_; 2088# my ($self) = @_;
2062# 2089#
2063# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2090# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2064# 2091#
2065# $self->SUPER::destroy; 2092# $self->SUPER::destroy;
2066#} 2093#}
2067 2094
2068############################################################################# 2095#############################################################################
2069 2096
2070package CFPlus::UI::EntryBase; 2097package DC::UI::EntryBase;
2071 2098
2072our @ISA = CFPlus::UI::Label::; 2099our @ISA = DC::UI::Label::;
2073 2100
2074use CFPlus::OpenGL; 2101use DC::OpenGL;
2075 2102
2076sub new { 2103sub new {
2077 my $class = shift; 2104 my $class = shift;
2078 2105
2079 $class->SUPER::new ( 2106 $class->SUPER::new (
2080 fg => [1, 1, 1], 2107 fg => [1, 1, 1],
2081 bg => [0, 0, 0, 0.2], 2108 bg => [0, 0, 0, 0.2],
2082 outline => [0.6, 0.3, 0.1], 2109 outline => undef,
2083 active_bg => [0, 0, 1, .2], 2110 active_bg => [0, 0, 1, .2],
2084 active_fg => [1, 1, 1], 2111 active_fg => [1, 1, 1],
2085 active_outline => [1, 1, 0], 2112 active_outline => [1, 1, 0],
2086 can_hover => 1, 2113 can_hover => 1,
2087 can_focus => 1, 2114 can_focus => 1,
2115 align => 0,
2088 valign => 0, 2116 valign => 0.5,
2089 can_events => 1, 2117 can_events => 1,
2090 ellipsise => 0, 2118 ellipsise => 0,
2119 padding_x => 4,
2120 padding_y => 2,
2091 #text => ... 2121 #text => ...
2092 #hidden => "*", 2122 #hidden => "*",
2093 @_ 2123 @_
2094 ) 2124 )
2095} 2125}
2142 2172
2143 my $text = $self->get_text; 2173 my $text = $self->get_text;
2144 2174
2145 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2175 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2146 2176
2147 if ($uni == 8) { 2177 if ($sym == DC::SDLK_BACKSPACE) {
2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2178 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2149 } elsif ($uni == 127) { 2179 } elsif ($sym == DC::SDLK_DELETE) {
2150 substr $text, $self->{cursor}, 1, ""; 2180 substr $text, $self->{cursor}, 1, "";
2151 } elsif ($sym == CFPlus::SDLK_LEFT) { 2181 } elsif ($sym == DC::SDLK_LEFT) {
2152 --$self->{cursor} if $self->{cursor}; 2182 --$self->{cursor} if $self->{cursor};
2153 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2183 } elsif ($sym == DC::SDLK_RIGHT) {
2154 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2184 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2155 } elsif ($sym == CFPlus::SDLK_HOME) { 2185 } elsif ($sym == DC::SDLK_HOME) {
2156 # what a hack 2186 # what a hack
2157 $self->{cursor} = 2187 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2188 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1 2189 ? length $1
2160 : 0; 2190 : 0;
2161 } elsif ($sym == CFPlus::SDLK_END) { 2191 } elsif ($sym == DC::SDLK_END) {
2162 # uh, again 2192 # uh, again
2163 $self->{cursor} = 2193 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2194 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2165 ? $self->{cursor} + length $1 2195 ? $self->{cursor} + length $1
2166 : length $self->{text}; 2196 : length $self->{text};
2257 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2258 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2288 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2259 glLineWidth 1; 2289 glLineWidth 1;
2260 2290
2261 } else { 2291 } else {
2262 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2263 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2264 glVertex .5, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2265 glVertex .5, $self->{h} - 2.5; 2295 glVertex .5, $self->{h} - 2.5;
2266 glVertex $self->{w} - .5, $self->{h} - 2.5; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2267 glVertex $self->{w} - .5, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
2269 } 2299 }
2270} 2300}
2271 2301
2272############################################################################# 2302#############################################################################
2273 2303
2274package CFPlus::UI::Entry; 2304package DC::UI::Entry;
2275 2305
2276our @ISA = CFPlus::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2277 2307
2278use CFPlus::OpenGL; 2308use DC::OpenGL;
2309
2310sub new {
2311 my $class = shift;
2312
2313 $class->SUPER::new (
2314 history_pointer => -1,
2315 @_
2316 )
2317}
2318
2279 2319
2280sub invoke_key_down { 2320sub invoke_key_down {
2281 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2282 2322
2283 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2289 $self->{history_pointer} = -1; 2329 $self->{history_pointer} = -1;
2290 $self->{history_saveback} = ''; 2330 $self->{history_saveback} = '';
2291 $self->emit (activate => $txt); 2331 $self->emit (activate => $txt);
2292 $self->update; 2332 $self->update;
2293 2333
2294 } elsif ($sym == CFPlus::SDLK_UP) { 2334 } elsif ($sym == DC::SDLK_UP) {
2295 if ($self->{history_pointer} < 0) { 2335 if ($self->{history_pointer} < 0) {
2296 $self->{history_saveback} = $self->get_text; 2336 $self->{history_saveback} = $self->get_text;
2297 } 2337 }
2298 if (@{$self->{history} || []} > 0) { 2338 if (@{$self->{history} || []} > 0) {
2299 $self->{history_pointer}++; 2339 $self->{history_pointer}++;
2301 $self->{history_pointer} = @{$self->{history} || []} - 1; 2341 $self->{history_pointer} = @{$self->{history} || []} - 1;
2302 } 2342 }
2303 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2343 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2304 } 2344 }
2305 2345
2306 } elsif ($sym == CFPlus::SDLK_DOWN) { 2346 } elsif ($sym == DC::SDLK_DOWN) {
2307 $self->{history_pointer}--; 2347 $self->{history_pointer}--;
2308 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2309 2349
2310 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2311 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2312 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2313 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2314 } 2357 }
2315 2358
2316 } else { 2359 } else {
2317 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2318 } 2361 }
2320 1 2363 1
2321} 2364}
2322 2365
2323############################################################################# 2366#############################################################################
2324 2367
2325package CFPlus::UI::TextEdit; 2368package DC::UI::TextEdit;
2326 2369
2327our @ISA = CFPlus::UI::EntryBase::; 2370our @ISA = DC::UI::EntryBase::;
2328 2371
2329use CFPlus::OpenGL; 2372use DC::OpenGL;
2373
2374sub new {
2375 my $class = shift;
2376
2377 $class->SUPER::new (
2378 padding_y => 4,
2379
2380 @_
2381 )
2382}
2330 2383
2331sub move_cursor_ver { 2384sub move_cursor_ver {
2332 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2333 2386
2334 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2335 2388
2336 $y += $dy; 2389 $line += $dy;
2337 2390
2338 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2391 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2339 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2340 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2341 $self->update; 2394 $self->update;
2342 return; 2395 return;
2343 } 2396 }
2346sub invoke_key_down { 2399sub invoke_key_down {
2347 my ($self, $ev) = @_; 2400 my ($self, $ev) = @_;
2348 2401
2349 my $sym = $ev->{sym}; 2402 my $sym = $ev->{sym};
2350 2403
2351 if ($sym == CFPlus::SDLK_UP) { 2404 if ($sym == DC::SDLK_UP) {
2352 $self->move_cursor_ver (-1); 2405 $self->move_cursor_ver (-1);
2353 } elsif ($sym == CFPlus::SDLK_DOWN) { 2406 } elsif ($sym == DC::SDLK_DOWN) {
2354 $self->move_cursor_ver (+1); 2407 $self->move_cursor_ver (+1);
2355 } else { 2408 } else {
2356 return $self->SUPER::invoke_key_down ($ev) 2409 return $self->SUPER::invoke_key_down ($ev)
2357 } 2410 }
2358 2411
2359 1 2412 1
2360} 2413}
2361 2414
2362############################################################################# 2415#############################################################################
2363 2416
2364package CFPlus::UI::ButtonBin; 2417package DC::UI::ButtonBin;
2365 2418
2366our @ISA = CFPlus::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2367 2420
2368use CFPlus::OpenGL; 2421use DC::OpenGL;
2369 2422
2370my @tex = 2423my @tex =
2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2372 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2373 2426
2374sub new { 2427sub new {
2375 my $class = shift; 2428 my $class = shift;
2376 2429
2377 $class->SUPER::new ( 2430 $class->SUPER::new (
2378 can_hover => 1, 2431 can_hover => 1,
2379 align => 0, 2432 align => 0.5,
2380 valign => 0, 2433 valign => 0.5,
2381 can_events => 1, 2434 can_events => 1,
2382 @_ 2435 @_
2383 ) 2436 )
2384} 2437}
2385 2438
2408 $self->SUPER::_draw; 2461 $self->SUPER::_draw;
2409} 2462}
2410 2463
2411############################################################################# 2464#############################################################################
2412 2465
2413package CFPlus::UI::Button; 2466package DC::UI::Button;
2414 2467
2415our @ISA = CFPlus::UI::Label::; 2468our @ISA = DC::UI::Label::;
2416 2469
2417use CFPlus::OpenGL; 2470use DC::OpenGL;
2418 2471
2419my @tex = 2472my @tex =
2420 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2421 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2422 2475
2423sub new { 2476sub new {
2424 my $class = shift; 2477 my $class = shift;
2425 2478
2426 $class->SUPER::new ( 2479 $class->SUPER::new (
2427 padding_x => 4, 2480 padding_x => 8,
2428 padding_y => 4, 2481 padding_y => 4,
2429 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2430 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2431 can_hover => 1, 2484 can_hover => 1,
2432 align => 0, 2485 align => 0.5,
2433 valign => 0, 2486 valign => 0.5,
2434 can_events => 1, 2487 can_events => 1,
2435 @_ 2488 @_
2436 ) 2489 )
2437} 2490}
2438 2491
2463 $self->SUPER::_draw; 2516 $self->SUPER::_draw;
2464} 2517}
2465 2518
2466############################################################################# 2519#############################################################################
2467 2520
2468package CFPlus::UI::CheckBox; 2521package DC::UI::CheckBox;
2469 2522
2470our @ISA = CFPlus::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2471 2524
2472my @tex = 2525my @tex =
2473 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2474 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2475 2528
2476use CFPlus::OpenGL; 2529use DC::OpenGL;
2477 2530
2478sub new { 2531sub new {
2479 my $class = shift; 2532 my $class = shift;
2480 2533
2481 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2482 padding_x => 2, 2536 padding_x => 2,
2483 padding_y => 2, 2537 padding_y => 2,
2484 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2485 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2486 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2492} 2546}
2493 2547
2494sub size_request { 2548sub size_request {
2495 my ($self) = @_; 2549 my ($self) = @_;
2496 2550
2497 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2498} 2552}
2499 2553
2500sub toggle { 2554sub toggle {
2501 my ($self) = @_; 2555 my ($self) = @_;
2502 2556
2538 glDisable GL_TEXTURE_2D; 2592 glDisable GL_TEXTURE_2D;
2539} 2593}
2540 2594
2541############################################################################# 2595#############################################################################
2542 2596
2543package CFPlus::UI::Image; 2597package DC::UI::Image;
2544 2598
2545our @ISA = CFPlus::UI::Base::; 2599our @ISA = DC::UI::Base::;
2546 2600
2547use CFPlus::OpenGL; 2601use DC::OpenGL;
2548 2602
2549our %texture_cache; 2603our %texture_cache;
2550 2604
2551sub new { 2605sub new {
2552 my $class = shift; 2606 my $class = shift;
2559 2613
2560 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2561 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2562 2616
2563 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2564 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2565 2619
2566 CFPlus::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2567 2621
2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2569 2623
2570 $self 2624 $self
2571} 2625}
2572 2626
2573sub STORABLE_freeze { 2627sub STORABLE_freeze {
2574 my ($self, $cloning) = @_; 2628 my ($self, $cloning) = @_;
2575 2629
2576 $self->{path} 2630 $self->{path}
2577 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2631 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2578 2632
2579 $self->{path} 2633 $self->{path}
2580} 2634}
2581 2635
2582sub STORABLE_attach { 2636sub STORABLE_attach {
2613 glDisable GL_TEXTURE_2D; 2667 glDisable GL_TEXTURE_2D;
2614} 2668}
2615 2669
2616############################################################################# 2670#############################################################################
2617 2671
2618package CFPlus::UI::ImageButton; 2672package DC::UI::ImageButton;
2619 2673
2620our @ISA = CFPlus::UI::Image::; 2674our @ISA = DC::UI::Image::;
2621 2675
2622use CFPlus::OpenGL; 2676use DC::OpenGL;
2623 2677
2624my %textures; 2678my %textures;
2625 2679
2626sub new { 2680sub new {
2627 my $class = shift; 2681 my $class = shift;
2630 padding_x => 4, 2684 padding_x => 4,
2631 padding_y => 4, 2685 padding_y => 4,
2632 fg => [1, 1, 1], 2686 fg => [1, 1, 1],
2633 active_fg => [0, 0, 1], 2687 active_fg => [0, 0, 1],
2634 can_hover => 1, 2688 can_hover => 1,
2635 align => 0, 2689 align => 0.5,
2636 valign => 0, 2690 valign => 0.5,
2637 can_events => 1, 2691 can_events => 1,
2638 @_ 2692 @_
2639 ); 2693 );
2640} 2694}
2641 2695
2655 1 2709 1
2656} 2710}
2657 2711
2658############################################################################# 2712#############################################################################
2659 2713
2660package CFPlus::UI::VGauge; 2714package DC::UI::VGauge;
2661 2715
2662our @ISA = CFPlus::UI::Base::; 2716our @ISA = DC::UI::Base::;
2663 2717
2664use List::Util qw(min max); 2718use List::Util qw(min max);
2665 2719
2666use CFPlus::OpenGL; 2720use DC::OpenGL;
2667 2721
2668my %tex = ( 2722my %tex = (
2669 food => [ 2723 food => [
2670 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2671 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2725 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2672 ], 2726 ],
2673 grace => [ 2727 grace => [
2674 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2728 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/ 2729 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2676 ], 2730 ],
2677 hp => [ 2731 hp => [
2678 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2679 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2733 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2680 ], 2734 ],
2681 mana => [ 2735 mana => [
2682 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2736 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/ 2737 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2684 ], 2738 ],
2685); 2739);
2686 2740
2687# eg. VGauge->new (gauge => 'food'), default gauge: food 2741# eg. VGauge->new (gauge => 'food'), default gauge: food
2790 glDisable GL_TEXTURE_2D; 2844 glDisable GL_TEXTURE_2D;
2791} 2845}
2792 2846
2793############################################################################# 2847#############################################################################
2794 2848
2795package CFPlus::UI::Progress; 2849package DC::UI::Progress;
2796 2850
2797our @ISA = CFPlus::UI::Label::; 2851our @ISA = DC::UI::Label::;
2798 2852
2799use CFPlus::OpenGL; 2853use DC::OpenGL;
2800 2854
2801sub new { 2855sub new {
2802 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2803 2857
2804 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2805 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2806 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2807 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2808 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2809 fontsize => 0.9, 2865 fontsize => 0.9,
2810 valign => 0, 2866 valign => 0.5,
2811 align => 0, 2867 align => 0.5,
2812 can_events => 1, 2868 can_events => 1,
2813 ellipsise => 1, 2869 ellipsise => 1,
2814 label => "%d%%", 2870 label => "%d%%",
2815 %arg, 2871 %arg,
2816 ); 2872 );
2824 my ($self, $label) = @_; 2880 my ($self, $label) = @_;
2825 2881
2826 return if $self->{label} eq $label; 2882 return if $self->{label} eq $label;
2827 $self->{label} = $label; 2883 $self->{label} = $label;
2828 2884
2829 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2885 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2830} 2886}
2831 2887
2832sub set_value { 2888sub set_value {
2833 my ($self, $value) = @_; 2889 my ($self, $value) = @_;
2834 2890
2849 my ($self) = @_; 2905 my ($self) = @_;
2850 2906
2851 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2852 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2853 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2854 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2855 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2856 2915
2857 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2858 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2859 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2860 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2861 } 2920 }
2862 2921
2863 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2864 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2865 2928
2866 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2867 2930
2868 { 2931 {
2869 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2871 } 2934 }
2872} 2935}
2873 2936
2874############################################################################# 2937#############################################################################
2875 2938
2876package CFPlus::UI::ExperienceProgress; 2939package DC::UI::ExperienceProgress;
2877 2940
2878our @ISA = CFPlus::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2879 2942
2880sub new { 2943sub new {
2881 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2882 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2883 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2884 tooltip => sub { 2950 tooltip => sub {
2885 my ($self) = @_; 2951 my ($self) = @_;
2886 2952
2887 sprintf "level %d\n%s points\n%s next level\n%s to go", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2954 $tt,
2888 $self->{lvl}, 2955 $self->{lvl},
2889 ::formsep ($self->{exp}), 2956 ::formsep ($self->{exp}),
2890 ::formsep ($self->{nxt}), 2957 ::formsep ($self->{nxt}),
2891 ::formsep ($self->{nxt} - $self->{exp}), 2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2892 }, 2960 },
2893 %arg
2894 ); 2961 );
2895 2962
2896 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2897 if $::CONN; 2964 if $::CONN;
2898 2965
2906 if $::CONN; 2973 if $::CONN;
2907 2974
2908 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2909} 2976}
2910 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2911sub set_value { 2992sub set_value {
2912 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2913 2994
2914 $self->{lvl} = $lvl; 2995 $self->{lvl} = $lvl;
2915 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2916 2997
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); 2998 $self->SUPER::set_value ($self->_percent);
2929} 2999}
2930 3000
2931############################################################################# 3001#############################################################################
2932 3002
2933package CFPlus::UI::Gauge; 3003package DC::UI::Gauge;
2934 3004
2935our @ISA = CFPlus::UI::VBox::; 3005our @ISA = DC::UI::VBox::;
2936 3006
2937sub new { 3007sub new {
2938 my ($class, %arg) = @_; 3008 my ($class, %arg) = @_;
2939 3009
2940 my $self = $class->SUPER::new ( 3010 my $self = $class->SUPER::new (
2942 can_hover => 1, 3012 can_hover => 1,
2943 can_events => 1, 3013 can_events => 1,
2944 %arg, 3014 %arg,
2945 ); 3015 );
2946 3016
2947 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3017 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2948 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3018 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2949 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3019 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2950 3020
2951 $self 3021 $self
2952} 3022}
2953 3023
2954sub set_fontsize { 3024sub set_fontsize {
2975 $self->{value}->set_text ($val); 3045 $self->{value}->set_text ($val);
2976} 3046}
2977 3047
2978############################################################################# 3048#############################################################################
2979 3049
2980package CFPlus::UI::Slider; 3050package DC::UI::Slider;
2981 3051
2982use strict; 3052use strict;
2983 3053
2984use CFPlus::OpenGL; 3054use DC::OpenGL;
2985 3055
2986our @ISA = CFPlus::UI::DrawBG::; 3056our @ISA = DC::UI::DrawBG::;
2987 3057
2988my @tex = 3058my @tex =
2989 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3059 map { new_from_resource DC::Texture $_ }
2990 qw(s1_slider.png s1_slider_bg.png); 3060 qw(s1_slider.png s1_slider_bg.png);
2991 3061
2992sub new { 3062sub new {
2993 my $class = shift; 3063 my $class = shift;
2994 3064
3031sub set_value { 3101sub set_value {
3032 my ($self, $value) = @_; 3102 my ($self, $value) = @_;
3033 3103
3034 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3104 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3035 3105
3036 $hi = $lo + 1 if $hi <= $lo; 3106 $hi = $lo if $hi < $lo;
3037 3107
3038 $page = $hi - $lo if $page > $hi - $lo; 3108 $value = $hi - $page if $value > $hi - $page;
3039
3040 $value = $lo if $value < $lo; 3109 $value = $lo if $value < $lo;
3041 $value = $hi - $page if $value > $hi - $page;
3042 3110
3043 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3111 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3044 if $unit; 3112 if $unit;
3045 3113
3046 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3114 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3090sub invoke_mouse_wheel { 3158sub invoke_mouse_wheel {
3091 my ($self, $ev) = @_; 3159 my ($self, $ev) = @_;
3092 3160
3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3161 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3094 3162
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3163 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3096 3164
3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3165 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3098 3166
3099 1 3167 1
3100} 3168}
3110 my ($self) = @_; 3178 my ($self) = @_;
3111 3179
3112 unless ($self->{knob_w}) { 3180 unless ($self->{knob_w}) {
3113 $self->set_value ($self->{range}[0]); 3181 $self->set_value ($self->{range}[0]);
3114 3182
3115 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3183 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3116 my $range = ($hi - $page - $lo) || 1e-100; 3184 my $range = ($hi - $page - $lo) || 1e-10;
3117 3185
3118 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3186 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3119 3187
3120 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3188 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3121 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3189 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3122 3190
3123 $value = ($value - $lo) / $range; 3191 $value = ($value - $lo) / $range;
3153 glDisable GL_TEXTURE_2D; 3221 glDisable GL_TEXTURE_2D;
3154} 3222}
3155 3223
3156############################################################################# 3224#############################################################################
3157 3225
3158package CFPlus::UI::ValSlider; 3226package DC::UI::ValSlider;
3159 3227
3160our @ISA = CFPlus::UI::HBox::; 3228our @ISA = DC::UI::HBox::;
3161 3229
3162sub new { 3230sub new {
3163 my ($class, %arg) = @_; 3231 my ($class, %arg) = @_;
3164 3232
3165 my $range = delete $arg{range}; 3233 my $range = delete $arg{range};
3166 3234
3167 my $self = $class->SUPER::new ( 3235 my $self = $class->SUPER::new (
3168 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3236 slider => (new DC::UI::Slider expand => 1, range => $range),
3169 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3237 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3170 to_value => sub { shift }, 3238 to_value => sub { shift },
3171 from_value => sub { shift }, 3239 from_value => sub { shift },
3172 %arg, 3240 %arg,
3173 ); 3241 );
3174 3242
3194sub set_range { shift->{slider}->set_range (@_) } 3262sub set_range { shift->{slider}->set_range (@_) }
3195sub set_value { shift->{slider}->set_value (@_) } 3263sub set_value { shift->{slider}->set_value (@_) }
3196 3264
3197############################################################################# 3265#############################################################################
3198 3266
3199package CFPlus::UI::TextScroller; 3267package DC::UI::TextScroller;
3200 3268
3201our @ISA = CFPlus::UI::HBox::; 3269our @ISA = DC::UI::HBox::;
3202 3270
3203use CFPlus::OpenGL; 3271use DC::OpenGL;
3204 3272
3205sub new { 3273sub new {
3206 my $class = shift; 3274 my $class = shift;
3207 3275
3208 my $self = $class->SUPER::new ( 3276 my $self = $class->SUPER::new (
3210 can_events => 1, 3278 can_events => 1,
3211 indent => 0, 3279 indent => 0,
3212 #font => default_font 3280 #font => default_font
3213 @_, 3281 @_,
3214 3282
3215 layout => (new CFPlus::Layout), 3283 layout => (new DC::Layout),
3216 par => [], 3284 par => [],
3217 max_par => 0, 3285 max_par => 0,
3218 height => 0, 3286 height => 0,
3219 children => [ 3287 children => [
3220 (new CFPlus::UI::Empty expand => 1), 3288 (new DC::UI::Empty expand => 1),
3221 (new CFPlus::UI::Slider vertical => 1), 3289 (new DC::UI::Slider vertical => 1),
3222 ], 3290 ],
3223 ); 3291 );
3224 3292
3225 $self->{children}[1]->connect (changed => sub { $self->update }); 3293 $self->{children}[1]->connect (changed => sub { $self->update });
3226 3294
3282 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3350 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3283 $layout->set_markup ($para->{markup}); 3351 $layout->set_markup ($para->{markup});
3284 3352
3285 $layout->set_shapes ( 3353 $layout->set_shapes (
3286 map 3354 map
3287 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3355 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3288 @{$para->{widget}} 3356 @{$para->{widget}}
3289 ); 3357 );
3290 3358
3291 $layout 3359 $layout
3292} 3360}
3409 $ROOT->on_post_alloc ($self => sub { 3477 $ROOT->on_post_alloc ($self => sub {
3410 $self->force_uptodate; 3478 $self->force_uptodate;
3411 3479
3412 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3480 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3413 3481
3414 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3482 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3415 glClearColor 0, 0, 0, 0; 3483 glClearColor 0, 0, 0, 0;
3416 glClear GL_COLOR_BUFFER_BIT; 3484 glClear GL_COLOR_BUFFER_BIT;
3417 3485
3418 package CFPlus::UI::Base; 3486 package DC::UI::Base;
3419 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3487 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3420 (0, 0, $self->{w}, $self->{h}); 3488 (0, 0, $self->{w}, $self->{h});
3421 3489
3422 my $top = int $self->{children}[1]{range}[0]; 3490 my $top = int $self->{children}[1]{range}[0];
3423 3491
3482 $self->{children}[1]->draw; 3550 $self->{children}[1]->draw;
3483} 3551}
3484 3552
3485############################################################################# 3553#############################################################################
3486 3554
3487package CFPlus::UI::Animator; 3555package DC::UI::Animator;
3488 3556
3489use CFPlus::OpenGL; 3557use DC::OpenGL;
3490 3558
3491our @ISA = CFPlus::UI::Bin::; 3559our @ISA = DC::UI::Bin::;
3492 3560
3493sub moveto { 3561sub moveto {
3494 my ($self, $x, $y) = @_; 3562 my ($self, $x, $y) = @_;
3495 3563
3496 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3564 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3524 glPopMatrix; 3592 glPopMatrix;
3525} 3593}
3526 3594
3527############################################################################# 3595#############################################################################
3528 3596
3529package CFPlus::UI::Flopper; 3597package DC::UI::Flopper;
3530 3598
3531our @ISA = CFPlus::UI::Button::; 3599our @ISA = DC::UI::Button::;
3532 3600
3533sub new { 3601sub new {
3534 my $class = shift; 3602 my $class = shift;
3535 3603
3536 my $self = $class->SUPER::new ( 3604 my $self = $class->SUPER::new (
3548 $self->{other}->toggle_visibility; 3616 $self->{other}->toggle_visibility;
3549} 3617}
3550 3618
3551############################################################################# 3619#############################################################################
3552 3620
3553package CFPlus::UI::Tooltip; 3621package DC::UI::Tooltip;
3554 3622
3555our @ISA = CFPlus::UI::Bin::; 3623our @ISA = DC::UI::Bin::;
3556 3624
3557use CFPlus::OpenGL; 3625use DC::OpenGL;
3558 3626
3559sub new { 3627sub new {
3560 my $class = shift; 3628 my $class = shift;
3561 3629
3562 $class->SUPER::new ( 3630 $class->SUPER::new (
3569 my ($self, $widget) = @_; 3637 my ($self, $widget) = @_;
3570 3638
3571 my $tip = $widget->{tooltip}; 3639 my $tip = $widget->{tooltip};
3572 $tip = $tip->($widget) if "CODE" eq ref $tip; 3640 $tip = $tip->($widget) if "CODE" eq ref $tip;
3573 3641
3574 $tip = CFPlus::Pod::section_label tooltip => $1 3642 $tip = DC::Pod::section_label tooltip => $1
3575 if $tip =~ /^#(.*)$/; 3643 if $tip =~ /^#(.*)$/;
3576 3644
3577 if ($ENV{CFPLUS_DEBUG} & 2) { 3645 if ($ENV{CFPLUS_DEBUG} & 2) {
3578 $tip .= "\n\n" . (ref $widget) . "\n" 3646 $tip .= "\n\n" . (ref $widget) . "\n"
3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3647 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3582 } 3650 }
3583 3651
3584 $tip =~ s/^\n+//; 3652 $tip =~ s/^\n+//;
3585 $tip =~ s/\n+$//; 3653 $tip =~ s/\n+$//;
3586 3654
3587 $self->add (new CFPlus::UI::Label 3655 $self->add (new DC::UI::Label
3656 fg => $DC::THEME{tooltip_fg},
3588 markup => $tip, 3657 markup => $tip,
3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3658 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3659 align => 0,
3590 fontsize => 0.8, 3660 fontsize => 0.8,
3591 style => 1, # FLAG_INVERSE 3661 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3592 ellipsise => 0, 3662 ellipsise => 0,
3593 font => ($widget->{tooltip_font} || $::FONT_PROP), 3663 font => ($widget->{tooltip_font} || $::FONT_PROP),
3594 ); 3664 );
3595} 3665}
3596 3666
3633sub _draw { 3703sub _draw {
3634 my ($self) = @_; 3704 my ($self) = @_;
3635 3705
3636 my ($w, $h) = @$self{qw(w h)}; 3706 my ($w, $h) = @$self{qw(w h)};
3637 3707
3638 glColor 1, 0.8, 0.4; 3708 glColor @{ $DC::THEME{tooltip_bg} };
3639 glRect 0, 0, $w, $h; 3709 glRect 0, 0, $w, $h;
3640 3710
3641 glColor 0, 0, 0; 3711 glColor @{ $DC::THEME{tooltip_border} };
3642 glRect_lineloop .5, .5, $w + .5, $h + .5; 3712 glRect_lineloop .5, .5, $w + .5, $h + .5;
3643 3713
3644 glTranslate 2, 2; 3714 glTranslate 2, 2;
3645 3715
3646 $self->SUPER::_draw; 3716 $self->SUPER::_draw;
3647} 3717}
3648 3718
3649############################################################################# 3719#############################################################################
3650 3720
3651package CFPlus::UI::Face; 3721package DC::UI::Face;
3652 3722
3653our @ISA = CFPlus::UI::DrawBG::; 3723our @ISA = DC::UI::DrawBG::;
3654 3724
3655use CFPlus::OpenGL; 3725use DC::OpenGL;
3656 3726
3657sub new { 3727sub new {
3658 my $class = shift; 3728 my $class = shift;
3659 3729
3660 my $self = $class->SUPER::new ( 3730 my $self = $class->SUPER::new (
3663 aspect => 1, 3733 aspect => 1,
3664 can_events => 0, 3734 can_events => 0,
3665 @_, 3735 @_,
3666 ); 3736 );
3667 3737
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; 3738 $self->update_anim;
3692 }
3693 3739
3694 $self 3740 $self
3695} 3741}
3696 3742
3697sub update_timer { 3743sub update_timer {
3698 my ($self) = @_; 3744 my ($self) = @_;
3699 3745
3700 return unless $self->{timer}; 3746 return unless $self->{timer};
3701 3747
3702 if ($self->{visible}) { 3748 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; 3749 $self->{timer}->start;
3709 } else { 3750 } else {
3710 $self->{timer}->stop; 3751 $self->{timer}->stop;
3711 } 3752 }
3712} 3753}
3725 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3726 } 3767 }
3727 } 3768 }
3728 } 3769 }
3729 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3730 } 3799 }
3731} 3800}
3732 3801
3733sub size_request { 3802sub size_request {
3734 my ($self) = @_; 3803 my ($self) = @_;
3756 return unless $self->{visible}; 3825 return unless $self->{visible};
3757 3826
3758 $self->SUPER::update; 3827 $self->SUPER::update;
3759} 3828}
3760 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3761sub invoke_visibility_change { 3851sub invoke_visibility_change {
3762 my ($self) = @_; 3852 my ($self) = @_;
3763 3853
3764 $self->update_timer; 3854 $self->update_timer;
3765 3855
3789 $self->SUPER::destroy; 3879 $self->SUPER::destroy;
3790} 3880}
3791 3881
3792############################################################################# 3882#############################################################################
3793 3883
3794package CFPlus::UI::Buttonbar; 3884package DC::UI::Buttonbar;
3795 3885
3796our @ISA = CFPlus::UI::HBox::; 3886our @ISA = DC::UI::HBox::;
3797 3887
3798# TODO: should actually wrap buttons and other goodies. 3888# TODO: should actually wrap buttons and other goodies.
3799 3889
3800############################################################################# 3890#############################################################################
3801 3891
3802package CFPlus::UI::Menu; 3892package DC::UI::Menu;
3803 3893
3804our @ISA = CFPlus::UI::Toplevel::; 3894our @ISA = DC::UI::Toplevel::;
3805 3895
3806use CFPlus::OpenGL; 3896use DC::OpenGL;
3807 3897
3808sub new { 3898sub new {
3809 my $class = shift; 3899 my $class = shift;
3810 3900
3811 my $self = $class->SUPER::new ( 3901 my $self = $class->SUPER::new (
3812 items => [], 3902 items => [],
3813 z => 100, 3903 z => 100,
3814 @_, 3904 @_,
3815 ); 3905 );
3816 3906
3817 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3907 $self->add ($self->{vbox} = new DC::UI::VBox);
3818 3908
3819 for my $item (@{ $self->{items} }) { 3909 for my $item (@{ $self->{items} }) {
3820 my ($widget, $cb, $tooltip) = @$item; 3910 my ($widget, $cb, $tooltip) = @$item;
3821 3911
3822 # handle various types of items, only text for now 3912 # handle various types of items, only text for now
3823 if (!ref $widget) { 3913 if (!ref $widget) {
3824 if ($widget =~ /\t/) { 3914 if ($widget =~ /\t/) {
3825 my ($left, $right) = split /\t/, $widget, 2; 3915 my ($left, $right) = split /\t/, $widget, 2;
3826 3916
3827 $widget = new CFPlus::UI::HBox 3917 $widget = new DC::UI::HBox
3828 can_hover => 1, 3918 can_hover => 1,
3829 can_events => 1, 3919 can_events => 1,
3830 tooltip => $tooltip, 3920 tooltip => $tooltip,
3831 children => [ 3921 children => [
3832 (new CFPlus::UI::Label markup => $left, expand => 1), 3922 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3833 (new CFPlus::UI::Label markup => $right, align => +1), 3923 (new DC::UI::Label markup => $right, align => 1),
3834 ], 3924 ],
3835 ; 3925 ;
3836 3926
3837 } else { 3927 } else {
3838 $widget = new CFPlus::UI::Label 3928 $widget = new DC::UI::Label
3839 can_hover => 1, 3929 can_hover => 1,
3840 can_events => 1, 3930 can_events => 1,
3931 align => 0,
3841 markup => $widget, 3932 markup => $widget,
3842 tooltip => $tooltip; 3933 tooltip => $tooltip;
3843 } 3934 }
3844 } 3935 }
3845 3936
3860 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3861 $GRAB = $self; 3952 $GRAB = $self;
3862 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3863 3954
3864 $self->show; 3955 $self->show;
3865 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3866} 3965}
3867 3966
3868sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3869 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3870 3969
3891 1 3990 1
3892} 3991}
3893 3992
3894############################################################################# 3993#############################################################################
3895 3994
3896package CFPlus::UI::Multiplexer; 3995package DC::UI::Multiplexer;
3897 3996
3898our @ISA = CFPlus::UI::Container::; 3997our @ISA = DC::UI::Container::;
3899 3998
3900sub new { 3999sub new {
3901 my $class = shift; 4000 my $class = shift;
3902 4001
3903 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3904 @_, 4003 @_,
3905 ); 4004 );
3906 4005
3907 $self->{current} = $self->{children}[0] 4006 $self->set_current_page (0);
3908 if @{ $self->{children} };
3909 4007
3910 $self 4008 $self
3911} 4009}
3912 4010
3913sub add { 4011sub add {
3914 my ($self, @widgets) = @_; 4012 my ($self, @widgets) = @_;
3915 4013
3916 $self->SUPER::add (@widgets); 4014 $self->SUPER::add (@widgets);
3917 4015
3918 $self->{current} = $self->{children}[0] 4016 $self->set_current_page (0)
3919 if @{ $self->{children} }; 4017 if @widgets == @{ $self->{children} };
3920} 4018}
3921 4019
3922sub get_current_page { 4020sub get_current_page {
3923 my ($self) = @_; 4021 my ($self) = @_;
3924 4022
3930 4028
3931 my $widget = ref $page_or_widget 4029 my $widget = ref $page_or_widget
3932 ? $page_or_widget 4030 ? $page_or_widget
3933 : $self->{children}[$page_or_widget]; 4031 : $self->{children}[$page_or_widget];
3934 4032
4033 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4034
3935 $self->{current} = $widget; 4035 if (($self->{current} = $widget)) {
4036 $self->{current}->set_visible if $self->{current} && $self->{visible};
3936 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4037 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3937 4038
3938 $self->emit (page_changed => $self->{current}); 4039 $self->emit (page_changed => $self->{current});
4040 }
3939 4041
3940 $self->realloc; 4042 $self->realloc;
3941} 4043}
3942 4044
3943sub visible_children { 4045sub visible_children {
3944 $_[0]{current} 4046 $_[0]{current} || ()
3945} 4047}
3946 4048
3947sub size_request { 4049sub size_request {
3948 my ($self) = @_; 4050 my ($self) = @_;
3949 4051
4052 $self->{current}
3950 $self->{current}->size_request 4053 ? $self->{current}->size_request
4054 : (0, 0)
3951} 4055}
3952 4056
3953sub invoke_size_allocate { 4057sub invoke_size_allocate {
3954 my ($self, $w, $h) = @_; 4058 my ($self, $w, $h) = @_;
3955 4059
3956 $self->{current}->configure (0, 0, $w, $h); 4060 $self->{current}->configure (0, 0, $w, $h)
4061 if $self->{current};
3957 4062
3958 1 4063 1
3959} 4064}
3960 4065
3961sub _draw { 4066sub _draw {
3962 my ($self) = @_; 4067 my ($self) = @_;
3963 4068
3964 $self->{current}->draw; 4069 $self->{current}->draw
4070 if $self->{current};
3965} 4071}
3966 4072
3967############################################################################# 4073#############################################################################
3968 4074
3969package CFPlus::UI::Notebook; 4075package DC::UI::Notebook;
3970 4076
3971use CFPlus::OpenGL; 4077use DC::OpenGL;
3972 4078
3973our @ISA = CFPlus::UI::VBox::; 4079our @ISA = DC::UI::VBox::;
3974 4080
3975sub new { 4081sub new {
3976 my $class = shift; 4082 my $class = shift;
3977 4083
3978 my $self = $class->SUPER::new ( 4084 my $self = $class->SUPER::new (
3979 buttonbar => (new CFPlus::UI::Buttonbar), 4085 buttonbar => (new DC::UI::Buttonbar),
3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4086 multiplexer => (new DC::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2], 4087 active_outline => [.7, .7, 0.2],
3982 # filter => # will be put between multiplexer and $self 4088 # filter => # will be put between multiplexer and $self
3983 @_, 4089 @_,
3984 ); 4090 );
3985 4091
3994 4100
3995 for my $child (@$widgets) { 4101 for my $child (@$widgets) {
3996 Scalar::Util::weaken $child; 4102 Scalar::Util::weaken $child;
3997 $child->{c_tab_} ||= do { 4103 $child->{c_tab_} ||= do {
3998 my $tab = 4104 my $tab =
3999 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4105 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4000 ? $child->{c_tab} 4106 ? $child->{c_tab}
4001 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4107 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4002 4108
4003 $tab->connect (activate => sub { 4109 $tab->connect (activate => sub {
4004 $wself->set_current_page ($child); 4110 $wself->set_current_page ($child);
4005 }); 4111 });
4006 4112
4036} 4142}
4037 4143
4038sub pages { 4144sub pages {
4039 my ($self) = @_; 4145 my ($self) = @_;
4040 $self->{multiplexer}->children 4146 $self->{multiplexer}->children
4147}
4148
4149sub page_index {
4150 my ($self, $widget) = @_;
4151
4152 my $i = 0;
4153 for ($self->pages) {
4154 if ($_ eq $widget) { return $i };
4155 $i++;
4156 }
4157
4158 undef
4041} 4159}
4042 4160
4043sub add_tab { 4161sub add_tab {
4044 my ($self, $title, $widget, $tooltip) = @_; 4162 my ($self, $title, $widget, $tooltip) = @_;
4045 4163
4082 } 4200 }
4083} 4201}
4084 4202
4085############################################################################# 4203#############################################################################
4086 4204
4087package CFPlus::UI::Selector; 4205package DC::UI::Selector;
4088 4206
4089use utf8; 4207use utf8;
4090 4208
4091our @ISA = CFPlus::UI::Button::; 4209our @ISA = DC::UI::Button::;
4092 4210
4093sub new { 4211sub new {
4094 my $class = shift; 4212 my $class = shift;
4095 4213
4096 my $self = $class->SUPER::new ( 4214 my $self = $class->SUPER::new (
4113 my ($value, $title, $tooltip) = @$_; 4231 my ($value, $title, $tooltip) = @$_;
4114 4232
4115 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4233 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4116 } 4234 }
4117 4235
4118 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4236 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4119} 4237}
4120 4238
4121sub _set_value { 4239sub _set_value {
4122 my ($self, $value) = @_; 4240 my ($self, $value) = @_;
4123 4241
4124 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4242 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4243 $item ||= $self->{options}[0]
4125 or return; 4244 or return;
4126 4245
4127 $self->{value} = $item->[0]; 4246 $self->{value} = $item->[0];
4128 $self->set_markup ("$item->[1] ⇓"); 4247 $self->set_markup ("$item->[1] ⇓");
4129 $self->set_tooltip ($item->[2]); 4248# $self->set_tooltip ($item->[2]);
4130} 4249}
4131 4250
4132sub set_value { 4251sub set_value {
4133 my ($self, $value) = @_; 4252 my ($self, $value) = @_;
4134 4253
4136 4255
4137 $self->_set_value ($value); 4256 $self->_set_value ($value);
4138 $self->emit (changed => $value); 4257 $self->emit (changed => $value);
4139} 4258}
4140 4259
4260sub set_options {
4261 my ($self, $options) = @_;
4262
4263 $self->{options} = $options;
4264 $self->_set_value ($self->{value});
4265}
4266
4141############################################################################# 4267#############################################################################
4142 4268
4143package CFPlus::UI::Statusbox; 4269package DC::UI::Statusbox;
4144 4270
4145our @ISA = CFPlus::UI::VBox::; 4271our @ISA = DC::UI::VBox::;
4146 4272
4147sub new { 4273sub new {
4148 my $class = shift; 4274 my $class = shift;
4149 4275
4150 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4151 fontsize => 0.8, 4277 fontsize => 0.8,
4152 @_, 4278 @_,
4153 ); 4279 );
4154 4280
4155 CFPlus::weaken (my $this = $self); 4281 DC::weaken (my $this = $self);
4156 4282
4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4283 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4158 4284
4159 $self 4285 $self
4160} 4286}
4161 4287
4162sub reorder { 4288sub reorder {
4163 my ($self) = @_; 4289 my ($self) = @_;
4164 my $NOW = Time::HiRes::time; 4290 my $NOW = EV::time;
4165 4291
4166 # freeze display when hovering over any label 4292 # freeze display when hovering over any label
4167 return if $CFPlus::UI::TOOLTIP->{owner} 4293 return if $DC::UI::TOOLTIP->{owner}
4168 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4294 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4169 values %{ $self->{item} }; 4295 values %{ $self->{item} };
4170 4296
4171 while (my ($k, $v) = each %{ $self->{item} }) { 4297 while (my ($k, $v) = each %{ $self->{item} }) {
4172 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4298 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4173 } 4299 }
4300
4301 $self->{timer}->set (1, 1);
4174 4302
4175 my @widgets; 4303 my @widgets;
4176 4304
4177 my @items = sort { 4305 my @items = sort {
4178 $a->{pri} <=> $b->{pri} 4306 $a->{pri} <=> $b->{pri}
4179 or $b->{id} <=> $a->{id} 4307 or $b->{id} <=> $a->{id}
4180 } values %{ $self->{item} }; 4308 } values %{ $self->{item} };
4181
4182 $self->{timer}->interval (1);
4183 4309
4184 my $count = 10 + 1; 4310 my $count = 10 + 1;
4185 for my $item (@items) { 4311 for my $item (@items) {
4186 last unless --$count; 4312 last unless --$count;
4187 4313
4194 for ($short) { 4320 for ($short) {
4195 s/^\s+//; 4321 s/^\s+//;
4196 s/\s+/ /g; 4322 s/\s+/ /g;
4197 } 4323 }
4198 4324
4199 new CFPlus::UI::Label 4325 new DC::UI::Label
4200 markup => $short, 4326 markup => $short,
4201 tooltip => $item->{tooltip}, 4327 tooltip => $item->{tooltip},
4202 tooltip_font => $::FONT_PROP, 4328 tooltip_font => $::FONT_PROP,
4203 tooltip_width => 0.67, 4329 tooltip_width => 0.67,
4204 fontsize => $item->{fontsize} || $self->{fontsize}, 4330 fontsize => $item->{fontsize} || $self->{fontsize},
4205 max_w => $::WIDTH * 0.44, 4331 max_w => $::WIDTH * 0.44,
4332 align => 0,
4206 fg => [@{ $item->{fg} }], 4333 fg => [@{ $item->{fg} }],
4207 can_events => 1, 4334 can_events => 1,
4208 can_hover => 1 4335 can_hover => 1
4209 }; 4336 };
4210 4337
4211 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4338 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4212 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4339 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4213 $label->update; 4340 $label->update;
4214 $label->set_max_size (undef, $label->{req_h} * $diff) 4341 $label->set_max_size (undef, $label->{req_h} * $diff)
4215 if $diff < 1; 4342 if $diff < 1;
4216 $self->{timer}->interval (1/30); 4343 $self->{timer}->set (1/30, 1/30);
4217 } else { 4344 } else {
4218 $label->{fg}[3] = $item->{fg}[3] || 1; 4345 $label->{fg}[3] = $item->{fg}[3] || 1;
4219 } 4346 }
4220 4347
4221 push @widgets, $label; 4348 push @widgets, $label;
4222 } 4349 }
4350
4351 my $hash = join ",", @widgets;
4352 return if $hash eq $self->{last_widget_hash};
4353 $self->{last_widget_hash} = $hash;
4223 4354
4224 $self->clear; 4355 $self->clear;
4225 $self->SUPER::add (reverse @widgets); 4356 $self->SUPER::add (reverse @widgets);
4226} 4357}
4227 4358
4283 $self->SUPER::destroy; 4414 $self->SUPER::destroy;
4284} 4415}
4285 4416
4286############################################################################# 4417#############################################################################
4287 4418
4288package CFPlus::UI::Root; 4419package DC::UI::Root;
4289 4420
4290our @ISA = CFPlus::UI::Container::; 4421our @ISA = DC::UI::Container::;
4291 4422
4292use List::Util qw(min max); 4423use List::Util qw(min max);
4293 4424
4294use CFPlus::OpenGL; 4425use DC::OpenGL;
4295 4426
4296sub new { 4427sub new {
4297 my $class = shift; 4428 my $class = shift;
4298 4429
4299 my $self = $class->SUPER::new ( 4430 my $self = $class->SUPER::new (
4300 visible => 1, 4431 visible => 1,
4301 @_, 4432 @_,
4302 ); 4433 );
4303 4434
4304 CFPlus::weaken ($self->{root} = $self); 4435 DC::weaken ($self->{root} = $self);
4305 4436
4306 $self 4437 $self
4307} 4438}
4308 4439
4309sub size_request { 4440sub size_request {
4357} 4488}
4358 4489
4359sub update { 4490sub update {
4360 my ($self) = @_; 4491 my ($self) = @_;
4361 4492
4362 $::WANT_REFRESH->start; 4493 $::WANT_REFRESH = 1;
4363} 4494}
4364 4495
4365sub add { 4496sub add {
4366 my ($self, @children) = @_; 4497 my ($self, @children) = @_;
4367 4498
4434 4565
4435 delete $queue{$widget+0}; 4566 delete $queue{$widget+0};
4436 4567
4437 my ($w, $h) = $widget->size_request; 4568 my ($w, $h) = $widget->size_request;
4438 4569
4439 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4570 $w += $widget->{padding_x} * 2;
4440 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4571 $h += $widget->{padding_y} * 2;
4572
4573 $w = max $widget->{min_w}, $w;
4574 $h = max $widget->{min_h}, $h;
4441 4575
4442 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4576 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4443 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4577 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4444 4578
4445 $w = $widget->{force_w} if exists $widget->{force_w}; 4579 $w = $widget->{force_w} if exists $widget->{force_w};
4512 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4646 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4513 glMatrixMode GL_MODELVIEW; 4647 glMatrixMode GL_MODELVIEW;
4514 glLoadIdentity; 4648 glLoadIdentity;
4515 4649
4516 { 4650 {
4517 package CFPlus::UI::Base; 4651 package DC::UI::Base;
4518 4652
4519 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4653 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4520 (0, 0, $self->{w}, $self->{h}); 4654 (0, 0, $self->{w}, $self->{h});
4521 4655
4522 $self->_draw; 4656 $self->_draw;
4523 } 4657 }
4524} 4658}
4525 4659
4526############################################################################# 4660#############################################################################
4527 4661
4528package CFPlus::UI; 4662package DC::UI;
4529 4663
4530$ROOT = new CFPlus::UI::Root; 4664$ROOT = new DC::UI::Root;
4531$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4532 4666
45331 46671
4534

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines