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.436 by root, Wed Aug 22 21:40:57 2007 UTC vs.
Revision 1.482 by root, Sat Aug 13 23:18:19 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines