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.392 by root, Sat Jul 21 13:37:09 2007 UTC vs.
Revision 1.473 by root, Sun Jan 11 03:19:47 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 33
33 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
34 35
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 37 $TOOLTIP->show;
41 } 38 }
42 39
43 return; 40 return;
46 } 43 }
47 44
48 $TOOLTIP->hide; 45 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
51}); 48};
52 49
53sub get_layout { 50sub get_layout {
54 my $layout; 51 my $layout;
55 52
56 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
92 89
93 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 92
96 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
97 } 94 }
98} 95}
99 96
100sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
101 my ($ev) = @_; 98 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
108 105
109 $GRAB = $widget; 106 $GRAB = $widget;
110 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
111 108
112 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
113 } 110 }
114 111
115 if ($GRAB) { 112 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 114 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 117
121 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
123 120
124 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 140 $grab->update if $grab;
144 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
145 142
146 check_hover $widget; 143 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
152 my ($ev) = @_; 149 my ($ev) = @_;
192# call when resolution changes etc. 189# call when resolution changes etc.
193sub rescale_widgets { 190sub rescale_widgets {
194 my ($sx, $sy) = @_; 191 my ($sx, $sy) = @_;
195 192
196 for my $widget (values %WIDGET) { 193 for my $widget (values %WIDGET) {
197 if ($widget->{is_toplevel}) { 194 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
198 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
200 197
201 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
202 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
214 reconfigure_widgets; 211 reconfigure_widgets;
215} 212}
216 213
217############################################################################# 214#############################################################################
218 215
219package CFPlus::UI::Event; 216package DC::UI::Event;
220 217
221sub xy { 218sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 220}
224 221
225############################################################################# 222#############################################################################
226 223
227package CFPlus::UI::Base; 224package DC::UI::Base;
228 225
229use strict; 226use strict;
230 227
231use CFPlus::OpenGL; 228use DC::OpenGL;
232 229
233sub new { 230sub new {
234 my $class = shift; 231 my $class = shift;
235 232
236 my $self = bless { 233 my $self = bless {
241 h => undef, 238 h => undef,
242 can_events => 1, 239 can_events => 1,
243 @_ 240 @_
244 }, $class; 241 }, $class;
245 242
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 244
248 for (keys %$self) { 245 for (keys %$self) {
249 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
251 } 248 }
252 } 249 }
253 250
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
256 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
257 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
258 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
259 256
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 259
263 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
273 $self->emit ("destroy"); 270 $self->emit ("destroy");
274 %$self = (); 271 %$self = ();
275} 272}
276 273
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub show {
282 my ($self) = @_; 279 my ($self) = @_;
283 280
284 return if $self->{parent}; 281 return if $self->{parent};
285 282
286 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
287} 284}
288 285
289sub set_visible { 286sub set_visible {
290 my ($self) = @_; 287 my ($self) = @_;
291 288
292 return if $self->{visible}; 289 return if $self->{visible};
290
291 $self->{parent} && $self->{parent}{root}#d#
292 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
293 293
294 $self->{root} = $self->{parent}{root}; 294 $self->{root} = $self->{parent}{root};
295 $self->{visible} = $self->{parent}{visible} + 1; 295 $self->{visible} = $self->{parent}{visible} + 1;
296 296
297 $self->emit (visibility_change => 1); 297 $self->emit (visibility_change => 1);
298 298
299 $self->realloc if !exists $self->{req_w}; 299 $self->realloc if !exists $self->{req_w};
300 300
301 $_->set_visible for $self->children; 301 $_->set_visible for $self->visible_children;
302} 302}
303 303
304sub set_invisible { 304sub set_invisible {
305 my ($self) = @_; 305 my ($self) = @_;
306 306
312 delete $self->{root}; 312 delete $self->{root};
313 313
314 undef $GRAB if $GRAB == $self; 314 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 315 undef $HOVER if $HOVER == $self;
316 316
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 317 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 318 if $TOOLTIP->{owner} == $self;
319 319
320 $self->emit ("focus_out"); 320 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 321 $self->emit (visibility_change => 0);
322} 322}
341sub hide { 341sub hide {
342 my ($self) = @_; 342 my ($self) = @_;
343 343
344 $self->set_invisible; 344 $self->set_invisible;
345 345
346 # extra $parent copy for 5.8.8+ bug workaround
347 # (otherwise $_[0] in remove gets freed
348 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 349 $parent->remove ($self);
347 if $self->{parent}; 350 }
348} 351}
349 352
350sub move_abs { 353sub move_abs {
351 my ($self, $x, $y, $z) = @_; 354 my ($self, $x, $y, $z) = @_;
352 355
364 $self->{force_h} = $h; 367 $self->{force_h} = $h;
365 368
366 $self->realloc; 369 $self->realloc;
367} 370}
368 371
372# traverse the widget chain up to find the maximum "physical" size constraints
373sub get_max_wh {
374 my ($self) = @_;
375
376 my ($w, $h) = @$self{qw(max_w max_h)};
377
378 if ($w <= 0 || $h <= 0) {
379 my ($mw, $mh) = $self->{parent}
380 ? $self->{parent}->get_max_wh
381 : ($::WIDTH, $::HEIGHT);
382
383 $w = $mw if $w <= 0;
384 $h = $mh if $h <= 0;
385 }
386
387 ($w, $h)
388}
389
369sub size_request { 390sub size_request {
370 require Carp; 391 require Carp;
371 Carp::confess "size_request is abstract"; 392 Carp::confess "size_request is abstract";
372} 393}
373 394
379 my ($self, $x, $y, $w, $h) = @_; 400 my ($self, $x, $y, $w, $h) = @_;
380 401
381 if ($self->{aspect}) { 402 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 403 my ($ow, $oh) = ($w, $h);
383 404
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 405 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 406 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 407
387 # use alignment to adjust x, y 408 # use alignment to adjust x, y
388 409
389 $x += int 0.5 * ($ow - $w); 410 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 411 $y += int 0.5 * ($oh - $h);
431 452
432 return if $self->{tooltip} eq $tooltip; 453 return if $self->{tooltip} eq $tooltip;
433 454
434 $self->{tooltip} = $tooltip; 455 $self->{tooltip} = $tooltip;
435 456
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 457 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 458 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 459 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 460 }
440} 461}
441 462
442# translate global coordinates to local coordinate system 463# translate global coordinates to local coordinate system
443sub coord2local { 464sub coord2local {
444 my ($self, $x, $y) = @_; 465 my ($self, $x, $y) = @_;
445 466
446 Carp::confess unless $self->{parent};#d# 467 return (undef, undef) unless $self->{parent};
447 468
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 469 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 470}
450 471
451# translate local coordinates to global coordinate system 472# translate local coordinates to global coordinate system
452sub coord2global { 473sub coord2global {
453 my ($self, $x, $y) = @_; 474 my ($self, $x, $y) = @_;
454 475
455 Carp::confess unless $self->{parent};#d# 476 return (undef, undef) unless $self->{parent};
456 477
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 478 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 479}
459 480
460sub invoke_focus_in { 481sub invoke_focus_in {
509sub connect { 530sub connect {
510 my ($self, $signal, $cb) = @_; 531 my ($self, $signal, $cb) = @_;
511 532
512 push @{ $self->{signal_cb}{$signal} }, $cb; 533 push @{ $self->{signal_cb}{$signal} }, $cb;
513 534
514 defined wantarray and CFPlus::guard { 535 defined wantarray and DC::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 537 @{ $self->{signal_cb}{$signal} };
517 } 538 }
518} 539}
519 540
557# in .xs 578# in .xs
558 579
559sub set_parent { 580sub set_parent {
560 my ($self, $parent) = @_; 581 my ($self, $parent) = @_;
561 582
562 CFPlus::weaken ($self->{parent} = $parent); 583 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 584 $self->set_visible if $parent->{visible};
564} 585}
565 586
566sub realloc { 587sub realloc {
567 my ($self) = @_; 588 my ($self) = @_;
593 614
594# using global variables seems a bit hacky, but passing through all drawing 615# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 616# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 617our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 618
598sub draw { 619#sub draw {
599 my ($self) = @_; 620#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 621
645sub _draw { 622sub _draw {
646 my ($self) = @_; 623 my ($self) = @_;
647 624
648 warn "no draw defined for $self\n"; 625 warn "no draw defined for $self\n";
649} 626}
650 627
651my $cntx;#d#
652sub DESTROY { 628sub DESTROY {
653 my ($self) = @_; 629 my ($self) = @_;
654 630
655 return if CFPlus::in_destruct; 631 return if DC::in_destruct;
656 632
633 local $@;
657 eval { $self->destroy }; 634 eval { $self->destroy };
658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 635 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659 636
660 delete $WIDGET{$self+0}; 637 delete $WIDGET{$self+0};
661} 638}
662 639
663############################################################################# 640#############################################################################
664 641
665package CFPlus::UI::DrawBG; 642package DC::UI::DrawBG;
666 643
667our @ISA = CFPlus::UI::Base::; 644our @ISA = DC::UI::Base::;
668 645
669use strict; 646use strict;
670use CFPlus::OpenGL; 647use DC::OpenGL;
671 648
672sub new { 649sub new {
673 my $class = shift; 650 my $class = shift;
674
675 # range [value, low, high, page]
676 651
677 $class->SUPER::new ( 652 $class->SUPER::new (
678 #bg => [0, 0, 0, 0.2], 653 #bg => [0, 0, 0, 0.2],
679 #active_bg => [1, 1, 1, 0.5], 654 #active_bg => [1, 1, 1, 0.5],
680 @_ 655 @_
692 my ($w, $h) = @$self{qw(w h)}; 667 my ($w, $h) = @$self{qw(w h)};
693 668
694 glEnable GL_BLEND; 669 glEnable GL_BLEND;
695 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 670 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
696 glColor_premultiply @$color; 671 glColor_premultiply @$color;
697
698 glBegin GL_QUADS;
699 glVertex 0 , 0;
700 glVertex 0 , $h;
701 glVertex $w, $h; 672 glRect 0, 0, $w, $h;
702 glVertex $w, 0;
703 glEnd;
704
705 glDisable GL_BLEND; 673 glDisable GL_BLEND;
706 } 674 }
707} 675}
708 676
709############################################################################# 677#############################################################################
710 678
711package CFPlus::UI::Empty; 679package DC::UI::Empty;
712 680
713our @ISA = CFPlus::UI::Base::; 681our @ISA = DC::UI::Base::;
714 682
715sub new { 683sub new {
716 my ($class, %arg) = @_; 684 my ($class, %arg) = @_;
717 $class->SUPER::new (can_events => 0, %arg); 685 $class->SUPER::new (can_events => 0, %arg);
718} 686}
725 693
726sub draw { } 694sub draw { }
727 695
728############################################################################# 696#############################################################################
729 697
730package CFPlus::UI::Container; 698package DC::UI::Container;
731 699
732our @ISA = CFPlus::UI::Base::; 700our @ISA = DC::UI::Base::;
733 701
734sub new { 702sub new {
735 my ($class, %arg) = @_; 703 my ($class, %arg) = @_;
736 704
737 my $children = delete $arg{children}; 705 my $children = delete $arg{children};
769 sort { $a->{z} <=> $b->{z} } 737 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 738 @{$self->{children}}, @widgets
771 ]; 739 ];
772 740
773 $self->realloc; 741 $self->realloc;
742
743 $self->emit (c_add => \@widgets);
744
745 map $_+0, @widgets
774} 746}
775 747
776sub children { 748sub children {
777 @{ $_[0]{children} } 749 @{ $_[0]{children} }
778} 750}
779 751
780sub remove { 752sub remove {
781 my ($self, $child) = @_; 753 my ($self, @widgets) = @_;
782 754
755 $self->emit (c_remove => \@widgets);
756
757 for my $child (@widgets) {
783 delete $child->{parent}; 758 delete $child->{parent};
784 $child->hide; 759 $child->hide;
785
786 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 760 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
761 }
787 762
788 $self->realloc; 763 $self->realloc;
789} 764}
790 765
791sub clear { 766sub clear {
792 my ($self) = @_; 767 my ($self) = @_;
793 768
794 my $children = delete $self->{children}; 769 my $children = $self->{children};
795 $self->{children} = []; 770 $self->{children} = [];
796 771
797 for (@$children) { 772 for (@$children) {
798 delete $_->{parent}; 773 delete $_->{parent};
799 $_->hide; 774 $_->hide;
819} 794}
820 795
821sub _draw { 796sub _draw {
822 my ($self) = @_; 797 my ($self) = @_;
823 798
824 $_->draw for @{$self->{children}}; 799 $_->draw for $self->visible_children;
825} 800}
826 801
827############################################################################# 802#############################################################################
828 803
829package CFPlus::UI::Bin; 804package DC::UI::Bin;
830 805
831our @ISA = CFPlus::UI::Container::; 806our @ISA = DC::UI::Container::;
832 807
833sub new { 808sub new {
834 my ($class, %arg) = @_; 809 my ($class, %arg) = @_;
835 810
836 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 811 my $child = (delete $arg{child}) || new DC::UI::Empty::;
837 812
838 $class->SUPER::new (children => [$child], %arg) 813 $class->SUPER::new (children => [$child], %arg)
839} 814}
840 815
841sub add { 816sub add {
842 my ($self, $child) = @_; 817 my ($self, $child) = @_;
843 818
844 $self->SUPER::remove ($_) for @{ $self->{children} }; 819 $self->clear;
845 $self->SUPER::add ($child); 820 $self->SUPER::add ($child);
846} 821}
847 822
848sub remove { 823sub remove {
849 my ($self, $widget) = @_; 824 my ($self, $widget) = @_;
850 825
851 $self->SUPER::remove ($widget); 826 $self->SUPER::remove ($widget);
852 827
853 $self->{children} = [new CFPlus::UI::Empty] 828 $self->{children} = [new DC::UI::Empty]
854 unless @{$self->{children}}; 829 unless @{$self->{children}};
855} 830}
856 831
857sub child { $_[0]->{children}[0] } 832sub child { $_[0]->{children}[0] }
858 833
867 842
868 1 843 1
869} 844}
870 845
871############################################################################# 846#############################################################################
872
873# back-buffered drawing area 847# back-buffered drawing area
874 848
875package CFPlus::UI::Window; 849package DC::UI::Window;
876 850
877our @ISA = CFPlus::UI::Bin::; 851our @ISA = DC::UI::Bin::;
878 852
879use CFPlus::OpenGL; 853use DC::OpenGL;
880 854
881sub new { 855sub new {
882 my ($class, %arg) = @_; 856 my ($class, %arg) = @_;
883 857
884 my $self = $class->SUPER::new (%arg); 858 my $self = $class->SUPER::new (%arg);
906} 880}
907 881
908sub render_child { 882sub render_child {
909 my ($self) = @_; 883 my ($self) = @_;
910 884
911 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 885 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
912 glClearColor 0, 0, 0, 0; 886 glClearColor 0, 0, 0, 0;
913 glClear GL_COLOR_BUFFER_BIT; 887 glClear GL_COLOR_BUFFER_BIT;
914 888
915 { 889 {
916 package CFPlus::UI::Base; 890 package DC::UI::Base;
917 891
918 local ($draw_x, $draw_y, $draw_w, $draw_h) = 892 local ($draw_x, $draw_y, $draw_w, $draw_h) =
919 (0, 0, $self->{w}, $self->{h}); 893 (0, 0, $self->{w}, $self->{h});
920 894
921 $self->_render; 895 $self->_render;
938 glDisable GL_TEXTURE_2D; 912 glDisable GL_TEXTURE_2D;
939} 913}
940 914
941############################################################################# 915#############################################################################
942 916
943package CFPlus::UI::ViewPort; 917package DC::UI::ViewPort;
944 918
945use List::Util qw(min max); 919use List::Util qw(min max);
946 920
947our @ISA = CFPlus::UI::Window::; 921our @ISA = DC::UI::Window::;
948 922
949sub new { 923sub new {
950 my $class = shift; 924 my $class = shift;
951 925
952 $class->SUPER::new ( 926 $class->SUPER::new (
994 $self->emit (changed => $x, $y); 968 $self->emit (changed => $x, $y);
995 $self->update; 969 $self->update;
996 } 970 }
997} 971}
998 972
973sub set_center {
974 my ($self, $x, $y) = @_;
975
976 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
977}
978
979sub make_visible {
980 my ($self, $x, $y, $border) = @_;
981
982 if ( $x < $self->{view_x} + $self->{w} * $border
983 || $x > $self->{view_x} + $self->{w} * (1 - $border)
984 || $y < $self->{view_y} + $self->{h} * $border
985 || $y > $self->{view_y} + $self->{h} * (1 - $border)
986 ) {
987 $self->set_center ($x, $y);
988 }
989}
990
999# hmm, this does not work for topleft of $self... but we should not ask for that 991# hmm, this does not work for topleft of $self... but we should not ask for that
1000sub coord2local { 992sub coord2local {
1001 my ($self, $x, $y) = @_; 993 my ($self, $x, $y) = @_;
1002 994
1003 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) 995 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1020 ) { 1012 ) {
1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1013 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1022 } else { 1014 } else {
1023 $self->CFPlus::UI::Base::find_widget ($x, $y) 1015 $self->DC::UI::Base::find_widget ($x, $y)
1024 } 1016 }
1025} 1017}
1026 1018
1027sub _render { 1019sub _render {
1028 my ($self) = @_; 1020 my ($self) = @_;
1029 1021
1030 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1022 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1031 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1023 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1032 1024
1033 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1025 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1034 1026
1035 $self->SUPER::_render; 1027 $self->SUPER::_render;
1036} 1028}
1037 1029
1038############################################################################# 1030#############################################################################
1039 1031
1040package CFPlus::UI::ScrolledWindow; 1032package DC::UI::ScrolledWindow;
1041 1033
1042our @ISA = CFPlus::UI::Table::; 1034our @ISA = DC::UI::Table::;
1043 1035
1044sub new { 1036sub new {
1045 my ($class, %arg) = @_; 1037 my ($class, %arg) = @_;
1046 1038
1047 my $child = delete $arg{child}; 1039 my $child = delete $arg{child};
1048 1040
1049 my $self; 1041 my $self;
1050 1042
1051 my $hslider = new CFPlus::UI::Slider 1043 my $hslider = new DC::UI::Slider
1052 col => 0, 1044 c_col => 0,
1053 row => 1, 1045 c_row => 1,
1054 vertical => 0, 1046 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub { 1048 on_changed => sub {
1057 $self->{hpos} = $_[1]; 1049 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 }, 1051 },
1060 ; 1052 ;
1061 1053
1062 my $vslider = new CFPlus::UI::Slider 1054 my $vslider = new DC::UI::Slider
1063 col => 1, 1055 c_col => 1,
1064 row => 0, 1056 c_row => 0,
1065 vertical => 1, 1057 vertical => 1,
1066 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1067 on_changed => sub { 1059 on_changed => sub {
1068 $self->{vpos} = $_[1]; 1060 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1079 col_expand => [1, 0], 1071 col_expand => [1, 0],
1080 row_expand => [1, 0], 1072 row_expand => [1, 0],
1081 %arg, 1073 %arg,
1082 ); 1074 );
1083 1075
1084 $self->{vp} = new CFPlus::UI::ViewPort 1076 $self->{vp} = new DC::UI::ViewPort
1085 col => 0, 1077 c_col => 0,
1086 row => 0, 1078 c_row => 0,
1087 expand => 1, 1079 expand => 1,
1088 scroll_x => $self->{scroll_x}, 1080 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y}, 1081 scroll_y => $self->{scroll_y},
1090 on_changed => sub { 1082 on_changed => sub {
1091 my ($vp, $x, $y) = @_; 1083 my ($vp, $x, $y) = @_;
1093 $vp->{parent}{hslider}->set_value ($x); 1085 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y); 1086 $vp->{parent}{vslider}->set_value ($y);
1095 1087
1096 0 1088 0
1097 }, 1089 },
1090 on_size_allocate => sub {
1091 my ($vp, $w, $h) = @_;
1092 $vp->{parent}->update_slider;
1093 0
1094 },
1098 ; 1095 ;
1099 1096
1100 $self->SUPER::add ($self->{vp}); 1097 $self->SUPER::add ($self->{vp});
1101 1098
1102 $self->add ($child) if $child; 1099 $self->add ($child) if $child;
1103 1100
1104 $self 1101 $self
1105} 1102}
1106
1107#TODO# update range on size_allocate depending on child?
1108 1103
1109sub add { 1104sub add {
1110 my ($self, $widget) = @_; 1105 my ($self, $widget) = @_;
1111 1106
1112 $self->{vp}->add ($self->{child} = $widget); 1107 $self->{vp}->add ($self->{child} = $widget);
1113} 1108}
1114 1109
1110sub set_offset { shift->{vp}->set_offset (@_) }
1111sub set_center { shift->{vp}->set_center (@_) }
1112sub make_visible { shift->{vp}->make_visible (@_) }
1113
1115sub update_slider { 1114sub update_slider {
1116 my ($self) = @_; 1115 my ($self) = @_;
1117 1116
1118 my $child = ($self->{vp} or return)->child; 1117 my $child = ($self->{vp} or return)->child;
1119 1118
1119 if ($self->{scroll_x}) {
1120 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1120 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1121 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1121 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1122 1122
1123 my $visible = $w1 > $w2; 1123 my $visible = $w1 > $w2;
1124 if ($visible != $self->{hslider}{visible}) { 1124 if ($visible != $self->{hslider_visible}) {
1125 $self->{hslider_visible} = $visible;
1125 $visible ? $self->SUPER::add ($self->{hslider}) 1126 $visible ? $self->SUPER::add ($self->{hslider})
1126 : $self->SUPER::remove ($self->{hslider}); 1127 : $self->SUPER::remove ($self->{hslider});
1128 }
1127 } 1129 }
1128 1130
1131 if ($self->{scroll_y}) {
1129 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1132 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1130 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1133 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1131 1134
1132 my $visible = $h1 > $h2; 1135 my $visible = $h1 > $h2;
1133 if ($visible != $self->{vslider}{visible}) { 1136 if ($visible != $self->{vslider_visible}) {
1137 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider}) 1138 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider}); 1139 : $self->SUPER::remove ($self->{vslider});
1140 }
1141 }
1142}
1143
1144sub start_dragging {
1145 my ($self, $ev) = @_;
1146
1147 $self->grab_focus;
1148
1149 my $ox = $self->{vp}{view_x};
1150 my $oy = $self->{vp}{view_y};
1151
1152 $self->{motion} = sub {
1153 my ($ev, $x, $y) = @_;
1154
1155 $ox -= $ev->{xrel};
1156 $oy -= $ev->{yrel};
1157
1158 $self->{vp}->set_offset ($ox, $oy);
1136 } 1159 };
1137}
1138
1139sub update {
1140 my ($self) = @_;
1141
1142 $self->update_slider;
1143 $self->SUPER::update;
1144} 1160}
1145 1161
1146sub invoke_mouse_wheel { 1162sub invoke_mouse_wheel {
1147 my ($self, $ev) = @_; 1163 my ($self, $ev) = @_;
1148 1164
1149 $self->{vslider}->emit (mouse_wheel => $ev) 1165 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1150 if $ev->{dy};
1151
1152 $self->{hslider}->emit (mouse_wheel => $ev) 1166 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1153 if $ev->{dx};
1154 1167
1155 1 1168 1
1156} 1169}
1157 1170
1158sub invoke_button_down { 1171sub invoke_button_down {
1159 my ($self, $ev, $x, $y) = @_; 1172 my ($self, $ev, $x, $y) = @_;
1160 1173
1161 if ($ev->{button} == 2) { 1174 if ($ev->{button} == 2) {
1162 $self->grab_focus; 1175 $self->start_dragging ($ev);
1163
1164 my $ox = $self->{vp}{view_x} + $ev->{x};
1165 my $oy = $self->{vp}{view_y} + $ev->{y};
1166
1167 $self->{motion} = sub {
1168 my ($ev, $x, $y) = @_;
1169
1170 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1171 $self->update;
1172 };
1173
1174 return 1; 1176 return 1;
1175 } 1177 }
1176 1178
1177 0 1179 0
1178} 1180}
1205 $self->SUPER::invoke_size_allocate ($w, $h) 1207 $self->SUPER::invoke_size_allocate ($w, $h)
1206} 1208}
1207 1209
1208############################################################################# 1210#############################################################################
1209 1211
1210package CFPlus::UI::Frame; 1212package DC::UI::Frame;
1211 1213
1212our @ISA = CFPlus::UI::Bin::; 1214our @ISA = DC::UI::Bin::;
1213 1215
1214use CFPlus::OpenGL; 1216use DC::OpenGL;
1215 1217
1216sub new { 1218sub new {
1217 my $class = shift; 1219 my $class = shift;
1218 1220
1219 $class->SUPER::new ( 1221 $class->SUPER::new (
1229 my ($w, $h) = @$self{qw(w h)}; 1231 my ($w, $h) = @$self{qw(w h)};
1230 1232
1231 glEnable GL_BLEND; 1233 glEnable GL_BLEND;
1232 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1234 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1233 glColor_premultiply @{ $self->{bg} }; 1235 glColor_premultiply @{ $self->{bg} };
1234
1235 glBegin GL_QUADS;
1236 glVertex 0 , 0;
1237 glVertex 0 , $h;
1238 glVertex $w, $h; 1236 glRect 0, 0, $w, $h;
1239 glVertex $w, 0;
1240 glEnd;
1241
1242 glDisable GL_BLEND; 1237 glDisable GL_BLEND;
1243 } 1238 }
1244 1239
1245 $self->SUPER::_draw; 1240 $self->SUPER::_draw;
1246} 1241}
1247 1242
1248############################################################################# 1243#############################################################################
1249 1244
1250package CFPlus::UI::FancyFrame; 1245package DC::UI::FancyFrame;
1251 1246
1252our @ISA = CFPlus::UI::Bin::; 1247our @ISA = DC::UI::Bin::;
1253 1248
1254use CFPlus::OpenGL; 1249use DC::OpenGL;
1255 1250
1256sub new { 1251sub new {
1257 my ($class, %arg) = @_; 1252 my ($class, %arg) = @_;
1258 1253
1259 if ((exists $arg{label}) && !ref $arg{label}) { 1254 if ((exists $arg{label}) && !ref $arg{label}) {
1260 $arg{label} = new CFPlus::UI::Label 1255 $arg{label} = new DC::UI::Label
1261 align => 1, 1256 align => 1,
1262 valign => 0, 1257 valign => 0.5,
1263 text => $arg{label}, 1258 text => $arg{label},
1264 fontsize => ($arg{border} || 0.8) * 0.75; 1259 fontsize => ($arg{border} || 0.8) * 0.75;
1265 } 1260 }
1266 1261
1267 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1268 # label => "", 1263 # label => "",
1269 fg => [0.6, 0.3, 0.1], 1264 fg => undef,
1270 border => 0.8, 1265 border => 0.8,
1271 style => 'single', 1266 style => 'single',
1272 %arg, 1267 %arg,
1273 ); 1268 );
1274 1269
1277 1272
1278sub add { 1273sub add {
1279 my ($self, @widgets) = @_; 1274 my ($self, @widgets) = @_;
1280 1275
1281 $self->SUPER::add (@widgets); 1276 $self->SUPER::add (@widgets);
1282 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1277 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1283} 1278}
1284 1279
1285sub border { 1280sub border {
1286 int $_[0]{border} * $::FONTSIZE 1281 int $_[0]{border} * $::FONTSIZE
1287} 1282}
1327 my $border = $self->border; 1322 my $border = $self->border;
1328 my ($w, $h) = ($self->{w}, $self->{h}); 1323 my ($w, $h) = ($self->{w}, $self->{h});
1329 1324
1330 $child->draw; 1325 $child->draw;
1331 1326
1332 glColor @{$self->{fg}}; 1327 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1333 glBegin GL_LINE_STRIP; 1328 glBegin GL_LINE_STRIP;
1334 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1329 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1335 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1330 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1336 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1331 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1337 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1332 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1345 } 1340 }
1346} 1341}
1347 1342
1348############################################################################# 1343#############################################################################
1349 1344
1350package CFPlus::UI::Toplevel; 1345package DC::UI::Toplevel;
1351 1346
1352our @ISA = CFPlus::UI::Bin::; 1347our @ISA = DC::UI::Bin::;
1353 1348
1354use CFPlus::OpenGL; 1349use DC::OpenGL;
1355 1350
1356my $bg = 1351my $bg =
1357 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1352 new_from_resource DC::Texture "d1_bg.png",
1358 mipmap => 1, wrap => 1; 1353 mipmap => 1, wrap => 1;
1359 1354
1360my @border = 1355my @border =
1361 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1356 map { new_from_resource DC::Texture $_, mipmap => 1 }
1362 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1357 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1363 1358
1364my @icon = 1359my @icon =
1365 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1360 map { new_from_resource DC::Texture $_, mipmap => 1 }
1366 qw(x1_move.png x1_resize.png); 1361 qw(x1_move.png x1_resize.png);
1367 1362
1368sub new { 1363sub new {
1369 my ($class, %arg) = @_; 1364 my ($class, %arg) = @_;
1370 1365
1371 my $self = $class->SUPER::new ( 1366 my $self = $class->SUPER::new (
1372 bg => [1, 1, 1, 1], 1367 bg => [1, 1, 1, 1],
1373 border_bg => [1, 1, 1, 1], 1368 border_bg => [1, 1, 1, 1],
1374 border => 0.6, 1369 border => 0.8,
1375 can_events => 1, 1370 can_events => 1,
1376 min_w => 64, 1371 min_w => 64,
1377 min_h => 32, 1372 min_h => 32,
1378 %arg, 1373 %arg,
1379 ); 1374 );
1380 1375
1381 $self->{title_widget} = new CFPlus::UI::Label 1376 $self->{title_widget} = new DC::UI::Label
1382 align => 0, 1377 align => 0.5,
1383 valign => 1, 1378 valign => 1,
1384 text => $self->{title}, 1379 text => $self->{title},
1385 fontsize => $self->{border}, 1380 fontsize => $self->{border},
1386 if exists $self->{title}; 1381 if exists $self->{title};
1387 1382
1388 if ($self->{has_close_button}) { 1383 if ($self->{has_close_button}) {
1389 $self->{close_button} = 1384 $self->{close_button} =
1390 new CFPlus::UI::ImageButton 1385 new DC::UI::ImageButton
1391 path => 'x1_close.png', 1386 path => 'x1_close.png',
1392 on_activate => sub { $self->emit ("delete") }; 1387 on_activate => sub { $self->emit ("delete") };
1393 1388
1394 $self->CFPlus::UI::Container::add ($self->{close_button}); 1389 $self->DC::UI::Container::add ($self->{close_button});
1395 } 1390 }
1396 1391
1397 $self 1392 $self
1398} 1393}
1399 1394
1400sub add { 1395sub add {
1401 my ($self, @widgets) = @_; 1396 my ($self, @widgets) = @_;
1402 1397
1403 $self->SUPER::add (@widgets); 1398 $self->SUPER::add (@widgets);
1404 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1399 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1405 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1400 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1406} 1401}
1407 1402
1408sub border { 1403sub border {
1409 int $_[0]{border} * $::FONTSIZE 1404 int $_[0]{border} * $::FONTSIZE
1405}
1406
1407sub get_max_wh {
1408 my ($self) = @_;
1409
1410 return ($self->{w}, $self->{h})
1411 if $self->{visible} && $self->{w};
1412
1413 $self->SUPER::get_max_wh
1410} 1414}
1411 1415
1412sub size_request { 1416sub size_request {
1413 my ($self) = @_; 1417 my ($self) = @_;
1414 1418
1539 glEnable GL_TEXTURE_2D; 1543 glEnable GL_TEXTURE_2D;
1540 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1544 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1541 1545
1542 my $border = $self->border; 1546 my $border = $self->border;
1543 1547
1548 if ($border) {
1544 glColor @{ $self->{border_bg} }; 1549 glColor @{ $self->{border_bg} };
1545 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1550 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1546 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1551 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1547 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1552 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1548 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1553 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1549 1554
1550 # move 1555 # move
1551 my $w2 = ($w - $border) * .5; 1556 my $w2 = ($w - $border) * .5;
1552 my $h2 = ($h - $border) * .5; 1557 my $h2 = ($h - $border) * .5;
1553 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1558 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1554 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1559 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1555 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1560 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1556 1561
1557 # resize 1562 # resize
1558 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1563 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1559 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1564 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1560 unless $self->{has_close_button}; 1565 unless $self->{has_close_button};
1561 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1566 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1562 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1567 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1568 }
1563 1569
1564 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1570 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1565 glColor @{ $self->{bg} }; 1571 glColor @{ $self->{bg} };
1566 1572
1567 # TODO: repeat texture not scale 1573 # TODO: repeat texture not scale
1586 if $self->{close_button}; 1592 if $self->{close_button};
1587} 1593}
1588 1594
1589############################################################################# 1595#############################################################################
1590 1596
1591package CFPlus::UI::Table; 1597package DC::UI::Table;
1592 1598
1593our @ISA = CFPlus::UI::Container::; 1599our @ISA = DC::UI::Container::;
1594 1600
1595use List::Util qw(max sum); 1601use List::Util qw(max sum);
1596 1602
1597use CFPlus::OpenGL; 1603use DC::OpenGL;
1598 1604
1599sub new { 1605sub new {
1600 my $class = shift; 1606 my $class = shift;
1601 1607
1602 $class->SUPER::new ( 1608 $class->SUPER::new (
1608 1614
1609sub add { 1615sub add {
1610 my ($self, @widgets) = @_; 1616 my ($self, @widgets) = @_;
1611 1617
1612 for my $child (@widgets) { 1618 for my $child (@widgets) {
1613 $child->{rowspan} ||= 1; 1619 $child->{c_rowspan} ||= 1;
1614 $child->{colspan} ||= 1; 1620 $child->{c_colspan} ||= 1;
1615 } 1621 }
1616 1622
1617 $self->SUPER::add (@widgets); 1623 $self->SUPER::add (@widgets);
1618} 1624}
1619 1625
1623 my @widgets; 1629 my @widgets;
1624 1630
1625 while (@_) { 1631 while (@_) {
1626 my ($col, $row, $child) = splice @_, 0, 3, (); 1632 my ($col, $row, $child) = splice @_, 0, 3, ();
1627 1633
1628 $child->{row} = $row; 1634 $child->{c_row} = $row;
1629 $child->{col} = $col; 1635 $child->{c_col} = $col;
1630 1636
1631 push @widgets, $child; 1637 push @widgets, $child;
1632 } 1638 }
1633 1639
1634 $self->add (@widgets); 1640 $self->add (@widgets);
1640 my (@w, @h); 1646 my (@w, @h);
1641 1647
1642 my @children = $self->children; 1648 my @children = $self->children;
1643 1649
1644 # first pass, columns 1650 # first pass, columns
1645 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1651 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1646 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1652 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1647 1653
1648 my $sw = sum @w[$c .. $c + $cs - 1]; 1654 my $sw = sum @w[$c .. $c + $cs - 1];
1649 1655
1650 if ($w > $sw) { 1656 if ($w > $sw) {
1651 $_ += ($w - $sw) / $cs for @w[$c .. $c + $cs - 1]; 1657 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1652 } 1658 }
1653 } 1659 }
1654 1660
1655 # second pass, rows 1661 # second pass, rows
1656 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1662 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1657 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1663 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1658 1664
1659 my $sh = sum @h[$r .. $r + $rs - 1]; 1665 my $sh = sum @h[$r .. $r + $rs - 1];
1660 1666
1661 if ($h > $sh) { 1667 if ($h > $sh) {
1662 $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1]; 1668 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1663 } 1669 }
1664 } 1670 }
1665 1671
1666 (\@w, \@h) 1672 (\@w, \@h)
1667} 1673}
1690 @col_expand = (1) x @$ws unless @col_expand; 1696 @col_expand = (1) x @$ws unless @col_expand;
1691 my $col_expand = (sum @col_expand) || 1; 1697 my $col_expand = (sum @col_expand) || 1;
1692 1698
1693 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1699 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1694 1700
1695 CFPlus::UI::harmonize $ws; 1701 DC::UI::harmonize $ws;
1696 1702
1697 my @row_expand = @{$self->{row_expand}}; 1703 my @row_expand = @{$self->{row_expand}};
1698 @row_expand = (1) x @$ws unless @row_expand; 1704 @row_expand = (1) x @$ws unless @row_expand;
1699 my $row_expand = (sum @row_expand) || 1; 1705 my $row_expand = (sum @row_expand) || 1;
1700 1706
1701 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1707 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1702 1708
1703 CFPlus::UI::harmonize $hs; 1709 DC::UI::harmonize $hs;
1704 1710
1705 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1711 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1706 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1712 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1707 1713
1708 for my $widget ($self->children) { 1714 for my $widget ($self->children) {
1709 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1715 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1710 1716
1711 $widget->configure ( 1717 $widget->configure (
1712 $x[$c], $y[$r], 1718 $x[$c], $y[$r],
1713 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1719 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1714 ); 1720 );
1717 1 1723 1
1718} 1724}
1719 1725
1720############################################################################# 1726#############################################################################
1721 1727
1722package CFPlus::UI::Fixed; 1728package DC::UI::Fixed;
1723 1729
1724use List::Util qw(min max); 1730use List::Util qw(min max);
1725 1731
1726our @ISA = CFPlus::UI::Container::; 1732our @ISA = DC::UI::Container::;
1727
1728sub add {
1729 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1730
1731 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1732 $self->SUPER::add ($child);
1733}
1734 1733
1735sub _scale($$$) { 1734sub _scale($$$) {
1736 my ($mode, $val, $max) = @_; 1735 my ($rel, $val, $max) = @_;
1737 1736
1738 $mode eq "abs" ? $val 1737 $rel ? $val * $max : $val
1739 : $mode eq "rel" ? $val * $max
1740 : 0
1741} 1738}
1742 1739
1743sub size_request { 1740sub size_request {
1744 my ($self) = @_; 1741 my ($self) = @_;
1745 1742
1746 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1743 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1747 1744
1748 # determine overall size by querying abs widgets 1745 # determine overall size by querying abs widgets
1749 for my $child ($self->visible_children) { 1746 for my $child ($self->visible_children) {
1750 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1747 unless ($child->{c_rel}) {
1748 my $x = $child->{c_x};
1749 my $y = $child->{c_y};
1751 1750
1752 if ($pos eq "abs") {
1753 $w = _scale $size, $w, $child->{req_w};
1754 $h = _scale $size, $h, $child->{req_h};
1755
1756 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1751 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1757 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1752 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1758 } 1753 }
1759 } 1754 }
1760 1755
1761 my $W = $x2 - $x1; 1756 my $W = $x2 - $x1;
1762 my $H = $y2 - $y1; 1757 my $H = $y2 - $y1;
1763 1758
1764 # now layout remaining widgets 1759 # now layout remaining widgets
1765 for my $child ($self->visible_children) { 1760 for my $child ($self->visible_children) {
1766 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1761 if ($child->{c_rel}) {
1762 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1763 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1767 1764
1768 if ($pos ne "abs") {
1769 $x = _scale $pos, $x, $W;
1770 $y = _scale $pos, $x, $H;
1771 $w = _scale $size, $w, $child->{req_w};
1772 $h = _scale $size, $h, $child->{req_h};
1773
1774 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1765 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1775 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1766 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1776 } 1767 }
1777 } 1768 }
1778 1769
1779 my $W = $x2 - $x1; 1770 my $W = $x2 - $x1;
1780 my $H = $y2 - $y1; 1771 my $H = $y2 - $y1;
1784 1775
1785sub invoke_size_allocate { 1776sub invoke_size_allocate {
1786 my ($self, $W, $H) = @_; 1777 my ($self, $W, $H) = @_;
1787 1778
1788 for my $child ($self->visible_children) { 1779 for my $child ($self->visible_children) {
1789 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1780 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1781 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1790 1782
1791 $x = _scale $pos, $x, $W; 1783 $x += $child->{c_halign} * $child->{req_w};
1792 $y = _scale $pos, $x, $H; 1784 $y += $child->{c_valign} * $child->{req_h};
1793 $w = _scale $size, $w, $child->{req_w};
1794 $h = _scale $size, $h, $child->{req_h};
1795 1785
1796 $child->configure ($x, $y, $w, $h); 1786 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1797 } 1787 }
1798 1788
1799 1 1789 1
1800} 1790}
1801 1791
1802############################################################################# 1792#############################################################################
1803 1793
1804package CFPlus::UI::Box; 1794package DC::UI::Box;
1805 1795
1806our @ISA = CFPlus::UI::Container::; 1796our @ISA = DC::UI::Container::;
1807 1797
1808sub size_request { 1798sub size_request {
1809 my ($self) = @_; 1799 my ($self) = @_;
1800
1801 my @children = $self->visible_children;
1810 1802
1811 $self->{vertical} 1803 $self->{vertical}
1812 ? ( 1804 ? (
1813 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1805 (List::Util::max map $_->{req_w}, @children),
1814 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1806 (List::Util::sum map $_->{req_h}, @children),
1815 ) 1807 )
1816 : ( 1808 : (
1817 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1809 (List::Util::sum map $_->{req_w}, @children),
1818 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1810 (List::Util::max map $_->{req_h}, @children),
1819 ) 1811 )
1820} 1812}
1821 1813
1822sub invoke_size_allocate { 1814sub invoke_size_allocate {
1823 my ($self, $w, $h) = @_; 1815 my ($self, $w, $h) = @_;
1844 $req[$_] += $space * $children[$_]{expand} 1836 $req[$_] += $space * $children[$_]{expand}
1845 for 0 .. $#children; 1837 for 0 .. $#children;
1846 } 1838 }
1847 } 1839 }
1848 1840
1849 CFPlus::UI::harmonize \@req; 1841 DC::UI::harmonize \@req;
1850 1842
1851 my $pos = 0; 1843 my $pos = 0;
1852 for (0 .. $#children) { 1844 for (0 .. $#children) {
1853 my $alloc = $req[$_]; 1845 my $alloc = $req[$_];
1854 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1846 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1859 1 1851 1
1860} 1852}
1861 1853
1862############################################################################# 1854#############################################################################
1863 1855
1864package CFPlus::UI::HBox; 1856package DC::UI::HBox;
1865 1857
1866our @ISA = CFPlus::UI::Box::; 1858our @ISA = DC::UI::Box::;
1867 1859
1868sub new { 1860sub new {
1869 my $class = shift; 1861 my $class = shift;
1870 1862
1871 $class->SUPER::new ( 1863 $class->SUPER::new (
1874 ) 1866 )
1875} 1867}
1876 1868
1877############################################################################# 1869#############################################################################
1878 1870
1879package CFPlus::UI::VBox; 1871package DC::UI::VBox;
1880 1872
1881our @ISA = CFPlus::UI::Box::; 1873our @ISA = DC::UI::Box::;
1882 1874
1883sub new { 1875sub new {
1884 my $class = shift; 1876 my $class = shift;
1885 1877
1886 $class->SUPER::new ( 1878 $class->SUPER::new (
1889 ) 1881 )
1890} 1882}
1891 1883
1892############################################################################# 1884#############################################################################
1893 1885
1894package CFPlus::UI::Label; 1886package DC::UI::Label;
1895 1887
1896our @ISA = CFPlus::UI::DrawBG::; 1888our @ISA = DC::UI::DrawBG::;
1897 1889
1898use CFPlus::OpenGL; 1890use DC::OpenGL;
1899 1891
1900sub new { 1892sub new {
1901 my ($class, %arg) = @_; 1893 my ($class, %arg) = @_;
1902 1894
1903 my $self = $class->SUPER::new ( 1895 my $self = $class->SUPER::new (
1908 #text => initial text 1900 #text => initial text
1909 #markup => initial narkup 1901 #markup => initial narkup
1910 #max_w => maximum pixel width 1902 #max_w => maximum pixel width
1911 #style => 0, # render flags 1903 #style => 0, # render flags
1912 ellipsise => 3, # end 1904 ellipsise => 3, # end
1913 layout => (new CFPlus::Layout), 1905 layout => (new DC::Layout),
1914 fontsize => 1, 1906 fontsize => 1,
1915 align => -1, 1907 align => 0.5,
1916 valign => -1, 1908 valign => 0.5,
1917 padding_x => 2, 1909 padding_x => 4,
1918 padding_y => 2, 1910 padding_y => 2,
1919 can_events => 0, 1911 can_events => 0,
1920 %arg 1912 %arg
1921 ); 1913 );
1922 1914
1923 if (exists $self->{template}) { 1915 if (exists $self->{template}) {
1924 my $layout = new CFPlus::Layout; 1916 my $layout = new DC::Layout;
1925 $layout->set_text (delete $self->{template}); 1917 $layout->set_text (delete $self->{template});
1926 $self->{template} = $layout; 1918 $self->{template} = $layout;
1927 } 1919 }
1928 1920
1929 if (exists $self->{markup}) { 1921 if (exists $self->{markup}) {
1947 1939
1948 delete $self->{ox}; 1940 delete $self->{ox};
1949 $self->SUPER::realloc; 1941 $self->SUPER::realloc;
1950} 1942}
1951 1943
1944sub clear {
1945 my ($self) = @_;
1946
1947 $self->set_text ("");
1948}
1949
1952sub set_text { 1950sub set_text {
1953 my ($self, $text) = @_; 1951 my ($self, $text) = @_;
1954 1952
1955 return if $self->{text} eq "T$text"; 1953 return if $self->{text} eq "T$text";
1956 $self->{text} = "T$text"; 1954 $self->{text} = "T$text";
1979 1977
1980sub size_request { 1978sub size_request {
1981 my ($self) = @_; 1979 my ($self) = @_;
1982 1980
1983 $self->{size_req} ||= do { 1981 $self->{size_req} ||= do {
1982 my ($max_w, $max_h) = $self->get_max_wh;
1983
1984 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1984 $self->{layout}->set_font ($self->{font}) if $self->{font};
1985 $self->{layout}->set_width ($self->{max_w} || -1); 1985 $self->{layout}->set_width ($max_w);
1986 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1986 $self->{layout}->set_ellipsise ($self->{ellipsise});
1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1989 1989
1990 my ($w, $h) = $self->{layout}->size; 1990 my ($w, $h) = $self->{layout}->size;
1991 1991
1992 if (exists $self->{template}) { 1992 if (exists $self->{template}) {
1993 $self->{template}->set_font ($self->{font}) if $self->{font}; 1993 $self->{template}->set_font ($self->{font}) if $self->{font};
1994 $self->{template}->set_width ($self->{max_w} || -1); 1994 $self->{template}->set_width ($max_w);
1995 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1995 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1996 1996
1997 my ($w2, $h2) = $self->{template}->size; 1997 my ($w2, $h2) = $self->{template}->size;
1998 1998
1999 $w = List::Util::max $w, $w2; 1999 $w = List::Util::max $w, $w2;
2055 2055
2056 [$self->{layout}->size] 2056 [$self->{layout}->size]
2057 }; 2057 };
2058 2058
2059 unless (exists $self->{ox}) { 2059 unless (exists $self->{ox}) {
2060 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2060 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2061 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2061 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2062 : ($self->{w} - $size->[0]) * 0.5);
2063 2062
2064 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2063 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2065 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2066 : ($self->{h} - $size->[1]) * 0.5);
2067 }; 2064 };
2068 2065
2069 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2066# unless ($self->{list}) {
2070 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2067# $self->{list} = DC::OpenGL::glGenList;
2071 2068# DC::OpenGL::glNewList $self->{list};
2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2069# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2070# DC::OpenGL::glEndList;
2071# }
2072#
2073# DC::OpenGL::glCallList $self->{list};
2074
2075 $self->{layout}->draw;
2073} 2076}
2077
2078#sub destroy {
2079# my ($self) = @_;
2080#
2081# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2082#
2083# $self->SUPER::destroy;
2084#}
2074 2085
2075############################################################################# 2086#############################################################################
2076 2087
2077package CFPlus::UI::EntryBase; 2088package DC::UI::EntryBase;
2078 2089
2079our @ISA = CFPlus::UI::Label::; 2090our @ISA = DC::UI::Label::;
2080 2091
2081use CFPlus::OpenGL; 2092use DC::OpenGL;
2082 2093
2083sub new { 2094sub new {
2084 my $class = shift; 2095 my $class = shift;
2085 2096
2086 $class->SUPER::new ( 2097 $class->SUPER::new (
2087 fg => [1, 1, 1], 2098 fg => [1, 1, 1],
2088 bg => [0, 0, 0, 0.2], 2099 bg => [0, 0, 0, 0.2],
2100 outline => undef,
2089 active_bg => [1, 1, 1, 0.5], 2101 active_bg => [0, 0, 1, .2],
2090 active_fg => [0, 0, 0], 2102 active_fg => [1, 1, 1],
2103 active_outline => [1, 1, 0],
2091 can_hover => 1, 2104 can_hover => 1,
2092 can_focus => 1, 2105 can_focus => 1,
2106 align => 0,
2093 valign => 0, 2107 valign => 0.5,
2094 can_events => 1, 2108 can_events => 1,
2095 ellipsise => 0, 2109 ellipsise => 0,
2110 padding_x => 4,
2111 padding_y => 2,
2096 #text => ... 2112 #text => ...
2097 #hidden => "*", 2113 #hidden => "*",
2098 @_ 2114 @_
2099 ) 2115 )
2100} 2116}
2147 2163
2148 my $text = $self->get_text; 2164 my $text = $self->get_text;
2149 2165
2150 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2166 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2151 2167
2152 if ($uni == 8) { 2168 if ($sym == DC::SDLK_BACKSPACE) {
2153 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2169 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2154 } elsif ($uni == 127) { 2170 } elsif ($sym == DC::SDLK_DELETE) {
2155 substr $text, $self->{cursor}, 1, ""; 2171 substr $text, $self->{cursor}, 1, "";
2156 } elsif ($sym == CFPlus::SDLK_LEFT) { 2172 } elsif ($sym == DC::SDLK_LEFT) {
2157 --$self->{cursor} if $self->{cursor}; 2173 --$self->{cursor} if $self->{cursor};
2158 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2174 } elsif ($sym == DC::SDLK_RIGHT) {
2159 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2175 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2160 } elsif ($sym == CFPlus::SDLK_HOME) { 2176 } elsif ($sym == DC::SDLK_HOME) {
2161 # what a hack 2177 # what a hack
2162 $self->{cursor} = 2178 $self->{cursor} =
2163 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2179 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2164 ? length $1 2180 ? length $1
2165 : 0; 2181 : 0;
2166 } elsif ($sym == CFPlus::SDLK_END) { 2182 } elsif ($sym == DC::SDLK_END) {
2167 # uh, again 2183 # uh, again
2168 $self->{cursor} = 2184 $self->{cursor} =
2169 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2185 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2170 ? $self->{cursor} + length $1 2186 ? $self->{cursor} + length $1
2171 : length $self->{text}; 2187 : length $self->{text};
2235 glColor_premultiply @{$self->{bg}}; 2251 glColor_premultiply @{$self->{bg}};
2236 } 2252 }
2237 2253
2238 glEnable GL_BLEND; 2254 glEnable GL_BLEND;
2239 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2255 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2240 glBegin GL_QUADS;
2241 glVertex 0 , 0;
2242 glVertex 0 , $self->{h};
2243 glVertex $self->{w}, $self->{h}; 2256 glRect 0, 0, $self->{w}, $self->{h};
2244 glVertex $self->{w}, 0;
2245 glEnd;
2246 glDisable GL_BLEND; 2257 glDisable GL_BLEND;
2247 2258
2248 $self->SUPER::_draw; 2259 $self->SUPER::_draw;
2249 2260
2250 #TODO: force update every cursor change :( 2261 #TODO: force update every cursor change :(
2252 2263
2253 unless (exists $self->{cur_h}) { 2264 unless (exists $self->{cur_h}) {
2254 my $text = substr $self->{text}, 0, $self->{cursor}; 2265 my $text = substr $self->{text}, 0, $self->{cursor};
2255 utf8::encode $text; 2266 utf8::encode $text;
2256 2267
2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2268 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2258 } 2269 }
2259 2270
2271 glColor_premultiply @{$self->{active_fg}};
2260 glBegin GL_LINES; 2272 glBegin GL_LINES;
2261 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2273 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2262 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2274 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2263 glEnd; 2275 glEnd;
2276
2277 glLineWidth 3;
2278 glColor @{$self->{active_outline}};
2279 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2280 glLineWidth 1;
2281
2282 } else {
2283 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2284 glBegin GL_LINE_STRIP;
2285 glVertex .5, $self->{h} * .5;
2286 glVertex .5, $self->{h} - 2.5;
2287 glVertex $self->{w} - .5, $self->{h} - 2.5;
2288 glVertex $self->{w} - .5, $self->{h} * .5;
2289 glEnd;
2264 } 2290 }
2265} 2291}
2266 2292
2267############################################################################# 2293#############################################################################
2268 2294
2269package CFPlus::UI::Entry; 2295package DC::UI::Entry;
2270 2296
2271our @ISA = CFPlus::UI::EntryBase::; 2297our @ISA = DC::UI::EntryBase::;
2272 2298
2273use CFPlus::OpenGL; 2299use DC::OpenGL;
2300
2301sub new {
2302 my $class = shift;
2303
2304 $class->SUPER::new (
2305 history_pointer => -1,
2306 @_
2307 )
2308}
2309
2274 2310
2275sub invoke_key_down { 2311sub invoke_key_down {
2276 my ($self, $ev) = @_; 2312 my ($self, $ev) = @_;
2277 2313
2278 my $sym = $ev->{sym}; 2314 my $sym = $ev->{sym};
2284 $self->{history_pointer} = -1; 2320 $self->{history_pointer} = -1;
2285 $self->{history_saveback} = ''; 2321 $self->{history_saveback} = '';
2286 $self->emit (activate => $txt); 2322 $self->emit (activate => $txt);
2287 $self->update; 2323 $self->update;
2288 2324
2289 } elsif ($sym == CFPlus::SDLK_UP) { 2325 } elsif ($sym == DC::SDLK_UP) {
2290 if ($self->{history_pointer} < 0) { 2326 if ($self->{history_pointer} < 0) {
2291 $self->{history_saveback} = $self->get_text; 2327 $self->{history_saveback} = $self->get_text;
2292 } 2328 }
2293 if (@{$self->{history} || []} > 0) { 2329 if (@{$self->{history} || []} > 0) {
2294 $self->{history_pointer}++; 2330 $self->{history_pointer}++;
2296 $self->{history_pointer} = @{$self->{history} || []} - 1; 2332 $self->{history_pointer} = @{$self->{history} || []} - 1;
2297 } 2333 }
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2334 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2299 } 2335 }
2300 2336
2301 } elsif ($sym == CFPlus::SDLK_DOWN) { 2337 } elsif ($sym == DC::SDLK_DOWN) {
2302 $self->{history_pointer}--; 2338 $self->{history_pointer}--;
2303 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2339 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2304 2340
2305 if ($self->{history_pointer} >= 0) { 2341 if ($self->{history_pointer} >= 0) {
2306 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2307 } else { 2343 } else {
2344 if (defined $self->{history_saveback}) {
2308 $self->set_text ($self->{history_saveback}); 2345 $self->set_text ($self->{history_saveback});
2346 $self->{history_saveback} = undef;
2347 }
2309 } 2348 }
2310 2349
2311 } else { 2350 } else {
2312 return $self->SUPER::invoke_key_down ($ev) 2351 return $self->SUPER::invoke_key_down ($ev)
2313 } 2352 }
2315 1 2354 1
2316} 2355}
2317 2356
2318############################################################################# 2357#############################################################################
2319 2358
2320package CFPlus::UI::TextEdit; 2359package DC::UI::TextEdit;
2321 2360
2322our @ISA = CFPlus::UI::EntryBase::; 2361our @ISA = DC::UI::EntryBase::;
2323 2362
2324use CFPlus::OpenGL; 2363use DC::OpenGL;
2364
2365sub new {
2366 my $class = shift;
2367
2368 $class->SUPER::new (
2369 padding_y => 4,
2370
2371 @_
2372 )
2373}
2325 2374
2326sub move_cursor_ver { 2375sub move_cursor_ver {
2327 my ($self, $dy) = @_; 2376 my ($self, $dy) = @_;
2328 2377
2329 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2378 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2330 2379
2331 $y += $dy; 2380 $line += $dy;
2332 2381
2333 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2382 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2334 $self->{cursor} = $index; 2383 $self->{cursor} = $index;
2335 delete $self->{cur_h}; 2384 delete $self->{cur_h};
2336 $self->update; 2385 $self->update;
2337 return; 2386 return;
2338 } 2387 }
2341sub invoke_key_down { 2390sub invoke_key_down {
2342 my ($self, $ev) = @_; 2391 my ($self, $ev) = @_;
2343 2392
2344 my $sym = $ev->{sym}; 2393 my $sym = $ev->{sym};
2345 2394
2346 if ($sym == CFPlus::SDLK_UP) { 2395 if ($sym == DC::SDLK_UP) {
2347 $self->move_cursor_ver (-1); 2396 $self->move_cursor_ver (-1);
2348 } elsif ($sym == CFPlus::SDLK_DOWN) { 2397 } elsif ($sym == DC::SDLK_DOWN) {
2349 $self->move_cursor_ver (+1); 2398 $self->move_cursor_ver (+1);
2350 } else { 2399 } else {
2351 return $self->SUPER::invoke_key_down ($ev) 2400 return $self->SUPER::invoke_key_down ($ev)
2352 } 2401 }
2353 2402
2354 1 2403 1
2355} 2404}
2356 2405
2357############################################################################# 2406#############################################################################
2358 2407
2359package CFPlus::UI::Button; 2408package DC::UI::ButtonBin;
2360 2409
2361our @ISA = CFPlus::UI::Label::; 2410our @ISA = DC::UI::Bin::;
2362 2411
2363use CFPlus::OpenGL; 2412use DC::OpenGL;
2364 2413
2365my @tex = 2414my @tex =
2366 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2415 map { new_from_resource DC::Texture $_, mipmap => 1 }
2367 qw(b1_button_inactive.png b1_button_active.png); 2416 qw(b1_button_inactive.png b1_button_active.png);
2368 2417
2369sub new { 2418sub new {
2370 my $class = shift; 2419 my $class = shift;
2371 2420
2372 $class->SUPER::new ( 2421 $class->SUPER::new (
2422 can_hover => 1,
2423 align => 0.5,
2424 valign => 0.5,
2425 can_events => 1,
2426 @_
2427 )
2428}
2429
2430sub invoke_button_up {
2431 my ($self, $ev, $x, $y) = @_;
2432
2433 $self->emit ("activate")
2434 if $x >= 0 && $x < $self->{w}
2435 && $y >= 0 && $y < $self->{h};
2436
2437 1
2438}
2439
2440sub _draw {
2441 my ($self) = @_;
2442
2443 glEnable GL_TEXTURE_2D;
2444 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2445 glColor 0, 0, 0, 1;
2446
2447 my $tex = $tex[$GRAB == $self];
2448 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2449
2450 glDisable GL_TEXTURE_2D;
2451
2452 $self->SUPER::_draw;
2453}
2454
2455#############################################################################
2456
2457package DC::UI::Button;
2458
2459our @ISA = DC::UI::Label::;
2460
2461use DC::OpenGL;
2462
2463my @tex =
2464 map { new_from_resource DC::Texture $_, mipmap => 1 }
2465 qw(b1_button_inactive.png b1_button_active.png);
2466
2467sub new {
2468 my $class = shift;
2469
2470 $class->SUPER::new (
2373 padding_x => 4, 2471 padding_x => 8,
2374 padding_y => 4, 2472 padding_y => 4,
2375 fg => [1.0, 1.0, 1.0], 2473 fg => [1.0, 1.0, 1.0],
2376 active_fg => [0.8, 0.8, 0.8], 2474 active_fg => [0.8, 0.8, 0.8],
2377 can_hover => 1, 2475 can_hover => 1,
2378 align => 0, 2476 align => 0.5,
2379 valign => 0, 2477 valign => 0.5,
2380 can_events => 1, 2478 can_events => 1,
2381 @_ 2479 @_
2382 ) 2480 )
2383} 2481}
2384 2482
2409 $self->SUPER::_draw; 2507 $self->SUPER::_draw;
2410} 2508}
2411 2509
2412############################################################################# 2510#############################################################################
2413 2511
2414package CFPlus::UI::CheckBox; 2512package DC::UI::CheckBox;
2415 2513
2416our @ISA = CFPlus::UI::DrawBG::; 2514our @ISA = DC::UI::DrawBG::;
2417 2515
2418my @tex = 2516my @tex =
2419 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2517 map { new_from_resource DC::Texture $_, mipmap => 1 }
2420 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2518 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2421 2519
2422use CFPlus::OpenGL; 2520use DC::OpenGL;
2423 2521
2424sub new { 2522sub new {
2425 my $class = shift; 2523 my $class = shift;
2426 2524
2427 $class->SUPER::new ( 2525 $class->SUPER::new (
2467sub _draw { 2565sub _draw {
2468 my ($self) = @_; 2566 my ($self) = @_;
2469 2567
2470 $self->SUPER::_draw; 2568 $self->SUPER::_draw;
2471 2569
2472 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2570 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2473 2571
2474 my ($w, $h) = @$self{qw(w h)}; 2572 my ($w, $h) = @$self{qw(w h)};
2475 2573
2476 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2574 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2477 2575
2484 glDisable GL_TEXTURE_2D; 2582 glDisable GL_TEXTURE_2D;
2485} 2583}
2486 2584
2487############################################################################# 2585#############################################################################
2488 2586
2489package CFPlus::UI::Image; 2587package DC::UI::Image;
2490 2588
2491our @ISA = CFPlus::UI::Base::; 2589our @ISA = DC::UI::Base::;
2492 2590
2493use CFPlus::OpenGL; 2591use DC::OpenGL;
2494 2592
2495our %texture_cache; 2593our %texture_cache;
2496 2594
2497sub new { 2595sub new {
2498 my $class = shift; 2596 my $class = shift;
2499 2597
2500 my $self = $class->SUPER::new ( 2598 my $self = $class->SUPER::new (
2501 can_events => 0, 2599 can_events => 0,
2600 scale => 1,
2502 @_, 2601 @_,
2503 ); 2602 );
2504 2603
2505 $self->{path} || $self->{tex} 2604 $self->{path} || $self->{tex}
2506 or Carp::croak "'path' or 'tex' attributes required"; 2605 or Carp::croak "'path' or 'tex' attributes required";
2507 2606
2508 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2607 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2509 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2608 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2510 2609
2511 CFPlus::weaken $texture_cache{$self->{path}}; 2610 DC::weaken $texture_cache{$self->{path}};
2512 2611
2513 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2612 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2514 2613
2515 $self 2614 $self
2516} 2615}
2517 2616
2518sub STORABLE_freeze { 2617sub STORABLE_freeze {
2519 my ($self, $cloning) = @_; 2618 my ($self, $cloning) = @_;
2520 2619
2521 $self->{path} 2620 $self->{path}
2522 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2621 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2523 2622
2524 $self->{path} 2623 $self->{path}
2525} 2624}
2526 2625
2527sub STORABLE_attach { 2626sub STORABLE_attach {
2531} 2630}
2532 2631
2533sub size_request { 2632sub size_request {
2534 my ($self) = @_; 2633 my ($self) = @_;
2535 2634
2536 ($self->{tex}{w}, $self->{tex}{h}) 2635 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2537} 2636}
2538 2637
2539sub _draw { 2638sub _draw {
2540 my ($self) = @_; 2639 my ($self) = @_;
2541 2640
2551 } 2650 }
2552 2651
2553 glEnable GL_TEXTURE_2D; 2652 glEnable GL_TEXTURE_2D;
2554 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2653 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2555 2654
2556 $tex->draw_quad (0, 0, $w, $h); 2655 $tex->draw_quad_alpha (0, 0, $w, $h);
2557 2656
2558 glDisable GL_TEXTURE_2D; 2657 glDisable GL_TEXTURE_2D;
2559} 2658}
2560 2659
2561############################################################################# 2660#############################################################################
2562 2661
2563package CFPlus::UI::ImageButton; 2662package DC::UI::ImageButton;
2564 2663
2565our @ISA = CFPlus::UI::Image::; 2664our @ISA = DC::UI::Image::;
2566 2665
2567use CFPlus::OpenGL; 2666use DC::OpenGL;
2568 2667
2569my %textures; 2668my %textures;
2570 2669
2571sub new { 2670sub new {
2572 my $class = shift; 2671 my $class = shift;
2575 padding_x => 4, 2674 padding_x => 4,
2576 padding_y => 4, 2675 padding_y => 4,
2577 fg => [1, 1, 1], 2676 fg => [1, 1, 1],
2578 active_fg => [0, 0, 1], 2677 active_fg => [0, 0, 1],
2579 can_hover => 1, 2678 can_hover => 1,
2580 align => 0, 2679 align => 0.5,
2581 valign => 0, 2680 valign => 0.5,
2582 can_events => 1, 2681 can_events => 1,
2583 @_ 2682 @_
2584 ); 2683 );
2585} 2684}
2586 2685
2686sub invoke_button_down {
2687 my ($self, $ev, $x, $y) = @_;
2688
2689 1
2690}
2691
2587sub invoke_button_up { 2692sub invoke_button_up {
2588 my ($self, $ev, $x, $y) = @_; 2693 my ($self, $ev, $x, $y) = @_;
2589 2694
2590 $self->emit ("activate") 2695 $self->emit ("activate")
2591 if $x >= 0 && $x < $self->{w} 2696 if $x >= 0 && $x < $self->{w}
2594 1 2699 1
2595} 2700}
2596 2701
2597############################################################################# 2702#############################################################################
2598 2703
2599package CFPlus::UI::VGauge; 2704package DC::UI::VGauge;
2600 2705
2601our @ISA = CFPlus::UI::Base::; 2706our @ISA = DC::UI::Base::;
2602 2707
2603use List::Util qw(min max); 2708use List::Util qw(min max);
2604 2709
2605use CFPlus::OpenGL; 2710use DC::OpenGL;
2606 2711
2607my %tex = ( 2712my %tex = (
2608 food => [ 2713 food => [
2609 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2714 map { new_from_resource DC::Texture $_, mipmap => 1 }
2610 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2715 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2611 ], 2716 ],
2612 grace => [ 2717 grace => [
2613 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2718 map { new_from_resource DC::Texture $_, mipmap => 1 }
2614 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2719 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2615 ], 2720 ],
2616 hp => [ 2721 hp => [
2617 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2722 map { new_from_resource DC::Texture $_, mipmap => 1 }
2618 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2723 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2619 ], 2724 ],
2620 mana => [ 2725 mana => [
2621 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2726 map { new_from_resource DC::Texture $_, mipmap => 1 }
2622 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2727 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2623 ], 2728 ],
2624); 2729);
2625 2730
2626# eg. VGauge->new (gauge => 'food'), default gauge: food 2731# eg. VGauge->new (gauge => 'food'), default gauge: food
2729 glDisable GL_TEXTURE_2D; 2834 glDisable GL_TEXTURE_2D;
2730} 2835}
2731 2836
2732############################################################################# 2837#############################################################################
2733 2838
2839package DC::UI::Progress;
2840
2841our @ISA = DC::UI::Label::;
2842
2843use DC::OpenGL;
2844
2845sub new {
2846 my ($class, %arg) = @_;
2847
2848 my $self = $class->SUPER::new (
2849 padding_x => 2,
2850 padding_y => 2,
2851 fg => [1, 1, 1],
2852 bg => [0, 0, 1, 0.2],
2853 bar => [0.7, 0.5, 0.1, 0.8],
2854 outline => [0.4, 0.3, 0],
2855 fontsize => 0.9,
2856 valign => 0.5,
2857 align => 0.5,
2858 can_events => 1,
2859 ellipsise => 1,
2860 label => "%d%%",
2861 %arg,
2862 );
2863
2864 $self->set_value ($arg{value} || -1);
2865
2866 $self
2867}
2868
2869sub set_label {
2870 my ($self, $label) = @_;
2871
2872 return if $self->{label} eq $label;
2873 $self->{label} = $label;
2874
2875 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2876}
2877
2878sub set_value {
2879 my ($self, $value) = @_;
2880
2881 if ($self->{value} ne $value) {
2882 $self->{value} = $value;
2883
2884 if ($value < 0) {
2885 $self->set_text ("-");
2886 } else {
2887 $self->set_text (sprintf $self->{label}, $value * 100);
2888 }
2889
2890 $self->update;
2891 }
2892}
2893
2894sub _draw {
2895 my ($self) = @_;
2896
2897 glEnable GL_BLEND;
2898 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2899
2900 my $px = $self->{padding_x};
2901 my $py = $self->{padding_y};
2902
2903 if ($self->{value} >= 0) {
2904 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2905
2906 glColor_premultiply @{$self->{bar}};
2907 glRect $px, $py, $s, $self->{h} - $py;
2908 glColor_premultiply @{$self->{bg}};
2909 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2910 }
2911
2912 glColor_premultiply @{$self->{outline}};
2913
2914 $px -= .5;
2915 $py -= .5;
2916
2917 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2918
2919 glDisable GL_BLEND;
2920
2921 {
2922 local $self->{bg}; # do not draw background
2923 $self->SUPER::_draw;
2924 }
2925}
2926
2927#############################################################################
2928
2929package DC::UI::ExperienceProgress;
2930
2931our @ISA = DC::UI::Progress::;
2932
2933sub new {
2934 my ($class, %arg) = @_;
2935
2936 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2937
2938 my $self = $class->SUPER::new (
2939 %arg,
2940 tooltip => sub {
2941 my ($self) = @_;
2942
2943 sprintf "%slevel %d\n%s points\n%s next level\n%s to go",
2944 $tt,
2945 $self->{lvl},
2946 ::formsep ($self->{exp}),
2947 ::formsep ($self->{nxt}),
2948 ::formsep ($self->{nxt} - $self->{exp}),
2949 },
2950 );
2951
2952 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2953 if $::CONN;
2954
2955 $self
2956}
2957
2958sub DESTROY {
2959 my ($self) = @_;
2960
2961 delete $::CONN->{on_exp_update}{$self+0}
2962 if $::CONN;
2963
2964 $self->SUPER::DESTROY;
2965}
2966
2967sub set_value {
2968 my ($self, $lvl, $exp) = @_;
2969
2970 $self->{lvl} = $lvl;
2971 $self->{exp} = $exp;
2972
2973 my $v = -1;
2974
2975 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2976 my $l0 = $table->[$lvl - 1];
2977 my $l1 = $table->[$lvl];
2978
2979 $self->{nxt} = $l1;
2980
2981 $v = ($exp - $l0) / ($l1 - $l0);
2982 }
2983
2984 $self->SUPER::set_value ($v);
2985}
2986
2987#############################################################################
2988
2734package CFPlus::UI::Gauge; 2989package DC::UI::Gauge;
2735 2990
2736our @ISA = CFPlus::UI::VBox::; 2991our @ISA = DC::UI::VBox::;
2737 2992
2738sub new { 2993sub new {
2739 my ($class, %arg) = @_; 2994 my ($class, %arg) = @_;
2740 2995
2741 my $self = $class->SUPER::new ( 2996 my $self = $class->SUPER::new (
2743 can_hover => 1, 2998 can_hover => 1,
2744 can_events => 1, 2999 can_events => 1,
2745 %arg, 3000 %arg,
2746 ); 3001 );
2747 3002
2748 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3003 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2749 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3004 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2750 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3005 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2751 3006
2752 $self 3007 $self
2753} 3008}
2754 3009
2755sub set_fontsize { 3010sub set_fontsize {
2776 $self->{value}->set_text ($val); 3031 $self->{value}->set_text ($val);
2777} 3032}
2778 3033
2779############################################################################# 3034#############################################################################
2780 3035
2781package CFPlus::UI::Slider; 3036package DC::UI::Slider;
2782 3037
2783use strict; 3038use strict;
2784 3039
2785use CFPlus::OpenGL; 3040use DC::OpenGL;
2786 3041
2787our @ISA = CFPlus::UI::DrawBG::; 3042our @ISA = DC::UI::DrawBG::;
2788 3043
2789my @tex = 3044my @tex =
2790 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3045 map { new_from_resource DC::Texture $_ }
2791 qw(s1_slider.png s1_slider_bg.png); 3046 qw(s1_slider.png s1_slider_bg.png);
2792 3047
2793sub new { 3048sub new {
2794 my $class = shift; 3049 my $class = shift;
2795 3050
2832sub set_value { 3087sub set_value {
2833 my ($self, $value) = @_; 3088 my ($self, $value) = @_;
2834 3089
2835 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3090 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2836 3091
2837 $hi = $lo + 1 if $hi <= $lo; 3092 $hi = $lo if $hi < $lo;
2838 3093
2839 $page = $hi - $lo if $page > $hi - $lo; 3094 $value = $hi - $page if $value > $hi - $page;
2840
2841 $value = $lo if $value < $lo; 3095 $value = $lo if $value < $lo;
2842 $value = $hi - $page if $value > $hi - $page;
2843 3096
2844 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3097 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2845 if $unit; 3098 if $unit;
2846 3099
2847 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3100 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2863 3116
2864 $self->SUPER::invoke_button_down ($ev, $x, $y); 3117 $self->SUPER::invoke_button_down ($ev, $x, $y);
2865 3118
2866 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3119 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2867 3120
2868 $self->invoke_mouse_motion ($ev, $x, $y) 3121 $self->invoke_mouse_motion ($ev, $x, $y);
3122
3123 1
2869} 3124}
2870 3125
2871sub invoke_mouse_motion { 3126sub invoke_mouse_motion {
2872 my ($self, $ev, $x, $y) = @_; 3127 my ($self, $ev, $x, $y) = @_;
2873 3128
2889sub invoke_mouse_wheel { 3144sub invoke_mouse_wheel {
2890 my ($self, $ev) = @_; 3145 my ($self, $ev) = @_;
2891 3146
2892 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3147 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2893 3148
2894 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3149 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2895 3150
2896 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3151 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2897 3152
2898 ! ! $delta 3153 1
2899} 3154}
2900 3155
2901sub update { 3156sub update {
2902 my ($self) = @_; 3157 my ($self) = @_;
2903 3158
2909 my ($self) = @_; 3164 my ($self) = @_;
2910 3165
2911 unless ($self->{knob_w}) { 3166 unless ($self->{knob_w}) {
2912 $self->set_value ($self->{range}[0]); 3167 $self->set_value ($self->{range}[0]);
2913 3168
2914 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3169 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2915 my $range = ($hi - $page - $lo) || 1e-100; 3170 my $range = ($hi - $page - $lo) || 1e-10;
2916 3171
2917 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3172 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
2918 3173
2919 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3174 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2920 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3175 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2921 3176
2922 $value = ($value - $lo) / $range; 3177 $value = ($value - $lo) / $range;
2952 glDisable GL_TEXTURE_2D; 3207 glDisable GL_TEXTURE_2D;
2953} 3208}
2954 3209
2955############################################################################# 3210#############################################################################
2956 3211
2957package CFPlus::UI::ValSlider; 3212package DC::UI::ValSlider;
2958 3213
2959our @ISA = CFPlus::UI::HBox::; 3214our @ISA = DC::UI::HBox::;
2960 3215
2961sub new { 3216sub new {
2962 my ($class, %arg) = @_; 3217 my ($class, %arg) = @_;
2963 3218
2964 my $range = delete $arg{range}; 3219 my $range = delete $arg{range};
2965 3220
2966 my $self = $class->SUPER::new ( 3221 my $self = $class->SUPER::new (
2967 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3222 slider => (new DC::UI::Slider expand => 1, range => $range),
2968 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3223 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2969 to_value => sub { shift }, 3224 to_value => sub { shift },
2970 from_value => sub { shift }, 3225 from_value => sub { shift },
2971 %arg, 3226 %arg,
2972 ); 3227 );
2973 3228
2993sub set_range { shift->{slider}->set_range (@_) } 3248sub set_range { shift->{slider}->set_range (@_) }
2994sub set_value { shift->{slider}->set_value (@_) } 3249sub set_value { shift->{slider}->set_value (@_) }
2995 3250
2996############################################################################# 3251#############################################################################
2997 3252
2998package CFPlus::UI::TextScroller; 3253package DC::UI::TextScroller;
2999 3254
3000our @ISA = CFPlus::UI::HBox::; 3255our @ISA = DC::UI::HBox::;
3001 3256
3002use CFPlus::OpenGL; 3257use DC::OpenGL;
3003 3258
3004sub new { 3259sub new {
3005 my $class = shift; 3260 my $class = shift;
3006 3261
3007 my $self = $class->SUPER::new ( 3262 my $self = $class->SUPER::new (
3009 can_events => 1, 3264 can_events => 1,
3010 indent => 0, 3265 indent => 0,
3011 #font => default_font 3266 #font => default_font
3012 @_, 3267 @_,
3013 3268
3014 layout => (new CFPlus::Layout), 3269 layout => (new DC::Layout),
3015 par => [], 3270 par => [],
3016 max_par => 0, 3271 max_par => 0,
3017 height => 0, 3272 height => 0,
3018 children => [ 3273 children => [
3019 (new CFPlus::UI::Empty expand => 1), 3274 (new DC::UI::Empty expand => 1),
3020 (new CFPlus::UI::Slider vertical => 1), 3275 (new DC::UI::Slider vertical => 1),
3021 ], 3276 ],
3022 ); 3277 );
3023 3278
3024 $self->{children}[1]->connect (changed => sub { $self->update }); 3279 $self->{children}[1]->connect (changed => sub { $self->update });
3025 3280
3034} 3289}
3035 3290
3036sub size_request { 3291sub size_request {
3037 my ($self) = @_; 3292 my ($self) = @_;
3038 3293
3039 my ($empty, $slider) = @{ $self->{children} }; 3294 my ($empty, $slider) = $self->visible_children;
3040 3295
3041 local $self->{children} = [$empty, $slider]; 3296 local $self->{children} = [$empty, $slider];
3042 $self->SUPER::size_request 3297 $self->SUPER::size_request
3043} 3298}
3044 3299
3081 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3336 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3082 $layout->set_markup ($para->{markup}); 3337 $layout->set_markup ($para->{markup});
3083 3338
3084 $layout->set_shapes ( 3339 $layout->set_shapes (
3085 map 3340 map
3086 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3341 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3087 @{$para->{widget}} 3342 @{$para->{widget}}
3088 ); 3343 );
3089 3344
3090 $layout 3345 $layout
3091} 3346}
3208 $ROOT->on_post_alloc ($self => sub { 3463 $ROOT->on_post_alloc ($self => sub {
3209 $self->force_uptodate; 3464 $self->force_uptodate;
3210 3465
3211 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3466 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3212 3467
3213 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3468 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3214 glClearColor 0, 0, 0, 0; 3469 glClearColor 0, 0, 0, 0;
3215 glClear GL_COLOR_BUFFER_BIT; 3470 glClear GL_COLOR_BUFFER_BIT;
3216 3471
3217 package CFPlus::UI::Base; 3472 package DC::UI::Base;
3218 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3473 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3219 (0, 0, $self->{w}, $self->{h}); 3474 (0, 0, $self->{w}, $self->{h});
3220 3475
3221 my $top = int $self->{children}[1]{range}[0]; 3476 my $top = int $self->{children}[1]{range}[0];
3222 3477
3233 3488
3234 if ($y0 < $y + $h && $y < $y1) { 3489 if ($y0 < $y + $h && $y < $y1) {
3235 my $layout = $self->get_layout ($para); 3490 my $layout = $self->get_layout ($para);
3236 3491
3237 $layout->render ($para->{indent}, $y - $y0); 3492 $layout->render ($para->{indent}, $y - $y0);
3493 $layout->draw;
3238 3494
3239 if (my @w = @{ $para->{widget} }) { 3495 if (my @w = @{ $para->{widget} }) {
3240 my @s = $layout->get_shapes; 3496 my @s = $layout->get_shapes;
3241 3497
3242 for (@w) { 3498 for (@w) {
3280 $self->{children}[1]->draw; 3536 $self->{children}[1]->draw;
3281} 3537}
3282 3538
3283############################################################################# 3539#############################################################################
3284 3540
3285package CFPlus::UI::Animator; 3541package DC::UI::Animator;
3286 3542
3287use CFPlus::OpenGL; 3543use DC::OpenGL;
3288 3544
3289our @ISA = CFPlus::UI::Bin::; 3545our @ISA = DC::UI::Bin::;
3290 3546
3291sub moveto { 3547sub moveto {
3292 my ($self, $x, $y) = @_; 3548 my ($self, $x, $y) = @_;
3293 3549
3294 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3550 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3322 glPopMatrix; 3578 glPopMatrix;
3323} 3579}
3324 3580
3325############################################################################# 3581#############################################################################
3326 3582
3327package CFPlus::UI::Flopper; 3583package DC::UI::Flopper;
3328 3584
3329our @ISA = CFPlus::UI::Button::; 3585our @ISA = DC::UI::Button::;
3330 3586
3331sub new { 3587sub new {
3332 my $class = shift; 3588 my $class = shift;
3333 3589
3334 my $self = $class->SUPER::new ( 3590 my $self = $class->SUPER::new (
3346 $self->{other}->toggle_visibility; 3602 $self->{other}->toggle_visibility;
3347} 3603}
3348 3604
3349############################################################################# 3605#############################################################################
3350 3606
3351package CFPlus::UI::Tooltip; 3607package DC::UI::Tooltip;
3352 3608
3353our @ISA = CFPlus::UI::Bin::; 3609our @ISA = DC::UI::Bin::;
3354 3610
3355use CFPlus::OpenGL; 3611use DC::OpenGL;
3356 3612
3357sub new { 3613sub new {
3358 my $class = shift; 3614 my $class = shift;
3359 3615
3360 $class->SUPER::new ( 3616 $class->SUPER::new (
3364} 3620}
3365 3621
3366sub set_tooltip_from { 3622sub set_tooltip_from {
3367 my ($self, $widget) = @_; 3623 my ($self, $widget) = @_;
3368 3624
3369 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3370 if $widget->{tooltip} =~ /^#(.*)$/;
3371
3372 my $tooltip = $widget->{tooltip}; 3625 my $tip = $widget->{tooltip};
3626 $tip = $tip->($widget) if "CODE" eq ref $tip;
3627
3628 $tip = DC::Pod::section_label tooltip => $1
3629 if $tip =~ /^#(.*)$/;
3373 3630
3374 if ($ENV{CFPLUS_DEBUG} & 2) { 3631 if ($ENV{CFPLUS_DEBUG} & 2) {
3375 $tooltip .= "\n\n" . (ref $widget) . "\n" 3632 $tip .= "\n\n" . (ref $widget) . "\n"
3376 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3633 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3377 . "req $widget->{req_w} $widget->{req_h}\n" 3634 . "req $widget->{req_w} $widget->{req_h}\n"
3378 . "visible $widget->{visible}"; 3635 . "visible $widget->{visible}";
3379 } 3636 }
3380 3637
3381 $tooltip =~ s/^\n+//; 3638 $tip =~ s/^\n+//;
3382 $tooltip =~ s/\n+$//; 3639 $tip =~ s/\n+$//;
3383 3640
3384 $self->add (new CFPlus::UI::Label 3641 $self->add (new DC::UI::Label
3642 fg => $DC::THEME{tooltip_fg},
3385 markup => $tooltip, 3643 markup => $tip,
3386 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3644 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3645 align => 0,
3387 fontsize => 0.8, 3646 fontsize => 0.8,
3388 style => 1, # FLAG_INVERSE 3647 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3389 ellipsise => 0, 3648 ellipsise => 0,
3390 font => ($widget->{tooltip_font} || $::FONT_PROP), 3649 font => ($widget->{tooltip_font} || $::FONT_PROP),
3391 ); 3650 );
3392} 3651}
3393 3652
3428} 3687}
3429 3688
3430sub _draw { 3689sub _draw {
3431 my ($self) = @_; 3690 my ($self) = @_;
3432 3691
3433 glTranslate 0.375, 0.375;
3434
3435 my ($w, $h) = @$self{qw(w h)}; 3692 my ($w, $h) = @$self{qw(w h)};
3436 3693
3437 glColor 1, 0.8, 0.4; 3694 glColor @{ $DC::THEME{tooltip_bg} };
3438 glBegin GL_QUADS; 3695 glRect 0, 0, $w, $h;
3439 glVertex 0 , 0;
3440 glVertex 0 , $h;
3441 glVertex $w, $h;
3442 glVertex $w, 0;
3443 glEnd;
3444 3696
3445 glColor 0, 0, 0; 3697 glColor @{ $DC::THEME{tooltip_border} };
3446 glBegin GL_LINE_LOOP; 3698 glRect_lineloop .5, .5, $w + .5, $h + .5;
3447 glVertex 0 , 0;
3448 glVertex 0 , $h;
3449 glVertex $w, $h;
3450 glVertex $w, 0;
3451 glEnd;
3452 3699
3453 glTranslate 2 - 0.375, 2 - 0.375; 3700 glTranslate 2, 2;
3454 3701
3455 $self->SUPER::_draw; 3702 $self->SUPER::_draw;
3456} 3703}
3457 3704
3458############################################################################# 3705#############################################################################
3459 3706
3460package CFPlus::UI::Face; 3707package DC::UI::Face;
3461 3708
3462our @ISA = CFPlus::UI::DrawBG::; 3709our @ISA = DC::UI::DrawBG::;
3463 3710
3464use CFPlus::OpenGL; 3711use DC::OpenGL;
3465 3712
3466sub new { 3713sub new {
3467 my $class = shift; 3714 my $class = shift;
3468 3715
3469 my $self = $class->SUPER::new ( 3716 my $self = $class->SUPER::new (
3473 can_events => 0, 3720 can_events => 0,
3474 @_, 3721 @_,
3475 ); 3722 );
3476 3723
3477 if ($self->{anim} && $self->{animspeed}) { 3724 if ($self->{anim} && $self->{animspeed}) {
3478 CFPlus::weaken (my $widget = $self); 3725 DC::weaken (my $widget = $self);
3479 3726
3480 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3727 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3481 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3728 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3482 $self->{timer} = Event->timer (
3483 parked => 1,
3484 cb => sub {
3485 return unless $::CONN && $widget; 3729 return unless $::CONN;
3486 3730
3731 my $w = $widget
3732 or return;
3733
3487 ++$widget->{frame}; 3734 ++$w->{frame};
3488 $widget->update_face; 3735 $w->update_face;
3736
3737 # somehow, $widget can go away
3489 $widget->update; 3738 $w->update;
3490
3491 $widget->update_timer; 3739 $w->update_timer;
3492 },
3493 ); 3740 };
3494 3741
3495 $self->update_face; 3742 $self->update_face;
3496 $self->update_timer; 3743 $self->update_timer;
3497 } 3744 }
3498 3745
3503 my ($self) = @_; 3750 my ($self) = @_;
3504 3751
3505 return unless $self->{timer}; 3752 return unless $self->{timer};
3506 3753
3507 if ($self->{visible}) { 3754 if ($self->{visible}) {
3508 $self->{timer}->at (
3509 $self->{anim_start}
3510 + $self->{animspeed}
3511 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3512 );
3513 $self->{timer}->start; 3755 $self->{timer}->start;
3514 } else { 3756 } else {
3515 $self->{timer}->stop; 3757 $self->{timer}->stop;
3516 } 3758 }
3517} 3759}
3518 3760
3519sub update_face { 3761sub update_face {
3520 my ($self) = @_; 3762 my ($self) = @_;
3521 3763
3522 return unless $::CONN;
3523
3524 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3525 if ($anim && @$anim) {
3526 delete $self->{wait_face};
3527 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3528 }
3529 }
3530}
3531
3532sub size_request {
3533 my ($self) = @_;
3534
3535 if ($::CONN) { 3764 if ($::CONN) {
3536 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3537 if (my $tex = $::CONN->{texture}[$faceid]) { 3766 if ($anim && @$anim) {
3538 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3539 } else { 3768 delete $self->{face_change_cb};
3540 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3769
3541 $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 }
3542 }); 3774 }
3543 } 3775 }
3544 } 3776 }
3545 } 3777 }
3778}
3779
3780sub size_request {
3781 my ($self) = @_;
3782
3783 if ($::CONN) {
3784 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3785 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3786 if ($tex->{name}) {
3787 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3788 } elsif (!$tex->{loading}) {
3789 $tex->upload (sub { $self->reconfigure });
3790 }
3791 }
3792
3793 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3794 }
3795 }
3546 3796
3547 ($self->{size_w} || 8, $self->{size_h} || 8) 3797 ($self->{size_w} || 8, $self->{size_h} || 8)
3548} 3798}
3549 3799
3550sub update { 3800sub update {
3564} 3814}
3565 3815
3566sub _draw { 3816sub _draw {
3567 my ($self) = @_; 3817 my ($self) = @_;
3568 3818
3569 return unless $::CONN;
3570
3571 $self->SUPER::_draw; 3819 $self->SUPER::_draw;
3572 3820
3573 my $faceid = $::CONN->{faceid}[$self->{face}] 3821 if (my $tex = $self->{tex}) {
3574 or return;
3575
3576 my $tex = $::CONN->{texture}[$faceid];
3577
3578 if ($tex) {
3579 glEnable GL_TEXTURE_2D; 3822 glEnable GL_TEXTURE_2D;
3580 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3823 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3581 glColor 0, 0, 0, 1; 3824 glColor 0, 0, 0, 1;
3582 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3825 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3583 glDisable GL_TEXTURE_2D; 3826 glDisable GL_TEXTURE_2D;
3593 $self->SUPER::destroy; 3836 $self->SUPER::destroy;
3594} 3837}
3595 3838
3596############################################################################# 3839#############################################################################
3597 3840
3598package CFPlus::UI::Buttonbar; 3841package DC::UI::Buttonbar;
3599 3842
3600our @ISA = CFPlus::UI::HBox::; 3843our @ISA = DC::UI::HBox::;
3601 3844
3602# TODO: should actually wrap buttons and other goodies. 3845# TODO: should actually wrap buttons and other goodies.
3603 3846
3604############################################################################# 3847#############################################################################
3605 3848
3606package CFPlus::UI::Menu; 3849package DC::UI::Menu;
3607 3850
3608our @ISA = CFPlus::UI::Toplevel::; 3851our @ISA = DC::UI::Toplevel::;
3609 3852
3610use CFPlus::OpenGL; 3853use DC::OpenGL;
3611 3854
3612sub new { 3855sub new {
3613 my $class = shift; 3856 my $class = shift;
3614 3857
3615 my $self = $class->SUPER::new ( 3858 my $self = $class->SUPER::new (
3616 items => [], 3859 items => [],
3617 z => 100, 3860 z => 100,
3618 @_, 3861 @_,
3619 ); 3862 );
3620 3863
3621 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3864 $self->add ($self->{vbox} = new DC::UI::VBox);
3622 3865
3623 for my $item (@{ $self->{items} }) { 3866 for my $item (@{ $self->{items} }) {
3624 my ($widget, $cb, $tooltip) = @$item; 3867 my ($widget, $cb, $tooltip) = @$item;
3625 3868
3626 # handle various types of items, only text for now 3869 # handle various types of items, only text for now
3627 if (!ref $widget) { 3870 if (!ref $widget) {
3628 if ($widget =~ /\t/) { 3871 if ($widget =~ /\t/) {
3629 my ($left, $right) = split /\t/, $widget, 2; 3872 my ($left, $right) = split /\t/, $widget, 2;
3630 3873
3631 $widget = new CFPlus::UI::HBox 3874 $widget = new DC::UI::HBox
3632 can_hover => 1, 3875 can_hover => 1,
3633 can_events => 1, 3876 can_events => 1,
3634 tooltip => $tooltip, 3877 tooltip => $tooltip,
3635 children => [ 3878 children => [
3636 (new CFPlus::UI::Label markup => $left, expand => 1), 3879 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3637 (new CFPlus::UI::Label markup => $right, align => +1), 3880 (new DC::UI::Label markup => $right, align => 1),
3638 ], 3881 ],
3639 ; 3882 ;
3640 3883
3641 } else { 3884 } else {
3642 $widget = new CFPlus::UI::Label 3885 $widget = new DC::UI::Label
3643 can_hover => 1, 3886 can_hover => 1,
3644 can_events => 1, 3887 can_events => 1,
3888 align => 0,
3645 markup => $widget, 3889 markup => $widget,
3646 tooltip => $tooltip; 3890 tooltip => $tooltip;
3647 } 3891 }
3648 } 3892 }
3649 3893
3665 $GRAB = $self; 3909 $GRAB = $self;
3666 $self->{button} = $ev->{button}; 3910 $self->{button} = $ev->{button};
3667 3911
3668 $self->show; 3912 $self->show;
3669 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3913 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
3914
3915 1 # so it can be used inside event handlers
3670} 3916}
3671 3917
3672sub invoke_mouse_motion { 3918sub invoke_mouse_motion {
3673 my ($self, $ev, $x, $y) = @_; 3919 my ($self, $ev, $x, $y) = @_;
3674 3920
3695 1 3941 1
3696} 3942}
3697 3943
3698############################################################################# 3944#############################################################################
3699 3945
3700package CFPlus::UI::Multiplexer; 3946package DC::UI::Multiplexer;
3701 3947
3702our @ISA = CFPlus::UI::Container::; 3948our @ISA = DC::UI::Container::;
3703 3949
3704sub new { 3950sub new {
3705 my $class = shift; 3951 my $class = shift;
3706 3952
3707 my $self = $class->SUPER::new ( 3953 my $self = $class->SUPER::new (
3708 @_, 3954 @_,
3709 ); 3955 );
3710 3956
3711 $self->{current} = $self->{children}[0] 3957 $self->set_current_page (0);
3712 if @{ $self->{children} };
3713 3958
3714 $self 3959 $self
3715} 3960}
3716 3961
3717sub add { 3962sub add {
3718 my ($self, @widgets) = @_; 3963 my ($self, @widgets) = @_;
3719 3964
3720 $self->SUPER::add (@widgets); 3965 $self->SUPER::add (@widgets);
3721 3966
3722 $self->{current} = $self->{children}[0] 3967 $self->set_current_page (0)
3723 if @{ $self->{children} }; 3968 if @widgets == @{ $self->{children} };
3724} 3969}
3725 3970
3726sub get_current_page { 3971sub get_current_page {
3727 my ($self) = @_; 3972 my ($self) = @_;
3728 3973
3734 3979
3735 my $widget = ref $page_or_widget 3980 my $widget = ref $page_or_widget
3736 ? $page_or_widget 3981 ? $page_or_widget
3737 : $self->{children}[$page_or_widget]; 3982 : $self->{children}[$page_or_widget];
3738 3983
3984 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3985
3739 $self->{current} = $widget; 3986 if (($self->{current} = $widget)) {
3987 $self->{current}->set_visible if $self->{current} && $self->{visible};
3740 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3988 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3741 3989
3742 $self->emit (page_changed => $self->{current}); 3990 $self->emit (page_changed => $self->{current});
3991 }
3743 3992
3744 $self->realloc; 3993 $self->realloc;
3745} 3994}
3746 3995
3747sub visible_children { 3996sub visible_children {
3748 $_[0]{current} 3997 $_[0]{current} || ()
3749} 3998}
3750 3999
3751sub size_request { 4000sub size_request {
3752 my ($self) = @_; 4001 my ($self) = @_;
3753 4002
4003 $self->{current}
3754 $self->{current}->size_request 4004 ? $self->{current}->size_request
4005 : (0, 0)
3755} 4006}
3756 4007
3757sub invoke_size_allocate { 4008sub invoke_size_allocate {
3758 my ($self, $w, $h) = @_; 4009 my ($self, $w, $h) = @_;
3759 4010
3760 $self->{current}->configure (0, 0, $w, $h); 4011 $self->{current}->configure (0, 0, $w, $h)
4012 if $self->{current};
3761 4013
3762 1 4014 1
3763} 4015}
3764 4016
3765sub _draw { 4017sub _draw {
3766 my ($self) = @_; 4018 my ($self) = @_;
3767 4019
3768 $self->{current}->draw; 4020 $self->{current}->draw
4021 if $self->{current};
3769} 4022}
3770 4023
3771############################################################################# 4024#############################################################################
3772 4025
3773package CFPlus::UI::Notebook; 4026package DC::UI::Notebook;
3774 4027
4028use DC::OpenGL;
4029
3775our @ISA = CFPlus::UI::VBox::; 4030our @ISA = DC::UI::VBox::;
3776 4031
3777sub new { 4032sub new {
3778 my $class = shift; 4033 my $class = shift;
3779 4034
3780 my $self = $class->SUPER::new ( 4035 my $self = $class->SUPER::new (
3781 buttonbar => (new CFPlus::UI::Buttonbar), 4036 buttonbar => (new DC::UI::Buttonbar),
3782 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4037 multiplexer => (new DC::UI::Multiplexer expand => 1),
4038 active_outline => [.7, .7, 0.2],
3783 # filter => # will be put between multiplexer and $self 4039 # filter => # will be put between multiplexer and $self
3784 @_, 4040 @_,
3785 ); 4041 );
3786 4042
3787 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4043 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3788 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4044 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3789 4045
4046 {
4047 Scalar::Util::weaken (my $wself = $self);
4048
4049 $self->{multiplexer}->connect (c_add => sub {
4050 my ($mplex, $widgets) = @_;
4051
4052 for my $child (@$widgets) {
4053 Scalar::Util::weaken $child;
4054 $child->{c_tab_} ||= do {
4055 my $tab =
4056 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4057 ? $child->{c_tab}
4058 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4059
4060 $tab->connect (activate => sub {
4061 $wself->set_current_page ($child);
4062 });
4063
4064 $tab
4065 };
4066
4067 $self->{buttonbar}->add ($child->{c_tab_});
4068 }
4069 });
4070
4071 $self->{multiplexer}->connect (c_remove => sub {
4072 my ($mplex, $widgets) = @_;
4073
4074 for my $child (@$widgets) {
4075 $wself->{buttonbar}->remove ($child->{c_tab_});
4076 }
4077 });
4078 }
4079
3790 $self 4080 $self
3791} 4081}
3792 4082
3793sub add { 4083sub add {
4084 my ($self, @widgets) = @_;
4085
4086 $self->{multiplexer}->add (@widgets)
4087}
4088
4089sub remove {
4090 my ($self, @widgets) = @_;
4091
4092 $self->{multiplexer}->remove (@widgets)
4093}
4094
4095sub pages {
4096 my ($self) = @_;
4097 $self->{multiplexer}->children
4098}
4099
4100sub page_index {
4101 my ($self, $widget) = @_;
4102
4103 my $i = 0;
4104 for ($self->pages) {
4105 if ($_ eq $widget) { return $i };
4106 $i++;
4107 }
4108
4109 undef
4110}
4111
4112sub add_tab {
3794 my ($self, $title, $widget, $tooltip) = @_; 4113 my ($self, $title, $widget, $tooltip) = @_;
3795 4114
3796 CFPlus::weaken $self; 4115 $title = [$title, $tooltip] unless ref $title;
4116 $widget->{c_tab} = $title;
3797 4117
3798 $self->{buttonbar}->add (new CFPlus::UI::Button
3799 markup => $title,
3800 tooltip => $tooltip,
3801 on_activate => sub { $self->set_current_page ($widget) },
3802 );
3803
3804 $self->{multiplexer}->add ($widget); 4118 $self->add ($widget);
3805} 4119}
3806 4120
3807sub get_current_page { 4121sub get_current_page {
3808 my ($self) = @_; 4122 my ($self) = @_;
3809 4123
3815 4129
3816 $self->{multiplexer}->set_current_page ($page); 4130 $self->{multiplexer}->set_current_page ($page);
3817 $self->emit (page_changed => $self->{multiplexer}{current}); 4131 $self->emit (page_changed => $self->{multiplexer}{current});
3818} 4132}
3819 4133
4134sub _draw {
4135 my ($self) = @_;
4136
4137 $self->SUPER::_draw ();
4138
4139 if (my $cur = $self->{multiplexer}{current}) {
4140 if ($cur = $cur->{c_tab_}) {
4141 glTranslate $self->{buttonbar}{x} + $cur->{x},
4142 $self->{buttonbar}{y} + $cur->{y};
4143 glLineWidth 3;
4144 #glEnable GL_BLEND;
4145 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4146 glColor @{$self->{active_outline}};
4147 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4148 glLineWidth 1;
4149 #glDisable GL_BLEND;
4150 }
4151 }
4152}
4153
3820############################################################################# 4154#############################################################################
3821 4155
3822package CFPlus::UI::Selector; 4156package DC::UI::Selector;
3823 4157
3824use utf8; 4158use utf8;
3825 4159
3826our @ISA = CFPlus::UI::Button::; 4160our @ISA = DC::UI::Button::;
3827 4161
3828sub new { 4162sub new {
3829 my $class = shift; 4163 my $class = shift;
3830 4164
3831 my $self = $class->SUPER::new ( 4165 my $self = $class->SUPER::new (
3848 my ($value, $title, $tooltip) = @$_; 4182 my ($value, $title, $tooltip) = @$_;
3849 4183
3850 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4184 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3851 } 4185 }
3852 4186
3853 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4187 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3854} 4188}
3855 4189
3856sub _set_value { 4190sub _set_value {
3857 my ($self, $value) = @_; 4191 my ($self, $value) = @_;
3858 4192
3859 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4193 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4194 $item ||= $self->{options}[0]
3860 or return; 4195 or return;
3861 4196
3862 $self->{value} = $item->[0]; 4197 $self->{value} = $item->[0];
3863 $self->set_markup ("$item->[1] ⇓"); 4198 $self->set_markup ("$item->[1] ⇓");
3864 $self->set_tooltip ($item->[2]); 4199# $self->set_tooltip ($item->[2]);
3865} 4200}
3866 4201
3867sub set_value { 4202sub set_value {
3868 my ($self, $value) = @_; 4203 my ($self, $value) = @_;
3869 4204
3871 4206
3872 $self->_set_value ($value); 4207 $self->_set_value ($value);
3873 $self->emit (changed => $value); 4208 $self->emit (changed => $value);
3874} 4209}
3875 4210
4211sub set_options {
4212 my ($self, $options) = @_;
4213
4214 $self->{options} = $options;
4215 $self->_set_value ($self->{value});
4216}
4217
3876############################################################################# 4218#############################################################################
3877 4219
3878package CFPlus::UI::Statusbox; 4220package DC::UI::Statusbox;
3879 4221
3880our @ISA = CFPlus::UI::VBox::; 4222our @ISA = DC::UI::VBox::;
3881 4223
3882sub new { 4224sub new {
3883 my $class = shift; 4225 my $class = shift;
3884 4226
3885 my $self = $class->SUPER::new ( 4227 my $self = $class->SUPER::new (
3886 fontsize => 0.8, 4228 fontsize => 0.8,
3887 @_, 4229 @_,
3888 ); 4230 );
3889 4231
3890 CFPlus::weaken (my $this = $self); 4232 DC::weaken (my $this = $self);
3891 4233
3892 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4234 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3893 4235
3894 $self 4236 $self
3895} 4237}
3896 4238
3897sub reorder { 4239sub reorder {
3898 my ($self) = @_; 4240 my ($self) = @_;
3899 my $NOW = Time::HiRes::time; 4241 my $NOW = EV::time;
3900 4242
3901 # freeze display when hovering over any label 4243 # freeze display when hovering over any label
3902 return if $CFPlus::UI::TOOLTIP->{owner} 4244 return if $DC::UI::TOOLTIP->{owner}
3903 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4245 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3904 values %{ $self->{item} }; 4246 values %{ $self->{item} };
3905 4247
3906 while (my ($k, $v) = each %{ $self->{item} }) { 4248 while (my ($k, $v) = each %{ $self->{item} }) {
3907 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4249 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3908 } 4250 }
4251
4252 $self->{timer}->set (1, 1);
3909 4253
3910 my @widgets; 4254 my @widgets;
3911 4255
3912 my @items = sort { 4256 my @items = sort {
3913 $a->{pri} <=> $b->{pri} 4257 $a->{pri} <=> $b->{pri}
3914 or $b->{id} <=> $a->{id} 4258 or $b->{id} <=> $a->{id}
3915 } values %{ $self->{item} }; 4259 } values %{ $self->{item} };
3916
3917 $self->{timer}->interval (1);
3918 4260
3919 my $count = 10 + 1; 4261 my $count = 10 + 1;
3920 for my $item (@items) { 4262 for my $item (@items) {
3921 last unless --$count; 4263 last unless --$count;
3922 4264
3929 for ($short) { 4271 for ($short) {
3930 s/^\s+//; 4272 s/^\s+//;
3931 s/\s+/ /g; 4273 s/\s+/ /g;
3932 } 4274 }
3933 4275
3934 new CFPlus::UI::Label 4276 new DC::UI::Label
3935 markup => $short, 4277 markup => $short,
3936 tooltip => $item->{tooltip}, 4278 tooltip => $item->{tooltip},
3937 tooltip_font => $::FONT_PROP, 4279 tooltip_font => $::FONT_PROP,
3938 tooltip_width => 0.67, 4280 tooltip_width => 0.67,
3939 fontsize => $item->{fontsize} || $self->{fontsize}, 4281 fontsize => $item->{fontsize} || $self->{fontsize},
3940 max_w => $::WIDTH * 0.44, 4282 max_w => $::WIDTH * 0.44,
4283 align => 0,
3941 fg => [@{ $item->{fg} }], 4284 fg => [@{ $item->{fg} }],
3942 can_events => 1, 4285 can_events => 1,
3943 can_hover => 1 4286 can_hover => 1
3944 }; 4287 };
3945 4288
3946 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4289 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3947 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4290 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3948 $label->update; 4291 $label->update;
3949 $label->set_max_size (undef, $label->{req_h} * $diff) 4292 $label->set_max_size (undef, $label->{req_h} * $diff)
3950 if $diff < 1; 4293 if $diff < 1;
3951 $self->{timer}->interval (1/30); 4294 $self->{timer}->set (1/30, 1/30);
3952 } else { 4295 } else {
3953 $label->{fg}[3] = $item->{fg}[3] || 1; 4296 $label->{fg}[3] = $item->{fg}[3] || 1;
3954 } 4297 }
3955 4298
3956 push @widgets, $label; 4299 push @widgets, $label;
3957 } 4300 }
4301
4302 my $hash = join ",", @widgets;
4303 return if $hash eq $self->{last_widget_hash};
4304 $self->{last_widget_hash} = $hash;
3958 4305
3959 $self->clear; 4306 $self->clear;
3960 $self->SUPER::add (reverse @widgets); 4307 $self->SUPER::add (reverse @widgets);
3961} 4308}
3962 4309
4018 $self->SUPER::destroy; 4365 $self->SUPER::destroy;
4019} 4366}
4020 4367
4021############################################################################# 4368#############################################################################
4022 4369
4023package CFPlus::UI::Root; 4370package DC::UI::Root;
4024 4371
4025our @ISA = CFPlus::UI::Container::; 4372our @ISA = DC::UI::Container::;
4026 4373
4027use List::Util qw(min max); 4374use List::Util qw(min max);
4028 4375
4029use CFPlus::OpenGL; 4376use DC::OpenGL;
4030 4377
4031sub new { 4378sub new {
4032 my $class = shift; 4379 my $class = shift;
4033 4380
4034 my $self = $class->SUPER::new ( 4381 my $self = $class->SUPER::new (
4035 visible => 1, 4382 visible => 1,
4036 @_, 4383 @_,
4037 ); 4384 );
4038 4385
4039 CFPlus::weaken ($self->{root} = $self); 4386 DC::weaken ($self->{root} = $self);
4040 4387
4041 $self 4388 $self
4042} 4389}
4043 4390
4044sub size_request { 4391sub size_request {
4092} 4439}
4093 4440
4094sub update { 4441sub update {
4095 my ($self) = @_; 4442 my ($self) = @_;
4096 4443
4097 $::WANT_REFRESH++; 4444 $::WANT_REFRESH = 1;
4098} 4445}
4099 4446
4100sub add { 4447sub add {
4101 my ($self, @children) = @_; 4448 my ($self, @children) = @_;
4102 4449
4139 while ($self->{refresh_hook}) { 4486 while ($self->{refresh_hook}) {
4140 $_->() 4487 $_->()
4141 for values %{delete $self->{refresh_hook}}; 4488 for values %{delete $self->{refresh_hook}};
4142 } 4489 }
4143 4490
4144 if ($self->{realloc}) { 4491 while ($self->{realloc}) {
4145 my %queue; 4492 my %queue;
4146 my @queue; 4493 my @queue;
4147 my $widget; 4494 my $widget;
4148 4495
4149 outer: 4496 outer:
4169 4516
4170 delete $queue{$widget+0}; 4517 delete $queue{$widget+0};
4171 4518
4172 my ($w, $h) = $widget->size_request; 4519 my ($w, $h) = $widget->size_request;
4173 4520
4174 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4521 $w += $widget->{padding_x} * 2;
4175 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4522 $h += $widget->{padding_y} * 2;
4523
4524 $w = max $widget->{min_w}, $w;
4525 $h = max $widget->{min_h}, $h;
4176 4526
4177 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4527 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4178 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4528 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4179 4529
4180 $w = $widget->{force_w} if exists $widget->{force_w}; 4530 $w = $widget->{force_w} if exists $widget->{force_w};
4196 } 4546 }
4197 } 4547 }
4198 4548
4199 delete $self->{realloc}{$widget+0}; 4549 delete $self->{realloc}{$widget+0};
4200 } 4550 }
4201 }
4202 4551
4203 while (my $size_alloc = delete $self->{size_alloc}) { 4552 while (my $size_alloc = delete $self->{size_alloc}) {
4204 my @queue = sort { $b->{visible} <=> $a->{visible} } 4553 my @queue = sort { $a->{visible} <=> $b->{visible} }
4205 values %$size_alloc; 4554 values %$size_alloc;
4206 4555
4207 while () { 4556 while () {
4208 my $widget = pop @queue || last; 4557 my $widget = pop @queue || last;
4209 4558
4210 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4559 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4211 4560
4212 $w = 0 if $w < 0;
4213 $h = 0 if $h < 0;
4214
4215 $w = max $widget->{min_w}, $w; 4561 $w = max $widget->{min_w}, $w;
4216 $h = max $widget->{min_h}, $h; 4562 $h = max $widget->{min_h}, $h;
4217 4563
4218# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4564# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4219# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4565# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4220 4566
4221 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4567 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4222 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4568 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4223 4569
4224 $w = int $w + 0.5; 4570 $w = int $w + 0.5;
4225 $h = int $h + 0.5; 4571 $h = int $h + 0.5;
4226 4572
4227 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4573 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4228 $widget->{old_w} = $widget->{w}; 4574 $widget->{old_w} = $widget->{w};
4229 $widget->{old_h} = $widget->{h}; 4575 $widget->{old_h} = $widget->{h};
4230 4576
4231 $widget->{w} = $w; 4577 $widget->{w} = $w;
4232 $widget->{h} = $h; 4578 $widget->{h} = $h;
4233 4579
4234 $widget->emit (size_allocate => $w, $h); 4580 $widget->emit (size_allocate => $w, $h);
4581 }
4235 } 4582 }
4236 } 4583 }
4237 } 4584 }
4238 4585
4239 while ($self->{post_alloc_hook}) { 4586 while ($self->{post_alloc_hook}) {
4250 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4597 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4251 glMatrixMode GL_MODELVIEW; 4598 glMatrixMode GL_MODELVIEW;
4252 glLoadIdentity; 4599 glLoadIdentity;
4253 4600
4254 { 4601 {
4255 package CFPlus::UI::Base; 4602 package DC::UI::Base;
4256 4603
4257 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4604 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4258 (0, 0, $self->{w}, $self->{h}); 4605 (0, 0, $self->{w}, $self->{h});
4259 4606
4260 $self->_draw; 4607 $self->_draw;
4261 } 4608 }
4262} 4609}
4263 4610
4264############################################################################# 4611#############################################################################
4265 4612
4266package CFPlus::UI; 4613package DC::UI;
4267 4614
4268$ROOT = new CFPlus::UI::Root; 4615$ROOT = new DC::UI::Root;
4269$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4616$TOOLTIP = new DC::UI::Tooltip z => 900;
4270 4617
42711 46181
4272

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines