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.436 by root, Wed Aug 22 21:40:57 2007 UTC vs.
Revision 1.487 by root, Fri Nov 23 12:14:02 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};
269 $self->emit ("destroy"); 272 $self->emit ("destroy");
270 %$self = (); 273 %$self = ();
271} 274}
272 275
273sub TO_JSON { 276sub TO_JSON {
274 { __w_ => $_[0]{s_id} } 277 { "\fw" => $_[0]{s_id} }
275} 278}
276 279
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;
372}
373
374# traverse the widget chain up to find the maximum "physical" size constraints
375sub get_max_wh {
376 my ($self) = @_;
377
378 my ($w, $h) = @$self{qw(max_w max_h)};
379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
382 ? $self->{parent}->get_max_wh
383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
363} 390}
364 391
365sub size_request { 392sub size_request {
366 require Carp; 393 require Carp;
367 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
375 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
376 403
377 if ($self->{aspect}) { 404 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
379 406
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 409
383 # use alignment to adjust x, y 410 # use alignment to adjust x, y
384 411
385 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
427 454
428 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
429 456
430 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
431 458
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 462 }
436} 463}
437 464
438# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
439sub coord2local { 466sub coord2local {
440 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
441 468
442 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
443 470
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 472}
446 473
447# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
448sub coord2global { 475sub coord2global {
449 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
450 477
451 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
452 479
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 481}
455 482
456sub invoke_focus_in { 483sub invoke_focus_in {
505sub connect { 532sub connect {
506 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
507 534
508 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
509 536
510 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
513 } 540 }
514} 541}
515 542
553# in .xs 580# in .xs
554 581
555sub set_parent { 582sub set_parent {
556 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
557 584
558 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
559 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
560} 587}
561 588
562sub realloc { 589sub realloc {
563 my ($self) = @_; 590 my ($self) = @_;
601} 628}
602 629
603sub DESTROY { 630sub DESTROY {
604 my ($self) = @_; 631 my ($self) = @_;
605 632
606 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
607 634
608 local $@; 635 local $@;
609 eval { $self->destroy }; 636 eval { $self->destroy };
610 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
611 638
612 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
613} 640}
614 641
615############################################################################# 642#############################################################################
616 643
617package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
618 645
619our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
620 647
621use strict; 648use common::sense;
649
622use CFPlus::OpenGL; 650use DC::OpenGL;
623 651
624sub new { 652sub new {
625 my $class = shift; 653 my $class = shift;
626 654
627 $class->SUPER::new ( 655 $class->SUPER::new (
629 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
630 @_ 658 @_
631 ) 659 )
632} 660}
633 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
634sub _draw { 669sub _draw {
635 my ($self) = @_; 670 my ($self) = @_;
636 671
637 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
638 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
639 : $self->{bg}; 674 : $self->{bg};
640 675
641 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
642 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
643 678
649 } 684 }
650} 685}
651 686
652############################################################################# 687#############################################################################
653 688
654package CFPlus::UI::Empty; 689package DC::UI::Empty;
655 690
656our @ISA = CFPlus::UI::Base::; 691our @ISA = DC::UI::Base::;
657 692
658sub new { 693sub new {
659 my ($class, %arg) = @_; 694 my ($class, %arg) = @_;
660 $class->SUPER::new (can_events => 0, %arg); 695 $class->SUPER::new (can_events => 0, %arg);
661} 696}
668 703
669sub draw { } 704sub draw { }
670 705
671############################################################################# 706#############################################################################
672 707
673package CFPlus::UI::Container; 708package DC::UI::Container;
674 709
675our @ISA = CFPlus::UI::Base::; 710our @ISA = DC::UI::Base::;
676 711
677sub new { 712sub new {
678 my ($class, %arg) = @_; 713 my ($class, %arg) = @_;
679 714
680 my $children = delete $arg{children}; 715 my $children = delete $arg{children};
774 $_->draw for $self->visible_children; 809 $_->draw for $self->visible_children;
775} 810}
776 811
777############################################################################# 812#############################################################################
778 813
779package CFPlus::UI::Bin; 814package DC::UI::Bin;
780 815
781our @ISA = CFPlus::UI::Container::; 816our @ISA = DC::UI::Container::;
782 817
783sub new { 818sub new {
784 my ($class, %arg) = @_; 819 my ($class, %arg) = @_;
785 820
786 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 821 my $child = (delete $arg{child}) || new DC::UI::Empty::;
787 822
788 $class->SUPER::new (children => [$child], %arg) 823 $class->SUPER::new (children => [$child], %arg)
789} 824}
790 825
791sub add { 826sub add {
798sub remove { 833sub remove {
799 my ($self, $widget) = @_; 834 my ($self, $widget) = @_;
800 835
801 $self->SUPER::remove ($widget); 836 $self->SUPER::remove ($widget);
802 837
803 $self->{children} = [new CFPlus::UI::Empty] 838 $self->{children} = [new DC::UI::Empty]
804 unless @{$self->{children}}; 839 unless @{$self->{children}};
805} 840}
806 841
807sub child { $_[0]->{children}[0] } 842sub child { $_[0]->{children}[0] }
808 843
817 852
818 1 853 1
819} 854}
820 855
821############################################################################# 856#############################################################################
822
823# back-buffered drawing area 857# back-buffered drawing area
824 858
825package CFPlus::UI::Window; 859package DC::UI::Window;
826 860
827our @ISA = CFPlus::UI::Bin::; 861our @ISA = DC::UI::Bin::;
828 862
829use CFPlus::OpenGL; 863use DC::OpenGL;
830 864
831sub new { 865sub new {
832 my ($class, %arg) = @_; 866 my ($class, %arg) = @_;
833 867
834 my $self = $class->SUPER::new (%arg); 868 my $self = $class->SUPER::new (%arg);
856} 890}
857 891
858sub render_child { 892sub render_child {
859 my ($self) = @_; 893 my ($self) = @_;
860 894
861 $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 {
862 glClearColor 0, 0, 0, 0; 896 glClearColor 0, 0, 0, 0;
863 glClear GL_COLOR_BUFFER_BIT; 897 glClear GL_COLOR_BUFFER_BIT;
864 898
865 { 899 {
866 package CFPlus::UI::Base; 900 package DC::UI::Base;
867 901
868 local ($draw_x, $draw_y, $draw_w, $draw_h) = 902 local ($draw_x, $draw_y, $draw_w, $draw_h) =
869 (0, 0, $self->{w}, $self->{h}); 903 (0, 0, $self->{w}, $self->{h});
870 904
871 $self->_render; 905 $self->_render;
888 glDisable GL_TEXTURE_2D; 922 glDisable GL_TEXTURE_2D;
889} 923}
890 924
891############################################################################# 925#############################################################################
892 926
893package CFPlus::UI::ViewPort; 927package DC::UI::ViewPort;
894 928
895use List::Util qw(min max); 929use List::Util qw(min max);
896 930
897our @ISA = CFPlus::UI::Window::; 931our @ISA = DC::UI::Window::;
898 932
899sub new { 933sub new {
900 my $class = shift; 934 my $class = shift;
901 935
902 $class->SUPER::new ( 936 $class->SUPER::new (
986 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
987 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
988 ) { 1022 ) {
989 $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})
990 } else { 1024 } else {
991 $self->CFPlus::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
992 } 1026 }
993} 1027}
994 1028
995sub _render { 1029sub _render {
996 my ($self) = @_; 1030 my ($self) = @_;
997 1031
998 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};
999 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};
1000 1034
1001 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1002 1036
1003 $self->SUPER::_render; 1037 $self->SUPER::_render;
1004} 1038}
1005 1039
1006############################################################################# 1040#############################################################################
1007 1041
1008package CFPlus::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1009 1043
1010our @ISA = CFPlus::UI::Table::; 1044our @ISA = DC::UI::Table::;
1011 1045
1012sub new { 1046sub new {
1013 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1014 1048
1015 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1016 1050
1017 my $self; 1051 my $self;
1018 1052
1019 my $hslider = new CFPlus::UI::Slider 1053 my $hslider = new DC::UI::Slider
1020 c_col => 0, 1054 c_col => 0,
1021 c_row => 1, 1055 c_row => 1,
1022 vertical => 0, 1056 vertical => 0,
1023 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1058 on_changed => sub {
1025 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1027 }, 1061 },
1028 ; 1062 ;
1029 1063
1030 my $vslider = new CFPlus::UI::Slider 1064 my $vslider = new DC::UI::Slider
1031 c_col => 1, 1065 c_col => 1,
1032 c_row => 0, 1066 c_row => 0,
1033 vertical => 1, 1067 vertical => 1,
1034 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1035 on_changed => sub { 1069 on_changed => sub {
1047 col_expand => [1, 0], 1081 col_expand => [1, 0],
1048 row_expand => [1, 0], 1082 row_expand => [1, 0],
1049 %arg, 1083 %arg,
1050 ); 1084 );
1051 1085
1052 $self->{vp} = new CFPlus::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1053 c_col => 0, 1087 c_col => 0,
1054 c_row => 0, 1088 c_row => 0,
1055 expand => 1, 1089 expand => 1,
1056 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1057 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1122 1156
1123 $self->grab_focus; 1157 $self->grab_focus;
1124 1158
1125 my $ox = $self->{vp}{view_x}; 1159 my $ox = $self->{vp}{view_x};
1126 my $oy = $self->{vp}{view_y}; 1160 my $oy = $self->{vp}{view_y};
1127 1161
1128 $self->{motion} = sub { 1162 $self->{motion} = sub {
1129 my ($ev, $x, $y) = @_; 1163 my ($ev, $x, $y) = @_;
1130 1164
1131 $ox -= $ev->{xrel}; 1165 $ox -= $ev->{xrel};
1132 $oy -= $ev->{yrel}; 1166 $oy -= $ev->{yrel};
1183 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1184} 1218}
1185 1219
1186############################################################################# 1220#############################################################################
1187 1221
1188package CFPlus::UI::Frame; 1222package DC::UI::Frame;
1189 1223
1190our @ISA = CFPlus::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1191 1225
1192use CFPlus::OpenGL; 1226use DC::OpenGL;
1193 1227
1194sub new { 1228sub new {
1195 my $class = shift; 1229 my $class = shift;
1196 1230
1197 $class->SUPER::new ( 1231 $class->SUPER::new (
1216 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1217} 1251}
1218 1252
1219############################################################################# 1253#############################################################################
1220 1254
1221package CFPlus::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1222 1256
1223our @ISA = CFPlus::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1224 1258
1225use CFPlus::OpenGL; 1259use DC::OpenGL;
1226 1260
1227sub new { 1261sub new {
1228 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1229 1263
1230 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1231 $arg{label} = new CFPlus::UI::Label 1265 $arg{label} = new DC::UI::Label
1232 align => 1, 1266 align => 1,
1233 valign => 0, 1267 valign => 0.5,
1234 text => $arg{label}, 1268 text => $arg{label},
1235 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1236 } 1270 }
1237 1271
1238 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1239 # label => "", 1273 # label => "",
1240 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1241 border => 0.8, 1275 border => 0.8,
1242 style => 'single', 1276 style => 'single',
1243 %arg, 1277 %arg,
1244 ); 1278 );
1245 1279
1248 1282
1249sub add { 1283sub add {
1250 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1251 1285
1252 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1253 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1254} 1288}
1255 1289
1256sub border { 1290sub border {
1257 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1258} 1292}
1298 my $border = $self->border; 1332 my $border = $self->border;
1299 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1300 1334
1301 $child->draw; 1335 $child->draw;
1302 1336
1303 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1304 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1305 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1306 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1307 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;
1308 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;
1316 } 1350 }
1317} 1351}
1318 1352
1319############################################################################# 1353#############################################################################
1320 1354
1321package CFPlus::UI::Toplevel; 1355package DC::UI::Toplevel;
1322 1356
1323our @ISA = CFPlus::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1324 1358
1325use CFPlus::OpenGL; 1359use DC::OpenGL;
1326 1360
1327my $bg = 1361my $bg =
1328 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1329 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1330 1364
1331my @border = 1365my @border =
1332 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1333 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);
1334 1368
1335my @icon = 1369my @icon =
1336 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1370 map { new_from_resource DC::Texture $_, mipmap => 1 }
1337 qw(x1_move.png x1_resize.png); 1371 qw(x1_move.png x1_resize.png);
1338 1372
1339sub new { 1373sub new {
1340 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1341 1375
1342 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1343 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1344 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1345 border => 0.6, 1379 border => 0.8,
1346 can_events => 1, 1380 can_events => 1,
1347 min_w => 64, 1381 min_w => 64,
1348 min_h => 32, 1382 min_h => 32,
1349 %arg, 1383 %arg,
1350 ); 1384 );
1351 1385
1352 $self->{title_widget} = new CFPlus::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1353 align => 0, 1387 align => 0.5,
1354 valign => 1, 1388 valign => 1,
1355 text => $self->{title}, 1389 text => $self->{title},
1356 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1357 if exists $self->{title}; 1391 if exists $self->{title};
1358 1392
1359 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1360 $self->{close_button} = 1394 $self->{close_button} =
1361 new CFPlus::UI::ImageButton 1395 new DC::UI::ImageButton
1362 path => 'x1_close.png', 1396 path => 'x1_close.png',
1363 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1364 1398
1365 $self->CFPlus::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1366 } 1400 }
1367 1401
1368 $self 1402 $self
1369} 1403}
1370 1404
1371sub add { 1405sub add {
1372 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1373 1407
1374 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1375 $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};
1376 $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};
1377} 1411}
1378 1412
1379sub border { 1413sub border {
1380 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1415}
1416
1417sub get_max_wh {
1418 my ($self) = @_;
1419
1420 return ($self->{w}, $self->{h})
1421 if $self->{visible} && $self->{w};
1422
1423 $self->SUPER::get_max_wh
1381} 1424}
1382 1425
1383sub size_request { 1426sub size_request {
1384 my ($self) = @_; 1427 my ($self) = @_;
1385 1428
1421 1464
1422sub invoke_delete { 1465sub invoke_delete {
1423 my ($self) = @_; 1466 my ($self) = @_;
1424 1467
1425 $self->hide; 1468 $self->hide;
1426 1469
1427 1 1470 1
1428} 1471}
1429 1472
1430sub invoke_button_down { 1473sub invoke_button_down {
1431 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1432 1475
1433 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1434 my $border = $self->border; 1477 my $border = $self->border;
1435 1478
1436 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
1437 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
1438 1481
1439 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1440 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1441 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1442 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1443 1486
1444 my $mx = $x < $border; 1487 my $mx = $x < $border;
1448 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1449 1492
1450 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1451 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1452 1495
1496 $self->set_size (
1453 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1454 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1455 1500
1456 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1457 $self->realloc; 1502 $self->realloc;
1458 }; 1503 };
1459 1504
1460 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1461 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1462 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1463 1508
1464 $self->{motion} = sub { 1509 $self->{motion} = sub {
1465 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1510 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1512 1557
1513 my $border = $self->border; 1558 my $border = $self->border;
1514 1559
1560 if ($border) {
1515 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1516 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1517 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1518 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1519 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1565 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1520 1566
1521 # move 1567 # move
1522 my $w2 = ($w - $border) * .5; 1568 my $w2 = ($w - $border) * .5;
1523 my $h2 = ($h - $border) * .5; 1569 my $h2 = ($h - $border) * .5;
1524 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1570 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1525 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1571 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1526 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1572 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1527 1573
1528 # resize 1574 # resize
1529 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1530 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1576 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1531 unless $self->{has_close_button}; 1577 unless $self->{has_close_button};
1532 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1578 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1533 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1579 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1580 }
1534 1581
1535 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1536 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1537 1584
1538 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
1557 if $self->{close_button}; 1604 if $self->{close_button};
1558} 1605}
1559 1606
1560############################################################################# 1607#############################################################################
1561 1608
1562package CFPlus::UI::Table; 1609package DC::UI::Table;
1563 1610
1564our @ISA = CFPlus::UI::Container::; 1611our @ISA = DC::UI::Container::;
1565 1612
1566use List::Util qw(max sum); 1613use List::Util qw(max sum);
1567 1614
1568use CFPlus::OpenGL; 1615use DC::OpenGL;
1569 1616
1570sub new { 1617sub new {
1571 my $class = shift; 1618 my $class = shift;
1572 1619
1573 $class->SUPER::new ( 1620 $class->SUPER::new (
1617 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)};
1618 1665
1619 my $sw = sum @w[$c .. $c + $cs - 1]; 1666 my $sw = sum @w[$c .. $c + $cs - 1];
1620 1667
1621 if ($w > $sw) { 1668 if ($w > $sw) {
1622 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1669 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1623 } 1670 }
1624 } 1671 }
1625 1672
1626 # second pass, rows 1673 # second pass, rows
1627 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1674 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1628 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)};
1629 1676
1630 my $sh = sum @h[$r .. $r + $rs - 1]; 1677 my $sh = sum @h[$r .. $r + $rs - 1];
1631 1678
1632 if ($h > $sh) { 1679 if ($h > $sh) {
1633 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1680 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1634 } 1681 }
1635 } 1682 }
1636 1683
1637 (\@w, \@h) 1684 (\@w, \@h)
1638} 1685}
1661 @col_expand = (1) x @$ws unless @col_expand; 1708 @col_expand = (1) x @$ws unless @col_expand;
1662 my $col_expand = (sum @col_expand) || 1; 1709 my $col_expand = (sum @col_expand) || 1;
1663 1710
1664 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1711 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1665 1712
1666 CFPlus::UI::harmonize $ws; 1713 DC::UI::harmonize $ws;
1667 1714
1668 my @row_expand = @{$self->{row_expand}}; 1715 my @row_expand = @{$self->{row_expand}};
1669 @row_expand = (1) x @$ws unless @row_expand; 1716 @row_expand = (1) x @$ws unless @row_expand;
1670 my $row_expand = (sum @row_expand) || 1; 1717 my $row_expand = (sum @row_expand) || 1;
1671 1718
1672 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1719 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1673 1720
1674 CFPlus::UI::harmonize $hs; 1721 DC::UI::harmonize $hs;
1675 1722
1676 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1723 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1677 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1724 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1678 1725
1679 for my $widget ($self->children) { 1726 for my $widget ($self->children) {
1688 1 1735 1
1689} 1736}
1690 1737
1691############################################################################# 1738#############################################################################
1692 1739
1693package CFPlus::UI::Fixed; 1740package DC::UI::Fixed;
1694 1741
1695use List::Util qw(min max); 1742use List::Util qw(min max);
1696 1743
1697our @ISA = CFPlus::UI::Container::; 1744our @ISA = DC::UI::Container::;
1698 1745
1699sub _scale($$$) { 1746sub _scale($$$) {
1700 my ($rel, $val, $max) = @_; 1747 my ($rel, $val, $max) = @_;
1701 1748
1702 $rel ? $val * $max : $val 1749 $rel ? $val * $max : $val
1754 1 1801 1
1755} 1802}
1756 1803
1757############################################################################# 1804#############################################################################
1758 1805
1759package CFPlus::UI::Box; 1806package DC::UI::Box;
1760 1807
1761our @ISA = CFPlus::UI::Container::; 1808our @ISA = DC::UI::Container::;
1762 1809
1763sub size_request { 1810sub size_request {
1764 my ($self) = @_; 1811 my ($self) = @_;
1765 1812
1766 my @children = $self->visible_children; 1813 my @children = $self->visible_children;
1793 if ($req > $space) { 1840 if ($req > $space) {
1794 # ah well, not enough space 1841 # ah well, not enough space
1795 $_ *= $space / $req for @req; 1842 $_ *= $space / $req for @req;
1796 } else { 1843 } else {
1797 my $expand = (List::Util::sum map $_->{expand}, @children) || 1; 1844 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1798 1845
1799 $space = ($space - $req) / $expand; # remaining space to give away 1846 $space = ($space - $req) / $expand; # remaining space to give away
1800 1847
1801 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1802 for 0 .. $#children; 1849 for 0 .. $#children;
1803 } 1850 }
1804 } 1851 }
1805 1852
1806 CFPlus::UI::harmonize \@req; 1853 DC::UI::harmonize \@req;
1807 1854
1808 my $pos = 0; 1855 my $pos = 0;
1809 for (0 .. $#children) { 1856 for (0 .. $#children) {
1810 my $alloc = $req[$_]; 1857 my $alloc = $req[$_];
1811 $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));
1816 1 1863 1
1817} 1864}
1818 1865
1819############################################################################# 1866#############################################################################
1820 1867
1821package CFPlus::UI::HBox; 1868package DC::UI::HBox;
1822 1869
1823our @ISA = CFPlus::UI::Box::; 1870our @ISA = DC::UI::Box::;
1824 1871
1825sub new { 1872sub new {
1826 my $class = shift; 1873 my $class = shift;
1827 1874
1828 $class->SUPER::new ( 1875 $class->SUPER::new (
1831 ) 1878 )
1832} 1879}
1833 1880
1834############################################################################# 1881#############################################################################
1835 1882
1836package CFPlus::UI::VBox; 1883package DC::UI::VBox;
1837 1884
1838our @ISA = CFPlus::UI::Box::; 1885our @ISA = DC::UI::Box::;
1839 1886
1840sub new { 1887sub new {
1841 my $class = shift; 1888 my $class = shift;
1842 1889
1843 $class->SUPER::new ( 1890 $class->SUPER::new (
1846 ) 1893 )
1847} 1894}
1848 1895
1849############################################################################# 1896#############################################################################
1850 1897
1851package CFPlus::UI::Label; 1898package DC::UI::Label;
1852 1899
1853our @ISA = CFPlus::UI::DrawBG::; 1900our @ISA = DC::UI::DrawBG::;
1854 1901
1855use CFPlus::OpenGL; 1902use DC::OpenGL;
1856 1903
1857sub new { 1904sub new {
1858 my ($class, %arg) = @_; 1905 my ($class, %arg) = @_;
1859 1906
1860 my $self = $class->SUPER::new ( 1907 my $self = $class->SUPER::new (
1865 #text => initial text 1912 #text => initial text
1866 #markup => initial narkup 1913 #markup => initial narkup
1867 #max_w => maximum pixel width 1914 #max_w => maximum pixel width
1868 #style => 0, # render flags 1915 #style => 0, # render flags
1869 ellipsise => 3, # end 1916 ellipsise => 3, # end
1870 layout => (new CFPlus::Layout), 1917 layout => (new DC::Layout),
1871 fontsize => 1, 1918 fontsize => 1,
1872 align => -1, 1919 align => 0.5,
1873 valign => -1, 1920 valign => 0.5,
1874 padding_x => 2, 1921 padding_x => 4,
1875 padding_y => 2, 1922 padding_y => 2,
1876 can_events => 0, 1923 can_events => 0,
1877 %arg 1924 %arg
1878 ); 1925 );
1879 1926
1880 if (exists $self->{template}) { 1927 if (exists $self->{template}) {
1881 my $layout = new CFPlus::Layout; 1928 my $layout = new DC::Layout;
1882 $layout->set_text (delete $self->{template}); 1929 $layout->set_text (delete $self->{template});
1883 $self->{template} = $layout; 1930 $self->{template} = $layout;
1884 } 1931 }
1885 1932
1886 if (exists $self->{markup}) { 1933 if (exists $self->{markup}) {
1942 1989
1943sub size_request { 1990sub size_request {
1944 my ($self) = @_; 1991 my ($self) = @_;
1945 1992
1946 $self->{size_req} ||= do { 1993 $self->{size_req} ||= do {
1994 my ($max_w, $max_h) = $self->get_max_wh;
1995
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1997 $self->{layout}->set_width ($max_w);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2000 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 2001
1953 my ($w, $h) = $self->{layout}->size; 2002 my ($w, $h) = $self->{layout}->size;
1954 2003
1955 if (exists $self->{template}) { 2004 if (exists $self->{template}) {
1956 $self->{template}->set_font ($self->{font}) if $self->{font}; 2005 $self->{template}->set_font ($self->{font}) if $self->{font};
1957 $self->{template}->set_width ($self->{max_w} || -1); 2006 $self->{template}->set_width ($max_w);
1958 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2007 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1959 2008
1960 my ($w2, $h2) = $self->{template}->size; 2009 my ($w2, $h2) = $self->{template}->size;
1961 2010
1962 $w = List::Util::max $w, $w2; 2011 $w = List::Util::max $w, $w2;
2018 2067
2019 [$self->{layout}->size] 2068 [$self->{layout}->size]
2020 }; 2069 };
2021 2070
2022 unless (exists $self->{ox}) { 2071 unless (exists $self->{ox}) {
2023 $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);
2024 : $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);
2025 : ($self->{w} - $size->[0]) * 0.5);
2026
2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2029 : ($self->{h} - $size->[1]) * 0.5);
2030 2074
2031 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2075 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2032 }; 2076 };
2033 2077
2034# unless ($self->{list}) { 2078# unless ($self->{list}) {
2035# $self->{list} = CFPlus::OpenGL::glGenList; 2079# $self->{list} = DC::OpenGL::glGenList;
2036# CFPlus::OpenGL::glNewList $self->{list}; 2080# DC::OpenGL::glNewList $self->{list};
2037# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2081# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2038# CFPlus::OpenGL::glEndList; 2082# DC::OpenGL::glEndList;
2039# } 2083# }
2040# 2084#
2041# CFPlus::OpenGL::glCallList $self->{list}; 2085# DC::OpenGL::glCallList $self->{list};
2042 2086
2043 $self->{layout}->draw; 2087 $self->{layout}->draw;
2044} 2088}
2045 2089
2046#sub destroy { 2090#sub destroy {
2047# my ($self) = @_; 2091# my ($self) = @_;
2048# 2092#
2049# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2093# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2050# 2094#
2051# $self->SUPER::destroy; 2095# $self->SUPER::destroy;
2052#} 2096#}
2053 2097
2054############################################################################# 2098#############################################################################
2055 2099
2056package CFPlus::UI::EntryBase; 2100package DC::UI::EntryBase;
2057 2101
2058our @ISA = CFPlus::UI::Label::; 2102our @ISA = DC::UI::Label::;
2059 2103
2060use CFPlus::OpenGL; 2104use DC::OpenGL;
2061 2105
2062sub new { 2106sub new {
2063 my $class = shift; 2107 my $class = shift;
2064 2108
2065 $class->SUPER::new ( 2109 $class->SUPER::new (
2066 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2067 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2068 outline => [0.6, 0.3, 0.1], 2112 outline => undef,
2069 active_bg => [0, 0, 1, .2], 2113 active_bg => [0, 0, 1, .2],
2070 active_fg => [1, 1, 1], 2114 active_fg => [1, 1, 1],
2071 active_outline => [1, 1, 0], 2115 active_outline => [1, 1, 0],
2072 can_hover => 1, 2116 can_hover => 1,
2073 can_focus => 1, 2117 can_focus => 1,
2118 align => 0,
2074 valign => 0, 2119 valign => 0.5,
2075 can_events => 1, 2120 can_events => 1,
2076 ellipsise => 0, 2121 ellipsise => 0,
2122 padding_x => 4,
2123 padding_y => 2,
2077 #text => ... 2124 #text => ...
2078 #hidden => "*", 2125 #hidden => "*",
2079 @_ 2126 @_
2080 ) 2127 )
2081} 2128}
2128 2175
2129 my $text = $self->get_text; 2176 my $text = $self->get_text;
2130 2177
2131 $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;
2132 2179
2133 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2134 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2135 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2136 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2137 } elsif ($sym == CFPlus::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2138 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2139 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2140 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2141 } elsif ($sym == CFPlus::SDLK_HOME) { 2188 } elsif ($sym == DC::SDLK_HOME) {
2142 # what a hack 2189 # what a hack
2143 $self->{cursor} = 2190 $self->{cursor} =
2144 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2191 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2145 ? length $1 2192 ? length $1
2146 : 0; 2193 : 0;
2147 } elsif ($sym == CFPlus::SDLK_END) { 2194 } elsif ($sym == DC::SDLK_END) {
2148 # uh, again 2195 # uh, again
2149 $self->{cursor} = 2196 $self->{cursor} =
2150 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2197 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2151 ? $self->{cursor} + length $1 2198 ? $self->{cursor} + length $1
2152 : length $self->{text}; 2199 : length $self->{text};
2191 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; 2238 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2192 $self->{cursor} = length $text; 2239 $self->{cursor} = length $text;
2193 2240
2194 $self->_set_text ($self->{text}); 2241 $self->_set_text ($self->{text});
2195 $self->update; 2242 $self->update;
2196 2243
2197 1 2244 1
2198} 2245}
2199 2246
2200sub invoke_mouse_motion { 2247sub invoke_mouse_motion {
2201 my ($self, $ev, $x, $y) = @_; 2248 my ($self, $ev, $x, $y) = @_;
2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2280 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2234 } 2281 }
2235 2282
2236 glColor_premultiply @{$self->{active_fg}}; 2283 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2284 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2285 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2286 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2287 glEnd;
2241 2288
2242 glLineWidth 3; 2289 glLineWidth 3;
2243 glColor @{$self->{active_outline}}; 2290 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2291 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2245 glLineWidth 1; 2292 glLineWidth 1;
2246 2293
2247 } else { 2294 } else {
2248 glColor @{$self->{outline}}; 2295 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP; 2296 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5; 2297 glVertex .5, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3; 2298 glVertex .5, $self->{h} - 2.5;
2253 glVertex $self->{w} - 1, $self->{h} - 3; 2299 glVertex $self->{w} - .5, $self->{h} - 2.5;
2254 glVertex $self->{w} - 1, $self->{h} * .5; 2300 glVertex $self->{w} - .5, $self->{h} * .5;
2255 glEnd; 2301 glEnd;
2256 } 2302 }
2257} 2303}
2258 2304
2259############################################################################# 2305#############################################################################
2260 2306
2261package CFPlus::UI::Entry; 2307package DC::UI::Entry;
2262 2308
2263our @ISA = CFPlus::UI::EntryBase::; 2309our @ISA = DC::UI::EntryBase::;
2264 2310
2265use 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
2266 2322
2267sub invoke_key_down { 2323sub invoke_key_down {
2268 my ($self, $ev) = @_; 2324 my ($self, $ev) = @_;
2269 2325
2270 my $sym = $ev->{sym}; 2326 my $sym = $ev->{sym};
2276 $self->{history_pointer} = -1; 2332 $self->{history_pointer} = -1;
2277 $self->{history_saveback} = ''; 2333 $self->{history_saveback} = '';
2278 $self->emit (activate => $txt); 2334 $self->emit (activate => $txt);
2279 $self->update; 2335 $self->update;
2280 2336
2281 } elsif ($sym == CFPlus::SDLK_UP) { 2337 } elsif ($sym == DC::SDLK_UP) {
2282 if ($self->{history_pointer} < 0) { 2338 if ($self->{history_pointer} < 0) {
2283 $self->{history_saveback} = $self->get_text; 2339 $self->{history_saveback} = $self->get_text;
2284 } 2340 }
2285 if (@{$self->{history} || []} > 0) { 2341 if (@{$self->{history} || []} > 0) {
2286 $self->{history_pointer}++; 2342 $self->{history_pointer}++;
2288 $self->{history_pointer} = @{$self->{history} || []} - 1; 2344 $self->{history_pointer} = @{$self->{history} || []} - 1;
2289 } 2345 }
2290 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2346 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2291 } 2347 }
2292 2348
2293 } elsif ($sym == CFPlus::SDLK_DOWN) { 2349 } elsif ($sym == DC::SDLK_DOWN) {
2294 $self->{history_pointer}--; 2350 $self->{history_pointer}--;
2295 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2351 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2296 2352
2297 if ($self->{history_pointer} >= 0) { 2353 if ($self->{history_pointer} >= 0) {
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2354 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2299 } else { 2355 } else {
2356 if (defined $self->{history_saveback}) {
2300 $self->set_text ($self->{history_saveback}); 2357 $self->set_text ($self->{history_saveback});
2358 $self->{history_saveback} = undef;
2359 }
2301 } 2360 }
2302 2361
2303 } else { 2362 } else {
2304 return $self->SUPER::invoke_key_down ($ev) 2363 return $self->SUPER::invoke_key_down ($ev)
2305 } 2364 }
2307 1 2366 1
2308} 2367}
2309 2368
2310############################################################################# 2369#############################################################################
2311 2370
2312package CFPlus::UI::TextEdit; 2371package DC::UI::TextEdit;
2313 2372
2314our @ISA = CFPlus::UI::EntryBase::; 2373our @ISA = DC::UI::EntryBase::;
2315 2374
2316use 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}
2317 2386
2318sub move_cursor_ver { 2387sub move_cursor_ver {
2319 my ($self, $dy) = @_; 2388 my ($self, $dy) = @_;
2320 2389
2321 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2390 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2322 2391
2323 $y += $dy; 2392 $line += $dy;
2324 2393
2325 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))) {
2326 $self->{cursor} = $index; 2395 $self->{cursor} = $index;
2327 delete $self->{cur_h}; 2396 delete $self->{cur_h};
2328 $self->update; 2397 $self->update;
2329 return; 2398 return;
2330 } 2399 }
2333sub invoke_key_down { 2402sub invoke_key_down {
2334 my ($self, $ev) = @_; 2403 my ($self, $ev) = @_;
2335 2404
2336 my $sym = $ev->{sym}; 2405 my $sym = $ev->{sym};
2337 2406
2338 if ($sym == CFPlus::SDLK_UP) { 2407 if ($sym == DC::SDLK_UP) {
2339 $self->move_cursor_ver (-1); 2408 $self->move_cursor_ver (-1);
2340 } elsif ($sym == CFPlus::SDLK_DOWN) { 2409 } elsif ($sym == DC::SDLK_DOWN) {
2341 $self->move_cursor_ver (+1); 2410 $self->move_cursor_ver (+1);
2342 } else { 2411 } else {
2343 return $self->SUPER::invoke_key_down ($ev) 2412 return $self->SUPER::invoke_key_down ($ev)
2344 } 2413 }
2345 2414
2346 1 2415 1
2347} 2416}
2348 2417
2349############################################################################# 2418#############################################################################
2350 2419
2351package CFPlus::UI::ButtonBin; 2420package DC::UI::ButtonBin;
2352 2421
2353our @ISA = CFPlus::UI::Bin::; 2422our @ISA = DC::UI::Bin::;
2354 2423
2355use CFPlus::OpenGL; 2424use DC::OpenGL;
2356 2425
2357my @tex = 2426my @tex =
2358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2359 qw(b1_button_inactive.png b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2360 2429
2361sub new { 2430sub new {
2362 my $class = shift; 2431 my $class = shift;
2363 2432
2364 $class->SUPER::new ( 2433 $class->SUPER::new (
2365 can_hover => 1, 2434 can_hover => 1,
2366 align => 0, 2435 align => 0.5,
2367 valign => 0, 2436 valign => 0.5,
2368 can_events => 1, 2437 can_events => 1,
2369 @_ 2438 @_
2370 ) 2439 )
2371} 2440}
2372 2441
2395 $self->SUPER::_draw; 2464 $self->SUPER::_draw;
2396} 2465}
2397 2466
2398############################################################################# 2467#############################################################################
2399 2468
2400package CFPlus::UI::Button; 2469package DC::UI::Button;
2401 2470
2402our @ISA = CFPlus::UI::Label::; 2471our @ISA = DC::UI::Label::;
2403 2472
2404use CFPlus::OpenGL; 2473use DC::OpenGL;
2405 2474
2406my @tex = 2475my @tex =
2407 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2408 qw(b1_button_inactive.png b1_button_active.png); 2477 qw(b1_button_inactive.png b1_button_active.png);
2409 2478
2410sub new { 2479sub new {
2411 my $class = shift; 2480 my $class = shift;
2412 2481
2413 $class->SUPER::new ( 2482 $class->SUPER::new (
2414 padding_x => 4, 2483 padding_x => 8,
2415 padding_y => 4, 2484 padding_y => 4,
2416 fg => [1.0, 1.0, 1.0], 2485 fg => [1.0, 1.0, 1.0],
2417 active_fg => [0.8, 0.8, 0.8], 2486 active_fg => [0.8, 0.8, 0.8],
2418 can_hover => 1, 2487 can_hover => 1,
2419 align => 0, 2488 align => 0.5,
2420 valign => 0, 2489 valign => 0.5,
2421 can_events => 1, 2490 can_events => 1,
2422 @_ 2491 @_
2423 ) 2492 )
2424} 2493}
2425 2494
2450 $self->SUPER::_draw; 2519 $self->SUPER::_draw;
2451} 2520}
2452 2521
2453############################################################################# 2522#############################################################################
2454 2523
2455package CFPlus::UI::CheckBox; 2524package DC::UI::CheckBox;
2456 2525
2457our @ISA = CFPlus::UI::DrawBG::; 2526our @ISA = DC::UI::DrawBG::;
2458 2527
2459my @tex = 2528my @tex =
2460 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2529 map { new_from_resource DC::Texture $_, mipmap => 1 }
2461 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2530 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2462 2531
2463use CFPlus::OpenGL; 2532use DC::OpenGL;
2464 2533
2465sub new { 2534sub new {
2466 my $class = shift; 2535 my $class = shift;
2467 2536
2468 $class->SUPER::new ( 2537 $class->SUPER::new (
2538 fontsize => 1,
2469 padding_x => 2, 2539 padding_x => 2,
2470 padding_y => 2, 2540 padding_y => 2,
2471 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2472 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2473 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2479} 2549}
2480 2550
2481sub size_request { 2551sub size_request {
2482 my ($self) = @_; 2552 my ($self) = @_;
2483 2553
2484 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2485} 2555}
2486 2556
2487sub toggle { 2557sub toggle {
2488 my ($self) = @_; 2558 my ($self) = @_;
2489 2559
2508sub _draw { 2578sub _draw {
2509 my ($self) = @_; 2579 my ($self) = @_;
2510 2580
2511 $self->SUPER::_draw; 2581 $self->SUPER::_draw;
2512 2582
2513 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2583 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2514 2584
2515 my ($w, $h) = @$self{qw(w h)}; 2585 my ($w, $h) = @$self{qw(w h)};
2516 2586
2517 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2587 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2518 2588
2525 glDisable GL_TEXTURE_2D; 2595 glDisable GL_TEXTURE_2D;
2526} 2596}
2527 2597
2528############################################################################# 2598#############################################################################
2529 2599
2530package CFPlus::UI::Image; 2600package DC::UI::Image;
2531 2601
2532our @ISA = CFPlus::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2533 2603
2534use CFPlus::OpenGL; 2604use DC::OpenGL;
2535 2605
2536our %texture_cache; 2606our %texture_cache;
2537 2607
2538sub new { 2608sub new {
2539 my $class = shift; 2609 my $class = shift;
2546 2616
2547 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2549 2619
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2552 2622
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2554 2624
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2626
2557 $self 2627 $self
2558} 2628}
2559 2629
2560sub STORABLE_freeze { 2630sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2631 my ($self, $cloning) = @_;
2562 2632
2563 $self->{path} 2633 $self->{path}
2564 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";
2565 2635
2566 $self->{path} 2636 $self->{path}
2567} 2637}
2568 2638
2569sub STORABLE_attach { 2639sub STORABLE_attach {
2570 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2571 2641
2572 $self->new (path => $path) 2642 $self->new (path => $path)
2573} 2643}
2574 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2575sub size_request { 2652sub size_request {
2576 my ($self) = @_; 2653 my ($self) = @_;
2577 2654
2578 (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})
2579} 2656}
2580 2657
2581sub _draw { 2658sub _draw {
2582 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2583 2662
2584 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2585 2664
2586 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2587 2666
2600 glDisable GL_TEXTURE_2D; 2679 glDisable GL_TEXTURE_2D;
2601} 2680}
2602 2681
2603############################################################################# 2682#############################################################################
2604 2683
2605package CFPlus::UI::ImageButton; 2684package DC::UI::ImageButton;
2606 2685
2607our @ISA = CFPlus::UI::Image::; 2686our @ISA = DC::UI::Image::;
2608 2687
2609use CFPlus::OpenGL; 2688use DC::OpenGL;
2610
2611my %textures;
2612 2689
2613sub new { 2690sub new {
2614 my $class = shift; 2691 my $class = shift;
2615 2692
2616 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2617 padding_x => 4, 2694 padding_x => 4,
2618 padding_y => 4, 2695 padding_y => 4,
2619 fg => [1, 1, 1], 2696 fg => [1, 1, 1],
2620 active_fg => [0, 0, 1], 2697 active_fg => [0, 0, 1],
2621 can_hover => 1, 2698 can_hover => 1,
2622 align => 0, 2699 align => 0.5,
2623 valign => 0, 2700 valign => 0.5,
2624 can_events => 1, 2701 can_events => 1,
2625 @_ 2702 @_
2626 ); 2703 );
2627} 2704}
2628 2705
2642 1 2719 1
2643} 2720}
2644 2721
2645############################################################################# 2722#############################################################################
2646 2723
2647package CFPlus::UI::VGauge; 2724package DC::UI::VGauge;
2648 2725
2649our @ISA = CFPlus::UI::Base::; 2726our @ISA = DC::UI::Base::;
2650 2727
2651use List::Util qw(min max); 2728use List::Util qw(min max);
2652 2729
2653use CFPlus::OpenGL; 2730use DC::OpenGL;
2654 2731
2655my %tex = ( 2732my %tex = (
2656 food => [ 2733 food => [
2657 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2658 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2659 ], 2736 ],
2660 grace => [ 2737 grace => [
2661 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2662 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/
2663 ], 2740 ],
2664 hp => [ 2741 hp => [
2665 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2666 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2667 ], 2744 ],
2668 mana => [ 2745 mana => [
2669 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2670 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/
2671 ], 2748 ],
2672); 2749);
2673 2750
2674# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
2777 glDisable GL_TEXTURE_2D; 2854 glDisable GL_TEXTURE_2D;
2778} 2855}
2779 2856
2780############################################################################# 2857#############################################################################
2781 2858
2782package CFPlus::UI::Progress; 2859package DC::UI::Progress;
2783 2860
2784our @ISA = CFPlus::UI::Label::; 2861our @ISA = DC::UI::Label::;
2785 2862
2786use CFPlus::OpenGL; 2863use DC::OpenGL;
2787 2864
2788sub new { 2865sub new {
2789 my ($class, %arg) = @_; 2866 my ($class, %arg) = @_;
2790 2867
2791 my $self = $class->SUPER::new ( 2868 my $self = $class->SUPER::new (
2869 padding_x => 2,
2870 padding_y => 2,
2792 fg => [1, 1, 1], 2871 fg => [1, 1, 1],
2793 bg => [0, 0, 1, 0.2], 2872 bg => [0, 0, 1, 0.2],
2794 bar => [0.7, 0.5, 0.1, 0.8], 2873 bar => [0.7, 0.5, 0.1, 0.8],
2795 outline => [0.4, 0.3, 0], 2874 outline => [0.4, 0.3, 0],
2796 fontsize => 0.9, 2875 fontsize => 0.9,
2797 valign => 0, 2876 valign => 0.5,
2798 align => 0, 2877 align => 0.5,
2799 can_events => 1, 2878 can_events => 1,
2800 ellipsise => 1, 2879 ellipsise => 1,
2801 label => "%d%%", 2880 label => "%d%%",
2802 %arg, 2881 %arg,
2803 ); 2882 );
2811 my ($self, $label) = @_; 2890 my ($self, $label) = @_;
2812 2891
2813 return if $self->{label} eq $label; 2892 return if $self->{label} eq $label;
2814 $self->{label} = $label; 2893 $self->{label} = $label;
2815 2894
2816 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2895 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2817} 2896}
2818 2897
2819sub set_value { 2898sub set_value {
2820 my ($self, $value) = @_; 2899 my ($self, $value) = @_;
2821 2900
2836 my ($self) = @_; 2915 my ($self) = @_;
2837 2916
2838 glEnable GL_BLEND; 2917 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2918 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840 2919
2920 my $px = $self->{padding_x};
2921 my $py = $self->{padding_y};
2922
2841 if ($self->{value} >= 0) { 2923 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2924 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2843 2925
2844 glColor_premultiply @{$self->{bar}}; 2926 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2; 2927 glRect $px, $py, $s, $self->{h} - $py;
2846 glColor_premultiply @{$self->{bg}}; 2928 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2929 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2848 } 2930 }
2849 2931
2850 glColor_premultiply @{$self->{outline}}; 2932 glColor_premultiply @{$self->{outline}};
2933
2934 $px -= .5;
2935 $py -= .5;
2936
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2937 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2852 2938
2853 glDisable GL_BLEND; 2939 glDisable GL_BLEND;
2854 2940
2855 { 2941 {
2856 local $self->{bg}; # do not draw background 2942 local $self->{bg}; # do not draw background
2858 } 2944 }
2859} 2945}
2860 2946
2861############################################################################# 2947#############################################################################
2862 2948
2863package CFPlus::UI::ExperienceProgress; 2949package DC::UI::ExperienceProgress;
2864 2950
2865our @ISA = CFPlus::UI::Progress::; 2951our @ISA = DC::UI::Progress::;
2866 2952
2867sub new { 2953sub new {
2868 my ($class, %arg) = @_; 2954 my ($class, %arg) = @_;
2869 2955
2956 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2957
2870 my $self = $class->SUPER::new ( 2958 my $self = $class->SUPER::new (
2959 %arg,
2871 tooltip => sub { 2960 tooltip => sub {
2872 my ($self) = @_; 2961 my ($self) = @_;
2873 2962
2874 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,
2875 $self->{lvl}, 2965 $self->{lvl},
2876 ::formsep ($self->{exp}), 2966 ::formsep ($self->{exp}),
2877 ::formsep ($self->{nxt}), 2967 ::formsep ($self->{nxt}),
2878 ::formsep ($self->{nxt} - $self->{exp}), 2968 ::formsep ($self->{nxt} - $self->{exp}),
2969 $self->_percent * 100,
2879 }, 2970 },
2880 %arg
2881 ); 2971 );
2882 2972
2883 $::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}) }
2884 if $::CONN; 2974 if $::CONN;
2885 2975
2893 if $::CONN; 2983 if $::CONN;
2894 2984
2895 $self->SUPER::DESTROY; 2985 $self->SUPER::DESTROY;
2896} 2986}
2897 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
2898sub set_value { 3002sub set_value {
2899 my ($self, $lvl, $exp) = @_; 3003 my ($self, $lvl, $exp) = @_;
2900 3004
2901 $self->{lvl} = $lvl; 3005 $self->{lvl} = $lvl;
2902 $self->{exp} = $exp; 3006 $self->{exp} = $exp;
2903 3007
2904 my $v = -1;
2905
2906 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2907 my $l0 = $table->[$lvl - 1];
2908 my $l1 = $table->[$lvl];
2909
2910 $self->{nxt} = $l1;
2911
2912 $v = ($exp - $l0) / ($l1 - $l0);
2913 }
2914
2915 $self->SUPER::set_value ($v); 3008 $self->SUPER::set_value ($self->_percent);
2916} 3009}
2917 3010
2918############################################################################# 3011#############################################################################
2919 3012
2920package CFPlus::UI::Gauge; 3013package DC::UI::Gauge;
2921 3014
2922our @ISA = CFPlus::UI::VBox::; 3015our @ISA = DC::UI::VBox::;
2923 3016
2924sub new { 3017sub new {
2925 my ($class, %arg) = @_; 3018 my ($class, %arg) = @_;
2926 3019
2927 my $self = $class->SUPER::new ( 3020 my $self = $class->SUPER::new (
2929 can_hover => 1, 3022 can_hover => 1,
2930 can_events => 1, 3023 can_events => 1,
2931 %arg, 3024 %arg,
2932 ); 3025 );
2933 3026
2934 $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");
2935 $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);
2936 $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");
2937 3030
2938 $self 3031 $self
2939} 3032}
2940 3033
2941sub set_fontsize { 3034sub set_fontsize {
2962 $self->{value}->set_text ($val); 3055 $self->{value}->set_text ($val);
2963} 3056}
2964 3057
2965############################################################################# 3058#############################################################################
2966 3059
2967package CFPlus::UI::Slider; 3060package DC::UI::Slider;
2968 3061
2969use strict; 3062use common::sense;
2970 3063
2971use CFPlus::OpenGL; 3064use DC::OpenGL;
2972 3065
2973our @ISA = CFPlus::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
2974 3067
2975my @tex = 3068my @tex =
2976 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
2977 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
2978 3071
2979sub new { 3072sub new {
2980 my $class = shift; 3073 my $class = shift;
2981 3074
3018sub set_value { 3111sub set_value {
3019 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
3020 3113
3021 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3022 3115
3023 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
3024 3117
3025 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
3026
3027 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
3028 $value = $hi - $page if $value > $hi - $page;
3029 3120
3030 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3031 if $unit; 3122 if $unit;
3032 3123
3033 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3048 my ($self, $ev, $x, $y) = @_; 3139 my ($self, $ev, $x, $y) = @_;
3049 3140
3050 $self->SUPER::invoke_button_down ($ev, $x, $y); 3141 $self->SUPER::invoke_button_down ($ev, $x, $y);
3051 3142
3052 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3143 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
3053 3144
3054 $self->invoke_mouse_motion ($ev, $x, $y); 3145 $self->invoke_mouse_motion ($ev, $x, $y);
3055 3146
3056 1 3147 1
3057} 3148}
3058 3149
3062 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
3063 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3064 3155
3065 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
3066 3157
3067 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3068 3159
3069 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3070 } else { 3161 } else {
3071 return 0; 3162 return 0;
3072 } 3163 }
3077sub invoke_mouse_wheel { 3168sub invoke_mouse_wheel {
3078 my ($self, $ev) = @_; 3169 my ($self, $ev) = @_;
3079 3170
3080 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3171 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3081 3172
3082 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3173 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3083 3174
3084 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3175 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3085 3176
3086 1 3177 1
3087} 3178}
3097 my ($self) = @_; 3188 my ($self) = @_;
3098 3189
3099 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
3100 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
3101 3192
3102 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3103 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
3104 3195
3105 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};
3106 3197
3107 $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;
3108 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3109 3200
3110 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
3118 3209
3119 glScale $self->{w}, $self->{h}; 3210 glScale $self->{w}, $self->{h};
3120 3211
3121 if ($self->{vertical}) { 3212 if ($self->{vertical}) {
3122 # draw a vertical slider like a rotated horizontal slider 3213 # draw a vertical slider like a rotated horizontal slider
3123 3214
3124 glTranslate 1, 0, 0; 3215 glTranslate 1, 0, 0;
3125 glRotate 90, 0, 0, 1; 3216 glRotate 90, 0, 0, 1;
3126 } 3217 }
3127 3218
3128 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3219 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
3140 glDisable GL_TEXTURE_2D; 3231 glDisable GL_TEXTURE_2D;
3141} 3232}
3142 3233
3143############################################################################# 3234#############################################################################
3144 3235
3145package CFPlus::UI::ValSlider; 3236package DC::UI::ValSlider;
3146 3237
3147our @ISA = CFPlus::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
3148 3239
3149sub new { 3240sub new {
3150 my ($class, %arg) = @_; 3241 my ($class, %arg) = @_;
3151 3242
3152 my $range = delete $arg{range}; 3243 my $range = delete $arg{range};
3153 3244
3154 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
3155 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3246 slider => (new DC::UI::Slider expand => 1, range => $range),
3156 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3247 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3157 to_value => sub { shift }, 3248 to_value => sub { shift },
3158 from_value => sub { shift }, 3249 from_value => sub { shift },
3159 %arg, 3250 %arg,
3160 ); 3251 );
3161 3252
3181sub set_range { shift->{slider}->set_range (@_) } 3272sub set_range { shift->{slider}->set_range (@_) }
3182sub set_value { shift->{slider}->set_value (@_) } 3273sub set_value { shift->{slider}->set_value (@_) }
3183 3274
3184############################################################################# 3275#############################################################################
3185 3276
3186package CFPlus::UI::TextScroller; 3277package DC::UI::TextScroller;
3187 3278
3188our @ISA = CFPlus::UI::HBox::; 3279our @ISA = DC::UI::HBox::;
3189 3280
3190use CFPlus::OpenGL; 3281use DC::OpenGL;
3191 3282
3192sub new { 3283sub new {
3193 my $class = shift; 3284 my $class = shift;
3194 3285
3195 my $self = $class->SUPER::new ( 3286 my $self = $class->SUPER::new (
3196 fontsize => 1, 3287 fontsize => 1,
3197 can_events => 1, 3288 can_events => 1,
3198 indent => 0, 3289 indent => 0,
3199 #font => default_font 3290 #font => default_font
3200 @_, 3291 @_,
3201 3292
3202 layout => (new CFPlus::Layout), 3293 layout => (new DC::Layout),
3203 par => [],
3204 max_par => 0, 3294 max_par => 0,
3205 height => 0, 3295 height => 0,
3206 children => [ 3296 children => [
3207 (new CFPlus::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3208 (new CFPlus::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3209 ], 3299 ],
3210 ); 3300 );
3211 3301
3212 $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} };
3213 3305
3214 $self 3306 $self
3215} 3307}
3216 3308
3217sub set_fontsize { 3309sub set_fontsize {
3269 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3361 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3270 $layout->set_markup ($para->{markup}); 3362 $layout->set_markup ($para->{markup});
3271 3363
3272 $layout->set_shapes ( 3364 $layout->set_shapes (
3273 map 3365 map
3274 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3366 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3275 @{$para->{widget}} 3367 @{$para->{widget}}
3276 ); 3368 );
3277 3369
3278 $layout 3370 $layout
3279} 3371}
3396 $ROOT->on_post_alloc ($self => sub { 3488 $ROOT->on_post_alloc ($self => sub {
3397 $self->force_uptodate; 3489 $self->force_uptodate;
3398 3490
3399 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3491 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3400 3492
3401 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3493 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3402 glClearColor 0, 0, 0, 0; 3494 glClearColor 0, 0, 0, 0;
3403 glClear GL_COLOR_BUFFER_BIT; 3495 glClear GL_COLOR_BUFFER_BIT;
3404 3496
3405 package CFPlus::UI::Base; 3497 package DC::UI::Base;
3406 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3498 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3407 (0, 0, $self->{w}, $self->{h}); 3499 (0, 0, $self->{w}, $self->{h});
3408 3500
3409 my $top = int $self->{children}[1]{range}[0]; 3501 my $top = int $self->{children}[1]{range}[0];
3410 3502
3469 $self->{children}[1]->draw; 3561 $self->{children}[1]->draw;
3470} 3562}
3471 3563
3472############################################################################# 3564#############################################################################
3473 3565
3474package CFPlus::UI::Animator; 3566package DC::UI::Animator;
3475 3567
3476use CFPlus::OpenGL; 3568use DC::OpenGL;
3477 3569
3478our @ISA = CFPlus::UI::Bin::; 3570our @ISA = DC::UI::Bin::;
3479 3571
3480sub moveto { 3572sub moveto {
3481 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3482 3574
3483 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3484 $self->{speed} = 0.001; 3576 $self->{speed} = 0.001;
3485 $self->{time} = 1; 3577 $self->{time} = 1;
3486 3578
3487 ::animation_start $self; 3579 ::animation_start $self;
3488} 3580}
3489 3581
3490sub animate { 3582sub animate {
3491 my ($self, $interval) = @_; 3583 my ($self, $interval) = @_;
3495 $self->{time} = 0; 3587 $self->{time} = 0;
3496 ::animation_stop $self; 3588 ::animation_stop $self;
3497 } 3589 }
3498 3590
3499 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; 3591 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3500 3592
3501 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); 3593 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3502 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); 3594 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3503} 3595}
3504 3596
3505sub _draw { 3597sub _draw {
3511 glPopMatrix; 3603 glPopMatrix;
3512} 3604}
3513 3605
3514############################################################################# 3606#############################################################################
3515 3607
3516package CFPlus::UI::Flopper; 3608package DC::UI::Flopper;
3517 3609
3518our @ISA = CFPlus::UI::Button::; 3610our @ISA = DC::UI::Button::;
3519 3611
3520sub new { 3612sub new {
3521 my $class = shift; 3613 my $class = shift;
3522 3614
3523 my $self = $class->SUPER::new ( 3615 my $self = $class->SUPER::new (
3535 $self->{other}->toggle_visibility; 3627 $self->{other}->toggle_visibility;
3536} 3628}
3537 3629
3538############################################################################# 3630#############################################################################
3539 3631
3540package CFPlus::UI::Tooltip; 3632package DC::UI::Tooltip;
3541 3633
3542our @ISA = CFPlus::UI::Bin::; 3634our @ISA = DC::UI::Bin::;
3543 3635
3544use CFPlus::OpenGL; 3636use DC::OpenGL;
3545 3637
3546sub new { 3638sub new {
3547 my $class = shift; 3639 my $class = shift;
3548 3640
3549 $class->SUPER::new ( 3641 $class->SUPER::new (
3550 @_, 3642 @_,
3551 can_events => 0, 3643 can_events => 0,
3552 ) 3644 )
3553} 3645}
3554 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
3555sub set_tooltip_from { 3699sub set_tooltip_from {
3556 my ($self, $widget) = @_; 3700 my ($self, $widget) = @_;
3557 3701
3558 my $tip = $widget->{tooltip}; 3702 my $tip = $widget->{tooltip};
3559 $tip = $tip->($widget) if "CODE" eq ref $tip; 3703 $tip = $tip->($widget) if "CODE" eq ref $tip;
3560
3561 $tip = CFPlus::Pod::section_label tooltip => $1
3562 if $tip =~ /^#(.*)$/;
3563 3704
3564 if ($ENV{CFPLUS_DEBUG} & 2) {
3565 $tip .= "\n\n" . (ref $widget) . "\n"
3566 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3567 . "req $widget->{req_w} $widget->{req_h}\n"
3568 . "visible $widget->{visible}";
3569 }
3570
3571 $tip =~ s/^\n+//;
3572 $tip =~ s/\n+$//;
3573
3574 $self->add (new CFPlus::UI::Label 3705 $self->add (new DC::UI::Label
3575 markup => $tip, 3706 fg => $DC::THEME{tooltip_fg},
3576 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3707 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3708 align => 0,
3577 fontsize => 0.8, 3709 fontsize => 0.8,
3578 style => 1, # FLAG_INVERSE 3710 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3579 ellipsise => 0, 3711 ellipsise => 0,
3580 font => ($widget->{tooltip_font} || $::FONT_PROP), 3712 font => ($widget->{tooltip_font} || $::FONT_PROP),
3581 ); 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 };
3582} 3727}
3583 3728
3584sub size_request { 3729sub size_request {
3585 my ($self) = @_; 3730 my ($self) = @_;
3586 3731
3618} 3763}
3619 3764
3620sub _draw { 3765sub _draw {
3621 my ($self) = @_; 3766 my ($self) = @_;
3622 3767
3623 glTranslate 0.375, 0.375;
3624
3625 my ($w, $h) = @$self{qw(w h)}; 3768 my ($w, $h) = @$self{qw(w h)};
3626 3769
3627 glColor 1, 0.8, 0.4; 3770 glColor @{ $DC::THEME{tooltip_bg} };
3628 glRect 0, 0, $w, $h; 3771 glRect 0, 0, $w, $h;
3629 3772
3630 glColor 0, 0, 0; 3773 glColor @{ $DC::THEME{tooltip_border} };
3631 glRect_lineloop 0, 0, $w, $h; 3774 glRect_lineloop .5, .5, $w + .5, $h + .5;
3632 3775
3633 glTranslate 2 - 0.375, 2 - 0.375; 3776 glTranslate 2, 2;
3634 3777
3635 $self->SUPER::_draw; 3778 $self->SUPER::_draw;
3636} 3779}
3637 3780
3638############################################################################# 3781#############################################################################
3639 3782
3640package CFPlus::UI::Face; 3783package DC::UI::Face;
3641 3784
3642our @ISA = CFPlus::UI::DrawBG::; 3785our @ISA = DC::UI::DrawBG::;
3643 3786
3644use CFPlus::OpenGL; 3787use DC::OpenGL;
3645 3788
3646sub new { 3789sub new {
3647 my $class = shift; 3790 my $class = shift;
3648 3791
3649 my $self = $class->SUPER::new ( 3792 my $self = $class->SUPER::new (
3652 aspect => 1, 3795 aspect => 1,
3653 can_events => 0, 3796 can_events => 0,
3654 @_, 3797 @_,
3655 ); 3798 );
3656 3799
3657 if ($self->{anim} && $self->{animspeed}) {
3658 CFPlus::weaken (my $widget = $self);
3659
3660 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3661 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3662 $self->{timer} = Event->timer (
3663 parked => 1,
3664 cb => sub {
3665 return unless $::CONN;
3666
3667 my $w = $widget
3668 or return;
3669
3670 ++$w->{frame};
3671 $w->update_face;
3672
3673 # somehow, $widget can go away
3674 $w->update;
3675 $w->update_timer;
3676 },
3677 );
3678
3679 $self->update_face;
3680 $self->update_timer; 3800 $self->update_anim;
3681 } 3801
3682
3683 $self 3802 $self
3684} 3803}
3685 3804
3686sub update_timer { 3805sub update_timer {
3687 my ($self) = @_; 3806 my ($self) = @_;
3688 3807
3689 return unless $self->{timer}; 3808 return unless $self->{timer};
3690 3809
3691 if ($self->{visible}) { 3810 if ($self->{visible}) {
3692 $self->{timer}->at (
3693 $self->{anim_start}
3694 + $self->{animspeed}
3695 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3696 );
3697 $self->{timer}->start; 3811 $self->{timer}->start;
3698 } else { 3812 } else {
3699 $self->{timer}->stop; 3813 $self->{timer}->stop;
3700 } 3814 }
3701} 3815}
3714 $tex->upload (sub { $self->reconfigure }); 3828 $tex->upload (sub { $self->reconfigure });
3715 } 3829 }
3716 } 3830 }
3717 } 3831 }
3718 } 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};
3719 } 3861 }
3720} 3862}
3721 3863
3722sub size_request { 3864sub size_request {
3723 my ($self) = @_; 3865 my ($self) = @_;
3745 return unless $self->{visible}; 3887 return unless $self->{visible};
3746 3888
3747 $self->SUPER::update; 3889 $self->SUPER::update;
3748} 3890}
3749 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
3750sub invoke_visibility_change { 3913sub invoke_visibility_change {
3751 my ($self) = @_; 3914 my ($self) = @_;
3752 3915
3753 $self->update_timer; 3916 $self->update_timer;
3754 3917
3778 $self->SUPER::destroy; 3941 $self->SUPER::destroy;
3779} 3942}
3780 3943
3781############################################################################# 3944#############################################################################
3782 3945
3783package CFPlus::UI::Buttonbar; 3946package DC::UI::Buttonbar;
3784 3947
3785our @ISA = CFPlus::UI::HBox::; 3948our @ISA = DC::UI::HBox::;
3786 3949
3787# TODO: should actually wrap buttons and other goodies. 3950# TODO: should actually wrap buttons and other goodies.
3788 3951
3789############################################################################# 3952#############################################################################
3790 3953
3791package CFPlus::UI::Menu; 3954package DC::UI::Menu;
3792 3955
3793our @ISA = CFPlus::UI::Toplevel::; 3956our @ISA = DC::UI::Toplevel::;
3794 3957
3795use CFPlus::OpenGL; 3958use DC::OpenGL;
3796 3959
3797sub new { 3960sub new {
3798 my $class = shift; 3961 my $class = shift;
3799 3962
3800 my $self = $class->SUPER::new ( 3963 my $self = $class->SUPER::new (
3801 items => [], 3964 items => [],
3802 z => 100, 3965 z => 100,
3803 @_, 3966 @_,
3804 ); 3967 );
3805 3968
3806 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3969 $self->add ($self->{vbox} = new DC::UI::VBox);
3807 3970
3808 for my $item (@{ $self->{items} }) { 3971 for my $item (@{ $self->{items} }) {
3809 my ($widget, $cb, $tooltip) = @$item; 3972 my ($widget, $cb, $tooltip) = @$item;
3810 3973
3811 # handle various types of items, only text for now 3974 # handle various types of items, only text for now
3812 if (!ref $widget) { 3975 if (!ref $widget) {
3813 if ($widget =~ /\t/) { 3976 if ($widget =~ /\t/) {
3814 my ($left, $right) = split /\t/, $widget, 2; 3977 my ($left, $right) = split /\t/, $widget, 2;
3815 3978
3816 $widget = new CFPlus::UI::HBox 3979 $widget = new DC::UI::HBox
3817 can_hover => 1, 3980 can_hover => 1,
3818 can_events => 1, 3981 can_events => 1,
3819 tooltip => $tooltip, 3982 tooltip => $tooltip,
3820 children => [ 3983 children => [
3821 (new CFPlus::UI::Label markup => $left, expand => 1), 3984 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3822 (new CFPlus::UI::Label markup => $right, align => +1), 3985 (new DC::UI::Label markup => $right, align => 1),
3823 ], 3986 ],
3824 ; 3987 ;
3825 3988
3826 } else { 3989 } else {
3827 $widget = new CFPlus::UI::Label 3990 $widget = new DC::UI::Label
3828 can_hover => 1, 3991 can_hover => 1,
3829 can_events => 1, 3992 can_events => 1,
3993 align => 0,
3830 markup => $widget, 3994 markup => $widget,
3831 tooltip => $tooltip; 3995 tooltip => $tooltip;
3832 } 3996 }
3833 } 3997 }
3834 3998
3849 # maybe save $GRAB? must be careful about events... 4013 # maybe save $GRAB? must be careful about events...
3850 $GRAB = $self; 4014 $GRAB = $self;
3851 $self->{button} = $ev->{button}; 4015 $self->{button} = $ev->{button};
3852 4016
3853 $self->show; 4017 $self->show;
3854 $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
3855} 4027}
3856 4028
3857sub invoke_mouse_motion { 4029sub invoke_mouse_motion {
3858 my ($self, $ev, $x, $y) = @_; 4030 my ($self, $ev, $x, $y) = @_;
3859 4031
3880 1 4052 1
3881} 4053}
3882 4054
3883############################################################################# 4055#############################################################################
3884 4056
3885package CFPlus::UI::Multiplexer; 4057package DC::UI::Multiplexer;
3886 4058
3887our @ISA = CFPlus::UI::Container::; 4059our @ISA = DC::UI::Container::;
3888 4060
3889sub new { 4061sub new {
3890 my $class = shift; 4062 my $class = shift;
3891 4063
3892 my $self = $class->SUPER::new ( 4064 my $self = $class->SUPER::new (
3893 @_, 4065 @_,
3894 ); 4066 );
3895 4067
3896 $self->{current} = $self->{children}[0] 4068 $self->set_current_page (0);
3897 if @{ $self->{children} };
3898 4069
3899 $self 4070 $self
3900} 4071}
3901 4072
3902sub add { 4073sub add {
3903 my ($self, @widgets) = @_; 4074 my ($self, @widgets) = @_;
3904 4075
3905 $self->SUPER::add (@widgets); 4076 $self->SUPER::add (@widgets);
3906 4077
3907 $self->{current} = $self->{children}[0] 4078 $self->set_current_page (0)
3908 if @{ $self->{children} }; 4079 if @widgets == @{ $self->{children} };
3909} 4080}
3910 4081
3911sub get_current_page { 4082sub get_current_page {
3912 my ($self) = @_; 4083 my ($self) = @_;
3913 4084
3919 4090
3920 my $widget = ref $page_or_widget 4091 my $widget = ref $page_or_widget
3921 ? $page_or_widget 4092 ? $page_or_widget
3922 : $self->{children}[$page_or_widget]; 4093 : $self->{children}[$page_or_widget];
3923 4094
4095 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4096
3924 $self->{current} = $widget; 4097 if (($self->{current} = $widget)) {
4098 $self->{current}->set_visible if $self->{current} && $self->{visible};
3925 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4099 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3926 4100
3927 $self->emit (page_changed => $self->{current}); 4101 $self->emit (page_changed => $self->{current});
4102 }
3928 4103
3929 $self->realloc; 4104 $self->realloc;
3930} 4105}
3931 4106
3932sub visible_children { 4107sub visible_children {
3933 $_[0]{current} 4108 $_[0]{current} || ()
3934} 4109}
3935 4110
3936sub size_request { 4111sub size_request {
3937 my ($self) = @_; 4112 my ($self) = @_;
3938 4113
4114 $self->{current}
3939 $self->{current}->size_request 4115 ? $self->{current}->size_request
4116 : (0, 0)
3940} 4117}
3941 4118
3942sub invoke_size_allocate { 4119sub invoke_size_allocate {
3943 my ($self, $w, $h) = @_; 4120 my ($self, $w, $h) = @_;
3944 4121
3945 $self->{current}->configure (0, 0, $w, $h); 4122 $self->{current}->configure (0, 0, $w, $h)
4123 if $self->{current};
3946 4124
3947 1 4125 1
3948} 4126}
3949 4127
3950sub _draw { 4128sub _draw {
3951 my ($self) = @_; 4129 my ($self) = @_;
3952 4130
3953 $self->{current}->draw; 4131 $self->{current}->draw
4132 if $self->{current};
3954} 4133}
3955 4134
3956############################################################################# 4135#############################################################################
3957 4136
3958package CFPlus::UI::Notebook; 4137package DC::UI::Notebook;
3959 4138
3960use CFPlus::OpenGL; 4139use DC::OpenGL;
3961 4140
3962our @ISA = CFPlus::UI::VBox::; 4141our @ISA = DC::UI::VBox::;
3963 4142
3964sub new { 4143sub new {
3965 my $class = shift; 4144 my $class = shift;
3966 4145
3967 my $self = $class->SUPER::new ( 4146 my $self = $class->SUPER::new (
3968 buttonbar => (new CFPlus::UI::Buttonbar), 4147 buttonbar => (new DC::UI::Buttonbar),
3969 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4148 multiplexer => (new DC::UI::Multiplexer expand => 1),
3970 active_outline => [1, 1, 0], 4149 active_outline => [.7, .7, 0.2],
3971 # filter => # will be put between multiplexer and $self 4150 # filter => # will be put between multiplexer and $self
3972 @_, 4151 @_,
3973 ); 4152 );
3974 4153
3975 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4154 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3983 4162
3984 for my $child (@$widgets) { 4163 for my $child (@$widgets) {
3985 Scalar::Util::weaken $child; 4164 Scalar::Util::weaken $child;
3986 $child->{c_tab_} ||= do { 4165 $child->{c_tab_} ||= do {
3987 my $tab = 4166 my $tab =
3988 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4167 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3989 ? $child->{c_tab} 4168 ? $child->{c_tab}
3990 : 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];
3991 4170
3992 $tab->connect (activate => sub { 4171 $tab->connect (activate => sub {
3993 $wself->set_current_page ($child); 4172 $wself->set_current_page ($child);
3994 }); 4173 });
3995 4174
4027sub pages { 4206sub pages {
4028 my ($self) = @_; 4207 my ($self) = @_;
4029 $self->{multiplexer}->children 4208 $self->{multiplexer}->children
4030} 4209}
4031 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
4221}
4222
4032sub add_tab { 4223sub add_tab {
4033 my ($self, $title, $widget, $tooltip) = @_; 4224 my ($self, $title, $widget, $tooltip) = @_;
4034 4225
4035 $title = [$title, $tooltip] unless ref $title; 4226 $title = [$title, $tooltip] unless ref $title;
4036 $widget->{c_tab} = $title; 4227 $widget->{c_tab} = $title;
4056 4247
4057 $self->SUPER::_draw (); 4248 $self->SUPER::_draw ();
4058 4249
4059 if (my $cur = $self->{multiplexer}{current}) { 4250 if (my $cur = $self->{multiplexer}{current}) {
4060 if ($cur = $cur->{c_tab_}) { 4251 if ($cur = $cur->{c_tab_}) {
4061 glTranslate $cur->{x}, $cur->{y}; 4252 glTranslate $self->{buttonbar}{x} + $cur->{x},
4253 $self->{buttonbar}{y} + $cur->{y};
4062 glLineWidth 3; 4254 glLineWidth 3;
4255 #glEnable GL_BLEND;
4256 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4063 glColor @{$self->{active_outline}}; 4257 glColor @{$self->{active_outline}};
4064 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4258 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4065 glLineWidth 1; 4259 glLineWidth 1;
4260 #glDisable GL_BLEND;
4066 } 4261 }
4067 } 4262 }
4068} 4263}
4069 4264
4070############################################################################# 4265#############################################################################
4071 4266
4072package CFPlus::UI::Selector; 4267package DC::UI::Selector;
4073 4268
4074use utf8; 4269use utf8;
4075 4270
4076our @ISA = CFPlus::UI::Button::; 4271our @ISA = DC::UI::Button::;
4077 4272
4078sub new { 4273sub new {
4079 my $class = shift; 4274 my $class = shift;
4080 4275
4081 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4098 my ($value, $title, $tooltip) = @$_; 4293 my ($value, $title, $tooltip) = @$_;
4099 4294
4100 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4295 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4101 } 4296 }
4102 4297
4103 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4298 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4104} 4299}
4105 4300
4106sub _set_value { 4301sub _set_value {
4107 my ($self, $value) = @_; 4302 my ($self, $value) = @_;
4108 4303
4109 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4304 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4305 $item ||= $self->{options}[0]
4110 or return; 4306 or return;
4111 4307
4112 $self->{value} = $item->[0]; 4308 $self->{value} = $item->[0];
4113 $self->set_markup ("$item->[1] ⇓"); 4309 $self->set_markup ("$item->[1] ⇓");
4114 $self->set_tooltip ($item->[2]); 4310# $self->set_tooltip ($item->[2]);
4115} 4311}
4116 4312
4117sub set_value { 4313sub set_value {
4118 my ($self, $value) = @_; 4314 my ($self, $value) = @_;
4119 4315
4121 4317
4122 $self->_set_value ($value); 4318 $self->_set_value ($value);
4123 $self->emit (changed => $value); 4319 $self->emit (changed => $value);
4124} 4320}
4125 4321
4322sub set_options {
4323 my ($self, $options) = @_;
4324
4325 $self->{options} = $options;
4326 $self->_set_value ($self->{value});
4327}
4328
4126############################################################################# 4329#############################################################################
4127 4330
4128package CFPlus::UI::Statusbox; 4331package DC::UI::Statusbox;
4129 4332
4130our @ISA = CFPlus::UI::VBox::; 4333our @ISA = DC::UI::VBox::;
4131 4334
4132sub new { 4335sub new {
4133 my $class = shift; 4336 my $class = shift;
4134 4337
4135 my $self = $class->SUPER::new ( 4338 my $self = $class->SUPER::new (
4136 fontsize => 0.8, 4339 fontsize => 0.8,
4137 @_, 4340 @_,
4138 ); 4341 );
4139 4342
4140 CFPlus::weaken (my $this = $self); 4343 DC::weaken (my $this = $self);
4141 4344
4142 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4345 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4143 4346
4144 $self 4347 $self
4145} 4348}
4146 4349
4147sub reorder { 4350sub reorder {
4148 my ($self) = @_; 4351 my ($self) = @_;
4149 my $NOW = Time::HiRes::time; 4352 my $NOW = AE::time;
4150 4353
4151 # freeze display when hovering over any label 4354 # freeze display when hovering over any label
4152 return if $CFPlus::UI::TOOLTIP->{owner} 4355 return if $DC::UI::TOOLTIP->{owner}
4153 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4356 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4154 values %{ $self->{item} }; 4357 values %{ $self->{item} };
4155 4358
4156 while (my ($k, $v) = each %{ $self->{item} }) { 4359 while (my ($k, $v) = each %{ $self->{item} }) {
4157 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4360 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4158 } 4361 }
4362
4363 $self->{timer}->set (1, 1);
4159 4364
4160 my @widgets; 4365 my @widgets;
4161 4366
4162 my @items = sort { 4367 my @items = sort {
4163 $a->{pri} <=> $b->{pri} 4368 $a->{pri} <=> $b->{pri}
4164 or $b->{id} <=> $a->{id} 4369 or $b->{id} <=> $a->{id}
4165 } values %{ $self->{item} }; 4370 } values %{ $self->{item} };
4166
4167 $self->{timer}->interval (1);
4168 4371
4169 my $count = 10 + 1; 4372 my $count = 10 + 1;
4170 for my $item (@items) { 4373 for my $item (@items) {
4171 last unless --$count; 4374 last unless --$count;
4172 4375
4179 for ($short) { 4382 for ($short) {
4180 s/^\s+//; 4383 s/^\s+//;
4181 s/\s+/ /g; 4384 s/\s+/ /g;
4182 } 4385 }
4183 4386
4184 new CFPlus::UI::Label 4387 new DC::UI::Label
4185 markup => $short, 4388 markup => $short,
4186 tooltip => $item->{tooltip}, 4389 tooltip => $item->{tooltip},
4187 tooltip_font => $::FONT_PROP, 4390 tooltip_font => $::FONT_PROP,
4188 tooltip_width => 0.67, 4391 tooltip_width => 0.67,
4189 fontsize => $item->{fontsize} || $self->{fontsize}, 4392 fontsize => $item->{fontsize} || $self->{fontsize},
4190 max_w => $::WIDTH * 0.44, 4393 max_w => $::WIDTH * 0.44,
4394 align => 0,
4191 fg => [@{ $item->{fg} }], 4395 fg => [@{ $item->{fg} }],
4192 can_events => 1, 4396 can_events => 1,
4193 can_hover => 1 4397 can_hover => 1
4194 }; 4398 };
4195 4399
4196 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4400 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4197 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4401 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4198 $label->update; 4402 $label->update;
4199 $label->set_max_size (undef, $label->{req_h} * $diff) 4403 $label->set_max_size (undef, $label->{req_h} * $diff)
4200 if $diff < 1; 4404 if $diff < 1;
4201 $self->{timer}->interval (1/30); 4405 $self->{timer}->set (1/30, 1/30);
4202 } else { 4406 } else {
4203 $label->{fg}[3] = $item->{fg}[3] || 1; 4407 $label->{fg}[3] = $item->{fg}[3] || 1;
4204 } 4408 }
4205 4409
4206 push @widgets, $label; 4410 push @widgets, $label;
4207 } 4411 }
4412
4413 my $hash = join ",", @widgets;
4414 return if $hash eq $self->{last_widget_hash};
4415 $self->{last_widget_hash} = $hash;
4208 4416
4209 $self->clear; 4417 $self->clear;
4210 $self->SUPER::add (reverse @widgets); 4418 $self->SUPER::add (reverse @widgets);
4211} 4419}
4212 4420
4248 $ROOT->on_refresh (reorder => sub { 4456 $ROOT->on_refresh (reorder => sub {
4249 $self->reorder; 4457 $self->reorder;
4250 }); 4458 });
4251} 4459}
4252 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
4253sub reconfigure { 4471sub reconfigure {
4254 my ($self) = @_; 4472 my ($self) = @_;
4255 4473
4256 delete $_->{label} 4474 delete $_->{label}
4257 for values %{ $self->{item} || {} }; 4475 for values %{ $self->{item} || {} };
4268 $self->SUPER::destroy; 4486 $self->SUPER::destroy;
4269} 4487}
4270 4488
4271############################################################################# 4489#############################################################################
4272 4490
4273package CFPlus::UI::Root; 4491package DC::UI::Root;
4274 4492
4275our @ISA = CFPlus::UI::Container::; 4493our @ISA = DC::UI::Container::;
4276 4494
4277use List::Util qw(min max); 4495use List::Util qw(min max);
4278 4496
4279use CFPlus::OpenGL; 4497use DC::OpenGL;
4280 4498
4281sub new { 4499sub new {
4282 my $class = shift; 4500 my $class = shift;
4283 4501
4284 my $self = $class->SUPER::new ( 4502 my $self = $class->SUPER::new (
4285 visible => 1, 4503 visible => 1,
4286 @_, 4504 @_,
4287 ); 4505 );
4288 4506
4289 CFPlus::weaken ($self->{root} = $self); 4507 DC::weaken ($self->{root} = $self);
4290 4508
4291 $self 4509 $self
4292} 4510}
4293 4511
4294sub size_request { 4512sub size_request {
4342} 4560}
4343 4561
4344sub update { 4562sub update {
4345 my ($self) = @_; 4563 my ($self) = @_;
4346 4564
4347 $::WANT_REFRESH->start; 4565 $::WANT_REFRESH = 1;
4348} 4566}
4349 4567
4350sub add { 4568sub add {
4351 my ($self, @children) = @_; 4569 my ($self, @children) = @_;
4352 4570
4411 while () { 4629 while () {
4412 @queue or last outer; 4630 @queue or last outer;
4413 4631
4414 $widget = pop @{ $queue[-1] || [] } 4632 $widget = pop @{ $queue[-1] || [] }
4415 and last; 4633 and last;
4416 4634
4417 pop @queue; 4635 pop @queue;
4418 } 4636 }
4419 4637
4420 delete $queue{$widget+0}; 4638 delete $queue{$widget+0};
4421 4639
4422 my ($w, $h) = $widget->size_request; 4640 my ($w, $h) = $widget->size_request;
4423 4641
4424 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4642 $w += $widget->{padding_x} * 2;
4425 $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;
4426 4647
4427 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4648 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4428 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4649 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4429 4650
4430 $w = $widget->{force_w} if exists $widget->{force_w}; 4651 $w = $widget->{force_w} if exists $widget->{force_w};
4497 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4718 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4498 glMatrixMode GL_MODELVIEW; 4719 glMatrixMode GL_MODELVIEW;
4499 glLoadIdentity; 4720 glLoadIdentity;
4500 4721
4501 { 4722 {
4502 package CFPlus::UI::Base; 4723 package DC::UI::Base;
4503 4724
4504 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4725 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4505 (0, 0, $self->{w}, $self->{h}); 4726 (0, 0, $self->{w}, $self->{h});
4506 4727
4507 $self->_draw; 4728 $self->_draw;
4508 } 4729 }
4509} 4730}
4510 4731
4511############################################################################# 4732#############################################################################
4512 4733
4513package CFPlus::UI; 4734package DC::UI;
4514 4735
4515$ROOT = new CFPlus::UI::Root; 4736$ROOT = new DC::UI::Root;
4516$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4737$TOOLTIP = new DC::UI::Tooltip z => 900;
4517 4738
45181 47391
4519

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines