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.418 by elmex, Tue Jul 24 18:39:39 2007 UTC vs.
Revision 1.473 by root, Sun Jan 11 03:19:47 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines