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

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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines