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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines