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.388 by root, Fri Jul 20 18:30:03 2007 UTC vs.
Revision 1.479 by root, Thu Feb 4 20:01:09 2010 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
7use Event;
8 6
9use CFPlus; 7use Guard ();
8
9use DC;
10use CFPlus::Pod; 10use DC::Pod;
11use CFPlus::Texture; 11use DC::Texture;
12 12
13our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
14 14
15our $LAYOUT; 15our $LAYOUT;
16our $ROOT; 16our $ROOT;
17our $TOOLTIP; 17our $TOOLTIP;
18our $BUTTON_STATE; 18our $BUTTON_STATE;
19 19
20our %WIDGET; # all widgets, weak-referenced 20our %WIDGET; # all widgets, weak-referenced
21 21
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
23 $_[0]->stop;
24
23 if (!$GRAB) { 25 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 26 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 27 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 28 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 29 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 32 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 33 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 34
33 return if $ENV{CFPLUS_DEBUG} & 8; 35 return if $ENV{CFPLUS_DEBUG} & 8;
34 36
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 37 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 38 $TOOLTIP->show;
41 } 39 }
42 40
43 return; 41 return;
46 } 44 }
47 45
48 $TOOLTIP->hide; 46 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 47 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 48 delete $TOOLTIP->{owner};
51}); 49};
52 50
53sub get_layout { 51sub get_layout {
54 my $layout; 52 my $layout;
55 53
56 for (grep { $_->{name} } values %WIDGET) { 54 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 89 my $hover = $HOVER; $HOVER = $widget;
92 90
93 $hover->update if $hover && $hover->{can_hover}; 91 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 92 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 93
96 $TOOLTIP_WATCHER->start; 94 $TOOLTIP_WATCHER->again;
97 } 95 }
98} 96}
99 97
100sub feed_sdl_button_down_event { 98sub feed_sdl_button_down_event {
101 my ($ev) = @_; 99 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 105 my $widget = $ROOT->find_widget ($x, $y);
108 106
109 $GRAB = $widget; 107 $GRAB = $widget;
110 $GRAB->update if $GRAB; 108 $GRAB->update if $GRAB;
111 109
112 $TOOLTIP_WATCHER->cb->(); 110 $TOOLTIP_WATCHER->invoke;
113 } 111 }
114 112
115 if ($GRAB) { 113 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 114 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 115 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 116 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 117 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 118
121 $ev->{dx} = $shift ? $delta : 0; 119 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 120 $ev->{dy} = $shift ? 0 : $delta;
123 121
124 $GRAB->emit (mouse_wheel => $ev); 122 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 140 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 141 $grab->update if $grab;
144 $GRAB->update if $GRAB; 142 $GRAB->update if $GRAB;
145 143
146 check_hover $widget; 144 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 145 $TOOLTIP_WATCHER->invoke;
148 } 146 }
149} 147}
150 148
151sub feed_sdl_motion_event { 149sub feed_sdl_motion_event {
152 my ($ev) = @_; 150 my ($ev) = @_;
192# call when resolution changes etc. 190# call when resolution changes etc.
193sub rescale_widgets { 191sub rescale_widgets {
194 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
195 193
196 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
197 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
198 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 196 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 197 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
200 198
201 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 199 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
202 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 200 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
214 reconfigure_widgets; 212 reconfigure_widgets;
215} 213}
216 214
217############################################################################# 215#############################################################################
218 216
219package CFPlus::UI::Event; 217package DC::UI::Event;
220 218
221sub xy { 219sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 220 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 221}
224 222
225############################################################################# 223#############################################################################
226 224
227package CFPlus::UI::Base; 225package DC::UI::Base;
228 226
229use strict; 227use strict;
230 228
231use CFPlus::OpenGL; 229use DC::OpenGL;
232 230
233sub new { 231sub new {
234 my $class = shift; 232 my $class = shift;
235 233
236 my $self = bless { 234 my $self = bless {
241 h => undef, 239 h => undef,
242 can_events => 1, 240 can_events => 1,
243 @_ 241 @_
244 }, $class; 242 }, $class;
245 243
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 244 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 245
248 for (keys %$self) { 246 for (keys %$self) {
249 if (/^on_(.*)$/) { 247 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 248 $self->connect ($1 => delete $self->{$_});
251 } 249 }
252 } 250 }
253 251
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 252 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 253 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
256 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 254 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
257 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 255 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
258 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 256 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
259 257
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 258 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 259 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 260
263 $self->show if $layout->{show}; 261 $self->show if $layout->{show};
273 $self->emit ("destroy"); 271 $self->emit ("destroy");
274 %$self = (); 272 %$self = ();
275} 273}
276 274
277sub TO_JSON { 275sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 276 { "\fw" => $_[0]{s_id} }
279} 277}
280 278
281sub show { 279sub show {
282 my ($self) = @_; 280 my ($self) = @_;
283 281
284 return if $self->{parent}; 282 return if $self->{parent};
285 283
286 $CFPlus::UI::ROOT->add ($self); 284 $DC::UI::ROOT->add ($self);
287} 285}
288 286
289sub set_visible { 287sub set_visible {
290 my ($self) = @_; 288 my ($self) = @_;
291 289
292 return if $self->{visible}; 290 return if $self->{visible};
291
292 $self->{parent} && $self->{parent}{root}#d#
293 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
293 294
294 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
295 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
296 297
297 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
298 299
299 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
300 301
301 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
302} 303}
303 304
304sub set_invisible { 305sub set_invisible {
305 my ($self) = @_; 306 my ($self) = @_;
306 307
312 delete $self->{root}; 313 delete $self->{root};
313 314
314 undef $GRAB if $GRAB == $self; 315 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 316 undef $HOVER if $HOVER == $self;
316 317
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 318 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 319 if $TOOLTIP->{owner} == $self;
319 320
320 $self->emit ("focus_out"); 321 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 322 $self->emit (visibility_change => 0);
322} 323}
341sub hide { 342sub hide {
342 my ($self) = @_; 343 my ($self) = @_;
343 344
344 $self->set_invisible; 345 $self->set_invisible;
345 346
347 # extra $parent copy for 5.8.8+ bug workaround
348 # (otherwise $_[0] in remove gets freed
349 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 350 $parent->remove ($self);
347 if $self->{parent}; 351 }
348} 352}
349 353
350sub move_abs { 354sub move_abs {
351 my ($self, $x, $y, $z) = @_; 355 my ($self, $x, $y, $z) = @_;
352 356
364 $self->{force_h} = $h; 368 $self->{force_h} = $h;
365 369
366 $self->realloc; 370 $self->realloc;
367} 371}
368 372
373# traverse the widget chain up to find the maximum "physical" size constraints
374sub get_max_wh {
375 my ($self) = @_;
376
377 my ($w, $h) = @$self{qw(max_w max_h)};
378
379 if ($w <= 0 || $h <= 0) {
380 my ($mw, $mh) = $self->{parent}
381 ? $self->{parent}->get_max_wh
382 : ($::WIDTH, $::HEIGHT);
383
384 $w = $mw if $w <= 0;
385 $h = $mh if $h <= 0;
386 }
387
388 ($w, $h)
389}
390
369sub size_request { 391sub size_request {
370 require Carp; 392 require Carp;
371 Carp::confess "size_request is abstract"; 393 Carp::confess "size_request is abstract";
372} 394}
373 395
379 my ($self, $x, $y, $w, $h) = @_; 401 my ($self, $x, $y, $w, $h) = @_;
380 402
381 if ($self->{aspect}) { 403 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 404 my ($ow, $oh) = ($w, $h);
383 405
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 406 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 407 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 408
387 # use alignment to adjust x, y 409 # use alignment to adjust x, y
388 410
389 $x += int 0.5 * ($ow - $w); 411 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 412 $y += int 0.5 * ($oh - $h);
431 453
432 return if $self->{tooltip} eq $tooltip; 454 return if $self->{tooltip} eq $tooltip;
433 455
434 $self->{tooltip} = $tooltip; 456 $self->{tooltip} = $tooltip;
435 457
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 458 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 459 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 460 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 461 }
440} 462}
441 463
442# translate global coordinates to local coordinate system 464# translate global coordinates to local coordinate system
443sub coord2local { 465sub coord2local {
444 my ($self, $x, $y) = @_; 466 my ($self, $x, $y) = @_;
445 467
446 Carp::confess unless $self->{parent};#d# 468 return (undef, undef) unless $self->{parent};
447 469
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 470 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 471}
450 472
451# translate local coordinates to global coordinate system 473# translate local coordinates to global coordinate system
452sub coord2global { 474sub coord2global {
453 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
454 476
455 Carp::confess unless $self->{parent};#d# 477 return (undef, undef) unless $self->{parent};
456 478
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 479 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 480}
459 481
460sub invoke_focus_in { 482sub invoke_focus_in {
509sub connect { 531sub connect {
510 my ($self, $signal, $cb) = @_; 532 my ($self, $signal, $cb) = @_;
511 533
512 push @{ $self->{signal_cb}{$signal} }, $cb; 534 push @{ $self->{signal_cb}{$signal} }, $cb;
513 535
514 defined wantarray and CFPlus::guard { 536 defined wantarray and Guard::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 537 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 538 @{ $self->{signal_cb}{$signal} };
517 } 539 }
518} 540}
519 541
557# in .xs 579# in .xs
558 580
559sub set_parent { 581sub set_parent {
560 my ($self, $parent) = @_; 582 my ($self, $parent) = @_;
561 583
562 CFPlus::weaken ($self->{parent} = $parent); 584 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 585 $self->set_visible if $parent->{visible};
564} 586}
565 587
566sub realloc { 588sub realloc {
567 my ($self) = @_; 589 my ($self) = @_;
593 615
594# using global variables seems a bit hacky, but passing through all drawing 616# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 617# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 618our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 619
598sub draw { 620#sub draw {
599 my ($self) = @_; 621#CFPlus.xs
600
601 return unless $self->{h} && $self->{w};
602
603 # update screen rectangle
604 local $draw_x = $draw_x + $self->{x};
605 local $draw_y = $draw_y + $self->{y};
606
607 # skip widgets that are entirely outside the drawing area
608 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
609 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
610
611 glPushMatrix;
612 glTranslate $self->{x}, $self->{y}, 0;
613
614 if ($self == $HOVER && $self->{can_hover}) {
615 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
616 glEnable GL_BLEND;
617 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
618 glBegin GL_QUADS;
619 glVertex 0 , 0;
620 glVertex $self->{w}, 0;
621 glVertex $self->{w}, $self->{h};
622 glVertex 0 , $self->{h};
623 glEnd;
624 glDisable GL_BLEND;
625 }
626
627 if ($ENV{CFPLUS_DEBUG} & 1) {
628 glPushMatrix;
629 glColor 1, 1, 0, 1;
630 glTranslate 0.375, 0.375;
631 glBegin GL_LINE_LOOP;
632 glVertex 0 , 0;
633 glVertex $self->{w} - 1, 0;
634 glVertex $self->{w} - 1, $self->{h} - 1;
635 glVertex 0 , $self->{h} - 1;
636 glEnd;
637 glPopMatrix;
638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
639 }
640
641 $self->_draw;
642 glPopMatrix;
643}
644 622
645sub _draw { 623sub _draw {
646 my ($self) = @_; 624 my ($self) = @_;
647 625
648 warn "no draw defined for $self\n"; 626 warn "no draw defined for $self\n";
649} 627}
650 628
651my $cntx;#d#
652sub DESTROY { 629sub DESTROY {
653 my ($self) = @_; 630 my ($self) = @_;
654 631
655 return if CFPlus::in_destruct; 632 return if DC::in_destruct;
656 633
634 local $@;
657 eval { $self->destroy }; 635 eval { $self->destroy };
658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659 637
660 delete $WIDGET{$self+0}; 638 delete $WIDGET{$self+0};
661} 639}
662 640
663############################################################################# 641#############################################################################
664 642
665package CFPlus::UI::DrawBG; 643package DC::UI::DrawBG;
666 644
667our @ISA = CFPlus::UI::Base::; 645our @ISA = DC::UI::Base::;
668 646
669use strict; 647use strict;
670use CFPlus::OpenGL; 648use DC::OpenGL;
671 649
672sub new { 650sub new {
673 my $class = shift; 651 my $class = shift;
674
675 # range [value, low, high, page]
676 652
677 $class->SUPER::new ( 653 $class->SUPER::new (
678 #bg => [0, 0, 0, 0.2], 654 #bg => [0, 0, 0, 0.2],
679 #active_bg => [1, 1, 1, 0.5], 655 #active_bg => [1, 1, 1, 0.5],
680 @_ 656 @_
681 ) 657 )
682} 658}
683 659
660sub set_bg {
661 my ($self, $bg) = @_;
662
663 $self->{bg} = $bg;
664 $self->update;
665}
666
684sub _draw { 667sub _draw {
685 my ($self) = @_; 668 my ($self) = @_;
686 669
687 my $color = $FOCUS == $self && $self->{active_bg} 670 my $color = $FOCUS == $self
688 ? $self->{active_bg} 671 ? $self->{active_bg} || $self->{bg}
689 : $self->{bg}; 672 : $self->{bg};
690 673
691 if ($color && (@$color < 4 || $color->[3])) { 674 if ($color && (@$color < 4 || $color->[3])) {
692 my ($w, $h) = @$self{qw(w h)}; 675 my ($w, $h) = @$self{qw(w h)};
693 676
694 glEnable GL_BLEND; 677 glEnable GL_BLEND;
695 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 678 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
696 glColor_premultiply @$color; 679 glColor_premultiply @$color;
697
698 glBegin GL_QUADS;
699 glVertex 0 , 0;
700 glVertex 0 , $h;
701 glVertex $w, $h; 680 glRect 0, 0, $w, $h;
702 glVertex $w, 0;
703 glEnd;
704
705 glDisable GL_BLEND; 681 glDisable GL_BLEND;
706 } 682 }
707} 683}
708 684
709############################################################################# 685#############################################################################
710 686
711package CFPlus::UI::Empty; 687package DC::UI::Empty;
712 688
713our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
714 690
715sub new { 691sub new {
716 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
717 $class->SUPER::new (can_events => 0, %arg); 693 $class->SUPER::new (can_events => 0, %arg);
718} 694}
725 701
726sub draw { } 702sub draw { }
727 703
728############################################################################# 704#############################################################################
729 705
730package CFPlus::UI::Container; 706package DC::UI::Container;
731 707
732our @ISA = CFPlus::UI::Base::; 708our @ISA = DC::UI::Base::;
733 709
734sub new { 710sub new {
735 my ($class, %arg) = @_; 711 my ($class, %arg) = @_;
736 712
737 my $children = delete $arg{children}; 713 my $children = delete $arg{children};
741 can_events => 0, 717 can_events => 0,
742 %arg, 718 %arg,
743 ); 719 );
744 720
745 $self->add (@$children) 721 $self->add (@$children)
746 if $children; 722 if $children && @$children;
747 723
748 $self 724 $self
749} 725}
750 726
751sub realloc { 727sub realloc {
760 my ($self, @widgets) = @_; 736 my ($self, @widgets) = @_;
761 737
762 $_->set_parent ($self) 738 $_->set_parent ($self)
763 for @widgets; 739 for @widgets;
764 740
741 # TODO: only do this in widgets that need it, e.g. root, fixed
765 use sort 'stable'; 742 use sort 'stable';
766 743
767 $self->{children} = [ 744 $self->{children} = [
768 sort { $a->{z} <=> $b->{z} } 745 sort { $a->{z} <=> $b->{z} }
769 @{$self->{children}}, @widgets 746 @{$self->{children}}, @widgets
770 ]; 747 ];
771 748
772 $self->realloc; 749 $self->realloc;
750
751 $self->emit (c_add => \@widgets);
752
753 map $_+0, @widgets
773} 754}
774 755
775sub children { 756sub children {
776 @{ $_[0]{children} } 757 @{ $_[0]{children} }
777} 758}
778 759
779sub remove { 760sub remove {
780 my ($self, $child) = @_; 761 my ($self, @widgets) = @_;
781 762
763 $self->emit (c_remove => \@widgets);
764
765 for my $child (@widgets) {
782 delete $child->{parent}; 766 delete $child->{parent};
783 $child->hide; 767 $child->hide;
784
785 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 768 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
769 }
786 770
787 $self->realloc; 771 $self->realloc;
788} 772}
789 773
790sub clear { 774sub clear {
791 my ($self) = @_; 775 my ($self) = @_;
792 776
793 my $children = delete $self->{children}; 777 my $children = $self->{children};
794 $self->{children} = []; 778 $self->{children} = [];
795 779
796 for (@$children) { 780 for (@$children) {
797 delete $_->{parent}; 781 delete $_->{parent};
798 $_->hide; 782 $_->hide;
818} 802}
819 803
820sub _draw { 804sub _draw {
821 my ($self) = @_; 805 my ($self) = @_;
822 806
823 $_->draw for @{$self->{children}}; 807 $_->draw for $self->visible_children;
824} 808}
825 809
826############################################################################# 810#############################################################################
827 811
828package CFPlus::UI::Bin; 812package DC::UI::Bin;
829 813
830our @ISA = CFPlus::UI::Container::; 814our @ISA = DC::UI::Container::;
831 815
832sub new { 816sub new {
833 my ($class, %arg) = @_; 817 my ($class, %arg) = @_;
834 818
835 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 819 my $child = (delete $arg{child}) || new DC::UI::Empty::;
836 820
837 $class->SUPER::new (children => [$child], %arg) 821 $class->SUPER::new (children => [$child], %arg)
838} 822}
839 823
840sub add { 824sub add {
841 my ($self, $child) = @_; 825 my ($self, $child) = @_;
842 826
843 $self->SUPER::remove ($_) for @{ $self->{children} }; 827 $self->clear;
844 $self->SUPER::add ($child); 828 $self->SUPER::add ($child);
845} 829}
846 830
847sub remove { 831sub remove {
848 my ($self, $widget) = @_; 832 my ($self, $widget) = @_;
849 833
850 $self->SUPER::remove ($widget); 834 $self->SUPER::remove ($widget);
851 835
852 $self->{children} = [new CFPlus::UI::Empty] 836 $self->{children} = [new DC::UI::Empty]
853 unless @{$self->{children}}; 837 unless @{$self->{children}};
854} 838}
855 839
856sub child { $_[0]->{children}[0] } 840sub child { $_[0]->{children}[0] }
857 841
866 850
867 1 851 1
868} 852}
869 853
870############################################################################# 854#############################################################################
871
872# back-buffered drawing area 855# back-buffered drawing area
873 856
874package CFPlus::UI::Window; 857package DC::UI::Window;
875 858
876our @ISA = CFPlus::UI::Bin::; 859our @ISA = DC::UI::Bin::;
877 860
878use CFPlus::OpenGL; 861use DC::OpenGL;
879 862
880sub new { 863sub new {
881 my ($class, %arg) = @_; 864 my ($class, %arg) = @_;
882 865
883 my $self = $class->SUPER::new (%arg); 866 my $self = $class->SUPER::new (%arg);
905} 888}
906 889
907sub render_child { 890sub render_child {
908 my ($self) = @_; 891 my ($self) = @_;
909 892
910 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 893 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
911 glClearColor 0, 0, 0, 0; 894 glClearColor 0, 0, 0, 0;
912 glClear GL_COLOR_BUFFER_BIT; 895 glClear GL_COLOR_BUFFER_BIT;
913 896
914 { 897 {
915 package CFPlus::UI::Base; 898 package DC::UI::Base;
916 899
917 local ($draw_x, $draw_y, $draw_w, $draw_h) = 900 local ($draw_x, $draw_y, $draw_w, $draw_h) =
918 (0, 0, $self->{w}, $self->{h}); 901 (0, 0, $self->{w}, $self->{h});
919 902
920 $self->_render; 903 $self->_render;
937 glDisable GL_TEXTURE_2D; 920 glDisable GL_TEXTURE_2D;
938} 921}
939 922
940############################################################################# 923#############################################################################
941 924
942package CFPlus::UI::ViewPort; 925package DC::UI::ViewPort;
943 926
944use List::Util qw(min max); 927use List::Util qw(min max);
945 928
946our @ISA = CFPlus::UI::Window::; 929our @ISA = DC::UI::Window::;
947 930
948sub new { 931sub new {
949 my $class = shift; 932 my $class = shift;
950 933
951 $class->SUPER::new ( 934 $class->SUPER::new (
958sub size_request { 941sub size_request {
959 my ($self) = @_; 942 my ($self) = @_;
960 943
961 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 944 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
962 945
963 $w = 10 if $self->{scroll_x}; 946 $w = 1 if $self->{scroll_x};
964 $h = 10 if $self->{scroll_y}; 947 $h = 1 if $self->{scroll_y};
965 948
966 ($w, $h) 949 ($w, $h)
967} 950}
968 951
969sub invoke_size_allocate { 952sub invoke_size_allocate {
990 $self->{view_x} = $x; 973 $self->{view_x} = $x;
991 $self->{view_y} = $y; 974 $self->{view_y} = $y;
992 975
993 $self->emit (changed => $x, $y); 976 $self->emit (changed => $x, $y);
994 $self->update; 977 $self->update;
978 }
979}
980
981sub set_center {
982 my ($self, $x, $y) = @_;
983
984 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
985}
986
987sub make_visible {
988 my ($self, $x, $y, $border) = @_;
989
990 if ( $x < $self->{view_x} + $self->{w} * $border
991 || $x > $self->{view_x} + $self->{w} * (1 - $border)
992 || $y < $self->{view_y} + $self->{h} * $border
993 || $y > $self->{view_y} + $self->{h} * (1 - $border)
994 ) {
995 $self->set_center ($x, $y);
995 } 996 }
996} 997}
997 998
998# hmm, this does not work for topleft of $self... but we should not ask for that 999# hmm, this does not work for topleft of $self... but we should not ask for that
999sub coord2local { 1000sub coord2local {
1017 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1018 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1019 ) { 1020 ) {
1020 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1021 } else { 1022 } else {
1022 $self->CFPlus::UI::Base::find_widget ($x, $y) 1023 $self->DC::UI::Base::find_widget ($x, $y)
1023 } 1024 }
1024} 1025}
1025 1026
1026sub _render { 1027sub _render {
1027 my ($self) = @_; 1028 my ($self) = @_;
1028 1029
1029 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1030 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1030 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1031 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1031 1032
1032 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1033 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1033 1034
1034 $self->SUPER::_render; 1035 $self->SUPER::_render;
1035} 1036}
1036 1037
1037############################################################################# 1038#############################################################################
1038 1039
1039package CFPlus::UI::ScrolledWindow; 1040package DC::UI::ScrolledWindow;
1040 1041
1041our @ISA = CFPlus::UI::Table::; 1042our @ISA = DC::UI::Table::;
1042 1043
1043sub new { 1044sub new {
1044 my ($class, %arg) = @_; 1045 my ($class, %arg) = @_;
1045 1046
1046 my $child = delete $arg{child}; 1047 my $child = delete $arg{child};
1047 1048
1048 my $self; 1049 my $self;
1049 1050
1050 my $hslider = new CFPlus::UI::Slider 1051 my $hslider = new DC::UI::Slider
1052 c_col => 0,
1053 c_row => 1,
1051 vertical => 0, 1054 vertical => 0,
1052 range => [0, 0, 1, 0.01], # HACK fix 1055 range => [0, 0, 1, 0.01], # HACK fix
1053 on_changed => sub { 1056 on_changed => sub {
1054 $self->{hpos} = $_[1]; 1057 $self->{hpos} = $_[1];
1055 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1056 }, 1059 },
1057 ; 1060 ;
1058 1061
1059 my $vslider = new CFPlus::UI::Slider 1062 my $vslider = new DC::UI::Slider
1063 c_col => 1,
1064 c_row => 0,
1060 vertical => 1, 1065 vertical => 1,
1061 range => [0, 0, 1, 0.01], # HACK fix 1066 range => [0, 0, 1, 0.01], # HACK fix
1062 on_changed => sub { 1067 on_changed => sub {
1063 $self->{vpos} = $_[1]; 1068 $self->{vpos} = $_[1];
1064 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1074 col_expand => [1, 0], 1079 col_expand => [1, 0],
1075 row_expand => [1, 0], 1080 row_expand => [1, 0],
1076 %arg, 1081 %arg,
1077 ); 1082 );
1078 1083
1079 $self->{vp} = new CFPlus::UI::ViewPort 1084 $self->{vp} = new DC::UI::ViewPort
1085 c_col => 0,
1086 c_row => 0,
1080 expand => 1, 1087 expand => 1,
1081 scroll_x => $self->{scroll_x}, 1088 scroll_x => $self->{scroll_x},
1082 scroll_y => $self->{scroll_y}, 1089 scroll_y => $self->{scroll_y},
1083 on_changed => sub { 1090 on_changed => sub {
1084 my ($vp, $x, $y) = @_; 1091 my ($vp, $x, $y) = @_;
1086 $vp->{parent}{hslider}->set_value ($x); 1093 $vp->{parent}{hslider}->set_value ($x);
1087 $vp->{parent}{vslider}->set_value ($y); 1094 $vp->{parent}{vslider}->set_value ($y);
1088 1095
1089 0 1096 0
1090 }, 1097 },
1098 on_size_allocate => sub {
1099 my ($vp, $w, $h) = @_;
1100 $vp->{parent}->update_slider;
1101 0
1102 },
1091 ; 1103 ;
1092 1104
1093 $self->SUPER::add_at (0, 0, $self->{vp}); 1105 $self->SUPER::add ($self->{vp});
1094 1106
1095 $self->add ($child) if $child; 1107 $self->add ($child) if $child;
1096 1108
1097 $self 1109 $self
1098} 1110}
1099
1100#TODO# update range on size_allocate depending on child
1101 1111
1102sub add { 1112sub add {
1103 my ($self, $widget) = @_; 1113 my ($self, $widget) = @_;
1104 1114
1105 $self->{vp}->add ($self->{child} = $widget); 1115 $self->{vp}->add ($self->{child} = $widget);
1106} 1116}
1107 1117
1118sub set_offset { shift->{vp}->set_offset (@_) }
1119sub set_center { shift->{vp}->set_center (@_) }
1120sub make_visible { shift->{vp}->make_visible (@_) }
1121
1108sub update_slider { 1122sub update_slider {
1109 my ($self) = @_; 1123 my ($self) = @_;
1110 1124
1111 my $child = ($self->{vp} or return)->child; 1125 my $child = ($self->{vp} or return)->child;
1112 1126
1127 if ($self->{scroll_x}) {
1113 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1128 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1114 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1129 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1115 1130
1116 my $visible = $w1 > $w2; 1131 my $visible = $w1 > $w2;
1117 if ($visible != $self->{hslider}{visible}) { 1132 if ($visible != $self->{hslider_visible}) {
1133 $self->{hslider_visible} = $visible;
1118 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) 1134 $visible ? $self->SUPER::add ($self->{hslider})
1119 : $self->{hslider}->hide; 1135 : $self->SUPER::remove ($self->{hslider});
1136 }
1120 } 1137 }
1121 1138
1139 if ($self->{scroll_y}) {
1122 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1140 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1123 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1141 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1124 1142
1125 my $visible = $h1 > $h2; 1143 my $visible = $h1 > $h2;
1126 if ($visible != $self->{vslider}{visible}) { 1144 if ($visible != $self->{vslider_visible}) {
1145 $self->{vslider_visible} = $visible;
1127 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) 1146 $visible ? $self->SUPER::add ($self->{vslider})
1128 : $self->{vslider}->hide; 1147 : $self->SUPER::remove ($self->{vslider});
1148 }
1149 }
1150}
1151
1152sub start_dragging {
1153 my ($self, $ev) = @_;
1154
1155 $self->grab_focus;
1156
1157 my $ox = $self->{vp}{view_x};
1158 my $oy = $self->{vp}{view_y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $ox -= $ev->{xrel};
1164 $oy -= $ev->{yrel};
1165
1166 $self->{vp}->set_offset ($ox, $oy);
1129 } 1167 };
1130}
1131
1132sub update {
1133 my ($self) = @_;
1134
1135 $self->update_slider;
1136 $self->SUPER::update;
1137} 1168}
1138 1169
1139sub invoke_mouse_wheel { 1170sub invoke_mouse_wheel {
1140 my ($self, $ev) = @_; 1171 my ($self, $ev) = @_;
1141 1172
1142 $self->{vslider}->emit (mouse_wheel => $ev) 1173 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1143 if $ev->{dy};
1144
1145 $self->{hslider}->emit (mouse_wheel => $ev) 1174 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1146 if $ev->{dx};
1147 1175
1148 1 1176 1
1149} 1177}
1150 1178
1151sub invoke_button_down { 1179sub invoke_button_down {
1152 my ($self, $ev, $x, $y) = @_; 1180 my ($self, $ev, $x, $y) = @_;
1153 1181
1154 if ($ev->{button} == 2) { 1182 if ($ev->{button} == 2) {
1155 $self->grab_focus; 1183 $self->start_dragging ($ev);
1156
1157 my $ox = $self->{vp}{view_x} + $ev->{x};
1158 my $oy = $self->{vp}{view_y} + $ev->{y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1164 $self->update;
1165 };
1166
1167 return 1; 1184 return 1;
1168 } 1185 }
1169 1186
1170 0 1187 0
1171} 1188}
1198 $self->SUPER::invoke_size_allocate ($w, $h) 1215 $self->SUPER::invoke_size_allocate ($w, $h)
1199} 1216}
1200 1217
1201############################################################################# 1218#############################################################################
1202 1219
1203package CFPlus::UI::Frame; 1220package DC::UI::Frame;
1204 1221
1205our @ISA = CFPlus::UI::Bin::; 1222our @ISA = DC::UI::Bin::;
1206 1223
1207use CFPlus::OpenGL; 1224use DC::OpenGL;
1208 1225
1209sub new { 1226sub new {
1210 my $class = shift; 1227 my $class = shift;
1211 1228
1212 $class->SUPER::new ( 1229 $class->SUPER::new (
1222 my ($w, $h) = @$self{qw(w h)}; 1239 my ($w, $h) = @$self{qw(w h)};
1223 1240
1224 glEnable GL_BLEND; 1241 glEnable GL_BLEND;
1225 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1242 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1226 glColor_premultiply @{ $self->{bg} }; 1243 glColor_premultiply @{ $self->{bg} };
1227
1228 glBegin GL_QUADS;
1229 glVertex 0 , 0;
1230 glVertex 0 , $h;
1231 glVertex $w, $h; 1244 glRect 0, 0, $w, $h;
1232 glVertex $w, 0;
1233 glEnd;
1234
1235 glDisable GL_BLEND; 1245 glDisable GL_BLEND;
1236 } 1246 }
1237 1247
1238 $self->SUPER::_draw; 1248 $self->SUPER::_draw;
1239} 1249}
1240 1250
1241############################################################################# 1251#############################################################################
1242 1252
1243package CFPlus::UI::FancyFrame; 1253package DC::UI::FancyFrame;
1244 1254
1245our @ISA = CFPlus::UI::Bin::; 1255our @ISA = DC::UI::Bin::;
1246 1256
1247use CFPlus::OpenGL; 1257use DC::OpenGL;
1248 1258
1249sub new { 1259sub new {
1250 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1251 1261
1252 if ((exists $arg{label}) && !ref $arg{label}) { 1262 if ((exists $arg{label}) && !ref $arg{label}) {
1253 $arg{label} = new CFPlus::UI::Label 1263 $arg{label} = new DC::UI::Label
1254 align => 1, 1264 align => 1,
1255 valign => 0, 1265 valign => 0.5,
1256 text => $arg{label}, 1266 text => $arg{label},
1257 fontsize => ($arg{border} || 0.8) * 0.75; 1267 fontsize => ($arg{border} || 0.8) * 0.75;
1258 } 1268 }
1259 1269
1260 my $self = $class->SUPER::new ( 1270 my $self = $class->SUPER::new (
1261 # label => "", 1271 # label => "",
1262 fg => [0.6, 0.3, 0.1], 1272 fg => undef,
1263 border => 0.8, 1273 border => 0.8,
1264 style => 'single', 1274 style => 'single',
1265 %arg, 1275 %arg,
1266 ); 1276 );
1267 1277
1270 1280
1271sub add { 1281sub add {
1272 my ($self, @widgets) = @_; 1282 my ($self, @widgets) = @_;
1273 1283
1274 $self->SUPER::add (@widgets); 1284 $self->SUPER::add (@widgets);
1275 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1285 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1276} 1286}
1277 1287
1278sub border { 1288sub border {
1279 int $_[0]{border} * $::FONTSIZE 1289 int $_[0]{border} * $::FONTSIZE
1280} 1290}
1320 my $border = $self->border; 1330 my $border = $self->border;
1321 my ($w, $h) = ($self->{w}, $self->{h}); 1331 my ($w, $h) = ($self->{w}, $self->{h});
1322 1332
1323 $child->draw; 1333 $child->draw;
1324 1334
1325 glColor @{$self->{fg}}; 1335 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1326 glBegin GL_LINE_STRIP; 1336 glBegin GL_LINE_STRIP;
1327 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1337 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1328 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1338 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1329 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1339 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1330 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1340 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1338 } 1348 }
1339} 1349}
1340 1350
1341############################################################################# 1351#############################################################################
1342 1352
1343package CFPlus::UI::Toplevel; 1353package DC::UI::Toplevel;
1344 1354
1345our @ISA = CFPlus::UI::Bin::; 1355our @ISA = DC::UI::Bin::;
1346 1356
1347use CFPlus::OpenGL; 1357use DC::OpenGL;
1348 1358
1349my $bg = 1359my $bg =
1350 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1360 new_from_resource DC::Texture "d1_bg.png",
1351 mipmap => 1, wrap => 1; 1361 mipmap => 1, wrap => 1;
1352 1362
1353my @border = 1363my @border =
1354 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1364 map { new_from_resource DC::Texture $_, mipmap => 1 }
1355 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1365 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1356 1366
1357my @icon = 1367my @icon =
1358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1368 map { new_from_resource DC::Texture $_, mipmap => 1 }
1359 qw(x1_move.png x1_resize.png); 1369 qw(x1_move.png x1_resize.png);
1360 1370
1361sub new { 1371sub new {
1362 my ($class, %arg) = @_; 1372 my ($class, %arg) = @_;
1363 1373
1364 my $self = $class->SUPER::new ( 1374 my $self = $class->SUPER::new (
1365 bg => [1, 1, 1, 1], 1375 bg => [1, 1, 1, 1],
1366 border_bg => [1, 1, 1, 1], 1376 border_bg => [1, 1, 1, 1],
1367 border => 0.6, 1377 border => 0.8,
1368 can_events => 1, 1378 can_events => 1,
1369 min_w => 64, 1379 min_w => 64,
1370 min_h => 32, 1380 min_h => 32,
1371 %arg, 1381 %arg,
1372 ); 1382 );
1373 1383
1374 $self->{title_widget} = new CFPlus::UI::Label 1384 $self->{title_widget} = new DC::UI::Label
1375 align => 0, 1385 align => 0.5,
1376 valign => 1, 1386 valign => 1,
1377 text => $self->{title}, 1387 text => $self->{title},
1378 fontsize => $self->{border}, 1388 fontsize => $self->{border},
1379 if exists $self->{title}; 1389 if exists $self->{title};
1380 1390
1381 if ($self->{has_close_button}) { 1391 if ($self->{has_close_button}) {
1382 $self->{close_button} = 1392 $self->{close_button} =
1383 new CFPlus::UI::ImageButton 1393 new DC::UI::ImageButton
1384 path => 'x1_close.png', 1394 path => 'x1_close.png',
1385 on_activate => sub { $self->emit ("delete") }; 1395 on_activate => sub { $self->emit ("delete") };
1386 1396
1387 $self->CFPlus::UI::Container::add ($self->{close_button}); 1397 $self->DC::UI::Container::add ($self->{close_button});
1388 } 1398 }
1389 1399
1390 $self 1400 $self
1391} 1401}
1392 1402
1393sub add { 1403sub add {
1394 my ($self, @widgets) = @_; 1404 my ($self, @widgets) = @_;
1395 1405
1396 $self->SUPER::add (@widgets); 1406 $self->SUPER::add (@widgets);
1397 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1407 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1398 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1408 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1399} 1409}
1400 1410
1401sub border { 1411sub border {
1402 int $_[0]{border} * $::FONTSIZE 1412 int $_[0]{border} * $::FONTSIZE
1413}
1414
1415sub get_max_wh {
1416 my ($self) = @_;
1417
1418 return ($self->{w}, $self->{h})
1419 if $self->{visible} && $self->{w};
1420
1421 $self->SUPER::get_max_wh
1403} 1422}
1404 1423
1405sub size_request { 1424sub size_request {
1406 my ($self) = @_; 1425 my ($self) = @_;
1407 1426
1532 glEnable GL_TEXTURE_2D; 1551 glEnable GL_TEXTURE_2D;
1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1534 1553
1535 my $border = $self->border; 1554 my $border = $self->border;
1536 1555
1556 if ($border) {
1537 glColor @{ $self->{border_bg} }; 1557 glColor @{ $self->{border_bg} };
1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1558 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1559 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1560 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1541 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1561 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1542 1562
1543 # move 1563 # move
1544 my $w2 = ($w - $border) * .5; 1564 my $w2 = ($w - $border) * .5;
1545 my $h2 = ($h - $border) * .5; 1565 my $h2 = ($h - $border) * .5;
1546 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1566 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1547 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1548 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1549 1569
1550 # resize 1570 # resize
1551 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1571 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1572 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1553 unless $self->{has_close_button}; 1573 unless $self->{has_close_button};
1554 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1574 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1555 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1576 }
1556 1577
1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1578 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1558 glColor @{ $self->{bg} }; 1579 glColor @{ $self->{bg} };
1559 1580
1560 # TODO: repeat texture not scale 1581 # TODO: repeat texture not scale
1579 if $self->{close_button}; 1600 if $self->{close_button};
1580} 1601}
1581 1602
1582############################################################################# 1603#############################################################################
1583 1604
1584package CFPlus::UI::Table; 1605package DC::UI::Table;
1585 1606
1586our @ISA = CFPlus::UI::Base::; 1607our @ISA = DC::UI::Container::;
1587 1608
1588use List::Util qw(max sum); 1609use List::Util qw(max sum);
1589 1610
1590use CFPlus::OpenGL; 1611use DC::OpenGL;
1591 1612
1592sub new { 1613sub new {
1593 my $class = shift; 1614 my $class = shift;
1594 1615
1595 $class->SUPER::new ( 1616 $class->SUPER::new (
1596 children => [],
1597 col_expand => [], 1617 col_expand => [],
1598 row_expand => [], 1618 row_expand => [],
1599 @_, 1619 @_,
1600 ) 1620 )
1601} 1621}
1602 1622
1603sub children {
1604 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1605}
1606
1607# TODO: store row/col info in child widget and use standard add/del
1608sub add { 1623sub add {
1609 my $self = shift; 1624 my ($self, @widgets) = @_;
1610 1625
1611 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# 1626 for my $child (@widgets) {
1612 $self->add_at (@_); 1627 $child->{c_rowspan} ||= 1;
1628 $child->{c_colspan} ||= 1;
1629 }
1630
1631 $self->SUPER::add (@widgets);
1613} 1632}
1614 1633
1615sub add_at { 1634sub add_at {
1616 my $self = shift; 1635 my $self = shift;
1617 1636
1637 my @widgets;
1638
1618 while (@_) { 1639 while (@_) {
1619 my ($col, $row, $child) = splice @_, 0, 3, (); 1640 my ($col, $row, $child) = splice @_, 0, 3, ();
1620 1641
1621 $child->set_parent ($self); 1642 $child->{c_row} = $row;
1622 $self->{children}[$row][$col] = $child; 1643 $child->{c_col} = $col;
1623 }
1624 1644
1625 $self->{force_realloc} = 1; 1645 push @widgets, $child;
1626 $self->{force_size_alloc} = 1; 1646 }
1627 $self->realloc;
1628}
1629 1647
1630sub remove { 1648 $self->add (@widgets);
1649}
1650
1651sub get_wh {
1631 my ($self, $child) = @_; 1652 my ($self) = @_;
1632 1653
1633 for (@{ $self->{children} }) { 1654 my (@w, @h);
1634 for (@{ $_ || [] }) { 1655
1635 $_ = undef if $_ == $child; 1656 my @children = $self->children;
1657
1658 # first pass, columns
1659 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1660 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1661
1662 my $sw = sum @w[$c .. $c + $cs - 1];
1663
1664 if ($w > $sw) {
1665 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1636 } 1666 }
1637 } 1667 }
1638}
1639 1668
1640# TODO: move to container class maybe? send children a signal on removal? 1669 # second pass, rows
1641sub clear { 1670 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1642 my ($self) = @_;
1643
1644 my @children = $self->children;
1645 delete $self->{children};
1646
1647 for (@children) {
1648 delete $_->{parent};
1649 $_->hide;
1650 }
1651
1652 $self->realloc;
1653}
1654
1655sub get_wh {
1656 my ($self) = @_;
1657
1658 my (@w, @h);
1659
1660 for my $y (0 .. $#{$self->{children}}) {
1661 my $row = $self->{children}[$y]
1662 or next;
1663
1664 for my $x (0 .. $#$row) {
1665 my $widget = $row->[$x]
1666 or next;
1667 my ($w, $h) = @$widget{qw(req_w req_h)}; 1671 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1668 1672
1669 $w[$x] = max $w[$x], $w; 1673 my $sh = sum @h[$r .. $r + $rs - 1];
1670 $h[$y] = max $h[$y], $h; 1674
1675 if ($h > $sh) {
1676 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1671 } 1677 }
1672 } 1678 }
1673 1679
1674 (\@w, \@h) 1680 (\@w, \@h)
1675} 1681}
1691 my ($ws, $hs) = $self->get_wh; 1697 my ($ws, $hs) = $self->get_wh;
1692 1698
1693 my $req_w = (sum @$ws) || 1; 1699 my $req_w = (sum @$ws) || 1;
1694 my $req_h = (sum @$hs) || 1; 1700 my $req_h = (sum @$hs) || 1;
1695 1701
1696 # TODO: nicer code 1702 # now linearly scale the rows/columns to the allocated size
1697 my @col_expand = @{$self->{col_expand}}; 1703 my @col_expand = @{$self->{col_expand}};
1698 @col_expand = (1) x @$ws unless @col_expand; 1704 @col_expand = (1) x @$ws unless @col_expand;
1699 my $col_expand = (sum @col_expand) || 1; 1705 my $col_expand = (sum @col_expand) || 1;
1700 1706
1701 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1707 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1702 1708
1703 CFPlus::UI::harmonize $ws; 1709 DC::UI::harmonize $ws;
1704 1710
1705 my @row_expand = @{$self->{row_expand}}; 1711 my @row_expand = @{$self->{row_expand}};
1706 @row_expand = (1) x @$ws unless @row_expand; 1712 @row_expand = (1) x @$ws unless @row_expand;
1707 my $row_expand = (sum @row_expand) || 1; 1713 my $row_expand = (sum @row_expand) || 1;
1708 1714
1709 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1715 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1710 1716
1711 CFPlus::UI::harmonize $hs; 1717 DC::UI::harmonize $hs;
1712 1718
1713 my $y; 1719 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1720 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1714 1721
1715 for my $r (0 .. $#{$self->{children}}) { 1722 for my $widget ($self->children) {
1716 my $row = $self->{children}[$r] 1723 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1717 or next;
1718 1724
1719 my $x = 0; 1725 $widget->configure (
1720 my $row_h = $hs->[$r]; 1726 $x[$c], $y[$r],
1727 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1721 1728 );
1722 for my $c (0 .. $#$row) {
1723 my $col_w = $ws->[$c];
1724
1725 if (my $widget = $row->[$c]) {
1726 $widget->configure ($x, $y, $col_w, $row_h);
1727 }
1728
1729 $x += $col_w;
1730 }
1731
1732 $y += $row_h;
1733 } 1729 }
1734 1730
1735 1 1731 1
1736} 1732}
1737 1733
1738sub find_widget {
1739 my ($self, $x, $y) = @_;
1740
1741 $x -= $self->{x};
1742 $y -= $self->{y};
1743
1744 my $res;
1745
1746 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1747 $res = $_->find_widget ($x, $y)
1748 and return $res;
1749 }
1750
1751 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1752}
1753
1754sub _draw {
1755 my ($self) = @_;
1756
1757 for (grep $_, @{$self->{children}}) {
1758 $_->draw for grep $_, @$_;
1759 }
1760}
1761
1762############################################################################# 1734#############################################################################
1763 1735
1764package CFPlus::UI::Fixed; 1736package DC::UI::Fixed;
1765 1737
1766use List::Util qw(min max); 1738use List::Util qw(min max);
1767 1739
1768our @ISA = CFPlus::UI::Container::; 1740our @ISA = DC::UI::Container::;
1769
1770sub add {
1771 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1772
1773 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1774 $self->SUPER::add ($child);
1775}
1776 1741
1777sub _scale($$$) { 1742sub _scale($$$) {
1778 my ($mode, $val, $max) = @_; 1743 my ($rel, $val, $max) = @_;
1779 1744
1780 $mode eq "abs" ? $val 1745 $rel ? $val * $max : $val
1781 : $mode eq "rel" ? $val * $max
1782 : 0
1783} 1746}
1784 1747
1785sub size_request { 1748sub size_request {
1786 my ($self) = @_; 1749 my ($self) = @_;
1787 1750
1788 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1751 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1789 1752
1790 # determine overall size by querying abs widgets 1753 # determine overall size by querying abs widgets
1791 for my $child ($self->visible_children) { 1754 for my $child ($self->visible_children) {
1792 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1755 unless ($child->{c_rel}) {
1756 my $x = $child->{c_x};
1757 my $y = $child->{c_y};
1793 1758
1794 if ($pos eq "abs") {
1795 $w = _scale $size, $w, $child->{req_w};
1796 $h = _scale $size, $h, $child->{req_h};
1797
1798 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1759 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1799 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1760 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1800 } 1761 }
1801 } 1762 }
1802 1763
1803 my $W = $x2 - $x1; 1764 my $W = $x2 - $x1;
1804 my $H = $y2 - $y1; 1765 my $H = $y2 - $y1;
1805 1766
1806 # now layout remaining widgets 1767 # now layout remaining widgets
1807 for my $child ($self->visible_children) { 1768 for my $child ($self->visible_children) {
1808 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1769 if ($child->{c_rel}) {
1770 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1771 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1809 1772
1810 if ($pos ne "abs") {
1811 $x = _scale $pos, $x, $W;
1812 $y = _scale $pos, $x, $H;
1813 $w = _scale $size, $w, $child->{req_w};
1814 $h = _scale $size, $h, $child->{req_h};
1815
1816 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1773 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1817 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1774 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1818 } 1775 }
1819 } 1776 }
1820 1777
1821 my $W = $x2 - $x1; 1778 my $W = $x2 - $x1;
1822 my $H = $y2 - $y1; 1779 my $H = $y2 - $y1;
1826 1783
1827sub invoke_size_allocate { 1784sub invoke_size_allocate {
1828 my ($self, $W, $H) = @_; 1785 my ($self, $W, $H) = @_;
1829 1786
1830 for my $child ($self->visible_children) { 1787 for my $child ($self->visible_children) {
1831 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1788 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1789 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1832 1790
1833 $x = _scale $pos, $x, $W; 1791 $x += $child->{c_halign} * $child->{req_w};
1834 $y = _scale $pos, $x, $H; 1792 $y += $child->{c_valign} * $child->{req_h};
1835 $w = _scale $size, $w, $child->{req_w};
1836 $h = _scale $size, $h, $child->{req_h};
1837 1793
1838 $child->configure ($x, $y, $w, $h); 1794 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1839 } 1795 }
1840 1796
1841 1 1797 1
1842} 1798}
1843 1799
1844############################################################################# 1800#############################################################################
1845 1801
1846package CFPlus::UI::Box; 1802package DC::UI::Box;
1847 1803
1848our @ISA = CFPlus::UI::Container::; 1804our @ISA = DC::UI::Container::;
1849 1805
1850sub size_request { 1806sub size_request {
1851 my ($self) = @_; 1807 my ($self) = @_;
1808
1809 my @children = $self->visible_children;
1852 1810
1853 $self->{vertical} 1811 $self->{vertical}
1854 ? ( 1812 ? (
1855 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1813 (List::Util::max map $_->{req_w}, @children),
1856 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1814 (List::Util::sum map $_->{req_h}, @children),
1857 ) 1815 )
1858 : ( 1816 : (
1859 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1817 (List::Util::sum map $_->{req_w}, @children),
1860 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1818 (List::Util::max map $_->{req_h}, @children),
1861 ) 1819 )
1862} 1820}
1863 1821
1864sub invoke_size_allocate { 1822sub invoke_size_allocate {
1865 my ($self, $w, $h) = @_; 1823 my ($self, $w, $h) = @_;
1886 $req[$_] += $space * $children[$_]{expand} 1844 $req[$_] += $space * $children[$_]{expand}
1887 for 0 .. $#children; 1845 for 0 .. $#children;
1888 } 1846 }
1889 } 1847 }
1890 1848
1891 CFPlus::UI::harmonize \@req; 1849 DC::UI::harmonize \@req;
1892 1850
1893 my $pos = 0; 1851 my $pos = 0;
1894 for (0 .. $#children) { 1852 for (0 .. $#children) {
1895 my $alloc = $req[$_]; 1853 my $alloc = $req[$_];
1896 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1854 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1901 1 1859 1
1902} 1860}
1903 1861
1904############################################################################# 1862#############################################################################
1905 1863
1906package CFPlus::UI::HBox; 1864package DC::UI::HBox;
1907 1865
1908our @ISA = CFPlus::UI::Box::; 1866our @ISA = DC::UI::Box::;
1909 1867
1910sub new { 1868sub new {
1911 my $class = shift; 1869 my $class = shift;
1912 1870
1913 $class->SUPER::new ( 1871 $class->SUPER::new (
1916 ) 1874 )
1917} 1875}
1918 1876
1919############################################################################# 1877#############################################################################
1920 1878
1921package CFPlus::UI::VBox; 1879package DC::UI::VBox;
1922 1880
1923our @ISA = CFPlus::UI::Box::; 1881our @ISA = DC::UI::Box::;
1924 1882
1925sub new { 1883sub new {
1926 my $class = shift; 1884 my $class = shift;
1927 1885
1928 $class->SUPER::new ( 1886 $class->SUPER::new (
1931 ) 1889 )
1932} 1890}
1933 1891
1934############################################################################# 1892#############################################################################
1935 1893
1936package CFPlus::UI::Label; 1894package DC::UI::Label;
1937 1895
1938our @ISA = CFPlus::UI::DrawBG::; 1896our @ISA = DC::UI::DrawBG::;
1939 1897
1940use CFPlus::OpenGL; 1898use DC::OpenGL;
1941 1899
1942sub new { 1900sub new {
1943 my ($class, %arg) = @_; 1901 my ($class, %arg) = @_;
1944 1902
1945 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1950 #text => initial text 1908 #text => initial text
1951 #markup => initial narkup 1909 #markup => initial narkup
1952 #max_w => maximum pixel width 1910 #max_w => maximum pixel width
1953 #style => 0, # render flags 1911 #style => 0, # render flags
1954 ellipsise => 3, # end 1912 ellipsise => 3, # end
1955 layout => (new CFPlus::Layout), 1913 layout => (new DC::Layout),
1956 fontsize => 1, 1914 fontsize => 1,
1957 align => -1, 1915 align => 0.5,
1958 valign => -1, 1916 valign => 0.5,
1959 padding_x => 2, 1917 padding_x => 4,
1960 padding_y => 2, 1918 padding_y => 2,
1961 can_events => 0, 1919 can_events => 0,
1962 %arg 1920 %arg
1963 ); 1921 );
1964 1922
1965 if (exists $self->{template}) { 1923 if (exists $self->{template}) {
1966 my $layout = new CFPlus::Layout; 1924 my $layout = new DC::Layout;
1967 $layout->set_text (delete $self->{template}); 1925 $layout->set_text (delete $self->{template});
1968 $self->{template} = $layout; 1926 $self->{template} = $layout;
1969 } 1927 }
1970 1928
1971 if (exists $self->{markup}) { 1929 if (exists $self->{markup}) {
1989 1947
1990 delete $self->{ox}; 1948 delete $self->{ox};
1991 $self->SUPER::realloc; 1949 $self->SUPER::realloc;
1992} 1950}
1993 1951
1952sub clear {
1953 my ($self) = @_;
1954
1955 $self->set_text ("");
1956}
1957
1994sub set_text { 1958sub set_text {
1995 my ($self, $text) = @_; 1959 my ($self, $text) = @_;
1996 1960
1997 return if $self->{text} eq "T$text"; 1961 return if $self->{text} eq "T$text";
1998 $self->{text} = "T$text"; 1962 $self->{text} = "T$text";
2021 1985
2022sub size_request { 1986sub size_request {
2023 my ($self) = @_; 1987 my ($self) = @_;
2024 1988
2025 $self->{size_req} ||= do { 1989 $self->{size_req} ||= do {
1990 my ($max_w, $max_h) = $self->get_max_wh;
1991
2026 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1992 $self->{layout}->set_font ($self->{font}) if $self->{font};
2027 $self->{layout}->set_width ($self->{max_w} || -1); 1993 $self->{layout}->set_width ($max_w);
2028 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1994 $self->{layout}->set_ellipsise ($self->{ellipsise});
2029 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1995 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2030 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1996 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2031 1997
2032 my ($w, $h) = $self->{layout}->size; 1998 my ($w, $h) = $self->{layout}->size;
2033 1999
2034 if (exists $self->{template}) { 2000 if (exists $self->{template}) {
2035 $self->{template}->set_font ($self->{font}) if $self->{font}; 2001 $self->{template}->set_font ($self->{font}) if $self->{font};
2036 $self->{template}->set_width ($self->{max_w} || -1); 2002 $self->{template}->set_width ($max_w);
2037 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2003 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
2038 2004
2039 my ($w2, $h2) = $self->{template}->size; 2005 my ($w2, $h2) = $self->{template}->size;
2040 2006
2041 $w = List::Util::max $w, $w2; 2007 $w = List::Util::max $w, $w2;
2097 2063
2098 [$self->{layout}->size] 2064 [$self->{layout}->size]
2099 }; 2065 };
2100 2066
2101 unless (exists $self->{ox}) { 2067 unless (exists $self->{ox}) {
2102 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2068 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2103 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2069 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2104 : ($self->{w} - $size->[0]) * 0.5);
2105 2070
2106 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2071 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2107 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2108 : ($self->{h} - $size->[1]) * 0.5);
2109 }; 2072 };
2110 2073
2111 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2074# unless ($self->{list}) {
2112 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2075# $self->{list} = DC::OpenGL::glGenList;
2113 2076# DC::OpenGL::glNewList $self->{list};
2114 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2077# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2078# DC::OpenGL::glEndList;
2079# }
2080#
2081# DC::OpenGL::glCallList $self->{list};
2082
2083 $self->{layout}->draw;
2115} 2084}
2085
2086#sub destroy {
2087# my ($self) = @_;
2088#
2089# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2090#
2091# $self->SUPER::destroy;
2092#}
2116 2093
2117############################################################################# 2094#############################################################################
2118 2095
2119package CFPlus::UI::EntryBase; 2096package DC::UI::EntryBase;
2120 2097
2121our @ISA = CFPlus::UI::Label::; 2098our @ISA = DC::UI::Label::;
2122 2099
2123use CFPlus::OpenGL; 2100use DC::OpenGL;
2124 2101
2125sub new { 2102sub new {
2126 my $class = shift; 2103 my $class = shift;
2127 2104
2128 $class->SUPER::new ( 2105 $class->SUPER::new (
2129 fg => [1, 1, 1], 2106 fg => [1, 1, 1],
2130 bg => [0, 0, 0, 0.2], 2107 bg => [0, 0, 0, 0.2],
2108 outline => undef,
2131 active_bg => [1, 1, 1, 0.5], 2109 active_bg => [0, 0, 1, .2],
2132 active_fg => [0, 0, 0], 2110 active_fg => [1, 1, 1],
2111 active_outline => [1, 1, 0],
2133 can_hover => 1, 2112 can_hover => 1,
2134 can_focus => 1, 2113 can_focus => 1,
2114 align => 0,
2135 valign => 0, 2115 valign => 0.5,
2136 can_events => 1, 2116 can_events => 1,
2137 ellipsise => 0, 2117 ellipsise => 0,
2118 padding_x => 4,
2119 padding_y => 2,
2138 #text => ... 2120 #text => ...
2139 #hidden => "*", 2121 #hidden => "*",
2140 @_ 2122 @_
2141 ) 2123 )
2142} 2124}
2189 2171
2190 my $text = $self->get_text; 2172 my $text = $self->get_text;
2191 2173
2192 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2174 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2193 2175
2194 if ($uni == 8) { 2176 if ($sym == DC::SDLK_BACKSPACE) {
2195 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2177 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2196 } elsif ($uni == 127) { 2178 } elsif ($sym == DC::SDLK_DELETE) {
2197 substr $text, $self->{cursor}, 1, ""; 2179 substr $text, $self->{cursor}, 1, "";
2198 } elsif ($sym == CFPlus::SDLK_LEFT) { 2180 } elsif ($sym == DC::SDLK_LEFT) {
2199 --$self->{cursor} if $self->{cursor}; 2181 --$self->{cursor} if $self->{cursor};
2200 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2182 } elsif ($sym == DC::SDLK_RIGHT) {
2201 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2183 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2202 } elsif ($sym == CFPlus::SDLK_HOME) { 2184 } elsif ($sym == DC::SDLK_HOME) {
2203 # what a hack 2185 # what a hack
2204 $self->{cursor} = 2186 $self->{cursor} =
2205 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2187 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2206 ? length $1 2188 ? length $1
2207 : 0; 2189 : 0;
2208 } elsif ($sym == CFPlus::SDLK_END) { 2190 } elsif ($sym == DC::SDLK_END) {
2209 # uh, again 2191 # uh, again
2210 $self->{cursor} = 2192 $self->{cursor} =
2211 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2193 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2212 ? $self->{cursor} + length $1 2194 ? $self->{cursor} + length $1
2213 : length $self->{text}; 2195 : length $self->{text};
2277 glColor_premultiply @{$self->{bg}}; 2259 glColor_premultiply @{$self->{bg}};
2278 } 2260 }
2279 2261
2280 glEnable GL_BLEND; 2262 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2263 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2282 glBegin GL_QUADS;
2283 glVertex 0 , 0;
2284 glVertex 0 , $self->{h};
2285 glVertex $self->{w}, $self->{h}; 2264 glRect 0, 0, $self->{w}, $self->{h};
2286 glVertex $self->{w}, 0;
2287 glEnd;
2288 glDisable GL_BLEND; 2265 glDisable GL_BLEND;
2289 2266
2290 $self->SUPER::_draw; 2267 $self->SUPER::_draw;
2291 2268
2292 #TODO: force update every cursor change :( 2269 #TODO: force update every cursor change :(
2294 2271
2295 unless (exists $self->{cur_h}) { 2272 unless (exists $self->{cur_h}) {
2296 my $text = substr $self->{text}, 0, $self->{cursor}; 2273 my $text = substr $self->{text}, 0, $self->{cursor};
2297 utf8::encode $text; 2274 utf8::encode $text;
2298 2275
2299 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2276 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2300 } 2277 }
2301 2278
2279 glColor_premultiply @{$self->{active_fg}};
2302 glBegin GL_LINES; 2280 glBegin GL_LINES;
2303 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2281 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2304 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2282 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2305 glEnd; 2283 glEnd;
2284
2285 glLineWidth 3;
2286 glColor @{$self->{active_outline}};
2287 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2288 glLineWidth 1;
2289
2290 } else {
2291 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2292 glBegin GL_LINE_STRIP;
2293 glVertex .5, $self->{h} * .5;
2294 glVertex .5, $self->{h} - 2.5;
2295 glVertex $self->{w} - .5, $self->{h} - 2.5;
2296 glVertex $self->{w} - .5, $self->{h} * .5;
2297 glEnd;
2306 } 2298 }
2307} 2299}
2308 2300
2309############################################################################# 2301#############################################################################
2310 2302
2311package CFPlus::UI::Entry; 2303package DC::UI::Entry;
2312 2304
2313our @ISA = CFPlus::UI::EntryBase::; 2305our @ISA = DC::UI::EntryBase::;
2314 2306
2315use CFPlus::OpenGL; 2307use DC::OpenGL;
2308
2309sub new {
2310 my $class = shift;
2311
2312 $class->SUPER::new (
2313 history_pointer => -1,
2314 @_
2315 )
2316}
2317
2316 2318
2317sub invoke_key_down { 2319sub invoke_key_down {
2318 my ($self, $ev) = @_; 2320 my ($self, $ev) = @_;
2319 2321
2320 my $sym = $ev->{sym}; 2322 my $sym = $ev->{sym};
2326 $self->{history_pointer} = -1; 2328 $self->{history_pointer} = -1;
2327 $self->{history_saveback} = ''; 2329 $self->{history_saveback} = '';
2328 $self->emit (activate => $txt); 2330 $self->emit (activate => $txt);
2329 $self->update; 2331 $self->update;
2330 2332
2331 } elsif ($sym == CFPlus::SDLK_UP) { 2333 } elsif ($sym == DC::SDLK_UP) {
2332 if ($self->{history_pointer} < 0) { 2334 if ($self->{history_pointer} < 0) {
2333 $self->{history_saveback} = $self->get_text; 2335 $self->{history_saveback} = $self->get_text;
2334 } 2336 }
2335 if (@{$self->{history} || []} > 0) { 2337 if (@{$self->{history} || []} > 0) {
2336 $self->{history_pointer}++; 2338 $self->{history_pointer}++;
2338 $self->{history_pointer} = @{$self->{history} || []} - 1; 2340 $self->{history_pointer} = @{$self->{history} || []} - 1;
2339 } 2341 }
2340 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2341 } 2343 }
2342 2344
2343 } elsif ($sym == CFPlus::SDLK_DOWN) { 2345 } elsif ($sym == DC::SDLK_DOWN) {
2344 $self->{history_pointer}--; 2346 $self->{history_pointer}--;
2345 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2347 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2346 2348
2347 if ($self->{history_pointer} >= 0) { 2349 if ($self->{history_pointer} >= 0) {
2348 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2350 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2349 } else { 2351 } else {
2352 if (defined $self->{history_saveback}) {
2350 $self->set_text ($self->{history_saveback}); 2353 $self->set_text ($self->{history_saveback});
2354 $self->{history_saveback} = undef;
2355 }
2351 } 2356 }
2352 2357
2353 } else { 2358 } else {
2354 return $self->SUPER::invoke_key_down ($ev) 2359 return $self->SUPER::invoke_key_down ($ev)
2355 } 2360 }
2357 1 2362 1
2358} 2363}
2359 2364
2360############################################################################# 2365#############################################################################
2361 2366
2362package CFPlus::UI::TextEdit; 2367package DC::UI::TextEdit;
2363 2368
2364our @ISA = CFPlus::UI::EntryBase::; 2369our @ISA = DC::UI::EntryBase::;
2365 2370
2366use CFPlus::OpenGL; 2371use DC::OpenGL;
2372
2373sub new {
2374 my $class = shift;
2375
2376 $class->SUPER::new (
2377 padding_y => 4,
2378
2379 @_
2380 )
2381}
2367 2382
2368sub move_cursor_ver { 2383sub move_cursor_ver {
2369 my ($self, $dy) = @_; 2384 my ($self, $dy) = @_;
2370 2385
2371 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2386 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2372 2387
2373 $y += $dy; 2388 $line += $dy;
2374 2389
2375 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2390 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2376 $self->{cursor} = $index; 2391 $self->{cursor} = $index;
2377 delete $self->{cur_h}; 2392 delete $self->{cur_h};
2378 $self->update; 2393 $self->update;
2379 return; 2394 return;
2380 } 2395 }
2383sub invoke_key_down { 2398sub invoke_key_down {
2384 my ($self, $ev) = @_; 2399 my ($self, $ev) = @_;
2385 2400
2386 my $sym = $ev->{sym}; 2401 my $sym = $ev->{sym};
2387 2402
2388 if ($sym == CFPlus::SDLK_UP) { 2403 if ($sym == DC::SDLK_UP) {
2389 $self->move_cursor_ver (-1); 2404 $self->move_cursor_ver (-1);
2390 } elsif ($sym == CFPlus::SDLK_DOWN) { 2405 } elsif ($sym == DC::SDLK_DOWN) {
2391 $self->move_cursor_ver (+1); 2406 $self->move_cursor_ver (+1);
2392 } else { 2407 } else {
2393 return $self->SUPER::invoke_key_down ($ev) 2408 return $self->SUPER::invoke_key_down ($ev)
2394 } 2409 }
2395 2410
2396 1 2411 1
2397} 2412}
2398 2413
2399############################################################################# 2414#############################################################################
2400 2415
2401package CFPlus::UI::Button; 2416package DC::UI::ButtonBin;
2402 2417
2403our @ISA = CFPlus::UI::Label::; 2418our @ISA = DC::UI::Bin::;
2404 2419
2405use CFPlus::OpenGL; 2420use DC::OpenGL;
2406 2421
2407my @tex = 2422my @tex =
2408 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2423 map { new_from_resource DC::Texture $_, mipmap => 1 }
2409 qw(b1_button_inactive.png b1_button_active.png); 2424 qw(b1_button_inactive.png b1_button_active.png);
2410 2425
2411sub new { 2426sub new {
2412 my $class = shift; 2427 my $class = shift;
2413 2428
2414 $class->SUPER::new ( 2429 $class->SUPER::new (
2430 can_hover => 1,
2431 align => 0.5,
2432 valign => 0.5,
2433 can_events => 1,
2434 @_
2435 )
2436}
2437
2438sub invoke_button_up {
2439 my ($self, $ev, $x, $y) = @_;
2440
2441 $self->emit ("activate")
2442 if $x >= 0 && $x < $self->{w}
2443 && $y >= 0 && $y < $self->{h};
2444
2445 1
2446}
2447
2448sub _draw {
2449 my ($self) = @_;
2450
2451 glEnable GL_TEXTURE_2D;
2452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2453 glColor 0, 0, 0, 1;
2454
2455 my $tex = $tex[$GRAB == $self];
2456 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2457
2458 glDisable GL_TEXTURE_2D;
2459
2460 $self->SUPER::_draw;
2461}
2462
2463#############################################################################
2464
2465package DC::UI::Button;
2466
2467our @ISA = DC::UI::Label::;
2468
2469use DC::OpenGL;
2470
2471my @tex =
2472 map { new_from_resource DC::Texture $_, mipmap => 1 }
2473 qw(b1_button_inactive.png b1_button_active.png);
2474
2475sub new {
2476 my $class = shift;
2477
2478 $class->SUPER::new (
2415 padding_x => 4, 2479 padding_x => 8,
2416 padding_y => 4, 2480 padding_y => 4,
2417 fg => [1.0, 1.0, 1.0], 2481 fg => [1.0, 1.0, 1.0],
2418 active_fg => [0.8, 0.8, 0.8], 2482 active_fg => [0.8, 0.8, 0.8],
2419 can_hover => 1, 2483 can_hover => 1,
2420 align => 0, 2484 align => 0.5,
2421 valign => 0, 2485 valign => 0.5,
2422 can_events => 1, 2486 can_events => 1,
2423 @_ 2487 @_
2424 ) 2488 )
2425} 2489}
2426 2490
2451 $self->SUPER::_draw; 2515 $self->SUPER::_draw;
2452} 2516}
2453 2517
2454############################################################################# 2518#############################################################################
2455 2519
2456package CFPlus::UI::CheckBox; 2520package DC::UI::CheckBox;
2457 2521
2458our @ISA = CFPlus::UI::DrawBG::; 2522our @ISA = DC::UI::DrawBG::;
2459 2523
2460my @tex = 2524my @tex =
2461 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2525 map { new_from_resource DC::Texture $_, mipmap => 1 }
2462 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2526 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2463 2527
2464use CFPlus::OpenGL; 2528use DC::OpenGL;
2465 2529
2466sub new { 2530sub new {
2467 my $class = shift; 2531 my $class = shift;
2468 2532
2469 $class->SUPER::new ( 2533 $class->SUPER::new (
2534 fontsize => 1,
2470 padding_x => 2, 2535 padding_x => 2,
2471 padding_y => 2, 2536 padding_y => 2,
2472 fg => [1, 1, 1], 2537 fg => [1, 1, 1],
2473 active_fg => [1, 1, 0], 2538 active_fg => [1, 1, 0],
2474 bg => [0, 0, 0, 0.2], 2539 bg => [0, 0, 0, 0.2],
2480} 2545}
2481 2546
2482sub size_request { 2547sub size_request {
2483 my ($self) = @_; 2548 my ($self) = @_;
2484 2549
2485 (6) x 2 2550 ($self->{fontsize} * $::FONTSIZE) x 2
2486} 2551}
2487 2552
2488sub toggle { 2553sub toggle {
2489 my ($self) = @_; 2554 my ($self) = @_;
2490 2555
2509sub _draw { 2574sub _draw {
2510 my ($self) = @_; 2575 my ($self) = @_;
2511 2576
2512 $self->SUPER::_draw; 2577 $self->SUPER::_draw;
2513 2578
2514 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2579 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2515 2580
2516 my ($w, $h) = @$self{qw(w h)}; 2581 my ($w, $h) = @$self{qw(w h)};
2517 2582
2518 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2583 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2519 2584
2526 glDisable GL_TEXTURE_2D; 2591 glDisable GL_TEXTURE_2D;
2527} 2592}
2528 2593
2529############################################################################# 2594#############################################################################
2530 2595
2531package CFPlus::UI::Image; 2596package DC::UI::Image;
2532 2597
2533our @ISA = CFPlus::UI::Base::; 2598our @ISA = DC::UI::DrawBG::;
2534 2599
2535use CFPlus::OpenGL; 2600use DC::OpenGL;
2536 2601
2537our %texture_cache; 2602our %texture_cache;
2538 2603
2539sub new { 2604sub new {
2540 my $class = shift; 2605 my $class = shift;
2541 2606
2542 my $self = $class->SUPER::new ( 2607 my $self = $class->SUPER::new (
2543 can_events => 0, 2608 can_events => 0,
2609 scale => 1,
2544 @_, 2610 @_,
2545 ); 2611 );
2546 2612
2547 $self->{path} || $self->{tex} 2613 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2614 or Carp::croak "'path' or 'tex' attributes required";
2549 2615
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2616 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2617 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2552 2618
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2619 DC::weaken $texture_cache{$self->{path}};
2554 2620
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2621 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2622
2557 $self 2623 $self
2558} 2624}
2559 2625
2560sub STORABLE_freeze { 2626sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2627 my ($self, $cloning) = @_;
2562 2628
2563 $self->{path} 2629 $self->{path}
2564 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2630 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2565 2631
2566 $self->{path} 2632 $self->{path}
2567} 2633}
2568 2634
2569sub STORABLE_attach { 2635sub STORABLE_attach {
2570 my ($self, $cloning, $path) = @_; 2636 my ($self, $cloning, $path) = @_;
2571 2637
2572 $self->new (path => $path) 2638 $self->new (path => $path)
2573} 2639}
2574 2640
2641sub set_texture {
2642 my ($self, $tex) = @_;
2643
2644 $self->{tex} = $tex;
2645 $self->update;
2646}
2647
2575sub size_request { 2648sub size_request {
2576 my ($self) = @_; 2649 my ($self) = @_;
2577 2650
2578 ($self->{tex}{w}, $self->{tex}{h}) 2651 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2579} 2652}
2580 2653
2581sub _draw { 2654sub _draw {
2582 my ($self) = @_; 2655 my ($self) = @_;
2656
2657 $self->SUPER::_draw;
2583 2658
2584 my $tex = $self->{tex}; 2659 my $tex = $self->{tex};
2585 2660
2586 my ($w, $h) = ($self->{w}, $self->{h}); 2661 my ($w, $h) = ($self->{w}, $self->{h});
2587 2662
2593 } 2668 }
2594 2669
2595 glEnable GL_TEXTURE_2D; 2670 glEnable GL_TEXTURE_2D;
2596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2671 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2597 2672
2598 $tex->draw_quad (0, 0, $w, $h); 2673 $tex->draw_quad_alpha (0, 0, $w, $h);
2599 2674
2600 glDisable GL_TEXTURE_2D; 2675 glDisable GL_TEXTURE_2D;
2601} 2676}
2602 2677
2603############################################################################# 2678#############################################################################
2604 2679
2605package CFPlus::UI::ImageButton; 2680package DC::UI::ImageButton;
2606 2681
2607our @ISA = CFPlus::UI::Image::; 2682our @ISA = DC::UI::Image::;
2608 2683
2609use CFPlus::OpenGL; 2684use DC::OpenGL;
2610
2611my %textures;
2612 2685
2613sub new { 2686sub new {
2614 my $class = shift; 2687 my $class = shift;
2615 2688
2616 my $self = $class->SUPER::new ( 2689 my $self = $class->SUPER::new (
2617 padding_x => 4, 2690 padding_x => 4,
2618 padding_y => 4, 2691 padding_y => 4,
2619 fg => [1, 1, 1], 2692 fg => [1, 1, 1],
2620 active_fg => [0, 0, 1], 2693 active_fg => [0, 0, 1],
2621 can_hover => 1, 2694 can_hover => 1,
2622 align => 0, 2695 align => 0.5,
2623 valign => 0, 2696 valign => 0.5,
2624 can_events => 1, 2697 can_events => 1,
2625 @_ 2698 @_
2626 ); 2699 );
2627} 2700}
2628 2701
2702sub invoke_button_down {
2703 my ($self, $ev, $x, $y) = @_;
2704
2705 1
2706}
2707
2629sub invoke_button_up { 2708sub invoke_button_up {
2630 my ($self, $ev, $x, $y) = @_; 2709 my ($self, $ev, $x, $y) = @_;
2631 2710
2632 $self->emit ("activate") 2711 $self->emit ("activate")
2633 if $x >= 0 && $x < $self->{w} 2712 if $x >= 0 && $x < $self->{w}
2636 1 2715 1
2637} 2716}
2638 2717
2639############################################################################# 2718#############################################################################
2640 2719
2641package CFPlus::UI::VGauge; 2720package DC::UI::VGauge;
2642 2721
2643our @ISA = CFPlus::UI::Base::; 2722our @ISA = DC::UI::Base::;
2644 2723
2645use List::Util qw(min max); 2724use List::Util qw(min max);
2646 2725
2647use CFPlus::OpenGL; 2726use DC::OpenGL;
2648 2727
2649my %tex = ( 2728my %tex = (
2650 food => [ 2729 food => [
2651 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2730 map { new_from_resource DC::Texture $_, mipmap => 1 }
2652 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2731 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2653 ], 2732 ],
2654 grace => [ 2733 grace => [
2655 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2656 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2735 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2657 ], 2736 ],
2658 hp => [ 2737 hp => [
2659 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2660 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2739 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2661 ], 2740 ],
2662 mana => [ 2741 mana => [
2663 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2664 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2743 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2665 ], 2744 ],
2666); 2745);
2667 2746
2668# eg. VGauge->new (gauge => 'food'), default gauge: food 2747# eg. VGauge->new (gauge => 'food'), default gauge: food
2771 glDisable GL_TEXTURE_2D; 2850 glDisable GL_TEXTURE_2D;
2772} 2851}
2773 2852
2774############################################################################# 2853#############################################################################
2775 2854
2855package DC::UI::Progress;
2856
2857our @ISA = DC::UI::Label::;
2858
2859use DC::OpenGL;
2860
2861sub new {
2862 my ($class, %arg) = @_;
2863
2864 my $self = $class->SUPER::new (
2865 padding_x => 2,
2866 padding_y => 2,
2867 fg => [1, 1, 1],
2868 bg => [0, 0, 1, 0.2],
2869 bar => [0.7, 0.5, 0.1, 0.8],
2870 outline => [0.4, 0.3, 0],
2871 fontsize => 0.9,
2872 valign => 0.5,
2873 align => 0.5,
2874 can_events => 1,
2875 ellipsise => 1,
2876 label => "%d%%",
2877 %arg,
2878 );
2879
2880 $self->set_value ($arg{value} || -1);
2881
2882 $self
2883}
2884
2885sub set_label {
2886 my ($self, $label) = @_;
2887
2888 return if $self->{label} eq $label;
2889 $self->{label} = $label;
2890
2891 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2892}
2893
2894sub set_value {
2895 my ($self, $value) = @_;
2896
2897 if ($self->{value} ne $value) {
2898 $self->{value} = $value;
2899
2900 if ($value < 0) {
2901 $self->set_text ("-");
2902 } else {
2903 $self->set_text (sprintf $self->{label}, $value * 100);
2904 }
2905
2906 $self->update;
2907 }
2908}
2909
2910sub _draw {
2911 my ($self) = @_;
2912
2913 glEnable GL_BLEND;
2914 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2915
2916 my $px = $self->{padding_x};
2917 my $py = $self->{padding_y};
2918
2919 if ($self->{value} >= 0) {
2920 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2921
2922 glColor_premultiply @{$self->{bar}};
2923 glRect $px, $py, $s, $self->{h} - $py;
2924 glColor_premultiply @{$self->{bg}};
2925 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2926 }
2927
2928 glColor_premultiply @{$self->{outline}};
2929
2930 $px -= .5;
2931 $py -= .5;
2932
2933 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2934
2935 glDisable GL_BLEND;
2936
2937 {
2938 local $self->{bg}; # do not draw background
2939 $self->SUPER::_draw;
2940 }
2941}
2942
2943#############################################################################
2944
2945package DC::UI::ExperienceProgress;
2946
2947our @ISA = DC::UI::Progress::;
2948
2949sub new {
2950 my ($class, %arg) = @_;
2951
2952 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2953
2954 my $self = $class->SUPER::new (
2955 %arg,
2956 tooltip => sub {
2957 my ($self) = @_;
2958
2959 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2960 $tt,
2961 $self->{lvl},
2962 ::formsep ($self->{exp}),
2963 ::formsep ($self->{nxt}),
2964 ::formsep ($self->{nxt} - $self->{exp}),
2965 $self->_percent * 100,
2966 },
2967 );
2968
2969 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2970 if $::CONN;
2971
2972 $self
2973}
2974
2975sub DESTROY {
2976 my ($self) = @_;
2977
2978 delete $::CONN->{on_exp_update}{$self+0}
2979 if $::CONN;
2980
2981 $self->SUPER::DESTROY;
2982}
2983
2984sub _percent {
2985 my ($self) = @_;
2986
2987 my $table = $::CONN && $::CONN->{exp_table}
2988 or return -1;
2989
2990 my $l0 = $table->[$self->{lvl} - 1];
2991 my $l1 = $table->[$self->{lvl}];
2992
2993 $self->{nxt} = $l1;
2994
2995 ($self->{exp} - $l0) / ($l1 - $l0)
2996}
2997
2998sub set_value {
2999 my ($self, $lvl, $exp) = @_;
3000
3001 $self->{lvl} = $lvl;
3002 $self->{exp} = $exp;
3003
3004 $self->SUPER::set_value ($self->_percent);
3005}
3006
3007#############################################################################
3008
2776package CFPlus::UI::Gauge; 3009package DC::UI::Gauge;
2777 3010
2778our @ISA = CFPlus::UI::VBox::; 3011our @ISA = DC::UI::VBox::;
2779 3012
2780sub new { 3013sub new {
2781 my ($class, %arg) = @_; 3014 my ($class, %arg) = @_;
2782 3015
2783 my $self = $class->SUPER::new ( 3016 my $self = $class->SUPER::new (
2785 can_hover => 1, 3018 can_hover => 1,
2786 can_events => 1, 3019 can_events => 1,
2787 %arg, 3020 %arg,
2788 ); 3021 );
2789 3022
2790 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3023 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2791 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3024 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2792 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3025 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2793 3026
2794 $self 3027 $self
2795} 3028}
2796 3029
2797sub set_fontsize { 3030sub set_fontsize {
2818 $self->{value}->set_text ($val); 3051 $self->{value}->set_text ($val);
2819} 3052}
2820 3053
2821############################################################################# 3054#############################################################################
2822 3055
2823package CFPlus::UI::Slider; 3056package DC::UI::Slider;
2824 3057
2825use strict; 3058use strict;
2826 3059
2827use CFPlus::OpenGL; 3060use DC::OpenGL;
2828 3061
2829our @ISA = CFPlus::UI::DrawBG::; 3062our @ISA = DC::UI::DrawBG::;
2830 3063
2831my @tex = 3064my @tex =
2832 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3065 map { new_from_resource DC::Texture $_ }
2833 qw(s1_slider.png s1_slider_bg.png); 3066 qw(s1_slider.png s1_slider_bg.png);
2834 3067
2835sub new { 3068sub new {
2836 my $class = shift; 3069 my $class = shift;
2837 3070
2874sub set_value { 3107sub set_value {
2875 my ($self, $value) = @_; 3108 my ($self, $value) = @_;
2876 3109
2877 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2878 3111
2879 $hi = $lo + 1 if $hi <= $lo; 3112 $hi = $lo if $hi < $lo;
2880 3113
2881 $page = $hi - $lo if $page > $hi - $lo; 3114 $value = $hi - $page if $value > $hi - $page;
2882
2883 $value = $lo if $value < $lo; 3115 $value = $lo if $value < $lo;
2884 $value = $hi - $page if $value > $hi - $page;
2885 3116
2886 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3117 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2887 if $unit; 3118 if $unit;
2888 3119
2889 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3120 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2905 3136
2906 $self->SUPER::invoke_button_down ($ev, $x, $y); 3137 $self->SUPER::invoke_button_down ($ev, $x, $y);
2907 3138
2908 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3139 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2909 3140
2910 $self->invoke_mouse_motion ($ev, $x, $y) 3141 $self->invoke_mouse_motion ($ev, $x, $y);
3142
3143 1
2911} 3144}
2912 3145
2913sub invoke_mouse_motion { 3146sub invoke_mouse_motion {
2914 my ($self, $ev, $x, $y) = @_; 3147 my ($self, $ev, $x, $y) = @_;
2915 3148
2931sub invoke_mouse_wheel { 3164sub invoke_mouse_wheel {
2932 my ($self, $ev) = @_; 3165 my ($self, $ev) = @_;
2933 3166
2934 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3167 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2935 3168
2936 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3169 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2937 3170
2938 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3171 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2939 3172
2940 ! ! $delta 3173 1
2941} 3174}
2942 3175
2943sub update { 3176sub update {
2944 my ($self) = @_; 3177 my ($self) = @_;
2945 3178
2951 my ($self) = @_; 3184 my ($self) = @_;
2952 3185
2953 unless ($self->{knob_w}) { 3186 unless ($self->{knob_w}) {
2954 $self->set_value ($self->{range}[0]); 3187 $self->set_value ($self->{range}[0]);
2955 3188
2956 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3189 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2957 my $range = ($hi - $page - $lo) || 1e-100; 3190 my $range = ($hi - $page - $lo) || 1e-10;
2958 3191
2959 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3192 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
2960 3193
2961 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3194 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2962 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3195 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2963 3196
2964 $value = ($value - $lo) / $range; 3197 $value = ($value - $lo) / $range;
2994 glDisable GL_TEXTURE_2D; 3227 glDisable GL_TEXTURE_2D;
2995} 3228}
2996 3229
2997############################################################################# 3230#############################################################################
2998 3231
2999package CFPlus::UI::ValSlider; 3232package DC::UI::ValSlider;
3000 3233
3001our @ISA = CFPlus::UI::HBox::; 3234our @ISA = DC::UI::HBox::;
3002 3235
3003sub new { 3236sub new {
3004 my ($class, %arg) = @_; 3237 my ($class, %arg) = @_;
3005 3238
3006 my $range = delete $arg{range}; 3239 my $range = delete $arg{range};
3007 3240
3008 my $self = $class->SUPER::new ( 3241 my $self = $class->SUPER::new (
3009 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3242 slider => (new DC::UI::Slider expand => 1, range => $range),
3010 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3243 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3011 to_value => sub { shift }, 3244 to_value => sub { shift },
3012 from_value => sub { shift }, 3245 from_value => sub { shift },
3013 %arg, 3246 %arg,
3014 ); 3247 );
3015 3248
3035sub set_range { shift->{slider}->set_range (@_) } 3268sub set_range { shift->{slider}->set_range (@_) }
3036sub set_value { shift->{slider}->set_value (@_) } 3269sub set_value { shift->{slider}->set_value (@_) }
3037 3270
3038############################################################################# 3271#############################################################################
3039 3272
3040package CFPlus::UI::TextScroller; 3273package DC::UI::TextScroller;
3041 3274
3042our @ISA = CFPlus::UI::HBox::; 3275our @ISA = DC::UI::HBox::;
3043 3276
3044use CFPlus::OpenGL; 3277use DC::OpenGL;
3045 3278
3046sub new { 3279sub new {
3047 my $class = shift; 3280 my $class = shift;
3048 3281
3049 my $self = $class->SUPER::new ( 3282 my $self = $class->SUPER::new (
3051 can_events => 1, 3284 can_events => 1,
3052 indent => 0, 3285 indent => 0,
3053 #font => default_font 3286 #font => default_font
3054 @_, 3287 @_,
3055 3288
3056 layout => (new CFPlus::Layout), 3289 layout => (new DC::Layout),
3057 par => [], 3290 par => [],
3058 max_par => 0, 3291 max_par => 0,
3059 height => 0, 3292 height => 0,
3060 children => [ 3293 children => [
3061 (new CFPlus::UI::Empty expand => 1), 3294 (new DC::UI::Empty expand => 1),
3062 (new CFPlus::UI::Slider vertical => 1), 3295 (new DC::UI::Slider vertical => 1),
3063 ], 3296 ],
3064 ); 3297 );
3065 3298
3066 $self->{children}[1]->connect (changed => sub { $self->update }); 3299 $self->{children}[1]->connect (changed => sub { $self->update });
3067 3300
3076} 3309}
3077 3310
3078sub size_request { 3311sub size_request {
3079 my ($self) = @_; 3312 my ($self) = @_;
3080 3313
3081 my ($empty, $slider) = @{ $self->{children} }; 3314 my ($empty, $slider) = $self->visible_children;
3082 3315
3083 local $self->{children} = [$empty, $slider]; 3316 local $self->{children} = [$empty, $slider];
3084 $self->SUPER::size_request 3317 $self->SUPER::size_request
3085} 3318}
3086 3319
3123 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3356 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3124 $layout->set_markup ($para->{markup}); 3357 $layout->set_markup ($para->{markup});
3125 3358
3126 $layout->set_shapes ( 3359 $layout->set_shapes (
3127 map 3360 map
3128 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3361 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3129 @{$para->{widget}} 3362 @{$para->{widget}}
3130 ); 3363 );
3131 3364
3132 $layout 3365 $layout
3133} 3366}
3250 $ROOT->on_post_alloc ($self => sub { 3483 $ROOT->on_post_alloc ($self => sub {
3251 $self->force_uptodate; 3484 $self->force_uptodate;
3252 3485
3253 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3486 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3254 3487
3255 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3488 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3256 glClearColor 0, 0, 0, 0; 3489 glClearColor 0, 0, 0, 0;
3257 glClear GL_COLOR_BUFFER_BIT; 3490 glClear GL_COLOR_BUFFER_BIT;
3258 3491
3259 package CFPlus::UI::Base; 3492 package DC::UI::Base;
3260 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3493 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3261 (0, 0, $self->{w}, $self->{h}); 3494 (0, 0, $self->{w}, $self->{h});
3262 3495
3263 my $top = int $self->{children}[1]{range}[0]; 3496 my $top = int $self->{children}[1]{range}[0];
3264 3497
3275 3508
3276 if ($y0 < $y + $h && $y < $y1) { 3509 if ($y0 < $y + $h && $y < $y1) {
3277 my $layout = $self->get_layout ($para); 3510 my $layout = $self->get_layout ($para);
3278 3511
3279 $layout->render ($para->{indent}, $y - $y0); 3512 $layout->render ($para->{indent}, $y - $y0);
3513 $layout->draw;
3280 3514
3281 if (my @w = @{ $para->{widget} }) { 3515 if (my @w = @{ $para->{widget} }) {
3282 my @s = $layout->get_shapes; 3516 my @s = $layout->get_shapes;
3283 3517
3284 for (@w) { 3518 for (@w) {
3322 $self->{children}[1]->draw; 3556 $self->{children}[1]->draw;
3323} 3557}
3324 3558
3325############################################################################# 3559#############################################################################
3326 3560
3327package CFPlus::UI::Animator; 3561package DC::UI::Animator;
3328 3562
3329use CFPlus::OpenGL; 3563use DC::OpenGL;
3330 3564
3331our @ISA = CFPlus::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3332 3566
3333sub moveto { 3567sub moveto {
3334 my ($self, $x, $y) = @_; 3568 my ($self, $x, $y) = @_;
3335 3569
3336 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3570 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3364 glPopMatrix; 3598 glPopMatrix;
3365} 3599}
3366 3600
3367############################################################################# 3601#############################################################################
3368 3602
3369package CFPlus::UI::Flopper; 3603package DC::UI::Flopper;
3370 3604
3371our @ISA = CFPlus::UI::Button::; 3605our @ISA = DC::UI::Button::;
3372 3606
3373sub new { 3607sub new {
3374 my $class = shift; 3608 my $class = shift;
3375 3609
3376 my $self = $class->SUPER::new ( 3610 my $self = $class->SUPER::new (
3388 $self->{other}->toggle_visibility; 3622 $self->{other}->toggle_visibility;
3389} 3623}
3390 3624
3391############################################################################# 3625#############################################################################
3392 3626
3393package CFPlus::UI::Tooltip; 3627package DC::UI::Tooltip;
3394 3628
3395our @ISA = CFPlus::UI::Bin::; 3629our @ISA = DC::UI::Bin::;
3396 3630
3397use CFPlus::OpenGL; 3631use DC::OpenGL;
3398 3632
3399sub new { 3633sub new {
3400 my $class = shift; 3634 my $class = shift;
3401 3635
3402 $class->SUPER::new ( 3636 $class->SUPER::new (
3406} 3640}
3407 3641
3408sub set_tooltip_from { 3642sub set_tooltip_from {
3409 my ($self, $widget) = @_; 3643 my ($self, $widget) = @_;
3410 3644
3411 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3412 if $widget->{tooltip} =~ /^#(.*)$/;
3413
3414 my $tooltip = $widget->{tooltip}; 3645 my $tip = $widget->{tooltip};
3646 $tip = $tip->($widget) if "CODE" eq ref $tip;
3647
3648 $tip = DC::Pod::section_label tooltip => $1
3649 if $tip =~ /^#(.*)$/;
3415 3650
3416 if ($ENV{CFPLUS_DEBUG} & 2) { 3651 if ($ENV{CFPLUS_DEBUG} & 2) {
3417 $tooltip .= "\n\n" . (ref $widget) . "\n" 3652 $tip .= "\n\n" . (ref $widget) . "\n"
3418 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3653 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3419 . "req $widget->{req_w} $widget->{req_h}\n" 3654 . "req $widget->{req_w} $widget->{req_h}\n"
3420 . "visible $widget->{visible}"; 3655 . "visible $widget->{visible}";
3421 } 3656 }
3422 3657
3423 $tooltip =~ s/^\n+//; 3658 $tip =~ s/^\n+//;
3424 $tooltip =~ s/\n+$//; 3659 $tip =~ s/\n+$//;
3425 3660
3426 $self->add (new CFPlus::UI::Label 3661 $self->add (new DC::UI::Label
3662 fg => $DC::THEME{tooltip_fg},
3427 markup => $tooltip, 3663 markup => $tip,
3428 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3664 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3665 align => 0,
3429 fontsize => 0.8, 3666 fontsize => 0.8,
3430 style => 1, # FLAG_INVERSE 3667 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3431 ellipsise => 0, 3668 ellipsise => 0,
3432 font => ($widget->{tooltip_font} || $::FONT_PROP), 3669 font => ($widget->{tooltip_font} || $::FONT_PROP),
3433 ); 3670 );
3434} 3671}
3435 3672
3470} 3707}
3471 3708
3472sub _draw { 3709sub _draw {
3473 my ($self) = @_; 3710 my ($self) = @_;
3474 3711
3475 glTranslate 0.375, 0.375;
3476
3477 my ($w, $h) = @$self{qw(w h)}; 3712 my ($w, $h) = @$self{qw(w h)};
3478 3713
3479 glColor 1, 0.8, 0.4; 3714 glColor @{ $DC::THEME{tooltip_bg} };
3480 glBegin GL_QUADS; 3715 glRect 0, 0, $w, $h;
3481 glVertex 0 , 0;
3482 glVertex 0 , $h;
3483 glVertex $w, $h;
3484 glVertex $w, 0;
3485 glEnd;
3486 3716
3487 glColor 0, 0, 0; 3717 glColor @{ $DC::THEME{tooltip_border} };
3488 glBegin GL_LINE_LOOP; 3718 glRect_lineloop .5, .5, $w + .5, $h + .5;
3489 glVertex 0 , 0;
3490 glVertex 0 , $h;
3491 glVertex $w, $h;
3492 glVertex $w, 0;
3493 glEnd;
3494 3719
3495 glTranslate 2 - 0.375, 2 - 0.375; 3720 glTranslate 2, 2;
3496 3721
3497 $self->SUPER::_draw; 3722 $self->SUPER::_draw;
3498} 3723}
3499 3724
3500############################################################################# 3725#############################################################################
3501 3726
3502package CFPlus::UI::Face; 3727package DC::UI::Face;
3503 3728
3504our @ISA = CFPlus::UI::DrawBG::; 3729our @ISA = DC::UI::DrawBG::;
3505 3730
3506use CFPlus::OpenGL; 3731use DC::OpenGL;
3507 3732
3508sub new { 3733sub new {
3509 my $class = shift; 3734 my $class = shift;
3510 3735
3511 my $self = $class->SUPER::new ( 3736 my $self = $class->SUPER::new (
3514 aspect => 1, 3739 aspect => 1,
3515 can_events => 0, 3740 can_events => 0,
3516 @_, 3741 @_,
3517 ); 3742 );
3518 3743
3519 if ($self->{anim} && $self->{animspeed}) {
3520 CFPlus::weaken (my $widget = $self);
3521
3522 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3523 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3524 $self->{timer} = Event->timer (
3525 parked => 1,
3526 cb => sub {
3527 return unless $::CONN && $widget;
3528
3529 ++$widget->{frame};
3530 $widget->update_face;
3531 $widget->update;
3532
3533 $widget->update_timer;
3534 },
3535 );
3536
3537 $self->update_face;
3538 $self->update_timer; 3744 $self->update_anim;
3539 }
3540 3745
3541 $self 3746 $self
3542} 3747}
3543 3748
3544sub update_timer { 3749sub update_timer {
3545 my ($self) = @_; 3750 my ($self) = @_;
3546 3751
3547 return unless $self->{timer}; 3752 return unless $self->{timer};
3548 3753
3549 if ($self->{visible}) { 3754 if ($self->{visible}) {
3550 $self->{timer}->at (
3551 $self->{anim_start}
3552 + $self->{animspeed}
3553 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3554 );
3555 $self->{timer}->start; 3755 $self->{timer}->start;
3556 } else { 3756 } else {
3557 $self->{timer}->stop; 3757 $self->{timer}->stop;
3558 } 3758 }
3559} 3759}
3560 3760
3561sub update_face { 3761sub update_face {
3562 my ($self) = @_; 3762 my ($self) = @_;
3563 3763
3564 return unless $::CONN;
3565
3566 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3567 if ($anim && @$anim) {
3568 delete $self->{wait_face};
3569 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3570 }
3571 }
3572}
3573
3574sub size_request {
3575 my ($self) = @_;
3576
3577 if ($::CONN) { 3764 if ($::CONN) {
3578 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3579 if (my $tex = $::CONN->{texture}[$faceid]) { 3766 if ($anim && @$anim) {
3580 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3581 } else { 3768 delete $self->{face_change_cb};
3582 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3769
3583 $self->realloc; 3770 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3771 unless ($tex->{name} || $tex->{loading}) {
3772 $tex->upload (sub { $self->reconfigure });
3773 }
3584 }); 3774 }
3585 } 3775 }
3586 } 3776 }
3587 } 3777 }
3778}
3779
3780sub update_anim {
3781 my ($self) = @_;
3782
3783 if ($self->{anim} && $self->{animspeed}) {
3784 DC::weaken (my $widget = $self);
3785
3786 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3787 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3788 return unless $::CONN;
3789
3790 my $w = $widget
3791 or return;
3792
3793 ++$w->{frame};
3794 $w->update_face;
3795
3796 # somehow, $widget can go away
3797 $w->update;
3798 $w->update_timer;
3799 };
3800
3801 $self->update_face;
3802 $self->update_timer;
3803 } else {
3804 delete $self->{timer};
3805 }
3806}
3807
3808sub size_request {
3809 my ($self) = @_;
3810
3811 if ($::CONN) {
3812 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3813 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3814 if ($tex->{name}) {
3815 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3816 } elsif (!$tex->{loading}) {
3817 $tex->upload (sub { $self->reconfigure });
3818 }
3819 }
3820
3821 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3822 }
3823 }
3588 3824
3589 ($self->{size_w} || 8, $self->{size_h} || 8) 3825 ($self->{size_w} || 8, $self->{size_h} || 8)
3590} 3826}
3591 3827
3592sub update { 3828sub update {
3595 return unless $self->{visible}; 3831 return unless $self->{visible};
3596 3832
3597 $self->SUPER::update; 3833 $self->SUPER::update;
3598} 3834}
3599 3835
3836sub set_face {
3837 my ($self, $face) = @_;
3838
3839 $self->{face} = $face;
3840 $self->reconfigure;
3841}
3842
3843sub set_anim {
3844 my ($self, $anim) = @_;
3845
3846 $self->{anim} = $anim;
3847 $self->update_anim;
3848}
3849
3850sub set_animspeed {
3851 my ($self, $animspeed) = @_;
3852
3853 $self->{animspeed} = $animspeed;
3854 $self->update_anim;
3855}
3856
3600sub invoke_visibility_change { 3857sub invoke_visibility_change {
3601 my ($self) = @_; 3858 my ($self) = @_;
3602 3859
3603 $self->update_timer; 3860 $self->update_timer;
3604 3861
3606} 3863}
3607 3864
3608sub _draw { 3865sub _draw {
3609 my ($self) = @_; 3866 my ($self) = @_;
3610 3867
3611 return unless $::CONN;
3612
3613 $self->SUPER::_draw; 3868 $self->SUPER::_draw;
3614 3869
3615 my $faceid = $::CONN->{faceid}[$self->{face}] 3870 if (my $tex = $self->{tex}) {
3616 or return;
3617
3618 my $tex = $::CONN->{texture}[$faceid];
3619
3620 if ($tex) {
3621 glEnable GL_TEXTURE_2D; 3871 glEnable GL_TEXTURE_2D;
3622 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3623 glColor 0, 0, 0, 1; 3873 glColor 0, 0, 0, 1;
3624 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3874 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3625 glDisable GL_TEXTURE_2D; 3875 glDisable GL_TEXTURE_2D;
3635 $self->SUPER::destroy; 3885 $self->SUPER::destroy;
3636} 3886}
3637 3887
3638############################################################################# 3888#############################################################################
3639 3889
3640package CFPlus::UI::Buttonbar; 3890package DC::UI::Buttonbar;
3641 3891
3642our @ISA = CFPlus::UI::HBox::; 3892our @ISA = DC::UI::HBox::;
3643 3893
3644# TODO: should actually wrap buttons and other goodies. 3894# TODO: should actually wrap buttons and other goodies.
3645 3895
3646############################################################################# 3896#############################################################################
3647 3897
3648package CFPlus::UI::Menu; 3898package DC::UI::Menu;
3649 3899
3650our @ISA = CFPlus::UI::Toplevel::; 3900our @ISA = DC::UI::Toplevel::;
3651 3901
3652use CFPlus::OpenGL; 3902use DC::OpenGL;
3653 3903
3654sub new { 3904sub new {
3655 my $class = shift; 3905 my $class = shift;
3656 3906
3657 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3658 items => [], 3908 items => [],
3659 z => 100, 3909 z => 100,
3660 @_, 3910 @_,
3661 ); 3911 );
3662 3912
3663 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3913 $self->add ($self->{vbox} = new DC::UI::VBox);
3664 3914
3665 for my $item (@{ $self->{items} }) { 3915 for my $item (@{ $self->{items} }) {
3666 my ($widget, $cb, $tooltip) = @$item; 3916 my ($widget, $cb, $tooltip) = @$item;
3667 3917
3668 # handle various types of items, only text for now 3918 # handle various types of items, only text for now
3669 if (!ref $widget) { 3919 if (!ref $widget) {
3670 if ($widget =~ /\t/) { 3920 if ($widget =~ /\t/) {
3671 my ($left, $right) = split /\t/, $widget, 2; 3921 my ($left, $right) = split /\t/, $widget, 2;
3672 3922
3673 $widget = new CFPlus::UI::HBox 3923 $widget = new DC::UI::HBox
3674 can_hover => 1, 3924 can_hover => 1,
3675 can_events => 1, 3925 can_events => 1,
3676 tooltip => $tooltip, 3926 tooltip => $tooltip,
3677 children => [ 3927 children => [
3678 (new CFPlus::UI::Label markup => $left, expand => 1), 3928 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3679 (new CFPlus::UI::Label markup => $right, align => +1), 3929 (new DC::UI::Label markup => $right, align => 1),
3680 ], 3930 ],
3681 ; 3931 ;
3682 3932
3683 } else { 3933 } else {
3684 $widget = new CFPlus::UI::Label 3934 $widget = new DC::UI::Label
3685 can_hover => 1, 3935 can_hover => 1,
3686 can_events => 1, 3936 can_events => 1,
3937 align => 0,
3687 markup => $widget, 3938 markup => $widget,
3688 tooltip => $tooltip; 3939 tooltip => $tooltip;
3689 } 3940 }
3690 } 3941 }
3691 3942
3706 # maybe save $GRAB? must be careful about events... 3957 # maybe save $GRAB? must be careful about events...
3707 $GRAB = $self; 3958 $GRAB = $self;
3708 $self->{button} = $ev->{button}; 3959 $self->{button} = $ev->{button};
3709 3960
3710 $self->show; 3961 $self->show;
3711 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3962
3963 my $x = $ev->{x};
3964 my $y = $ev->{y};
3965
3966 $self->{root}->on_post_alloc ($self => sub {
3967 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3968 });
3969
3970 1 # so it can be used inside event handlers
3712} 3971}
3713 3972
3714sub invoke_mouse_motion { 3973sub invoke_mouse_motion {
3715 my ($self, $ev, $x, $y) = @_; 3974 my ($self, $ev, $x, $y) = @_;
3716 3975
3737 1 3996 1
3738} 3997}
3739 3998
3740############################################################################# 3999#############################################################################
3741 4000
3742package CFPlus::UI::Multiplexer; 4001package DC::UI::Multiplexer;
3743 4002
3744our @ISA = CFPlus::UI::Container::; 4003our @ISA = DC::UI::Container::;
3745 4004
3746sub new { 4005sub new {
3747 my $class = shift; 4006 my $class = shift;
3748 4007
3749 my $self = $class->SUPER::new ( 4008 my $self = $class->SUPER::new (
3750 @_, 4009 @_,
3751 ); 4010 );
3752 4011
3753 $self->{current} = $self->{children}[0] 4012 $self->set_current_page (0);
3754 if @{ $self->{children} };
3755 4013
3756 $self 4014 $self
3757} 4015}
3758 4016
3759sub add { 4017sub add {
3760 my ($self, @widgets) = @_; 4018 my ($self, @widgets) = @_;
3761 4019
3762 $self->SUPER::add (@widgets); 4020 $self->SUPER::add (@widgets);
3763 4021
3764 $self->{current} = $self->{children}[0] 4022 $self->set_current_page (0)
3765 if @{ $self->{children} }; 4023 if @widgets == @{ $self->{children} };
3766} 4024}
3767 4025
3768sub get_current_page { 4026sub get_current_page {
3769 my ($self) = @_; 4027 my ($self) = @_;
3770 4028
3776 4034
3777 my $widget = ref $page_or_widget 4035 my $widget = ref $page_or_widget
3778 ? $page_or_widget 4036 ? $page_or_widget
3779 : $self->{children}[$page_or_widget]; 4037 : $self->{children}[$page_or_widget];
3780 4038
4039 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4040
3781 $self->{current} = $widget; 4041 if (($self->{current} = $widget)) {
4042 $self->{current}->set_visible if $self->{current} && $self->{visible};
3782 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4043 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3783 4044
3784 $self->emit (page_changed => $self->{current}); 4045 $self->emit (page_changed => $self->{current});
4046 }
3785 4047
3786 $self->realloc; 4048 $self->realloc;
3787} 4049}
3788 4050
3789sub visible_children { 4051sub visible_children {
3790 $_[0]{current} 4052 $_[0]{current} || ()
3791} 4053}
3792 4054
3793sub size_request { 4055sub size_request {
3794 my ($self) = @_; 4056 my ($self) = @_;
3795 4057
4058 $self->{current}
3796 $self->{current}->size_request 4059 ? $self->{current}->size_request
4060 : (0, 0)
3797} 4061}
3798 4062
3799sub invoke_size_allocate { 4063sub invoke_size_allocate {
3800 my ($self, $w, $h) = @_; 4064 my ($self, $w, $h) = @_;
3801 4065
3802 $self->{current}->configure (0, 0, $w, $h); 4066 $self->{current}->configure (0, 0, $w, $h)
4067 if $self->{current};
3803 4068
3804 1 4069 1
3805} 4070}
3806 4071
3807sub _draw { 4072sub _draw {
3808 my ($self) = @_; 4073 my ($self) = @_;
3809 4074
3810 $self->{current}->draw; 4075 $self->{current}->draw
4076 if $self->{current};
3811} 4077}
3812 4078
3813############################################################################# 4079#############################################################################
3814 4080
3815package CFPlus::UI::Notebook; 4081package DC::UI::Notebook;
3816 4082
4083use DC::OpenGL;
4084
3817our @ISA = CFPlus::UI::VBox::; 4085our @ISA = DC::UI::VBox::;
3818 4086
3819sub new { 4087sub new {
3820 my $class = shift; 4088 my $class = shift;
3821 4089
3822 my $self = $class->SUPER::new ( 4090 my $self = $class->SUPER::new (
3823 buttonbar => (new CFPlus::UI::Buttonbar), 4091 buttonbar => (new DC::UI::Buttonbar),
3824 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4092 multiplexer => (new DC::UI::Multiplexer expand => 1),
4093 active_outline => [.7, .7, 0.2],
3825 # filter => # will be put between multiplexer and $self 4094 # filter => # will be put between multiplexer and $self
3826 @_, 4095 @_,
3827 ); 4096 );
3828 4097
3829 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4098 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3830 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4099 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3831 4100
4101 {
4102 Scalar::Util::weaken (my $wself = $self);
4103
4104 $self->{multiplexer}->connect (c_add => sub {
4105 my ($mplex, $widgets) = @_;
4106
4107 for my $child (@$widgets) {
4108 Scalar::Util::weaken $child;
4109 $child->{c_tab_} ||= do {
4110 my $tab =
4111 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4112 ? $child->{c_tab}
4113 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4114
4115 $tab->connect (activate => sub {
4116 $wself->set_current_page ($child);
4117 });
4118
4119 $tab
4120 };
4121
4122 $self->{buttonbar}->add ($child->{c_tab_});
4123 }
4124 });
4125
4126 $self->{multiplexer}->connect (c_remove => sub {
4127 my ($mplex, $widgets) = @_;
4128
4129 for my $child (@$widgets) {
4130 $wself->{buttonbar}->remove ($child->{c_tab_});
4131 }
4132 });
4133 }
4134
3832 $self 4135 $self
3833} 4136}
3834 4137
3835sub add { 4138sub add {
4139 my ($self, @widgets) = @_;
4140
4141 $self->{multiplexer}->add (@widgets)
4142}
4143
4144sub remove {
4145 my ($self, @widgets) = @_;
4146
4147 $self->{multiplexer}->remove (@widgets)
4148}
4149
4150sub pages {
4151 my ($self) = @_;
4152 $self->{multiplexer}->children
4153}
4154
4155sub page_index {
4156 my ($self, $widget) = @_;
4157
4158 my $i = 0;
4159 for ($self->pages) {
4160 if ($_ eq $widget) { return $i };
4161 $i++;
4162 }
4163
4164 undef
4165}
4166
4167sub add_tab {
3836 my ($self, $title, $widget, $tooltip) = @_; 4168 my ($self, $title, $widget, $tooltip) = @_;
3837 4169
3838 CFPlus::weaken $self; 4170 $title = [$title, $tooltip] unless ref $title;
4171 $widget->{c_tab} = $title;
3839 4172
3840 $self->{buttonbar}->add (new CFPlus::UI::Button
3841 markup => $title,
3842 tooltip => $tooltip,
3843 on_activate => sub { $self->set_current_page ($widget) },
3844 );
3845
3846 $self->{multiplexer}->add ($widget); 4173 $self->add ($widget);
3847} 4174}
3848 4175
3849sub get_current_page { 4176sub get_current_page {
3850 my ($self) = @_; 4177 my ($self) = @_;
3851 4178
3857 4184
3858 $self->{multiplexer}->set_current_page ($page); 4185 $self->{multiplexer}->set_current_page ($page);
3859 $self->emit (page_changed => $self->{multiplexer}{current}); 4186 $self->emit (page_changed => $self->{multiplexer}{current});
3860} 4187}
3861 4188
4189sub _draw {
4190 my ($self) = @_;
4191
4192 $self->SUPER::_draw ();
4193
4194 if (my $cur = $self->{multiplexer}{current}) {
4195 if ($cur = $cur->{c_tab_}) {
4196 glTranslate $self->{buttonbar}{x} + $cur->{x},
4197 $self->{buttonbar}{y} + $cur->{y};
4198 glLineWidth 3;
4199 #glEnable GL_BLEND;
4200 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4201 glColor @{$self->{active_outline}};
4202 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4203 glLineWidth 1;
4204 #glDisable GL_BLEND;
4205 }
4206 }
4207}
4208
3862############################################################################# 4209#############################################################################
3863 4210
3864package CFPlus::UI::Selector; 4211package DC::UI::Selector;
3865 4212
3866use utf8; 4213use utf8;
3867 4214
3868our @ISA = CFPlus::UI::Button::; 4215our @ISA = DC::UI::Button::;
3869 4216
3870sub new { 4217sub new {
3871 my $class = shift; 4218 my $class = shift;
3872 4219
3873 my $self = $class->SUPER::new ( 4220 my $self = $class->SUPER::new (
3890 my ($value, $title, $tooltip) = @$_; 4237 my ($value, $title, $tooltip) = @$_;
3891 4238
3892 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4239 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3893 } 4240 }
3894 4241
3895 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4242 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3896} 4243}
3897 4244
3898sub _set_value { 4245sub _set_value {
3899 my ($self, $value) = @_; 4246 my ($self, $value) = @_;
3900 4247
3901 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4248 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4249 $item ||= $self->{options}[0]
3902 or return; 4250 or return;
3903 4251
3904 $self->{value} = $item->[0]; 4252 $self->{value} = $item->[0];
3905 $self->set_markup ("$item->[1] ⇓"); 4253 $self->set_markup ("$item->[1] ⇓");
3906 $self->set_tooltip ($item->[2]); 4254# $self->set_tooltip ($item->[2]);
3907} 4255}
3908 4256
3909sub set_value { 4257sub set_value {
3910 my ($self, $value) = @_; 4258 my ($self, $value) = @_;
3911 4259
3913 4261
3914 $self->_set_value ($value); 4262 $self->_set_value ($value);
3915 $self->emit (changed => $value); 4263 $self->emit (changed => $value);
3916} 4264}
3917 4265
4266sub set_options {
4267 my ($self, $options) = @_;
4268
4269 $self->{options} = $options;
4270 $self->_set_value ($self->{value});
4271}
4272
3918############################################################################# 4273#############################################################################
3919 4274
3920package CFPlus::UI::Statusbox; 4275package DC::UI::Statusbox;
3921 4276
3922our @ISA = CFPlus::UI::VBox::; 4277our @ISA = DC::UI::VBox::;
3923 4278
3924sub new { 4279sub new {
3925 my $class = shift; 4280 my $class = shift;
3926 4281
3927 my $self = $class->SUPER::new ( 4282 my $self = $class->SUPER::new (
3928 fontsize => 0.8, 4283 fontsize => 0.8,
3929 @_, 4284 @_,
3930 ); 4285 );
3931 4286
3932 CFPlus::weaken (my $this = $self); 4287 DC::weaken (my $this = $self);
3933 4288
3934 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4289 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3935 4290
3936 $self 4291 $self
3937} 4292}
3938 4293
3939sub reorder { 4294sub reorder {
3940 my ($self) = @_; 4295 my ($self) = @_;
3941 my $NOW = Time::HiRes::time; 4296 my $NOW = EV::time;
3942 4297
3943 # freeze display when hovering over any label 4298 # freeze display when hovering over any label
3944 return if $CFPlus::UI::TOOLTIP->{owner} 4299 return if $DC::UI::TOOLTIP->{owner}
3945 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4300 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3946 values %{ $self->{item} }; 4301 values %{ $self->{item} };
3947 4302
3948 while (my ($k, $v) = each %{ $self->{item} }) { 4303 while (my ($k, $v) = each %{ $self->{item} }) {
3949 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4304 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3950 } 4305 }
4306
4307 $self->{timer}->set (1, 1);
3951 4308
3952 my @widgets; 4309 my @widgets;
3953 4310
3954 my @items = sort { 4311 my @items = sort {
3955 $a->{pri} <=> $b->{pri} 4312 $a->{pri} <=> $b->{pri}
3956 or $b->{id} <=> $a->{id} 4313 or $b->{id} <=> $a->{id}
3957 } values %{ $self->{item} }; 4314 } values %{ $self->{item} };
3958
3959 $self->{timer}->interval (1);
3960 4315
3961 my $count = 10 + 1; 4316 my $count = 10 + 1;
3962 for my $item (@items) { 4317 for my $item (@items) {
3963 last unless --$count; 4318 last unless --$count;
3964 4319
3971 for ($short) { 4326 for ($short) {
3972 s/^\s+//; 4327 s/^\s+//;
3973 s/\s+/ /g; 4328 s/\s+/ /g;
3974 } 4329 }
3975 4330
3976 new CFPlus::UI::Label 4331 new DC::UI::Label
3977 markup => $short, 4332 markup => $short,
3978 tooltip => $item->{tooltip}, 4333 tooltip => $item->{tooltip},
3979 tooltip_font => $::FONT_PROP, 4334 tooltip_font => $::FONT_PROP,
3980 tooltip_width => 0.67, 4335 tooltip_width => 0.67,
3981 fontsize => $item->{fontsize} || $self->{fontsize}, 4336 fontsize => $item->{fontsize} || $self->{fontsize},
3982 max_w => $::WIDTH * 0.44, 4337 max_w => $::WIDTH * 0.44,
4338 align => 0,
3983 fg => [@{ $item->{fg} }], 4339 fg => [@{ $item->{fg} }],
3984 can_events => 1, 4340 can_events => 1,
3985 can_hover => 1 4341 can_hover => 1
3986 }; 4342 };
3987 4343
3988 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4344 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3989 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4345 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3990 $label->update; 4346 $label->update;
3991 $label->set_max_size (undef, $label->{req_h} * $diff) 4347 $label->set_max_size (undef, $label->{req_h} * $diff)
3992 if $diff < 1; 4348 if $diff < 1;
3993 $self->{timer}->interval (1/30); 4349 $self->{timer}->set (1/30, 1/30);
3994 } else { 4350 } else {
3995 $label->{fg}[3] = $item->{fg}[3] || 1; 4351 $label->{fg}[3] = $item->{fg}[3] || 1;
3996 } 4352 }
3997 4353
3998 push @widgets, $label; 4354 push @widgets, $label;
3999 } 4355 }
4356
4357 my $hash = join ",", @widgets;
4358 return if $hash eq $self->{last_widget_hash};
4359 $self->{last_widget_hash} = $hash;
4000 4360
4001 $self->clear; 4361 $self->clear;
4002 $self->SUPER::add (reverse @widgets); 4362 $self->SUPER::add (reverse @widgets);
4003} 4363}
4004 4364
4060 $self->SUPER::destroy; 4420 $self->SUPER::destroy;
4061} 4421}
4062 4422
4063############################################################################# 4423#############################################################################
4064 4424
4065package CFPlus::UI::Root; 4425package DC::UI::Root;
4066 4426
4067our @ISA = CFPlus::UI::Container::; 4427our @ISA = DC::UI::Container::;
4068 4428
4069use List::Util qw(min max); 4429use List::Util qw(min max);
4070 4430
4071use CFPlus::OpenGL; 4431use DC::OpenGL;
4072 4432
4073sub new { 4433sub new {
4074 my $class = shift; 4434 my $class = shift;
4075 4435
4076 my $self = $class->SUPER::new ( 4436 my $self = $class->SUPER::new (
4077 visible => 1, 4437 visible => 1,
4078 @_, 4438 @_,
4079 ); 4439 );
4080 4440
4081 CFPlus::weaken ($self->{root} = $self); 4441 DC::weaken ($self->{root} = $self);
4082 4442
4083 $self 4443 $self
4084} 4444}
4085 4445
4086sub size_request { 4446sub size_request {
4134} 4494}
4135 4495
4136sub update { 4496sub update {
4137 my ($self) = @_; 4497 my ($self) = @_;
4138 4498
4139 $::WANT_REFRESH++; 4499 $::WANT_REFRESH = 1;
4140} 4500}
4141 4501
4142sub add { 4502sub add {
4143 my ($self, @children) = @_; 4503 my ($self, @children) = @_;
4144 4504
4181 while ($self->{refresh_hook}) { 4541 while ($self->{refresh_hook}) {
4182 $_->() 4542 $_->()
4183 for values %{delete $self->{refresh_hook}}; 4543 for values %{delete $self->{refresh_hook}};
4184 } 4544 }
4185 4545
4186 if ($self->{realloc}) { 4546 while ($self->{realloc}) {
4187 my %queue; 4547 my %queue;
4188 my @queue; 4548 my @queue;
4189 my $widget; 4549 my $widget;
4190 4550
4191 outer: 4551 outer:
4211 4571
4212 delete $queue{$widget+0}; 4572 delete $queue{$widget+0};
4213 4573
4214 my ($w, $h) = $widget->size_request; 4574 my ($w, $h) = $widget->size_request;
4215 4575
4216 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4576 $w += $widget->{padding_x} * 2;
4217 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4577 $h += $widget->{padding_y} * 2;
4578
4579 $w = max $widget->{min_w}, $w;
4580 $h = max $widget->{min_h}, $h;
4218 4581
4219 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4582 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4220 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4583 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4221 4584
4222 $w = $widget->{force_w} if exists $widget->{force_w}; 4585 $w = $widget->{force_w} if exists $widget->{force_w};
4238 } 4601 }
4239 } 4602 }
4240 4603
4241 delete $self->{realloc}{$widget+0}; 4604 delete $self->{realloc}{$widget+0};
4242 } 4605 }
4243 }
4244 4606
4245 while (my $size_alloc = delete $self->{size_alloc}) { 4607 while (my $size_alloc = delete $self->{size_alloc}) {
4246 my @queue = sort { $b->{visible} <=> $a->{visible} } 4608 my @queue = sort { $a->{visible} <=> $b->{visible} }
4247 values %$size_alloc; 4609 values %$size_alloc;
4248 4610
4249 while () { 4611 while () {
4250 my $widget = pop @queue || last; 4612 my $widget = pop @queue || last;
4251 4613
4252 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4614 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4253 4615
4254 $w = 0 if $w < 0;
4255 $h = 0 if $h < 0;
4256
4257 $w = max $widget->{min_w}, $w; 4616 $w = max $widget->{min_w}, $w;
4258 $h = max $widget->{min_h}, $h; 4617 $h = max $widget->{min_h}, $h;
4259 4618
4260# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4619# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4261# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4620# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4262 4621
4263 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4622 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4264 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4623 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4265 4624
4266 $w = int $w + 0.5; 4625 $w = int $w + 0.5;
4267 $h = int $h + 0.5; 4626 $h = int $h + 0.5;
4268 4627
4269 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4628 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4270 $widget->{old_w} = $widget->{w}; 4629 $widget->{old_w} = $widget->{w};
4271 $widget->{old_h} = $widget->{h}; 4630 $widget->{old_h} = $widget->{h};
4272 4631
4273 $widget->{w} = $w; 4632 $widget->{w} = $w;
4274 $widget->{h} = $h; 4633 $widget->{h} = $h;
4275 4634
4276 $widget->emit (size_allocate => $w, $h); 4635 $widget->emit (size_allocate => $w, $h);
4636 }
4277 } 4637 }
4278 } 4638 }
4279 } 4639 }
4280 4640
4281 while ($self->{post_alloc_hook}) { 4641 while ($self->{post_alloc_hook}) {
4292 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4652 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4293 glMatrixMode GL_MODELVIEW; 4653 glMatrixMode GL_MODELVIEW;
4294 glLoadIdentity; 4654 glLoadIdentity;
4295 4655
4296 { 4656 {
4297 package CFPlus::UI::Base; 4657 package DC::UI::Base;
4298 4658
4299 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4659 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4300 (0, 0, $self->{w}, $self->{h}); 4660 (0, 0, $self->{w}, $self->{h});
4301 4661
4302 $self->_draw; 4662 $self->_draw;
4303 } 4663 }
4304} 4664}
4305 4665
4306############################################################################# 4666#############################################################################
4307 4667
4308package CFPlus::UI; 4668package DC::UI;
4309 4669
4310$ROOT = new CFPlus::UI::Root; 4670$ROOT = new DC::UI::Root;
4311$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4671$TOOLTIP = new DC::UI::Tooltip z => 900;
4312 4672
43131 46731
4314

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines