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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines