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.434 by root, Wed Aug 22 21:32:09 2007 UTC vs.
Revision 1.477 by root, Sun Jan 11 23:50:27 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use Guard ();
9
10use DC;
10use CFPlus::Pod; 11use DC::Pod;
11use CFPlus::Texture; 12use DC::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
17our $TOOLTIP; 18our $TOOLTIP;
18our $BUTTON_STATE; 19our $BUTTON_STATE;
19 20
20our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
21 22
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
24 $_[0]->stop;
25
23 if (!$GRAB) { 26 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 29 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 30 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 33 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 34 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 35
33 return if $ENV{CFPLUS_DEBUG} & 8; 36 return if $ENV{CFPLUS_DEBUG} & 8;
34 37
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 38 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 39 $TOOLTIP->show;
41 } 40 }
42 41
43 return; 42 return;
46 } 45 }
47 46
48 $TOOLTIP->hide; 47 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 48 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 49 delete $TOOLTIP->{owner};
51}); 50};
52 51
53sub get_layout { 52sub get_layout {
54 my $layout; 53 my $layout;
55 54
56 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
92 91
93 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 94
96 $TOOLTIP_WATCHER->start; 95 $TOOLTIP_WATCHER->again;
97 } 96 }
98} 97}
99 98
100sub feed_sdl_button_down_event { 99sub feed_sdl_button_down_event {
101 my ($ev) = @_; 100 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 106 my $widget = $ROOT->find_widget ($x, $y);
108 107
109 $GRAB = $widget; 108 $GRAB = $widget;
110 $GRAB->update if $GRAB; 109 $GRAB->update if $GRAB;
111 110
112 $TOOLTIP_WATCHER->cb->(); 111 $TOOLTIP_WATCHER->invoke;
113 } 112 }
114 113
115 if ($GRAB) { 114 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 116 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 119
121 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
123 122
124 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 141 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 142 $grab->update if $grab;
144 $GRAB->update if $GRAB; 143 $GRAB->update if $GRAB;
145 144
146 check_hover $widget; 145 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 146 $TOOLTIP_WATCHER->invoke;
148 } 147 }
149} 148}
150 149
151sub feed_sdl_motion_event { 150sub feed_sdl_motion_event {
152 my ($ev) = @_; 151 my ($ev) = @_;
192# call when resolution changes etc. 191# call when resolution changes etc.
193sub rescale_widgets { 192sub rescale_widgets {
194 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
195 194
196 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
197 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
198 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
200 199
201 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
202 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
214 reconfigure_widgets; 213 reconfigure_widgets;
215} 214}
216 215
217############################################################################# 216#############################################################################
218 217
219package CFPlus::UI::Event; 218package DC::UI::Event;
220 219
221sub xy { 220sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 222}
224 223
225############################################################################# 224#############################################################################
226 225
227package CFPlus::UI::Base; 226package DC::UI::Base;
228 227
229use strict; 228use strict;
230 229
231use CFPlus::OpenGL; 230use DC::OpenGL;
232 231
233sub new { 232sub new {
234 my $class = shift; 233 my $class = shift;
235 234
236 my $self = bless { 235 my $self = bless {
241 h => undef, 240 h => undef,
242 can_events => 1, 241 can_events => 1,
243 @_ 242 @_
244 }, $class; 243 }, $class;
245 244
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 246
248 for (keys %$self) { 247 for (keys %$self) {
249 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
251 } 250 }
252 } 251 }
253 252
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
256 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
257 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
258 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
259 258
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 261
263 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
273 $self->emit ("destroy"); 272 $self->emit ("destroy");
274 %$self = (); 273 %$self = ();
275} 274}
276 275
277sub TO_JSON { 276sub TO_JSON {
278 { __w_ => $_[0]{s_id} } 277 { "\fw" => $_[0]{s_id} }
279} 278}
280 279
281sub show { 280sub show {
282 my ($self) = @_; 281 my ($self) = @_;
283 282
284 return if $self->{parent}; 283 return if $self->{parent};
285 284
286 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
287} 286}
288 287
289sub set_visible { 288sub set_visible {
290 my ($self) = @_; 289 my ($self) = @_;
291 290
292 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
293 295
294 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
295 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
296 298
297 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
298 300
299 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
300 302
301 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
302} 304}
303 305
304sub set_invisible { 306sub set_invisible {
305 my ($self) = @_; 307 my ($self) = @_;
306 308
312 delete $self->{root}; 314 delete $self->{root};
313 315
314 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
316 318
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 319 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
319 321
320 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
322} 324}
341sub hide { 343sub hide {
342 my ($self) = @_; 344 my ($self) = @_;
343 345
344 $self->set_invisible; 346 $self->set_invisible;
345 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 351 $parent->remove ($self);
347 if $self->{parent}; 352 }
348} 353}
349 354
350sub move_abs { 355sub move_abs {
351 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
352 357
364 $self->{force_h} = $h; 369 $self->{force_h} = $h;
365 370
366 $self->realloc; 371 $self->realloc;
367} 372}
368 373
374# traverse the widget chain up to find the maximum "physical" size constraints
375sub get_max_wh {
376 my ($self) = @_;
377
378 my ($w, $h) = @$self{qw(max_w max_h)};
379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
382 ? $self->{parent}->get_max_wh
383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
390}
391
369sub size_request { 392sub size_request {
370 require Carp; 393 require Carp;
371 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
372} 395}
373 396
379 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
380 403
381 if ($self->{aspect}) { 404 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
383 406
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 409
387 # use alignment to adjust x, y 410 # use alignment to adjust x, y
388 411
389 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
431 454
432 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
433 456
434 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
435 458
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 462 }
440} 463}
441 464
442# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
443sub coord2local { 466sub coord2local {
444 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
445 468
446 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
447 470
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 472}
450 473
451# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
452sub coord2global { 475sub coord2global {
453 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
454 477
455 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
456 479
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 481}
459 482
460sub invoke_focus_in { 483sub invoke_focus_in {
509sub connect { 532sub connect {
510 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
511 534
512 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
513 536
514 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
517 } 540 }
518} 541}
519 542
557# in .xs 580# in .xs
558 581
559sub set_parent { 582sub set_parent {
560 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
561 584
562 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
564} 587}
565 588
566sub realloc { 589sub realloc {
567 my ($self) = @_; 590 my ($self) = @_;
605} 628}
606 629
607sub DESTROY { 630sub DESTROY {
608 my ($self) = @_; 631 my ($self) = @_;
609 632
610 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
611 634
612 local $@; 635 local $@;
613 eval { $self->destroy }; 636 eval { $self->destroy };
614 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
615 638
616 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
617} 640}
618 641
619############################################################################# 642#############################################################################
620 643
621package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
622 645
623our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
624 647
625use strict; 648use strict;
626use CFPlus::OpenGL; 649use DC::OpenGL;
627 650
628sub new { 651sub new {
629 my $class = shift; 652 my $class = shift;
630 653
631 $class->SUPER::new ( 654 $class->SUPER::new (
633 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
634 @_ 657 @_
635 ) 658 )
636} 659}
637 660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
638sub _draw { 668sub _draw {
639 my ($self) = @_; 669 my ($self) = @_;
640 670
641 my $color = $FOCUS == $self && $self->{active_bg} 671 my $color = $FOCUS == $self && $self->{active_bg}
642 ? $self->{active_bg} 672 ? $self->{active_bg}
653 } 683 }
654} 684}
655 685
656############################################################################# 686#############################################################################
657 687
658package CFPlus::UI::Empty; 688package DC::UI::Empty;
659 689
660our @ISA = CFPlus::UI::Base::; 690our @ISA = DC::UI::Base::;
661 691
662sub new { 692sub new {
663 my ($class, %arg) = @_; 693 my ($class, %arg) = @_;
664 $class->SUPER::new (can_events => 0, %arg); 694 $class->SUPER::new (can_events => 0, %arg);
665} 695}
672 702
673sub draw { } 703sub draw { }
674 704
675############################################################################# 705#############################################################################
676 706
677package CFPlus::UI::Container; 707package DC::UI::Container;
678 708
679our @ISA = CFPlus::UI::Base::; 709our @ISA = DC::UI::Base::;
680 710
681sub new { 711sub new {
682 my ($class, %arg) = @_; 712 my ($class, %arg) = @_;
683 713
684 my $children = delete $arg{children}; 714 my $children = delete $arg{children};
778 $_->draw for $self->visible_children; 808 $_->draw for $self->visible_children;
779} 809}
780 810
781############################################################################# 811#############################################################################
782 812
783package CFPlus::UI::Bin; 813package DC::UI::Bin;
784 814
785our @ISA = CFPlus::UI::Container::; 815our @ISA = DC::UI::Container::;
786 816
787sub new { 817sub new {
788 my ($class, %arg) = @_; 818 my ($class, %arg) = @_;
789 819
790 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 820 my $child = (delete $arg{child}) || new DC::UI::Empty::;
791 821
792 $class->SUPER::new (children => [$child], %arg) 822 $class->SUPER::new (children => [$child], %arg)
793} 823}
794 824
795sub add { 825sub add {
802sub remove { 832sub remove {
803 my ($self, $widget) = @_; 833 my ($self, $widget) = @_;
804 834
805 $self->SUPER::remove ($widget); 835 $self->SUPER::remove ($widget);
806 836
807 $self->{children} = [new CFPlus::UI::Empty] 837 $self->{children} = [new DC::UI::Empty]
808 unless @{$self->{children}}; 838 unless @{$self->{children}};
809} 839}
810 840
811sub child { $_[0]->{children}[0] } 841sub child { $_[0]->{children}[0] }
812 842
821 851
822 1 852 1
823} 853}
824 854
825############################################################################# 855#############################################################################
826
827# back-buffered drawing area 856# back-buffered drawing area
828 857
829package CFPlus::UI::Window; 858package DC::UI::Window;
830 859
831our @ISA = CFPlus::UI::Bin::; 860our @ISA = DC::UI::Bin::;
832 861
833use CFPlus::OpenGL; 862use DC::OpenGL;
834 863
835sub new { 864sub new {
836 my ($class, %arg) = @_; 865 my ($class, %arg) = @_;
837 866
838 my $self = $class->SUPER::new (%arg); 867 my $self = $class->SUPER::new (%arg);
860} 889}
861 890
862sub render_child { 891sub render_child {
863 my ($self) = @_; 892 my ($self) = @_;
864 893
865 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 894 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
866 glClearColor 0, 0, 0, 0; 895 glClearColor 0, 0, 0, 0;
867 glClear GL_COLOR_BUFFER_BIT; 896 glClear GL_COLOR_BUFFER_BIT;
868 897
869 { 898 {
870 package CFPlus::UI::Base; 899 package DC::UI::Base;
871 900
872 local ($draw_x, $draw_y, $draw_w, $draw_h) = 901 local ($draw_x, $draw_y, $draw_w, $draw_h) =
873 (0, 0, $self->{w}, $self->{h}); 902 (0, 0, $self->{w}, $self->{h});
874 903
875 $self->_render; 904 $self->_render;
892 glDisable GL_TEXTURE_2D; 921 glDisable GL_TEXTURE_2D;
893} 922}
894 923
895############################################################################# 924#############################################################################
896 925
897package CFPlus::UI::ViewPort; 926package DC::UI::ViewPort;
898 927
899use List::Util qw(min max); 928use List::Util qw(min max);
900 929
901our @ISA = CFPlus::UI::Window::; 930our @ISA = DC::UI::Window::;
902 931
903sub new { 932sub new {
904 my $class = shift; 933 my $class = shift;
905 934
906 $class->SUPER::new ( 935 $class->SUPER::new (
990 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1019 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
991 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1020 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
992 ) { 1021 ) {
993 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1022 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
994 } else { 1023 } else {
995 $self->CFPlus::UI::Base::find_widget ($x, $y) 1024 $self->DC::UI::Base::find_widget ($x, $y)
996 } 1025 }
997} 1026}
998 1027
999sub _render { 1028sub _render {
1000 my ($self) = @_; 1029 my ($self) = @_;
1001 1030
1002 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1031 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1003 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1032 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1004 1033
1005 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1034 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1006 1035
1007 $self->SUPER::_render; 1036 $self->SUPER::_render;
1008} 1037}
1009 1038
1010############################################################################# 1039#############################################################################
1011 1040
1012package CFPlus::UI::ScrolledWindow; 1041package DC::UI::ScrolledWindow;
1013 1042
1014our @ISA = CFPlus::UI::Table::; 1043our @ISA = DC::UI::Table::;
1015 1044
1016sub new { 1045sub new {
1017 my ($class, %arg) = @_; 1046 my ($class, %arg) = @_;
1018 1047
1019 my $child = delete $arg{child}; 1048 my $child = delete $arg{child};
1020 1049
1021 my $self; 1050 my $self;
1022 1051
1023 my $hslider = new CFPlus::UI::Slider 1052 my $hslider = new DC::UI::Slider
1024 c_col => 0, 1053 c_col => 0,
1025 c_row => 1, 1054 c_row => 1,
1026 vertical => 0, 1055 vertical => 0,
1027 range => [0, 0, 1, 0.01], # HACK fix 1056 range => [0, 0, 1, 0.01], # HACK fix
1028 on_changed => sub { 1057 on_changed => sub {
1029 $self->{hpos} = $_[1]; 1058 $self->{hpos} = $_[1];
1030 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1059 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1031 }, 1060 },
1032 ; 1061 ;
1033 1062
1034 my $vslider = new CFPlus::UI::Slider 1063 my $vslider = new DC::UI::Slider
1035 c_col => 1, 1064 c_col => 1,
1036 c_row => 0, 1065 c_row => 0,
1037 vertical => 1, 1066 vertical => 1,
1038 range => [0, 0, 1, 0.01], # HACK fix 1067 range => [0, 0, 1, 0.01], # HACK fix
1039 on_changed => sub { 1068 on_changed => sub {
1051 col_expand => [1, 0], 1080 col_expand => [1, 0],
1052 row_expand => [1, 0], 1081 row_expand => [1, 0],
1053 %arg, 1082 %arg,
1054 ); 1083 );
1055 1084
1056 $self->{vp} = new CFPlus::UI::ViewPort 1085 $self->{vp} = new DC::UI::ViewPort
1057 c_col => 0, 1086 c_col => 0,
1058 c_row => 0, 1087 c_row => 0,
1059 expand => 1, 1088 expand => 1,
1060 scroll_x => $self->{scroll_x}, 1089 scroll_x => $self->{scroll_x},
1061 scroll_y => $self->{scroll_y}, 1090 scroll_y => $self->{scroll_y},
1187 $self->SUPER::invoke_size_allocate ($w, $h) 1216 $self->SUPER::invoke_size_allocate ($w, $h)
1188} 1217}
1189 1218
1190############################################################################# 1219#############################################################################
1191 1220
1192package CFPlus::UI::Frame; 1221package DC::UI::Frame;
1193 1222
1194our @ISA = CFPlus::UI::Bin::; 1223our @ISA = DC::UI::Bin::;
1195 1224
1196use CFPlus::OpenGL; 1225use DC::OpenGL;
1197 1226
1198sub new { 1227sub new {
1199 my $class = shift; 1228 my $class = shift;
1200 1229
1201 $class->SUPER::new ( 1230 $class->SUPER::new (
1220 $self->SUPER::_draw; 1249 $self->SUPER::_draw;
1221} 1250}
1222 1251
1223############################################################################# 1252#############################################################################
1224 1253
1225package CFPlus::UI::FancyFrame; 1254package DC::UI::FancyFrame;
1226 1255
1227our @ISA = CFPlus::UI::Bin::; 1256our @ISA = DC::UI::Bin::;
1228 1257
1229use CFPlus::OpenGL; 1258use DC::OpenGL;
1230 1259
1231sub new { 1260sub new {
1232 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1233 1262
1234 if ((exists $arg{label}) && !ref $arg{label}) { 1263 if ((exists $arg{label}) && !ref $arg{label}) {
1235 $arg{label} = new CFPlus::UI::Label 1264 $arg{label} = new DC::UI::Label
1236 align => 1, 1265 align => 1,
1237 valign => 0, 1266 valign => 0.5,
1238 text => $arg{label}, 1267 text => $arg{label},
1239 fontsize => ($arg{border} || 0.8) * 0.75; 1268 fontsize => ($arg{border} || 0.8) * 0.75;
1240 } 1269 }
1241 1270
1242 my $self = $class->SUPER::new ( 1271 my $self = $class->SUPER::new (
1243 # label => "", 1272 # label => "",
1244 fg => [0.6, 0.3, 0.1], 1273 fg => undef,
1245 border => 0.8, 1274 border => 0.8,
1246 style => 'single', 1275 style => 'single',
1247 %arg, 1276 %arg,
1248 ); 1277 );
1249 1278
1252 1281
1253sub add { 1282sub add {
1254 my ($self, @widgets) = @_; 1283 my ($self, @widgets) = @_;
1255 1284
1256 $self->SUPER::add (@widgets); 1285 $self->SUPER::add (@widgets);
1257 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1286 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1258} 1287}
1259 1288
1260sub border { 1289sub border {
1261 int $_[0]{border} * $::FONTSIZE 1290 int $_[0]{border} * $::FONTSIZE
1262} 1291}
1302 my $border = $self->border; 1331 my $border = $self->border;
1303 my ($w, $h) = ($self->{w}, $self->{h}); 1332 my ($w, $h) = ($self->{w}, $self->{h});
1304 1333
1305 $child->draw; 1334 $child->draw;
1306 1335
1307 glColor @{$self->{fg}}; 1336 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1308 glBegin GL_LINE_STRIP; 1337 glBegin GL_LINE_STRIP;
1309 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1338 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1310 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1311 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1312 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1320 } 1349 }
1321} 1350}
1322 1351
1323############################################################################# 1352#############################################################################
1324 1353
1325package CFPlus::UI::Toplevel; 1354package DC::UI::Toplevel;
1326 1355
1327our @ISA = CFPlus::UI::Bin::; 1356our @ISA = DC::UI::Bin::;
1328 1357
1329use CFPlus::OpenGL; 1358use DC::OpenGL;
1330 1359
1331my $bg = 1360my $bg =
1332 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1361 new_from_resource DC::Texture "d1_bg.png",
1333 mipmap => 1, wrap => 1; 1362 mipmap => 1, wrap => 1;
1334 1363
1335my @border = 1364my @border =
1336 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1365 map { new_from_resource DC::Texture $_, mipmap => 1 }
1337 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1366 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1338 1367
1339my @icon = 1368my @icon =
1340 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1369 map { new_from_resource DC::Texture $_, mipmap => 1 }
1341 qw(x1_move.png x1_resize.png); 1370 qw(x1_move.png x1_resize.png);
1342 1371
1343sub new { 1372sub new {
1344 my ($class, %arg) = @_; 1373 my ($class, %arg) = @_;
1345 1374
1346 my $self = $class->SUPER::new ( 1375 my $self = $class->SUPER::new (
1347 bg => [1, 1, 1, 1], 1376 bg => [1, 1, 1, 1],
1348 border_bg => [1, 1, 1, 1], 1377 border_bg => [1, 1, 1, 1],
1349 border => 0.6, 1378 border => 0.8,
1350 can_events => 1, 1379 can_events => 1,
1351 min_w => 64, 1380 min_w => 64,
1352 min_h => 32, 1381 min_h => 32,
1353 %arg, 1382 %arg,
1354 ); 1383 );
1355 1384
1356 $self->{title_widget} = new CFPlus::UI::Label 1385 $self->{title_widget} = new DC::UI::Label
1357 align => 0, 1386 align => 0.5,
1358 valign => 1, 1387 valign => 1,
1359 text => $self->{title}, 1388 text => $self->{title},
1360 fontsize => $self->{border}, 1389 fontsize => $self->{border},
1361 if exists $self->{title}; 1390 if exists $self->{title};
1362 1391
1363 if ($self->{has_close_button}) { 1392 if ($self->{has_close_button}) {
1364 $self->{close_button} = 1393 $self->{close_button} =
1365 new CFPlus::UI::ImageButton 1394 new DC::UI::ImageButton
1366 path => 'x1_close.png', 1395 path => 'x1_close.png',
1367 on_activate => sub { $self->emit ("delete") }; 1396 on_activate => sub { $self->emit ("delete") };
1368 1397
1369 $self->CFPlus::UI::Container::add ($self->{close_button}); 1398 $self->DC::UI::Container::add ($self->{close_button});
1370 } 1399 }
1371 1400
1372 $self 1401 $self
1373} 1402}
1374 1403
1375sub add { 1404sub add {
1376 my ($self, @widgets) = @_; 1405 my ($self, @widgets) = @_;
1377 1406
1378 $self->SUPER::add (@widgets); 1407 $self->SUPER::add (@widgets);
1379 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1408 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1380 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1409 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1381} 1410}
1382 1411
1383sub border { 1412sub border {
1384 int $_[0]{border} * $::FONTSIZE 1413 int $_[0]{border} * $::FONTSIZE
1414}
1415
1416sub get_max_wh {
1417 my ($self) = @_;
1418
1419 return ($self->{w}, $self->{h})
1420 if $self->{visible} && $self->{w};
1421
1422 $self->SUPER::get_max_wh
1385} 1423}
1386 1424
1387sub size_request { 1425sub size_request {
1388 my ($self) = @_; 1426 my ($self) = @_;
1389 1427
1514 glEnable GL_TEXTURE_2D; 1552 glEnable GL_TEXTURE_2D;
1515 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1516 1554
1517 my $border = $self->border; 1555 my $border = $self->border;
1518 1556
1557 if ($border) {
1519 glColor @{ $self->{border_bg} }; 1558 glColor @{ $self->{border_bg} };
1520 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1559 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1521 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1560 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1522 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1561 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1523 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1562 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1524 1563
1525 # move 1564 # move
1526 my $w2 = ($w - $border) * .5; 1565 my $w2 = ($w - $border) * .5;
1527 my $h2 = ($h - $border) * .5; 1566 my $h2 = ($h - $border) * .5;
1528 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1529 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1530 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1569 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1531 1570
1532 # resize 1571 # resize
1533 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1572 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1534 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1573 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1535 unless $self->{has_close_button}; 1574 unless $self->{has_close_button};
1536 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1537 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1576 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1577 }
1538 1578
1539 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1579 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1540 glColor @{ $self->{bg} }; 1580 glColor @{ $self->{bg} };
1541 1581
1542 # TODO: repeat texture not scale 1582 # TODO: repeat texture not scale
1561 if $self->{close_button}; 1601 if $self->{close_button};
1562} 1602}
1563 1603
1564############################################################################# 1604#############################################################################
1565 1605
1566package CFPlus::UI::Table; 1606package DC::UI::Table;
1567 1607
1568our @ISA = CFPlus::UI::Container::; 1608our @ISA = DC::UI::Container::;
1569 1609
1570use List::Util qw(max sum); 1610use List::Util qw(max sum);
1571 1611
1572use CFPlus::OpenGL; 1612use DC::OpenGL;
1573 1613
1574sub new { 1614sub new {
1575 my $class = shift; 1615 my $class = shift;
1576 1616
1577 $class->SUPER::new ( 1617 $class->SUPER::new (
1621 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1661 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1622 1662
1623 my $sw = sum @w[$c .. $c + $cs - 1]; 1663 my $sw = sum @w[$c .. $c + $cs - 1];
1624 1664
1625 if ($w > $sw) { 1665 if ($w > $sw) {
1626 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1666 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1627 } 1667 }
1628 } 1668 }
1629 1669
1630 # second pass, rows 1670 # second pass, rows
1631 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1671 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1632 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1672 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1633 1673
1634 my $sh = sum @h[$r .. $r + $rs - 1]; 1674 my $sh = sum @h[$r .. $r + $rs - 1];
1635 1675
1636 if ($h > $sh) { 1676 if ($h > $sh) {
1637 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1677 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1638 } 1678 }
1639 } 1679 }
1640 1680
1641 (\@w, \@h) 1681 (\@w, \@h)
1642} 1682}
1665 @col_expand = (1) x @$ws unless @col_expand; 1705 @col_expand = (1) x @$ws unless @col_expand;
1666 my $col_expand = (sum @col_expand) || 1; 1706 my $col_expand = (sum @col_expand) || 1;
1667 1707
1668 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1708 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1669 1709
1670 CFPlus::UI::harmonize $ws; 1710 DC::UI::harmonize $ws;
1671 1711
1672 my @row_expand = @{$self->{row_expand}}; 1712 my @row_expand = @{$self->{row_expand}};
1673 @row_expand = (1) x @$ws unless @row_expand; 1713 @row_expand = (1) x @$ws unless @row_expand;
1674 my $row_expand = (sum @row_expand) || 1; 1714 my $row_expand = (sum @row_expand) || 1;
1675 1715
1676 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1716 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1677 1717
1678 CFPlus::UI::harmonize $hs; 1718 DC::UI::harmonize $hs;
1679 1719
1680 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1720 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1681 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1721 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1682 1722
1683 for my $widget ($self->children) { 1723 for my $widget ($self->children) {
1692 1 1732 1
1693} 1733}
1694 1734
1695############################################################################# 1735#############################################################################
1696 1736
1697package CFPlus::UI::Fixed; 1737package DC::UI::Fixed;
1698 1738
1699use List::Util qw(min max); 1739use List::Util qw(min max);
1700 1740
1701our @ISA = CFPlus::UI::Container::; 1741our @ISA = DC::UI::Container::;
1702 1742
1703sub _scale($$$) { 1743sub _scale($$$) {
1704 my ($rel, $val, $max) = @_; 1744 my ($rel, $val, $max) = @_;
1705 1745
1706 $rel ? $val * $max : $val 1746 $rel ? $val * $max : $val
1758 1 1798 1
1759} 1799}
1760 1800
1761############################################################################# 1801#############################################################################
1762 1802
1763package CFPlus::UI::Box; 1803package DC::UI::Box;
1764 1804
1765our @ISA = CFPlus::UI::Container::; 1805our @ISA = DC::UI::Container::;
1766 1806
1767sub size_request { 1807sub size_request {
1768 my ($self) = @_; 1808 my ($self) = @_;
1769 1809
1770 my @children = $self->visible_children; 1810 my @children = $self->visible_children;
1805 $req[$_] += $space * $children[$_]{expand} 1845 $req[$_] += $space * $children[$_]{expand}
1806 for 0 .. $#children; 1846 for 0 .. $#children;
1807 } 1847 }
1808 } 1848 }
1809 1849
1810 CFPlus::UI::harmonize \@req; 1850 DC::UI::harmonize \@req;
1811 1851
1812 my $pos = 0; 1852 my $pos = 0;
1813 for (0 .. $#children) { 1853 for (0 .. $#children) {
1814 my $alloc = $req[$_]; 1854 my $alloc = $req[$_];
1815 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1855 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1820 1 1860 1
1821} 1861}
1822 1862
1823############################################################################# 1863#############################################################################
1824 1864
1825package CFPlus::UI::HBox; 1865package DC::UI::HBox;
1826 1866
1827our @ISA = CFPlus::UI::Box::; 1867our @ISA = DC::UI::Box::;
1828 1868
1829sub new { 1869sub new {
1830 my $class = shift; 1870 my $class = shift;
1831 1871
1832 $class->SUPER::new ( 1872 $class->SUPER::new (
1835 ) 1875 )
1836} 1876}
1837 1877
1838############################################################################# 1878#############################################################################
1839 1879
1840package CFPlus::UI::VBox; 1880package DC::UI::VBox;
1841 1881
1842our @ISA = CFPlus::UI::Box::; 1882our @ISA = DC::UI::Box::;
1843 1883
1844sub new { 1884sub new {
1845 my $class = shift; 1885 my $class = shift;
1846 1886
1847 $class->SUPER::new ( 1887 $class->SUPER::new (
1850 ) 1890 )
1851} 1891}
1852 1892
1853############################################################################# 1893#############################################################################
1854 1894
1855package CFPlus::UI::Label; 1895package DC::UI::Label;
1856 1896
1857our @ISA = CFPlus::UI::DrawBG::; 1897our @ISA = DC::UI::DrawBG::;
1858 1898
1859use CFPlus::OpenGL; 1899use DC::OpenGL;
1860 1900
1861sub new { 1901sub new {
1862 my ($class, %arg) = @_; 1902 my ($class, %arg) = @_;
1863 1903
1864 my $self = $class->SUPER::new ( 1904 my $self = $class->SUPER::new (
1869 #text => initial text 1909 #text => initial text
1870 #markup => initial narkup 1910 #markup => initial narkup
1871 #max_w => maximum pixel width 1911 #max_w => maximum pixel width
1872 #style => 0, # render flags 1912 #style => 0, # render flags
1873 ellipsise => 3, # end 1913 ellipsise => 3, # end
1874 layout => (new CFPlus::Layout), 1914 layout => (new DC::Layout),
1875 fontsize => 1, 1915 fontsize => 1,
1876 align => -1, 1916 align => 0.5,
1877 valign => -1, 1917 valign => 0.5,
1878 padding_x => 2, 1918 padding_x => 4,
1879 padding_y => 2, 1919 padding_y => 2,
1880 can_events => 0, 1920 can_events => 0,
1881 %arg 1921 %arg
1882 ); 1922 );
1883 1923
1884 if (exists $self->{template}) { 1924 if (exists $self->{template}) {
1885 my $layout = new CFPlus::Layout; 1925 my $layout = new DC::Layout;
1886 $layout->set_text (delete $self->{template}); 1926 $layout->set_text (delete $self->{template});
1887 $self->{template} = $layout; 1927 $self->{template} = $layout;
1888 } 1928 }
1889 1929
1890 if (exists $self->{markup}) { 1930 if (exists $self->{markup}) {
1946 1986
1947sub size_request { 1987sub size_request {
1948 my ($self) = @_; 1988 my ($self) = @_;
1949 1989
1950 $self->{size_req} ||= do { 1990 $self->{size_req} ||= do {
1991 my ($max_w, $max_h) = $self->get_max_wh;
1992
1951 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1993 $self->{layout}->set_font ($self->{font}) if $self->{font};
1952 $self->{layout}->set_width ($self->{max_w} || -1); 1994 $self->{layout}->set_width ($max_w);
1953 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1995 $self->{layout}->set_ellipsise ($self->{ellipsise});
1954 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1996 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1955 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1956 1998
1957 my ($w, $h) = $self->{layout}->size; 1999 my ($w, $h) = $self->{layout}->size;
1958 2000
1959 if (exists $self->{template}) { 2001 if (exists $self->{template}) {
1960 $self->{template}->set_font ($self->{font}) if $self->{font}; 2002 $self->{template}->set_font ($self->{font}) if $self->{font};
1961 $self->{template}->set_width ($self->{max_w} || -1); 2003 $self->{template}->set_width ($max_w);
1962 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2004 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1963 2005
1964 my ($w2, $h2) = $self->{template}->size; 2006 my ($w2, $h2) = $self->{template}->size;
1965 2007
1966 $w = List::Util::max $w, $w2; 2008 $w = List::Util::max $w, $w2;
2022 2064
2023 [$self->{layout}->size] 2065 [$self->{layout}->size]
2024 }; 2066 };
2025 2067
2026 unless (exists $self->{ox}) { 2068 unless (exists $self->{ox}) {
2027 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2069 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2028 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2070 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2029 : ($self->{w} - $size->[0]) * 0.5);
2030
2031 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2032 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2033 : ($self->{h} - $size->[1]) * 0.5);
2034 2071
2035 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2036 }; 2073 };
2037 2074
2038# unless ($self->{list}) { 2075# unless ($self->{list}) {
2039# $self->{list} = CFPlus::OpenGL::glGenList; 2076# $self->{list} = DC::OpenGL::glGenList;
2040# CFPlus::OpenGL::glNewList $self->{list}; 2077# DC::OpenGL::glNewList $self->{list};
2041# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2078# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2042# CFPlus::OpenGL::glEndList; 2079# DC::OpenGL::glEndList;
2043# } 2080# }
2044# 2081#
2045# CFPlus::OpenGL::glCallList $self->{list}; 2082# DC::OpenGL::glCallList $self->{list};
2046 2083
2047 $self->{layout}->draw; 2084 $self->{layout}->draw;
2048} 2085}
2049 2086
2050#sub destroy { 2087#sub destroy {
2051# my ($self) = @_; 2088# my ($self) = @_;
2052# 2089#
2053# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2090# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2054# 2091#
2055# $self->SUPER::destroy; 2092# $self->SUPER::destroy;
2056#} 2093#}
2057 2094
2058############################################################################# 2095#############################################################################
2059 2096
2060package CFPlus::UI::EntryBase; 2097package DC::UI::EntryBase;
2061 2098
2062our @ISA = CFPlus::UI::Label::; 2099our @ISA = DC::UI::Label::;
2063 2100
2064use CFPlus::OpenGL; 2101use DC::OpenGL;
2065 2102
2066sub new { 2103sub new {
2067 my $class = shift; 2104 my $class = shift;
2068 2105
2069 $class->SUPER::new ( 2106 $class->SUPER::new (
2070 fg => [1, 1, 1], 2107 fg => [1, 1, 1],
2071 bg => [0, 0, 0, 0.2], 2108 bg => [0, 0, 0, 0.2],
2072 outline => [0.6, 0.3, 0.1], 2109 outline => undef,
2073 active_bg => [0, 0, 1, .2], 2110 active_bg => [0, 0, 1, .2],
2074 active_fg => [1, 1, 1], 2111 active_fg => [1, 1, 1],
2075 active_outline => [1, 1, 0], 2112 active_outline => [1, 1, 0],
2076 can_hover => 1, 2113 can_hover => 1,
2077 can_focus => 1, 2114 can_focus => 1,
2115 align => 0,
2078 valign => 0, 2116 valign => 0.5,
2079 can_events => 1, 2117 can_events => 1,
2080 ellipsise => 0, 2118 ellipsise => 0,
2119 padding_x => 4,
2120 padding_y => 2,
2081 #text => ... 2121 #text => ...
2082 #hidden => "*", 2122 #hidden => "*",
2083 @_ 2123 @_
2084 ) 2124 )
2085} 2125}
2132 2172
2133 my $text = $self->get_text; 2173 my $text = $self->get_text;
2134 2174
2135 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2175 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2136 2176
2137 if ($uni == 8) { 2177 if ($sym == DC::SDLK_BACKSPACE) {
2138 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2178 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2139 } elsif ($uni == 127) { 2179 } elsif ($sym == DC::SDLK_DELETE) {
2140 substr $text, $self->{cursor}, 1, ""; 2180 substr $text, $self->{cursor}, 1, "";
2141 } elsif ($sym == CFPlus::SDLK_LEFT) { 2181 } elsif ($sym == DC::SDLK_LEFT) {
2142 --$self->{cursor} if $self->{cursor}; 2182 --$self->{cursor} if $self->{cursor};
2143 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2183 } elsif ($sym == DC::SDLK_RIGHT) {
2144 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2184 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2145 } elsif ($sym == CFPlus::SDLK_HOME) { 2185 } elsif ($sym == DC::SDLK_HOME) {
2146 # what a hack 2186 # what a hack
2147 $self->{cursor} = 2187 $self->{cursor} =
2148 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2188 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2149 ? length $1 2189 ? length $1
2150 : 0; 2190 : 0;
2151 } elsif ($sym == CFPlus::SDLK_END) { 2191 } elsif ($sym == DC::SDLK_END) {
2152 # uh, again 2192 # uh, again
2153 $self->{cursor} = 2193 $self->{cursor} =
2154 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2194 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2155 ? $self->{cursor} + length $1 2195 ? $self->{cursor} + length $1
2156 : length $self->{text}; 2196 : length $self->{text};
2237 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2277 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2238 } 2278 }
2239 2279
2240 glColor_premultiply @{$self->{active_fg}}; 2280 glColor_premultiply @{$self->{active_fg}};
2241 glBegin GL_LINES; 2281 glBegin GL_LINES;
2242 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2282 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2243 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2283 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2244 glEnd; 2284 glEnd;
2245 2285
2246 glLineWidth 3; 2286 glLineWidth 3;
2247 glColor @{$self->{active_outline}}; 2287 glColor @{$self->{active_outline}};
2248 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2288 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2249 glLineWidth 1; 2289 glLineWidth 1;
2250 2290
2251 } else { 2291 } else {
2252 glColor @{$self->{outline}}; 2292 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2253 glTranslate .375, .375;
2254 glBegin GL_LINE_STRIP; 2293 glBegin GL_LINE_STRIP;
2255 glVertex 0, $self->{h} * .5; 2294 glVertex .5, $self->{h} * .5;
2256 glVertex 0, $self->{h} - 3; 2295 glVertex .5, $self->{h} - 2.5;
2257 glVertex $self->{w} - 1, $self->{h} - 3; 2296 glVertex $self->{w} - .5, $self->{h} - 2.5;
2258 glVertex $self->{w} - 1, $self->{h} * .5; 2297 glVertex $self->{w} - .5, $self->{h} * .5;
2259 glEnd; 2298 glEnd;
2260 } 2299 }
2261} 2300}
2262 2301
2263############################################################################# 2302#############################################################################
2264 2303
2265package CFPlus::UI::Entry; 2304package DC::UI::Entry;
2266 2305
2267our @ISA = CFPlus::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2268 2307
2269use CFPlus::OpenGL; 2308use DC::OpenGL;
2309
2310sub new {
2311 my $class = shift;
2312
2313 $class->SUPER::new (
2314 history_pointer => -1,
2315 @_
2316 )
2317}
2318
2270 2319
2271sub invoke_key_down { 2320sub invoke_key_down {
2272 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2273 2322
2274 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2280 $self->{history_pointer} = -1; 2329 $self->{history_pointer} = -1;
2281 $self->{history_saveback} = ''; 2330 $self->{history_saveback} = '';
2282 $self->emit (activate => $txt); 2331 $self->emit (activate => $txt);
2283 $self->update; 2332 $self->update;
2284 2333
2285 } elsif ($sym == CFPlus::SDLK_UP) { 2334 } elsif ($sym == DC::SDLK_UP) {
2286 if ($self->{history_pointer} < 0) { 2335 if ($self->{history_pointer} < 0) {
2287 $self->{history_saveback} = $self->get_text; 2336 $self->{history_saveback} = $self->get_text;
2288 } 2337 }
2289 if (@{$self->{history} || []} > 0) { 2338 if (@{$self->{history} || []} > 0) {
2290 $self->{history_pointer}++; 2339 $self->{history_pointer}++;
2292 $self->{history_pointer} = @{$self->{history} || []} - 1; 2341 $self->{history_pointer} = @{$self->{history} || []} - 1;
2293 } 2342 }
2294 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2343 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2295 } 2344 }
2296 2345
2297 } elsif ($sym == CFPlus::SDLK_DOWN) { 2346 } elsif ($sym == DC::SDLK_DOWN) {
2298 $self->{history_pointer}--; 2347 $self->{history_pointer}--;
2299 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2300 2349
2301 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2302 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2303 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2304 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2305 } 2357 }
2306 2358
2307 } else { 2359 } else {
2308 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2309 } 2361 }
2311 1 2363 1
2312} 2364}
2313 2365
2314############################################################################# 2366#############################################################################
2315 2367
2316package CFPlus::UI::TextEdit; 2368package DC::UI::TextEdit;
2317 2369
2318our @ISA = CFPlus::UI::EntryBase::; 2370our @ISA = DC::UI::EntryBase::;
2319 2371
2320use CFPlus::OpenGL; 2372use DC::OpenGL;
2373
2374sub new {
2375 my $class = shift;
2376
2377 $class->SUPER::new (
2378 padding_y => 4,
2379
2380 @_
2381 )
2382}
2321 2383
2322sub move_cursor_ver { 2384sub move_cursor_ver {
2323 my ($self, $dy) = @_; 2385 my ($self, $dy) = @_;
2324 2386
2325 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2387 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2326 2388
2327 $y += $dy; 2389 $line += $dy;
2328 2390
2329 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2391 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2330 $self->{cursor} = $index; 2392 $self->{cursor} = $index;
2331 delete $self->{cur_h}; 2393 delete $self->{cur_h};
2332 $self->update; 2394 $self->update;
2333 return; 2395 return;
2334 } 2396 }
2337sub invoke_key_down { 2399sub invoke_key_down {
2338 my ($self, $ev) = @_; 2400 my ($self, $ev) = @_;
2339 2401
2340 my $sym = $ev->{sym}; 2402 my $sym = $ev->{sym};
2341 2403
2342 if ($sym == CFPlus::SDLK_UP) { 2404 if ($sym == DC::SDLK_UP) {
2343 $self->move_cursor_ver (-1); 2405 $self->move_cursor_ver (-1);
2344 } elsif ($sym == CFPlus::SDLK_DOWN) { 2406 } elsif ($sym == DC::SDLK_DOWN) {
2345 $self->move_cursor_ver (+1); 2407 $self->move_cursor_ver (+1);
2346 } else { 2408 } else {
2347 return $self->SUPER::invoke_key_down ($ev) 2409 return $self->SUPER::invoke_key_down ($ev)
2348 } 2410 }
2349 2411
2350 1 2412 1
2351} 2413}
2352 2414
2353############################################################################# 2415#############################################################################
2354 2416
2355package CFPlus::UI::ButtonBin; 2417package DC::UI::ButtonBin;
2356 2418
2357our @ISA = CFPlus::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2358 2420
2359use CFPlus::OpenGL; 2421use DC::OpenGL;
2360 2422
2361my @tex = 2423my @tex =
2362 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2363 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2364 2426
2365sub new { 2427sub new {
2366 my $class = shift; 2428 my $class = shift;
2367 2429
2368 $class->SUPER::new ( 2430 $class->SUPER::new (
2369 can_hover => 1, 2431 can_hover => 1,
2370 align => 0, 2432 align => 0.5,
2371 valign => 0, 2433 valign => 0.5,
2372 can_events => 1, 2434 can_events => 1,
2373 @_ 2435 @_
2374 ) 2436 )
2375} 2437}
2376 2438
2399 $self->SUPER::_draw; 2461 $self->SUPER::_draw;
2400} 2462}
2401 2463
2402############################################################################# 2464#############################################################################
2403 2465
2404package CFPlus::UI::Button; 2466package DC::UI::Button;
2405 2467
2406our @ISA = CFPlus::UI::Label::; 2468our @ISA = DC::UI::Label::;
2407 2469
2408use CFPlus::OpenGL; 2470use DC::OpenGL;
2409 2471
2410my @tex = 2472my @tex =
2411 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2412 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2413 2475
2414sub new { 2476sub new {
2415 my $class = shift; 2477 my $class = shift;
2416 2478
2417 $class->SUPER::new ( 2479 $class->SUPER::new (
2418 padding_x => 4, 2480 padding_x => 8,
2419 padding_y => 4, 2481 padding_y => 4,
2420 fg => [1.0, 1.0, 1.0], 2482 fg => [1.0, 1.0, 1.0],
2421 active_fg => [0.8, 0.8, 0.8], 2483 active_fg => [0.8, 0.8, 0.8],
2422 can_hover => 1, 2484 can_hover => 1,
2423 align => 0, 2485 align => 0.5,
2424 valign => 0, 2486 valign => 0.5,
2425 can_events => 1, 2487 can_events => 1,
2426 @_ 2488 @_
2427 ) 2489 )
2428} 2490}
2429 2491
2454 $self->SUPER::_draw; 2516 $self->SUPER::_draw;
2455} 2517}
2456 2518
2457############################################################################# 2519#############################################################################
2458 2520
2459package CFPlus::UI::CheckBox; 2521package DC::UI::CheckBox;
2460 2522
2461our @ISA = CFPlus::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2462 2524
2463my @tex = 2525my @tex =
2464 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2465 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2466 2528
2467use CFPlus::OpenGL; 2529use DC::OpenGL;
2468 2530
2469sub new { 2531sub new {
2470 my $class = shift; 2532 my $class = shift;
2471 2533
2472 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2473 padding_x => 2, 2536 padding_x => 2,
2474 padding_y => 2, 2537 padding_y => 2,
2475 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2476 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2477 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2483} 2546}
2484 2547
2485sub size_request { 2548sub size_request {
2486 my ($self) = @_; 2549 my ($self) = @_;
2487 2550
2488 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2489} 2552}
2490 2553
2491sub toggle { 2554sub toggle {
2492 my ($self) = @_; 2555 my ($self) = @_;
2493 2556
2512sub _draw { 2575sub _draw {
2513 my ($self) = @_; 2576 my ($self) = @_;
2514 2577
2515 $self->SUPER::_draw; 2578 $self->SUPER::_draw;
2516 2579
2517 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2580 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2518 2581
2519 my ($w, $h) = @$self{qw(w h)}; 2582 my ($w, $h) = @$self{qw(w h)};
2520 2583
2521 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2584 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2522 2585
2529 glDisable GL_TEXTURE_2D; 2592 glDisable GL_TEXTURE_2D;
2530} 2593}
2531 2594
2532############################################################################# 2595#############################################################################
2533 2596
2534package CFPlus::UI::Image; 2597package DC::UI::Image;
2535 2598
2536our @ISA = CFPlus::UI::Base::; 2599our @ISA = DC::UI::Base::;
2537 2600
2538use CFPlus::OpenGL; 2601use DC::OpenGL;
2539 2602
2540our %texture_cache; 2603our %texture_cache;
2541 2604
2542sub new { 2605sub new {
2543 my $class = shift; 2606 my $class = shift;
2550 2613
2551 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2552 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2553 2616
2554 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2555 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2556 2619
2557 CFPlus::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2558 2621
2559 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2560 2623
2561 $self 2624 $self
2562} 2625}
2563 2626
2564sub STORABLE_freeze { 2627sub STORABLE_freeze {
2565 my ($self, $cloning) = @_; 2628 my ($self, $cloning) = @_;
2566 2629
2567 $self->{path} 2630 $self->{path}
2568 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2631 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2569 2632
2570 $self->{path} 2633 $self->{path}
2571} 2634}
2572 2635
2573sub STORABLE_attach { 2636sub STORABLE_attach {
2604 glDisable GL_TEXTURE_2D; 2667 glDisable GL_TEXTURE_2D;
2605} 2668}
2606 2669
2607############################################################################# 2670#############################################################################
2608 2671
2609package CFPlus::UI::ImageButton; 2672package DC::UI::ImageButton;
2610 2673
2611our @ISA = CFPlus::UI::Image::; 2674our @ISA = DC::UI::Image::;
2612 2675
2613use CFPlus::OpenGL; 2676use DC::OpenGL;
2614 2677
2615my %textures; 2678my %textures;
2616 2679
2617sub new { 2680sub new {
2618 my $class = shift; 2681 my $class = shift;
2621 padding_x => 4, 2684 padding_x => 4,
2622 padding_y => 4, 2685 padding_y => 4,
2623 fg => [1, 1, 1], 2686 fg => [1, 1, 1],
2624 active_fg => [0, 0, 1], 2687 active_fg => [0, 0, 1],
2625 can_hover => 1, 2688 can_hover => 1,
2626 align => 0, 2689 align => 0.5,
2627 valign => 0, 2690 valign => 0.5,
2628 can_events => 1, 2691 can_events => 1,
2629 @_ 2692 @_
2630 ); 2693 );
2631} 2694}
2632 2695
2646 1 2709 1
2647} 2710}
2648 2711
2649############################################################################# 2712#############################################################################
2650 2713
2651package CFPlus::UI::VGauge; 2714package DC::UI::VGauge;
2652 2715
2653our @ISA = CFPlus::UI::Base::; 2716our @ISA = DC::UI::Base::;
2654 2717
2655use List::Util qw(min max); 2718use List::Util qw(min max);
2656 2719
2657use CFPlus::OpenGL; 2720use DC::OpenGL;
2658 2721
2659my %tex = ( 2722my %tex = (
2660 food => [ 2723 food => [
2661 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2662 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2725 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2663 ], 2726 ],
2664 grace => [ 2727 grace => [
2665 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2728 map { new_from_resource DC::Texture $_, mipmap => 1 }
2666 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2729 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2667 ], 2730 ],
2668 hp => [ 2731 hp => [
2669 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2670 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2733 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2671 ], 2734 ],
2672 mana => [ 2735 mana => [
2673 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2736 map { new_from_resource DC::Texture $_, mipmap => 1 }
2674 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2737 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2675 ], 2738 ],
2676); 2739);
2677 2740
2678# eg. VGauge->new (gauge => 'food'), default gauge: food 2741# eg. VGauge->new (gauge => 'food'), default gauge: food
2781 glDisable GL_TEXTURE_2D; 2844 glDisable GL_TEXTURE_2D;
2782} 2845}
2783 2846
2784############################################################################# 2847#############################################################################
2785 2848
2786package CFPlus::UI::Progress; 2849package DC::UI::Progress;
2787 2850
2788our @ISA = CFPlus::UI::Label::; 2851our @ISA = DC::UI::Label::;
2789 2852
2790use CFPlus::OpenGL; 2853use DC::OpenGL;
2791 2854
2792sub new { 2855sub new {
2793 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2794 2857
2795 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2796 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2797 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2798 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2799 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2800 fontsize => 0.9, 2865 fontsize => 0.9,
2801 valign => 0, 2866 valign => 0.5,
2802 align => 0, 2867 align => 0.5,
2803 can_events => 1, 2868 can_events => 1,
2804 ellipsise => 1, 2869 ellipsise => 1,
2805 label => "%d%%", 2870 label => "%d%%",
2806 %arg, 2871 %arg,
2807 ); 2872 );
2815 my ($self, $label) = @_; 2880 my ($self, $label) = @_;
2816 2881
2817 return if $self->{label} eq $label; 2882 return if $self->{label} eq $label;
2818 $self->{label} = $label; 2883 $self->{label} = $label;
2819 2884
2820 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2885 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2821} 2886}
2822 2887
2823sub set_value { 2888sub set_value {
2824 my ($self, $value) = @_; 2889 my ($self, $value) = @_;
2825 2890
2840 my ($self) = @_; 2905 my ($self) = @_;
2841 2906
2842 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2843 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2844 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2845 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2846 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2847 2915
2848 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2849 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2850 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2851 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2852 } 2920 }
2853 2921
2854 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2855 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2856 2928
2857 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2858 2930
2859 { 2931 {
2860 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2862 } 2934 }
2863} 2935}
2864 2936
2865############################################################################# 2937#############################################################################
2866 2938
2867package CFPlus::UI::ExperienceProgress; 2939package DC::UI::ExperienceProgress;
2868 2940
2869our @ISA = CFPlus::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2870 2942
2871sub new { 2943sub new {
2872 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2873 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2874 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2875 tooltip => sub { 2950 tooltip => sub {
2876 my ($self) = @_; 2951 my ($self) = @_;
2877 2952
2878 sprintf "%s points experience (level %d).\n%s points to next level %s.", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2879 ::formsep $self->{exp}, 2954 $tt,
2880 $self->{lvl}, 2955 $self->{lvl},
2956 ::formsep ($self->{exp}),
2881 ::formsep $self->{nxt} 2957 ::formsep ($self->{nxt}),
2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2882 }, 2960 },
2883 %arg
2884 ); 2961 );
2885 2962
2886 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2887 if $::CONN; 2964 if $::CONN;
2888 2965
2896 if $::CONN; 2973 if $::CONN;
2897 2974
2898 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2899} 2976}
2900 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2901sub set_value { 2992sub set_value {
2902 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2903 2994
2904 $self->{lvl} = $exp; 2995 $self->{lvl} = $lvl;
2905 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2906 2997
2907 my $v = -1;
2908
2909 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2910 my $l0 = $table->[$lvl - 1];
2911 my $l1 = $table->[$lvl];
2912
2913 $self->{nxt} = $l1;
2914
2915 $v = ($exp - $l0) / ($l1 - $l0);
2916 }
2917
2918 $self->SUPER::set_value ($v); 2998 $self->SUPER::set_value ($self->_percent);
2919} 2999}
2920 3000
2921############################################################################# 3001#############################################################################
2922 3002
2923package CFPlus::UI::Gauge; 3003package DC::UI::Gauge;
2924 3004
2925our @ISA = CFPlus::UI::VBox::; 3005our @ISA = DC::UI::VBox::;
2926 3006
2927sub new { 3007sub new {
2928 my ($class, %arg) = @_; 3008 my ($class, %arg) = @_;
2929 3009
2930 my $self = $class->SUPER::new ( 3010 my $self = $class->SUPER::new (
2932 can_hover => 1, 3012 can_hover => 1,
2933 can_events => 1, 3013 can_events => 1,
2934 %arg, 3014 %arg,
2935 ); 3015 );
2936 3016
2937 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3017 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2938 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3018 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2939 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3019 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2940 3020
2941 $self 3021 $self
2942} 3022}
2943 3023
2944sub set_fontsize { 3024sub set_fontsize {
2965 $self->{value}->set_text ($val); 3045 $self->{value}->set_text ($val);
2966} 3046}
2967 3047
2968############################################################################# 3048#############################################################################
2969 3049
2970package CFPlus::UI::Slider; 3050package DC::UI::Slider;
2971 3051
2972use strict; 3052use strict;
2973 3053
2974use CFPlus::OpenGL; 3054use DC::OpenGL;
2975 3055
2976our @ISA = CFPlus::UI::DrawBG::; 3056our @ISA = DC::UI::DrawBG::;
2977 3057
2978my @tex = 3058my @tex =
2979 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3059 map { new_from_resource DC::Texture $_ }
2980 qw(s1_slider.png s1_slider_bg.png); 3060 qw(s1_slider.png s1_slider_bg.png);
2981 3061
2982sub new { 3062sub new {
2983 my $class = shift; 3063 my $class = shift;
2984 3064
3021sub set_value { 3101sub set_value {
3022 my ($self, $value) = @_; 3102 my ($self, $value) = @_;
3023 3103
3024 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3104 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3025 3105
3026 $hi = $lo + 1 if $hi <= $lo; 3106 $hi = $lo if $hi < $lo;
3027 3107
3028 $page = $hi - $lo if $page > $hi - $lo; 3108 $value = $hi - $page if $value > $hi - $page;
3029
3030 $value = $lo if $value < $lo; 3109 $value = $lo if $value < $lo;
3031 $value = $hi - $page if $value > $hi - $page;
3032 3110
3033 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3111 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3034 if $unit; 3112 if $unit;
3035 3113
3036 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3114 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3080sub invoke_mouse_wheel { 3158sub invoke_mouse_wheel {
3081 my ($self, $ev) = @_; 3159 my ($self, $ev) = @_;
3082 3160
3083 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3161 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3084 3162
3085 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3163 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3086 3164
3087 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3165 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3088 3166
3089 1 3167 1
3090} 3168}
3100 my ($self) = @_; 3178 my ($self) = @_;
3101 3179
3102 unless ($self->{knob_w}) { 3180 unless ($self->{knob_w}) {
3103 $self->set_value ($self->{range}[0]); 3181 $self->set_value ($self->{range}[0]);
3104 3182
3105 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3183 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3106 my $range = ($hi - $page - $lo) || 1e-100; 3184 my $range = ($hi - $page - $lo) || 1e-10;
3107 3185
3108 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3186 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3109 3187
3110 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3188 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3111 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3189 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3112 3190
3113 $value = ($value - $lo) / $range; 3191 $value = ($value - $lo) / $range;
3143 glDisable GL_TEXTURE_2D; 3221 glDisable GL_TEXTURE_2D;
3144} 3222}
3145 3223
3146############################################################################# 3224#############################################################################
3147 3225
3148package CFPlus::UI::ValSlider; 3226package DC::UI::ValSlider;
3149 3227
3150our @ISA = CFPlus::UI::HBox::; 3228our @ISA = DC::UI::HBox::;
3151 3229
3152sub new { 3230sub new {
3153 my ($class, %arg) = @_; 3231 my ($class, %arg) = @_;
3154 3232
3155 my $range = delete $arg{range}; 3233 my $range = delete $arg{range};
3156 3234
3157 my $self = $class->SUPER::new ( 3235 my $self = $class->SUPER::new (
3158 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3236 slider => (new DC::UI::Slider expand => 1, range => $range),
3159 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3237 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3160 to_value => sub { shift }, 3238 to_value => sub { shift },
3161 from_value => sub { shift }, 3239 from_value => sub { shift },
3162 %arg, 3240 %arg,
3163 ); 3241 );
3164 3242
3184sub set_range { shift->{slider}->set_range (@_) } 3262sub set_range { shift->{slider}->set_range (@_) }
3185sub set_value { shift->{slider}->set_value (@_) } 3263sub set_value { shift->{slider}->set_value (@_) }
3186 3264
3187############################################################################# 3265#############################################################################
3188 3266
3189package CFPlus::UI::TextScroller; 3267package DC::UI::TextScroller;
3190 3268
3191our @ISA = CFPlus::UI::HBox::; 3269our @ISA = DC::UI::HBox::;
3192 3270
3193use CFPlus::OpenGL; 3271use DC::OpenGL;
3194 3272
3195sub new { 3273sub new {
3196 my $class = shift; 3274 my $class = shift;
3197 3275
3198 my $self = $class->SUPER::new ( 3276 my $self = $class->SUPER::new (
3200 can_events => 1, 3278 can_events => 1,
3201 indent => 0, 3279 indent => 0,
3202 #font => default_font 3280 #font => default_font
3203 @_, 3281 @_,
3204 3282
3205 layout => (new CFPlus::Layout), 3283 layout => (new DC::Layout),
3206 par => [], 3284 par => [],
3207 max_par => 0, 3285 max_par => 0,
3208 height => 0, 3286 height => 0,
3209 children => [ 3287 children => [
3210 (new CFPlus::UI::Empty expand => 1), 3288 (new DC::UI::Empty expand => 1),
3211 (new CFPlus::UI::Slider vertical => 1), 3289 (new DC::UI::Slider vertical => 1),
3212 ], 3290 ],
3213 ); 3291 );
3214 3292
3215 $self->{children}[1]->connect (changed => sub { $self->update }); 3293 $self->{children}[1]->connect (changed => sub { $self->update });
3216 3294
3272 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3350 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3273 $layout->set_markup ($para->{markup}); 3351 $layout->set_markup ($para->{markup});
3274 3352
3275 $layout->set_shapes ( 3353 $layout->set_shapes (
3276 map 3354 map
3277 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3355 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3278 @{$para->{widget}} 3356 @{$para->{widget}}
3279 ); 3357 );
3280 3358
3281 $layout 3359 $layout
3282} 3360}
3399 $ROOT->on_post_alloc ($self => sub { 3477 $ROOT->on_post_alloc ($self => sub {
3400 $self->force_uptodate; 3478 $self->force_uptodate;
3401 3479
3402 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3480 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3403 3481
3404 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3482 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3405 glClearColor 0, 0, 0, 0; 3483 glClearColor 0, 0, 0, 0;
3406 glClear GL_COLOR_BUFFER_BIT; 3484 glClear GL_COLOR_BUFFER_BIT;
3407 3485
3408 package CFPlus::UI::Base; 3486 package DC::UI::Base;
3409 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3487 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3410 (0, 0, $self->{w}, $self->{h}); 3488 (0, 0, $self->{w}, $self->{h});
3411 3489
3412 my $top = int $self->{children}[1]{range}[0]; 3490 my $top = int $self->{children}[1]{range}[0];
3413 3491
3472 $self->{children}[1]->draw; 3550 $self->{children}[1]->draw;
3473} 3551}
3474 3552
3475############################################################################# 3553#############################################################################
3476 3554
3477package CFPlus::UI::Animator; 3555package DC::UI::Animator;
3478 3556
3479use CFPlus::OpenGL; 3557use DC::OpenGL;
3480 3558
3481our @ISA = CFPlus::UI::Bin::; 3559our @ISA = DC::UI::Bin::;
3482 3560
3483sub moveto { 3561sub moveto {
3484 my ($self, $x, $y) = @_; 3562 my ($self, $x, $y) = @_;
3485 3563
3486 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3564 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3514 glPopMatrix; 3592 glPopMatrix;
3515} 3593}
3516 3594
3517############################################################################# 3595#############################################################################
3518 3596
3519package CFPlus::UI::Flopper; 3597package DC::UI::Flopper;
3520 3598
3521our @ISA = CFPlus::UI::Button::; 3599our @ISA = DC::UI::Button::;
3522 3600
3523sub new { 3601sub new {
3524 my $class = shift; 3602 my $class = shift;
3525 3603
3526 my $self = $class->SUPER::new ( 3604 my $self = $class->SUPER::new (
3538 $self->{other}->toggle_visibility; 3616 $self->{other}->toggle_visibility;
3539} 3617}
3540 3618
3541############################################################################# 3619#############################################################################
3542 3620
3543package CFPlus::UI::Tooltip; 3621package DC::UI::Tooltip;
3544 3622
3545our @ISA = CFPlus::UI::Bin::; 3623our @ISA = DC::UI::Bin::;
3546 3624
3547use CFPlus::OpenGL; 3625use DC::OpenGL;
3548 3626
3549sub new { 3627sub new {
3550 my $class = shift; 3628 my $class = shift;
3551 3629
3552 $class->SUPER::new ( 3630 $class->SUPER::new (
3556} 3634}
3557 3635
3558sub set_tooltip_from { 3636sub set_tooltip_from {
3559 my ($self, $widget) = @_; 3637 my ($self, $widget) = @_;
3560 3638
3561 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3562 if $widget->{tooltip} =~ /^#(.*)$/;
3563
3564 my $tooltip = $widget->{tooltip}; 3639 my $tip = $widget->{tooltip};
3640 $tip = $tip->($widget) if "CODE" eq ref $tip;
3641
3642 $tip = DC::Pod::section_label tooltip => $1
3643 if $tip =~ /^#(.*)$/;
3565 3644
3566 if ($ENV{CFPLUS_DEBUG} & 2) { 3645 if ($ENV{CFPLUS_DEBUG} & 2) {
3567 $tooltip .= "\n\n" . (ref $widget) . "\n" 3646 $tip .= "\n\n" . (ref $widget) . "\n"
3568 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3647 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3569 . "req $widget->{req_w} $widget->{req_h}\n" 3648 . "req $widget->{req_w} $widget->{req_h}\n"
3570 . "visible $widget->{visible}"; 3649 . "visible $widget->{visible}";
3571 } 3650 }
3572 3651
3573 $tooltip =~ s/^\n+//; 3652 $tip =~ s/^\n+//;
3574 $tooltip =~ s/\n+$//; 3653 $tip =~ s/\n+$//;
3575 3654
3576 $self->add (new CFPlus::UI::Label 3655 $self->add (new DC::UI::Label
3656 fg => $DC::THEME{tooltip_fg},
3577 markup => $tooltip, 3657 markup => $tip,
3578 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3658 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3659 align => 0,
3579 fontsize => 0.8, 3660 fontsize => 0.8,
3580 style => 1, # FLAG_INVERSE 3661 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3581 ellipsise => 0, 3662 ellipsise => 0,
3582 font => ($widget->{tooltip_font} || $::FONT_PROP), 3663 font => ($widget->{tooltip_font} || $::FONT_PROP),
3583 ); 3664 );
3584} 3665}
3585 3666
3620} 3701}
3621 3702
3622sub _draw { 3703sub _draw {
3623 my ($self) = @_; 3704 my ($self) = @_;
3624 3705
3625 glTranslate 0.375, 0.375;
3626
3627 my ($w, $h) = @$self{qw(w h)}; 3706 my ($w, $h) = @$self{qw(w h)};
3628 3707
3629 glColor 1, 0.8, 0.4; 3708 glColor @{ $DC::THEME{tooltip_bg} };
3630 glRect 0, 0, $w, $h; 3709 glRect 0, 0, $w, $h;
3631 3710
3632 glColor 0, 0, 0; 3711 glColor @{ $DC::THEME{tooltip_border} };
3633 glRect_lineloop 0, 0, $w, $h; 3712 glRect_lineloop .5, .5, $w + .5, $h + .5;
3634 3713
3635 glTranslate 2 - 0.375, 2 - 0.375; 3714 glTranslate 2, 2;
3636 3715
3637 $self->SUPER::_draw; 3716 $self->SUPER::_draw;
3638} 3717}
3639 3718
3640############################################################################# 3719#############################################################################
3641 3720
3642package CFPlus::UI::Face; 3721package DC::UI::Face;
3643 3722
3644our @ISA = CFPlus::UI::DrawBG::; 3723our @ISA = DC::UI::DrawBG::;
3645 3724
3646use CFPlus::OpenGL; 3725use DC::OpenGL;
3647 3726
3648sub new { 3727sub new {
3649 my $class = shift; 3728 my $class = shift;
3650 3729
3651 my $self = $class->SUPER::new ( 3730 my $self = $class->SUPER::new (
3654 aspect => 1, 3733 aspect => 1,
3655 can_events => 0, 3734 can_events => 0,
3656 @_, 3735 @_,
3657 ); 3736 );
3658 3737
3659 if ($self->{anim} && $self->{animspeed}) {
3660 CFPlus::weaken (my $widget = $self);
3661
3662 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3663 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3664 $self->{timer} = Event->timer (
3665 parked => 1,
3666 cb => sub {
3667 return unless $::CONN;
3668
3669 my $w = $widget
3670 or return;
3671
3672 ++$w->{frame};
3673 $w->update_face;
3674
3675 # somehow, $widget can go away
3676 $w->update;
3677 $w->update_timer;
3678 },
3679 );
3680
3681 $self->update_face;
3682 $self->update_timer; 3738 $self->update_anim;
3683 }
3684 3739
3685 $self 3740 $self
3686} 3741}
3687 3742
3688sub update_timer { 3743sub update_timer {
3689 my ($self) = @_; 3744 my ($self) = @_;
3690 3745
3691 return unless $self->{timer}; 3746 return unless $self->{timer};
3692 3747
3693 if ($self->{visible}) { 3748 if ($self->{visible}) {
3694 $self->{timer}->at (
3695 $self->{anim_start}
3696 + $self->{animspeed}
3697 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3698 );
3699 $self->{timer}->start; 3749 $self->{timer}->start;
3700 } else { 3750 } else {
3701 $self->{timer}->stop; 3751 $self->{timer}->stop;
3702 } 3752 }
3703} 3753}
3716 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3717 } 3767 }
3718 } 3768 }
3719 } 3769 }
3720 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3721 } 3799 }
3722} 3800}
3723 3801
3724sub size_request { 3802sub size_request {
3725 my ($self) = @_; 3803 my ($self) = @_;
3747 return unless $self->{visible}; 3825 return unless $self->{visible};
3748 3826
3749 $self->SUPER::update; 3827 $self->SUPER::update;
3750} 3828}
3751 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3752sub invoke_visibility_change { 3851sub invoke_visibility_change {
3753 my ($self) = @_; 3852 my ($self) = @_;
3754 3853
3755 $self->update_timer; 3854 $self->update_timer;
3756 3855
3780 $self->SUPER::destroy; 3879 $self->SUPER::destroy;
3781} 3880}
3782 3881
3783############################################################################# 3882#############################################################################
3784 3883
3785package CFPlus::UI::Buttonbar; 3884package DC::UI::Buttonbar;
3786 3885
3787our @ISA = CFPlus::UI::HBox::; 3886our @ISA = DC::UI::HBox::;
3788 3887
3789# TODO: should actually wrap buttons and other goodies. 3888# TODO: should actually wrap buttons and other goodies.
3790 3889
3791############################################################################# 3890#############################################################################
3792 3891
3793package CFPlus::UI::Menu; 3892package DC::UI::Menu;
3794 3893
3795our @ISA = CFPlus::UI::Toplevel::; 3894our @ISA = DC::UI::Toplevel::;
3796 3895
3797use CFPlus::OpenGL; 3896use DC::OpenGL;
3798 3897
3799sub new { 3898sub new {
3800 my $class = shift; 3899 my $class = shift;
3801 3900
3802 my $self = $class->SUPER::new ( 3901 my $self = $class->SUPER::new (
3803 items => [], 3902 items => [],
3804 z => 100, 3903 z => 100,
3805 @_, 3904 @_,
3806 ); 3905 );
3807 3906
3808 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3907 $self->add ($self->{vbox} = new DC::UI::VBox);
3809 3908
3810 for my $item (@{ $self->{items} }) { 3909 for my $item (@{ $self->{items} }) {
3811 my ($widget, $cb, $tooltip) = @$item; 3910 my ($widget, $cb, $tooltip) = @$item;
3812 3911
3813 # handle various types of items, only text for now 3912 # handle various types of items, only text for now
3814 if (!ref $widget) { 3913 if (!ref $widget) {
3815 if ($widget =~ /\t/) { 3914 if ($widget =~ /\t/) {
3816 my ($left, $right) = split /\t/, $widget, 2; 3915 my ($left, $right) = split /\t/, $widget, 2;
3817 3916
3818 $widget = new CFPlus::UI::HBox 3917 $widget = new DC::UI::HBox
3819 can_hover => 1, 3918 can_hover => 1,
3820 can_events => 1, 3919 can_events => 1,
3821 tooltip => $tooltip, 3920 tooltip => $tooltip,
3822 children => [ 3921 children => [
3823 (new CFPlus::UI::Label markup => $left, expand => 1), 3922 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3824 (new CFPlus::UI::Label markup => $right, align => +1), 3923 (new DC::UI::Label markup => $right, align => 1),
3825 ], 3924 ],
3826 ; 3925 ;
3827 3926
3828 } else { 3927 } else {
3829 $widget = new CFPlus::UI::Label 3928 $widget = new DC::UI::Label
3830 can_hover => 1, 3929 can_hover => 1,
3831 can_events => 1, 3930 can_events => 1,
3931 align => 0,
3832 markup => $widget, 3932 markup => $widget,
3833 tooltip => $tooltip; 3933 tooltip => $tooltip;
3834 } 3934 }
3835 } 3935 }
3836 3936
3851 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3852 $GRAB = $self; 3952 $GRAB = $self;
3853 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3854 3954
3855 $self->show; 3955 $self->show;
3856 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3857} 3965}
3858 3966
3859sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3860 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3861 3969
3882 1 3990 1
3883} 3991}
3884 3992
3885############################################################################# 3993#############################################################################
3886 3994
3887package CFPlus::UI::Multiplexer; 3995package DC::UI::Multiplexer;
3888 3996
3889our @ISA = CFPlus::UI::Container::; 3997our @ISA = DC::UI::Container::;
3890 3998
3891sub new { 3999sub new {
3892 my $class = shift; 4000 my $class = shift;
3893 4001
3894 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3895 @_, 4003 @_,
3896 ); 4004 );
3897 4005
3898 $self->{current} = $self->{children}[0] 4006 $self->set_current_page (0);
3899 if @{ $self->{children} };
3900 4007
3901 $self 4008 $self
3902} 4009}
3903 4010
3904sub add { 4011sub add {
3905 my ($self, @widgets) = @_; 4012 my ($self, @widgets) = @_;
3906 4013
3907 $self->SUPER::add (@widgets); 4014 $self->SUPER::add (@widgets);
3908 4015
3909 $self->{current} = $self->{children}[0] 4016 $self->set_current_page (0)
3910 if @{ $self->{children} }; 4017 if @widgets == @{ $self->{children} };
3911} 4018}
3912 4019
3913sub get_current_page { 4020sub get_current_page {
3914 my ($self) = @_; 4021 my ($self) = @_;
3915 4022
3921 4028
3922 my $widget = ref $page_or_widget 4029 my $widget = ref $page_or_widget
3923 ? $page_or_widget 4030 ? $page_or_widget
3924 : $self->{children}[$page_or_widget]; 4031 : $self->{children}[$page_or_widget];
3925 4032
4033 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4034
3926 $self->{current} = $widget; 4035 if (($self->{current} = $widget)) {
4036 $self->{current}->set_visible if $self->{current} && $self->{visible};
3927 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4037 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3928 4038
3929 $self->emit (page_changed => $self->{current}); 4039 $self->emit (page_changed => $self->{current});
4040 }
3930 4041
3931 $self->realloc; 4042 $self->realloc;
3932} 4043}
3933 4044
3934sub visible_children { 4045sub visible_children {
3935 $_[0]{current} 4046 $_[0]{current} || ()
3936} 4047}
3937 4048
3938sub size_request { 4049sub size_request {
3939 my ($self) = @_; 4050 my ($self) = @_;
3940 4051
4052 $self->{current}
3941 $self->{current}->size_request 4053 ? $self->{current}->size_request
4054 : (0, 0)
3942} 4055}
3943 4056
3944sub invoke_size_allocate { 4057sub invoke_size_allocate {
3945 my ($self, $w, $h) = @_; 4058 my ($self, $w, $h) = @_;
3946 4059
3947 $self->{current}->configure (0, 0, $w, $h); 4060 $self->{current}->configure (0, 0, $w, $h)
4061 if $self->{current};
3948 4062
3949 1 4063 1
3950} 4064}
3951 4065
3952sub _draw { 4066sub _draw {
3953 my ($self) = @_; 4067 my ($self) = @_;
3954 4068
3955 $self->{current}->draw; 4069 $self->{current}->draw
4070 if $self->{current};
3956} 4071}
3957 4072
3958############################################################################# 4073#############################################################################
3959 4074
3960package CFPlus::UI::Notebook; 4075package DC::UI::Notebook;
3961 4076
3962use CFPlus::OpenGL; 4077use DC::OpenGL;
3963 4078
3964our @ISA = CFPlus::UI::VBox::; 4079our @ISA = DC::UI::VBox::;
3965 4080
3966sub new { 4081sub new {
3967 my $class = shift; 4082 my $class = shift;
3968 4083
3969 my $self = $class->SUPER::new ( 4084 my $self = $class->SUPER::new (
3970 buttonbar => (new CFPlus::UI::Buttonbar), 4085 buttonbar => (new DC::UI::Buttonbar),
3971 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4086 multiplexer => (new DC::UI::Multiplexer expand => 1),
3972 active_outline => [1, 1, 0], 4087 active_outline => [.7, .7, 0.2],
3973 # filter => # will be put between multiplexer and $self 4088 # filter => # will be put between multiplexer and $self
3974 @_, 4089 @_,
3975 ); 4090 );
3976 4091
3977 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4092 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3985 4100
3986 for my $child (@$widgets) { 4101 for my $child (@$widgets) {
3987 Scalar::Util::weaken $child; 4102 Scalar::Util::weaken $child;
3988 $child->{c_tab_} ||= do { 4103 $child->{c_tab_} ||= do {
3989 my $tab = 4104 my $tab =
3990 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4105 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3991 ? $child->{c_tab} 4106 ? $child->{c_tab}
3992 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4107 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3993 4108
3994 $tab->connect (activate => sub { 4109 $tab->connect (activate => sub {
3995 $wself->set_current_page ($child); 4110 $wself->set_current_page ($child);
3996 }); 4111 });
3997 4112
4029sub pages { 4144sub pages {
4030 my ($self) = @_; 4145 my ($self) = @_;
4031 $self->{multiplexer}->children 4146 $self->{multiplexer}->children
4032} 4147}
4033 4148
4149sub page_index {
4150 my ($self, $widget) = @_;
4151
4152 my $i = 0;
4153 for ($self->pages) {
4154 if ($_ eq $widget) { return $i };
4155 $i++;
4156 }
4157
4158 undef
4159}
4160
4034sub add_tab { 4161sub add_tab {
4035 my ($self, $title, $widget, $tooltip) = @_; 4162 my ($self, $title, $widget, $tooltip) = @_;
4036 4163
4037 $title = [$title, $tooltip] unless ref $title; 4164 $title = [$title, $tooltip] unless ref $title;
4038 $widget->{c_tab} = $title; 4165 $widget->{c_tab} = $title;
4058 4185
4059 $self->SUPER::_draw (); 4186 $self->SUPER::_draw ();
4060 4187
4061 if (my $cur = $self->{multiplexer}{current}) { 4188 if (my $cur = $self->{multiplexer}{current}) {
4062 if ($cur = $cur->{c_tab_}) { 4189 if ($cur = $cur->{c_tab_}) {
4063 glTranslate $cur->{x}, $cur->{y}; 4190 glTranslate $self->{buttonbar}{x} + $cur->{x},
4191 $self->{buttonbar}{y} + $cur->{y};
4064 glLineWidth 3; 4192 glLineWidth 3;
4193 #glEnable GL_BLEND;
4194 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4065 glColor @{$self->{active_outline}}; 4195 glColor @{$self->{active_outline}};
4066 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4196 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4067 glLineWidth 1; 4197 glLineWidth 1;
4198 #glDisable GL_BLEND;
4068 } 4199 }
4069 } 4200 }
4070} 4201}
4071 4202
4072############################################################################# 4203#############################################################################
4073 4204
4074package CFPlus::UI::Selector; 4205package DC::UI::Selector;
4075 4206
4076use utf8; 4207use utf8;
4077 4208
4078our @ISA = CFPlus::UI::Button::; 4209our @ISA = DC::UI::Button::;
4079 4210
4080sub new { 4211sub new {
4081 my $class = shift; 4212 my $class = shift;
4082 4213
4083 my $self = $class->SUPER::new ( 4214 my $self = $class->SUPER::new (
4100 my ($value, $title, $tooltip) = @$_; 4231 my ($value, $title, $tooltip) = @$_;
4101 4232
4102 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4233 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4103 } 4234 }
4104 4235
4105 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4236 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4106} 4237}
4107 4238
4108sub _set_value { 4239sub _set_value {
4109 my ($self, $value) = @_; 4240 my ($self, $value) = @_;
4110 4241
4111 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4242 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4243 $item ||= $self->{options}[0]
4112 or return; 4244 or return;
4113 4245
4114 $self->{value} = $item->[0]; 4246 $self->{value} = $item->[0];
4115 $self->set_markup ("$item->[1] ⇓"); 4247 $self->set_markup ("$item->[1] ⇓");
4116 $self->set_tooltip ($item->[2]); 4248# $self->set_tooltip ($item->[2]);
4117} 4249}
4118 4250
4119sub set_value { 4251sub set_value {
4120 my ($self, $value) = @_; 4252 my ($self, $value) = @_;
4121 4253
4123 4255
4124 $self->_set_value ($value); 4256 $self->_set_value ($value);
4125 $self->emit (changed => $value); 4257 $self->emit (changed => $value);
4126} 4258}
4127 4259
4260sub set_options {
4261 my ($self, $options) = @_;
4262
4263 $self->{options} = $options;
4264 $self->_set_value ($self->{value});
4265}
4266
4128############################################################################# 4267#############################################################################
4129 4268
4130package CFPlus::UI::Statusbox; 4269package DC::UI::Statusbox;
4131 4270
4132our @ISA = CFPlus::UI::VBox::; 4271our @ISA = DC::UI::VBox::;
4133 4272
4134sub new { 4273sub new {
4135 my $class = shift; 4274 my $class = shift;
4136 4275
4137 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4138 fontsize => 0.8, 4277 fontsize => 0.8,
4139 @_, 4278 @_,
4140 ); 4279 );
4141 4280
4142 CFPlus::weaken (my $this = $self); 4281 DC::weaken (my $this = $self);
4143 4282
4144 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4283 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4145 4284
4146 $self 4285 $self
4147} 4286}
4148 4287
4149sub reorder { 4288sub reorder {
4150 my ($self) = @_; 4289 my ($self) = @_;
4151 my $NOW = Time::HiRes::time; 4290 my $NOW = EV::time;
4152 4291
4153 # freeze display when hovering over any label 4292 # freeze display when hovering over any label
4154 return if $CFPlus::UI::TOOLTIP->{owner} 4293 return if $DC::UI::TOOLTIP->{owner}
4155 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4294 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4156 values %{ $self->{item} }; 4295 values %{ $self->{item} };
4157 4296
4158 while (my ($k, $v) = each %{ $self->{item} }) { 4297 while (my ($k, $v) = each %{ $self->{item} }) {
4159 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4298 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4160 } 4299 }
4300
4301 $self->{timer}->set (1, 1);
4161 4302
4162 my @widgets; 4303 my @widgets;
4163 4304
4164 my @items = sort { 4305 my @items = sort {
4165 $a->{pri} <=> $b->{pri} 4306 $a->{pri} <=> $b->{pri}
4166 or $b->{id} <=> $a->{id} 4307 or $b->{id} <=> $a->{id}
4167 } values %{ $self->{item} }; 4308 } values %{ $self->{item} };
4168
4169 $self->{timer}->interval (1);
4170 4309
4171 my $count = 10 + 1; 4310 my $count = 10 + 1;
4172 for my $item (@items) { 4311 for my $item (@items) {
4173 last unless --$count; 4312 last unless --$count;
4174 4313
4181 for ($short) { 4320 for ($short) {
4182 s/^\s+//; 4321 s/^\s+//;
4183 s/\s+/ /g; 4322 s/\s+/ /g;
4184 } 4323 }
4185 4324
4186 new CFPlus::UI::Label 4325 new DC::UI::Label
4187 markup => $short, 4326 markup => $short,
4188 tooltip => $item->{tooltip}, 4327 tooltip => $item->{tooltip},
4189 tooltip_font => $::FONT_PROP, 4328 tooltip_font => $::FONT_PROP,
4190 tooltip_width => 0.67, 4329 tooltip_width => 0.67,
4191 fontsize => $item->{fontsize} || $self->{fontsize}, 4330 fontsize => $item->{fontsize} || $self->{fontsize},
4192 max_w => $::WIDTH * 0.44, 4331 max_w => $::WIDTH * 0.44,
4332 align => 0,
4193 fg => [@{ $item->{fg} }], 4333 fg => [@{ $item->{fg} }],
4194 can_events => 1, 4334 can_events => 1,
4195 can_hover => 1 4335 can_hover => 1
4196 }; 4336 };
4197 4337
4198 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4338 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4199 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4339 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4200 $label->update; 4340 $label->update;
4201 $label->set_max_size (undef, $label->{req_h} * $diff) 4341 $label->set_max_size (undef, $label->{req_h} * $diff)
4202 if $diff < 1; 4342 if $diff < 1;
4203 $self->{timer}->interval (1/30); 4343 $self->{timer}->set (1/30, 1/30);
4204 } else { 4344 } else {
4205 $label->{fg}[3] = $item->{fg}[3] || 1; 4345 $label->{fg}[3] = $item->{fg}[3] || 1;
4206 } 4346 }
4207 4347
4208 push @widgets, $label; 4348 push @widgets, $label;
4209 } 4349 }
4350
4351 my $hash = join ",", @widgets;
4352 return if $hash eq $self->{last_widget_hash};
4353 $self->{last_widget_hash} = $hash;
4210 4354
4211 $self->clear; 4355 $self->clear;
4212 $self->SUPER::add (reverse @widgets); 4356 $self->SUPER::add (reverse @widgets);
4213} 4357}
4214 4358
4270 $self->SUPER::destroy; 4414 $self->SUPER::destroy;
4271} 4415}
4272 4416
4273############################################################################# 4417#############################################################################
4274 4418
4275package CFPlus::UI::Root; 4419package DC::UI::Root;
4276 4420
4277our @ISA = CFPlus::UI::Container::; 4421our @ISA = DC::UI::Container::;
4278 4422
4279use List::Util qw(min max); 4423use List::Util qw(min max);
4280 4424
4281use CFPlus::OpenGL; 4425use DC::OpenGL;
4282 4426
4283sub new { 4427sub new {
4284 my $class = shift; 4428 my $class = shift;
4285 4429
4286 my $self = $class->SUPER::new ( 4430 my $self = $class->SUPER::new (
4287 visible => 1, 4431 visible => 1,
4288 @_, 4432 @_,
4289 ); 4433 );
4290 4434
4291 CFPlus::weaken ($self->{root} = $self); 4435 DC::weaken ($self->{root} = $self);
4292 4436
4293 $self 4437 $self
4294} 4438}
4295 4439
4296sub size_request { 4440sub size_request {
4344} 4488}
4345 4489
4346sub update { 4490sub update {
4347 my ($self) = @_; 4491 my ($self) = @_;
4348 4492
4349 $::WANT_REFRESH->start; 4493 $::WANT_REFRESH = 1;
4350} 4494}
4351 4495
4352sub add { 4496sub add {
4353 my ($self, @children) = @_; 4497 my ($self, @children) = @_;
4354 4498
4421 4565
4422 delete $queue{$widget+0}; 4566 delete $queue{$widget+0};
4423 4567
4424 my ($w, $h) = $widget->size_request; 4568 my ($w, $h) = $widget->size_request;
4425 4569
4426 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4570 $w += $widget->{padding_x} * 2;
4427 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4571 $h += $widget->{padding_y} * 2;
4572
4573 $w = max $widget->{min_w}, $w;
4574 $h = max $widget->{min_h}, $h;
4428 4575
4429 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4576 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4430 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4577 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4431 4578
4432 $w = $widget->{force_w} if exists $widget->{force_w}; 4579 $w = $widget->{force_w} if exists $widget->{force_w};
4499 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4646 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4500 glMatrixMode GL_MODELVIEW; 4647 glMatrixMode GL_MODELVIEW;
4501 glLoadIdentity; 4648 glLoadIdentity;
4502 4649
4503 { 4650 {
4504 package CFPlus::UI::Base; 4651 package DC::UI::Base;
4505 4652
4506 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4653 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4507 (0, 0, $self->{w}, $self->{h}); 4654 (0, 0, $self->{w}, $self->{h});
4508 4655
4509 $self->_draw; 4656 $self->_draw;
4510 } 4657 }
4511} 4658}
4512 4659
4513############################################################################# 4660#############################################################################
4514 4661
4515package CFPlus::UI; 4662package DC::UI;
4516 4663
4517$ROOT = new CFPlus::UI::Root; 4664$ROOT = new DC::UI::Root;
4518$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4519 4666
45201 46671
4521

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines