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.422 by root, Thu Aug 2 14:35:21 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 c_col => 0, 1044 c_col => 0,
1033 c_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 c_col => 1, 1055 c_col => 1,
1044 c_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 {
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 c_col => 0, 1077 c_col => 0,
1066 c_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},
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 (
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_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->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1662 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_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) {
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}) {
1961 1977
1962sub size_request { 1978sub size_request {
1963 my ($self) = @_; 1979 my ($self) = @_;
1964 1980
1965 $self->{size_req} ||= do { 1981 $self->{size_req} ||= do {
1982 my ($max_w, $max_h) = $self->get_max_wh;
1983
1966 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1984 $self->{layout}->set_font ($self->{font}) if $self->{font};
1967 $self->{layout}->set_width ($self->{max_w} || -1); 1985 $self->{layout}->set_width ($max_w);
1968 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1986 $self->{layout}->set_ellipsise ($self->{ellipsise});
1969 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1970 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1971 1989
1972 my ($w, $h) = $self->{layout}->size; 1990 my ($w, $h) = $self->{layout}->size;
1973 1991
1974 if (exists $self->{template}) { 1992 if (exists $self->{template}) {
1975 $self->{template}->set_font ($self->{font}) if $self->{font}; 1993 $self->{template}->set_font ($self->{font}) if $self->{font};
1976 $self->{template}->set_width ($self->{max_w} || -1); 1994 $self->{template}->set_width ($max_w);
1977 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1995 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1978 1996
1979 my ($w2, $h2) = $self->{template}->size; 1997 my ($w2, $h2) = $self->{template}->size;
1980 1998
1981 $w = List::Util::max $w, $w2; 1999 $w = List::Util::max $w, $w2;
2037 2055
2038 [$self->{layout}->size] 2056 [$self->{layout}->size]
2039 }; 2057 };
2040 2058
2041 unless (exists $self->{ox}) { 2059 unless (exists $self->{ox}) {
2042 $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);
2043 : $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);
2044 : ($self->{w} - $size->[0]) * 0.5);
2045 2062
2046 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2063 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2047 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2048 : ($self->{h} - $size->[1]) * 0.5);
2049 }; 2064 };
2050 2065
2051# unless ($self->{list}) { 2066# unless ($self->{list}) {
2052# $self->{list} = CFPlus::OpenGL::glGenList; 2067# $self->{list} = DC::OpenGL::glGenList;
2053# CFPlus::OpenGL::glNewList $self->{list}; 2068# DC::OpenGL::glNewList $self->{list};
2054# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2069# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2055# CFPlus::OpenGL::glEndList; 2070# DC::OpenGL::glEndList;
2056# } 2071# }
2057# 2072#
2058# CFPlus::OpenGL::glCallList $self->{list}; 2073# DC::OpenGL::glCallList $self->{list};
2059 2074
2060 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2075 $self->{layout}->draw;
2061} 2076}
2062 2077
2063#sub destroy { 2078#sub destroy {
2064# my ($self) = @_; 2079# my ($self) = @_;
2065# 2080#
2066# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2081# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2067# 2082#
2068# $self->SUPER::destroy; 2083# $self->SUPER::destroy;
2069#} 2084#}
2070 2085
2071############################################################################# 2086#############################################################################
2072 2087
2073package CFPlus::UI::EntryBase; 2088package DC::UI::EntryBase;
2074 2089
2075our @ISA = CFPlus::UI::Label::; 2090our @ISA = DC::UI::Label::;
2076 2091
2077use CFPlus::OpenGL; 2092use DC::OpenGL;
2078 2093
2079sub new { 2094sub new {
2080 my $class = shift; 2095 my $class = shift;
2081 2096
2082 $class->SUPER::new ( 2097 $class->SUPER::new (
2083 fg => [1, 1, 1], 2098 fg => [1, 1, 1],
2084 bg => [0, 0, 0, 0.2], 2099 bg => [0, 0, 0, 0.2],
2085 outline => [0.6, 0.3, 0.1], 2100 outline => undef,
2086 active_bg => [0, 0, 1, .2], 2101 active_bg => [0, 0, 1, .2],
2087 active_fg => [1, 1, 1], 2102 active_fg => [1, 1, 1],
2088 active_outline => [1, 1, 0], 2103 active_outline => [1, 1, 0],
2089 can_hover => 1, 2104 can_hover => 1,
2090 can_focus => 1, 2105 can_focus => 1,
2106 align => 0,
2091 valign => 0, 2107 valign => 0.5,
2092 can_events => 1, 2108 can_events => 1,
2093 ellipsise => 0, 2109 ellipsise => 0,
2110 padding_x => 4,
2111 padding_y => 2,
2094 #text => ... 2112 #text => ...
2095 #hidden => "*", 2113 #hidden => "*",
2096 @_ 2114 @_
2097 ) 2115 )
2098} 2116}
2145 2163
2146 my $text = $self->get_text; 2164 my $text = $self->get_text;
2147 2165
2148 $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;
2149 2167
2150 if ($uni == 8) { 2168 if ($sym == DC::SDLK_BACKSPACE) {
2151 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2169 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2152 } elsif ($uni == 127) { 2170 } elsif ($sym == DC::SDLK_DELETE) {
2153 substr $text, $self->{cursor}, 1, ""; 2171 substr $text, $self->{cursor}, 1, "";
2154 } elsif ($sym == CFPlus::SDLK_LEFT) { 2172 } elsif ($sym == DC::SDLK_LEFT) {
2155 --$self->{cursor} if $self->{cursor}; 2173 --$self->{cursor} if $self->{cursor};
2156 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2174 } elsif ($sym == DC::SDLK_RIGHT) {
2157 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2175 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2158 } elsif ($sym == CFPlus::SDLK_HOME) { 2176 } elsif ($sym == DC::SDLK_HOME) {
2159 # what a hack 2177 # what a hack
2160 $self->{cursor} = 2178 $self->{cursor} =
2161 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2179 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2162 ? length $1 2180 ? length $1
2163 : 0; 2181 : 0;
2164 } elsif ($sym == CFPlus::SDLK_END) { 2182 } elsif ($sym == DC::SDLK_END) {
2165 # uh, again 2183 # uh, again
2166 $self->{cursor} = 2184 $self->{cursor} =
2167 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2185 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2168 ? $self->{cursor} + length $1 2186 ? $self->{cursor} + length $1
2169 : length $self->{text}; 2187 : length $self->{text};
2233 glColor_premultiply @{$self->{bg}}; 2251 glColor_premultiply @{$self->{bg}};
2234 } 2252 }
2235 2253
2236 glEnable GL_BLEND; 2254 glEnable GL_BLEND;
2237 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2255 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2238 glBegin GL_QUADS;
2239 glVertex 0 , 0;
2240 glVertex 0 , $self->{h};
2241 glVertex $self->{w}, $self->{h}; 2256 glRect 0, 0, $self->{w}, $self->{h};
2242 glVertex $self->{w}, 0;
2243 glEnd;
2244 glDisable GL_BLEND; 2257 glDisable GL_BLEND;
2245 2258
2246 $self->SUPER::_draw; 2259 $self->SUPER::_draw;
2247 2260
2248 #TODO: force update every cursor change :( 2261 #TODO: force update every cursor change :(
2255 @$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);
2256 } 2269 }
2257 2270
2258 glColor_premultiply @{$self->{active_fg}}; 2271 glColor_premultiply @{$self->{active_fg}};
2259 glBegin GL_LINES; 2272 glBegin GL_LINES;
2260 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};
2261 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};
2262 glEnd; 2275 glEnd;
2263 2276
2264 glLineWidth 3; 2277 glLineWidth 3;
2265 glColor @{$self->{active_outline}}; 2278 glColor @{$self->{active_outline}};
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}};
2266 glBegin GL_LINE_LOOP; 2284 glBegin GL_LINE_STRIP;
2267 glVertex 0, 0; 2285 glVertex .5, $self->{h} * .5;
2268 glVertex $self->{w} - 1, 0;
2269 glVertex $self->{w} - 1, $self->{h} - 1;
2270 glVertex 0, $self->{h} - 1; 2286 glVertex .5, $self->{h} - 2.5;
2287 glVertex $self->{w} - .5, $self->{h} - 2.5;
2288 glVertex $self->{w} - .5, $self->{h} * .5;
2271 glEnd; 2289 glEnd;
2272 glLineWidth 1;
2273
2274 } else {
2275 glColor @{$self->{outline}};
2276 glTranslate .375, .375;
2277 glBegin GL_LINE_STRIP;
2278 glVertex 0, $self->{h} * .5;
2279 glVertex 0, $self->{h} - 3;
2280 glVertex $self->{w} - 1, $self->{h} - 3;
2281 glVertex $self->{w} - 1, $self->{h} * .5;
2282 glEnd;
2283 } 2290 }
2284} 2291}
2285 2292
2286############################################################################# 2293#############################################################################
2287 2294
2288package CFPlus::UI::Entry; 2295package DC::UI::Entry;
2289 2296
2290our @ISA = CFPlus::UI::EntryBase::; 2297our @ISA = DC::UI::EntryBase::;
2291 2298
2292use 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
2293 2310
2294sub invoke_key_down { 2311sub invoke_key_down {
2295 my ($self, $ev) = @_; 2312 my ($self, $ev) = @_;
2296 2313
2297 my $sym = $ev->{sym}; 2314 my $sym = $ev->{sym};
2303 $self->{history_pointer} = -1; 2320 $self->{history_pointer} = -1;
2304 $self->{history_saveback} = ''; 2321 $self->{history_saveback} = '';
2305 $self->emit (activate => $txt); 2322 $self->emit (activate => $txt);
2306 $self->update; 2323 $self->update;
2307 2324
2308 } elsif ($sym == CFPlus::SDLK_UP) { 2325 } elsif ($sym == DC::SDLK_UP) {
2309 if ($self->{history_pointer} < 0) { 2326 if ($self->{history_pointer} < 0) {
2310 $self->{history_saveback} = $self->get_text; 2327 $self->{history_saveback} = $self->get_text;
2311 } 2328 }
2312 if (@{$self->{history} || []} > 0) { 2329 if (@{$self->{history} || []} > 0) {
2313 $self->{history_pointer}++; 2330 $self->{history_pointer}++;
2315 $self->{history_pointer} = @{$self->{history} || []} - 1; 2332 $self->{history_pointer} = @{$self->{history} || []} - 1;
2316 } 2333 }
2317 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2334 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2318 } 2335 }
2319 2336
2320 } elsif ($sym == CFPlus::SDLK_DOWN) { 2337 } elsif ($sym == DC::SDLK_DOWN) {
2321 $self->{history_pointer}--; 2338 $self->{history_pointer}--;
2322 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2339 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2323 2340
2324 if ($self->{history_pointer} >= 0) { 2341 if ($self->{history_pointer} >= 0) {
2325 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2326 } else { 2343 } else {
2344 if (defined $self->{history_saveback}) {
2327 $self->set_text ($self->{history_saveback}); 2345 $self->set_text ($self->{history_saveback});
2346 $self->{history_saveback} = undef;
2347 }
2328 } 2348 }
2329 2349
2330 } else { 2350 } else {
2331 return $self->SUPER::invoke_key_down ($ev) 2351 return $self->SUPER::invoke_key_down ($ev)
2332 } 2352 }
2334 1 2354 1
2335} 2355}
2336 2356
2337############################################################################# 2357#############################################################################
2338 2358
2339package CFPlus::UI::TextEdit; 2359package DC::UI::TextEdit;
2340 2360
2341our @ISA = CFPlus::UI::EntryBase::; 2361our @ISA = DC::UI::EntryBase::;
2342 2362
2343use 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}
2344 2374
2345sub move_cursor_ver { 2375sub move_cursor_ver {
2346 my ($self, $dy) = @_; 2376 my ($self, $dy) = @_;
2347 2377
2348 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2378 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2349 2379
2350 $y += $dy; 2380 $line += $dy;
2351 2381
2352 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))) {
2353 $self->{cursor} = $index; 2383 $self->{cursor} = $index;
2354 delete $self->{cur_h}; 2384 delete $self->{cur_h};
2355 $self->update; 2385 $self->update;
2356 return; 2386 return;
2357 } 2387 }
2360sub invoke_key_down { 2390sub invoke_key_down {
2361 my ($self, $ev) = @_; 2391 my ($self, $ev) = @_;
2362 2392
2363 my $sym = $ev->{sym}; 2393 my $sym = $ev->{sym};
2364 2394
2365 if ($sym == CFPlus::SDLK_UP) { 2395 if ($sym == DC::SDLK_UP) {
2366 $self->move_cursor_ver (-1); 2396 $self->move_cursor_ver (-1);
2367 } elsif ($sym == CFPlus::SDLK_DOWN) { 2397 } elsif ($sym == DC::SDLK_DOWN) {
2368 $self->move_cursor_ver (+1); 2398 $self->move_cursor_ver (+1);
2369 } else { 2399 } else {
2370 return $self->SUPER::invoke_key_down ($ev) 2400 return $self->SUPER::invoke_key_down ($ev)
2371 } 2401 }
2372 2402
2373 1 2403 1
2374} 2404}
2375 2405
2376############################################################################# 2406#############################################################################
2377 2407
2378package CFPlus::UI::ButtonBin; 2408package DC::UI::ButtonBin;
2379 2409
2380our @ISA = CFPlus::UI::Bin::; 2410our @ISA = DC::UI::Bin::;
2381 2411
2382use CFPlus::OpenGL; 2412use DC::OpenGL;
2383 2413
2384my @tex = 2414my @tex =
2385 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2415 map { new_from_resource DC::Texture $_, mipmap => 1 }
2386 qw(b1_button_inactive.png b1_button_active.png); 2416 qw(b1_button_inactive.png b1_button_active.png);
2387 2417
2388sub new { 2418sub new {
2389 my $class = shift; 2419 my $class = shift;
2390 2420
2391 $class->SUPER::new ( 2421 $class->SUPER::new (
2392 can_hover => 1, 2422 can_hover => 1,
2393 align => 0, 2423 align => 0.5,
2394 valign => 0, 2424 valign => 0.5,
2395 can_events => 1, 2425 can_events => 1,
2396 @_ 2426 @_
2397 ) 2427 )
2398} 2428}
2399 2429
2422 $self->SUPER::_draw; 2452 $self->SUPER::_draw;
2423} 2453}
2424 2454
2425############################################################################# 2455#############################################################################
2426 2456
2427package CFPlus::UI::Button; 2457package DC::UI::Button;
2428 2458
2429our @ISA = CFPlus::UI::Label::; 2459our @ISA = DC::UI::Label::;
2430 2460
2431use CFPlus::OpenGL; 2461use DC::OpenGL;
2432 2462
2433my @tex = 2463my @tex =
2434 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2464 map { new_from_resource DC::Texture $_, mipmap => 1 }
2435 qw(b1_button_inactive.png b1_button_active.png); 2465 qw(b1_button_inactive.png b1_button_active.png);
2436 2466
2437sub new { 2467sub new {
2438 my $class = shift; 2468 my $class = shift;
2439 2469
2440 $class->SUPER::new ( 2470 $class->SUPER::new (
2441 padding_x => 4, 2471 padding_x => 8,
2442 padding_y => 4, 2472 padding_y => 4,
2443 fg => [1.0, 1.0, 1.0], 2473 fg => [1.0, 1.0, 1.0],
2444 active_fg => [0.8, 0.8, 0.8], 2474 active_fg => [0.8, 0.8, 0.8],
2445 can_hover => 1, 2475 can_hover => 1,
2446 align => 0, 2476 align => 0.5,
2447 valign => 0, 2477 valign => 0.5,
2448 can_events => 1, 2478 can_events => 1,
2449 @_ 2479 @_
2450 ) 2480 )
2451} 2481}
2452 2482
2477 $self->SUPER::_draw; 2507 $self->SUPER::_draw;
2478} 2508}
2479 2509
2480############################################################################# 2510#############################################################################
2481 2511
2482package CFPlus::UI::CheckBox; 2512package DC::UI::CheckBox;
2483 2513
2484our @ISA = CFPlus::UI::DrawBG::; 2514our @ISA = DC::UI::DrawBG::;
2485 2515
2486my @tex = 2516my @tex =
2487 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2517 map { new_from_resource DC::Texture $_, mipmap => 1 }
2488 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2518 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2489 2519
2490use CFPlus::OpenGL; 2520use DC::OpenGL;
2491 2521
2492sub new { 2522sub new {
2493 my $class = shift; 2523 my $class = shift;
2494 2524
2495 $class->SUPER::new ( 2525 $class->SUPER::new (
2535sub _draw { 2565sub _draw {
2536 my ($self) = @_; 2566 my ($self) = @_;
2537 2567
2538 $self->SUPER::_draw; 2568 $self->SUPER::_draw;
2539 2569
2540 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2570 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2541 2571
2542 my ($w, $h) = @$self{qw(w h)}; 2572 my ($w, $h) = @$self{qw(w h)};
2543 2573
2544 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;
2545 2575
2552 glDisable GL_TEXTURE_2D; 2582 glDisable GL_TEXTURE_2D;
2553} 2583}
2554 2584
2555############################################################################# 2585#############################################################################
2556 2586
2557package CFPlus::UI::Image; 2587package DC::UI::Image;
2558 2588
2559our @ISA = CFPlus::UI::Base::; 2589our @ISA = DC::UI::Base::;
2560 2590
2561use CFPlus::OpenGL; 2591use DC::OpenGL;
2562 2592
2563our %texture_cache; 2593our %texture_cache;
2564 2594
2565sub new { 2595sub new {
2566 my $class = shift; 2596 my $class = shift;
2573 2603
2574 $self->{path} || $self->{tex} 2604 $self->{path} || $self->{tex}
2575 or Carp::croak "'path' or 'tex' attributes required"; 2605 or Carp::croak "'path' or 'tex' attributes required";
2576 2606
2577 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2607 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2578 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2608 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2579 2609
2580 CFPlus::weaken $texture_cache{$self->{path}}; 2610 DC::weaken $texture_cache{$self->{path}};
2581 2611
2582 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2612 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2583 2613
2584 $self 2614 $self
2585} 2615}
2586 2616
2587sub STORABLE_freeze { 2617sub STORABLE_freeze {
2588 my ($self, $cloning) = @_; 2618 my ($self, $cloning) = @_;
2589 2619
2590 $self->{path} 2620 $self->{path}
2591 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";
2592 2622
2593 $self->{path} 2623 $self->{path}
2594} 2624}
2595 2625
2596sub STORABLE_attach { 2626sub STORABLE_attach {
2627 glDisable GL_TEXTURE_2D; 2657 glDisable GL_TEXTURE_2D;
2628} 2658}
2629 2659
2630############################################################################# 2660#############################################################################
2631 2661
2632package CFPlus::UI::ImageButton; 2662package DC::UI::ImageButton;
2633 2663
2634our @ISA = CFPlus::UI::Image::; 2664our @ISA = DC::UI::Image::;
2635 2665
2636use CFPlus::OpenGL; 2666use DC::OpenGL;
2637 2667
2638my %textures; 2668my %textures;
2639 2669
2640sub new { 2670sub new {
2641 my $class = shift; 2671 my $class = shift;
2644 padding_x => 4, 2674 padding_x => 4,
2645 padding_y => 4, 2675 padding_y => 4,
2646 fg => [1, 1, 1], 2676 fg => [1, 1, 1],
2647 active_fg => [0, 0, 1], 2677 active_fg => [0, 0, 1],
2648 can_hover => 1, 2678 can_hover => 1,
2649 align => 0, 2679 align => 0.5,
2650 valign => 0, 2680 valign => 0.5,
2651 can_events => 1, 2681 can_events => 1,
2652 @_ 2682 @_
2653 ); 2683 );
2654} 2684}
2655 2685
2686sub invoke_button_down {
2687 my ($self, $ev, $x, $y) = @_;
2688
2689 1
2690}
2691
2656sub invoke_button_up { 2692sub invoke_button_up {
2657 my ($self, $ev, $x, $y) = @_; 2693 my ($self, $ev, $x, $y) = @_;
2658 2694
2659 $self->emit ("activate") 2695 $self->emit ("activate")
2660 if $x >= 0 && $x < $self->{w} 2696 if $x >= 0 && $x < $self->{w}
2663 1 2699 1
2664} 2700}
2665 2701
2666############################################################################# 2702#############################################################################
2667 2703
2668package CFPlus::UI::VGauge; 2704package DC::UI::VGauge;
2669 2705
2670our @ISA = CFPlus::UI::Base::; 2706our @ISA = DC::UI::Base::;
2671 2707
2672use List::Util qw(min max); 2708use List::Util qw(min max);
2673 2709
2674use CFPlus::OpenGL; 2710use DC::OpenGL;
2675 2711
2676my %tex = ( 2712my %tex = (
2677 food => [ 2713 food => [
2678 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2714 map { new_from_resource DC::Texture $_, mipmap => 1 }
2679 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2715 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2680 ], 2716 ],
2681 grace => [ 2717 grace => [
2682 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2718 map { new_from_resource DC::Texture $_, mipmap => 1 }
2683 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/
2684 ], 2720 ],
2685 hp => [ 2721 hp => [
2686 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2722 map { new_from_resource DC::Texture $_, mipmap => 1 }
2687 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2723 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2688 ], 2724 ],
2689 mana => [ 2725 mana => [
2690 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2726 map { new_from_resource DC::Texture $_, mipmap => 1 }
2691 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/
2692 ], 2728 ],
2693); 2729);
2694 2730
2695# eg. VGauge->new (gauge => 'food'), default gauge: food 2731# eg. VGauge->new (gauge => 'food'), default gauge: food
2798 glDisable GL_TEXTURE_2D; 2834 glDisable GL_TEXTURE_2D;
2799} 2835}
2800 2836
2801############################################################################# 2837#############################################################################
2802 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
2803package CFPlus::UI::Gauge; 2989package DC::UI::Gauge;
2804 2990
2805our @ISA = CFPlus::UI::VBox::; 2991our @ISA = DC::UI::VBox::;
2806 2992
2807sub new { 2993sub new {
2808 my ($class, %arg) = @_; 2994 my ($class, %arg) = @_;
2809 2995
2810 my $self = $class->SUPER::new ( 2996 my $self = $class->SUPER::new (
2812 can_hover => 1, 2998 can_hover => 1,
2813 can_events => 1, 2999 can_events => 1,
2814 %arg, 3000 %arg,
2815 ); 3001 );
2816 3002
2817 $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");
2818 $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);
2819 $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");
2820 3006
2821 $self 3007 $self
2822} 3008}
2823 3009
2824sub set_fontsize { 3010sub set_fontsize {
2845 $self->{value}->set_text ($val); 3031 $self->{value}->set_text ($val);
2846} 3032}
2847 3033
2848############################################################################# 3034#############################################################################
2849 3035
2850package CFPlus::UI::Slider; 3036package DC::UI::Slider;
2851 3037
2852use strict; 3038use strict;
2853 3039
2854use CFPlus::OpenGL; 3040use DC::OpenGL;
2855 3041
2856our @ISA = CFPlus::UI::DrawBG::; 3042our @ISA = DC::UI::DrawBG::;
2857 3043
2858my @tex = 3044my @tex =
2859 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3045 map { new_from_resource DC::Texture $_ }
2860 qw(s1_slider.png s1_slider_bg.png); 3046 qw(s1_slider.png s1_slider_bg.png);
2861 3047
2862sub new { 3048sub new {
2863 my $class = shift; 3049 my $class = shift;
2864 3050
2901sub set_value { 3087sub set_value {
2902 my ($self, $value) = @_; 3088 my ($self, $value) = @_;
2903 3089
2904 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3090 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2905 3091
2906 $hi = $lo + 1 if $hi <= $lo; 3092 $hi = $lo if $hi < $lo;
2907 3093
2908 $page = $hi - $lo if $page > $hi - $lo; 3094 $value = $hi - $page if $value > $hi - $page;
2909
2910 $value = $lo if $value < $lo; 3095 $value = $lo if $value < $lo;
2911 $value = $hi - $page if $value > $hi - $page;
2912 3096
2913 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3097 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2914 if $unit; 3098 if $unit;
2915 3099
2916 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3100 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2960sub invoke_mouse_wheel { 3144sub invoke_mouse_wheel {
2961 my ($self, $ev) = @_; 3145 my ($self, $ev) = @_;
2962 3146
2963 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3147 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2964 3148
2965 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3149 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2966 3150
2967 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3151 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2968 3152
2969 1 3153 1
2970} 3154}
2980 my ($self) = @_; 3164 my ($self) = @_;
2981 3165
2982 unless ($self->{knob_w}) { 3166 unless ($self->{knob_w}) {
2983 $self->set_value ($self->{range}[0]); 3167 $self->set_value ($self->{range}[0]);
2984 3168
2985 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3169 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2986 my $range = ($hi - $page - $lo) || 1e-100; 3170 my $range = ($hi - $page - $lo) || 1e-10;
2987 3171
2988 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};
2989 3173
2990 $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;
2991 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3175 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2992 3176
2993 $value = ($value - $lo) / $range; 3177 $value = ($value - $lo) / $range;
3023 glDisable GL_TEXTURE_2D; 3207 glDisable GL_TEXTURE_2D;
3024} 3208}
3025 3209
3026############################################################################# 3210#############################################################################
3027 3211
3028package CFPlus::UI::ValSlider; 3212package DC::UI::ValSlider;
3029 3213
3030our @ISA = CFPlus::UI::HBox::; 3214our @ISA = DC::UI::HBox::;
3031 3215
3032sub new { 3216sub new {
3033 my ($class, %arg) = @_; 3217 my ($class, %arg) = @_;
3034 3218
3035 my $range = delete $arg{range}; 3219 my $range = delete $arg{range};
3036 3220
3037 my $self = $class->SUPER::new ( 3221 my $self = $class->SUPER::new (
3038 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3222 slider => (new DC::UI::Slider expand => 1, range => $range),
3039 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3223 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3040 to_value => sub { shift }, 3224 to_value => sub { shift },
3041 from_value => sub { shift }, 3225 from_value => sub { shift },
3042 %arg, 3226 %arg,
3043 ); 3227 );
3044 3228
3064sub set_range { shift->{slider}->set_range (@_) } 3248sub set_range { shift->{slider}->set_range (@_) }
3065sub set_value { shift->{slider}->set_value (@_) } 3249sub set_value { shift->{slider}->set_value (@_) }
3066 3250
3067############################################################################# 3251#############################################################################
3068 3252
3069package CFPlus::UI::TextScroller; 3253package DC::UI::TextScroller;
3070 3254
3071our @ISA = CFPlus::UI::HBox::; 3255our @ISA = DC::UI::HBox::;
3072 3256
3073use CFPlus::OpenGL; 3257use DC::OpenGL;
3074 3258
3075sub new { 3259sub new {
3076 my $class = shift; 3260 my $class = shift;
3077 3261
3078 my $self = $class->SUPER::new ( 3262 my $self = $class->SUPER::new (
3080 can_events => 1, 3264 can_events => 1,
3081 indent => 0, 3265 indent => 0,
3082 #font => default_font 3266 #font => default_font
3083 @_, 3267 @_,
3084 3268
3085 layout => (new CFPlus::Layout), 3269 layout => (new DC::Layout),
3086 par => [], 3270 par => [],
3087 max_par => 0, 3271 max_par => 0,
3088 height => 0, 3272 height => 0,
3089 children => [ 3273 children => [
3090 (new CFPlus::UI::Empty expand => 1), 3274 (new DC::UI::Empty expand => 1),
3091 (new CFPlus::UI::Slider vertical => 1), 3275 (new DC::UI::Slider vertical => 1),
3092 ], 3276 ],
3093 ); 3277 );
3094 3278
3095 $self->{children}[1]->connect (changed => sub { $self->update }); 3279 $self->{children}[1]->connect (changed => sub { $self->update });
3096 3280
3152 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3336 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3153 $layout->set_markup ($para->{markup}); 3337 $layout->set_markup ($para->{markup});
3154 3338
3155 $layout->set_shapes ( 3339 $layout->set_shapes (
3156 map 3340 map
3157 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3341 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3158 @{$para->{widget}} 3342 @{$para->{widget}}
3159 ); 3343 );
3160 3344
3161 $layout 3345 $layout
3162} 3346}
3279 $ROOT->on_post_alloc ($self => sub { 3463 $ROOT->on_post_alloc ($self => sub {
3280 $self->force_uptodate; 3464 $self->force_uptodate;
3281 3465
3282 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3466 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3283 3467
3284 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3468 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3285 glClearColor 0, 0, 0, 0; 3469 glClearColor 0, 0, 0, 0;
3286 glClear GL_COLOR_BUFFER_BIT; 3470 glClear GL_COLOR_BUFFER_BIT;
3287 3471
3288 package CFPlus::UI::Base; 3472 package DC::UI::Base;
3289 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3473 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3290 (0, 0, $self->{w}, $self->{h}); 3474 (0, 0, $self->{w}, $self->{h});
3291 3475
3292 my $top = int $self->{children}[1]{range}[0]; 3476 my $top = int $self->{children}[1]{range}[0];
3293 3477
3304 3488
3305 if ($y0 < $y + $h && $y < $y1) { 3489 if ($y0 < $y + $h && $y < $y1) {
3306 my $layout = $self->get_layout ($para); 3490 my $layout = $self->get_layout ($para);
3307 3491
3308 $layout->render ($para->{indent}, $y - $y0); 3492 $layout->render ($para->{indent}, $y - $y0);
3493 $layout->draw;
3309 3494
3310 if (my @w = @{ $para->{widget} }) { 3495 if (my @w = @{ $para->{widget} }) {
3311 my @s = $layout->get_shapes; 3496 my @s = $layout->get_shapes;
3312 3497
3313 for (@w) { 3498 for (@w) {
3351 $self->{children}[1]->draw; 3536 $self->{children}[1]->draw;
3352} 3537}
3353 3538
3354############################################################################# 3539#############################################################################
3355 3540
3356package CFPlus::UI::Animator; 3541package DC::UI::Animator;
3357 3542
3358use CFPlus::OpenGL; 3543use DC::OpenGL;
3359 3544
3360our @ISA = CFPlus::UI::Bin::; 3545our @ISA = DC::UI::Bin::;
3361 3546
3362sub moveto { 3547sub moveto {
3363 my ($self, $x, $y) = @_; 3548 my ($self, $x, $y) = @_;
3364 3549
3365 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3550 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3393 glPopMatrix; 3578 glPopMatrix;
3394} 3579}
3395 3580
3396############################################################################# 3581#############################################################################
3397 3582
3398package CFPlus::UI::Flopper; 3583package DC::UI::Flopper;
3399 3584
3400our @ISA = CFPlus::UI::Button::; 3585our @ISA = DC::UI::Button::;
3401 3586
3402sub new { 3587sub new {
3403 my $class = shift; 3588 my $class = shift;
3404 3589
3405 my $self = $class->SUPER::new ( 3590 my $self = $class->SUPER::new (
3417 $self->{other}->toggle_visibility; 3602 $self->{other}->toggle_visibility;
3418} 3603}
3419 3604
3420############################################################################# 3605#############################################################################
3421 3606
3422package CFPlus::UI::Tooltip; 3607package DC::UI::Tooltip;
3423 3608
3424our @ISA = CFPlus::UI::Bin::; 3609our @ISA = DC::UI::Bin::;
3425 3610
3426use CFPlus::OpenGL; 3611use DC::OpenGL;
3427 3612
3428sub new { 3613sub new {
3429 my $class = shift; 3614 my $class = shift;
3430 3615
3431 $class->SUPER::new ( 3616 $class->SUPER::new (
3435} 3620}
3436 3621
3437sub set_tooltip_from { 3622sub set_tooltip_from {
3438 my ($self, $widget) = @_; 3623 my ($self, $widget) = @_;
3439 3624
3440 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3441 if $widget->{tooltip} =~ /^#(.*)$/;
3442
3443 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 =~ /^#(.*)$/;
3444 3630
3445 if ($ENV{CFPLUS_DEBUG} & 2) { 3631 if ($ENV{CFPLUS_DEBUG} & 2) {
3446 $tooltip .= "\n\n" . (ref $widget) . "\n" 3632 $tip .= "\n\n" . (ref $widget) . "\n"
3447 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3633 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3448 . "req $widget->{req_w} $widget->{req_h}\n" 3634 . "req $widget->{req_w} $widget->{req_h}\n"
3449 . "visible $widget->{visible}"; 3635 . "visible $widget->{visible}";
3450 } 3636 }
3451 3637
3452 $tooltip =~ s/^\n+//; 3638 $tip =~ s/^\n+//;
3453 $tooltip =~ s/\n+$//; 3639 $tip =~ s/\n+$//;
3454 3640
3455 $self->add (new CFPlus::UI::Label 3641 $self->add (new DC::UI::Label
3642 fg => $DC::THEME{tooltip_fg},
3456 markup => $tooltip, 3643 markup => $tip,
3457 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3644 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3645 align => 0,
3458 fontsize => 0.8, 3646 fontsize => 0.8,
3459 style => 1, # FLAG_INVERSE 3647 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3460 ellipsise => 0, 3648 ellipsise => 0,
3461 font => ($widget->{tooltip_font} || $::FONT_PROP), 3649 font => ($widget->{tooltip_font} || $::FONT_PROP),
3462 ); 3650 );
3463} 3651}
3464 3652
3499} 3687}
3500 3688
3501sub _draw { 3689sub _draw {
3502 my ($self) = @_; 3690 my ($self) = @_;
3503 3691
3504 glTranslate 0.375, 0.375;
3505
3506 my ($w, $h) = @$self{qw(w h)}; 3692 my ($w, $h) = @$self{qw(w h)};
3507 3693
3508 glColor 1, 0.8, 0.4; 3694 glColor @{ $DC::THEME{tooltip_bg} };
3509 glRect 0, 0, $w, $h; 3695 glRect 0, 0, $w, $h;
3510 3696
3511 glColor 0, 0, 0; 3697 glColor @{ $DC::THEME{tooltip_border} };
3512 glBegin GL_LINE_LOOP; 3698 glRect_lineloop .5, .5, $w + .5, $h + .5;
3513 glVertex 0 , 0;
3514 glVertex 0 , $h;
3515 glVertex $w, $h;
3516 glVertex $w, 0;
3517 glEnd;
3518 3699
3519 glTranslate 2 - 0.375, 2 - 0.375; 3700 glTranslate 2, 2;
3520 3701
3521 $self->SUPER::_draw; 3702 $self->SUPER::_draw;
3522} 3703}
3523 3704
3524############################################################################# 3705#############################################################################
3525 3706
3526package CFPlus::UI::Face; 3707package DC::UI::Face;
3527 3708
3528our @ISA = CFPlus::UI::DrawBG::; 3709our @ISA = DC::UI::DrawBG::;
3529 3710
3530use CFPlus::OpenGL; 3711use DC::OpenGL;
3531 3712
3532sub new { 3713sub new {
3533 my $class = shift; 3714 my $class = shift;
3534 3715
3535 my $self = $class->SUPER::new ( 3716 my $self = $class->SUPER::new (
3539 can_events => 0, 3720 can_events => 0,
3540 @_, 3721 @_,
3541 ); 3722 );
3542 3723
3543 if ($self->{anim} && $self->{animspeed}) { 3724 if ($self->{anim} && $self->{animspeed}) {
3544 CFPlus::weaken (my $widget = $self); 3725 DC::weaken (my $widget = $self);
3545 3726
3546 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3727 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3547 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3728 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3548 $self->{timer} = Event->timer (
3549 parked => 1,
3550 cb => sub {
3551 return unless $::CONN && $widget; 3729 return unless $::CONN;
3552 3730
3731 my $w = $widget
3732 or return;
3733
3553 ++$widget->{frame}; 3734 ++$w->{frame};
3554 $widget->update_face; 3735 $w->update_face;
3736
3737 # somehow, $widget can go away
3555 $widget->update; 3738 $w->update;
3556
3557 $widget->update_timer; 3739 $w->update_timer;
3558 },
3559 ); 3740 };
3560 3741
3561 $self->update_face; 3742 $self->update_face;
3562 $self->update_timer; 3743 $self->update_timer;
3563 } 3744 }
3564 3745
3569 my ($self) = @_; 3750 my ($self) = @_;
3570 3751
3571 return unless $self->{timer}; 3752 return unless $self->{timer};
3572 3753
3573 if ($self->{visible}) { 3754 if ($self->{visible}) {
3574 $self->{timer}->at (
3575 $self->{anim_start}
3576 + $self->{animspeed}
3577 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3578 );
3579 $self->{timer}->start; 3755 $self->{timer}->start;
3580 } else { 3756 } else {
3581 $self->{timer}->stop; 3757 $self->{timer}->stop;
3582 } 3758 }
3583} 3759}
3584 3760
3585sub update_face { 3761sub update_face {
3586 my ($self) = @_; 3762 my ($self) = @_;
3587 3763
3588 return unless $::CONN; 3764 if ($::CONN) {
3589
3590 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3591 if ($anim && @$anim) { 3766 if ($anim && @$anim) {
3592 delete $self->{wait_face};
3593 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3768 delete $self->{face_change_cb};
3769
3594 $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 }
3595 } 3776 }
3596 } 3777 }
3597} 3778}
3598 3779
3599sub size_request { 3780sub size_request {
3600 my ($self) = @_; 3781 my ($self) = @_;
3601 3782
3602 if ($::CONN) { 3783 if ($::CONN) {
3603 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3784 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3604 if (my $tex = $::CONN->{texture}[$faceid]) { 3785 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3605 $self->{tex} = $tex; 3786 if ($tex->{name}) {
3606 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3787 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3607 } else { 3788 } elsif (!$tex->{loading}) {
3608 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3789 $tex->upload (sub { $self->reconfigure });
3609 $self->realloc;
3610 }); 3790 }
3611 } 3791 }
3792
3793 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3612 } 3794 }
3613 } 3795 }
3614 3796
3615 ($self->{size_w} || 8, $self->{size_h} || 8) 3797 ($self->{size_w} || 8, $self->{size_h} || 8)
3616} 3798}
3654 $self->SUPER::destroy; 3836 $self->SUPER::destroy;
3655} 3837}
3656 3838
3657############################################################################# 3839#############################################################################
3658 3840
3659package CFPlus::UI::Buttonbar; 3841package DC::UI::Buttonbar;
3660 3842
3661our @ISA = CFPlus::UI::HBox::; 3843our @ISA = DC::UI::HBox::;
3662 3844
3663# TODO: should actually wrap buttons and other goodies. 3845# TODO: should actually wrap buttons and other goodies.
3664 3846
3665############################################################################# 3847#############################################################################
3666 3848
3667package CFPlus::UI::Menu; 3849package DC::UI::Menu;
3668 3850
3669our @ISA = CFPlus::UI::Toplevel::; 3851our @ISA = DC::UI::Toplevel::;
3670 3852
3671use CFPlus::OpenGL; 3853use DC::OpenGL;
3672 3854
3673sub new { 3855sub new {
3674 my $class = shift; 3856 my $class = shift;
3675 3857
3676 my $self = $class->SUPER::new ( 3858 my $self = $class->SUPER::new (
3677 items => [], 3859 items => [],
3678 z => 100, 3860 z => 100,
3679 @_, 3861 @_,
3680 ); 3862 );
3681 3863
3682 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3864 $self->add ($self->{vbox} = new DC::UI::VBox);
3683 3865
3684 for my $item (@{ $self->{items} }) { 3866 for my $item (@{ $self->{items} }) {
3685 my ($widget, $cb, $tooltip) = @$item; 3867 my ($widget, $cb, $tooltip) = @$item;
3686 3868
3687 # handle various types of items, only text for now 3869 # handle various types of items, only text for now
3688 if (!ref $widget) { 3870 if (!ref $widget) {
3689 if ($widget =~ /\t/) { 3871 if ($widget =~ /\t/) {
3690 my ($left, $right) = split /\t/, $widget, 2; 3872 my ($left, $right) = split /\t/, $widget, 2;
3691 3873
3692 $widget = new CFPlus::UI::HBox 3874 $widget = new DC::UI::HBox
3693 can_hover => 1, 3875 can_hover => 1,
3694 can_events => 1, 3876 can_events => 1,
3695 tooltip => $tooltip, 3877 tooltip => $tooltip,
3696 children => [ 3878 children => [
3697 (new CFPlus::UI::Label markup => $left, expand => 1), 3879 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3698 (new CFPlus::UI::Label markup => $right, align => +1), 3880 (new DC::UI::Label markup => $right, align => 1),
3699 ], 3881 ],
3700 ; 3882 ;
3701 3883
3702 } else { 3884 } else {
3703 $widget = new CFPlus::UI::Label 3885 $widget = new DC::UI::Label
3704 can_hover => 1, 3886 can_hover => 1,
3705 can_events => 1, 3887 can_events => 1,
3888 align => 0,
3706 markup => $widget, 3889 markup => $widget,
3707 tooltip => $tooltip; 3890 tooltip => $tooltip;
3708 } 3891 }
3709 } 3892 }
3710 3893
3726 $GRAB = $self; 3909 $GRAB = $self;
3727 $self->{button} = $ev->{button}; 3910 $self->{button} = $ev->{button};
3728 3911
3729 $self->show; 3912 $self->show;
3730 $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
3731} 3916}
3732 3917
3733sub invoke_mouse_motion { 3918sub invoke_mouse_motion {
3734 my ($self, $ev, $x, $y) = @_; 3919 my ($self, $ev, $x, $y) = @_;
3735 3920
3756 1 3941 1
3757} 3942}
3758 3943
3759############################################################################# 3944#############################################################################
3760 3945
3761package CFPlus::UI::Multiplexer; 3946package DC::UI::Multiplexer;
3762 3947
3763our @ISA = CFPlus::UI::Container::; 3948our @ISA = DC::UI::Container::;
3764 3949
3765sub new { 3950sub new {
3766 my $class = shift; 3951 my $class = shift;
3767 3952
3768 my $self = $class->SUPER::new ( 3953 my $self = $class->SUPER::new (
3769 @_, 3954 @_,
3770 ); 3955 );
3771 3956
3772 $self->{current} = $self->{children}[0] 3957 $self->set_current_page (0);
3773 if @{ $self->{children} };
3774 3958
3775 $self 3959 $self
3776} 3960}
3777 3961
3778sub add { 3962sub add {
3779 my ($self, @widgets) = @_; 3963 my ($self, @widgets) = @_;
3780 3964
3781 $self->SUPER::add (@widgets); 3965 $self->SUPER::add (@widgets);
3782 3966
3783 $self->{current} = $self->{children}[0] 3967 $self->set_current_page (0)
3784 if @{ $self->{children} }; 3968 if @widgets == @{ $self->{children} };
3785} 3969}
3786 3970
3787sub get_current_page { 3971sub get_current_page {
3788 my ($self) = @_; 3972 my ($self) = @_;
3789 3973
3795 3979
3796 my $widget = ref $page_or_widget 3980 my $widget = ref $page_or_widget
3797 ? $page_or_widget 3981 ? $page_or_widget
3798 : $self->{children}[$page_or_widget]; 3982 : $self->{children}[$page_or_widget];
3799 3983
3984 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3985
3800 $self->{current} = $widget; 3986 if (($self->{current} = $widget)) {
3987 $self->{current}->set_visible if $self->{current} && $self->{visible};
3801 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3988 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3802 3989
3803 $self->emit (page_changed => $self->{current}); 3990 $self->emit (page_changed => $self->{current});
3991 }
3804 3992
3805 $self->realloc; 3993 $self->realloc;
3806} 3994}
3807 3995
3808sub visible_children { 3996sub visible_children {
3809 $_[0]{current} 3997 $_[0]{current} || ()
3810} 3998}
3811 3999
3812sub size_request { 4000sub size_request {
3813 my ($self) = @_; 4001 my ($self) = @_;
3814 4002
4003 $self->{current}
3815 $self->{current}->size_request 4004 ? $self->{current}->size_request
4005 : (0, 0)
3816} 4006}
3817 4007
3818sub invoke_size_allocate { 4008sub invoke_size_allocate {
3819 my ($self, $w, $h) = @_; 4009 my ($self, $w, $h) = @_;
3820 4010
3821 $self->{current}->configure (0, 0, $w, $h); 4011 $self->{current}->configure (0, 0, $w, $h)
4012 if $self->{current};
3822 4013
3823 1 4014 1
3824} 4015}
3825 4016
3826sub _draw { 4017sub _draw {
3827 my ($self) = @_; 4018 my ($self) = @_;
3828 4019
3829 $self->{current}->draw; 4020 $self->{current}->draw
4021 if $self->{current};
3830} 4022}
3831 4023
3832############################################################################# 4024#############################################################################
3833 4025
3834package CFPlus::UI::Notebook; 4026package DC::UI::Notebook;
3835 4027
4028use DC::OpenGL;
4029
3836our @ISA = CFPlus::UI::VBox::; 4030our @ISA = DC::UI::VBox::;
3837 4031
3838sub new { 4032sub new {
3839 my $class = shift; 4033 my $class = shift;
3840 4034
3841 my $self = $class->SUPER::new ( 4035 my $self = $class->SUPER::new (
3842 buttonbar => (new CFPlus::UI::Buttonbar), 4036 buttonbar => (new DC::UI::Buttonbar),
3843 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4037 multiplexer => (new DC::UI::Multiplexer expand => 1),
4038 active_outline => [.7, .7, 0.2],
3844 # filter => # will be put between multiplexer and $self 4039 # filter => # will be put between multiplexer and $self
3845 @_, 4040 @_,
3846 ); 4041 );
3847 4042
3848 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4043 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3856 4051
3857 for my $child (@$widgets) { 4052 for my $child (@$widgets) {
3858 Scalar::Util::weaken $child; 4053 Scalar::Util::weaken $child;
3859 $child->{c_tab_} ||= do { 4054 $child->{c_tab_} ||= do {
3860 my $tab = 4055 my $tab =
3861 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4056 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3862 ? $child->{c_tab} 4057 ? $child->{c_tab}
3863 : 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];
3864 4059
3865 $tab->connect (activate => sub { 4060 $tab->connect (activate => sub {
3866 $wself->set_current_page ($child); 4061 $wself->set_current_page ($child);
3867 }); 4062 });
3868 4063
3900sub pages { 4095sub pages {
3901 my ($self) = @_; 4096 my ($self) = @_;
3902 $self->{multiplexer}->children 4097 $self->{multiplexer}->children
3903} 4098}
3904 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
3905sub add_tab { 4112sub add_tab {
3906 my ($self, $title, $widget, $tooltip) = @_; 4113 my ($self, $title, $widget, $tooltip) = @_;
3907 4114
3908 $title = [$title, $tooltip] unless ref $title; 4115 $title = [$title, $tooltip] unless ref $title;
3909 $widget->{c_tab} = $title; 4116 $widget->{c_tab} = $title;
3922 4129
3923 $self->{multiplexer}->set_current_page ($page); 4130 $self->{multiplexer}->set_current_page ($page);
3924 $self->emit (page_changed => $self->{multiplexer}{current}); 4131 $self->emit (page_changed => $self->{multiplexer}{current});
3925} 4132}
3926 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
3927############################################################################# 4154#############################################################################
3928 4155
3929package CFPlus::UI::Selector; 4156package DC::UI::Selector;
3930 4157
3931use utf8; 4158use utf8;
3932 4159
3933our @ISA = CFPlus::UI::Button::; 4160our @ISA = DC::UI::Button::;
3934 4161
3935sub new { 4162sub new {
3936 my $class = shift; 4163 my $class = shift;
3937 4164
3938 my $self = $class->SUPER::new ( 4165 my $self = $class->SUPER::new (
3955 my ($value, $title, $tooltip) = @$_; 4182 my ($value, $title, $tooltip) = @$_;
3956 4183
3957 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4184 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3958 } 4185 }
3959 4186
3960 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4187 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3961} 4188}
3962 4189
3963sub _set_value { 4190sub _set_value {
3964 my ($self, $value) = @_; 4191 my ($self, $value) = @_;
3965 4192
3966 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4193 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4194 $item ||= $self->{options}[0]
3967 or return; 4195 or return;
3968 4196
3969 $self->{value} = $item->[0]; 4197 $self->{value} = $item->[0];
3970 $self->set_markup ("$item->[1] ⇓"); 4198 $self->set_markup ("$item->[1] ⇓");
3971 $self->set_tooltip ($item->[2]); 4199# $self->set_tooltip ($item->[2]);
3972} 4200}
3973 4201
3974sub set_value { 4202sub set_value {
3975 my ($self, $value) = @_; 4203 my ($self, $value) = @_;
3976 4204
3978 4206
3979 $self->_set_value ($value); 4207 $self->_set_value ($value);
3980 $self->emit (changed => $value); 4208 $self->emit (changed => $value);
3981} 4209}
3982 4210
4211sub set_options {
4212 my ($self, $options) = @_;
4213
4214 $self->{options} = $options;
4215 $self->_set_value ($self->{value});
4216}
4217
3983############################################################################# 4218#############################################################################
3984 4219
3985package CFPlus::UI::Statusbox; 4220package DC::UI::Statusbox;
3986 4221
3987our @ISA = CFPlus::UI::VBox::; 4222our @ISA = DC::UI::VBox::;
3988 4223
3989sub new { 4224sub new {
3990 my $class = shift; 4225 my $class = shift;
3991 4226
3992 my $self = $class->SUPER::new ( 4227 my $self = $class->SUPER::new (
3993 fontsize => 0.8, 4228 fontsize => 0.8,
3994 @_, 4229 @_,
3995 ); 4230 );
3996 4231
3997 CFPlus::weaken (my $this = $self); 4232 DC::weaken (my $this = $self);
3998 4233
3999 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4234 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4000 4235
4001 $self 4236 $self
4002} 4237}
4003 4238
4004sub reorder { 4239sub reorder {
4005 my ($self) = @_; 4240 my ($self) = @_;
4006 my $NOW = Time::HiRes::time; 4241 my $NOW = EV::time;
4007 4242
4008 # freeze display when hovering over any label 4243 # freeze display when hovering over any label
4009 return if $CFPlus::UI::TOOLTIP->{owner} 4244 return if $DC::UI::TOOLTIP->{owner}
4010 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4245 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4011 values %{ $self->{item} }; 4246 values %{ $self->{item} };
4012 4247
4013 while (my ($k, $v) = each %{ $self->{item} }) { 4248 while (my ($k, $v) = each %{ $self->{item} }) {
4014 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4249 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4015 } 4250 }
4251
4252 $self->{timer}->set (1, 1);
4016 4253
4017 my @widgets; 4254 my @widgets;
4018 4255
4019 my @items = sort { 4256 my @items = sort {
4020 $a->{pri} <=> $b->{pri} 4257 $a->{pri} <=> $b->{pri}
4021 or $b->{id} <=> $a->{id} 4258 or $b->{id} <=> $a->{id}
4022 } values %{ $self->{item} }; 4259 } values %{ $self->{item} };
4023
4024 $self->{timer}->interval (1);
4025 4260
4026 my $count = 10 + 1; 4261 my $count = 10 + 1;
4027 for my $item (@items) { 4262 for my $item (@items) {
4028 last unless --$count; 4263 last unless --$count;
4029 4264
4036 for ($short) { 4271 for ($short) {
4037 s/^\s+//; 4272 s/^\s+//;
4038 s/\s+/ /g; 4273 s/\s+/ /g;
4039 } 4274 }
4040 4275
4041 new CFPlus::UI::Label 4276 new DC::UI::Label
4042 markup => $short, 4277 markup => $short,
4043 tooltip => $item->{tooltip}, 4278 tooltip => $item->{tooltip},
4044 tooltip_font => $::FONT_PROP, 4279 tooltip_font => $::FONT_PROP,
4045 tooltip_width => 0.67, 4280 tooltip_width => 0.67,
4046 fontsize => $item->{fontsize} || $self->{fontsize}, 4281 fontsize => $item->{fontsize} || $self->{fontsize},
4047 max_w => $::WIDTH * 0.44, 4282 max_w => $::WIDTH * 0.44,
4283 align => 0,
4048 fg => [@{ $item->{fg} }], 4284 fg => [@{ $item->{fg} }],
4049 can_events => 1, 4285 can_events => 1,
4050 can_hover => 1 4286 can_hover => 1
4051 }; 4287 };
4052 4288
4053 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4289 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4054 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4290 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4055 $label->update; 4291 $label->update;
4056 $label->set_max_size (undef, $label->{req_h} * $diff) 4292 $label->set_max_size (undef, $label->{req_h} * $diff)
4057 if $diff < 1; 4293 if $diff < 1;
4058 $self->{timer}->interval (1/30); 4294 $self->{timer}->set (1/30, 1/30);
4059 } else { 4295 } else {
4060 $label->{fg}[3] = $item->{fg}[3] || 1; 4296 $label->{fg}[3] = $item->{fg}[3] || 1;
4061 } 4297 }
4062 4298
4063 push @widgets, $label; 4299 push @widgets, $label;
4064 } 4300 }
4301
4302 my $hash = join ",", @widgets;
4303 return if $hash eq $self->{last_widget_hash};
4304 $self->{last_widget_hash} = $hash;
4065 4305
4066 $self->clear; 4306 $self->clear;
4067 $self->SUPER::add (reverse @widgets); 4307 $self->SUPER::add (reverse @widgets);
4068} 4308}
4069 4309
4125 $self->SUPER::destroy; 4365 $self->SUPER::destroy;
4126} 4366}
4127 4367
4128############################################################################# 4368#############################################################################
4129 4369
4130package CFPlus::UI::Root; 4370package DC::UI::Root;
4131 4371
4132our @ISA = CFPlus::UI::Container::; 4372our @ISA = DC::UI::Container::;
4133 4373
4134use List::Util qw(min max); 4374use List::Util qw(min max);
4135 4375
4136use CFPlus::OpenGL; 4376use DC::OpenGL;
4137 4377
4138sub new { 4378sub new {
4139 my $class = shift; 4379 my $class = shift;
4140 4380
4141 my $self = $class->SUPER::new ( 4381 my $self = $class->SUPER::new (
4142 visible => 1, 4382 visible => 1,
4143 @_, 4383 @_,
4144 ); 4384 );
4145 4385
4146 CFPlus::weaken ($self->{root} = $self); 4386 DC::weaken ($self->{root} = $self);
4147 4387
4148 $self 4388 $self
4149} 4389}
4150 4390
4151sub size_request { 4391sub size_request {
4199} 4439}
4200 4440
4201sub update { 4441sub update {
4202 my ($self) = @_; 4442 my ($self) = @_;
4203 4443
4204 $::WANT_REFRESH++; 4444 $::WANT_REFRESH = 1;
4205} 4445}
4206 4446
4207sub add { 4447sub add {
4208 my ($self, @children) = @_; 4448 my ($self, @children) = @_;
4209 4449
4276 4516
4277 delete $queue{$widget+0}; 4517 delete $queue{$widget+0};
4278 4518
4279 my ($w, $h) = $widget->size_request; 4519 my ($w, $h) = $widget->size_request;
4280 4520
4281 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4521 $w += $widget->{padding_x} * 2;
4282 $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;
4283 4526
4284 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4527 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4285 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4528 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4286 4529
4287 $w = $widget->{force_w} if exists $widget->{force_w}; 4530 $w = $widget->{force_w} if exists $widget->{force_w};
4354 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4597 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4355 glMatrixMode GL_MODELVIEW; 4598 glMatrixMode GL_MODELVIEW;
4356 glLoadIdentity; 4599 glLoadIdentity;
4357 4600
4358 { 4601 {
4359 package CFPlus::UI::Base; 4602 package DC::UI::Base;
4360 4603
4361 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4604 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4362 (0, 0, $self->{w}, $self->{h}); 4605 (0, 0, $self->{w}, $self->{h});
4363 4606
4364 $self->_draw; 4607 $self->_draw;
4365 } 4608 }
4366} 4609}
4367 4610
4368############################################################################# 4611#############################################################################
4369 4612
4370package CFPlus::UI; 4613package DC::UI;
4371 4614
4372$ROOT = new CFPlus::UI::Root; 4615$ROOT = new DC::UI::Root;
4373$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4616$TOOLTIP = new DC::UI::Tooltip z => 900;
4374 4617
43751 46181
4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines