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.371 by root, Sun Jul 15 22:07:48 2007 UTC vs.
Revision 1.476 by root, Sun Jan 11 23:45:52 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 Guard ();
9
10use DC;
10use CFPlus::Pod; 11use DC::Pod;
11use CFPlus::Texture; 12use DC::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
17our $TOOLTIP; 18our $TOOLTIP;
18our $BUTTON_STATE; 19our $BUTTON_STATE;
19 20
20our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
21 22
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
24 $_[0]->stop;
25
23 if (!$GRAB) { 26 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 29 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 30 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 33 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 34 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 35
33 return if $ENV{CFPLUS_DEBUG} & 8; 36 return if $ENV{CFPLUS_DEBUG} & 8;
34 37
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 38 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 39 $TOOLTIP->show;
41 } 40 }
42 41
43 return; 42 return;
46 } 45 }
47 46
48 $TOOLTIP->hide; 47 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 48 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 49 delete $TOOLTIP->{owner};
51}); 50};
52 51
53sub get_layout { 52sub get_layout {
54 my $layout; 53 my $layout;
55 54
56 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
92 91
93 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 94
96 $TOOLTIP_WATCHER->start; 95 $TOOLTIP_WATCHER->again;
97 } 96 }
98} 97}
99 98
100sub feed_sdl_button_down_event { 99sub feed_sdl_button_down_event {
101 my ($ev) = @_; 100 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 106 my $widget = $ROOT->find_widget ($x, $y);
108 107
109 $GRAB = $widget; 108 $GRAB = $widget;
110 $GRAB->update if $GRAB; 109 $GRAB->update if $GRAB;
111 110
112 $TOOLTIP_WATCHER->cb->(); 111 $TOOLTIP_WATCHER->invoke;
113 } 112 }
114 113
115 if ($GRAB) { 114 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 116 # mousewheel
118 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
119
120 $ev->{dx} = $shift ? $delta : 0;
121 $ev->{dy} = $shift ? 0 : $delta;
122
120 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
121 } else { 124 } else {
122 $GRAB->emit (button_down => $ev) 125 $GRAB->emit (button_down => $ev)
123 } 126 }
124 } 127 }
138 my $grab = $GRAB; undef $GRAB; 141 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 142 $grab->update if $grab;
140 $GRAB->update if $GRAB; 143 $GRAB->update if $GRAB;
141 144
142 check_hover $widget; 145 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 146 $TOOLTIP_WATCHER->invoke;
144 } 147 }
145} 148}
146 149
147sub feed_sdl_motion_event { 150sub feed_sdl_motion_event {
148 my ($ev) = @_; 151 my ($ev) = @_;
188# call when resolution changes etc. 191# call when resolution changes etc.
189sub rescale_widgets { 192sub rescale_widgets {
190 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
191 194
192 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 199
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 213 reconfigure_widgets;
211} 214}
212 215
213############################################################################# 216#############################################################################
214 217
215package CFPlus::UI::Event; 218package DC::UI::Event;
216 219
217sub xy { 220sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 222}
220 223
221############################################################################# 224#############################################################################
222 225
223package CFPlus::UI::Base; 226package DC::UI::Base;
224 227
225use strict; 228use strict;
226 229
227use CFPlus::OpenGL; 230use DC::OpenGL;
228 231
229sub new { 232sub new {
230 my $class = shift; 233 my $class = shift;
231 234
232 my $self = bless { 235 my $self = bless {
237 h => undef, 240 h => undef,
238 can_events => 1, 241 can_events => 1,
239 @_ 242 @_
240 }, $class; 243 }, $class;
241 244
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 246
244 for (keys %$self) { 247 for (keys %$self) {
245 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
247 } 250 }
248 } 251 }
249 252
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 258
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 261
259 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
269 $self->emit ("destroy"); 272 $self->emit ("destroy");
270 %$self = (); 273 %$self = ();
271} 274}
272 275
273sub TO_JSON { 276sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} } 277 { "\fw" => $_[0]{s_id} }
275} 278}
276 279
277sub show { 280sub show {
278 my ($self) = @_; 281 my ($self) = @_;
279 282
280 return if $self->{parent}; 283 return if $self->{parent};
281 284
282 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
283} 286}
284 287
285sub set_visible { 288sub set_visible {
286 my ($self) = @_; 289 my ($self) = @_;
287 290
288 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 295
290 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
292 298
293 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
294 300
295 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
296 302
297 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
298} 304}
299 305
300sub set_invisible { 306sub set_invisible {
301 my ($self) = @_; 307 my ($self) = @_;
302 308
308 delete $self->{root}; 314 delete $self->{root};
309 315
310 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
312 318
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 319 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
315 321
316 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
318} 324}
337sub hide { 343sub hide {
338 my ($self) = @_; 344 my ($self) = @_;
339 345
340 $self->set_invisible; 346 $self->set_invisible;
341 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 351 $parent->remove ($self);
343 if $self->{parent}; 352 }
344} 353}
345 354
346sub move_abs { 355sub move_abs {
347 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
348 357
360 $self->{force_h} = $h; 369 $self->{force_h} = $h;
361 370
362 $self->realloc; 371 $self->realloc;
363} 372}
364 373
374# traverse the widget chain up to find the maximum "physical" size constraints
375sub get_max_wh {
376 my ($self) = @_;
377
378 my ($w, $h) = @$self{qw(max_w max_h)};
379
380 if ($w <= 0 || $h <= 0) {
381 my ($mw, $mh) = $self->{parent}
382 ? $self->{parent}->get_max_wh
383 : ($::WIDTH, $::HEIGHT);
384
385 $w = $mw if $w <= 0;
386 $h = $mh if $h <= 0;
387 }
388
389 ($w, $h)
390}
391
365sub size_request { 392sub size_request {
366 require Carp; 393 require Carp;
367 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
368} 395}
369 396
375 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
376 403
377 if ($self->{aspect}) { 404 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
379 406
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 409
383 # use alignment to adjust x, y 410 # use alignment to adjust x, y
384 411
385 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
427 454
428 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
429 456
430 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
431 458
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 462 }
436} 463}
437 464
438# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
439sub coord2local { 466sub coord2local {
440 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
441 468
442 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
443 470
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 472}
446 473
447# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
448sub coord2global { 475sub coord2global {
449 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
450 477
451 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
452 479
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 481}
455 482
456sub invoke_focus_in { 483sub invoke_focus_in {
505sub connect { 532sub connect {
506 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
507 534
508 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
509 536
510 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
513 } 540 }
514} 541}
515 542
547 574
548 # parent 575 # parent
549 $self->{parent} && $self->{parent}->emit ($signal, @args) 576 $self->{parent} && $self->{parent}->emit ($signal, @args)
550} 577}
551 578
552sub find_widget { 579#sub find_widget {
553 my ($self, $x, $y) = @_; 580# in .xs
554
555 return () unless $self->{can_events};
556
557 return $self
558 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
559 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
560
561 ()
562}
563 581
564sub set_parent { 582sub set_parent {
565 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
566 584
567 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
568 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
569} 587}
570 588
571sub realloc { 589sub realloc {
572 my ($self) = @_; 590 my ($self) = @_;
598 616
599# using global variables seems a bit hacky, but passing through all drawing 617# using global variables seems a bit hacky, but passing through all drawing
600# functions seems pointless. 618# functions seems pointless.
601our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 619our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
602 620
603sub draw { 621#sub draw {
604 my ($self) = @_; 622#CFPlus.xs
605
606 return unless $self->{h} && $self->{w};
607
608 # update screen rectangle
609 local $draw_x = $draw_x + $self->{x};
610 local $draw_y = $draw_y + $self->{y};
611
612 # skip widgets that are entirely outside the drawing area
613 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
614 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
615
616 glPushMatrix;
617 glTranslate $self->{x}, $self->{y}, 0;
618
619 if ($self == $HOVER && $self->{can_hover}) {
620 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
621 glEnable GL_BLEND;
622 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
623 glBegin GL_QUADS;
624 glVertex 0 , 0;
625 glVertex $self->{w}, 0;
626 glVertex $self->{w}, $self->{h};
627 glVertex 0 , $self->{h};
628 glEnd;
629 glDisable GL_BLEND;
630 }
631
632 if ($ENV{CFPLUS_DEBUG} & 1) {
633 glPushMatrix;
634 glColor 1, 1, 0, 1;
635 glTranslate 0.375, 0.375;
636 glBegin GL_LINE_LOOP;
637 glVertex 0 , 0;
638 glVertex $self->{w} - 1, 0;
639 glVertex $self->{w} - 1, $self->{h} - 1;
640 glVertex 0 , $self->{h} - 1;
641 glEnd;
642 glPopMatrix;
643 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
644 }
645
646 $self->_draw;
647 glPopMatrix;
648}
649 623
650sub _draw { 624sub _draw {
651 my ($self) = @_; 625 my ($self) = @_;
652 626
653 warn "no draw defined for $self\n"; 627 warn "no draw defined for $self\n";
654} 628}
655 629
656my $cntx;#d#
657sub DESTROY { 630sub DESTROY {
658 my ($self) = @_; 631 my ($self) = @_;
659 632
660 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
661 634
635 local $@;
662 eval { $self->destroy }; 636 eval { $self->destroy };
663 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
664 638
665 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
666} 640}
667 641
668############################################################################# 642#############################################################################
669 643
670package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
671 645
672our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
673 647
674use strict; 648use strict;
675use CFPlus::OpenGL; 649use DC::OpenGL;
676 650
677sub new { 651sub new {
678 my $class = shift; 652 my $class = shift;
679
680 # range [value, low, high, page]
681 653
682 $class->SUPER::new ( 654 $class->SUPER::new (
683 #bg => [0, 0, 0, 0.2], 655 #bg => [0, 0, 0, 0.2],
684 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
685 @_ 657 @_
697 my ($w, $h) = @$self{qw(w h)}; 669 my ($w, $h) = @$self{qw(w h)};
698 670
699 glEnable GL_BLEND; 671 glEnable GL_BLEND;
700 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 672 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
701 glColor_premultiply @$color; 673 glColor_premultiply @$color;
702
703 glBegin GL_QUADS;
704 glVertex 0 , 0;
705 glVertex 0 , $h;
706 glVertex $w, $h; 674 glRect 0, 0, $w, $h;
707 glVertex $w, 0;
708 glEnd;
709
710 glDisable GL_BLEND; 675 glDisable GL_BLEND;
711 } 676 }
712} 677}
713 678
714############################################################################# 679#############################################################################
715 680
716package CFPlus::UI::Empty; 681package DC::UI::Empty;
717 682
718our @ISA = CFPlus::UI::Base::; 683our @ISA = DC::UI::Base::;
719 684
720sub new { 685sub new {
721 my ($class, %arg) = @_; 686 my ($class, %arg) = @_;
722 $class->SUPER::new (can_events => 0, %arg); 687 $class->SUPER::new (can_events => 0, %arg);
723} 688}
730 695
731sub draw { } 696sub draw { }
732 697
733############################################################################# 698#############################################################################
734 699
735package CFPlus::UI::Container; 700package DC::UI::Container;
736 701
737our @ISA = CFPlus::UI::Base::; 702our @ISA = DC::UI::Base::;
738 703
739sub new { 704sub new {
740 my ($class, %arg) = @_; 705 my ($class, %arg) = @_;
741 706
742 my $children = delete $arg{children}; 707 my $children = delete $arg{children};
746 can_events => 0, 711 can_events => 0,
747 %arg, 712 %arg,
748 ); 713 );
749 714
750 $self->add (@$children) 715 $self->add (@$children)
751 if $children; 716 if $children && @$children;
752 717
753 $self 718 $self
754} 719}
755 720
756sub realloc { 721sub realloc {
765 my ($self, @widgets) = @_; 730 my ($self, @widgets) = @_;
766 731
767 $_->set_parent ($self) 732 $_->set_parent ($self)
768 for @widgets; 733 for @widgets;
769 734
735 # TODO: only do this in widgets that need it, e.g. root, fixed
770 use sort 'stable'; 736 use sort 'stable';
771 737
772 $self->{children} = [ 738 $self->{children} = [
773 sort { $a->{z} <=> $b->{z} } 739 sort { $a->{z} <=> $b->{z} }
774 @{$self->{children}}, @widgets 740 @{$self->{children}}, @widgets
775 ]; 741 ];
776 742
777 $self->realloc; 743 $self->realloc;
744
745 $self->emit (c_add => \@widgets);
746
747 map $_+0, @widgets
778} 748}
779 749
780sub children { 750sub children {
781 @{ $_[0]{children} } 751 @{ $_[0]{children} }
782} 752}
783 753
784sub remove { 754sub remove {
785 my ($self, $child) = @_; 755 my ($self, @widgets) = @_;
786 756
757 $self->emit (c_remove => \@widgets);
758
759 for my $child (@widgets) {
787 delete $child->{parent}; 760 delete $child->{parent};
788 $child->hide; 761 $child->hide;
789
790 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 762 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
763 }
791 764
792 $self->realloc; 765 $self->realloc;
793} 766}
794 767
795sub clear { 768sub clear {
796 my ($self) = @_; 769 my ($self) = @_;
797 770
798 my $children = delete $self->{children}; 771 my $children = $self->{children};
799 $self->{children} = []; 772 $self->{children} = [];
800 773
801 for (@$children) { 774 for (@$children) {
802 delete $_->{parent}; 775 delete $_->{parent};
803 $_->hide; 776 $_->hide;
823} 796}
824 797
825sub _draw { 798sub _draw {
826 my ($self) = @_; 799 my ($self) = @_;
827 800
828 $_->draw for @{$self->{children}}; 801 $_->draw for $self->visible_children;
829} 802}
830 803
831############################################################################# 804#############################################################################
832 805
833package CFPlus::UI::Bin; 806package DC::UI::Bin;
834 807
835our @ISA = CFPlus::UI::Container::; 808our @ISA = DC::UI::Container::;
836 809
837sub new { 810sub new {
838 my ($class, %arg) = @_; 811 my ($class, %arg) = @_;
839 812
840 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 813 my $child = (delete $arg{child}) || new DC::UI::Empty::;
841 814
842 $class->SUPER::new (children => [$child], %arg) 815 $class->SUPER::new (children => [$child], %arg)
843} 816}
844 817
845sub add { 818sub add {
846 my ($self, $child) = @_; 819 my ($self, $child) = @_;
847 820
848 $self->SUPER::remove ($_) for @{ $self->{children} }; 821 $self->clear;
849 $self->SUPER::add ($child); 822 $self->SUPER::add ($child);
850} 823}
851 824
852sub remove { 825sub remove {
853 my ($self, $widget) = @_; 826 my ($self, $widget) = @_;
854 827
855 $self->SUPER::remove ($widget); 828 $self->SUPER::remove ($widget);
856 829
857 $self->{children} = [new CFPlus::UI::Empty] 830 $self->{children} = [new DC::UI::Empty]
858 unless @{$self->{children}}; 831 unless @{$self->{children}};
859} 832}
860 833
861sub child { $_[0]->{children}[0] } 834sub child { $_[0]->{children}[0] }
862 835
871 844
872 1 845 1
873} 846}
874 847
875############################################################################# 848#############################################################################
876
877# back-buffered drawing area 849# back-buffered drawing area
878 850
879package CFPlus::UI::Window; 851package DC::UI::Window;
880 852
881our @ISA = CFPlus::UI::Bin::; 853our @ISA = DC::UI::Bin::;
882 854
883use CFPlus::OpenGL; 855use DC::OpenGL;
884 856
885sub new { 857sub new {
886 my ($class, %arg) = @_; 858 my ($class, %arg) = @_;
887 859
888 my $self = $class->SUPER::new (%arg); 860 my $self = $class->SUPER::new (%arg);
910} 882}
911 883
912sub render_child { 884sub render_child {
913 my ($self) = @_; 885 my ($self) = @_;
914 886
915 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 887 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
916 glClearColor 0, 0, 0, 0; 888 glClearColor 0, 0, 0, 0;
917 glClear GL_COLOR_BUFFER_BIT; 889 glClear GL_COLOR_BUFFER_BIT;
918 890
919 { 891 {
920 package CFPlus::UI::Base; 892 package DC::UI::Base;
921 893
922 local ($draw_x, $draw_y, $draw_w, $draw_h) = 894 local ($draw_x, $draw_y, $draw_w, $draw_h) =
923 (0, 0, $self->{w}, $self->{h}); 895 (0, 0, $self->{w}, $self->{h});
924 896
925 $self->_render; 897 $self->_render;
942 glDisable GL_TEXTURE_2D; 914 glDisable GL_TEXTURE_2D;
943} 915}
944 916
945############################################################################# 917#############################################################################
946 918
947package CFPlus::UI::ViewPort; 919package DC::UI::ViewPort;
948 920
921use List::Util qw(min max);
922
949our @ISA = CFPlus::UI::Window::; 923our @ISA = DC::UI::Window::;
950 924
951sub new { 925sub new {
952 my $class = shift; 926 my $class = shift;
953 927
954 $class->SUPER::new ( 928 $class->SUPER::new (
961sub size_request { 935sub size_request {
962 my ($self) = @_; 936 my ($self) = @_;
963 937
964 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 938 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
965 939
966 $w = 10 if $self->{scroll_x}; 940 $w = 1 if $self->{scroll_x};
967 $h = 10 if $self->{scroll_y}; 941 $h = 1 if $self->{scroll_y};
968 942
969 ($w, $h) 943 ($w, $h)
970} 944}
971 945
972sub invoke_size_allocate { 946sub invoke_size_allocate {
984} 958}
985 959
986sub set_offset { 960sub set_offset {
987 my ($self, $x, $y) = @_; 961 my ($self, $x, $y) = @_;
988 962
963 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
964 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
965
966 if ($x != $self->{view_x} or $y != $self->{view_y}) {
989 $self->{view_x} = int $x; 967 $self->{view_x} = $x;
990 $self->{view_y} = int $y; 968 $self->{view_y} = $y;
991 969
970 $self->emit (changed => $x, $y);
992 $self->update; 971 $self->update;
972 }
973}
974
975sub set_center {
976 my ($self, $x, $y) = @_;
977
978 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
979}
980
981sub make_visible {
982 my ($self, $x, $y, $border) = @_;
983
984 if ( $x < $self->{view_x} + $self->{w} * $border
985 || $x > $self->{view_x} + $self->{w} * (1 - $border)
986 || $y < $self->{view_y} + $self->{h} * $border
987 || $y > $self->{view_y} + $self->{h} * (1 - $border)
988 ) {
989 $self->set_center ($x, $y);
990 }
993} 991}
994 992
995# hmm, this does not work for topleft of $self... but we should not ask for that 993# hmm, this does not work for topleft of $self... but we should not ask for that
996sub coord2local { 994sub coord2local {
997 my ($self, $x, $y) = @_; 995 my ($self, $x, $y) = @_;
1012 my ($self, $x, $y) = @_; 1010 my ($self, $x, $y) = @_;
1013 1011
1014 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1012 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1015 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1013 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1016 ) { 1014 ) {
1017 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1015 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1018 } else { 1016 } else {
1019 $self->CFPlus::UI::Base::find_widget ($x, $y) 1017 $self->DC::UI::Base::find_widget ($x, $y)
1020 } 1018 }
1021} 1019}
1022 1020
1023sub _render { 1021sub _render {
1024 my ($self) = @_; 1022 my ($self) = @_;
1025 1023
1026 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1024 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1027 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1025 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1028 1026
1029 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1027 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1030 1028
1031 $self->SUPER::_render; 1029 $self->SUPER::_render;
1032} 1030}
1033 1031
1034############################################################################# 1032#############################################################################
1035 1033
1036package CFPlus::UI::ScrolledWindow; 1034package DC::UI::ScrolledWindow;
1037 1035
1038our @ISA = CFPlus::UI::Table::; 1036our @ISA = DC::UI::Table::;
1039 1037
1040sub new { 1038sub new {
1041 my ($class, %arg) = @_; 1039 my ($class, %arg) = @_;
1042 1040
1043 my $child = delete $arg{child}; 1041 my $child = delete $arg{child};
1044 1042
1045 my $self; 1043 my $self;
1046 1044
1047 my $hslider = new CFPlus::UI::Slider 1045 my $hslider = new DC::UI::Slider
1046 c_col => 0,
1047 c_row => 1,
1048 vertical => 0, 1048 vertical => 0,
1049 range => [0, 0, 1, 0.01], # HACK fix 1049 range => [0, 0, 1, 0.01], # HACK fix
1050 on_changed => sub { 1050 on_changed => sub {
1051 $self->{hpos} = $_[1]; 1051 $self->{hpos} = $_[1];
1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1053 }, 1053 },
1054 ; 1054 ;
1055 1055
1056 my $vslider = new CFPlus::UI::Slider 1056 my $vslider = new DC::UI::Slider
1057 c_col => 1,
1058 c_row => 0,
1057 vertical => 1, 1059 vertical => 1,
1058 range => [0, 0, 1, 0.01], # HACK fix 1060 range => [0, 0, 1, 0.01], # HACK fix
1059 on_changed => sub { 1061 on_changed => sub {
1060 $self->{vpos} = $_[1]; 1062 $self->{vpos} = $_[1];
1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1063 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1062 }, 1064 },
1063 ; 1065 ;
1064 1066
1065 $self = $class->SUPER::new ( 1067 $self = $class->SUPER::new (
1066 vp => (new CFPlus::UI::ViewPort expand => 1), 1068 scroll_x => 0,
1069 scroll_y => 1,
1067 can_events => 1, 1070 can_events => 1,
1068 hslider => $hslider, 1071 hslider => $hslider,
1069 vslider => $vslider, 1072 vslider => $vslider,
1070 col_expand => [1, 0], 1073 col_expand => [1, 0],
1071 row_expand => [1, 0], 1074 row_expand => [1, 0],
1072 %arg, 1075 %arg,
1073 ); 1076 );
1074 1077
1078 $self->{vp} = new DC::UI::ViewPort
1079 c_col => 0,
1080 c_row => 0,
1081 expand => 1,
1082 scroll_x => $self->{scroll_x},
1083 scroll_y => $self->{scroll_y},
1084 on_changed => sub {
1085 my ($vp, $x, $y) = @_;
1086
1087 $vp->{parent}{hslider}->set_value ($x);
1088 $vp->{parent}{vslider}->set_value ($y);
1089
1090 0
1091 },
1092 on_size_allocate => sub {
1093 my ($vp, $w, $h) = @_;
1094 $vp->{parent}->update_slider;
1095 0
1096 },
1097 ;
1098
1075 $self->SUPER::add (0, 0, $self->{vp}); 1099 $self->SUPER::add ($self->{vp});
1076 $self->SUPER::add (1, 0, $self->{vslider});
1077 $self->SUPER::add (0, 1, $self->{hslider});
1078 1100
1079 $self->add ($child) if $child; 1101 $self->add ($child) if $child;
1080 1102
1081 $self 1103 $self
1082} 1104}
1083
1084#TODO# update range on size_allocate depending on child
1085 1105
1086sub add { 1106sub add {
1087 my ($self, $widget) = @_; 1107 my ($self, $widget) = @_;
1088 1108
1089 $self->{vp}->add ($self->{child} = $widget); 1109 $self->{vp}->add ($self->{child} = $widget);
1090} 1110}
1091 1111
1112sub set_offset { shift->{vp}->set_offset (@_) }
1113sub set_center { shift->{vp}->set_center (@_) }
1114sub make_visible { shift->{vp}->make_visible (@_) }
1115
1116sub update_slider {
1117 my ($self) = @_;
1118
1119 my $child = ($self->{vp} or return)->child;
1120
1121 if ($self->{scroll_x}) {
1122 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1123 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1124
1125 my $visible = $w1 > $w2;
1126 if ($visible != $self->{hslider_visible}) {
1127 $self->{hslider_visible} = $visible;
1128 $visible ? $self->SUPER::add ($self->{hslider})
1129 : $self->SUPER::remove ($self->{hslider});
1130 }
1131 }
1132
1133 if ($self->{scroll_y}) {
1134 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1135 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1136
1137 my $visible = $h1 > $h2;
1138 if ($visible != $self->{vslider_visible}) {
1139 $self->{vslider_visible} = $visible;
1140 $visible ? $self->SUPER::add ($self->{vslider})
1141 : $self->SUPER::remove ($self->{vslider});
1142 }
1143 }
1144}
1145
1146sub start_dragging {
1147 my ($self, $ev) = @_;
1148
1149 $self->grab_focus;
1150
1151 my $ox = $self->{vp}{view_x};
1152 my $oy = $self->{vp}{view_y};
1153
1154 $self->{motion} = sub {
1155 my ($ev, $x, $y) = @_;
1156
1157 $ox -= $ev->{xrel};
1158 $oy -= $ev->{yrel};
1159
1160 $self->{vp}->set_offset ($ox, $oy);
1161 };
1162}
1163
1092sub invoke_mouse_wheel { 1164sub invoke_mouse_wheel {
1093 my ($self, $ev) = @_; 1165 my ($self, $ev) = @_;
1094 1166
1095 return 0 unless $ev->{dy}; # only vertical movements
1096
1097 $self->{vslider}->emit (mouse_wheel => $ev); 1167 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1168 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1098 1169
1099 1 1170 1
1100} 1171}
1101 1172
1102sub update_slider { 1173sub invoke_button_down {
1103 my ($self) = @_; 1174 my ($self, $ev, $x, $y) = @_;
1104 1175
1105 my $child = ($self->{vp} or return)->child; 1176 if ($ev->{button} == 2) {
1177 $self->start_dragging ($ev);
1178 return 1;
1179 }
1106 1180
1107 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1181 0
1108 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1109 #$self->{hslider}->set_visibility ($w1 != $w2);
1110
1111 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1112 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1113 #$self->{vslider}->set_visibility ($h1 != $h2);
1114} 1182}
1115 1183
1116sub update { 1184sub invoke_button_up {
1117 my ($self) = @_; 1185 my ($self, $ev, $x, $y) = @_;
1118 1186
1119 $self->SUPER::update; 1187 if (delete $self->{motion}) {
1120 $self->update_slider; 1188 return 1;
1189 }
1190
1191 0
1192}
1193
1194sub invoke_mouse_motion {
1195 my ($self, $ev, $x, $y) = @_;
1196
1197 if ($self->{motion}) {
1198 $self->{motion}->($ev, $x, $y);
1199 return 1;
1200 }
1201
1202 0
1121} 1203}
1122 1204
1123sub invoke_size_allocate { 1205sub invoke_size_allocate {
1124 my ($self, $w, $h) = @_; 1206 my ($self, $w, $h) = @_;
1125 1207
1127 $self->SUPER::invoke_size_allocate ($w, $h) 1209 $self->SUPER::invoke_size_allocate ($w, $h)
1128} 1210}
1129 1211
1130############################################################################# 1212#############################################################################
1131 1213
1132package CFPlus::UI::Frame; 1214package DC::UI::Frame;
1133 1215
1134our @ISA = CFPlus::UI::Bin::; 1216our @ISA = DC::UI::Bin::;
1135 1217
1136use CFPlus::OpenGL; 1218use DC::OpenGL;
1137 1219
1138sub new { 1220sub new {
1139 my $class = shift; 1221 my $class = shift;
1140 1222
1141 $class->SUPER::new ( 1223 $class->SUPER::new (
1151 my ($w, $h) = @$self{qw(w h)}; 1233 my ($w, $h) = @$self{qw(w h)};
1152 1234
1153 glEnable GL_BLEND; 1235 glEnable GL_BLEND;
1154 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1236 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1155 glColor_premultiply @{ $self->{bg} }; 1237 glColor_premultiply @{ $self->{bg} };
1156
1157 glBegin GL_QUADS;
1158 glVertex 0 , 0;
1159 glVertex 0 , $h;
1160 glVertex $w, $h; 1238 glRect 0, 0, $w, $h;
1161 glVertex $w, 0;
1162 glEnd;
1163
1164 glDisable GL_BLEND; 1239 glDisable GL_BLEND;
1165 } 1240 }
1166 1241
1167 $self->SUPER::_draw; 1242 $self->SUPER::_draw;
1168} 1243}
1169 1244
1170############################################################################# 1245#############################################################################
1171 1246
1172package CFPlus::UI::FancyFrame; 1247package DC::UI::FancyFrame;
1173 1248
1174our @ISA = CFPlus::UI::Bin::; 1249our @ISA = DC::UI::Bin::;
1175 1250
1176use CFPlus::OpenGL; 1251use DC::OpenGL;
1177 1252
1178sub new { 1253sub new {
1179 my ($class, %arg) = @_; 1254 my ($class, %arg) = @_;
1180 1255
1181 if ((exists $arg{label}) && !ref $arg{label}) { 1256 if ((exists $arg{label}) && !ref $arg{label}) {
1182 $arg{label} = new CFPlus::UI::Label 1257 $arg{label} = new DC::UI::Label
1183 align => 1, 1258 align => 1,
1184 valign => 0, 1259 valign => 0.5,
1185 text => $arg{label}, 1260 text => $arg{label},
1186 fontsize => ($arg{border} || 0.8) * 0.75; 1261 fontsize => ($arg{border} || 0.8) * 0.75;
1187 } 1262 }
1188 1263
1189 my $self = $class->SUPER::new ( 1264 my $self = $class->SUPER::new (
1190 # label => "", 1265 # label => "",
1191 fg => [0.6, 0.3, 0.1], 1266 fg => undef,
1192 border => 0.8, 1267 border => 0.8,
1193 style => 'single', 1268 style => 'single',
1194 %arg, 1269 %arg,
1195 ); 1270 );
1196 1271
1199 1274
1200sub add { 1275sub add {
1201 my ($self, @widgets) = @_; 1276 my ($self, @widgets) = @_;
1202 1277
1203 $self->SUPER::add (@widgets); 1278 $self->SUPER::add (@widgets);
1204 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1279 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1205} 1280}
1206 1281
1207sub border { 1282sub border {
1208 int $_[0]{border} * $::FONTSIZE 1283 int $_[0]{border} * $::FONTSIZE
1209} 1284}
1249 my $border = $self->border; 1324 my $border = $self->border;
1250 my ($w, $h) = ($self->{w}, $self->{h}); 1325 my ($w, $h) = ($self->{w}, $self->{h});
1251 1326
1252 $child->draw; 1327 $child->draw;
1253 1328
1254 glColor @{$self->{fg}}; 1329 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1255 glBegin GL_LINE_STRIP; 1330 glBegin GL_LINE_STRIP;
1256 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1331 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1257 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1332 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1258 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1333 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1259 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1334 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1267 } 1342 }
1268} 1343}
1269 1344
1270############################################################################# 1345#############################################################################
1271 1346
1272package CFPlus::UI::Toplevel; 1347package DC::UI::Toplevel;
1273 1348
1274our @ISA = CFPlus::UI::Bin::; 1349our @ISA = DC::UI::Bin::;
1275 1350
1276use CFPlus::OpenGL; 1351use DC::OpenGL;
1277 1352
1278my $bg = 1353my $bg =
1279 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1354 new_from_resource DC::Texture "d1_bg.png",
1280 mipmap => 1, wrap => 1; 1355 mipmap => 1, wrap => 1;
1281 1356
1282my @border = 1357my @border =
1283 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1358 map { new_from_resource DC::Texture $_, mipmap => 1 }
1284 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1359 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1360
1361my @icon =
1362 map { new_from_resource DC::Texture $_, mipmap => 1 }
1363 qw(x1_move.png x1_resize.png);
1285 1364
1286sub new { 1365sub new {
1287 my ($class, %arg) = @_; 1366 my ($class, %arg) = @_;
1288 1367
1289 my $self = $class->SUPER::new ( 1368 my $self = $class->SUPER::new (
1290 bg => [1, 1, 1, 1], 1369 bg => [1, 1, 1, 1],
1291 border_bg => [1, 1, 1, 1], 1370 border_bg => [1, 1, 1, 1],
1292 border => 0.6, 1371 border => 0.8,
1293 can_events => 1, 1372 can_events => 1,
1294 min_w => 64, 1373 min_w => 64,
1295 min_h => 32, 1374 min_h => 32,
1296 %arg, 1375 %arg,
1297 ); 1376 );
1298 1377
1299 $self->{title_widget} = new CFPlus::UI::Label 1378 $self->{title_widget} = new DC::UI::Label
1300 align => 0, 1379 align => 0.5,
1301 valign => 1, 1380 valign => 1,
1302 text => $self->{title}, 1381 text => $self->{title},
1303 fontsize => $self->{border}, 1382 fontsize => $self->{border},
1304 if exists $self->{title}; 1383 if exists $self->{title};
1305 1384
1306 if ($self->{has_close_button}) { 1385 if ($self->{has_close_button}) {
1307 $self->{close_button} = 1386 $self->{close_button} =
1308 new CFPlus::UI::ImageButton 1387 new DC::UI::ImageButton
1309 path => 'x1_close.png', 1388 path => 'x1_close.png',
1310 on_activate => sub { $self->emit ("delete") }; 1389 on_activate => sub { $self->emit ("delete") };
1311 1390
1312 $self->CFPlus::UI::Container::add ($self->{close_button}); 1391 $self->DC::UI::Container::add ($self->{close_button});
1313 } 1392 }
1314 1393
1315 $self 1394 $self
1316} 1395}
1317 1396
1318sub add { 1397sub add {
1319 my ($self, @widgets) = @_; 1398 my ($self, @widgets) = @_;
1320 1399
1321 $self->SUPER::add (@widgets); 1400 $self->SUPER::add (@widgets);
1322 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1401 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1323 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1402 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1324} 1403}
1325 1404
1326sub border { 1405sub border {
1327 int $_[0]{border} * $::FONTSIZE 1406 int $_[0]{border} * $::FONTSIZE
1407}
1408
1409sub get_max_wh {
1410 my ($self) = @_;
1411
1412 return ($self->{w}, $self->{h})
1413 if $self->{visible} && $self->{w};
1414
1415 $self->SUPER::get_max_wh
1328} 1416}
1329 1417
1330sub size_request { 1418sub size_request {
1331 my ($self) = @_; 1419 my ($self) = @_;
1332 1420
1457 glEnable GL_TEXTURE_2D; 1545 glEnable GL_TEXTURE_2D;
1458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1546 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1459 1547
1460 my $border = $self->border; 1548 my $border = $self->border;
1461 1549
1550 if ($border) {
1462 glColor @{ $self->{border_bg} }; 1551 glColor @{ $self->{border_bg} };
1463 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1552 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1464 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1553 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1465 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1554 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1466 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1555 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1556
1557 # move
1558 my $w2 = ($w - $border) * .5;
1559 my $h2 = ($h - $border) * .5;
1560 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1561 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1562 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1563
1564 # resize
1565 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1566 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1567 unless $self->{has_close_button};
1568 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1569 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1570 }
1467 1571
1468 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1572 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1469 glColor @{ $self->{bg} }; 1573 glColor @{ $self->{bg} };
1470 1574
1471 # TODO: repeat texture not scale 1575 # TODO: repeat texture not scale
1490 if $self->{close_button}; 1594 if $self->{close_button};
1491} 1595}
1492 1596
1493############################################################################# 1597#############################################################################
1494 1598
1495package CFPlus::UI::Table; 1599package DC::UI::Table;
1496 1600
1497our @ISA = CFPlus::UI::Base::; 1601our @ISA = DC::UI::Container::;
1498 1602
1499use List::Util qw(max sum); 1603use List::Util qw(max sum);
1500 1604
1501use CFPlus::OpenGL; 1605use DC::OpenGL;
1502 1606
1503sub new { 1607sub new {
1504 my $class = shift; 1608 my $class = shift;
1505 1609
1506 $class->SUPER::new ( 1610 $class->SUPER::new (
1508 row_expand => [], 1612 row_expand => [],
1509 @_, 1613 @_,
1510 ) 1614 )
1511} 1615}
1512 1616
1513sub children {
1514 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1515}
1516
1517sub add { 1617sub add {
1618 my ($self, @widgets) = @_;
1619
1620 for my $child (@widgets) {
1621 $child->{c_rowspan} ||= 1;
1622 $child->{c_colspan} ||= 1;
1623 }
1624
1625 $self->SUPER::add (@widgets);
1626}
1627
1628sub add_at {
1518 my ($self) = shift; 1629 my $self = shift;
1630
1631 my @widgets;
1519 1632
1520 while (@_) { 1633 while (@_) {
1521 my ($x, $y, $child) = splice @_, 0, 3, (); 1634 my ($col, $row, $child) = splice @_, 0, 3, ();
1522 $child->set_parent ($self);
1523 $self->{children}[$y][$x] = $child;
1524 }
1525 1635
1526 $self->{force_realloc} = 1; 1636 $child->{c_row} = $row;
1527 $self->{force_size_alloc} = 1; 1637 $child->{c_col} = $col;
1528 $self->realloc;
1529}
1530 1638
1531sub remove { 1639 push @widgets, $child;
1640 }
1641
1642 $self->add (@widgets);
1643}
1644
1645sub get_wh {
1532 my ($self, $child) = @_; 1646 my ($self) = @_;
1533 1647
1534 # TODO: not yet implemented 1648 my (@w, @h);
1535}
1536
1537# TODO: move to container class maybe? send children a signal on removal?
1538sub clear {
1539 my ($self) = @_;
1540 1649
1541 my @children = $self->children; 1650 my @children = $self->children;
1542 delete $self->{children}; 1651
1652 # first pass, columns
1653 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1654 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1655
1656 my $sw = sum @w[$c .. $c + $cs - 1];
1657
1658 if ($w > $sw) {
1659 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1660 }
1543 1661 }
1544 for (@children) {
1545 delete $_->{parent};
1546 $_->hide;
1547 }
1548 1662
1549 $self->realloc; 1663 # second pass, rows
1550} 1664 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1551
1552sub get_wh {
1553 my ($self) = @_;
1554
1555 my (@w, @h);
1556
1557 for my $y (0 .. $#{$self->{children}}) {
1558 my $row = $self->{children}[$y]
1559 or next;
1560
1561 for my $x (0 .. $#$row) {
1562 my $widget = $row->[$x]
1563 or next;
1564 my ($w, $h) = @$widget{qw(req_w req_h)}; 1665 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1565 1666
1566 $w[$x] = max $w[$x], $w; 1667 my $sh = sum @h[$r .. $r + $rs - 1];
1567 $h[$y] = max $h[$y], $h; 1668
1669 if ($h > $sh) {
1670 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1568 } 1671 }
1569 } 1672 }
1570 1673
1571 (\@w, \@h) 1674 (\@w, \@h)
1572} 1675}
1588 my ($ws, $hs) = $self->get_wh; 1691 my ($ws, $hs) = $self->get_wh;
1589 1692
1590 my $req_w = (sum @$ws) || 1; 1693 my $req_w = (sum @$ws) || 1;
1591 my $req_h = (sum @$hs) || 1; 1694 my $req_h = (sum @$hs) || 1;
1592 1695
1593 # TODO: nicer code 1696 # now linearly scale the rows/columns to the allocated size
1594 my @col_expand = @{$self->{col_expand}}; 1697 my @col_expand = @{$self->{col_expand}};
1595 @col_expand = (1) x @$ws unless @col_expand; 1698 @col_expand = (1) x @$ws unless @col_expand;
1596 my $col_expand = (sum @col_expand) || 1; 1699 my $col_expand = (sum @col_expand) || 1;
1597 1700
1598 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1701 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1599 1702
1600 CFPlus::UI::harmonize $ws; 1703 DC::UI::harmonize $ws;
1601 1704
1602 my @row_expand = @{$self->{row_expand}}; 1705 my @row_expand = @{$self->{row_expand}};
1603 @row_expand = (1) x @$ws unless @row_expand; 1706 @row_expand = (1) x @$ws unless @row_expand;
1604 my $row_expand = (sum @row_expand) || 1; 1707 my $row_expand = (sum @row_expand) || 1;
1605 1708
1606 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1709 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1607 1710
1608 CFPlus::UI::harmonize $hs; 1711 DC::UI::harmonize $hs;
1609 1712
1610 my $y; 1713 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1714 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1611 1715
1612 for my $r (0 .. $#{$self->{children}}) { 1716 for my $widget ($self->children) {
1613 my $row = $self->{children}[$r] 1717 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1614 or next;
1615 1718
1616 my $x = 0; 1719 $widget->configure (
1617 my $row_h = $hs->[$r]; 1720 $x[$c], $y[$r],
1721 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1618 1722 );
1619 for my $c (0 .. $#$row) { 1723 }
1620 my $col_w = $ws->[$c];
1621 1724
1622 if (my $widget = $row->[$c]) { 1725 1
1623 $widget->configure ($x, $y, $col_w, $row_h); 1726}
1624 }
1625 1727
1626 $x += $col_w; 1728#############################################################################
1729
1730package DC::UI::Fixed;
1731
1732use List::Util qw(min max);
1733
1734our @ISA = DC::UI::Container::;
1735
1736sub _scale($$$) {
1737 my ($rel, $val, $max) = @_;
1738
1739 $rel ? $val * $max : $val
1740}
1741
1742sub size_request {
1743 my ($self) = @_;
1744
1745 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1746
1747 # determine overall size by querying abs widgets
1748 for my $child ($self->visible_children) {
1749 unless ($child->{c_rel}) {
1750 my $x = $child->{c_x};
1751 my $y = $child->{c_y};
1752
1753 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1754 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1627 } 1755 }
1756 }
1628 1757
1629 $y += $row_h; 1758 my $W = $x2 - $x1;
1759 my $H = $y2 - $y1;
1760
1761 # now layout remaining widgets
1762 for my $child ($self->visible_children) {
1763 if ($child->{c_rel}) {
1764 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1765 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1766
1767 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1768 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1769 }
1770 }
1771
1772 my $W = $x2 - $x1;
1773 my $H = $y2 - $y1;
1774
1775 ($W, $H)
1776}
1777
1778sub invoke_size_allocate {
1779 my ($self, $W, $H) = @_;
1780
1781 for my $child ($self->visible_children) {
1782 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1783 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1784
1785 $x += $child->{c_halign} * $child->{req_w};
1786 $y += $child->{c_valign} * $child->{req_h};
1787
1788 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1630 } 1789 }
1631 1790
1632 1 1791 1
1633} 1792}
1634 1793
1635sub find_widget {
1636 my ($self, $x, $y) = @_;
1637
1638 $x -= $self->{x};
1639 $y -= $self->{y};
1640
1641 my $res;
1642
1643 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1644 $res = $_->find_widget ($x, $y)
1645 and return $res;
1646 }
1647
1648 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1649}
1650
1651sub _draw {
1652 my ($self) = @_;
1653
1654 for (grep $_, @{$self->{children}}) {
1655 $_->draw for grep $_, @$_;
1656 }
1657}
1658
1659############################################################################# 1794#############################################################################
1660 1795
1661package CFPlus::UI::Box; 1796package DC::UI::Box;
1662 1797
1663our @ISA = CFPlus::UI::Container::; 1798our @ISA = DC::UI::Container::;
1664 1799
1665sub size_request { 1800sub size_request {
1666 my ($self) = @_; 1801 my ($self) = @_;
1802
1803 my @children = $self->visible_children;
1667 1804
1668 $self->{vertical} 1805 $self->{vertical}
1669 ? ( 1806 ? (
1670 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1807 (List::Util::max map $_->{req_w}, @children),
1671 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1808 (List::Util::sum map $_->{req_h}, @children),
1672 ) 1809 )
1673 : ( 1810 : (
1674 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1811 (List::Util::sum map $_->{req_w}, @children),
1675 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1812 (List::Util::max map $_->{req_h}, @children),
1676 ) 1813 )
1677} 1814}
1678 1815
1679sub invoke_size_allocate { 1816sub invoke_size_allocate {
1680 my ($self, $w, $h) = @_; 1817 my ($self, $w, $h) = @_;
1701 $req[$_] += $space * $children[$_]{expand} 1838 $req[$_] += $space * $children[$_]{expand}
1702 for 0 .. $#children; 1839 for 0 .. $#children;
1703 } 1840 }
1704 } 1841 }
1705 1842
1706 CFPlus::UI::harmonize \@req; 1843 DC::UI::harmonize \@req;
1707 1844
1708 my $pos = 0; 1845 my $pos = 0;
1709 for (0 .. $#children) { 1846 for (0 .. $#children) {
1710 my $alloc = $req[$_]; 1847 my $alloc = $req[$_];
1711 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1848 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1716 1 1853 1
1717} 1854}
1718 1855
1719############################################################################# 1856#############################################################################
1720 1857
1721package CFPlus::UI::HBox; 1858package DC::UI::HBox;
1722 1859
1723our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1724 1861
1725sub new { 1862sub new {
1726 my $class = shift; 1863 my $class = shift;
1727 1864
1728 $class->SUPER::new ( 1865 $class->SUPER::new (
1731 ) 1868 )
1732} 1869}
1733 1870
1734############################################################################# 1871#############################################################################
1735 1872
1736package CFPlus::UI::VBox; 1873package DC::UI::VBox;
1737 1874
1738our @ISA = CFPlus::UI::Box::; 1875our @ISA = DC::UI::Box::;
1739 1876
1740sub new { 1877sub new {
1741 my $class = shift; 1878 my $class = shift;
1742 1879
1743 $class->SUPER::new ( 1880 $class->SUPER::new (
1746 ) 1883 )
1747} 1884}
1748 1885
1749############################################################################# 1886#############################################################################
1750 1887
1751package CFPlus::UI::Label; 1888package DC::UI::Label;
1752 1889
1753our @ISA = CFPlus::UI::DrawBG::; 1890our @ISA = DC::UI::DrawBG::;
1754 1891
1755use CFPlus::OpenGL; 1892use DC::OpenGL;
1756 1893
1757sub new { 1894sub new {
1758 my ($class, %arg) = @_; 1895 my ($class, %arg) = @_;
1759 1896
1760 my $self = $class->SUPER::new ( 1897 my $self = $class->SUPER::new (
1765 #text => initial text 1902 #text => initial text
1766 #markup => initial narkup 1903 #markup => initial narkup
1767 #max_w => maximum pixel width 1904 #max_w => maximum pixel width
1768 #style => 0, # render flags 1905 #style => 0, # render flags
1769 ellipsise => 3, # end 1906 ellipsise => 3, # end
1770 layout => (new CFPlus::Layout), 1907 layout => (new DC::Layout),
1771 fontsize => 1, 1908 fontsize => 1,
1772 align => -1, 1909 align => 0.5,
1773 valign => -1, 1910 valign => 0.5,
1774 padding_x => 2, 1911 padding_x => 4,
1775 padding_y => 2, 1912 padding_y => 2,
1776 can_events => 0, 1913 can_events => 0,
1777 %arg 1914 %arg
1778 ); 1915 );
1779 1916
1780 if (exists $self->{template}) { 1917 if (exists $self->{template}) {
1781 my $layout = new CFPlus::Layout; 1918 my $layout = new DC::Layout;
1782 $layout->set_text (delete $self->{template}); 1919 $layout->set_text (delete $self->{template});
1783 $self->{template} = $layout; 1920 $self->{template} = $layout;
1784 } 1921 }
1785 1922
1786 if (exists $self->{markup}) { 1923 if (exists $self->{markup}) {
1804 1941
1805 delete $self->{ox}; 1942 delete $self->{ox};
1806 $self->SUPER::realloc; 1943 $self->SUPER::realloc;
1807} 1944}
1808 1945
1946sub clear {
1947 my ($self) = @_;
1948
1949 $self->set_text ("");
1950}
1951
1809sub set_text { 1952sub set_text {
1810 my ($self, $text) = @_; 1953 my ($self, $text) = @_;
1811 1954
1812 return if $self->{text} eq "T$text"; 1955 return if $self->{text} eq "T$text";
1813 $self->{text} = "T$text"; 1956 $self->{text} = "T$text";
1836 1979
1837sub size_request { 1980sub size_request {
1838 my ($self) = @_; 1981 my ($self) = @_;
1839 1982
1840 $self->{size_req} ||= do { 1983 $self->{size_req} ||= do {
1984 my ($max_w, $max_h) = $self->get_max_wh;
1985
1841 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1986 $self->{layout}->set_font ($self->{font}) if $self->{font};
1842 $self->{layout}->set_width ($self->{max_w} || -1); 1987 $self->{layout}->set_width ($max_w);
1843 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1988 $self->{layout}->set_ellipsise ($self->{ellipsise});
1844 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1989 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1845 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1990 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1846 1991
1847 my ($w, $h) = $self->{layout}->size; 1992 my ($w, $h) = $self->{layout}->size;
1848 1993
1849 if (exists $self->{template}) { 1994 if (exists $self->{template}) {
1850 $self->{template}->set_font ($self->{font}) if $self->{font}; 1995 $self->{template}->set_font ($self->{font}) if $self->{font};
1851 $self->{template}->set_width ($self->{max_w} || -1); 1996 $self->{template}->set_width ($max_w);
1852 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1997 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1853 1998
1854 my ($w2, $h2) = $self->{template}->size; 1999 my ($w2, $h2) = $self->{template}->size;
1855 2000
1856 $w = List::Util::max $w, $w2; 2001 $w = List::Util::max $w, $w2;
1912 2057
1913 [$self->{layout}->size] 2058 [$self->{layout}->size]
1914 }; 2059 };
1915 2060
1916 unless (exists $self->{ox}) { 2061 unless (exists $self->{ox}) {
1917 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2062 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
1918 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2063 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
1919 : ($self->{w} - $size->[0]) * 0.5);
1920 2064
1921 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2065 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1922 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1923 : ($self->{h} - $size->[1]) * 0.5);
1924 }; 2066 };
1925 2067
1926 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2068# unless ($self->{list}) {
1927 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2069# $self->{list} = DC::OpenGL::glGenList;
1928 2070# DC::OpenGL::glNewList $self->{list};
1929 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2071# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2072# DC::OpenGL::glEndList;
2073# }
2074#
2075# DC::OpenGL::glCallList $self->{list};
2076
2077 $self->{layout}->draw;
1930} 2078}
2079
2080#sub destroy {
2081# my ($self) = @_;
2082#
2083# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2084#
2085# $self->SUPER::destroy;
2086#}
1931 2087
1932############################################################################# 2088#############################################################################
1933 2089
1934package CFPlus::UI::EntryBase; 2090package DC::UI::EntryBase;
1935 2091
1936our @ISA = CFPlus::UI::Label::; 2092our @ISA = DC::UI::Label::;
1937 2093
1938use CFPlus::OpenGL; 2094use DC::OpenGL;
1939 2095
1940sub new { 2096sub new {
1941 my $class = shift; 2097 my $class = shift;
1942 2098
1943 $class->SUPER::new ( 2099 $class->SUPER::new (
1944 fg => [1, 1, 1], 2100 fg => [1, 1, 1],
1945 bg => [0, 0, 0, 0.2], 2101 bg => [0, 0, 0, 0.2],
2102 outline => undef,
1946 active_bg => [1, 1, 1, 0.5], 2103 active_bg => [0, 0, 1, .2],
1947 active_fg => [0, 0, 0], 2104 active_fg => [1, 1, 1],
2105 active_outline => [1, 1, 0],
1948 can_hover => 1, 2106 can_hover => 1,
1949 can_focus => 1, 2107 can_focus => 1,
2108 align => 0,
1950 valign => 0, 2109 valign => 0.5,
1951 can_events => 1, 2110 can_events => 1,
1952 ellipsise => 0, 2111 ellipsise => 0,
2112 padding_x => 4,
2113 padding_y => 2,
1953 #text => ... 2114 #text => ...
1954 #hidden => "*", 2115 #hidden => "*",
1955 @_ 2116 @_
1956 ) 2117 )
1957} 2118}
2004 2165
2005 my $text = $self->get_text; 2166 my $text = $self->get_text;
2006 2167
2007 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2168 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2008 2169
2009 if ($uni == 8) { 2170 if ($sym == DC::SDLK_BACKSPACE) {
2010 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2171 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2011 } elsif ($uni == 127) { 2172 } elsif ($sym == DC::SDLK_DELETE) {
2012 substr $text, $self->{cursor}, 1, ""; 2173 substr $text, $self->{cursor}, 1, "";
2013 } elsif ($sym == CFPlus::SDLK_LEFT) { 2174 } elsif ($sym == DC::SDLK_LEFT) {
2014 --$self->{cursor} if $self->{cursor}; 2175 --$self->{cursor} if $self->{cursor};
2015 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2176 } elsif ($sym == DC::SDLK_RIGHT) {
2016 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2177 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2017 } elsif ($sym == CFPlus::SDLK_HOME) { 2178 } elsif ($sym == DC::SDLK_HOME) {
2018 # what a hack 2179 # what a hack
2019 $self->{cursor} = 2180 $self->{cursor} =
2020 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2181 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2021 ? length $1 2182 ? length $1
2022 : 0; 2183 : 0;
2023 } elsif ($sym == CFPlus::SDLK_END) { 2184 } elsif ($sym == DC::SDLK_END) {
2024 # uh, again 2185 # uh, again
2025 $self->{cursor} = 2186 $self->{cursor} =
2026 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2187 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2027 ? $self->{cursor} + length $1 2188 ? $self->{cursor} + length $1
2028 : length $self->{text}; 2189 : length $self->{text};
2092 glColor_premultiply @{$self->{bg}}; 2253 glColor_premultiply @{$self->{bg}};
2093 } 2254 }
2094 2255
2095 glEnable GL_BLEND; 2256 glEnable GL_BLEND;
2096 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2257 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2097 glBegin GL_QUADS;
2098 glVertex 0 , 0;
2099 glVertex 0 , $self->{h};
2100 glVertex $self->{w}, $self->{h}; 2258 glRect 0, 0, $self->{w}, $self->{h};
2101 glVertex $self->{w}, 0;
2102 glEnd;
2103 glDisable GL_BLEND; 2259 glDisable GL_BLEND;
2104 2260
2105 $self->SUPER::_draw; 2261 $self->SUPER::_draw;
2106 2262
2107 #TODO: force update every cursor change :( 2263 #TODO: force update every cursor change :(
2109 2265
2110 unless (exists $self->{cur_h}) { 2266 unless (exists $self->{cur_h}) {
2111 my $text = substr $self->{text}, 0, $self->{cursor}; 2267 my $text = substr $self->{text}, 0, $self->{cursor};
2112 utf8::encode $text; 2268 utf8::encode $text;
2113 2269
2114 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2270 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2115 } 2271 }
2116 2272
2273 glColor_premultiply @{$self->{active_fg}};
2117 glBegin GL_LINES; 2274 glBegin GL_LINES;
2118 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2275 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2119 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2276 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2120 glEnd; 2277 glEnd;
2278
2279 glLineWidth 3;
2280 glColor @{$self->{active_outline}};
2281 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2282 glLineWidth 1;
2283
2284 } else {
2285 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2286 glBegin GL_LINE_STRIP;
2287 glVertex .5, $self->{h} * .5;
2288 glVertex .5, $self->{h} - 2.5;
2289 glVertex $self->{w} - .5, $self->{h} - 2.5;
2290 glVertex $self->{w} - .5, $self->{h} * .5;
2291 glEnd;
2121 } 2292 }
2122} 2293}
2123 2294
2124############################################################################# 2295#############################################################################
2125 2296
2126package CFPlus::UI::Entry; 2297package DC::UI::Entry;
2127 2298
2128our @ISA = CFPlus::UI::EntryBase::; 2299our @ISA = DC::UI::EntryBase::;
2129 2300
2130use CFPlus::OpenGL; 2301use DC::OpenGL;
2302
2303sub new {
2304 my $class = shift;
2305
2306 $class->SUPER::new (
2307 history_pointer => -1,
2308 @_
2309 )
2310}
2311
2131 2312
2132sub invoke_key_down { 2313sub invoke_key_down {
2133 my ($self, $ev) = @_; 2314 my ($self, $ev) = @_;
2134 2315
2135 my $sym = $ev->{sym}; 2316 my $sym = $ev->{sym};
2141 $self->{history_pointer} = -1; 2322 $self->{history_pointer} = -1;
2142 $self->{history_saveback} = ''; 2323 $self->{history_saveback} = '';
2143 $self->emit (activate => $txt); 2324 $self->emit (activate => $txt);
2144 $self->update; 2325 $self->update;
2145 2326
2146 } elsif ($sym == CFPlus::SDLK_UP) { 2327 } elsif ($sym == DC::SDLK_UP) {
2147 if ($self->{history_pointer} < 0) { 2328 if ($self->{history_pointer} < 0) {
2148 $self->{history_saveback} = $self->get_text; 2329 $self->{history_saveback} = $self->get_text;
2149 } 2330 }
2150 if (@{$self->{history} || []} > 0) { 2331 if (@{$self->{history} || []} > 0) {
2151 $self->{history_pointer}++; 2332 $self->{history_pointer}++;
2153 $self->{history_pointer} = @{$self->{history} || []} - 1; 2334 $self->{history_pointer} = @{$self->{history} || []} - 1;
2154 } 2335 }
2155 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2336 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2156 } 2337 }
2157 2338
2158 } elsif ($sym == CFPlus::SDLK_DOWN) { 2339 } elsif ($sym == DC::SDLK_DOWN) {
2159 $self->{history_pointer}--; 2340 $self->{history_pointer}--;
2160 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2341 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2161 2342
2162 if ($self->{history_pointer} >= 0) { 2343 if ($self->{history_pointer} >= 0) {
2163 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2344 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2164 } else { 2345 } else {
2346 if (defined $self->{history_saveback}) {
2165 $self->set_text ($self->{history_saveback}); 2347 $self->set_text ($self->{history_saveback});
2348 $self->{history_saveback} = undef;
2349 }
2166 } 2350 }
2167 2351
2168 } else { 2352 } else {
2169 return $self->SUPER::invoke_key_down ($ev) 2353 return $self->SUPER::invoke_key_down ($ev)
2170 } 2354 }
2172 1 2356 1
2173} 2357}
2174 2358
2175############################################################################# 2359#############################################################################
2176 2360
2177package CFPlus::UI::TextEdit; 2361package DC::UI::TextEdit;
2178 2362
2179our @ISA = CFPlus::UI::EntryBase::; 2363our @ISA = DC::UI::EntryBase::;
2180 2364
2181use CFPlus::OpenGL; 2365use DC::OpenGL;
2366
2367sub new {
2368 my $class = shift;
2369
2370 $class->SUPER::new (
2371 padding_y => 4,
2372
2373 @_
2374 )
2375}
2182 2376
2183sub move_cursor_ver { 2377sub move_cursor_ver {
2184 my ($self, $dy) = @_; 2378 my ($self, $dy) = @_;
2185 2379
2186 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2380 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2187 2381
2188 $y += $dy; 2382 $line += $dy;
2189 2383
2190 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2384 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2191 $self->{cursor} = $index; 2385 $self->{cursor} = $index;
2192 delete $self->{cur_h}; 2386 delete $self->{cur_h};
2193 $self->update; 2387 $self->update;
2194 return; 2388 return;
2195 } 2389 }
2198sub invoke_key_down { 2392sub invoke_key_down {
2199 my ($self, $ev) = @_; 2393 my ($self, $ev) = @_;
2200 2394
2201 my $sym = $ev->{sym}; 2395 my $sym = $ev->{sym};
2202 2396
2203 if ($sym == CFPlus::SDLK_UP) { 2397 if ($sym == DC::SDLK_UP) {
2204 $self->move_cursor_ver (-1); 2398 $self->move_cursor_ver (-1);
2205 } elsif ($sym == CFPlus::SDLK_DOWN) { 2399 } elsif ($sym == DC::SDLK_DOWN) {
2206 $self->move_cursor_ver (+1); 2400 $self->move_cursor_ver (+1);
2207 } else { 2401 } else {
2208 return $self->SUPER::invoke_key_down ($ev) 2402 return $self->SUPER::invoke_key_down ($ev)
2209 } 2403 }
2210 2404
2211 1 2405 1
2212} 2406}
2213 2407
2214############################################################################# 2408#############################################################################
2215 2409
2216package CFPlus::UI::Button; 2410package DC::UI::ButtonBin;
2217 2411
2218our @ISA = CFPlus::UI::Label::; 2412our @ISA = DC::UI::Bin::;
2219 2413
2220use CFPlus::OpenGL; 2414use DC::OpenGL;
2221 2415
2222my @tex = 2416my @tex =
2223 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2417 map { new_from_resource DC::Texture $_, mipmap => 1 }
2224 qw(b1_button_active.png); 2418 qw(b1_button_inactive.png b1_button_active.png);
2225 2419
2226sub new { 2420sub new {
2227 my $class = shift; 2421 my $class = shift;
2228 2422
2229 $class->SUPER::new ( 2423 $class->SUPER::new (
2230 padding_x => 4,
2231 padding_y => 4,
2232 fg => [1, 1, 1],
2233 active_fg => [0, 0, 1],
2234 can_hover => 1, 2424 can_hover => 1,
2235 align => 0, 2425 align => 0.5,
2236 valign => 0, 2426 valign => 0.5,
2237 can_events => 1, 2427 can_events => 1,
2238 @_ 2428 @_
2239 ) 2429 )
2240} 2430}
2241 2431
2250} 2440}
2251 2441
2252sub _draw { 2442sub _draw {
2253 my ($self) = @_; 2443 my ($self) = @_;
2254 2444
2255 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2256
2257 glEnable GL_TEXTURE_2D; 2445 glEnable GL_TEXTURE_2D;
2258 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2259 glColor 0, 0, 0, 1; 2447 glColor 0, 0, 0, 1;
2260 2448
2449 my $tex = $tex[$GRAB == $self];
2261 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2450 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2262 2451
2263 glDisable GL_TEXTURE_2D; 2452 glDisable GL_TEXTURE_2D;
2264 2453
2265 $self->SUPER::_draw; 2454 $self->SUPER::_draw;
2266} 2455}
2267 2456
2268############################################################################# 2457#############################################################################
2269 2458
2270package CFPlus::UI::CheckBox; 2459package DC::UI::Button;
2271 2460
2272our @ISA = CFPlus::UI::DrawBG::; 2461our @ISA = DC::UI::Label::;
2462
2463use DC::OpenGL;
2273 2464
2274my @tex = 2465my @tex =
2275 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2466 map { new_from_resource DC::Texture $_, mipmap => 1 }
2276 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2467 qw(b1_button_inactive.png b1_button_active.png);
2277
2278use CFPlus::OpenGL;
2279 2468
2280sub new { 2469sub new {
2281 my $class = shift; 2470 my $class = shift;
2282 2471
2283 $class->SUPER::new ( 2472 $class->SUPER::new (
2473 padding_x => 8,
2474 padding_y => 4,
2475 fg => [1.0, 1.0, 1.0],
2476 active_fg => [0.8, 0.8, 0.8],
2477 can_hover => 1,
2478 align => 0.5,
2479 valign => 0.5,
2480 can_events => 1,
2481 @_
2482 )
2483}
2484
2485sub invoke_button_up {
2486 my ($self, $ev, $x, $y) = @_;
2487
2488 $self->emit ("activate")
2489 if $x >= 0 && $x < $self->{w}
2490 && $y >= 0 && $y < $self->{h};
2491
2492 1
2493}
2494
2495sub _draw {
2496 my ($self) = @_;
2497
2498 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2499
2500 glEnable GL_TEXTURE_2D;
2501 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2502 glColor 0, 0, 0, 1;
2503
2504 my $tex = $tex[$GRAB == $self];
2505 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2506
2507 glDisable GL_TEXTURE_2D;
2508
2509 $self->SUPER::_draw;
2510}
2511
2512#############################################################################
2513
2514package DC::UI::CheckBox;
2515
2516our @ISA = DC::UI::DrawBG::;
2517
2518my @tex =
2519 map { new_from_resource DC::Texture $_, mipmap => 1 }
2520 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2521
2522use DC::OpenGL;
2523
2524sub new {
2525 my $class = shift;
2526
2527 $class->SUPER::new (
2528 fontsize => 1,
2284 padding_x => 2, 2529 padding_x => 2,
2285 padding_y => 2, 2530 padding_y => 2,
2286 fg => [1, 1, 1], 2531 fg => [1, 1, 1],
2287 active_fg => [1, 1, 0], 2532 active_fg => [1, 1, 0],
2288 bg => [0, 0, 0, 0.2], 2533 bg => [0, 0, 0, 0.2],
2294} 2539}
2295 2540
2296sub size_request { 2541sub size_request {
2297 my ($self) = @_; 2542 my ($self) = @_;
2298 2543
2299 (6) x 2 2544 ($self->{fontsize} * $::FONTSIZE) x 2
2300} 2545}
2301 2546
2302sub toggle { 2547sub toggle {
2303 my ($self) = @_; 2548 my ($self) = @_;
2304 2549
2323sub _draw { 2568sub _draw {
2324 my ($self) = @_; 2569 my ($self) = @_;
2325 2570
2326 $self->SUPER::_draw; 2571 $self->SUPER::_draw;
2327 2572
2328 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2573 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2329 2574
2330 my ($w, $h) = @$self{qw(w h)}; 2575 my ($w, $h) = @$self{qw(w h)};
2331 2576
2332 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2577 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2333 2578
2340 glDisable GL_TEXTURE_2D; 2585 glDisable GL_TEXTURE_2D;
2341} 2586}
2342 2587
2343############################################################################# 2588#############################################################################
2344 2589
2345package CFPlus::UI::Image; 2590package DC::UI::Image;
2346 2591
2347our @ISA = CFPlus::UI::Base::; 2592our @ISA = DC::UI::Base::;
2348 2593
2349use CFPlus::OpenGL; 2594use DC::OpenGL;
2350 2595
2351our %texture_cache; 2596our %texture_cache;
2352 2597
2353sub new { 2598sub new {
2354 my $class = shift; 2599 my $class = shift;
2355 2600
2356 my $self = $class->SUPER::new ( 2601 my $self = $class->SUPER::new (
2357 can_events => 0, 2602 can_events => 0,
2603 scale => 1,
2358 @_, 2604 @_,
2359 ); 2605 );
2360 2606
2361 $self->{path} || $self->{tex} 2607 $self->{path} || $self->{tex}
2362 or Carp::croak "'path' or 'tex' attributes required"; 2608 or Carp::croak "'path' or 'tex' attributes required";
2363 2609
2364 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2610 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2365 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2611 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2366 2612
2367 CFPlus::weaken $texture_cache{$self->{path}}; 2613 DC::weaken $texture_cache{$self->{path}};
2368 2614
2369 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2615 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2370 2616
2371 $self 2617 $self
2372} 2618}
2373 2619
2374sub STORABLE_freeze { 2620sub STORABLE_freeze {
2375 my ($self, $cloning) = @_; 2621 my ($self, $cloning) = @_;
2376 2622
2377 $self->{path} 2623 $self->{path}
2378 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2624 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2379 2625
2380 $self->{path} 2626 $self->{path}
2381} 2627}
2382 2628
2383sub STORABLE_attach { 2629sub STORABLE_attach {
2387} 2633}
2388 2634
2389sub size_request { 2635sub size_request {
2390 my ($self) = @_; 2636 my ($self) = @_;
2391 2637
2392 ($self->{tex}{w}, $self->{tex}{h}) 2638 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2393} 2639}
2394 2640
2395sub _draw { 2641sub _draw {
2396 my ($self) = @_; 2642 my ($self) = @_;
2397 2643
2407 } 2653 }
2408 2654
2409 glEnable GL_TEXTURE_2D; 2655 glEnable GL_TEXTURE_2D;
2410 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2411 2657
2412 $tex->draw_quad (0, 0, $w, $h); 2658 $tex->draw_quad_alpha (0, 0, $w, $h);
2413 2659
2414 glDisable GL_TEXTURE_2D; 2660 glDisable GL_TEXTURE_2D;
2415} 2661}
2416 2662
2417############################################################################# 2663#############################################################################
2418 2664
2419package CFPlus::UI::ImageButton; 2665package DC::UI::ImageButton;
2420 2666
2421our @ISA = CFPlus::UI::Image::; 2667our @ISA = DC::UI::Image::;
2422 2668
2423use CFPlus::OpenGL; 2669use DC::OpenGL;
2424 2670
2425my %textures; 2671my %textures;
2426 2672
2427sub new { 2673sub new {
2428 my $class = shift; 2674 my $class = shift;
2431 padding_x => 4, 2677 padding_x => 4,
2432 padding_y => 4, 2678 padding_y => 4,
2433 fg => [1, 1, 1], 2679 fg => [1, 1, 1],
2434 active_fg => [0, 0, 1], 2680 active_fg => [0, 0, 1],
2435 can_hover => 1, 2681 can_hover => 1,
2436 align => 0, 2682 align => 0.5,
2437 valign => 0, 2683 valign => 0.5,
2438 can_events => 1, 2684 can_events => 1,
2439 @_ 2685 @_
2440 ); 2686 );
2441} 2687}
2442 2688
2689sub invoke_button_down {
2690 my ($self, $ev, $x, $y) = @_;
2691
2692 1
2693}
2694
2443sub invoke_button_up { 2695sub invoke_button_up {
2444 my ($self, $ev, $x, $y) = @_; 2696 my ($self, $ev, $x, $y) = @_;
2445 2697
2446 $self->emit ("activate") 2698 $self->emit ("activate")
2447 if $x >= 0 && $x < $self->{w} 2699 if $x >= 0 && $x < $self->{w}
2450 1 2702 1
2451} 2703}
2452 2704
2453############################################################################# 2705#############################################################################
2454 2706
2455package CFPlus::UI::VGauge; 2707package DC::UI::VGauge;
2456 2708
2457our @ISA = CFPlus::UI::Base::; 2709our @ISA = DC::UI::Base::;
2458 2710
2459use List::Util qw(min max); 2711use List::Util qw(min max);
2460 2712
2461use CFPlus::OpenGL; 2713use DC::OpenGL;
2462 2714
2463my %tex = ( 2715my %tex = (
2464 food => [ 2716 food => [
2465 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2717 map { new_from_resource DC::Texture $_, mipmap => 1 }
2466 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2718 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2467 ], 2719 ],
2468 grace => [ 2720 grace => [
2469 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2721 map { new_from_resource DC::Texture $_, mipmap => 1 }
2470 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2722 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2471 ], 2723 ],
2472 hp => [ 2724 hp => [
2473 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2725 map { new_from_resource DC::Texture $_, mipmap => 1 }
2474 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2726 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2475 ], 2727 ],
2476 mana => [ 2728 mana => [
2477 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2729 map { new_from_resource DC::Texture $_, mipmap => 1 }
2478 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2730 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2479 ], 2731 ],
2480); 2732);
2481 2733
2482# eg. VGauge->new (gauge => 'food'), default gauge: food 2734# eg. VGauge->new (gauge => 'food'), default gauge: food
2585 glDisable GL_TEXTURE_2D; 2837 glDisable GL_TEXTURE_2D;
2586} 2838}
2587 2839
2588############################################################################# 2840#############################################################################
2589 2841
2842package DC::UI::Progress;
2843
2844our @ISA = DC::UI::Label::;
2845
2846use DC::OpenGL;
2847
2848sub new {
2849 my ($class, %arg) = @_;
2850
2851 my $self = $class->SUPER::new (
2852 padding_x => 2,
2853 padding_y => 2,
2854 fg => [1, 1, 1],
2855 bg => [0, 0, 1, 0.2],
2856 bar => [0.7, 0.5, 0.1, 0.8],
2857 outline => [0.4, 0.3, 0],
2858 fontsize => 0.9,
2859 valign => 0.5,
2860 align => 0.5,
2861 can_events => 1,
2862 ellipsise => 1,
2863 label => "%d%%",
2864 %arg,
2865 );
2866
2867 $self->set_value ($arg{value} || -1);
2868
2869 $self
2870}
2871
2872sub set_label {
2873 my ($self, $label) = @_;
2874
2875 return if $self->{label} eq $label;
2876 $self->{label} = $label;
2877
2878 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2879}
2880
2881sub set_value {
2882 my ($self, $value) = @_;
2883
2884 if ($self->{value} ne $value) {
2885 $self->{value} = $value;
2886
2887 if ($value < 0) {
2888 $self->set_text ("-");
2889 } else {
2890 $self->set_text (sprintf $self->{label}, $value * 100);
2891 }
2892
2893 $self->update;
2894 }
2895}
2896
2897sub _draw {
2898 my ($self) = @_;
2899
2900 glEnable GL_BLEND;
2901 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2902
2903 my $px = $self->{padding_x};
2904 my $py = $self->{padding_y};
2905
2906 if ($self->{value} >= 0) {
2907 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2908
2909 glColor_premultiply @{$self->{bar}};
2910 glRect $px, $py, $s, $self->{h} - $py;
2911 glColor_premultiply @{$self->{bg}};
2912 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2913 }
2914
2915 glColor_premultiply @{$self->{outline}};
2916
2917 $px -= .5;
2918 $py -= .5;
2919
2920 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2921
2922 glDisable GL_BLEND;
2923
2924 {
2925 local $self->{bg}; # do not draw background
2926 $self->SUPER::_draw;
2927 }
2928}
2929
2930#############################################################################
2931
2932package DC::UI::ExperienceProgress;
2933
2934our @ISA = DC::UI::Progress::;
2935
2936sub new {
2937 my ($class, %arg) = @_;
2938
2939 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2940
2941 my $self = $class->SUPER::new (
2942 %arg,
2943 tooltip => sub {
2944 my ($self) = @_;
2945
2946 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2947 $tt,
2948 $self->{lvl},
2949 ::formsep ($self->{exp}),
2950 ::formsep ($self->{nxt}),
2951 ::formsep ($self->{nxt} - $self->{exp}),
2952 $self->_percent * 100,
2953 },
2954 );
2955
2956 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2957 if $::CONN;
2958
2959 $self
2960}
2961
2962sub DESTROY {
2963 my ($self) = @_;
2964
2965 delete $::CONN->{on_exp_update}{$self+0}
2966 if $::CONN;
2967
2968 $self->SUPER::DESTROY;
2969}
2970
2971sub _percent {
2972 my ($self) = @_;
2973
2974 my $table = $::CONN && $::CONN->{exp_table}
2975 or return -1;
2976
2977 my $l0 = $table->[$self->{lvl} - 1];
2978 my $l1 = $table->[$self->{lvl}];
2979
2980 $self->{nxt} = $l1;
2981
2982 ($self->{exp} - $l0) / ($l1 - $l0)
2983}
2984
2985sub set_value {
2986 my ($self, $lvl, $exp) = @_;
2987
2988 $self->{lvl} = $lvl;
2989 $self->{exp} = $exp;
2990
2991 $self->SUPER::set_value ($self->_percent);
2992}
2993
2994#############################################################################
2995
2590package CFPlus::UI::Gauge; 2996package DC::UI::Gauge;
2591 2997
2592our @ISA = CFPlus::UI::VBox::; 2998our @ISA = DC::UI::VBox::;
2593 2999
2594sub new { 3000sub new {
2595 my ($class, %arg) = @_; 3001 my ($class, %arg) = @_;
2596 3002
2597 my $self = $class->SUPER::new ( 3003 my $self = $class->SUPER::new (
2599 can_hover => 1, 3005 can_hover => 1,
2600 can_events => 1, 3006 can_events => 1,
2601 %arg, 3007 %arg,
2602 ); 3008 );
2603 3009
2604 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3010 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2605 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3011 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2606 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3012 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2607 3013
2608 $self 3014 $self
2609} 3015}
2610 3016
2611sub set_fontsize { 3017sub set_fontsize {
2632 $self->{value}->set_text ($val); 3038 $self->{value}->set_text ($val);
2633} 3039}
2634 3040
2635############################################################################# 3041#############################################################################
2636 3042
2637package CFPlus::UI::Slider; 3043package DC::UI::Slider;
2638 3044
2639use strict; 3045use strict;
2640 3046
2641use CFPlus::OpenGL; 3047use DC::OpenGL;
2642 3048
2643our @ISA = CFPlus::UI::DrawBG::; 3049our @ISA = DC::UI::DrawBG::;
2644 3050
2645my @tex = 3051my @tex =
2646 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3052 map { new_from_resource DC::Texture $_ }
2647 qw(s1_slider.png s1_slider_bg.png); 3053 qw(s1_slider.png s1_slider_bg.png);
2648 3054
2649sub new { 3055sub new {
2650 my $class = shift; 3056 my $class = shift;
2651 3057
2688sub set_value { 3094sub set_value {
2689 my ($self, $value) = @_; 3095 my ($self, $value) = @_;
2690 3096
2691 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3097 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2692 3098
2693 $hi = $lo + 1 if $hi <= $lo; 3099 $hi = $lo if $hi < $lo;
2694 3100
2695 $page = $hi - $lo if $page > $hi - $lo; 3101 $value = $hi - $page if $value > $hi - $page;
2696
2697 $value = $lo if $value < $lo; 3102 $value = $lo if $value < $lo;
2698 $value = $hi - $page if $value > $hi - $page;
2699 3103
2700 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3104 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2701 if $unit; 3105 if $unit;
2702 3106
2703 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3107 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2719 3123
2720 $self->SUPER::invoke_button_down ($ev, $x, $y); 3124 $self->SUPER::invoke_button_down ($ev, $x, $y);
2721 3125
2722 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3126 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2723 3127
2724 $self->invoke_mouse_motion ($ev, $x, $y) 3128 $self->invoke_mouse_motion ($ev, $x, $y);
3129
3130 1
2725} 3131}
2726 3132
2727sub invoke_mouse_motion { 3133sub invoke_mouse_motion {
2728 my ($self, $ev, $x, $y) = @_; 3134 my ($self, $ev, $x, $y) = @_;
2729 3135
2745sub invoke_mouse_wheel { 3151sub invoke_mouse_wheel {
2746 my ($self, $ev) = @_; 3152 my ($self, $ev) = @_;
2747 3153
2748 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3154 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2749 3155
2750 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3156 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2751 3157
2752 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3158 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2753 3159
2754 ! ! $delta 3160 1
2755} 3161}
2756 3162
2757sub update { 3163sub update {
2758 my ($self) = @_; 3164 my ($self) = @_;
2759 3165
2765 my ($self) = @_; 3171 my ($self) = @_;
2766 3172
2767 unless ($self->{knob_w}) { 3173 unless ($self->{knob_w}) {
2768 $self->set_value ($self->{range}[0]); 3174 $self->set_value ($self->{range}[0]);
2769 3175
2770 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3176 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2771 my $range = ($hi - $page - $lo) || 1e-100; 3177 my $range = ($hi - $page - $lo) || 1e-10;
2772 3178
2773 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3179 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
2774 3180
2775 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3181 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2776 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3182 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2777 3183
2778 $value = ($value - $lo) / $range; 3184 $value = ($value - $lo) / $range;
2808 glDisable GL_TEXTURE_2D; 3214 glDisable GL_TEXTURE_2D;
2809} 3215}
2810 3216
2811############################################################################# 3217#############################################################################
2812 3218
2813package CFPlus::UI::ValSlider; 3219package DC::UI::ValSlider;
2814 3220
2815our @ISA = CFPlus::UI::HBox::; 3221our @ISA = DC::UI::HBox::;
2816 3222
2817sub new { 3223sub new {
2818 my ($class, %arg) = @_; 3224 my ($class, %arg) = @_;
2819 3225
2820 my $range = delete $arg{range}; 3226 my $range = delete $arg{range};
2821 3227
2822 my $self = $class->SUPER::new ( 3228 my $self = $class->SUPER::new (
2823 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3229 slider => (new DC::UI::Slider expand => 1, range => $range),
2824 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3230 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2825 to_value => sub { shift }, 3231 to_value => sub { shift },
2826 from_value => sub { shift }, 3232 from_value => sub { shift },
2827 %arg, 3233 %arg,
2828 ); 3234 );
2829 3235
2849sub set_range { shift->{slider}->set_range (@_) } 3255sub set_range { shift->{slider}->set_range (@_) }
2850sub set_value { shift->{slider}->set_value (@_) } 3256sub set_value { shift->{slider}->set_value (@_) }
2851 3257
2852############################################################################# 3258#############################################################################
2853 3259
2854package CFPlus::UI::TextScroller; 3260package DC::UI::TextScroller;
2855 3261
2856our @ISA = CFPlus::UI::HBox::; 3262our @ISA = DC::UI::HBox::;
2857 3263
2858use CFPlus::OpenGL; 3264use DC::OpenGL;
2859 3265
2860sub new { 3266sub new {
2861 my $class = shift; 3267 my $class = shift;
2862 3268
2863 my $self = $class->SUPER::new ( 3269 my $self = $class->SUPER::new (
2865 can_events => 1, 3271 can_events => 1,
2866 indent => 0, 3272 indent => 0,
2867 #font => default_font 3273 #font => default_font
2868 @_, 3274 @_,
2869 3275
2870 layout => (new CFPlus::Layout), 3276 layout => (new DC::Layout),
2871 par => [], 3277 par => [],
2872 max_par => 0, 3278 max_par => 0,
2873 height => 0, 3279 height => 0,
2874 children => [ 3280 children => [
2875 (new CFPlus::UI::Empty expand => 1), 3281 (new DC::UI::Empty expand => 1),
2876 (new CFPlus::UI::Slider vertical => 1), 3282 (new DC::UI::Slider vertical => 1),
2877 ], 3283 ],
2878 ); 3284 );
2879 3285
2880 $self->{children}[1]->connect (changed => sub { $self->update }); 3286 $self->{children}[1]->connect (changed => sub { $self->update });
2881 3287
2890} 3296}
2891 3297
2892sub size_request { 3298sub size_request {
2893 my ($self) = @_; 3299 my ($self) = @_;
2894 3300
2895 my ($empty, $slider) = @{ $self->{children} }; 3301 my ($empty, $slider) = $self->visible_children;
2896 3302
2897 local $self->{children} = [$empty, $slider]; 3303 local $self->{children} = [$empty, $slider];
2898 $self->SUPER::size_request 3304 $self->SUPER::size_request
2899} 3305}
2900 3306
2937 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3343 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2938 $layout->set_markup ($para->{markup}); 3344 $layout->set_markup ($para->{markup});
2939 3345
2940 $layout->set_shapes ( 3346 $layout->set_shapes (
2941 map 3347 map
2942 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3348 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2943 @{$para->{widget}} 3349 @{$para->{widget}}
2944 ); 3350 );
2945 3351
2946 $layout 3352 $layout
2947} 3353}
3064 $ROOT->on_post_alloc ($self => sub { 3470 $ROOT->on_post_alloc ($self => sub {
3065 $self->force_uptodate; 3471 $self->force_uptodate;
3066 3472
3067 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3473 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3068 3474
3069 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3475 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3070 glClearColor 0, 0, 0, 0; 3476 glClearColor 0, 0, 0, 0;
3071 glClear GL_COLOR_BUFFER_BIT; 3477 glClear GL_COLOR_BUFFER_BIT;
3072 3478
3073 package CFPlus::UI::Base; 3479 package DC::UI::Base;
3074 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3480 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3075 (0, 0, $self->{w}, $self->{h}); 3481 (0, 0, $self->{w}, $self->{h});
3076 3482
3077 my $top = int $self->{children}[1]{range}[0]; 3483 my $top = int $self->{children}[1]{range}[0];
3078 3484
3089 3495
3090 if ($y0 < $y + $h && $y < $y1) { 3496 if ($y0 < $y + $h && $y < $y1) {
3091 my $layout = $self->get_layout ($para); 3497 my $layout = $self->get_layout ($para);
3092 3498
3093 $layout->render ($para->{indent}, $y - $y0); 3499 $layout->render ($para->{indent}, $y - $y0);
3500 $layout->draw;
3094 3501
3095 if (my @w = @{ $para->{widget} }) { 3502 if (my @w = @{ $para->{widget} }) {
3096 my @s = $layout->get_shapes; 3503 my @s = $layout->get_shapes;
3097 3504
3098 for (@w) { 3505 for (@w) {
3136 $self->{children}[1]->draw; 3543 $self->{children}[1]->draw;
3137} 3544}
3138 3545
3139############################################################################# 3546#############################################################################
3140 3547
3141package CFPlus::UI::Animator; 3548package DC::UI::Animator;
3142 3549
3143use CFPlus::OpenGL; 3550use DC::OpenGL;
3144 3551
3145our @ISA = CFPlus::UI::Bin::; 3552our @ISA = DC::UI::Bin::;
3146 3553
3147sub moveto { 3554sub moveto {
3148 my ($self, $x, $y) = @_; 3555 my ($self, $x, $y) = @_;
3149 3556
3150 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3557 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3178 glPopMatrix; 3585 glPopMatrix;
3179} 3586}
3180 3587
3181############################################################################# 3588#############################################################################
3182 3589
3183package CFPlus::UI::Flopper; 3590package DC::UI::Flopper;
3184 3591
3185our @ISA = CFPlus::UI::Button::; 3592our @ISA = DC::UI::Button::;
3186 3593
3187sub new { 3594sub new {
3188 my $class = shift; 3595 my $class = shift;
3189 3596
3190 my $self = $class->SUPER::new ( 3597 my $self = $class->SUPER::new (
3202 $self->{other}->toggle_visibility; 3609 $self->{other}->toggle_visibility;
3203} 3610}
3204 3611
3205############################################################################# 3612#############################################################################
3206 3613
3207package CFPlus::UI::Tooltip; 3614package DC::UI::Tooltip;
3208 3615
3209our @ISA = CFPlus::UI::Bin::; 3616our @ISA = DC::UI::Bin::;
3210 3617
3211use CFPlus::OpenGL; 3618use DC::OpenGL;
3212 3619
3213sub new { 3620sub new {
3214 my $class = shift; 3621 my $class = shift;
3215 3622
3216 $class->SUPER::new ( 3623 $class->SUPER::new (
3220} 3627}
3221 3628
3222sub set_tooltip_from { 3629sub set_tooltip_from {
3223 my ($self, $widget) = @_; 3630 my ($self, $widget) = @_;
3224 3631
3225 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3226 if $widget->{tooltip} =~ /^#(.*)$/;
3227
3228 my $tooltip = $widget->{tooltip}; 3632 my $tip = $widget->{tooltip};
3633 $tip = $tip->($widget) if "CODE" eq ref $tip;
3634
3635 $tip = DC::Pod::section_label tooltip => $1
3636 if $tip =~ /^#(.*)$/;
3229 3637
3230 if ($ENV{CFPLUS_DEBUG} & 2) { 3638 if ($ENV{CFPLUS_DEBUG} & 2) {
3231 $tooltip .= "\n\n" . (ref $widget) . "\n" 3639 $tip .= "\n\n" . (ref $widget) . "\n"
3232 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3640 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3233 . "req $widget->{req_w} $widget->{req_h}\n" 3641 . "req $widget->{req_w} $widget->{req_h}\n"
3234 . "visible $widget->{visible}"; 3642 . "visible $widget->{visible}";
3235 } 3643 }
3236 3644
3237 $tooltip =~ s/^\n+//; 3645 $tip =~ s/^\n+//;
3238 $tooltip =~ s/\n+$//; 3646 $tip =~ s/\n+$//;
3239 3647
3240 $self->add (new CFPlus::UI::Label 3648 $self->add (new DC::UI::Label
3649 fg => $DC::THEME{tooltip_fg},
3241 markup => $tooltip, 3650 markup => $tip,
3242 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3651 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3652 align => 0,
3243 fontsize => 0.8, 3653 fontsize => 0.8,
3244 style => 1, # FLAG_INVERSE 3654 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3245 ellipsise => 0, 3655 ellipsise => 0,
3246 font => ($widget->{tooltip_font} || $::FONT_PROP), 3656 font => ($widget->{tooltip_font} || $::FONT_PROP),
3247 ); 3657 );
3248} 3658}
3249 3659
3284} 3694}
3285 3695
3286sub _draw { 3696sub _draw {
3287 my ($self) = @_; 3697 my ($self) = @_;
3288 3698
3289 glTranslate 0.375, 0.375;
3290
3291 my ($w, $h) = @$self{qw(w h)}; 3699 my ($w, $h) = @$self{qw(w h)};
3292 3700
3293 glColor 1, 0.8, 0.4; 3701 glColor @{ $DC::THEME{tooltip_bg} };
3294 glBegin GL_QUADS; 3702 glRect 0, 0, $w, $h;
3295 glVertex 0 , 0;
3296 glVertex 0 , $h;
3297 glVertex $w, $h;
3298 glVertex $w, 0;
3299 glEnd;
3300 3703
3301 glColor 0, 0, 0; 3704 glColor @{ $DC::THEME{tooltip_border} };
3302 glBegin GL_LINE_LOOP; 3705 glRect_lineloop .5, .5, $w + .5, $h + .5;
3303 glVertex 0 , 0;
3304 glVertex 0 , $h;
3305 glVertex $w, $h;
3306 glVertex $w, 0;
3307 glEnd;
3308 3706
3309 glTranslate 2 - 0.375, 2 - 0.375; 3707 glTranslate 2, 2;
3310 3708
3311 $self->SUPER::_draw; 3709 $self->SUPER::_draw;
3312} 3710}
3313 3711
3314############################################################################# 3712#############################################################################
3315 3713
3316package CFPlus::UI::Face; 3714package DC::UI::Face;
3317 3715
3318our @ISA = CFPlus::UI::DrawBG::; 3716our @ISA = DC::UI::DrawBG::;
3319 3717
3320use CFPlus::OpenGL; 3718use DC::OpenGL;
3321 3719
3322sub new { 3720sub new {
3323 my $class = shift; 3721 my $class = shift;
3324 3722
3325 my $self = $class->SUPER::new ( 3723 my $self = $class->SUPER::new (
3724 size_w => 32,
3725 size_h => 8,
3326 aspect => 1, 3726 aspect => 1,
3327 can_events => 0, 3727 can_events => 0,
3328 @_, 3728 @_,
3329 ); 3729 );
3330 3730
3331 if ($self->{anim} && $self->{animspeed}) { 3731 $self->update_anim;
3332 CFPlus::weaken (my $widget = $self);
3333
3334 $self->{timer} = Event->timer (
3335 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3336 hard => 1,
3337 interval => $self->{animspeed},
3338 cb => sub {
3339 delete $self->{wait_face};
3340 ++$widget->{frame};
3341 $widget->update;
3342 },
3343 );
3344 }
3345 3732
3346 $self 3733 $self
3347} 3734}
3348 3735
3736sub update_timer {
3737 my ($self) = @_;
3738
3739 return unless $self->{timer};
3740
3741 if ($self->{visible}) {
3742 $self->{timer}->start;
3743 } else {
3744 $self->{timer}->stop;
3745 }
3746}
3747
3748sub update_face {
3749 my ($self) = @_;
3750
3751 if ($::CONN) {
3752 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3753 if ($anim && @$anim) {
3754 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3755 delete $self->{face_change_cb};
3756
3757 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3758 unless ($tex->{name} || $tex->{loading}) {
3759 $tex->upload (sub { $self->reconfigure });
3760 }
3761 }
3762 }
3763 }
3764 }
3765}
3766
3767sub update_anim {
3768 my ($self) = @_;
3769
3770 if ($self->{anim} && $self->{animspeed}) {
3771 DC::weaken (my $widget = $self);
3772
3773 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3774 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3775 return unless $::CONN;
3776
3777 my $w = $widget
3778 or return;
3779
3780 ++$w->{frame};
3781 $w->update_face;
3782
3783 # somehow, $widget can go away
3784 $w->update;
3785 $w->update_timer;
3786 };
3787
3788 $self->update_face;
3789 $self->update_timer;
3790 } else {
3791 delete $self->{timer};
3792 }
3793}
3794
3349sub size_request { 3795sub size_request {
3350 (32, 8) 3796 my ($self) = @_;
3797
3798 if ($::CONN) {
3799 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3800 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3801 if ($tex->{name}) {
3802 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3803 } elsif (!$tex->{loading}) {
3804 $tex->upload (sub { $self->reconfigure });
3805 }
3806 }
3807
3808 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3809 }
3810 }
3811
3812 ($self->{size_w} || 8, $self->{size_h} || 8)
3351} 3813}
3352 3814
3353sub update { 3815sub update {
3354 my ($self) = @_; 3816 my ($self) = @_;
3355 3817
3356 return unless $self->{visible}; 3818 return unless $self->{visible};
3357 3819
3358 $self->SUPER::update; 3820 $self->SUPER::update;
3359} 3821}
3360 3822
3823sub set_face {
3824 my ($self, $face) = @_;
3825
3826 $self->{face} = $face;
3827 $self->reconfigure;
3828}
3829
3830sub set_anim {
3831 my ($self, $anim) = @_;
3832
3833 $self->{anim} = $anim;
3834 $self->update_anim;
3835}
3836
3837sub set_animspeed {
3838 my ($self, $animspeed) = @_;
3839
3840 $self->{animspeed} = $animspeed;
3841 $self->update_anim;
3842}
3843
3844sub invoke_visibility_change {
3845 my ($self) = @_;
3846
3847 $self->update_timer;
3848
3849 0
3850}
3851
3361sub _draw { 3852sub _draw {
3362 my ($self) = @_; 3853 my ($self) = @_;
3363 3854
3364 return unless $::CONN;
3365
3366 $self->SUPER::_draw; 3855 $self->SUPER::_draw;
3367 3856
3368 my $face; 3857 if (my $tex = $self->{tex}) {
3369
3370 if ($self->{frame}) {
3371 my $anim = $::CONN->{anim}[$self->{anim}];
3372
3373 $face = $anim->[ $self->{frame} % @$anim ]
3374 if $anim && @$anim;
3375 }
3376
3377 my $faceid = $::CONN->{faceid}[$face || $self->{face}]
3378 or return;
3379
3380 my $tex = $::CONN->{texture}[$faceid];
3381
3382 if ($tex) {
3383 glEnable GL_TEXTURE_2D; 3858 glEnable GL_TEXTURE_2D;
3384 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3385 glColor 0, 0, 0, 1; 3860 glColor 0, 0, 0, 1;
3386 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3861 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3387 glDisable GL_TEXTURE_2D; 3862 glDisable GL_TEXTURE_2D;
3388 } else {
3389 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3390 $self->update;
3391 });
3392 } 3863 }
3393} 3864}
3394 3865
3395sub destroy { 3866sub destroy {
3396 my ($self) = @_; 3867 my ($self) = @_;
3397 3868
3398 $self->{timer}->cancel 3869 (delete $self->{timer})->cancel
3399 if $self->{timer}; 3870 if $self->{timer};
3400 3871
3401 $self->SUPER::destroy; 3872 $self->SUPER::destroy;
3402} 3873}
3403 3874
3404############################################################################# 3875#############################################################################
3405 3876
3406package CFPlus::UI::Buttonbar; 3877package DC::UI::Buttonbar;
3407 3878
3408our @ISA = CFPlus::UI::HBox::; 3879our @ISA = DC::UI::HBox::;
3409 3880
3410# TODO: should actually wrap buttons and other goodies. 3881# TODO: should actually wrap buttons and other goodies.
3411 3882
3412############################################################################# 3883#############################################################################
3413 3884
3414package CFPlus::UI::Menu; 3885package DC::UI::Menu;
3415 3886
3416our @ISA = CFPlus::UI::Toplevel::; 3887our @ISA = DC::UI::Toplevel::;
3417 3888
3418use CFPlus::OpenGL; 3889use DC::OpenGL;
3419 3890
3420sub new { 3891sub new {
3421 my $class = shift; 3892 my $class = shift;
3422 3893
3423 my $self = $class->SUPER::new ( 3894 my $self = $class->SUPER::new (
3424 items => [], 3895 items => [],
3425 z => 100, 3896 z => 100,
3426 @_, 3897 @_,
3427 ); 3898 );
3428 3899
3429 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3900 $self->add ($self->{vbox} = new DC::UI::VBox);
3430 3901
3431 for my $item (@{ $self->{items} }) { 3902 for my $item (@{ $self->{items} }) {
3432 my ($widget, $cb, $tooltip) = @$item; 3903 my ($widget, $cb, $tooltip) = @$item;
3433 3904
3434 # handle various types of items, only text for now 3905 # handle various types of items, only text for now
3435 if (!ref $widget) { 3906 if (!ref $widget) {
3436 if ($widget =~ /\t/) { 3907 if ($widget =~ /\t/) {
3437 my ($left, $right) = split /\t/, $widget, 2; 3908 my ($left, $right) = split /\t/, $widget, 2;
3438 3909
3439 $widget = new CFPlus::UI::HBox 3910 $widget = new DC::UI::HBox
3440 can_hover => 1, 3911 can_hover => 1,
3441 can_events => 1, 3912 can_events => 1,
3442 tooltip => $tooltip, 3913 tooltip => $tooltip,
3443 children => [ 3914 children => [
3444 (new CFPlus::UI::Label markup => $left, expand => 1), 3915 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3445 (new CFPlus::UI::Label markup => $right, align => +1), 3916 (new DC::UI::Label markup => $right, align => 1),
3446 ], 3917 ],
3447 ; 3918 ;
3448 3919
3449 } else { 3920 } else {
3450 $widget = new CFPlus::UI::Label 3921 $widget = new DC::UI::Label
3451 can_hover => 1, 3922 can_hover => 1,
3452 can_events => 1, 3923 can_events => 1,
3924 align => 0,
3453 markup => $widget, 3925 markup => $widget,
3454 tooltip => $tooltip; 3926 tooltip => $tooltip;
3455 } 3927 }
3456 } 3928 }
3457 3929
3472 # maybe save $GRAB? must be careful about events... 3944 # maybe save $GRAB? must be careful about events...
3473 $GRAB = $self; 3945 $GRAB = $self;
3474 $self->{button} = $ev->{button}; 3946 $self->{button} = $ev->{button};
3475 3947
3476 $self->show; 3948 $self->show;
3477 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3949
3950 my $x = $ev->{x};
3951 my $y = $ev->{y};
3952
3953 $self->{root}->on_post_alloc ($self => sub {
3954 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3955 });
3956
3957 1 # so it can be used inside event handlers
3478} 3958}
3479 3959
3480sub invoke_mouse_motion { 3960sub invoke_mouse_motion {
3481 my ($self, $ev, $x, $y) = @_; 3961 my ($self, $ev, $x, $y) = @_;
3482 3962
3503 1 3983 1
3504} 3984}
3505 3985
3506############################################################################# 3986#############################################################################
3507 3987
3508package CFPlus::UI::Multiplexer; 3988package DC::UI::Multiplexer;
3509 3989
3510our @ISA = CFPlus::UI::Container::; 3990our @ISA = DC::UI::Container::;
3511 3991
3512sub new { 3992sub new {
3513 my $class = shift; 3993 my $class = shift;
3514 3994
3515 my $self = $class->SUPER::new ( 3995 my $self = $class->SUPER::new (
3516 @_, 3996 @_,
3517 ); 3997 );
3518 3998
3519 $self->{current} = $self->{children}[0] 3999 $self->set_current_page (0);
3520 if @{ $self->{children} };
3521 4000
3522 $self 4001 $self
3523} 4002}
3524 4003
3525sub add { 4004sub add {
3526 my ($self, @widgets) = @_; 4005 my ($self, @widgets) = @_;
3527 4006
3528 $self->SUPER::add (@widgets); 4007 $self->SUPER::add (@widgets);
3529 4008
3530 $self->{current} = $self->{children}[0] 4009 $self->set_current_page (0)
3531 if @{ $self->{children} }; 4010 if @widgets == @{ $self->{children} };
3532} 4011}
3533 4012
3534sub get_current_page { 4013sub get_current_page {
3535 my ($self) = @_; 4014 my ($self) = @_;
3536 4015
3542 4021
3543 my $widget = ref $page_or_widget 4022 my $widget = ref $page_or_widget
3544 ? $page_or_widget 4023 ? $page_or_widget
3545 : $self->{children}[$page_or_widget]; 4024 : $self->{children}[$page_or_widget];
3546 4025
4026 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4027
3547 $self->{current} = $widget; 4028 if (($self->{current} = $widget)) {
4029 $self->{current}->set_visible if $self->{current} && $self->{visible};
3548 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4030 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3549 4031
3550 $self->emit (page_changed => $self->{current}); 4032 $self->emit (page_changed => $self->{current});
4033 }
3551 4034
3552 $self->realloc; 4035 $self->realloc;
3553} 4036}
3554 4037
3555sub visible_children { 4038sub visible_children {
3556 $_[0]{current} 4039 $_[0]{current} || ()
3557} 4040}
3558 4041
3559sub size_request { 4042sub size_request {
3560 my ($self) = @_; 4043 my ($self) = @_;
3561 4044
4045 $self->{current}
3562 $self->{current}->size_request 4046 ? $self->{current}->size_request
4047 : (0, 0)
3563} 4048}
3564 4049
3565sub invoke_size_allocate { 4050sub invoke_size_allocate {
3566 my ($self, $w, $h) = @_; 4051 my ($self, $w, $h) = @_;
3567 4052
3568 $self->{current}->configure (0, 0, $w, $h); 4053 $self->{current}->configure (0, 0, $w, $h)
4054 if $self->{current};
3569 4055
3570 1 4056 1
3571} 4057}
3572 4058
3573sub _draw { 4059sub _draw {
3574 my ($self) = @_; 4060 my ($self) = @_;
3575 4061
3576 $self->{current}->draw; 4062 $self->{current}->draw
4063 if $self->{current};
3577} 4064}
3578 4065
3579############################################################################# 4066#############################################################################
3580 4067
3581package CFPlus::UI::Notebook; 4068package DC::UI::Notebook;
3582 4069
4070use DC::OpenGL;
4071
3583our @ISA = CFPlus::UI::VBox::; 4072our @ISA = DC::UI::VBox::;
3584 4073
3585sub new { 4074sub new {
3586 my $class = shift; 4075 my $class = shift;
3587 4076
3588 my $self = $class->SUPER::new ( 4077 my $self = $class->SUPER::new (
3589 buttonbar => (new CFPlus::UI::Buttonbar), 4078 buttonbar => (new DC::UI::Buttonbar),
3590 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4079 multiplexer => (new DC::UI::Multiplexer expand => 1),
4080 active_outline => [.7, .7, 0.2],
3591 # filter => # will be put between multiplexer and $self 4081 # filter => # will be put between multiplexer and $self
3592 @_, 4082 @_,
3593 ); 4083 );
3594 4084
3595 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4085 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3596 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4086 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3597 4087
4088 {
4089 Scalar::Util::weaken (my $wself = $self);
4090
4091 $self->{multiplexer}->connect (c_add => sub {
4092 my ($mplex, $widgets) = @_;
4093
4094 for my $child (@$widgets) {
4095 Scalar::Util::weaken $child;
4096 $child->{c_tab_} ||= do {
4097 my $tab =
4098 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4099 ? $child->{c_tab}
4100 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4101
4102 $tab->connect (activate => sub {
4103 $wself->set_current_page ($child);
4104 });
4105
4106 $tab
4107 };
4108
4109 $self->{buttonbar}->add ($child->{c_tab_});
4110 }
4111 });
4112
4113 $self->{multiplexer}->connect (c_remove => sub {
4114 my ($mplex, $widgets) = @_;
4115
4116 for my $child (@$widgets) {
4117 $wself->{buttonbar}->remove ($child->{c_tab_});
4118 }
4119 });
4120 }
4121
3598 $self 4122 $self
3599} 4123}
3600 4124
3601sub add { 4125sub add {
4126 my ($self, @widgets) = @_;
4127
4128 $self->{multiplexer}->add (@widgets)
4129}
4130
4131sub remove {
4132 my ($self, @widgets) = @_;
4133
4134 $self->{multiplexer}->remove (@widgets)
4135}
4136
4137sub pages {
4138 my ($self) = @_;
4139 $self->{multiplexer}->children
4140}
4141
4142sub page_index {
4143 my ($self, $widget) = @_;
4144
4145 my $i = 0;
4146 for ($self->pages) {
4147 if ($_ eq $widget) { return $i };
4148 $i++;
4149 }
4150
4151 undef
4152}
4153
4154sub add_tab {
3602 my ($self, $title, $widget, $tooltip) = @_; 4155 my ($self, $title, $widget, $tooltip) = @_;
3603 4156
3604 CFPlus::weaken $self; 4157 $title = [$title, $tooltip] unless ref $title;
4158 $widget->{c_tab} = $title;
3605 4159
3606 $self->{buttonbar}->add (new CFPlus::UI::Button
3607 markup => $title,
3608 tooltip => $tooltip,
3609 on_activate => sub { $self->set_current_page ($widget) },
3610 );
3611
3612 $self->{multiplexer}->add ($widget); 4160 $self->add ($widget);
3613} 4161}
3614 4162
3615sub get_current_page { 4163sub get_current_page {
3616 my ($self) = @_; 4164 my ($self) = @_;
3617 4165
3623 4171
3624 $self->{multiplexer}->set_current_page ($page); 4172 $self->{multiplexer}->set_current_page ($page);
3625 $self->emit (page_changed => $self->{multiplexer}{current}); 4173 $self->emit (page_changed => $self->{multiplexer}{current});
3626} 4174}
3627 4175
4176sub _draw {
4177 my ($self) = @_;
4178
4179 $self->SUPER::_draw ();
4180
4181 if (my $cur = $self->{multiplexer}{current}) {
4182 if ($cur = $cur->{c_tab_}) {
4183 glTranslate $self->{buttonbar}{x} + $cur->{x},
4184 $self->{buttonbar}{y} + $cur->{y};
4185 glLineWidth 3;
4186 #glEnable GL_BLEND;
4187 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4188 glColor @{$self->{active_outline}};
4189 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4190 glLineWidth 1;
4191 #glDisable GL_BLEND;
4192 }
4193 }
4194}
4195
3628############################################################################# 4196#############################################################################
3629 4197
3630package CFPlus::UI::Selector; 4198package DC::UI::Selector;
3631 4199
3632use utf8; 4200use utf8;
3633 4201
3634our @ISA = CFPlus::UI::Button::; 4202our @ISA = DC::UI::Button::;
3635 4203
3636sub new { 4204sub new {
3637 my $class = shift; 4205 my $class = shift;
3638 4206
3639 my $self = $class->SUPER::new ( 4207 my $self = $class->SUPER::new (
3656 my ($value, $title, $tooltip) = @$_; 4224 my ($value, $title, $tooltip) = @$_;
3657 4225
3658 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4226 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3659 } 4227 }
3660 4228
3661 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4229 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3662} 4230}
3663 4231
3664sub _set_value { 4232sub _set_value {
3665 my ($self, $value) = @_; 4233 my ($self, $value) = @_;
3666 4234
3667 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4235 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4236 $item ||= $self->{options}[0]
3668 or return; 4237 or return;
3669 4238
3670 $self->{value} = $item->[0]; 4239 $self->{value} = $item->[0];
3671 $self->set_markup ("$item->[1] ⇓"); 4240 $self->set_markup ("$item->[1] ⇓");
3672 $self->set_tooltip ($item->[2]); 4241# $self->set_tooltip ($item->[2]);
3673} 4242}
3674 4243
3675sub set_value { 4244sub set_value {
3676 my ($self, $value) = @_; 4245 my ($self, $value) = @_;
3677 4246
3679 4248
3680 $self->_set_value ($value); 4249 $self->_set_value ($value);
3681 $self->emit (changed => $value); 4250 $self->emit (changed => $value);
3682} 4251}
3683 4252
4253sub set_options {
4254 my ($self, $options) = @_;
4255
4256 $self->{options} = $options;
4257 $self->_set_value ($self->{value});
4258}
4259
3684############################################################################# 4260#############################################################################
3685 4261
3686package CFPlus::UI::Statusbox; 4262package DC::UI::Statusbox;
3687 4263
3688our @ISA = CFPlus::UI::VBox::; 4264our @ISA = DC::UI::VBox::;
3689 4265
3690sub new { 4266sub new {
3691 my $class = shift; 4267 my $class = shift;
3692 4268
3693 my $self = $class->SUPER::new ( 4269 my $self = $class->SUPER::new (
3694 fontsize => 0.8, 4270 fontsize => 0.8,
3695 @_, 4271 @_,
3696 ); 4272 );
3697 4273
3698 CFPlus::weaken (my $this = $self); 4274 DC::weaken (my $this = $self);
3699 4275
3700 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4276 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3701 4277
3702 $self 4278 $self
3703} 4279}
3704 4280
3705sub reorder { 4281sub reorder {
3706 my ($self) = @_; 4282 my ($self) = @_;
3707 my $NOW = Time::HiRes::time; 4283 my $NOW = EV::time;
3708 4284
3709 # freeze display when hovering over any label 4285 # freeze display when hovering over any label
3710 return if $CFPlus::UI::TOOLTIP->{owner} 4286 return if $DC::UI::TOOLTIP->{owner}
3711 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4287 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3712 values %{ $self->{item} }; 4288 values %{ $self->{item} };
3713 4289
3714 while (my ($k, $v) = each %{ $self->{item} }) { 4290 while (my ($k, $v) = each %{ $self->{item} }) {
3715 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4291 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3716 } 4292 }
4293
4294 $self->{timer}->set (1, 1);
3717 4295
3718 my @widgets; 4296 my @widgets;
3719 4297
3720 my @items = sort { 4298 my @items = sort {
3721 $a->{pri} <=> $b->{pri} 4299 $a->{pri} <=> $b->{pri}
3722 or $b->{id} <=> $a->{id} 4300 or $b->{id} <=> $a->{id}
3723 } values %{ $self->{item} }; 4301 } values %{ $self->{item} };
3724
3725 $self->{timer}->interval (1);
3726 4302
3727 my $count = 10 + 1; 4303 my $count = 10 + 1;
3728 for my $item (@items) { 4304 for my $item (@items) {
3729 last unless --$count; 4305 last unless --$count;
3730 4306
3737 for ($short) { 4313 for ($short) {
3738 s/^\s+//; 4314 s/^\s+//;
3739 s/\s+/ /g; 4315 s/\s+/ /g;
3740 } 4316 }
3741 4317
3742 new CFPlus::UI::Label 4318 new DC::UI::Label
3743 markup => $short, 4319 markup => $short,
3744 tooltip => $item->{tooltip}, 4320 tooltip => $item->{tooltip},
3745 tooltip_font => $::FONT_PROP, 4321 tooltip_font => $::FONT_PROP,
3746 tooltip_width => 0.67, 4322 tooltip_width => 0.67,
3747 fontsize => $item->{fontsize} || $self->{fontsize}, 4323 fontsize => $item->{fontsize} || $self->{fontsize},
3748 max_w => $::WIDTH * 0.44, 4324 max_w => $::WIDTH * 0.44,
4325 align => 0,
3749 fg => [@{ $item->{fg} }], 4326 fg => [@{ $item->{fg} }],
3750 can_events => 1, 4327 can_events => 1,
3751 can_hover => 1 4328 can_hover => 1
3752 }; 4329 };
3753 4330
3754 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4331 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3755 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4332 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3756 $label->update; 4333 $label->update;
3757 $label->set_max_size (undef, $label->{req_h} * $diff) 4334 $label->set_max_size (undef, $label->{req_h} * $diff)
3758 if $diff < 1; 4335 if $diff < 1;
3759 $self->{timer}->interval (1/30); 4336 $self->{timer}->set (1/30, 1/30);
3760 } else { 4337 } else {
3761 $label->{fg}[3] = $item->{fg}[3] || 1; 4338 $label->{fg}[3] = $item->{fg}[3] || 1;
3762 } 4339 }
3763 4340
3764 push @widgets, $label; 4341 push @widgets, $label;
3765 } 4342 }
4343
4344 my $hash = join ",", @widgets;
4345 return if $hash eq $self->{last_widget_hash};
4346 $self->{last_widget_hash} = $hash;
3766 4347
3767 $self->clear; 4348 $self->clear;
3768 $self->SUPER::add (reverse @widgets); 4349 $self->SUPER::add (reverse @widgets);
3769} 4350}
3770 4351
3826 $self->SUPER::destroy; 4407 $self->SUPER::destroy;
3827} 4408}
3828 4409
3829############################################################################# 4410#############################################################################
3830 4411
3831package CFPlus::UI::Root; 4412package DC::UI::Root;
3832 4413
3833our @ISA = CFPlus::UI::Container::; 4414our @ISA = DC::UI::Container::;
3834 4415
3835use List::Util qw(min max); 4416use List::Util qw(min max);
3836 4417
3837use CFPlus::OpenGL; 4418use DC::OpenGL;
3838 4419
3839sub new { 4420sub new {
3840 my $class = shift; 4421 my $class = shift;
3841 4422
3842 my $self = $class->SUPER::new ( 4423 my $self = $class->SUPER::new (
3843 visible => 1, 4424 visible => 1,
3844 @_, 4425 @_,
3845 ); 4426 );
3846 4427
3847 CFPlus::weaken ($self->{root} = $self); 4428 DC::weaken ($self->{root} = $self);
3848 4429
3849 $self 4430 $self
3850} 4431}
3851 4432
3852sub size_request { 4433sub size_request {
3900} 4481}
3901 4482
3902sub update { 4483sub update {
3903 my ($self) = @_; 4484 my ($self) = @_;
3904 4485
3905 $::WANT_REFRESH++; 4486 $::WANT_REFRESH = 1;
3906} 4487}
3907 4488
3908sub add { 4489sub add {
3909 my ($self, @children) = @_; 4490 my ($self, @children) = @_;
3910 4491
3947 while ($self->{refresh_hook}) { 4528 while ($self->{refresh_hook}) {
3948 $_->() 4529 $_->()
3949 for values %{delete $self->{refresh_hook}}; 4530 for values %{delete $self->{refresh_hook}};
3950 } 4531 }
3951 4532
3952 if ($self->{realloc}) { 4533 while ($self->{realloc}) {
3953 my %queue; 4534 my %queue;
3954 my @queue; 4535 my @queue;
3955 my $widget; 4536 my $widget;
3956 4537
3957 outer: 4538 outer:
3977 4558
3978 delete $queue{$widget+0}; 4559 delete $queue{$widget+0};
3979 4560
3980 my ($w, $h) = $widget->size_request; 4561 my ($w, $h) = $widget->size_request;
3981 4562
3982 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4563 $w += $widget->{padding_x} * 2;
3983 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4564 $h += $widget->{padding_y} * 2;
4565
4566 $w = max $widget->{min_w}, $w;
4567 $h = max $widget->{min_h}, $h;
3984 4568
3985 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4569 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3986 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4570 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3987 4571
3988 $w = $widget->{force_w} if exists $widget->{force_w}; 4572 $w = $widget->{force_w} if exists $widget->{force_w};
4004 } 4588 }
4005 } 4589 }
4006 4590
4007 delete $self->{realloc}{$widget+0}; 4591 delete $self->{realloc}{$widget+0};
4008 } 4592 }
4009 }
4010 4593
4011 while (my $size_alloc = delete $self->{size_alloc}) { 4594 while (my $size_alloc = delete $self->{size_alloc}) {
4012 my @queue = sort { $b->{visible} <=> $a->{visible} } 4595 my @queue = sort { $a->{visible} <=> $b->{visible} }
4013 values %$size_alloc; 4596 values %$size_alloc;
4014 4597
4015 while () { 4598 while () {
4016 my $widget = pop @queue || last; 4599 my $widget = pop @queue || last;
4017 4600
4018 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4601 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4019 4602
4020 $w = 0 if $w < 0;
4021 $h = 0 if $h < 0;
4022
4023 $w = max $widget->{min_w}, $w; 4603 $w = max $widget->{min_w}, $w;
4024 $h = max $widget->{min_h}, $h; 4604 $h = max $widget->{min_h}, $h;
4025 4605
4026# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4606# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4027# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4607# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4028 4608
4029 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4609 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4030 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4610 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4031 4611
4032 $w = int $w + 0.5; 4612 $w = int $w + 0.5;
4033 $h = int $h + 0.5; 4613 $h = int $h + 0.5;
4034 4614
4035 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4615 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4036 $widget->{old_w} = $widget->{w}; 4616 $widget->{old_w} = $widget->{w};
4037 $widget->{old_h} = $widget->{h}; 4617 $widget->{old_h} = $widget->{h};
4038 4618
4039 $widget->{w} = $w; 4619 $widget->{w} = $w;
4040 $widget->{h} = $h; 4620 $widget->{h} = $h;
4041 4621
4042 $widget->emit (size_allocate => $w, $h); 4622 $widget->emit (size_allocate => $w, $h);
4623 }
4043 } 4624 }
4044 } 4625 }
4045 } 4626 }
4046 4627
4047 while ($self->{post_alloc_hook}) { 4628 while ($self->{post_alloc_hook}) {
4048 $_->() 4629 $_->()
4049 for values %{delete $self->{post_alloc_hook}}; 4630 for values %{delete $self->{post_alloc_hook}};
4050 } 4631 }
4051
4052 4632
4053 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4633 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4054 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4634 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4055 glClear GL_COLOR_BUFFER_BIT; 4635 glClear GL_COLOR_BUFFER_BIT;
4056 4636
4059 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4639 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4060 glMatrixMode GL_MODELVIEW; 4640 glMatrixMode GL_MODELVIEW;
4061 glLoadIdentity; 4641 glLoadIdentity;
4062 4642
4063 { 4643 {
4064 package CFPlus::UI::Base; 4644 package DC::UI::Base;
4065 4645
4066 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4646 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4067 (0, 0, $self->{w}, $self->{h}); 4647 (0, 0, $self->{w}, $self->{h});
4068 4648
4069 $self->_draw; 4649 $self->_draw;
4070 } 4650 }
4071} 4651}
4072 4652
4073############################################################################# 4653#############################################################################
4074 4654
4075package CFPlus::UI; 4655package DC::UI;
4076 4656
4077$ROOT = new CFPlus::UI::Root; 4657$ROOT = new DC::UI::Root;
4078$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4658$TOOLTIP = new DC::UI::Tooltip z => 900;
4079 4659
40801 46601
4081

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines