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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines