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.440 by root, Sun Sep 16 18:27:48 2007 UTC vs.
Revision 1.488 by root, Fri Nov 23 12:39:05 2012 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
6
7use Event; 7use AnyEvent ();
8use Guard ();
8 9
9use CFPlus; 10use DC;
10use CFPlus::Pod; 11use DC::Pod;
11use CFPlus::Texture; 12use DC::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
17our $TOOLTIP; 18our $TOOLTIP;
18our $BUTTON_STATE; 19our $BUTTON_STATE;
19 20
20our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
21 22
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
24 $_[0]->stop;
25
23 if (!$GRAB) { 26 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 29 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 30 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 45 }
43 46
44 $TOOLTIP->hide; 47 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 48 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 49 delete $TOOLTIP->{owner};
47}); 50};
48 51
49sub get_layout { 52sub get_layout {
50 my $layout; 53 my $layout;
51 54
52 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
53 my $win = $layout->{$_->{name}} = { }; 56 my $win = $layout->{$_->{name}} = { };
54 57
55 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/; 58 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
56 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/; 59 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
57 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w}; 60 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
58 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h}; 61 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
59 62
68 71
69 $LAYOUT = $layout; 72 $LAYOUT = $layout;
70} 73}
71 74
72# class methods for events 75# class methods for events
73sub feed_sdl_key_down_event { 76sub feed_sdl_key_down_event {
74 $FOCUS->emit (key_down => $_[0]) 77 $FOCUS->emit (key_down => $_[0])
75 if $FOCUS; 78 if $FOCUS;
76} 79}
77 80
78sub feed_sdl_key_up_event { 81sub feed_sdl_key_up_event {
87 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
88 91
89 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 94
92 $TOOLTIP_WATCHER->start; 95 $TOOLTIP_WATCHER->again;
93 } 96 }
94} 97}
95 98
96sub feed_sdl_button_down_event { 99sub feed_sdl_button_down_event {
97 my ($ev) = @_; 100 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 106 my $widget = $ROOT->find_widget ($x, $y);
104 107
105 $GRAB = $widget; 108 $GRAB = $widget;
106 $GRAB->update if $GRAB; 109 $GRAB->update if $GRAB;
107 110
108 $TOOLTIP_WATCHER->cb->(); 111 $TOOLTIP_WATCHER->invoke;
109 } 112 }
110 113
111 if ($GRAB) { 114 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 116 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 119
117 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
119 122
120 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 141 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 142 $grab->update if $grab;
140 $GRAB->update if $GRAB; 143 $GRAB->update if $GRAB;
141 144
142 check_hover $widget; 145 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 146 $TOOLTIP_WATCHER->invoke;
144 } 147 }
145} 148}
146 149
147sub feed_sdl_motion_event { 150sub feed_sdl_motion_event {
148 my ($ev) = @_; 151 my ($ev) = @_;
188# call when resolution changes etc. 191# call when resolution changes etc.
189sub rescale_widgets { 192sub rescale_widgets {
190 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
191 194
192 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 199
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 213 reconfigure_widgets;
211} 214}
212 215
213############################################################################# 216#############################################################################
214 217
215package CFPlus::UI::Event; 218package DC::UI::Event;
216 219
217sub xy { 220sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 222}
220 223
221############################################################################# 224#############################################################################
222 225
223package CFPlus::UI::Base; 226package DC::UI::Base;
224 227
225use strict; 228use common::sense;
226 229
227use CFPlus::OpenGL; 230use DC::OpenGL;
228 231
229sub new { 232sub new {
230 my $class = shift; 233 my $class = shift;
231 234
232 my $self = bless { 235 my $self = bless {
237 h => undef, 240 h => undef,
238 can_events => 1, 241 can_events => 1,
239 @_ 242 @_
240 }, $class; 243 }, $class;
241 244
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 246
244 for (keys %$self) { 247 for (keys %$self) {
245 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
247 } 250 }
248 } 251 }
249 252
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 258
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 261
259 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
277sub show { 280sub show {
278 my ($self) = @_; 281 my ($self) = @_;
279 282
280 return if $self->{parent}; 283 return if $self->{parent};
281 284
282 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
283} 286}
284 287
285sub set_visible { 288sub set_visible {
286 my ($self) = @_; 289 my ($self) = @_;
287 290
288 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 295
290 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
292 298
293 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
294 300
295 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
296 302
297 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
298} 304}
299 305
300sub set_invisible { 306sub set_invisible {
301 my ($self) = @_; 307 my ($self) = @_;
302 308
308 delete $self->{root}; 314 delete $self->{root};
309 315
310 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
312 318
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 319 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
315 321
316 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
318} 324}
337sub hide { 343sub hide {
338 my ($self) = @_; 344 my ($self) = @_;
339 345
340 $self->set_invisible; 346 $self->set_invisible;
341 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 351 $parent->remove ($self);
343 if $self->{parent}; 352 }
344} 353}
345 354
346sub move_abs { 355sub move_abs {
347 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
348 357
354} 363}
355 364
356sub set_size { 365sub set_size {
357 my ($self, $w, $h) = @_; 366 my ($self, $w, $h) = @_;
358 367
359 $self->{force_w} = $w; 368 $self->{force_w} = List::Util::min $w, ($self->{max_w} || $::WIDTH );
360 $self->{force_h} = $h; 369 $self->{force_h} = List::Util::min $h, ($self->{max_h} || $::HEIGHT);
361 370
362 $self->realloc; 371 $self->realloc;
363} 372}
364 373
365# traverse the widget chain up to find the maximum "physical" size constraints 374## traverse the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh { 375#sub get_max_wh {
367 my ($self) = @_; 376# my ($self) = @_;
368 377#
369 return $self->{parent}->get_max_wh 378# my ($w, $h) = @$self{qw(max_w max_h)};
370 if $self->{parent}; 379#
371 380# if ($w <= 0 || $h <= 0) {
381# my ($mw, $mh) = $self->{parent}
382# ? $self->{parent}->get_max_wh
372 ($::WIDTH, $::HEIGHT) 383# : ($::WIDTH, $::HEIGHT);
373} 384#
385# $w = $mw if $w <= 0;
386# $h = $mh if $h <= 0;
387# }
388#
389# ($w, $h)
390#}
374 391
375sub size_request { 392sub size_request {
376 require Carp; 393 require Carp;
377 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
378} 395}
385 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
386 403
387 if ($self->{aspect}) { 404 if ($self->{aspect}) {
388 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
389 406
390 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
391 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
392 409
393 # use alignment to adjust x, y 410 # use alignment to adjust x, y
394 411
395 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
396 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
437 454
438 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
439 456
440 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
441 458
442 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
443 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
444 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
445 } 462 }
446} 463}
447 464
448# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
449sub coord2local { 466sub coord2local {
450 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
451 468
452 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
453 470
454 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
455} 472}
456 473
457# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
458sub coord2global { 475sub coord2global {
459 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
460 477
461 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
462 479
463 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
464} 481}
465 482
466sub invoke_focus_in { 483sub invoke_focus_in {
515sub connect { 532sub connect {
516 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
517 534
518 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
519 536
520 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
521 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
522 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
523 } 540 }
524} 541}
525 542
563# in .xs 580# in .xs
564 581
565sub set_parent { 582sub set_parent {
566 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
567 584
568 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
569 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
570} 587}
571 588
572sub realloc { 589sub realloc {
573 my ($self) = @_; 590 my ($self) = @_;
611} 628}
612 629
613sub DESTROY { 630sub DESTROY {
614 my ($self) = @_; 631 my ($self) = @_;
615 632
616 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
617 634
618 local $@; 635 local $@;
619 eval { $self->destroy }; 636 eval { $self->destroy };
620 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
621 638
622 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
623} 640}
624 641
625############################################################################# 642#############################################################################
626 643
627package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
628 645
629our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
630 647
631use strict; 648use common::sense;
649
632use CFPlus::OpenGL; 650use DC::OpenGL;
633 651
634sub new { 652sub new {
635 my $class = shift; 653 my $class = shift;
636 654
637 $class->SUPER::new ( 655 $class->SUPER::new (
639 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
640 @_ 658 @_
641 ) 659 )
642} 660}
643 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
644sub _draw { 669sub _draw {
645 my ($self) = @_; 670 my ($self) = @_;
646 671
647 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
648 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
649 : $self->{bg}; 674 : $self->{bg};
650 675
651 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
652 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
653 678
659 } 684 }
660} 685}
661 686
662############################################################################# 687#############################################################################
663 688
664package CFPlus::UI::Empty; 689package DC::UI::Empty;
665 690
666our @ISA = CFPlus::UI::Base::; 691our @ISA = DC::UI::Base::;
667 692
668sub new { 693sub new {
669 my ($class, %arg) = @_; 694 my ($class, %arg) = @_;
670 $class->SUPER::new (can_events => 0, %arg); 695 $class->SUPER::new (can_events => 0, %arg);
671} 696}
678 703
679sub draw { } 704sub draw { }
680 705
681############################################################################# 706#############################################################################
682 707
683package CFPlus::UI::Container; 708package DC::UI::Container;
684 709
685our @ISA = CFPlus::UI::Base::; 710our @ISA = DC::UI::Base::;
686 711
687sub new { 712sub new {
688 my ($class, %arg) = @_; 713 my ($class, %arg) = @_;
689 714
690 my $children = delete $arg{children}; 715 my $children = delete $arg{children};
784 $_->draw for $self->visible_children; 809 $_->draw for $self->visible_children;
785} 810}
786 811
787############################################################################# 812#############################################################################
788 813
789package CFPlus::UI::Bin; 814package DC::UI::Bin;
790 815
791our @ISA = CFPlus::UI::Container::; 816our @ISA = DC::UI::Container::;
792 817
793sub new { 818sub new {
794 my ($class, %arg) = @_; 819 my ($class, %arg) = @_;
795 820
796 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 821 my $child = (delete $arg{child}) || new DC::UI::Empty::;
797 822
798 $class->SUPER::new (children => [$child], %arg) 823 $class->SUPER::new (children => [$child], %arg)
799} 824}
800 825
801sub add { 826sub add {
808sub remove { 833sub remove {
809 my ($self, $widget) = @_; 834 my ($self, $widget) = @_;
810 835
811 $self->SUPER::remove ($widget); 836 $self->SUPER::remove ($widget);
812 837
813 $self->{children} = [new CFPlus::UI::Empty] 838 $self->{children} = [new DC::UI::Empty]
814 unless @{$self->{children}}; 839 unless @{$self->{children}};
815} 840}
816 841
817sub child { $_[0]->{children}[0] } 842sub child { $_[0]->{children}[0] }
818 843
829} 854}
830 855
831############################################################################# 856#############################################################################
832# back-buffered drawing area 857# back-buffered drawing area
833 858
834package CFPlus::UI::Window; 859package DC::UI::Window;
835 860
836our @ISA = CFPlus::UI::Bin::; 861our @ISA = DC::UI::Bin::;
837 862
838use CFPlus::OpenGL; 863use DC::OpenGL;
839 864
840sub new { 865sub new {
841 my ($class, %arg) = @_; 866 my ($class, %arg) = @_;
842 867
843 my $self = $class->SUPER::new (%arg); 868 my $self = $class->SUPER::new (%arg);
865} 890}
866 891
867sub render_child { 892sub render_child {
868 my ($self) = @_; 893 my ($self) = @_;
869 894
870 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 895 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
871 glClearColor 0, 0, 0, 0; 896 glClearColor 0, 0, 0, 0;
872 glClear GL_COLOR_BUFFER_BIT; 897 glClear GL_COLOR_BUFFER_BIT;
873 898
874 { 899 {
875 package CFPlus::UI::Base; 900 package DC::UI::Base;
876 901
877 local ($draw_x, $draw_y, $draw_w, $draw_h) = 902 local ($draw_x, $draw_y, $draw_w, $draw_h) =
878 (0, 0, $self->{w}, $self->{h}); 903 (0, 0, $self->{w}, $self->{h});
879 904
880 $self->_render; 905 $self->_render;
897 glDisable GL_TEXTURE_2D; 922 glDisable GL_TEXTURE_2D;
898} 923}
899 924
900############################################################################# 925#############################################################################
901 926
902package CFPlus::UI::ViewPort; 927package DC::UI::ViewPort;
903 928
904use List::Util qw(min max); 929use List::Util qw(min max);
905 930
906our @ISA = CFPlus::UI::Window::; 931our @ISA = DC::UI::Window::;
907 932
908sub new { 933sub new {
909 my $class = shift; 934 my $class = shift;
910 935
911 $class->SUPER::new ( 936 $class->SUPER::new (
995 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
996 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
997 ) { 1022 ) {
998 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1023 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
999 } else { 1024 } else {
1000 $self->CFPlus::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
1001 } 1026 }
1002} 1027}
1003 1028
1004sub _render { 1029sub _render {
1005 my ($self) = @_; 1030 my ($self) = @_;
1006 1031
1007 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1032 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1008 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1033 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1009 1034
1010 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1011 1036
1012 $self->SUPER::_render; 1037 $self->SUPER::_render;
1013} 1038}
1014 1039
1015############################################################################# 1040#############################################################################
1016 1041
1017package CFPlus::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1018 1043
1019our @ISA = CFPlus::UI::Table::; 1044our @ISA = DC::UI::Table::;
1020 1045
1021sub new { 1046sub new {
1022 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1023 1048
1024 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1025 1050
1026 my $self; 1051 my $self;
1027 1052
1028 my $hslider = new CFPlus::UI::Slider 1053 my $hslider = new DC::UI::Slider
1029 c_col => 0, 1054 c_col => 0,
1030 c_row => 1, 1055 c_row => 1,
1031 vertical => 0, 1056 vertical => 0,
1032 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1033 on_changed => sub { 1058 on_changed => sub {
1034 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1035 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1036 }, 1061 },
1037 ; 1062 ;
1038 1063
1039 my $vslider = new CFPlus::UI::Slider 1064 my $vslider = new DC::UI::Slider
1040 c_col => 1, 1065 c_col => 1,
1041 c_row => 0, 1066 c_row => 0,
1042 vertical => 1, 1067 vertical => 1,
1043 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1044 on_changed => sub { 1069 on_changed => sub {
1056 col_expand => [1, 0], 1081 col_expand => [1, 0],
1057 row_expand => [1, 0], 1082 row_expand => [1, 0],
1058 %arg, 1083 %arg,
1059 ); 1084 );
1060 1085
1061 $self->{vp} = new CFPlus::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1062 c_col => 0, 1087 c_col => 0,
1063 c_row => 0, 1088 c_row => 0,
1064 expand => 1, 1089 expand => 1,
1065 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1066 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1131 1156
1132 $self->grab_focus; 1157 $self->grab_focus;
1133 1158
1134 my $ox = $self->{vp}{view_x}; 1159 my $ox = $self->{vp}{view_x};
1135 my $oy = $self->{vp}{view_y}; 1160 my $oy = $self->{vp}{view_y};
1136 1161
1137 $self->{motion} = sub { 1162 $self->{motion} = sub {
1138 my ($ev, $x, $y) = @_; 1163 my ($ev, $x, $y) = @_;
1139 1164
1140 $ox -= $ev->{xrel}; 1165 $ox -= $ev->{xrel};
1141 $oy -= $ev->{yrel}; 1166 $oy -= $ev->{yrel};
1192 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1193} 1218}
1194 1219
1195############################################################################# 1220#############################################################################
1196 1221
1197package CFPlus::UI::Frame; 1222package DC::UI::Frame;
1198 1223
1199our @ISA = CFPlus::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1200 1225
1201use CFPlus::OpenGL; 1226use DC::OpenGL;
1202 1227
1203sub new { 1228sub new {
1204 my $class = shift; 1229 my $class = shift;
1205 1230
1206 $class->SUPER::new ( 1231 $class->SUPER::new (
1225 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1226} 1251}
1227 1252
1228############################################################################# 1253#############################################################################
1229 1254
1230package CFPlus::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1231 1256
1232our @ISA = CFPlus::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1233 1258
1234use CFPlus::OpenGL; 1259use DC::OpenGL;
1235 1260
1236sub new { 1261sub new {
1237 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1238 1263
1239 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1240 $arg{label} = new CFPlus::UI::Label 1265 $arg{label} = new DC::UI::Label
1241 align => 1, 1266 align => 1,
1242 valign => 0, 1267 valign => 0.5,
1243 text => $arg{label}, 1268 text => $arg{label},
1244 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1245 } 1270 }
1246 1271
1247 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1248 # label => "", 1273 # label => "",
1249 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1250 border => 0.8, 1275 border => 0.8,
1251 style => 'single', 1276 style => 'single',
1252 %arg, 1277 %arg,
1253 ); 1278 );
1254 1279
1257 1282
1258sub add { 1283sub add {
1259 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1260 1285
1261 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1262 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1263} 1288}
1264 1289
1265sub border { 1290sub border {
1266 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1267} 1292}
1307 my $border = $self->border; 1332 my $border = $self->border;
1308 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1309 1334
1310 $child->draw; 1335 $child->draw;
1311 1336
1312 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1313 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1314 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1315 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1317 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1342 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1325 } 1350 }
1326} 1351}
1327 1352
1328############################################################################# 1353#############################################################################
1329 1354
1330package CFPlus::UI::Toplevel; 1355package DC::UI::Toplevel;
1331 1356
1332our @ISA = CFPlus::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1333 1358
1334use CFPlus::OpenGL; 1359use DC::OpenGL;
1335 1360
1336my $bg = 1361my $bg =
1337 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1338 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1339 1364
1340my @border = 1365my @border =
1341 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1342 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1367 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1343 1368
1344my @icon = 1369my @icon =
1345 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1370 map { new_from_resource DC::Texture $_, mipmap => 1 }
1346 qw(x1_move.png x1_resize.png); 1371 qw(x1_move.png x1_resize.png);
1347 1372
1348sub new { 1373sub new {
1349 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1350 1375
1351 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1352 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1353 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1354 border => 0.6, 1379 border => 0.8,
1355 can_events => 1, 1380 can_events => 1,
1356 min_w => 64, 1381 min_w => 64,
1357 min_h => 32, 1382 min_h => 32,
1358 %arg, 1383 %arg,
1359 ); 1384 );
1360 1385
1361 $self->{title_widget} = new CFPlus::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1362 align => 0, 1387 align => 0.5,
1363 valign => 1, 1388 valign => 1,
1364 text => $self->{title}, 1389 text => $self->{title},
1365 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1366 if exists $self->{title}; 1391 if exists $self->{title};
1367 1392
1368 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1369 $self->{close_button} = 1394 $self->{close_button} =
1370 new CFPlus::UI::ImageButton 1395 new DC::UI::ImageButton
1371 path => 'x1_close.png', 1396 path => 'x1_close.png',
1372 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1373 1398
1374 $self->CFPlus::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1375 } 1400 }
1376 1401
1377 $self 1402 $self
1378} 1403}
1379 1404
1380sub add { 1405sub add {
1381 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1382 1407
1383 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1384 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1409 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1385 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1410 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1386} 1411}
1387 1412
1388sub border { 1413sub border {
1389 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1390} 1415}
1391 1416
1392sub get_max_wh { 1417#sub get_max_wh {
1393 my ($self) = @_; 1418# my ($self) = @_;
1394 1419#
1395 return ($self->{w}, $self->{h}) 1420# return ($self->{w}, $self->{h})
1396 if $self->{visible} && $self->{w}; 1421# if $self->{visible} && $self->{w};
1397 1422#
1398 $self->SUPER::get_max_wh 1423# $self->SUPER::get_max_wh
1399} 1424#}
1400 1425
1401sub size_request { 1426sub size_request {
1402 my ($self) = @_; 1427 my ($self) = @_;
1403 1428
1404 $self->{title_widget}->size_request 1429 $self->{title_widget}->size_request
1439 1464
1440sub invoke_delete { 1465sub invoke_delete {
1441 my ($self) = @_; 1466 my ($self) = @_;
1442 1467
1443 $self->hide; 1468 $self->hide;
1444 1469
1445 1 1470 1
1446} 1471}
1447 1472
1448sub invoke_button_down { 1473sub invoke_button_down {
1449 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1450 1475
1451 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1452 my $border = $self->border; 1477 my $border = $self->border;
1453 1478
1454 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); 1479 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); # left-right
1455 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); 1480 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); # top-down
1456 1481
1457 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1458 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1459 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1460 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1461 1486
1462 my $mx = $x < $border; 1487 my $mx = $x < $border;
1466 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1467 1492
1468 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1469 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1470 1495
1496 $self->set_size (
1471 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1472 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1473 1500
1474 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1475 $self->realloc; 1502 $self->realloc;
1476 }; 1503 };
1477 1504
1478 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1479 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1480 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1481 1508
1482 $self->{motion} = sub { 1509 $self->{motion} = sub {
1483 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1528 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1529 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1530 1557
1531 my $border = $self->border; 1558 my $border = $self->border;
1532 1559
1560 if ($border) {
1533 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1534 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1535 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1536 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1537 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1565 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1538 1566
1539 # move 1567 # move
1540 my $w2 = ($w - $border) * .5; 1568 my $w2 = ($w - $border) * .5;
1541 my $h2 = ($h - $border) * .5; 1569 my $h2 = ($h - $border) * .5;
1542 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1570 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1543 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1571 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1572 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1545 1573
1546 # resize 1574 # resize
1547 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1548 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1576 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1549 unless $self->{has_close_button}; 1577 unless $self->{has_close_button};
1550 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1578 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1551 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1579 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1580 }
1552 1581
1553 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1554 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1555 1584
1556 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
1575 if $self->{close_button}; 1604 if $self->{close_button};
1576} 1605}
1577 1606
1578############################################################################# 1607#############################################################################
1579 1608
1580package CFPlus::UI::Table; 1609package DC::UI::Table;
1581 1610
1582our @ISA = CFPlus::UI::Container::; 1611our @ISA = DC::UI::Container::;
1583 1612
1584use List::Util qw(max sum); 1613use List::Util qw(max sum);
1585 1614
1586use CFPlus::OpenGL; 1615use DC::OpenGL;
1587 1616
1588sub new { 1617sub new {
1589 my $class = shift; 1618 my $class = shift;
1590 1619
1591 $class->SUPER::new ( 1620 $class->SUPER::new (
1635 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1664 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1636 1665
1637 my $sw = sum @w[$c .. $c + $cs - 1]; 1666 my $sw = sum @w[$c .. $c + $cs - 1];
1638 1667
1639 if ($w > $sw) { 1668 if ($w > $sw) {
1640 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1669 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1641 } 1670 }
1642 } 1671 }
1643 1672
1644 # second pass, rows 1673 # second pass, rows
1645 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1674 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1646 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1675 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1647 1676
1648 my $sh = sum @h[$r .. $r + $rs - 1]; 1677 my $sh = sum @h[$r .. $r + $rs - 1];
1649 1678
1650 if ($h > $sh) { 1679 if ($h > $sh) {
1651 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1680 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1652 } 1681 }
1653 } 1682 }
1654 1683
1655 (\@w, \@h) 1684 (\@w, \@h)
1656} 1685}
1679 @col_expand = (1) x @$ws unless @col_expand; 1708 @col_expand = (1) x @$ws unless @col_expand;
1680 my $col_expand = (sum @col_expand) || 1; 1709 my $col_expand = (sum @col_expand) || 1;
1681 1710
1682 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1711 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1683 1712
1684 CFPlus::UI::harmonize $ws; 1713 DC::UI::harmonize $ws;
1685 1714
1686 my @row_expand = @{$self->{row_expand}}; 1715 my @row_expand = @{$self->{row_expand}};
1687 @row_expand = (1) x @$ws unless @row_expand; 1716 @row_expand = (1) x @$ws unless @row_expand;
1688 my $row_expand = (sum @row_expand) || 1; 1717 my $row_expand = (sum @row_expand) || 1;
1689 1718
1690 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1719 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1691 1720
1692 CFPlus::UI::harmonize $hs; 1721 DC::UI::harmonize $hs;
1693 1722
1694 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1723 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1695 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1724 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1696 1725
1697 for my $widget ($self->children) { 1726 for my $widget ($self->children) {
1706 1 1735 1
1707} 1736}
1708 1737
1709############################################################################# 1738#############################################################################
1710 1739
1711package CFPlus::UI::Fixed; 1740package DC::UI::Fixed;
1712 1741
1713use List::Util qw(min max); 1742use List::Util qw(min max);
1714 1743
1715our @ISA = CFPlus::UI::Container::; 1744our @ISA = DC::UI::Container::;
1716 1745
1717sub _scale($$$) { 1746sub _scale($$$) {
1718 my ($rel, $val, $max) = @_; 1747 my ($rel, $val, $max) = @_;
1719 1748
1720 $rel ? $val * $max : $val 1749 $rel ? $val * $max : $val
1772 1 1801 1
1773} 1802}
1774 1803
1775############################################################################# 1804#############################################################################
1776 1805
1777package CFPlus::UI::Box; 1806package DC::UI::Box;
1778 1807
1779our @ISA = CFPlus::UI::Container::; 1808our @ISA = DC::UI::Container::;
1780 1809
1781sub size_request { 1810sub size_request {
1782 my ($self) = @_; 1811 my ($self) = @_;
1783 1812
1784 my @children = $self->visible_children; 1813 my @children = $self->visible_children;
1811 if ($req > $space) { 1840 if ($req > $space) {
1812 # ah well, not enough space 1841 # ah well, not enough space
1813 $_ *= $space / $req for @req; 1842 $_ *= $space / $req for @req;
1814 } else { 1843 } else {
1815 my $expand = (List::Util::sum map $_->{expand}, @children) || 1; 1844 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1816 1845
1817 $space = ($space - $req) / $expand; # remaining space to give away 1846 $space = ($space - $req) / $expand; # remaining space to give away
1818 1847
1819 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1820 for 0 .. $#children; 1849 for 0 .. $#children;
1821 } 1850 }
1822 } 1851 }
1823 1852
1824 CFPlus::UI::harmonize \@req; 1853 DC::UI::harmonize \@req;
1825 1854
1826 my $pos = 0; 1855 my $pos = 0;
1827 for (0 .. $#children) { 1856 for (0 .. $#children) {
1828 my $alloc = $req[$_]; 1857 my $alloc = $req[$_];
1829 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1858 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1834 1 1863 1
1835} 1864}
1836 1865
1837############################################################################# 1866#############################################################################
1838 1867
1839package CFPlus::UI::HBox; 1868package DC::UI::HBox;
1840 1869
1841our @ISA = CFPlus::UI::Box::; 1870our @ISA = DC::UI::Box::;
1842 1871
1843sub new { 1872sub new {
1844 my $class = shift; 1873 my $class = shift;
1845 1874
1846 $class->SUPER::new ( 1875 $class->SUPER::new (
1849 ) 1878 )
1850} 1879}
1851 1880
1852############################################################################# 1881#############################################################################
1853 1882
1854package CFPlus::UI::VBox; 1883package DC::UI::VBox;
1855 1884
1856our @ISA = CFPlus::UI::Box::; 1885our @ISA = DC::UI::Box::;
1857 1886
1858sub new { 1887sub new {
1859 my $class = shift; 1888 my $class = shift;
1860 1889
1861 $class->SUPER::new ( 1890 $class->SUPER::new (
1864 ) 1893 )
1865} 1894}
1866 1895
1867############################################################################# 1896#############################################################################
1868 1897
1869package CFPlus::UI::Label; 1898package DC::UI::Label;
1870 1899
1871our @ISA = CFPlus::UI::DrawBG::; 1900our @ISA = DC::UI::DrawBG::;
1872 1901
1873use CFPlus::OpenGL; 1902use DC::OpenGL;
1874 1903
1875sub new { 1904sub new {
1876 my ($class, %arg) = @_; 1905 my ($class, %arg) = @_;
1877 1906
1878 my $self = $class->SUPER::new ( 1907 my $self = $class->SUPER::new (
1883 #text => initial text 1912 #text => initial text
1884 #markup => initial narkup 1913 #markup => initial narkup
1885 #max_w => maximum pixel width 1914 #max_w => maximum pixel width
1886 #style => 0, # render flags 1915 #style => 0, # render flags
1887 ellipsise => 3, # end 1916 ellipsise => 3, # end
1888 layout => (new CFPlus::Layout), 1917 layout => (new DC::Layout),
1889 fontsize => 1, 1918 fontsize => 1,
1890 align => -1, 1919 align => 0.5,
1891 valign => -1, 1920 valign => 0.5,
1892 padding_x => 2, 1921 padding_x => 4,
1893 padding_y => 2, 1922 padding_y => 2,
1894 can_events => 0, 1923 can_events => 0,
1895 %arg 1924 %arg
1896 ); 1925 );
1897 1926
1898 if (exists $self->{template}) { 1927 if (exists $self->{template}) {
1899 my $layout = new CFPlus::Layout; 1928 my $layout = new DC::Layout;
1900 $layout->set_text (delete $self->{template}); 1929 $layout->set_text (delete $self->{template});
1901 $self->{template} = $layout; 1930 $self->{template} = $layout;
1902 } 1931 }
1903 1932
1904 if (exists $self->{markup}) { 1933 if (exists $self->{markup}) {
1960 1989
1961sub size_request { 1990sub size_request {
1962 my ($self) = @_; 1991 my ($self) = @_;
1963 1992
1964 $self->{size_req} ||= do { 1993 $self->{size_req} ||= do {
1965 my ($max_w, $max_h) = $self->get_max_wh; 1994 my $max_w = $self->{w} || $self->{max_w} || 0x0fffffff; # actually 2**31-1 but allow for overflow inside pango
1966 1995
1967 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
1968 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1997 $self->{layout}->set_width ($max_w);
1969 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
1970 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1971 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2000 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1972 2001
1973 my ($w, $h) = $self->{layout}->size; 2002 my ($w, $h) = $self->{layout}->size;
1974 2003
1975 if (exists $self->{template}) { 2004 if (exists $self->{template}) {
1976 $self->{template}->set_font ($self->{font}) if $self->{font}; 2005 $self->{template}->set_font ($self->{font}) if $self->{font};
1977 $self->{template}->set_width ($self->{max_w} || -1); 2006 $self->{template}->set_width ($max_w);
1978 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2007 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1979 2008
1980 my ($w2, $h2) = $self->{template}->size; 2009 my ($w2, $h2) = $self->{template}->size;
1981 2010
1982 $w = List::Util::max $w, $w2; 2011 $w = List::Util::max $w, $w2;
2038 2067
2039 [$self->{layout}->size] 2068 [$self->{layout}->size]
2040 }; 2069 };
2041 2070
2042 unless (exists $self->{ox}) { 2071 unless (exists $self->{ox}) {
2043 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2072 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2044 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2073 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2045 : ($self->{w} - $size->[0]) * 0.5);
2046
2047 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2048 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2049 : ($self->{h} - $size->[1]) * 0.5);
2050 2074
2051 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2075 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052 }; 2076 };
2053 2077
2054# unless ($self->{list}) { 2078# unless ($self->{list}) {
2055# $self->{list} = CFPlus::OpenGL::glGenList; 2079# $self->{list} = DC::OpenGL::glGenList;
2056# CFPlus::OpenGL::glNewList $self->{list}; 2080# DC::OpenGL::glNewList $self->{list};
2057# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2081# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2058# CFPlus::OpenGL::glEndList; 2082# DC::OpenGL::glEndList;
2059# } 2083# }
2060# 2084#
2061# CFPlus::OpenGL::glCallList $self->{list}; 2085# DC::OpenGL::glCallList $self->{list};
2062 2086
2063 $self->{layout}->draw; 2087 $self->{layout}->draw;
2064} 2088}
2065 2089
2066#sub destroy { 2090#sub destroy {
2067# my ($self) = @_; 2091# my ($self) = @_;
2068# 2092#
2069# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2093# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2070# 2094#
2071# $self->SUPER::destroy; 2095# $self->SUPER::destroy;
2072#} 2096#}
2073 2097
2074############################################################################# 2098#############################################################################
2075 2099
2076package CFPlus::UI::EntryBase; 2100package DC::UI::EntryBase;
2077 2101
2078our @ISA = CFPlus::UI::Label::; 2102our @ISA = DC::UI::Label::;
2079 2103
2080use CFPlus::OpenGL; 2104use DC::OpenGL;
2081 2105
2082sub new { 2106sub new {
2083 my $class = shift; 2107 my $class = shift;
2084 2108
2085 $class->SUPER::new ( 2109 $class->SUPER::new (
2086 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2087 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2088 outline => [0.6, 0.3, 0.1], 2112 outline => undef,
2089 active_bg => [0, 0, 1, .2], 2113 active_bg => [0, 0, 1, .2],
2090 active_fg => [1, 1, 1], 2114 active_fg => [1, 1, 1],
2091 active_outline => [1, 1, 0], 2115 active_outline => [1, 1, 0],
2092 can_hover => 1, 2116 can_hover => 1,
2093 can_focus => 1, 2117 can_focus => 1,
2118 align => 0,
2094 valign => 0, 2119 valign => 0.5,
2095 can_events => 1, 2120 can_events => 1,
2096 ellipsise => 0, 2121 ellipsise => 0,
2122 padding_x => 4,
2123 padding_y => 2,
2097 #text => ... 2124 #text => ...
2098 #hidden => "*", 2125 #hidden => "*",
2099 @_ 2126 @_
2100 ) 2127 )
2101} 2128}
2148 2175
2149 my $text = $self->get_text; 2176 my $text = $self->get_text;
2150 2177
2151 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2178 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2152 2179
2153 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2154 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2155 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2156 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2157 } elsif ($sym == CFPlus::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2158 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2159 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2160 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2161 } elsif ($sym == CFPlus::SDLK_HOME) { 2188 } elsif ($sym == DC::SDLK_HOME) {
2162 # what a hack 2189 # what a hack
2163 $self->{cursor} = 2190 $self->{cursor} =
2164 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2191 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2165 ? length $1 2192 ? length $1
2166 : 0; 2193 : 0;
2167 } elsif ($sym == CFPlus::SDLK_END) { 2194 } elsif ($sym == DC::SDLK_END) {
2168 # uh, again 2195 # uh, again
2169 $self->{cursor} = 2196 $self->{cursor} =
2170 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2197 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2171 ? $self->{cursor} + length $1 2198 ? $self->{cursor} + length $1
2172 : length $self->{text}; 2199 : length $self->{text};
2211 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; 2238 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2212 $self->{cursor} = length $text; 2239 $self->{cursor} = length $text;
2213 2240
2214 $self->_set_text ($self->{text}); 2241 $self->_set_text ($self->{text});
2215 $self->update; 2242 $self->update;
2216 2243
2217 1 2244 1
2218} 2245}
2219 2246
2220sub invoke_mouse_motion { 2247sub invoke_mouse_motion {
2221 my ($self, $ev, $x, $y) = @_; 2248 my ($self, $ev, $x, $y) = @_;
2263 glColor @{$self->{active_outline}}; 2290 glColor @{$self->{active_outline}};
2264 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2291 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2265 glLineWidth 1; 2292 glLineWidth 1;
2266 2293
2267 } else { 2294 } else {
2268 glColor @{$self->{outline}}; 2295 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2269 glBegin GL_LINE_STRIP; 2296 glBegin GL_LINE_STRIP;
2270 glVertex .5, $self->{h} * .5; 2297 glVertex .5, $self->{h} * .5;
2271 glVertex .5, $self->{h} - 2.5; 2298 glVertex .5, $self->{h} - 2.5;
2272 glVertex $self->{w} - .5, $self->{h} - 2.5; 2299 glVertex $self->{w} - .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} * .5; 2300 glVertex $self->{w} - .5, $self->{h} * .5;
2275 } 2302 }
2276} 2303}
2277 2304
2278############################################################################# 2305#############################################################################
2279 2306
2280package CFPlus::UI::Entry; 2307package DC::UI::Entry;
2281 2308
2282our @ISA = CFPlus::UI::EntryBase::; 2309our @ISA = DC::UI::EntryBase::;
2283 2310
2284use CFPlus::OpenGL; 2311use DC::OpenGL;
2312
2313sub new {
2314 my $class = shift;
2315
2316 $class->SUPER::new (
2317 history_pointer => -1,
2318 @_
2319 )
2320}
2321
2285 2322
2286sub invoke_key_down { 2323sub invoke_key_down {
2287 my ($self, $ev) = @_; 2324 my ($self, $ev) = @_;
2288 2325
2289 my $sym = $ev->{sym}; 2326 my $sym = $ev->{sym};
2295 $self->{history_pointer} = -1; 2332 $self->{history_pointer} = -1;
2296 $self->{history_saveback} = ''; 2333 $self->{history_saveback} = '';
2297 $self->emit (activate => $txt); 2334 $self->emit (activate => $txt);
2298 $self->update; 2335 $self->update;
2299 2336
2300 } elsif ($sym == CFPlus::SDLK_UP) { 2337 } elsif ($sym == DC::SDLK_UP) {
2301 if ($self->{history_pointer} < 0) { 2338 if ($self->{history_pointer} < 0) {
2302 $self->{history_saveback} = $self->get_text; 2339 $self->{history_saveback} = $self->get_text;
2303 } 2340 }
2304 if (@{$self->{history} || []} > 0) { 2341 if (@{$self->{history} || []} > 0) {
2305 $self->{history_pointer}++; 2342 $self->{history_pointer}++;
2307 $self->{history_pointer} = @{$self->{history} || []} - 1; 2344 $self->{history_pointer} = @{$self->{history} || []} - 1;
2308 } 2345 }
2309 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2346 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2310 } 2347 }
2311 2348
2312 } elsif ($sym == CFPlus::SDLK_DOWN) { 2349 } elsif ($sym == DC::SDLK_DOWN) {
2313 $self->{history_pointer}--; 2350 $self->{history_pointer}--;
2314 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2351 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2315 2352
2316 if ($self->{history_pointer} >= 0) { 2353 if ($self->{history_pointer} >= 0) {
2317 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2354 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2318 } else { 2355 } else {
2356 if (defined $self->{history_saveback}) {
2319 $self->set_text ($self->{history_saveback}); 2357 $self->set_text ($self->{history_saveback});
2358 $self->{history_saveback} = undef;
2359 }
2320 } 2360 }
2321 2361
2322 } else { 2362 } else {
2323 return $self->SUPER::invoke_key_down ($ev) 2363 return $self->SUPER::invoke_key_down ($ev)
2324 } 2364 }
2326 1 2366 1
2327} 2367}
2328 2368
2329############################################################################# 2369#############################################################################
2330 2370
2331package CFPlus::UI::TextEdit; 2371package DC::UI::TextEdit;
2332 2372
2333our @ISA = CFPlus::UI::EntryBase::; 2373our @ISA = DC::UI::EntryBase::;
2334 2374
2335use CFPlus::OpenGL; 2375use DC::OpenGL;
2376
2377sub new {
2378 my $class = shift;
2379
2380 $class->SUPER::new (
2381 padding_y => 4,
2382
2383 @_
2384 )
2385}
2336 2386
2337sub move_cursor_ver { 2387sub move_cursor_ver {
2338 my ($self, $dy) = @_; 2388 my ($self, $dy) = @_;
2339 2389
2340 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2390 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2341 2391
2342 $y += $dy; 2392 $line += $dy;
2343 2393
2344 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2394 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2345 $self->{cursor} = $index; 2395 $self->{cursor} = $index;
2346 delete $self->{cur_h}; 2396 delete $self->{cur_h};
2347 $self->update; 2397 $self->update;
2348 return; 2398 return;
2349 } 2399 }
2352sub invoke_key_down { 2402sub invoke_key_down {
2353 my ($self, $ev) = @_; 2403 my ($self, $ev) = @_;
2354 2404
2355 my $sym = $ev->{sym}; 2405 my $sym = $ev->{sym};
2356 2406
2357 if ($sym == CFPlus::SDLK_UP) { 2407 if ($sym == DC::SDLK_UP) {
2358 $self->move_cursor_ver (-1); 2408 $self->move_cursor_ver (-1);
2359 } elsif ($sym == CFPlus::SDLK_DOWN) { 2409 } elsif ($sym == DC::SDLK_DOWN) {
2360 $self->move_cursor_ver (+1); 2410 $self->move_cursor_ver (+1);
2361 } else { 2411 } else {
2362 return $self->SUPER::invoke_key_down ($ev) 2412 return $self->SUPER::invoke_key_down ($ev)
2363 } 2413 }
2364 2414
2365 1 2415 1
2366} 2416}
2367 2417
2368############################################################################# 2418#############################################################################
2369 2419
2370package CFPlus::UI::ButtonBin; 2420package DC::UI::ButtonBin;
2371 2421
2372our @ISA = CFPlus::UI::Bin::; 2422our @ISA = DC::UI::Bin::;
2373 2423
2374use CFPlus::OpenGL; 2424use DC::OpenGL;
2375 2425
2376my @tex = 2426my @tex =
2377 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2378 qw(b1_button_inactive.png b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2379 2429
2380sub new { 2430sub new {
2381 my $class = shift; 2431 my $class = shift;
2382 2432
2383 $class->SUPER::new ( 2433 $class->SUPER::new (
2384 can_hover => 1, 2434 can_hover => 1,
2385 align => 0, 2435 align => 0.5,
2386 valign => 0, 2436 valign => 0.5,
2387 can_events => 1, 2437 can_events => 1,
2388 @_ 2438 @_
2389 ) 2439 )
2390} 2440}
2391 2441
2414 $self->SUPER::_draw; 2464 $self->SUPER::_draw;
2415} 2465}
2416 2466
2417############################################################################# 2467#############################################################################
2418 2468
2419package CFPlus::UI::Button; 2469package DC::UI::Button;
2420 2470
2421our @ISA = CFPlus::UI::Label::; 2471our @ISA = DC::UI::Label::;
2422 2472
2423use CFPlus::OpenGL; 2473use DC::OpenGL;
2424 2474
2425my @tex = 2475my @tex =
2426 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2427 qw(b1_button_inactive.png b1_button_active.png); 2477 qw(b1_button_inactive.png b1_button_active.png);
2428 2478
2429sub new { 2479sub new {
2430 my $class = shift; 2480 my $class = shift;
2431 2481
2432 $class->SUPER::new ( 2482 $class->SUPER::new (
2433 padding_x => 4, 2483 padding_x => 8,
2434 padding_y => 4, 2484 padding_y => 4,
2435 fg => [1.0, 1.0, 1.0], 2485 fg => [1.0, 1.0, 1.0],
2436 active_fg => [0.8, 0.8, 0.8], 2486 active_fg => [0.8, 0.8, 0.8],
2437 can_hover => 1, 2487 can_hover => 1,
2438 align => 0, 2488 align => 0.5,
2439 valign => 0, 2489 valign => 0.5,
2440 can_events => 1, 2490 can_events => 1,
2441 @_ 2491 @_
2442 ) 2492 )
2443} 2493}
2444 2494
2469 $self->SUPER::_draw; 2519 $self->SUPER::_draw;
2470} 2520}
2471 2521
2472############################################################################# 2522#############################################################################
2473 2523
2474package CFPlus::UI::CheckBox; 2524package DC::UI::CheckBox;
2475 2525
2476our @ISA = CFPlus::UI::DrawBG::; 2526our @ISA = DC::UI::DrawBG::;
2477 2527
2478my @tex = 2528my @tex =
2479 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2529 map { new_from_resource DC::Texture $_, mipmap => 1 }
2480 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2530 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2481 2531
2482use CFPlus::OpenGL; 2532use DC::OpenGL;
2483 2533
2484sub new { 2534sub new {
2485 my $class = shift; 2535 my $class = shift;
2486 2536
2487 $class->SUPER::new ( 2537 $class->SUPER::new (
2538 fontsize => 1,
2488 padding_x => 2, 2539 padding_x => 2,
2489 padding_y => 2, 2540 padding_y => 2,
2490 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2491 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2492 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2498} 2549}
2499 2550
2500sub size_request { 2551sub size_request {
2501 my ($self) = @_; 2552 my ($self) = @_;
2502 2553
2503 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2504} 2555}
2505 2556
2506sub toggle { 2557sub toggle {
2507 my ($self) = @_; 2558 my ($self) = @_;
2508 2559
2544 glDisable GL_TEXTURE_2D; 2595 glDisable GL_TEXTURE_2D;
2545} 2596}
2546 2597
2547############################################################################# 2598#############################################################################
2548 2599
2549package CFPlus::UI::Image; 2600package DC::UI::Image;
2550 2601
2551our @ISA = CFPlus::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2552 2603
2553use CFPlus::OpenGL; 2604use DC::OpenGL;
2554 2605
2555our %texture_cache; 2606our %texture_cache;
2556 2607
2557sub new { 2608sub new {
2558 my $class = shift; 2609 my $class = shift;
2565 2616
2566 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2567 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2568 2619
2569 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2570 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2571 2622
2572 CFPlus::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2573 2624
2574 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2575 2626
2576 $self 2627 $self
2577} 2628}
2578 2629
2579sub STORABLE_freeze { 2630sub STORABLE_freeze {
2580 my ($self, $cloning) = @_; 2631 my ($self, $cloning) = @_;
2581 2632
2582 $self->{path} 2633 $self->{path}
2583 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2634 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2584 2635
2585 $self->{path} 2636 $self->{path}
2586} 2637}
2587 2638
2588sub STORABLE_attach { 2639sub STORABLE_attach {
2589 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2590 2641
2591 $self->new (path => $path) 2642 $self->new (path => $path)
2592} 2643}
2593 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2594sub size_request { 2652sub size_request {
2595 my ($self) = @_; 2653 my ($self) = @_;
2596 2654
2597 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2655 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2598} 2656}
2599 2657
2600sub _draw { 2658sub _draw {
2601 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2602 2662
2603 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2604 2664
2605 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2606 2666
2619 glDisable GL_TEXTURE_2D; 2679 glDisable GL_TEXTURE_2D;
2620} 2680}
2621 2681
2622############################################################################# 2682#############################################################################
2623 2683
2624package CFPlus::UI::ImageButton; 2684package DC::UI::ImageButton;
2625 2685
2626our @ISA = CFPlus::UI::Image::; 2686our @ISA = DC::UI::Image::;
2627 2687
2628use CFPlus::OpenGL; 2688use DC::OpenGL;
2629
2630my %textures;
2631 2689
2632sub new { 2690sub new {
2633 my $class = shift; 2691 my $class = shift;
2634 2692
2635 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2636 padding_x => 4, 2694 padding_x => 4,
2637 padding_y => 4, 2695 padding_y => 4,
2638 fg => [1, 1, 1], 2696 fg => [1, 1, 1],
2639 active_fg => [0, 0, 1], 2697 active_fg => [0, 0, 1],
2640 can_hover => 1, 2698 can_hover => 1,
2641 align => 0, 2699 align => 0.5,
2642 valign => 0, 2700 valign => 0.5,
2643 can_events => 1, 2701 can_events => 1,
2644 @_ 2702 @_
2645 ); 2703 );
2646} 2704}
2647 2705
2661 1 2719 1
2662} 2720}
2663 2721
2664############################################################################# 2722#############################################################################
2665 2723
2666package CFPlus::UI::VGauge; 2724package DC::UI::VGauge;
2667 2725
2668our @ISA = CFPlus::UI::Base::; 2726our @ISA = DC::UI::Base::;
2669 2727
2670use List::Util qw(min max); 2728use List::Util qw(min max);
2671 2729
2672use CFPlus::OpenGL; 2730use DC::OpenGL;
2673 2731
2674my %tex = ( 2732my %tex = (
2675 food => [ 2733 food => [
2676 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2677 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2678 ], 2736 ],
2679 grace => [ 2737 grace => [
2680 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2739 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2682 ], 2740 ],
2683 hp => [ 2741 hp => [
2684 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2685 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2686 ], 2744 ],
2687 mana => [ 2745 mana => [
2688 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2747 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2690 ], 2748 ],
2691); 2749);
2692 2750
2693# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
2796 glDisable GL_TEXTURE_2D; 2854 glDisable GL_TEXTURE_2D;
2797} 2855}
2798 2856
2799############################################################################# 2857#############################################################################
2800 2858
2801package CFPlus::UI::Progress; 2859package DC::UI::Progress;
2802 2860
2803our @ISA = CFPlus::UI::Label::; 2861our @ISA = DC::UI::Label::;
2804 2862
2805use CFPlus::OpenGL; 2863use DC::OpenGL;
2806 2864
2807sub new { 2865sub new {
2808 my ($class, %arg) = @_; 2866 my ($class, %arg) = @_;
2809 2867
2810 my $self = $class->SUPER::new ( 2868 my $self = $class->SUPER::new (
2869 padding_x => 2,
2870 padding_y => 2,
2811 fg => [1, 1, 1], 2871 fg => [1, 1, 1],
2812 bg => [0, 0, 1, 0.2], 2872 bg => [0, 0, 1, 0.2],
2813 bar => [0.7, 0.5, 0.1, 0.8], 2873 bar => [0.7, 0.5, 0.1, 0.8],
2814 outline => [0.4, 0.3, 0], 2874 outline => [0.4, 0.3, 0],
2815 fontsize => 0.9, 2875 fontsize => 0.9,
2816 valign => 0, 2876 valign => 0.5,
2817 align => 0, 2877 align => 0.5,
2818 can_events => 1, 2878 can_events => 1,
2819 ellipsise => 1, 2879 ellipsise => 1,
2820 label => "%d%%", 2880 label => "%d%%",
2821 %arg, 2881 %arg,
2822 ); 2882 );
2830 my ($self, $label) = @_; 2890 my ($self, $label) = @_;
2831 2891
2832 return if $self->{label} eq $label; 2892 return if $self->{label} eq $label;
2833 $self->{label} = $label; 2893 $self->{label} = $label;
2834 2894
2835 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2895 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2836} 2896}
2837 2897
2838sub set_value { 2898sub set_value {
2839 my ($self, $value) = @_; 2899 my ($self, $value) = @_;
2840 2900
2855 my ($self) = @_; 2915 my ($self) = @_;
2856 2916
2857 glEnable GL_BLEND; 2917 glEnable GL_BLEND;
2858 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2918 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2859 2919
2920 my $px = $self->{padding_x};
2921 my $py = $self->{padding_y};
2922
2860 if ($self->{value} >= 0) { 2923 if ($self->{value} >= 0) {
2861 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2924 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2862 2925
2863 glColor_premultiply @{$self->{bar}}; 2926 glColor_premultiply @{$self->{bar}};
2864 glRect 2, 2, $s, $self->{h} - 2; 2927 glRect $px, $py, $s, $self->{h} - $py;
2865 glColor_premultiply @{$self->{bg}}; 2928 glColor_premultiply @{$self->{bg}};
2866 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2929 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2867 } 2930 }
2868 2931
2869 glColor_premultiply @{$self->{outline}}; 2932 glColor_premultiply @{$self->{outline}};
2933
2934 $px -= .5;
2935 $py -= .5;
2936
2870 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2937 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2871 2938
2872 glDisable GL_BLEND; 2939 glDisable GL_BLEND;
2873 2940
2874 { 2941 {
2875 local $self->{bg}; # do not draw background 2942 local $self->{bg}; # do not draw background
2877 } 2944 }
2878} 2945}
2879 2946
2880############################################################################# 2947#############################################################################
2881 2948
2882package CFPlus::UI::ExperienceProgress; 2949package DC::UI::ExperienceProgress;
2883 2950
2884our @ISA = CFPlus::UI::Progress::; 2951our @ISA = DC::UI::Progress::;
2885 2952
2886sub new { 2953sub new {
2887 my ($class, %arg) = @_; 2954 my ($class, %arg) = @_;
2888 2955
2956 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2957
2889 my $self = $class->SUPER::new ( 2958 my $self = $class->SUPER::new (
2959 %arg,
2890 tooltip => sub { 2960 tooltip => sub {
2891 my ($self) = @_; 2961 my ($self) = @_;
2892 2962
2893 sprintf "level %d\n%s points\n%s next level\n%s to go", 2963 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2964 $tt,
2894 $self->{lvl}, 2965 $self->{lvl},
2895 ::formsep ($self->{exp}), 2966 ::formsep ($self->{exp}),
2896 ::formsep ($self->{nxt}), 2967 ::formsep ($self->{nxt}),
2897 ::formsep ($self->{nxt} - $self->{exp}), 2968 ::formsep ($self->{nxt} - $self->{exp}),
2969 $self->_percent * 100,
2898 }, 2970 },
2899 %arg
2900 ); 2971 );
2901 2972
2902 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2973 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2903 if $::CONN; 2974 if $::CONN;
2904 2975
2912 if $::CONN; 2983 if $::CONN;
2913 2984
2914 $self->SUPER::DESTROY; 2985 $self->SUPER::DESTROY;
2915} 2986}
2916 2987
2988sub _percent {
2989 my ($self) = @_;
2990
2991 my $table = $::CONN && $::CONN->{exp_table}
2992 or return -1;
2993
2994 my $l0 = $table->[$self->{lvl} - 1];
2995 my $l1 = $table->[$self->{lvl}];
2996
2997 $self->{nxt} = $l1;
2998
2999 ($self->{exp} - $l0) / ($l1 - $l0)
3000}
3001
2917sub set_value { 3002sub set_value {
2918 my ($self, $lvl, $exp) = @_; 3003 my ($self, $lvl, $exp) = @_;
2919 3004
2920 $self->{lvl} = $lvl; 3005 $self->{lvl} = $lvl;
2921 $self->{exp} = $exp; 3006 $self->{exp} = $exp;
2922 3007
2923 my $v = -1;
2924
2925 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2926 my $l0 = $table->[$lvl - 1];
2927 my $l1 = $table->[$lvl];
2928
2929 $self->{nxt} = $l1;
2930
2931 $v = ($exp - $l0) / ($l1 - $l0);
2932 }
2933
2934 $self->SUPER::set_value ($v); 3008 $self->SUPER::set_value ($self->_percent);
2935} 3009}
2936 3010
2937############################################################################# 3011#############################################################################
2938 3012
2939package CFPlus::UI::Gauge; 3013package DC::UI::Gauge;
2940 3014
2941our @ISA = CFPlus::UI::VBox::; 3015our @ISA = DC::UI::VBox::;
2942 3016
2943sub new { 3017sub new {
2944 my ($class, %arg) = @_; 3018 my ($class, %arg) = @_;
2945 3019
2946 my $self = $class->SUPER::new ( 3020 my $self = $class->SUPER::new (
2948 can_hover => 1, 3022 can_hover => 1,
2949 can_events => 1, 3023 can_events => 1,
2950 %arg, 3024 %arg,
2951 ); 3025 );
2952 3026
2953 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3027 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2954 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3028 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2955 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3029 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2956 3030
2957 $self 3031 $self
2958} 3032}
2959 3033
2960sub set_fontsize { 3034sub set_fontsize {
2981 $self->{value}->set_text ($val); 3055 $self->{value}->set_text ($val);
2982} 3056}
2983 3057
2984############################################################################# 3058#############################################################################
2985 3059
2986package CFPlus::UI::Slider; 3060package DC::UI::Slider;
2987 3061
2988use strict; 3062use common::sense;
2989 3063
2990use CFPlus::OpenGL; 3064use DC::OpenGL;
2991 3065
2992our @ISA = CFPlus::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
2993 3067
2994my @tex = 3068my @tex =
2995 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
2996 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
2997 3071
2998sub new { 3072sub new {
2999 my $class = shift; 3073 my $class = shift;
3000 3074
3037sub set_value { 3111sub set_value {
3038 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
3039 3113
3040 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3041 3115
3042 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
3043 3117
3044 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
3045
3046 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
3047 $value = $hi - $page if $value > $hi - $page;
3048 3120
3049 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3050 if $unit; 3122 if $unit;
3051 3123
3052 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3067 my ($self, $ev, $x, $y) = @_; 3139 my ($self, $ev, $x, $y) = @_;
3068 3140
3069 $self->SUPER::invoke_button_down ($ev, $x, $y); 3141 $self->SUPER::invoke_button_down ($ev, $x, $y);
3070 3142
3071 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3143 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
3072 3144
3073 $self->invoke_mouse_motion ($ev, $x, $y); 3145 $self->invoke_mouse_motion ($ev, $x, $y);
3074 3146
3075 1 3147 1
3076} 3148}
3077 3149
3081 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
3082 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3083 3155
3084 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
3085 3157
3086 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3087 3159
3088 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3089 } else { 3161 } else {
3090 return 0; 3162 return 0;
3091 } 3163 }
3096sub invoke_mouse_wheel { 3168sub invoke_mouse_wheel {
3097 my ($self, $ev) = @_; 3169 my ($self, $ev) = @_;
3098 3170
3099 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3171 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3100 3172
3101 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3173 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3102 3174
3103 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3175 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3104 3176
3105 1 3177 1
3106} 3178}
3116 my ($self) = @_; 3188 my ($self) = @_;
3117 3189
3118 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
3119 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
3120 3192
3121 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3122 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
3123 3195
3124 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3196 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3125 3197
3126 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3198 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3127 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3128 3200
3129 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
3137 3209
3138 glScale $self->{w}, $self->{h}; 3210 glScale $self->{w}, $self->{h};
3139 3211
3140 if ($self->{vertical}) { 3212 if ($self->{vertical}) {
3141 # draw a vertical slider like a rotated horizontal slider 3213 # draw a vertical slider like a rotated horizontal slider
3142 3214
3143 glTranslate 1, 0, 0; 3215 glTranslate 1, 0, 0;
3144 glRotate 90, 0, 0, 1; 3216 glRotate 90, 0, 0, 1;
3145 } 3217 }
3146 3218
3147 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3219 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
3159 glDisable GL_TEXTURE_2D; 3231 glDisable GL_TEXTURE_2D;
3160} 3232}
3161 3233
3162############################################################################# 3234#############################################################################
3163 3235
3164package CFPlus::UI::ValSlider; 3236package DC::UI::ValSlider;
3165 3237
3166our @ISA = CFPlus::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
3167 3239
3168sub new { 3240sub new {
3169 my ($class, %arg) = @_; 3241 my ($class, %arg) = @_;
3170 3242
3171 my $range = delete $arg{range}; 3243 my $range = delete $arg{range};
3172 3244
3173 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
3174 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3246 slider => (new DC::UI::Slider expand => 1, range => $range),
3175 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3247 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3176 to_value => sub { shift }, 3248 to_value => sub { shift },
3177 from_value => sub { shift }, 3249 from_value => sub { shift },
3178 %arg, 3250 %arg,
3179 ); 3251 );
3180 3252
3200sub set_range { shift->{slider}->set_range (@_) } 3272sub set_range { shift->{slider}->set_range (@_) }
3201sub set_value { shift->{slider}->set_value (@_) } 3273sub set_value { shift->{slider}->set_value (@_) }
3202 3274
3203############################################################################# 3275#############################################################################
3204 3276
3205package CFPlus::UI::TextScroller; 3277package DC::UI::TextScroller;
3206 3278
3207our @ISA = CFPlus::UI::HBox::; 3279our @ISA = DC::UI::HBox::;
3208 3280
3209use CFPlus::OpenGL; 3281use DC::OpenGL;
3210 3282
3211sub new { 3283sub new {
3212 my $class = shift; 3284 my $class = shift;
3213 3285
3214 my $self = $class->SUPER::new ( 3286 my $self = $class->SUPER::new (
3215 fontsize => 1, 3287 fontsize => 1,
3216 can_events => 1, 3288 can_events => 1,
3217 indent => 0, 3289 indent => 0,
3218 #font => default_font 3290 #font => default_font
3219 @_, 3291 @_,
3220 3292
3221 layout => (new CFPlus::Layout), 3293 layout => (new DC::Layout),
3222 par => [],
3223 max_par => 0, 3294 max_par => 0,
3224 height => 0, 3295 height => 0,
3225 children => [ 3296 children => [
3226 (new CFPlus::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3227 (new CFPlus::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3228 ], 3299 ],
3229 ); 3300 );
3230 3301
3231 $self->{children}[1]->connect (changed => sub { $self->update }); 3302 $self->{children}[1]->connect (changed => sub { $self->update });
3303
3304 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3232 3305
3233 $self 3306 $self
3234} 3307}
3235 3308
3236sub set_fontsize { 3309sub set_fontsize {
3288 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3361 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3289 $layout->set_markup ($para->{markup}); 3362 $layout->set_markup ($para->{markup});
3290 3363
3291 $layout->set_shapes ( 3364 $layout->set_shapes (
3292 map 3365 map
3293 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3366 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3294 @{$para->{widget}} 3367 @{$para->{widget}}
3295 ); 3368 );
3296 3369
3297 $layout 3370 $layout
3298} 3371}
3415 $ROOT->on_post_alloc ($self => sub { 3488 $ROOT->on_post_alloc ($self => sub {
3416 $self->force_uptodate; 3489 $self->force_uptodate;
3417 3490
3418 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3491 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3419 3492
3420 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3493 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3421 glClearColor 0, 0, 0, 0; 3494 glClearColor 0, 0, 0, 0;
3422 glClear GL_COLOR_BUFFER_BIT; 3495 glClear GL_COLOR_BUFFER_BIT;
3423 3496
3424 package CFPlus::UI::Base; 3497 package DC::UI::Base;
3425 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3498 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3426 (0, 0, $self->{w}, $self->{h}); 3499 (0, 0, $self->{w}, $self->{h});
3427 3500
3428 my $top = int $self->{children}[1]{range}[0]; 3501 my $top = int $self->{children}[1]{range}[0];
3429 3502
3488 $self->{children}[1]->draw; 3561 $self->{children}[1]->draw;
3489} 3562}
3490 3563
3491############################################################################# 3564#############################################################################
3492 3565
3493package CFPlus::UI::Animator; 3566package DC::UI::Animator;
3494 3567
3495use CFPlus::OpenGL; 3568use DC::OpenGL;
3496 3569
3497our @ISA = CFPlus::UI::Bin::; 3570our @ISA = DC::UI::Bin::;
3498 3571
3499sub moveto { 3572sub moveto {
3500 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3501 3574
3502 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3503 $self->{speed} = 0.001; 3576 $self->{speed} = 0.001;
3504 $self->{time} = 1; 3577 $self->{time} = 1;
3505 3578
3506 ::animation_start $self; 3579 ::animation_start $self;
3507} 3580}
3508 3581
3509sub animate { 3582sub animate {
3510 my ($self, $interval) = @_; 3583 my ($self, $interval) = @_;
3514 $self->{time} = 0; 3587 $self->{time} = 0;
3515 ::animation_stop $self; 3588 ::animation_stop $self;
3516 } 3589 }
3517 3590
3518 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; 3591 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3519 3592
3520 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); 3593 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3521 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); 3594 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3522} 3595}
3523 3596
3524sub _draw { 3597sub _draw {
3530 glPopMatrix; 3603 glPopMatrix;
3531} 3604}
3532 3605
3533############################################################################# 3606#############################################################################
3534 3607
3535package CFPlus::UI::Flopper; 3608package DC::UI::Flopper;
3536 3609
3537our @ISA = CFPlus::UI::Button::; 3610our @ISA = DC::UI::Button::;
3538 3611
3539sub new { 3612sub new {
3540 my $class = shift; 3613 my $class = shift;
3541 3614
3542 my $self = $class->SUPER::new ( 3615 my $self = $class->SUPER::new (
3554 $self->{other}->toggle_visibility; 3627 $self->{other}->toggle_visibility;
3555} 3628}
3556 3629
3557############################################################################# 3630#############################################################################
3558 3631
3559package CFPlus::UI::Tooltip; 3632package DC::UI::Tooltip;
3560 3633
3561our @ISA = CFPlus::UI::Bin::; 3634our @ISA = DC::UI::Bin::;
3562 3635
3563use CFPlus::OpenGL; 3636use DC::OpenGL;
3564 3637
3565sub new { 3638sub new {
3566 my $class = shift; 3639 my $class = shift;
3567 3640
3568 $class->SUPER::new ( 3641 $class->SUPER::new (
3569 @_, 3642 @_,
3570 can_events => 0, 3643 can_events => 0,
3571 ) 3644 )
3572} 3645}
3573 3646
3647# expand, as good as possible
3648sub _expand_doclets {
3649 my ($tip) = @_;
3650
3651 $tip =~ s{#\(([^)]+)\)}{
3652 if ($::CONN) {
3653 exists $::CONN->{doclet}{$1}
3654 ? $::CONN->{doclet}{$1}
3655 : "(waiting for server to show full text)"
3656 } else {
3657 "(unable to show full text without server connection)"
3658 }
3659 }ge;
3660
3661 $tip =~ s/^\n+//;
3662 $tip =~ s/\n+$//;
3663
3664 $tip
3665}
3666
3667# expands a tooltip, potentially multiple times remotely
3668# and returns a guard. clals the clalback each time the text changes.
3669sub expand_tooltip {
3670 my ($tip, $cb) = @_;
3671
3672 # first expand #name tooltips from local pod
3673 $tip = DC::Pod::section_label tooltip => $1
3674 if $tip =~ /^#([^(].*)$/;
3675
3676 my $active; # true if any remote requests outstanding
3677
3678 if ($::CONN && $::CONN->{addme_success}) {
3679 # now find all doclet references
3680 for my $doclet ($tip =~ /#\(([^)]+)\)/g) {
3681 unless (exists $::CONN->{doclet}{$doclet}) {
3682 # need to ask the server
3683 # we don't try to avoid duplicate requests
3684
3685 $active = 1;
3686 $::CONN->send_exti_req (doclet => (split /\//, $doclet, 2), sub {
3687 $::CONN->{doclet}{$doclet} = DC::sanitise_cfxml $_[0];
3688 $cb->(_expand_doclets $tip) if $active;
3689 });
3690 }
3691 }
3692 }
3693
3694 $cb->(_expand_doclets $tip);
3695
3696 $active and Guard::guard { undef $active }
3697}
3698
3574sub set_tooltip_from { 3699sub set_tooltip_from {
3575 my ($self, $widget) = @_; 3700 my ($self, $widget) = @_;
3576 3701
3577 my $tip = $widget->{tooltip}; 3702 my $tip = $widget->{tooltip};
3578 $tip = $tip->($widget) if "CODE" eq ref $tip; 3703 $tip = $tip->($widget) if "CODE" eq ref $tip;
3579
3580 $tip = CFPlus::Pod::section_label tooltip => $1
3581 if $tip =~ /^#(.*)$/;
3582 3704
3583 if ($ENV{CFPLUS_DEBUG} & 2) {
3584 $tip .= "\n\n" . (ref $widget) . "\n"
3585 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3586 . "req $widget->{req_w} $widget->{req_h}\n"
3587 . "visible $widget->{visible}";
3588 }
3589
3590 $tip =~ s/^\n+//;
3591 $tip =~ s/\n+$//;
3592
3593 $self->add (new CFPlus::UI::Label 3705 $self->add (new DC::UI::Label
3594 markup => $tip, 3706 fg => $DC::THEME{tooltip_fg},
3595 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3707 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3708 align => 0,
3596 fontsize => 0.8, 3709 fontsize => 0.8,
3597 style => 1, # FLAG_INVERSE 3710 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3598 ellipsise => 0, 3711 ellipsise => 0,
3599 font => ($widget->{tooltip_font} || $::FONT_PROP), 3712 font => ($widget->{tooltip_font} || $::FONT_PROP),
3600 ); 3713 );
3714
3715 $self->{tooltip_expand} = expand_tooltip $tip, sub {
3716 my ($tip) = @_;
3717
3718 if ($ENV{CFPLUS_DEBUG} & 2) {
3719 $tip .= "\n\n" . (ref $widget) . "\n"
3720 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3721 . "req $widget->{req_w} $widget->{req_h}\n"
3722 . "visible $widget->{visible}";
3723 }
3724
3725 $self->{children}[0]->set_markup ($tip);
3726 };
3601} 3727}
3602 3728
3603sub size_request { 3729sub size_request {
3604 my ($self) = @_; 3730 my ($self) = @_;
3605 3731
3639sub _draw { 3765sub _draw {
3640 my ($self) = @_; 3766 my ($self) = @_;
3641 3767
3642 my ($w, $h) = @$self{qw(w h)}; 3768 my ($w, $h) = @$self{qw(w h)};
3643 3769
3644 glColor 1, 0.8, 0.4; 3770 glColor @{ $DC::THEME{tooltip_bg} };
3645 glRect 0, 0, $w, $h; 3771 glRect 0, 0, $w, $h;
3646 3772
3647 glColor 0, 0, 0; 3773 glColor @{ $DC::THEME{tooltip_border} };
3648 glRect_lineloop .5, .5, $w + .5, $h + .5; 3774 glRect_lineloop .5, .5, $w + .5, $h + .5;
3649 3775
3650 glTranslate 2, 2; 3776 glTranslate 2, 2;
3651 3777
3652 $self->SUPER::_draw; 3778 $self->SUPER::_draw;
3653} 3779}
3654 3780
3655############################################################################# 3781#############################################################################
3656 3782
3657package CFPlus::UI::Face; 3783package DC::UI::Face;
3658 3784
3659our @ISA = CFPlus::UI::DrawBG::; 3785our @ISA = DC::UI::DrawBG::;
3660 3786
3661use CFPlus::OpenGL; 3787use DC::OpenGL;
3662 3788
3663sub new { 3789sub new {
3664 my $class = shift; 3790 my $class = shift;
3665 3791
3666 my $self = $class->SUPER::new ( 3792 my $self = $class->SUPER::new (
3669 aspect => 1, 3795 aspect => 1,
3670 can_events => 0, 3796 can_events => 0,
3671 @_, 3797 @_,
3672 ); 3798 );
3673 3799
3674 if ($self->{anim} && $self->{animspeed}) {
3675 CFPlus::weaken (my $widget = $self);
3676
3677 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3678 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3679 $self->{timer} = Event->timer (
3680 parked => 1,
3681 cb => sub {
3682 return unless $::CONN;
3683
3684 my $w = $widget
3685 or return;
3686
3687 ++$w->{frame};
3688 $w->update_face;
3689
3690 # somehow, $widget can go away
3691 $w->update;
3692 $w->update_timer;
3693 },
3694 );
3695
3696 $self->update_face;
3697 $self->update_timer; 3800 $self->update_anim;
3698 } 3801
3699
3700 $self 3802 $self
3701} 3803}
3702 3804
3703sub update_timer { 3805sub update_timer {
3704 my ($self) = @_; 3806 my ($self) = @_;
3705 3807
3706 return unless $self->{timer}; 3808 return unless $self->{timer};
3707 3809
3708 if ($self->{visible}) { 3810 if ($self->{visible}) {
3709 $self->{timer}->at (
3710 $self->{anim_start}
3711 + $self->{animspeed}
3712 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3713 );
3714 $self->{timer}->start; 3811 $self->{timer}->start;
3715 } else { 3812 } else {
3716 $self->{timer}->stop; 3813 $self->{timer}->stop;
3717 } 3814 }
3718} 3815}
3731 $tex->upload (sub { $self->reconfigure }); 3828 $tex->upload (sub { $self->reconfigure });
3732 } 3829 }
3733 } 3830 }
3734 } 3831 }
3735 } 3832 }
3833 }
3834}
3835
3836sub update_anim {
3837 my ($self) = @_;
3838
3839 if ($self->{anim} && $self->{animspeed}) {
3840 DC::weaken (my $widget = $self);
3841
3842 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3843 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3844 return unless $::CONN;
3845
3846 my $w = $widget
3847 or return;
3848
3849 ++$w->{frame};
3850 $w->update_face;
3851
3852 # somehow, $widget can go away
3853 $w->update;
3854 $w->update_timer;
3855 };
3856
3857 $self->update_face;
3858 $self->update_timer;
3859 } else {
3860 delete $self->{timer};
3736 } 3861 }
3737} 3862}
3738 3863
3739sub size_request { 3864sub size_request {
3740 my ($self) = @_; 3865 my ($self) = @_;
3762 return unless $self->{visible}; 3887 return unless $self->{visible};
3763 3888
3764 $self->SUPER::update; 3889 $self->SUPER::update;
3765} 3890}
3766 3891
3892sub set_face {
3893 my ($self, $face) = @_;
3894
3895 $self->{face} = $face;
3896 $self->reconfigure;
3897}
3898
3899sub set_anim {
3900 my ($self, $anim) = @_;
3901
3902 $self->{anim} = $anim;
3903 $self->update_anim;
3904}
3905
3906sub set_animspeed {
3907 my ($self, $animspeed) = @_;
3908
3909 $self->{animspeed} = $animspeed;
3910 $self->update_anim;
3911}
3912
3767sub invoke_visibility_change { 3913sub invoke_visibility_change {
3768 my ($self) = @_; 3914 my ($self) = @_;
3769 3915
3770 $self->update_timer; 3916 $self->update_timer;
3771 3917
3795 $self->SUPER::destroy; 3941 $self->SUPER::destroy;
3796} 3942}
3797 3943
3798############################################################################# 3944#############################################################################
3799 3945
3800package CFPlus::UI::Buttonbar; 3946package DC::UI::Buttonbar;
3801 3947
3802our @ISA = CFPlus::UI::HBox::; 3948our @ISA = DC::UI::HBox::;
3803 3949
3804# TODO: should actually wrap buttons and other goodies. 3950# TODO: should actually wrap buttons and other goodies.
3805 3951
3806############################################################################# 3952#############################################################################
3807 3953
3808package CFPlus::UI::Menu; 3954package DC::UI::Menu;
3809 3955
3810our @ISA = CFPlus::UI::Toplevel::; 3956our @ISA = DC::UI::Toplevel::;
3811 3957
3812use CFPlus::OpenGL; 3958use DC::OpenGL;
3813 3959
3814sub new { 3960sub new {
3815 my $class = shift; 3961 my $class = shift;
3816 3962
3817 my $self = $class->SUPER::new ( 3963 my $self = $class->SUPER::new (
3818 items => [], 3964 items => [],
3819 z => 100, 3965 z => 100,
3820 @_, 3966 @_,
3821 ); 3967 );
3822 3968
3823 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3969 $self->add ($self->{vbox} = new DC::UI::VBox);
3824 3970
3825 for my $item (@{ $self->{items} }) { 3971 for my $item (@{ $self->{items} }) {
3826 my ($widget, $cb, $tooltip) = @$item; 3972 my ($widget, $cb, $tooltip) = @$item;
3827 3973
3828 # handle various types of items, only text for now 3974 # handle various types of items, only text for now
3829 if (!ref $widget) { 3975 if (!ref $widget) {
3830 if ($widget =~ /\t/) { 3976 if ($widget =~ /\t/) {
3831 my ($left, $right) = split /\t/, $widget, 2; 3977 my ($left, $right) = split /\t/, $widget, 2;
3832 3978
3833 $widget = new CFPlus::UI::HBox 3979 $widget = new DC::UI::HBox
3834 can_hover => 1, 3980 can_hover => 1,
3835 can_events => 1, 3981 can_events => 1,
3836 tooltip => $tooltip, 3982 tooltip => $tooltip,
3837 children => [ 3983 children => [
3838 (new CFPlus::UI::Label markup => $left, expand => 1), 3984 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3839 (new CFPlus::UI::Label markup => $right, align => +1), 3985 (new DC::UI::Label markup => $right, align => 1),
3840 ], 3986 ],
3841 ; 3987 ;
3842 3988
3843 } else { 3989 } else {
3844 $widget = new CFPlus::UI::Label 3990 $widget = new DC::UI::Label
3845 can_hover => 1, 3991 can_hover => 1,
3846 can_events => 1, 3992 can_events => 1,
3993 align => 0,
3847 markup => $widget, 3994 markup => $widget,
3848 tooltip => $tooltip; 3995 tooltip => $tooltip;
3849 } 3996 }
3850 } 3997 }
3851 3998
3866 # maybe save $GRAB? must be careful about events... 4013 # maybe save $GRAB? must be careful about events...
3867 $GRAB = $self; 4014 $GRAB = $self;
3868 $self->{button} = $ev->{button}; 4015 $self->{button} = $ev->{button};
3869 4016
3870 $self->show; 4017 $self->show;
3871 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 4018
4019 my $x = $ev->{x};
4020 my $y = $ev->{y};
4021
4022 $self->{root}->on_post_alloc ($self => sub {
4023 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
4024 });
4025
4026 1 # so it can be used inside event handlers
3872} 4027}
3873 4028
3874sub invoke_mouse_motion { 4029sub invoke_mouse_motion {
3875 my ($self, $ev, $x, $y) = @_; 4030 my ($self, $ev, $x, $y) = @_;
3876 4031
3897 1 4052 1
3898} 4053}
3899 4054
3900############################################################################# 4055#############################################################################
3901 4056
3902package CFPlus::UI::Multiplexer; 4057package DC::UI::Multiplexer;
3903 4058
3904our @ISA = CFPlus::UI::Container::; 4059our @ISA = DC::UI::Container::;
3905 4060
3906sub new { 4061sub new {
3907 my $class = shift; 4062 my $class = shift;
3908 4063
3909 my $self = $class->SUPER::new ( 4064 my $self = $class->SUPER::new (
3910 @_, 4065 @_,
3911 ); 4066 );
3912 4067
3913 $self->{current} = $self->{children}[0] 4068 $self->set_current_page (0);
3914 if @{ $self->{children} };
3915 4069
3916 $self 4070 $self
3917} 4071}
3918 4072
3919sub add { 4073sub add {
3920 my ($self, @widgets) = @_; 4074 my ($self, @widgets) = @_;
3921 4075
3922 $self->SUPER::add (@widgets); 4076 $self->SUPER::add (@widgets);
3923 4077
3924 $self->{current} = $self->{children}[0] 4078 $self->set_current_page (0)
3925 if @{ $self->{children} }; 4079 if @widgets == @{ $self->{children} };
3926} 4080}
3927 4081
3928sub get_current_page { 4082sub get_current_page {
3929 my ($self) = @_; 4083 my ($self) = @_;
3930 4084
3936 4090
3937 my $widget = ref $page_or_widget 4091 my $widget = ref $page_or_widget
3938 ? $page_or_widget 4092 ? $page_or_widget
3939 : $self->{children}[$page_or_widget]; 4093 : $self->{children}[$page_or_widget];
3940 4094
4095 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4096
3941 $self->{current} = $widget; 4097 if (($self->{current} = $widget)) {
4098 $self->{current}->set_visible if $self->{current} && $self->{visible};
3942 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4099 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3943 4100
3944 $self->emit (page_changed => $self->{current}); 4101 $self->emit (page_changed => $self->{current});
4102 }
3945 4103
3946 $self->realloc; 4104 $self->realloc;
3947} 4105}
3948 4106
3949sub visible_children { 4107sub visible_children {
3950 $_[0]{current} 4108 $_[0]{current} || ()
3951} 4109}
3952 4110
3953sub size_request { 4111sub size_request {
3954 my ($self) = @_; 4112 my ($self) = @_;
3955 4113
4114 $self->{current}
3956 $self->{current}->size_request 4115 ? $self->{current}->size_request
4116 : (0, 0)
3957} 4117}
3958 4118
3959sub invoke_size_allocate { 4119sub invoke_size_allocate {
3960 my ($self, $w, $h) = @_; 4120 my ($self, $w, $h) = @_;
3961 4121
3962 $self->{current}->configure (0, 0, $w, $h); 4122 $self->{current}->configure (0, 0, $w, $h)
4123 if $self->{current};
3963 4124
3964 1 4125 1
3965} 4126}
3966 4127
3967sub _draw { 4128sub _draw {
3968 my ($self) = @_; 4129 my ($self) = @_;
3969 4130
3970 $self->{current}->draw; 4131 $self->{current}->draw
4132 if $self->{current};
3971} 4133}
3972 4134
3973############################################################################# 4135#############################################################################
3974 4136
3975package CFPlus::UI::Notebook; 4137package DC::UI::Notebook;
3976 4138
3977use CFPlus::OpenGL; 4139use DC::OpenGL;
3978 4140
3979our @ISA = CFPlus::UI::VBox::; 4141our @ISA = DC::UI::VBox::;
3980 4142
3981sub new { 4143sub new {
3982 my $class = shift; 4144 my $class = shift;
3983 4145
3984 my $self = $class->SUPER::new ( 4146 my $self = $class->SUPER::new (
3985 buttonbar => (new CFPlus::UI::Buttonbar), 4147 buttonbar => (new DC::UI::Buttonbar),
3986 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4148 multiplexer => (new DC::UI::Multiplexer expand => 1),
3987 active_outline => [.7, .7, 0.2], 4149 active_outline => [.7, .7, 0.2],
3988 # filter => # will be put between multiplexer and $self 4150 # filter => # will be put between multiplexer and $self
3989 @_, 4151 @_,
3990 ); 4152 );
3991 4153
4000 4162
4001 for my $child (@$widgets) { 4163 for my $child (@$widgets) {
4002 Scalar::Util::weaken $child; 4164 Scalar::Util::weaken $child;
4003 $child->{c_tab_} ||= do { 4165 $child->{c_tab_} ||= do {
4004 my $tab = 4166 my $tab =
4005 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4167 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4006 ? $child->{c_tab} 4168 ? $child->{c_tab}
4007 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4169 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4008 4170
4009 $tab->connect (activate => sub { 4171 $tab->connect (activate => sub {
4010 $wself->set_current_page ($child); 4172 $wself->set_current_page ($child);
4011 }); 4173 });
4012 4174
4042} 4204}
4043 4205
4044sub pages { 4206sub pages {
4045 my ($self) = @_; 4207 my ($self) = @_;
4046 $self->{multiplexer}->children 4208 $self->{multiplexer}->children
4209}
4210
4211sub page_index {
4212 my ($self, $widget) = @_;
4213
4214 my $i = 0;
4215 for ($self->pages) {
4216 if ($_ eq $widget) { return $i };
4217 $i++;
4218 }
4219
4220 undef
4047} 4221}
4048 4222
4049sub add_tab { 4223sub add_tab {
4050 my ($self, $title, $widget, $tooltip) = @_; 4224 my ($self, $title, $widget, $tooltip) = @_;
4051 4225
4088 } 4262 }
4089} 4263}
4090 4264
4091############################################################################# 4265#############################################################################
4092 4266
4093package CFPlus::UI::Selector; 4267package DC::UI::Selector;
4094 4268
4095use utf8; 4269use utf8;
4096 4270
4097our @ISA = CFPlus::UI::Button::; 4271our @ISA = DC::UI::Button::;
4098 4272
4099sub new { 4273sub new {
4100 my $class = shift; 4274 my $class = shift;
4101 4275
4102 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4119 my ($value, $title, $tooltip) = @$_; 4293 my ($value, $title, $tooltip) = @$_;
4120 4294
4121 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4295 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4122 } 4296 }
4123 4297
4124 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4298 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4125} 4299}
4126 4300
4127sub _set_value { 4301sub _set_value {
4128 my ($self, $value) = @_; 4302 my ($self, $value) = @_;
4129 4303
4130 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4304 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4305 $item ||= $self->{options}[0]
4131 or return; 4306 or return;
4132 4307
4133 $self->{value} = $item->[0]; 4308 $self->{value} = $item->[0];
4134 $self->set_markup ("$item->[1] ⇓"); 4309 $self->set_markup ("$item->[1] ⇓");
4135 $self->set_tooltip ($item->[2]); 4310# $self->set_tooltip ($item->[2]);
4136} 4311}
4137 4312
4138sub set_value { 4313sub set_value {
4139 my ($self, $value) = @_; 4314 my ($self, $value) = @_;
4140 4315
4142 4317
4143 $self->_set_value ($value); 4318 $self->_set_value ($value);
4144 $self->emit (changed => $value); 4319 $self->emit (changed => $value);
4145} 4320}
4146 4321
4322sub set_options {
4323 my ($self, $options) = @_;
4324
4325 $self->{options} = $options;
4326 $self->_set_value ($self->{value});
4327}
4328
4147############################################################################# 4329#############################################################################
4148 4330
4149package CFPlus::UI::Statusbox; 4331package DC::UI::Statusbox;
4150 4332
4151our @ISA = CFPlus::UI::VBox::; 4333our @ISA = DC::UI::VBox::;
4152 4334
4153sub new { 4335sub new {
4154 my $class = shift; 4336 my $class = shift;
4155 4337
4156 my $self = $class->SUPER::new ( 4338 my $self = $class->SUPER::new (
4157 fontsize => 0.8, 4339 fontsize => 0.8,
4158 @_, 4340 @_,
4159 ); 4341 );
4160 4342
4161 CFPlus::weaken (my $this = $self); 4343 DC::weaken (my $this = $self);
4162 4344
4163 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4345 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4164 4346
4165 $self 4347 $self
4166} 4348}
4167 4349
4168sub reorder { 4350sub reorder {
4169 my ($self) = @_; 4351 my ($self) = @_;
4170 my $NOW = Time::HiRes::time; 4352 my $NOW = AE::time;
4171 4353
4172 # freeze display when hovering over any label 4354 # freeze display when hovering over any label
4173 return if $CFPlus::UI::TOOLTIP->{owner} 4355 return if $DC::UI::TOOLTIP->{owner}
4174 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4356 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4175 values %{ $self->{item} }; 4357 values %{ $self->{item} };
4176 4358
4177 while (my ($k, $v) = each %{ $self->{item} }) { 4359 while (my ($k, $v) = each %{ $self->{item} }) {
4178 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4360 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4179 } 4361 }
4362
4363 $self->{timer}->set (1, 1);
4180 4364
4181 my @widgets; 4365 my @widgets;
4182 4366
4183 my @items = sort { 4367 my @items = sort {
4184 $a->{pri} <=> $b->{pri} 4368 $a->{pri} <=> $b->{pri}
4185 or $b->{id} <=> $a->{id} 4369 or $b->{id} <=> $a->{id}
4186 } values %{ $self->{item} }; 4370 } values %{ $self->{item} };
4187
4188 $self->{timer}->interval (1);
4189 4371
4190 my $count = 10 + 1; 4372 my $count = 10 + 1;
4191 for my $item (@items) { 4373 for my $item (@items) {
4192 last unless --$count; 4374 last unless --$count;
4193 4375
4200 for ($short) { 4382 for ($short) {
4201 s/^\s+//; 4383 s/^\s+//;
4202 s/\s+/ /g; 4384 s/\s+/ /g;
4203 } 4385 }
4204 4386
4205 new CFPlus::UI::Label 4387 new DC::UI::Label
4206 markup => $short, 4388 markup => $short,
4207 tooltip => $item->{tooltip}, 4389 tooltip => $item->{tooltip},
4208 tooltip_font => $::FONT_PROP, 4390 tooltip_font => $::FONT_PROP,
4209 tooltip_width => 0.67, 4391 tooltip_width => 0.67,
4210 fontsize => $item->{fontsize} || $self->{fontsize}, 4392 fontsize => $item->{fontsize} || $self->{fontsize},
4211 max_w => $::WIDTH * 0.44, 4393 max_w => $::WIDTH * 0.44,
4394 align => 0,
4212 fg => [@{ $item->{fg} }], 4395 fg => [@{ $item->{fg} }],
4213 can_events => 1, 4396 can_events => 1,
4214 can_hover => 1 4397 can_hover => 1
4215 }; 4398 };
4216 4399
4217 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4400 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4401 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4219 $label->update; 4402 $label->update;
4220 $label->set_max_size (undef, $label->{req_h} * $diff) 4403 $label->set_max_size (undef, $label->{req_h} * $diff)
4221 if $diff < 1; 4404 if $diff < 1;
4222 $self->{timer}->interval (1/30); 4405 $self->{timer}->set (1/30, 1/30);
4223 } else { 4406 } else {
4224 $label->{fg}[3] = $item->{fg}[3] || 1; 4407 $label->{fg}[3] = $item->{fg}[3] || 1;
4225 } 4408 }
4226 4409
4227 push @widgets, $label; 4410 push @widgets, $label;
4228 } 4411 }
4412
4413 my $hash = join ",", @widgets;
4414 return if $hash eq $self->{last_widget_hash};
4415 $self->{last_widget_hash} = $hash;
4229 4416
4230 $self->clear; 4417 $self->clear;
4231 $self->SUPER::add (reverse @widgets); 4418 $self->SUPER::add (reverse @widgets);
4232} 4419}
4233 4420
4269 $ROOT->on_refresh (reorder => sub { 4456 $ROOT->on_refresh (reorder => sub {
4270 $self->reorder; 4457 $self->reorder;
4271 }); 4458 });
4272} 4459}
4273 4460
4461sub clr_group {
4462 my ($self, $group) = @_;
4463
4464 if (delete $self->{item}{$group}) {
4465 $ROOT->on_refresh (reorder => sub {
4466 $self->reorder;
4467 });
4468 }
4469}
4470
4274sub reconfigure { 4471sub reconfigure {
4275 my ($self) = @_; 4472 my ($self) = @_;
4276 4473
4277 delete $_->{label} 4474 delete $_->{label}
4278 for values %{ $self->{item} || {} }; 4475 for values %{ $self->{item} || {} };
4289 $self->SUPER::destroy; 4486 $self->SUPER::destroy;
4290} 4487}
4291 4488
4292############################################################################# 4489#############################################################################
4293 4490
4294package CFPlus::UI::Root; 4491package DC::UI::Root;
4295 4492
4296our @ISA = CFPlus::UI::Container::; 4493our @ISA = DC::UI::Container::;
4297 4494
4298use List::Util qw(min max); 4495use List::Util qw(min max);
4299 4496
4300use CFPlus::OpenGL; 4497use DC::OpenGL;
4301 4498
4302sub new { 4499sub new {
4303 my $class = shift; 4500 my $class = shift;
4304 4501
4305 my $self = $class->SUPER::new ( 4502 my $self = $class->SUPER::new (
4306 visible => 1, 4503 visible => 1,
4307 @_, 4504 @_,
4308 ); 4505 );
4309 4506
4310 CFPlus::weaken ($self->{root} = $self); 4507 DC::weaken ($self->{root} = $self);
4311 4508
4312 $self 4509 $self
4313} 4510}
4314 4511
4315sub size_request { 4512sub size_request {
4363} 4560}
4364 4561
4365sub update { 4562sub update {
4366 my ($self) = @_; 4563 my ($self) = @_;
4367 4564
4368 $::WANT_REFRESH->start; 4565 $::WANT_REFRESH = 1;
4369} 4566}
4370 4567
4371sub add { 4568sub add {
4372 my ($self, @children) = @_; 4569 my ($self, @children) = @_;
4373 4570
4432 while () { 4629 while () {
4433 @queue or last outer; 4630 @queue or last outer;
4434 4631
4435 $widget = pop @{ $queue[-1] || [] } 4632 $widget = pop @{ $queue[-1] || [] }
4436 and last; 4633 and last;
4437 4634
4438 pop @queue; 4635 pop @queue;
4439 } 4636 }
4440 4637
4441 delete $queue{$widget+0}; 4638 delete $queue{$widget+0};
4442 4639
4443 my ($w, $h) = $widget->size_request; 4640 my ($w, $h) = $widget->size_request;
4444 4641
4445 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4642 $w += $widget->{padding_x} * 2;
4446 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4643 $h += $widget->{padding_y} * 2;
4644
4645 $w = max $widget->{min_w}, $w;
4646 $h = max $widget->{min_h}, $h;
4447 4647
4448 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4648 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4449 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4649 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4450 4650
4451 $w = $widget->{force_w} if exists $widget->{force_w}; 4651 $w = $widget->{force_w} if exists $widget->{force_w};
4518 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4718 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4519 glMatrixMode GL_MODELVIEW; 4719 glMatrixMode GL_MODELVIEW;
4520 glLoadIdentity; 4720 glLoadIdentity;
4521 4721
4522 { 4722 {
4523 package CFPlus::UI::Base; 4723 package DC::UI::Base;
4524 4724
4525 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4725 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4526 (0, 0, $self->{w}, $self->{h}); 4726 (0, 0, $self->{w}, $self->{h});
4527 4727
4528 $self->_draw; 4728 $self->_draw;
4529 } 4729 }
4530} 4730}
4531 4731
4532############################################################################# 4732#############################################################################
4533 4733
4534package CFPlus::UI; 4734package DC::UI;
4535 4735
4536$ROOT = new CFPlus::UI::Root; 4736$ROOT = new DC::UI::Root;
4537$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4737$TOOLTIP = new DC::UI::Tooltip z => 900;
4538 4738
45391 47391
4540

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines