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.397 by root, Sat Jul 21 14:01:35 2007 UTC vs.
Revision 1.479 by root, Thu Feb 4 20:01:09 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines