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.479 by root, Thu Feb 4 20:01:09 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines