ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.371 by root, Sun Jul 15 22:07:48 2007 UTC vs.
Revision 1.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 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9; 116 my $delta = $ev->{button} * 2 - 9;
117 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
118
119 $ev->{dx} = $shift ? $delta : 0;
120 $ev->{dy} = $shift ? 0 : $delta;
121
120 $GRAB->emit (mouse_wheel => $ev); 122 $GRAB->emit (mouse_wheel => $ev);
121 } else { 123 } else {
122 $GRAB->emit (button_down => $ev) 124 $GRAB->emit (button_down => $ev)
123 } 125 }
124 } 126 }
138 my $grab = $GRAB; undef $GRAB; 140 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 141 $grab->update if $grab;
140 $GRAB->update if $GRAB; 142 $GRAB->update if $GRAB;
141 143
142 check_hover $widget; 144 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 145 $TOOLTIP_WATCHER->invoke;
144 } 146 }
145} 147}
146 148
147sub feed_sdl_motion_event { 149sub feed_sdl_motion_event {
148 my ($ev) = @_; 150 my ($ev) = @_;
188# call when resolution changes etc. 190# call when resolution changes etc.
189sub rescale_widgets { 191sub rescale_widgets {
190 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
191 193
192 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $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.]+$/;
195 $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.]+$/;
196 198
197 $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.]+$/;
198 $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};
210 reconfigure_widgets; 212 reconfigure_widgets;
211} 213}
212 214
213############################################################################# 215#############################################################################
214 216
215package CFPlus::UI::Event; 217package DC::UI::Event;
216 218
217sub xy { 219sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 220 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 221}
220 222
221############################################################################# 223#############################################################################
222 224
223package CFPlus::UI::Base; 225package DC::UI::Base;
224 226
225use strict; 227use strict;
226 228
227use CFPlus::OpenGL; 229use DC::OpenGL;
228 230
229sub new { 231sub new {
230 my $class = shift; 232 my $class = shift;
231 233
232 my $self = bless { 234 my $self = bless {
237 h => undef, 239 h => undef,
238 can_events => 1, 240 can_events => 1,
239 @_ 241 @_
240 }, $class; 242 }, $class;
241 243
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 244 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 245
244 for (keys %$self) { 246 for (keys %$self) {
245 if (/^on_(.*)$/) { 247 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 248 $self->connect ($1 => delete $self->{$_});
247 } 249 }
248 } 250 }
249 251
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 252 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $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};
252 $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};
253 $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};
254 $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};
255 257
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 258 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 259 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 260
259 $self->show if $layout->{show}; 261 $self->show if $layout->{show};
269 $self->emit ("destroy"); 271 $self->emit ("destroy");
270 %$self = (); 272 %$self = ();
271} 273}
272 274
273sub TO_JSON { 275sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} } 276 { "\fw" => $_[0]{s_id} }
275} 277}
276 278
277sub show { 279sub show {
278 my ($self) = @_; 280 my ($self) = @_;
279 281
280 return if $self->{parent}; 282 return if $self->{parent};
281 283
282 $CFPlus::UI::ROOT->add ($self); 284 $DC::UI::ROOT->add ($self);
283} 285}
284 286
285sub set_visible { 287sub set_visible {
286 my ($self) = @_; 288 my ($self) = @_;
287 289
288 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);
289 294
290 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
292 297
293 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
294 299
295 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
296 301
297 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
298} 303}
299 304
300sub set_invisible { 305sub set_invisible {
301 my ($self) = @_; 306 my ($self) = @_;
302 307
308 delete $self->{root}; 313 delete $self->{root};
309 314
310 undef $GRAB if $GRAB == $self; 315 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 316 undef $HOVER if $HOVER == $self;
312 317
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 318 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 319 if $TOOLTIP->{owner} == $self;
315 320
316 $self->emit ("focus_out"); 321 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 322 $self->emit (visibility_change => 0);
318} 323}
337sub hide { 342sub hide {
338 my ($self) = @_; 343 my ($self) = @_;
339 344
340 $self->set_invisible; 345 $self->set_invisible;
341 346
347 # extra $parent copy for 5.8.8+ bug workaround
348 # (otherwise $_[0] in remove gets freed
349 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 350 $parent->remove ($self);
343 if $self->{parent}; 351 }
344} 352}
345 353
346sub move_abs { 354sub move_abs {
347 my ($self, $x, $y, $z) = @_; 355 my ($self, $x, $y, $z) = @_;
348 356
360 $self->{force_h} = $h; 368 $self->{force_h} = $h;
361 369
362 $self->realloc; 370 $self->realloc;
363} 371}
364 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
365sub size_request { 391sub size_request {
366 require Carp; 392 require Carp;
367 Carp::confess "size_request is abstract"; 393 Carp::confess "size_request is abstract";
368} 394}
369 395
375 my ($self, $x, $y, $w, $h) = @_; 401 my ($self, $x, $y, $w, $h) = @_;
376 402
377 if ($self->{aspect}) { 403 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 404 my ($ow, $oh) = ($w, $h);
379 405
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 406 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 407 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 408
383 # use alignment to adjust x, y 409 # use alignment to adjust x, y
384 410
385 $x += int 0.5 * ($ow - $w); 411 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 412 $y += int 0.5 * ($oh - $h);
427 453
428 return if $self->{tooltip} eq $tooltip; 454 return if $self->{tooltip} eq $tooltip;
429 455
430 $self->{tooltip} = $tooltip; 456 $self->{tooltip} = $tooltip;
431 457
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 458 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 459 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 460 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 461 }
436} 462}
437 463
438# translate global coordinates to local coordinate system 464# translate global coordinates to local coordinate system
439sub coord2local { 465sub coord2local {
440 my ($self, $x, $y) = @_; 466 my ($self, $x, $y) = @_;
441 467
442 Carp::confess unless $self->{parent};#d# 468 return (undef, undef) unless $self->{parent};
443 469
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 470 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 471}
446 472
447# translate local coordinates to global coordinate system 473# translate local coordinates to global coordinate system
448sub coord2global { 474sub coord2global {
449 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
450 476
451 Carp::confess unless $self->{parent};#d# 477 return (undef, undef) unless $self->{parent};
452 478
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 479 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 480}
455 481
456sub invoke_focus_in { 482sub invoke_focus_in {
505sub connect { 531sub connect {
506 my ($self, $signal, $cb) = @_; 532 my ($self, $signal, $cb) = @_;
507 533
508 push @{ $self->{signal_cb}{$signal} }, $cb; 534 push @{ $self->{signal_cb}{$signal} }, $cb;
509 535
510 defined wantarray and CFPlus::guard { 536 defined wantarray and Guard::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 537 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 538 @{ $self->{signal_cb}{$signal} };
513 } 539 }
514} 540}
515 541
547 573
548 # parent 574 # parent
549 $self->{parent} && $self->{parent}->emit ($signal, @args) 575 $self->{parent} && $self->{parent}->emit ($signal, @args)
550} 576}
551 577
552sub find_widget { 578#sub find_widget {
553 my ($self, $x, $y) = @_; 579# in .xs
554
555 return () unless $self->{can_events};
556
557 return $self
558 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
559 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
560
561 ()
562}
563 580
564sub set_parent { 581sub set_parent {
565 my ($self, $parent) = @_; 582 my ($self, $parent) = @_;
566 583
567 CFPlus::weaken ($self->{parent} = $parent); 584 DC::weaken ($self->{parent} = $parent);
568 $self->set_visible if $parent->{visible}; 585 $self->set_visible if $parent->{visible};
569} 586}
570 587
571sub realloc { 588sub realloc {
572 my ($self) = @_; 589 my ($self) = @_;
598 615
599# 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
600# functions seems pointless. 617# functions seems pointless.
601our ($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
602 619
603sub draw { 620#sub draw {
604 my ($self) = @_; 621#CFPlus.xs
605
606 return unless $self->{h} && $self->{w};
607
608 # update screen rectangle
609 local $draw_x = $draw_x + $self->{x};
610 local $draw_y = $draw_y + $self->{y};
611
612 # skip widgets that are entirely outside the drawing area
613 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
614 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
615
616 glPushMatrix;
617 glTranslate $self->{x}, $self->{y}, 0;
618
619 if ($self == $HOVER && $self->{can_hover}) {
620 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
621 glEnable GL_BLEND;
622 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
623 glBegin GL_QUADS;
624 glVertex 0 , 0;
625 glVertex $self->{w}, 0;
626 glVertex $self->{w}, $self->{h};
627 glVertex 0 , $self->{h};
628 glEnd;
629 glDisable GL_BLEND;
630 }
631
632 if ($ENV{CFPLUS_DEBUG} & 1) {
633 glPushMatrix;
634 glColor 1, 1, 0, 1;
635 glTranslate 0.375, 0.375;
636 glBegin GL_LINE_LOOP;
637 glVertex 0 , 0;
638 glVertex $self->{w} - 1, 0;
639 glVertex $self->{w} - 1, $self->{h} - 1;
640 glVertex 0 , $self->{h} - 1;
641 glEnd;
642 glPopMatrix;
643 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
644 }
645
646 $self->_draw;
647 glPopMatrix;
648}
649 622
650sub _draw { 623sub _draw {
651 my ($self) = @_; 624 my ($self) = @_;
652 625
653 warn "no draw defined for $self\n"; 626 warn "no draw defined for $self\n";
654} 627}
655 628
656my $cntx;#d#
657sub DESTROY { 629sub DESTROY {
658 my ($self) = @_; 630 my ($self) = @_;
659 631
660 return if CFPlus::in_destruct; 632 return if DC::in_destruct;
661 633
634 local $@;
662 eval { $self->destroy }; 635 eval { $self->destroy };
663 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
664 637
665 delete $WIDGET{$self+0}; 638 delete $WIDGET{$self+0};
666} 639}
667 640
668############################################################################# 641#############################################################################
669 642
670package CFPlus::UI::DrawBG; 643package DC::UI::DrawBG;
671 644
672our @ISA = CFPlus::UI::Base::; 645our @ISA = DC::UI::Base::;
673 646
674use strict; 647use strict;
675use CFPlus::OpenGL; 648use DC::OpenGL;
676 649
677sub new { 650sub new {
678 my $class = shift; 651 my $class = shift;
679
680 # range [value, low, high, page]
681 652
682 $class->SUPER::new ( 653 $class->SUPER::new (
683 #bg => [0, 0, 0, 0.2], 654 #bg => [0, 0, 0, 0.2],
684 #active_bg => [1, 1, 1, 0.5], 655 #active_bg => [1, 1, 1, 0.5],
685 @_ 656 @_
686 ) 657 )
687} 658}
688 659
660sub set_bg {
661 my ($self, $bg) = @_;
662
663 $self->{bg} = $bg;
664 $self->update;
665}
666
689sub _draw { 667sub _draw {
690 my ($self) = @_; 668 my ($self) = @_;
691 669
692 my $color = $FOCUS == $self && $self->{active_bg} 670 my $color = $FOCUS == $self
693 ? $self->{active_bg} 671 ? $self->{active_bg} || $self->{bg}
694 : $self->{bg}; 672 : $self->{bg};
695 673
696 if ($color && (@$color < 4 || $color->[3])) { 674 if ($color && (@$color < 4 || $color->[3])) {
697 my ($w, $h) = @$self{qw(w h)}; 675 my ($w, $h) = @$self{qw(w h)};
698 676
699 glEnable GL_BLEND; 677 glEnable GL_BLEND;
700 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 678 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
701 glColor_premultiply @$color; 679 glColor_premultiply @$color;
702
703 glBegin GL_QUADS;
704 glVertex 0 , 0;
705 glVertex 0 , $h;
706 glVertex $w, $h; 680 glRect 0, 0, $w, $h;
707 glVertex $w, 0;
708 glEnd;
709
710 glDisable GL_BLEND; 681 glDisable GL_BLEND;
711 } 682 }
712} 683}
713 684
714############################################################################# 685#############################################################################
715 686
716package CFPlus::UI::Empty; 687package DC::UI::Empty;
717 688
718our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
719 690
720sub new { 691sub new {
721 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
722 $class->SUPER::new (can_events => 0, %arg); 693 $class->SUPER::new (can_events => 0, %arg);
723} 694}
730 701
731sub draw { } 702sub draw { }
732 703
733############################################################################# 704#############################################################################
734 705
735package CFPlus::UI::Container; 706package DC::UI::Container;
736 707
737our @ISA = CFPlus::UI::Base::; 708our @ISA = DC::UI::Base::;
738 709
739sub new { 710sub new {
740 my ($class, %arg) = @_; 711 my ($class, %arg) = @_;
741 712
742 my $children = delete $arg{children}; 713 my $children = delete $arg{children};
746 can_events => 0, 717 can_events => 0,
747 %arg, 718 %arg,
748 ); 719 );
749 720
750 $self->add (@$children) 721 $self->add (@$children)
751 if $children; 722 if $children && @$children;
752 723
753 $self 724 $self
754} 725}
755 726
756sub realloc { 727sub realloc {
765 my ($self, @widgets) = @_; 736 my ($self, @widgets) = @_;
766 737
767 $_->set_parent ($self) 738 $_->set_parent ($self)
768 for @widgets; 739 for @widgets;
769 740
741 # TODO: only do this in widgets that need it, e.g. root, fixed
770 use sort 'stable'; 742 use sort 'stable';
771 743
772 $self->{children} = [ 744 $self->{children} = [
773 sort { $a->{z} <=> $b->{z} } 745 sort { $a->{z} <=> $b->{z} }
774 @{$self->{children}}, @widgets 746 @{$self->{children}}, @widgets
775 ]; 747 ];
776 748
777 $self->realloc; 749 $self->realloc;
750
751 $self->emit (c_add => \@widgets);
752
753 map $_+0, @widgets
778} 754}
779 755
780sub children { 756sub children {
781 @{ $_[0]{children} } 757 @{ $_[0]{children} }
782} 758}
783 759
784sub remove { 760sub remove {
785 my ($self, $child) = @_; 761 my ($self, @widgets) = @_;
786 762
763 $self->emit (c_remove => \@widgets);
764
765 for my $child (@widgets) {
787 delete $child->{parent}; 766 delete $child->{parent};
788 $child->hide; 767 $child->hide;
789
790 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 768 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
769 }
791 770
792 $self->realloc; 771 $self->realloc;
793} 772}
794 773
795sub clear { 774sub clear {
796 my ($self) = @_; 775 my ($self) = @_;
797 776
798 my $children = delete $self->{children}; 777 my $children = $self->{children};
799 $self->{children} = []; 778 $self->{children} = [];
800 779
801 for (@$children) { 780 for (@$children) {
802 delete $_->{parent}; 781 delete $_->{parent};
803 $_->hide; 782 $_->hide;
823} 802}
824 803
825sub _draw { 804sub _draw {
826 my ($self) = @_; 805 my ($self) = @_;
827 806
828 $_->draw for @{$self->{children}}; 807 $_->draw for $self->visible_children;
829} 808}
830 809
831############################################################################# 810#############################################################################
832 811
833package CFPlus::UI::Bin; 812package DC::UI::Bin;
834 813
835our @ISA = CFPlus::UI::Container::; 814our @ISA = DC::UI::Container::;
836 815
837sub new { 816sub new {
838 my ($class, %arg) = @_; 817 my ($class, %arg) = @_;
839 818
840 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 819 my $child = (delete $arg{child}) || new DC::UI::Empty::;
841 820
842 $class->SUPER::new (children => [$child], %arg) 821 $class->SUPER::new (children => [$child], %arg)
843} 822}
844 823
845sub add { 824sub add {
846 my ($self, $child) = @_; 825 my ($self, $child) = @_;
847 826
848 $self->SUPER::remove ($_) for @{ $self->{children} }; 827 $self->clear;
849 $self->SUPER::add ($child); 828 $self->SUPER::add ($child);
850} 829}
851 830
852sub remove { 831sub remove {
853 my ($self, $widget) = @_; 832 my ($self, $widget) = @_;
854 833
855 $self->SUPER::remove ($widget); 834 $self->SUPER::remove ($widget);
856 835
857 $self->{children} = [new CFPlus::UI::Empty] 836 $self->{children} = [new DC::UI::Empty]
858 unless @{$self->{children}}; 837 unless @{$self->{children}};
859} 838}
860 839
861sub child { $_[0]->{children}[0] } 840sub child { $_[0]->{children}[0] }
862 841
871 850
872 1 851 1
873} 852}
874 853
875############################################################################# 854#############################################################################
876
877# back-buffered drawing area 855# back-buffered drawing area
878 856
879package CFPlus::UI::Window; 857package DC::UI::Window;
880 858
881our @ISA = CFPlus::UI::Bin::; 859our @ISA = DC::UI::Bin::;
882 860
883use CFPlus::OpenGL; 861use DC::OpenGL;
884 862
885sub new { 863sub new {
886 my ($class, %arg) = @_; 864 my ($class, %arg) = @_;
887 865
888 my $self = $class->SUPER::new (%arg); 866 my $self = $class->SUPER::new (%arg);
910} 888}
911 889
912sub render_child { 890sub render_child {
913 my ($self) = @_; 891 my ($self) = @_;
914 892
915 $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 {
916 glClearColor 0, 0, 0, 0; 894 glClearColor 0, 0, 0, 0;
917 glClear GL_COLOR_BUFFER_BIT; 895 glClear GL_COLOR_BUFFER_BIT;
918 896
919 { 897 {
920 package CFPlus::UI::Base; 898 package DC::UI::Base;
921 899
922 local ($draw_x, $draw_y, $draw_w, $draw_h) = 900 local ($draw_x, $draw_y, $draw_w, $draw_h) =
923 (0, 0, $self->{w}, $self->{h}); 901 (0, 0, $self->{w}, $self->{h});
924 902
925 $self->_render; 903 $self->_render;
942 glDisable GL_TEXTURE_2D; 920 glDisable GL_TEXTURE_2D;
943} 921}
944 922
945############################################################################# 923#############################################################################
946 924
947package CFPlus::UI::ViewPort; 925package DC::UI::ViewPort;
948 926
927use List::Util qw(min max);
928
949our @ISA = CFPlus::UI::Window::; 929our @ISA = DC::UI::Window::;
950 930
951sub new { 931sub new {
952 my $class = shift; 932 my $class = shift;
953 933
954 $class->SUPER::new ( 934 $class->SUPER::new (
961sub size_request { 941sub size_request {
962 my ($self) = @_; 942 my ($self) = @_;
963 943
964 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 944 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
965 945
966 $w = 10 if $self->{scroll_x}; 946 $w = 1 if $self->{scroll_x};
967 $h = 10 if $self->{scroll_y}; 947 $h = 1 if $self->{scroll_y};
968 948
969 ($w, $h) 949 ($w, $h)
970} 950}
971 951
972sub invoke_size_allocate { 952sub invoke_size_allocate {
984} 964}
985 965
986sub set_offset { 966sub set_offset {
987 my ($self, $x, $y) = @_; 967 my ($self, $x, $y) = @_;
988 968
969 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
970 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
971
972 if ($x != $self->{view_x} or $y != $self->{view_y}) {
989 $self->{view_x} = int $x; 973 $self->{view_x} = $x;
990 $self->{view_y} = int $y; 974 $self->{view_y} = $y;
991 975
976 $self->emit (changed => $x, $y);
992 $self->update; 977 $self->update;
978 }
979}
980
981sub set_center {
982 my ($self, $x, $y) = @_;
983
984 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
985}
986
987sub make_visible {
988 my ($self, $x, $y, $border) = @_;
989
990 if ( $x < $self->{view_x} + $self->{w} * $border
991 || $x > $self->{view_x} + $self->{w} * (1 - $border)
992 || $y < $self->{view_y} + $self->{h} * $border
993 || $y > $self->{view_y} + $self->{h} * (1 - $border)
994 ) {
995 $self->set_center ($x, $y);
996 }
993} 997}
994 998
995# 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
996sub coord2local { 1000sub coord2local {
997 my ($self, $x, $y) = @_; 1001 my ($self, $x, $y) = @_;
1012 my ($self, $x, $y) = @_; 1016 my ($self, $x, $y) = @_;
1013 1017
1014 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1015 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1016 ) { 1020 ) {
1017 $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})
1018 } else { 1022 } else {
1019 $self->CFPlus::UI::Base::find_widget ($x, $y) 1023 $self->DC::UI::Base::find_widget ($x, $y)
1020 } 1024 }
1021} 1025}
1022 1026
1023sub _render { 1027sub _render {
1024 my ($self) = @_; 1028 my ($self) = @_;
1025 1029
1026 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};
1027 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};
1028 1032
1029 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1033 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1030 1034
1031 $self->SUPER::_render; 1035 $self->SUPER::_render;
1032} 1036}
1033 1037
1034############################################################################# 1038#############################################################################
1035 1039
1036package CFPlus::UI::ScrolledWindow; 1040package DC::UI::ScrolledWindow;
1037 1041
1038our @ISA = CFPlus::UI::Table::; 1042our @ISA = DC::UI::Table::;
1039 1043
1040sub new { 1044sub new {
1041 my ($class, %arg) = @_; 1045 my ($class, %arg) = @_;
1042 1046
1043 my $child = delete $arg{child}; 1047 my $child = delete $arg{child};
1044 1048
1045 my $self; 1049 my $self;
1046 1050
1047 my $hslider = new CFPlus::UI::Slider 1051 my $hslider = new DC::UI::Slider
1052 c_col => 0,
1053 c_row => 1,
1048 vertical => 0, 1054 vertical => 0,
1049 range => [0, 0, 1, 0.01], # HACK fix 1055 range => [0, 0, 1, 0.01], # HACK fix
1050 on_changed => sub { 1056 on_changed => sub {
1051 $self->{hpos} = $_[1]; 1057 $self->{hpos} = $_[1];
1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1053 }, 1059 },
1054 ; 1060 ;
1055 1061
1056 my $vslider = new CFPlus::UI::Slider 1062 my $vslider = new DC::UI::Slider
1063 c_col => 1,
1064 c_row => 0,
1057 vertical => 1, 1065 vertical => 1,
1058 range => [0, 0, 1, 0.01], # HACK fix 1066 range => [0, 0, 1, 0.01], # HACK fix
1059 on_changed => sub { 1067 on_changed => sub {
1060 $self->{vpos} = $_[1]; 1068 $self->{vpos} = $_[1];
1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1062 }, 1070 },
1063 ; 1071 ;
1064 1072
1065 $self = $class->SUPER::new ( 1073 $self = $class->SUPER::new (
1066 vp => (new CFPlus::UI::ViewPort expand => 1), 1074 scroll_x => 0,
1075 scroll_y => 1,
1067 can_events => 1, 1076 can_events => 1,
1068 hslider => $hslider, 1077 hslider => $hslider,
1069 vslider => $vslider, 1078 vslider => $vslider,
1070 col_expand => [1, 0], 1079 col_expand => [1, 0],
1071 row_expand => [1, 0], 1080 row_expand => [1, 0],
1072 %arg, 1081 %arg,
1073 ); 1082 );
1074 1083
1084 $self->{vp} = new DC::UI::ViewPort
1085 c_col => 0,
1086 c_row => 0,
1087 expand => 1,
1088 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y},
1090 on_changed => sub {
1091 my ($vp, $x, $y) = @_;
1092
1093 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y);
1095
1096 0
1097 },
1098 on_size_allocate => sub {
1099 my ($vp, $w, $h) = @_;
1100 $vp->{parent}->update_slider;
1101 0
1102 },
1103 ;
1104
1075 $self->SUPER::add (0, 0, $self->{vp}); 1105 $self->SUPER::add ($self->{vp});
1076 $self->SUPER::add (1, 0, $self->{vslider});
1077 $self->SUPER::add (0, 1, $self->{hslider});
1078 1106
1079 $self->add ($child) if $child; 1107 $self->add ($child) if $child;
1080 1108
1081 $self 1109 $self
1082} 1110}
1083
1084#TODO# update range on size_allocate depending on child
1085 1111
1086sub add { 1112sub add {
1087 my ($self, $widget) = @_; 1113 my ($self, $widget) = @_;
1088 1114
1089 $self->{vp}->add ($self->{child} = $widget); 1115 $self->{vp}->add ($self->{child} = $widget);
1090} 1116}
1091 1117
1118sub set_offset { shift->{vp}->set_offset (@_) }
1119sub set_center { shift->{vp}->set_center (@_) }
1120sub make_visible { shift->{vp}->make_visible (@_) }
1121
1122sub update_slider {
1123 my ($self) = @_;
1124
1125 my $child = ($self->{vp} or return)->child;
1126
1127 if ($self->{scroll_x}) {
1128 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1129 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1130
1131 my $visible = $w1 > $w2;
1132 if ($visible != $self->{hslider_visible}) {
1133 $self->{hslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{hslider})
1135 : $self->SUPER::remove ($self->{hslider});
1136 }
1137 }
1138
1139 if ($self->{scroll_y}) {
1140 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1141 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1142
1143 my $visible = $h1 > $h2;
1144 if ($visible != $self->{vslider_visible}) {
1145 $self->{vslider_visible} = $visible;
1146 $visible ? $self->SUPER::add ($self->{vslider})
1147 : $self->SUPER::remove ($self->{vslider});
1148 }
1149 }
1150}
1151
1152sub start_dragging {
1153 my ($self, $ev) = @_;
1154
1155 $self->grab_focus;
1156
1157 my $ox = $self->{vp}{view_x};
1158 my $oy = $self->{vp}{view_y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $ox -= $ev->{xrel};
1164 $oy -= $ev->{yrel};
1165
1166 $self->{vp}->set_offset ($ox, $oy);
1167 };
1168}
1169
1092sub invoke_mouse_wheel { 1170sub invoke_mouse_wheel {
1093 my ($self, $ev) = @_; 1171 my ($self, $ev) = @_;
1094 1172
1095 return 0 unless $ev->{dy}; # only vertical movements
1096
1097 $self->{vslider}->emit (mouse_wheel => $ev); 1173 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1174 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1098 1175
1099 1 1176 1
1100} 1177}
1101 1178
1102sub update_slider { 1179sub invoke_button_down {
1103 my ($self) = @_; 1180 my ($self, $ev, $x, $y) = @_;
1104 1181
1105 my $child = ($self->{vp} or return)->child; 1182 if ($ev->{button} == 2) {
1183 $self->start_dragging ($ev);
1184 return 1;
1185 }
1106 1186
1107 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1187 0
1108 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1109 #$self->{hslider}->set_visibility ($w1 != $w2);
1110
1111 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1112 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1113 #$self->{vslider}->set_visibility ($h1 != $h2);
1114} 1188}
1115 1189
1116sub update { 1190sub invoke_button_up {
1117 my ($self) = @_; 1191 my ($self, $ev, $x, $y) = @_;
1118 1192
1119 $self->SUPER::update; 1193 if (delete $self->{motion}) {
1120 $self->update_slider; 1194 return 1;
1195 }
1196
1197 0
1198}
1199
1200sub invoke_mouse_motion {
1201 my ($self, $ev, $x, $y) = @_;
1202
1203 if ($self->{motion}) {
1204 $self->{motion}->($ev, $x, $y);
1205 return 1;
1206 }
1207
1208 0
1121} 1209}
1122 1210
1123sub invoke_size_allocate { 1211sub invoke_size_allocate {
1124 my ($self, $w, $h) = @_; 1212 my ($self, $w, $h) = @_;
1125 1213
1127 $self->SUPER::invoke_size_allocate ($w, $h) 1215 $self->SUPER::invoke_size_allocate ($w, $h)
1128} 1216}
1129 1217
1130############################################################################# 1218#############################################################################
1131 1219
1132package CFPlus::UI::Frame; 1220package DC::UI::Frame;
1133 1221
1134our @ISA = CFPlus::UI::Bin::; 1222our @ISA = DC::UI::Bin::;
1135 1223
1136use CFPlus::OpenGL; 1224use DC::OpenGL;
1137 1225
1138sub new { 1226sub new {
1139 my $class = shift; 1227 my $class = shift;
1140 1228
1141 $class->SUPER::new ( 1229 $class->SUPER::new (
1151 my ($w, $h) = @$self{qw(w h)}; 1239 my ($w, $h) = @$self{qw(w h)};
1152 1240
1153 glEnable GL_BLEND; 1241 glEnable GL_BLEND;
1154 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1242 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1155 glColor_premultiply @{ $self->{bg} }; 1243 glColor_premultiply @{ $self->{bg} };
1156
1157 glBegin GL_QUADS;
1158 glVertex 0 , 0;
1159 glVertex 0 , $h;
1160 glVertex $w, $h; 1244 glRect 0, 0, $w, $h;
1161 glVertex $w, 0;
1162 glEnd;
1163
1164 glDisable GL_BLEND; 1245 glDisable GL_BLEND;
1165 } 1246 }
1166 1247
1167 $self->SUPER::_draw; 1248 $self->SUPER::_draw;
1168} 1249}
1169 1250
1170############################################################################# 1251#############################################################################
1171 1252
1172package CFPlus::UI::FancyFrame; 1253package DC::UI::FancyFrame;
1173 1254
1174our @ISA = CFPlus::UI::Bin::; 1255our @ISA = DC::UI::Bin::;
1175 1256
1176use CFPlus::OpenGL; 1257use DC::OpenGL;
1177 1258
1178sub new { 1259sub new {
1179 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1180 1261
1181 if ((exists $arg{label}) && !ref $arg{label}) { 1262 if ((exists $arg{label}) && !ref $arg{label}) {
1182 $arg{label} = new CFPlus::UI::Label 1263 $arg{label} = new DC::UI::Label
1183 align => 1, 1264 align => 1,
1184 valign => 0, 1265 valign => 0.5,
1185 text => $arg{label}, 1266 text => $arg{label},
1186 fontsize => ($arg{border} || 0.8) * 0.75; 1267 fontsize => ($arg{border} || 0.8) * 0.75;
1187 } 1268 }
1188 1269
1189 my $self = $class->SUPER::new ( 1270 my $self = $class->SUPER::new (
1190 # label => "", 1271 # label => "",
1191 fg => [0.6, 0.3, 0.1], 1272 fg => undef,
1192 border => 0.8, 1273 border => 0.8,
1193 style => 'single', 1274 style => 'single',
1194 %arg, 1275 %arg,
1195 ); 1276 );
1196 1277
1199 1280
1200sub add { 1281sub add {
1201 my ($self, @widgets) = @_; 1282 my ($self, @widgets) = @_;
1202 1283
1203 $self->SUPER::add (@widgets); 1284 $self->SUPER::add (@widgets);
1204 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1285 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1205} 1286}
1206 1287
1207sub border { 1288sub border {
1208 int $_[0]{border} * $::FONTSIZE 1289 int $_[0]{border} * $::FONTSIZE
1209} 1290}
1249 my $border = $self->border; 1330 my $border = $self->border;
1250 my ($w, $h) = ($self->{w}, $self->{h}); 1331 my ($w, $h) = ($self->{w}, $self->{h});
1251 1332
1252 $child->draw; 1333 $child->draw;
1253 1334
1254 glColor @{$self->{fg}}; 1335 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1255 glBegin GL_LINE_STRIP; 1336 glBegin GL_LINE_STRIP;
1256 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1337 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1257 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1338 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1258 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;
1259 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;
1267 } 1348 }
1268} 1349}
1269 1350
1270############################################################################# 1351#############################################################################
1271 1352
1272package CFPlus::UI::Toplevel; 1353package DC::UI::Toplevel;
1273 1354
1274our @ISA = CFPlus::UI::Bin::; 1355our @ISA = DC::UI::Bin::;
1275 1356
1276use CFPlus::OpenGL; 1357use DC::OpenGL;
1277 1358
1278my $bg = 1359my $bg =
1279 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1360 new_from_resource DC::Texture "d1_bg.png",
1280 mipmap => 1, wrap => 1; 1361 mipmap => 1, wrap => 1;
1281 1362
1282my @border = 1363my @border =
1283 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1364 map { new_from_resource DC::Texture $_, mipmap => 1 }
1284 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1365 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1366
1367my @icon =
1368 map { new_from_resource DC::Texture $_, mipmap => 1 }
1369 qw(x1_move.png x1_resize.png);
1285 1370
1286sub new { 1371sub new {
1287 my ($class, %arg) = @_; 1372 my ($class, %arg) = @_;
1288 1373
1289 my $self = $class->SUPER::new ( 1374 my $self = $class->SUPER::new (
1290 bg => [1, 1, 1, 1], 1375 bg => [1, 1, 1, 1],
1291 border_bg => [1, 1, 1, 1], 1376 border_bg => [1, 1, 1, 1],
1292 border => 0.6, 1377 border => 0.8,
1293 can_events => 1, 1378 can_events => 1,
1294 min_w => 64, 1379 min_w => 64,
1295 min_h => 32, 1380 min_h => 32,
1296 %arg, 1381 %arg,
1297 ); 1382 );
1298 1383
1299 $self->{title_widget} = new CFPlus::UI::Label 1384 $self->{title_widget} = new DC::UI::Label
1300 align => 0, 1385 align => 0.5,
1301 valign => 1, 1386 valign => 1,
1302 text => $self->{title}, 1387 text => $self->{title},
1303 fontsize => $self->{border}, 1388 fontsize => $self->{border},
1304 if exists $self->{title}; 1389 if exists $self->{title};
1305 1390
1306 if ($self->{has_close_button}) { 1391 if ($self->{has_close_button}) {
1307 $self->{close_button} = 1392 $self->{close_button} =
1308 new CFPlus::UI::ImageButton 1393 new DC::UI::ImageButton
1309 path => 'x1_close.png', 1394 path => 'x1_close.png',
1310 on_activate => sub { $self->emit ("delete") }; 1395 on_activate => sub { $self->emit ("delete") };
1311 1396
1312 $self->CFPlus::UI::Container::add ($self->{close_button}); 1397 $self->DC::UI::Container::add ($self->{close_button});
1313 } 1398 }
1314 1399
1315 $self 1400 $self
1316} 1401}
1317 1402
1318sub add { 1403sub add {
1319 my ($self, @widgets) = @_; 1404 my ($self, @widgets) = @_;
1320 1405
1321 $self->SUPER::add (@widgets); 1406 $self->SUPER::add (@widgets);
1322 $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};
1323 $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};
1324} 1409}
1325 1410
1326sub border { 1411sub border {
1327 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
1328} 1422}
1329 1423
1330sub size_request { 1424sub size_request {
1331 my ($self) = @_; 1425 my ($self) = @_;
1332 1426
1457 glEnable GL_TEXTURE_2D; 1551 glEnable GL_TEXTURE_2D;
1458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1459 1553
1460 my $border = $self->border; 1554 my $border = $self->border;
1461 1555
1556 if ($border) {
1462 glColor @{ $self->{border_bg} }; 1557 glColor @{ $self->{border_bg} };
1463 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1558 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1464 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1559 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1465 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1560 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1466 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1561 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1562
1563 # move
1564 my $w2 = ($w - $border) * .5;
1565 my $h2 = ($h - $border) * .5;
1566 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1567 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1568 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1569
1570 # resize
1571 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1572 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1573 unless $self->{has_close_button};
1574 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1575 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1576 }
1467 1577
1468 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1578 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1469 glColor @{ $self->{bg} }; 1579 glColor @{ $self->{bg} };
1470 1580
1471 # TODO: repeat texture not scale 1581 # TODO: repeat texture not scale
1490 if $self->{close_button}; 1600 if $self->{close_button};
1491} 1601}
1492 1602
1493############################################################################# 1603#############################################################################
1494 1604
1495package CFPlus::UI::Table; 1605package DC::UI::Table;
1496 1606
1497our @ISA = CFPlus::UI::Base::; 1607our @ISA = DC::UI::Container::;
1498 1608
1499use List::Util qw(max sum); 1609use List::Util qw(max sum);
1500 1610
1501use CFPlus::OpenGL; 1611use DC::OpenGL;
1502 1612
1503sub new { 1613sub new {
1504 my $class = shift; 1614 my $class = shift;
1505 1615
1506 $class->SUPER::new ( 1616 $class->SUPER::new (
1508 row_expand => [], 1618 row_expand => [],
1509 @_, 1619 @_,
1510 ) 1620 )
1511} 1621}
1512 1622
1513sub children {
1514 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1515}
1516
1517sub add { 1623sub add {
1624 my ($self, @widgets) = @_;
1625
1626 for my $child (@widgets) {
1627 $child->{c_rowspan} ||= 1;
1628 $child->{c_colspan} ||= 1;
1629 }
1630
1631 $self->SUPER::add (@widgets);
1632}
1633
1634sub add_at {
1518 my ($self) = shift; 1635 my $self = shift;
1636
1637 my @widgets;
1519 1638
1520 while (@_) { 1639 while (@_) {
1521 my ($x, $y, $child) = splice @_, 0, 3, (); 1640 my ($col, $row, $child) = splice @_, 0, 3, ();
1522 $child->set_parent ($self);
1523 $self->{children}[$y][$x] = $child;
1524 }
1525 1641
1526 $self->{force_realloc} = 1; 1642 $child->{c_row} = $row;
1527 $self->{force_size_alloc} = 1; 1643 $child->{c_col} = $col;
1528 $self->realloc;
1529}
1530 1644
1531sub remove { 1645 push @widgets, $child;
1646 }
1647
1648 $self->add (@widgets);
1649}
1650
1651sub get_wh {
1532 my ($self, $child) = @_; 1652 my ($self) = @_;
1533 1653
1534 # TODO: not yet implemented 1654 my (@w, @h);
1535}
1536
1537# TODO: move to container class maybe? send children a signal on removal?
1538sub clear {
1539 my ($self) = @_;
1540 1655
1541 my @children = $self->children; 1656 my @children = $self->children;
1542 delete $self->{children}; 1657
1658 # first pass, columns
1659 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1660 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1661
1662 my $sw = sum @w[$c .. $c + $cs - 1];
1663
1664 if ($w > $sw) {
1665 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1666 }
1543 1667 }
1544 for (@children) {
1545 delete $_->{parent};
1546 $_->hide;
1547 }
1548 1668
1549 $self->realloc; 1669 # second pass, rows
1550} 1670 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1551
1552sub get_wh {
1553 my ($self) = @_;
1554
1555 my (@w, @h);
1556
1557 for my $y (0 .. $#{$self->{children}}) {
1558 my $row = $self->{children}[$y]
1559 or next;
1560
1561 for my $x (0 .. $#$row) {
1562 my $widget = $row->[$x]
1563 or next;
1564 my ($w, $h) = @$widget{qw(req_w req_h)}; 1671 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1565 1672
1566 $w[$x] = max $w[$x], $w; 1673 my $sh = sum @h[$r .. $r + $rs - 1];
1567 $h[$y] = max $h[$y], $h; 1674
1675 if ($h > $sh) {
1676 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1568 } 1677 }
1569 } 1678 }
1570 1679
1571 (\@w, \@h) 1680 (\@w, \@h)
1572} 1681}
1588 my ($ws, $hs) = $self->get_wh; 1697 my ($ws, $hs) = $self->get_wh;
1589 1698
1590 my $req_w = (sum @$ws) || 1; 1699 my $req_w = (sum @$ws) || 1;
1591 my $req_h = (sum @$hs) || 1; 1700 my $req_h = (sum @$hs) || 1;
1592 1701
1593 # TODO: nicer code 1702 # now linearly scale the rows/columns to the allocated size
1594 my @col_expand = @{$self->{col_expand}}; 1703 my @col_expand = @{$self->{col_expand}};
1595 @col_expand = (1) x @$ws unless @col_expand; 1704 @col_expand = (1) x @$ws unless @col_expand;
1596 my $col_expand = (sum @col_expand) || 1; 1705 my $col_expand = (sum @col_expand) || 1;
1597 1706
1598 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1707 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1599 1708
1600 CFPlus::UI::harmonize $ws; 1709 DC::UI::harmonize $ws;
1601 1710
1602 my @row_expand = @{$self->{row_expand}}; 1711 my @row_expand = @{$self->{row_expand}};
1603 @row_expand = (1) x @$ws unless @row_expand; 1712 @row_expand = (1) x @$ws unless @row_expand;
1604 my $row_expand = (sum @row_expand) || 1; 1713 my $row_expand = (sum @row_expand) || 1;
1605 1714
1606 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1715 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1607 1716
1608 CFPlus::UI::harmonize $hs; 1717 DC::UI::harmonize $hs;
1609 1718
1610 my $y; 1719 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1720 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1611 1721
1612 for my $r (0 .. $#{$self->{children}}) { 1722 for my $widget ($self->children) {
1613 my $row = $self->{children}[$r] 1723 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1614 or next;
1615 1724
1616 my $x = 0; 1725 $widget->configure (
1617 my $row_h = $hs->[$r]; 1726 $x[$c], $y[$r],
1727 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1618 1728 );
1619 for my $c (0 .. $#$row) { 1729 }
1620 my $col_w = $ws->[$c];
1621 1730
1622 if (my $widget = $row->[$c]) { 1731 1
1623 $widget->configure ($x, $y, $col_w, $row_h); 1732}
1624 }
1625 1733
1626 $x += $col_w; 1734#############################################################################
1735
1736package DC::UI::Fixed;
1737
1738use List::Util qw(min max);
1739
1740our @ISA = DC::UI::Container::;
1741
1742sub _scale($$$) {
1743 my ($rel, $val, $max) = @_;
1744
1745 $rel ? $val * $max : $val
1746}
1747
1748sub size_request {
1749 my ($self) = @_;
1750
1751 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1752
1753 # determine overall size by querying abs widgets
1754 for my $child ($self->visible_children) {
1755 unless ($child->{c_rel}) {
1756 my $x = $child->{c_x};
1757 my $y = $child->{c_y};
1758
1759 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1760 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1627 } 1761 }
1762 }
1628 1763
1629 $y += $row_h; 1764 my $W = $x2 - $x1;
1765 my $H = $y2 - $y1;
1766
1767 # now layout remaining widgets
1768 for my $child ($self->visible_children) {
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;
1772
1773 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1774 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1775 }
1776 }
1777
1778 my $W = $x2 - $x1;
1779 my $H = $y2 - $y1;
1780
1781 ($W, $H)
1782}
1783
1784sub invoke_size_allocate {
1785 my ($self, $W, $H) = @_;
1786
1787 for my $child ($self->visible_children) {
1788 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1789 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1790
1791 $x += $child->{c_halign} * $child->{req_w};
1792 $y += $child->{c_valign} * $child->{req_h};
1793
1794 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1630 } 1795 }
1631 1796
1632 1 1797 1
1633} 1798}
1634 1799
1635sub find_widget {
1636 my ($self, $x, $y) = @_;
1637
1638 $x -= $self->{x};
1639 $y -= $self->{y};
1640
1641 my $res;
1642
1643 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1644 $res = $_->find_widget ($x, $y)
1645 and return $res;
1646 }
1647
1648 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1649}
1650
1651sub _draw {
1652 my ($self) = @_;
1653
1654 for (grep $_, @{$self->{children}}) {
1655 $_->draw for grep $_, @$_;
1656 }
1657}
1658
1659############################################################################# 1800#############################################################################
1660 1801
1661package CFPlus::UI::Box; 1802package DC::UI::Box;
1662 1803
1663our @ISA = CFPlus::UI::Container::; 1804our @ISA = DC::UI::Container::;
1664 1805
1665sub size_request { 1806sub size_request {
1666 my ($self) = @_; 1807 my ($self) = @_;
1808
1809 my @children = $self->visible_children;
1667 1810
1668 $self->{vertical} 1811 $self->{vertical}
1669 ? ( 1812 ? (
1670 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1813 (List::Util::max map $_->{req_w}, @children),
1671 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1814 (List::Util::sum map $_->{req_h}, @children),
1672 ) 1815 )
1673 : ( 1816 : (
1674 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1817 (List::Util::sum map $_->{req_w}, @children),
1675 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1818 (List::Util::max map $_->{req_h}, @children),
1676 ) 1819 )
1677} 1820}
1678 1821
1679sub invoke_size_allocate { 1822sub invoke_size_allocate {
1680 my ($self, $w, $h) = @_; 1823 my ($self, $w, $h) = @_;
1701 $req[$_] += $space * $children[$_]{expand} 1844 $req[$_] += $space * $children[$_]{expand}
1702 for 0 .. $#children; 1845 for 0 .. $#children;
1703 } 1846 }
1704 } 1847 }
1705 1848
1706 CFPlus::UI::harmonize \@req; 1849 DC::UI::harmonize \@req;
1707 1850
1708 my $pos = 0; 1851 my $pos = 0;
1709 for (0 .. $#children) { 1852 for (0 .. $#children) {
1710 my $alloc = $req[$_]; 1853 my $alloc = $req[$_];
1711 $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));
1716 1 1859 1
1717} 1860}
1718 1861
1719############################################################################# 1862#############################################################################
1720 1863
1721package CFPlus::UI::HBox; 1864package DC::UI::HBox;
1722 1865
1723our @ISA = CFPlus::UI::Box::; 1866our @ISA = DC::UI::Box::;
1724 1867
1725sub new { 1868sub new {
1726 my $class = shift; 1869 my $class = shift;
1727 1870
1728 $class->SUPER::new ( 1871 $class->SUPER::new (
1731 ) 1874 )
1732} 1875}
1733 1876
1734############################################################################# 1877#############################################################################
1735 1878
1736package CFPlus::UI::VBox; 1879package DC::UI::VBox;
1737 1880
1738our @ISA = CFPlus::UI::Box::; 1881our @ISA = DC::UI::Box::;
1739 1882
1740sub new { 1883sub new {
1741 my $class = shift; 1884 my $class = shift;
1742 1885
1743 $class->SUPER::new ( 1886 $class->SUPER::new (
1746 ) 1889 )
1747} 1890}
1748 1891
1749############################################################################# 1892#############################################################################
1750 1893
1751package CFPlus::UI::Label; 1894package DC::UI::Label;
1752 1895
1753our @ISA = CFPlus::UI::DrawBG::; 1896our @ISA = DC::UI::DrawBG::;
1754 1897
1755use CFPlus::OpenGL; 1898use DC::OpenGL;
1756 1899
1757sub new { 1900sub new {
1758 my ($class, %arg) = @_; 1901 my ($class, %arg) = @_;
1759 1902
1760 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1765 #text => initial text 1908 #text => initial text
1766 #markup => initial narkup 1909 #markup => initial narkup
1767 #max_w => maximum pixel width 1910 #max_w => maximum pixel width
1768 #style => 0, # render flags 1911 #style => 0, # render flags
1769 ellipsise => 3, # end 1912 ellipsise => 3, # end
1770 layout => (new CFPlus::Layout), 1913 layout => (new DC::Layout),
1771 fontsize => 1, 1914 fontsize => 1,
1772 align => -1, 1915 align => 0.5,
1773 valign => -1, 1916 valign => 0.5,
1774 padding_x => 2, 1917 padding_x => 4,
1775 padding_y => 2, 1918 padding_y => 2,
1776 can_events => 0, 1919 can_events => 0,
1777 %arg 1920 %arg
1778 ); 1921 );
1779 1922
1780 if (exists $self->{template}) { 1923 if (exists $self->{template}) {
1781 my $layout = new CFPlus::Layout; 1924 my $layout = new DC::Layout;
1782 $layout->set_text (delete $self->{template}); 1925 $layout->set_text (delete $self->{template});
1783 $self->{template} = $layout; 1926 $self->{template} = $layout;
1784 } 1927 }
1785 1928
1786 if (exists $self->{markup}) { 1929 if (exists $self->{markup}) {
1804 1947
1805 delete $self->{ox}; 1948 delete $self->{ox};
1806 $self->SUPER::realloc; 1949 $self->SUPER::realloc;
1807} 1950}
1808 1951
1952sub clear {
1953 my ($self) = @_;
1954
1955 $self->set_text ("");
1956}
1957
1809sub set_text { 1958sub set_text {
1810 my ($self, $text) = @_; 1959 my ($self, $text) = @_;
1811 1960
1812 return if $self->{text} eq "T$text"; 1961 return if $self->{text} eq "T$text";
1813 $self->{text} = "T$text"; 1962 $self->{text} = "T$text";
1836 1985
1837sub size_request { 1986sub size_request {
1838 my ($self) = @_; 1987 my ($self) = @_;
1839 1988
1840 $self->{size_req} ||= do { 1989 $self->{size_req} ||= do {
1990 my ($max_w, $max_h) = $self->get_max_wh;
1991
1841 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1992 $self->{layout}->set_font ($self->{font}) if $self->{font};
1842 $self->{layout}->set_width ($self->{max_w} || -1); 1993 $self->{layout}->set_width ($max_w);
1843 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1994 $self->{layout}->set_ellipsise ($self->{ellipsise});
1844 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1995 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1845 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1996 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1846 1997
1847 my ($w, $h) = $self->{layout}->size; 1998 my ($w, $h) = $self->{layout}->size;
1848 1999
1849 if (exists $self->{template}) { 2000 if (exists $self->{template}) {
1850 $self->{template}->set_font ($self->{font}) if $self->{font}; 2001 $self->{template}->set_font ($self->{font}) if $self->{font};
1851 $self->{template}->set_width ($self->{max_w} || -1); 2002 $self->{template}->set_width ($max_w);
1852 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2003 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1853 2004
1854 my ($w2, $h2) = $self->{template}->size; 2005 my ($w2, $h2) = $self->{template}->size;
1855 2006
1856 $w = List::Util::max $w, $w2; 2007 $w = List::Util::max $w, $w2;
1912 2063
1913 [$self->{layout}->size] 2064 [$self->{layout}->size]
1914 }; 2065 };
1915 2066
1916 unless (exists $self->{ox}) { 2067 unless (exists $self->{ox}) {
1917 $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);
1918 : $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);
1919 : ($self->{w} - $size->[0]) * 0.5);
1920 2070
1921 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2071 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1922 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1923 : ($self->{h} - $size->[1]) * 0.5);
1924 }; 2072 };
1925 2073
1926 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2074# unless ($self->{list}) {
1927 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2075# $self->{list} = DC::OpenGL::glGenList;
1928 2076# DC::OpenGL::glNewList $self->{list};
1929 $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;
1930} 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#}
1931 2093
1932############################################################################# 2094#############################################################################
1933 2095
1934package CFPlus::UI::EntryBase; 2096package DC::UI::EntryBase;
1935 2097
1936our @ISA = CFPlus::UI::Label::; 2098our @ISA = DC::UI::Label::;
1937 2099
1938use CFPlus::OpenGL; 2100use DC::OpenGL;
1939 2101
1940sub new { 2102sub new {
1941 my $class = shift; 2103 my $class = shift;
1942 2104
1943 $class->SUPER::new ( 2105 $class->SUPER::new (
1944 fg => [1, 1, 1], 2106 fg => [1, 1, 1],
1945 bg => [0, 0, 0, 0.2], 2107 bg => [0, 0, 0, 0.2],
2108 outline => undef,
1946 active_bg => [1, 1, 1, 0.5], 2109 active_bg => [0, 0, 1, .2],
1947 active_fg => [0, 0, 0], 2110 active_fg => [1, 1, 1],
2111 active_outline => [1, 1, 0],
1948 can_hover => 1, 2112 can_hover => 1,
1949 can_focus => 1, 2113 can_focus => 1,
2114 align => 0,
1950 valign => 0, 2115 valign => 0.5,
1951 can_events => 1, 2116 can_events => 1,
1952 ellipsise => 0, 2117 ellipsise => 0,
2118 padding_x => 4,
2119 padding_y => 2,
1953 #text => ... 2120 #text => ...
1954 #hidden => "*", 2121 #hidden => "*",
1955 @_ 2122 @_
1956 ) 2123 )
1957} 2124}
2004 2171
2005 my $text = $self->get_text; 2172 my $text = $self->get_text;
2006 2173
2007 $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;
2008 2175
2009 if ($uni == 8) { 2176 if ($sym == DC::SDLK_BACKSPACE) {
2010 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2177 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2011 } elsif ($uni == 127) { 2178 } elsif ($sym == DC::SDLK_DELETE) {
2012 substr $text, $self->{cursor}, 1, ""; 2179 substr $text, $self->{cursor}, 1, "";
2013 } elsif ($sym == CFPlus::SDLK_LEFT) { 2180 } elsif ($sym == DC::SDLK_LEFT) {
2014 --$self->{cursor} if $self->{cursor}; 2181 --$self->{cursor} if $self->{cursor};
2015 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2182 } elsif ($sym == DC::SDLK_RIGHT) {
2016 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2183 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2017 } elsif ($sym == CFPlus::SDLK_HOME) { 2184 } elsif ($sym == DC::SDLK_HOME) {
2018 # what a hack 2185 # what a hack
2019 $self->{cursor} = 2186 $self->{cursor} =
2020 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2187 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2021 ? length $1 2188 ? length $1
2022 : 0; 2189 : 0;
2023 } elsif ($sym == CFPlus::SDLK_END) { 2190 } elsif ($sym == DC::SDLK_END) {
2024 # uh, again 2191 # uh, again
2025 $self->{cursor} = 2192 $self->{cursor} =
2026 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2193 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2027 ? $self->{cursor} + length $1 2194 ? $self->{cursor} + length $1
2028 : length $self->{text}; 2195 : length $self->{text};
2092 glColor_premultiply @{$self->{bg}}; 2259 glColor_premultiply @{$self->{bg}};
2093 } 2260 }
2094 2261
2095 glEnable GL_BLEND; 2262 glEnable GL_BLEND;
2096 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2263 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2097 glBegin GL_QUADS;
2098 glVertex 0 , 0;
2099 glVertex 0 , $self->{h};
2100 glVertex $self->{w}, $self->{h}; 2264 glRect 0, 0, $self->{w}, $self->{h};
2101 glVertex $self->{w}, 0;
2102 glEnd;
2103 glDisable GL_BLEND; 2265 glDisable GL_BLEND;
2104 2266
2105 $self->SUPER::_draw; 2267 $self->SUPER::_draw;
2106 2268
2107 #TODO: force update every cursor change :( 2269 #TODO: force update every cursor change :(
2109 2271
2110 unless (exists $self->{cur_h}) { 2272 unless (exists $self->{cur_h}) {
2111 my $text = substr $self->{text}, 0, $self->{cursor}; 2273 my $text = substr $self->{text}, 0, $self->{cursor};
2112 utf8::encode $text; 2274 utf8::encode $text;
2113 2275
2114 @$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);
2115 } 2277 }
2116 2278
2279 glColor_premultiply @{$self->{active_fg}};
2117 glBegin GL_LINES; 2280 glBegin GL_LINES;
2118 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};
2119 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};
2120 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;
2121 } 2298 }
2122} 2299}
2123 2300
2124############################################################################# 2301#############################################################################
2125 2302
2126package CFPlus::UI::Entry; 2303package DC::UI::Entry;
2127 2304
2128our @ISA = CFPlus::UI::EntryBase::; 2305our @ISA = DC::UI::EntryBase::;
2129 2306
2130use 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
2131 2318
2132sub invoke_key_down { 2319sub invoke_key_down {
2133 my ($self, $ev) = @_; 2320 my ($self, $ev) = @_;
2134 2321
2135 my $sym = $ev->{sym}; 2322 my $sym = $ev->{sym};
2141 $self->{history_pointer} = -1; 2328 $self->{history_pointer} = -1;
2142 $self->{history_saveback} = ''; 2329 $self->{history_saveback} = '';
2143 $self->emit (activate => $txt); 2330 $self->emit (activate => $txt);
2144 $self->update; 2331 $self->update;
2145 2332
2146 } elsif ($sym == CFPlus::SDLK_UP) { 2333 } elsif ($sym == DC::SDLK_UP) {
2147 if ($self->{history_pointer} < 0) { 2334 if ($self->{history_pointer} < 0) {
2148 $self->{history_saveback} = $self->get_text; 2335 $self->{history_saveback} = $self->get_text;
2149 } 2336 }
2150 if (@{$self->{history} || []} > 0) { 2337 if (@{$self->{history} || []} > 0) {
2151 $self->{history_pointer}++; 2338 $self->{history_pointer}++;
2153 $self->{history_pointer} = @{$self->{history} || []} - 1; 2340 $self->{history_pointer} = @{$self->{history} || []} - 1;
2154 } 2341 }
2155 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2156 } 2343 }
2157 2344
2158 } elsif ($sym == CFPlus::SDLK_DOWN) { 2345 } elsif ($sym == DC::SDLK_DOWN) {
2159 $self->{history_pointer}--; 2346 $self->{history_pointer}--;
2160 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2347 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2161 2348
2162 if ($self->{history_pointer} >= 0) { 2349 if ($self->{history_pointer} >= 0) {
2163 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2350 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2164 } else { 2351 } else {
2352 if (defined $self->{history_saveback}) {
2165 $self->set_text ($self->{history_saveback}); 2353 $self->set_text ($self->{history_saveback});
2354 $self->{history_saveback} = undef;
2355 }
2166 } 2356 }
2167 2357
2168 } else { 2358 } else {
2169 return $self->SUPER::invoke_key_down ($ev) 2359 return $self->SUPER::invoke_key_down ($ev)
2170 } 2360 }
2172 1 2362 1
2173} 2363}
2174 2364
2175############################################################################# 2365#############################################################################
2176 2366
2177package CFPlus::UI::TextEdit; 2367package DC::UI::TextEdit;
2178 2368
2179our @ISA = CFPlus::UI::EntryBase::; 2369our @ISA = DC::UI::EntryBase::;
2180 2370
2181use 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}
2182 2382
2183sub move_cursor_ver { 2383sub move_cursor_ver {
2184 my ($self, $dy) = @_; 2384 my ($self, $dy) = @_;
2185 2385
2186 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2386 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2187 2387
2188 $y += $dy; 2388 $line += $dy;
2189 2389
2190 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))) {
2191 $self->{cursor} = $index; 2391 $self->{cursor} = $index;
2192 delete $self->{cur_h}; 2392 delete $self->{cur_h};
2193 $self->update; 2393 $self->update;
2194 return; 2394 return;
2195 } 2395 }
2198sub invoke_key_down { 2398sub invoke_key_down {
2199 my ($self, $ev) = @_; 2399 my ($self, $ev) = @_;
2200 2400
2201 my $sym = $ev->{sym}; 2401 my $sym = $ev->{sym};
2202 2402
2203 if ($sym == CFPlus::SDLK_UP) { 2403 if ($sym == DC::SDLK_UP) {
2204 $self->move_cursor_ver (-1); 2404 $self->move_cursor_ver (-1);
2205 } elsif ($sym == CFPlus::SDLK_DOWN) { 2405 } elsif ($sym == DC::SDLK_DOWN) {
2206 $self->move_cursor_ver (+1); 2406 $self->move_cursor_ver (+1);
2207 } else { 2407 } else {
2208 return $self->SUPER::invoke_key_down ($ev) 2408 return $self->SUPER::invoke_key_down ($ev)
2209 } 2409 }
2210 2410
2211 1 2411 1
2212} 2412}
2213 2413
2214############################################################################# 2414#############################################################################
2215 2415
2216package CFPlus::UI::Button; 2416package DC::UI::ButtonBin;
2217 2417
2218our @ISA = CFPlus::UI::Label::; 2418our @ISA = DC::UI::Bin::;
2219 2419
2220use CFPlus::OpenGL; 2420use DC::OpenGL;
2221 2421
2222my @tex = 2422my @tex =
2223 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2423 map { new_from_resource DC::Texture $_, mipmap => 1 }
2224 qw(b1_button_active.png); 2424 qw(b1_button_inactive.png b1_button_active.png);
2225 2425
2226sub new { 2426sub new {
2227 my $class = shift; 2427 my $class = shift;
2228 2428
2229 $class->SUPER::new ( 2429 $class->SUPER::new (
2230 padding_x => 4,
2231 padding_y => 4,
2232 fg => [1, 1, 1],
2233 active_fg => [0, 0, 1],
2234 can_hover => 1, 2430 can_hover => 1,
2235 align => 0, 2431 align => 0.5,
2236 valign => 0, 2432 valign => 0.5,
2237 can_events => 1, 2433 can_events => 1,
2238 @_ 2434 @_
2239 ) 2435 )
2240} 2436}
2241 2437
2250} 2446}
2251 2447
2252sub _draw { 2448sub _draw {
2253 my ($self) = @_; 2449 my ($self) = @_;
2254 2450
2255 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2256
2257 glEnable GL_TEXTURE_2D; 2451 glEnable GL_TEXTURE_2D;
2258 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2259 glColor 0, 0, 0, 1; 2453 glColor 0, 0, 0, 1;
2260 2454
2455 my $tex = $tex[$GRAB == $self];
2261 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2456 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2262 2457
2263 glDisable GL_TEXTURE_2D; 2458 glDisable GL_TEXTURE_2D;
2264 2459
2265 $self->SUPER::_draw; 2460 $self->SUPER::_draw;
2266} 2461}
2267 2462
2268############################################################################# 2463#############################################################################
2269 2464
2270package CFPlus::UI::CheckBox; 2465package DC::UI::Button;
2271 2466
2272our @ISA = CFPlus::UI::DrawBG::; 2467our @ISA = DC::UI::Label::;
2468
2469use DC::OpenGL;
2273 2470
2274my @tex = 2471my @tex =
2275 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2472 map { new_from_resource DC::Texture $_, mipmap => 1 }
2276 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2473 qw(b1_button_inactive.png b1_button_active.png);
2277
2278use CFPlus::OpenGL;
2279 2474
2280sub new { 2475sub new {
2281 my $class = shift; 2476 my $class = shift;
2282 2477
2283 $class->SUPER::new ( 2478 $class->SUPER::new (
2479 padding_x => 8,
2480 padding_y => 4,
2481 fg => [1.0, 1.0, 1.0],
2482 active_fg => [0.8, 0.8, 0.8],
2483 can_hover => 1,
2484 align => 0.5,
2485 valign => 0.5,
2486 can_events => 1,
2487 @_
2488 )
2489}
2490
2491sub invoke_button_up {
2492 my ($self, $ev, $x, $y) = @_;
2493
2494 $self->emit ("activate")
2495 if $x >= 0 && $x < $self->{w}
2496 && $y >= 0 && $y < $self->{h};
2497
2498 1
2499}
2500
2501sub _draw {
2502 my ($self) = @_;
2503
2504 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2505
2506 glEnable GL_TEXTURE_2D;
2507 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2508 glColor 0, 0, 0, 1;
2509
2510 my $tex = $tex[$GRAB == $self];
2511 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2512
2513 glDisable GL_TEXTURE_2D;
2514
2515 $self->SUPER::_draw;
2516}
2517
2518#############################################################################
2519
2520package DC::UI::CheckBox;
2521
2522our @ISA = DC::UI::DrawBG::;
2523
2524my @tex =
2525 map { new_from_resource DC::Texture $_, mipmap => 1 }
2526 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2527
2528use DC::OpenGL;
2529
2530sub new {
2531 my $class = shift;
2532
2533 $class->SUPER::new (
2534 fontsize => 1,
2284 padding_x => 2, 2535 padding_x => 2,
2285 padding_y => 2, 2536 padding_y => 2,
2286 fg => [1, 1, 1], 2537 fg => [1, 1, 1],
2287 active_fg => [1, 1, 0], 2538 active_fg => [1, 1, 0],
2288 bg => [0, 0, 0, 0.2], 2539 bg => [0, 0, 0, 0.2],
2294} 2545}
2295 2546
2296sub size_request { 2547sub size_request {
2297 my ($self) = @_; 2548 my ($self) = @_;
2298 2549
2299 (6) x 2 2550 ($self->{fontsize} * $::FONTSIZE) x 2
2300} 2551}
2301 2552
2302sub toggle { 2553sub toggle {
2303 my ($self) = @_; 2554 my ($self) = @_;
2304 2555
2323sub _draw { 2574sub _draw {
2324 my ($self) = @_; 2575 my ($self) = @_;
2325 2576
2326 $self->SUPER::_draw; 2577 $self->SUPER::_draw;
2327 2578
2328 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2579 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2329 2580
2330 my ($w, $h) = @$self{qw(w h)}; 2581 my ($w, $h) = @$self{qw(w h)};
2331 2582
2332 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;
2333 2584
2340 glDisable GL_TEXTURE_2D; 2591 glDisable GL_TEXTURE_2D;
2341} 2592}
2342 2593
2343############################################################################# 2594#############################################################################
2344 2595
2345package CFPlus::UI::Image; 2596package DC::UI::Image;
2346 2597
2347our @ISA = CFPlus::UI::Base::; 2598our @ISA = DC::UI::DrawBG::;
2348 2599
2349use CFPlus::OpenGL; 2600use DC::OpenGL;
2350 2601
2351our %texture_cache; 2602our %texture_cache;
2352 2603
2353sub new { 2604sub new {
2354 my $class = shift; 2605 my $class = shift;
2355 2606
2356 my $self = $class->SUPER::new ( 2607 my $self = $class->SUPER::new (
2357 can_events => 0, 2608 can_events => 0,
2609 scale => 1,
2358 @_, 2610 @_,
2359 ); 2611 );
2360 2612
2361 $self->{path} || $self->{tex} 2613 $self->{path} || $self->{tex}
2362 or Carp::croak "'path' or 'tex' attributes required"; 2614 or Carp::croak "'path' or 'tex' attributes required";
2363 2615
2364 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2616 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2365 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2617 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2366 2618
2367 CFPlus::weaken $texture_cache{$self->{path}}; 2619 DC::weaken $texture_cache{$self->{path}};
2368 2620
2369 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2621 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2370 2622
2371 $self 2623 $self
2372} 2624}
2373 2625
2374sub STORABLE_freeze { 2626sub STORABLE_freeze {
2375 my ($self, $cloning) = @_; 2627 my ($self, $cloning) = @_;
2376 2628
2377 $self->{path} 2629 $self->{path}
2378 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";
2379 2631
2380 $self->{path} 2632 $self->{path}
2381} 2633}
2382 2634
2383sub STORABLE_attach { 2635sub STORABLE_attach {
2384 my ($self, $cloning, $path) = @_; 2636 my ($self, $cloning, $path) = @_;
2385 2637
2386 $self->new (path => $path) 2638 $self->new (path => $path)
2387} 2639}
2388 2640
2641sub set_texture {
2642 my ($self, $tex) = @_;
2643
2644 $self->{tex} = $tex;
2645 $self->update;
2646}
2647
2389sub size_request { 2648sub size_request {
2390 my ($self) = @_; 2649 my ($self) = @_;
2391 2650
2392 ($self->{tex}{w}, $self->{tex}{h}) 2651 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2393} 2652}
2394 2653
2395sub _draw { 2654sub _draw {
2396 my ($self) = @_; 2655 my ($self) = @_;
2656
2657 $self->SUPER::_draw;
2397 2658
2398 my $tex = $self->{tex}; 2659 my $tex = $self->{tex};
2399 2660
2400 my ($w, $h) = ($self->{w}, $self->{h}); 2661 my ($w, $h) = ($self->{w}, $self->{h});
2401 2662
2407 } 2668 }
2408 2669
2409 glEnable GL_TEXTURE_2D; 2670 glEnable GL_TEXTURE_2D;
2410 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2671 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2411 2672
2412 $tex->draw_quad (0, 0, $w, $h); 2673 $tex->draw_quad_alpha (0, 0, $w, $h);
2413 2674
2414 glDisable GL_TEXTURE_2D; 2675 glDisable GL_TEXTURE_2D;
2415} 2676}
2416 2677
2417############################################################################# 2678#############################################################################
2418 2679
2419package CFPlus::UI::ImageButton; 2680package DC::UI::ImageButton;
2420 2681
2421our @ISA = CFPlus::UI::Image::; 2682our @ISA = DC::UI::Image::;
2422 2683
2423use CFPlus::OpenGL; 2684use DC::OpenGL;
2424
2425my %textures;
2426 2685
2427sub new { 2686sub new {
2428 my $class = shift; 2687 my $class = shift;
2429 2688
2430 my $self = $class->SUPER::new ( 2689 my $self = $class->SUPER::new (
2431 padding_x => 4, 2690 padding_x => 4,
2432 padding_y => 4, 2691 padding_y => 4,
2433 fg => [1, 1, 1], 2692 fg => [1, 1, 1],
2434 active_fg => [0, 0, 1], 2693 active_fg => [0, 0, 1],
2435 can_hover => 1, 2694 can_hover => 1,
2436 align => 0, 2695 align => 0.5,
2437 valign => 0, 2696 valign => 0.5,
2438 can_events => 1, 2697 can_events => 1,
2439 @_ 2698 @_
2440 ); 2699 );
2441} 2700}
2442 2701
2702sub invoke_button_down {
2703 my ($self, $ev, $x, $y) = @_;
2704
2705 1
2706}
2707
2443sub invoke_button_up { 2708sub invoke_button_up {
2444 my ($self, $ev, $x, $y) = @_; 2709 my ($self, $ev, $x, $y) = @_;
2445 2710
2446 $self->emit ("activate") 2711 $self->emit ("activate")
2447 if $x >= 0 && $x < $self->{w} 2712 if $x >= 0 && $x < $self->{w}
2450 1 2715 1
2451} 2716}
2452 2717
2453############################################################################# 2718#############################################################################
2454 2719
2455package CFPlus::UI::VGauge; 2720package DC::UI::VGauge;
2456 2721
2457our @ISA = CFPlus::UI::Base::; 2722our @ISA = DC::UI::Base::;
2458 2723
2459use List::Util qw(min max); 2724use List::Util qw(min max);
2460 2725
2461use CFPlus::OpenGL; 2726use DC::OpenGL;
2462 2727
2463my %tex = ( 2728my %tex = (
2464 food => [ 2729 food => [
2465 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2730 map { new_from_resource DC::Texture $_, mipmap => 1 }
2466 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2731 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2467 ], 2732 ],
2468 grace => [ 2733 grace => [
2469 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2470 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2735 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2471 ], 2736 ],
2472 hp => [ 2737 hp => [
2473 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2474 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2739 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2475 ], 2740 ],
2476 mana => [ 2741 mana => [
2477 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2478 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2743 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2479 ], 2744 ],
2480); 2745);
2481 2746
2482# eg. VGauge->new (gauge => 'food'), default gauge: food 2747# eg. VGauge->new (gauge => 'food'), default gauge: food
2585 glDisable GL_TEXTURE_2D; 2850 glDisable GL_TEXTURE_2D;
2586} 2851}
2587 2852
2588############################################################################# 2853#############################################################################
2589 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
2590package CFPlus::UI::Gauge; 3009package DC::UI::Gauge;
2591 3010
2592our @ISA = CFPlus::UI::VBox::; 3011our @ISA = DC::UI::VBox::;
2593 3012
2594sub new { 3013sub new {
2595 my ($class, %arg) = @_; 3014 my ($class, %arg) = @_;
2596 3015
2597 my $self = $class->SUPER::new ( 3016 my $self = $class->SUPER::new (
2599 can_hover => 1, 3018 can_hover => 1,
2600 can_events => 1, 3019 can_events => 1,
2601 %arg, 3020 %arg,
2602 ); 3021 );
2603 3022
2604 $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");
2605 $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);
2606 $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");
2607 3026
2608 $self 3027 $self
2609} 3028}
2610 3029
2611sub set_fontsize { 3030sub set_fontsize {
2632 $self->{value}->set_text ($val); 3051 $self->{value}->set_text ($val);
2633} 3052}
2634 3053
2635############################################################################# 3054#############################################################################
2636 3055
2637package CFPlus::UI::Slider; 3056package DC::UI::Slider;
2638 3057
2639use strict; 3058use strict;
2640 3059
2641use CFPlus::OpenGL; 3060use DC::OpenGL;
2642 3061
2643our @ISA = CFPlus::UI::DrawBG::; 3062our @ISA = DC::UI::DrawBG::;
2644 3063
2645my @tex = 3064my @tex =
2646 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3065 map { new_from_resource DC::Texture $_ }
2647 qw(s1_slider.png s1_slider_bg.png); 3066 qw(s1_slider.png s1_slider_bg.png);
2648 3067
2649sub new { 3068sub new {
2650 my $class = shift; 3069 my $class = shift;
2651 3070
2688sub set_value { 3107sub set_value {
2689 my ($self, $value) = @_; 3108 my ($self, $value) = @_;
2690 3109
2691 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2692 3111
2693 $hi = $lo + 1 if $hi <= $lo; 3112 $hi = $lo if $hi < $lo;
2694 3113
2695 $page = $hi - $lo if $page > $hi - $lo; 3114 $value = $hi - $page if $value > $hi - $page;
2696
2697 $value = $lo if $value < $lo; 3115 $value = $lo if $value < $lo;
2698 $value = $hi - $page if $value > $hi - $page;
2699 3116
2700 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3117 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2701 if $unit; 3118 if $unit;
2702 3119
2703 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3120 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2719 3136
2720 $self->SUPER::invoke_button_down ($ev, $x, $y); 3137 $self->SUPER::invoke_button_down ($ev, $x, $y);
2721 3138
2722 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3139 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2723 3140
2724 $self->invoke_mouse_motion ($ev, $x, $y) 3141 $self->invoke_mouse_motion ($ev, $x, $y);
3142
3143 1
2725} 3144}
2726 3145
2727sub invoke_mouse_motion { 3146sub invoke_mouse_motion {
2728 my ($self, $ev, $x, $y) = @_; 3147 my ($self, $ev, $x, $y) = @_;
2729 3148
2745sub invoke_mouse_wheel { 3164sub invoke_mouse_wheel {
2746 my ($self, $ev) = @_; 3165 my ($self, $ev) = @_;
2747 3166
2748 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3167 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2749 3168
2750 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3169 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2751 3170
2752 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3171 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2753 3172
2754 ! ! $delta 3173 1
2755} 3174}
2756 3175
2757sub update { 3176sub update {
2758 my ($self) = @_; 3177 my ($self) = @_;
2759 3178
2765 my ($self) = @_; 3184 my ($self) = @_;
2766 3185
2767 unless ($self->{knob_w}) { 3186 unless ($self->{knob_w}) {
2768 $self->set_value ($self->{range}[0]); 3187 $self->set_value ($self->{range}[0]);
2769 3188
2770 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3189 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2771 my $range = ($hi - $page - $lo) || 1e-100; 3190 my $range = ($hi - $page - $lo) || 1e-10;
2772 3191
2773 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};
2774 3193
2775 $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;
2776 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3195 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2777 3196
2778 $value = ($value - $lo) / $range; 3197 $value = ($value - $lo) / $range;
2808 glDisable GL_TEXTURE_2D; 3227 glDisable GL_TEXTURE_2D;
2809} 3228}
2810 3229
2811############################################################################# 3230#############################################################################
2812 3231
2813package CFPlus::UI::ValSlider; 3232package DC::UI::ValSlider;
2814 3233
2815our @ISA = CFPlus::UI::HBox::; 3234our @ISA = DC::UI::HBox::;
2816 3235
2817sub new { 3236sub new {
2818 my ($class, %arg) = @_; 3237 my ($class, %arg) = @_;
2819 3238
2820 my $range = delete $arg{range}; 3239 my $range = delete $arg{range};
2821 3240
2822 my $self = $class->SUPER::new ( 3241 my $self = $class->SUPER::new (
2823 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3242 slider => (new DC::UI::Slider expand => 1, range => $range),
2824 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3243 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2825 to_value => sub { shift }, 3244 to_value => sub { shift },
2826 from_value => sub { shift }, 3245 from_value => sub { shift },
2827 %arg, 3246 %arg,
2828 ); 3247 );
2829 3248
2849sub set_range { shift->{slider}->set_range (@_) } 3268sub set_range { shift->{slider}->set_range (@_) }
2850sub set_value { shift->{slider}->set_value (@_) } 3269sub set_value { shift->{slider}->set_value (@_) }
2851 3270
2852############################################################################# 3271#############################################################################
2853 3272
2854package CFPlus::UI::TextScroller; 3273package DC::UI::TextScroller;
2855 3274
2856our @ISA = CFPlus::UI::HBox::; 3275our @ISA = DC::UI::HBox::;
2857 3276
2858use CFPlus::OpenGL; 3277use DC::OpenGL;
2859 3278
2860sub new { 3279sub new {
2861 my $class = shift; 3280 my $class = shift;
2862 3281
2863 my $self = $class->SUPER::new ( 3282 my $self = $class->SUPER::new (
2865 can_events => 1, 3284 can_events => 1,
2866 indent => 0, 3285 indent => 0,
2867 #font => default_font 3286 #font => default_font
2868 @_, 3287 @_,
2869 3288
2870 layout => (new CFPlus::Layout), 3289 layout => (new DC::Layout),
2871 par => [], 3290 par => [],
2872 max_par => 0, 3291 max_par => 0,
2873 height => 0, 3292 height => 0,
2874 children => [ 3293 children => [
2875 (new CFPlus::UI::Empty expand => 1), 3294 (new DC::UI::Empty expand => 1),
2876 (new CFPlus::UI::Slider vertical => 1), 3295 (new DC::UI::Slider vertical => 1),
2877 ], 3296 ],
2878 ); 3297 );
2879 3298
2880 $self->{children}[1]->connect (changed => sub { $self->update }); 3299 $self->{children}[1]->connect (changed => sub { $self->update });
2881 3300
2890} 3309}
2891 3310
2892sub size_request { 3311sub size_request {
2893 my ($self) = @_; 3312 my ($self) = @_;
2894 3313
2895 my ($empty, $slider) = @{ $self->{children} }; 3314 my ($empty, $slider) = $self->visible_children;
2896 3315
2897 local $self->{children} = [$empty, $slider]; 3316 local $self->{children} = [$empty, $slider];
2898 $self->SUPER::size_request 3317 $self->SUPER::size_request
2899} 3318}
2900 3319
2937 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3356 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2938 $layout->set_markup ($para->{markup}); 3357 $layout->set_markup ($para->{markup});
2939 3358
2940 $layout->set_shapes ( 3359 $layout->set_shapes (
2941 map 3360 map
2942 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3361 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2943 @{$para->{widget}} 3362 @{$para->{widget}}
2944 ); 3363 );
2945 3364
2946 $layout 3365 $layout
2947} 3366}
3064 $ROOT->on_post_alloc ($self => sub { 3483 $ROOT->on_post_alloc ($self => sub {
3065 $self->force_uptodate; 3484 $self->force_uptodate;
3066 3485
3067 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3486 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3068 3487
3069 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3488 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3070 glClearColor 0, 0, 0, 0; 3489 glClearColor 0, 0, 0, 0;
3071 glClear GL_COLOR_BUFFER_BIT; 3490 glClear GL_COLOR_BUFFER_BIT;
3072 3491
3073 package CFPlus::UI::Base; 3492 package DC::UI::Base;
3074 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3493 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3075 (0, 0, $self->{w}, $self->{h}); 3494 (0, 0, $self->{w}, $self->{h});
3076 3495
3077 my $top = int $self->{children}[1]{range}[0]; 3496 my $top = int $self->{children}[1]{range}[0];
3078 3497
3089 3508
3090 if ($y0 < $y + $h && $y < $y1) { 3509 if ($y0 < $y + $h && $y < $y1) {
3091 my $layout = $self->get_layout ($para); 3510 my $layout = $self->get_layout ($para);
3092 3511
3093 $layout->render ($para->{indent}, $y - $y0); 3512 $layout->render ($para->{indent}, $y - $y0);
3513 $layout->draw;
3094 3514
3095 if (my @w = @{ $para->{widget} }) { 3515 if (my @w = @{ $para->{widget} }) {
3096 my @s = $layout->get_shapes; 3516 my @s = $layout->get_shapes;
3097 3517
3098 for (@w) { 3518 for (@w) {
3136 $self->{children}[1]->draw; 3556 $self->{children}[1]->draw;
3137} 3557}
3138 3558
3139############################################################################# 3559#############################################################################
3140 3560
3141package CFPlus::UI::Animator; 3561package DC::UI::Animator;
3142 3562
3143use CFPlus::OpenGL; 3563use DC::OpenGL;
3144 3564
3145our @ISA = CFPlus::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3146 3566
3147sub moveto { 3567sub moveto {
3148 my ($self, $x, $y) = @_; 3568 my ($self, $x, $y) = @_;
3149 3569
3150 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3570 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3178 glPopMatrix; 3598 glPopMatrix;
3179} 3599}
3180 3600
3181############################################################################# 3601#############################################################################
3182 3602
3183package CFPlus::UI::Flopper; 3603package DC::UI::Flopper;
3184 3604
3185our @ISA = CFPlus::UI::Button::; 3605our @ISA = DC::UI::Button::;
3186 3606
3187sub new { 3607sub new {
3188 my $class = shift; 3608 my $class = shift;
3189 3609
3190 my $self = $class->SUPER::new ( 3610 my $self = $class->SUPER::new (
3202 $self->{other}->toggle_visibility; 3622 $self->{other}->toggle_visibility;
3203} 3623}
3204 3624
3205############################################################################# 3625#############################################################################
3206 3626
3207package CFPlus::UI::Tooltip; 3627package DC::UI::Tooltip;
3208 3628
3209our @ISA = CFPlus::UI::Bin::; 3629our @ISA = DC::UI::Bin::;
3210 3630
3211use CFPlus::OpenGL; 3631use DC::OpenGL;
3212 3632
3213sub new { 3633sub new {
3214 my $class = shift; 3634 my $class = shift;
3215 3635
3216 $class->SUPER::new ( 3636 $class->SUPER::new (
3220} 3640}
3221 3641
3222sub set_tooltip_from { 3642sub set_tooltip_from {
3223 my ($self, $widget) = @_; 3643 my ($self, $widget) = @_;
3224 3644
3225 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3226 if $widget->{tooltip} =~ /^#(.*)$/;
3227
3228 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 =~ /^#(.*)$/;
3229 3650
3230 if ($ENV{CFPLUS_DEBUG} & 2) { 3651 if ($ENV{CFPLUS_DEBUG} & 2) {
3231 $tooltip .= "\n\n" . (ref $widget) . "\n" 3652 $tip .= "\n\n" . (ref $widget) . "\n"
3232 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3653 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3233 . "req $widget->{req_w} $widget->{req_h}\n" 3654 . "req $widget->{req_w} $widget->{req_h}\n"
3234 . "visible $widget->{visible}"; 3655 . "visible $widget->{visible}";
3235 } 3656 }
3236 3657
3237 $tooltip =~ s/^\n+//; 3658 $tip =~ s/^\n+//;
3238 $tooltip =~ s/\n+$//; 3659 $tip =~ s/\n+$//;
3239 3660
3240 $self->add (new CFPlus::UI::Label 3661 $self->add (new DC::UI::Label
3662 fg => $DC::THEME{tooltip_fg},
3241 markup => $tooltip, 3663 markup => $tip,
3242 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3664 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3665 align => 0,
3243 fontsize => 0.8, 3666 fontsize => 0.8,
3244 style => 1, # FLAG_INVERSE 3667 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3245 ellipsise => 0, 3668 ellipsise => 0,
3246 font => ($widget->{tooltip_font} || $::FONT_PROP), 3669 font => ($widget->{tooltip_font} || $::FONT_PROP),
3247 ); 3670 );
3248} 3671}
3249 3672
3284} 3707}
3285 3708
3286sub _draw { 3709sub _draw {
3287 my ($self) = @_; 3710 my ($self) = @_;
3288 3711
3289 glTranslate 0.375, 0.375;
3290
3291 my ($w, $h) = @$self{qw(w h)}; 3712 my ($w, $h) = @$self{qw(w h)};
3292 3713
3293 glColor 1, 0.8, 0.4; 3714 glColor @{ $DC::THEME{tooltip_bg} };
3294 glBegin GL_QUADS; 3715 glRect 0, 0, $w, $h;
3295 glVertex 0 , 0;
3296 glVertex 0 , $h;
3297 glVertex $w, $h;
3298 glVertex $w, 0;
3299 glEnd;
3300 3716
3301 glColor 0, 0, 0; 3717 glColor @{ $DC::THEME{tooltip_border} };
3302 glBegin GL_LINE_LOOP; 3718 glRect_lineloop .5, .5, $w + .5, $h + .5;
3303 glVertex 0 , 0;
3304 glVertex 0 , $h;
3305 glVertex $w, $h;
3306 glVertex $w, 0;
3307 glEnd;
3308 3719
3309 glTranslate 2 - 0.375, 2 - 0.375; 3720 glTranslate 2, 2;
3310 3721
3311 $self->SUPER::_draw; 3722 $self->SUPER::_draw;
3312} 3723}
3313 3724
3314############################################################################# 3725#############################################################################
3315 3726
3316package CFPlus::UI::Face; 3727package DC::UI::Face;
3317 3728
3318our @ISA = CFPlus::UI::DrawBG::; 3729our @ISA = DC::UI::DrawBG::;
3319 3730
3320use CFPlus::OpenGL; 3731use DC::OpenGL;
3321 3732
3322sub new { 3733sub new {
3323 my $class = shift; 3734 my $class = shift;
3324 3735
3325 my $self = $class->SUPER::new ( 3736 my $self = $class->SUPER::new (
3737 size_w => 32,
3738 size_h => 8,
3326 aspect => 1, 3739 aspect => 1,
3327 can_events => 0, 3740 can_events => 0,
3328 @_, 3741 @_,
3329 ); 3742 );
3330 3743
3331 if ($self->{anim} && $self->{animspeed}) { 3744 $self->update_anim;
3332 CFPlus::weaken (my $widget = $self);
3333
3334 $self->{timer} = Event->timer (
3335 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3336 hard => 1,
3337 interval => $self->{animspeed},
3338 cb => sub {
3339 delete $self->{wait_face};
3340 ++$widget->{frame};
3341 $widget->update;
3342 },
3343 );
3344 }
3345 3745
3346 $self 3746 $self
3347} 3747}
3348 3748
3749sub update_timer {
3750 my ($self) = @_;
3751
3752 return unless $self->{timer};
3753
3754 if ($self->{visible}) {
3755 $self->{timer}->start;
3756 } else {
3757 $self->{timer}->stop;
3758 }
3759}
3760
3761sub update_face {
3762 my ($self) = @_;
3763
3764 if ($::CONN) {
3765 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3766 if ($anim && @$anim) {
3767 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3768 delete $self->{face_change_cb};
3769
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 }
3774 }
3775 }
3776 }
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
3349sub size_request { 3808sub size_request {
3350 (32, 8) 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 }
3824
3825 ($self->{size_w} || 8, $self->{size_h} || 8)
3351} 3826}
3352 3827
3353sub update { 3828sub update {
3354 my ($self) = @_; 3829 my ($self) = @_;
3355 3830
3356 return unless $self->{visible}; 3831 return unless $self->{visible};
3357 3832
3358 $self->SUPER::update; 3833 $self->SUPER::update;
3359} 3834}
3360 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
3857sub invoke_visibility_change {
3858 my ($self) = @_;
3859
3860 $self->update_timer;
3861
3862 0
3863}
3864
3361sub _draw { 3865sub _draw {
3362 my ($self) = @_; 3866 my ($self) = @_;
3363 3867
3364 return unless $::CONN;
3365
3366 $self->SUPER::_draw; 3868 $self->SUPER::_draw;
3367 3869
3368 my $face; 3870 if (my $tex = $self->{tex}) {
3369
3370 if ($self->{frame}) {
3371 my $anim = $::CONN->{anim}[$self->{anim}];
3372
3373 $face = $anim->[ $self->{frame} % @$anim ]
3374 if $anim && @$anim;
3375 }
3376
3377 my $faceid = $::CONN->{faceid}[$face || $self->{face}]
3378 or return;
3379
3380 my $tex = $::CONN->{texture}[$faceid];
3381
3382 if ($tex) {
3383 glEnable GL_TEXTURE_2D; 3871 glEnable GL_TEXTURE_2D;
3384 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3385 glColor 0, 0, 0, 1; 3873 glColor 0, 0, 0, 1;
3386 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3874 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3387 glDisable GL_TEXTURE_2D; 3875 glDisable GL_TEXTURE_2D;
3388 } else {
3389 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3390 $self->update;
3391 });
3392 } 3876 }
3393} 3877}
3394 3878
3395sub destroy { 3879sub destroy {
3396 my ($self) = @_; 3880 my ($self) = @_;
3397 3881
3398 $self->{timer}->cancel 3882 (delete $self->{timer})->cancel
3399 if $self->{timer}; 3883 if $self->{timer};
3400 3884
3401 $self->SUPER::destroy; 3885 $self->SUPER::destroy;
3402} 3886}
3403 3887
3404############################################################################# 3888#############################################################################
3405 3889
3406package CFPlus::UI::Buttonbar; 3890package DC::UI::Buttonbar;
3407 3891
3408our @ISA = CFPlus::UI::HBox::; 3892our @ISA = DC::UI::HBox::;
3409 3893
3410# TODO: should actually wrap buttons and other goodies. 3894# TODO: should actually wrap buttons and other goodies.
3411 3895
3412############################################################################# 3896#############################################################################
3413 3897
3414package CFPlus::UI::Menu; 3898package DC::UI::Menu;
3415 3899
3416our @ISA = CFPlus::UI::Toplevel::; 3900our @ISA = DC::UI::Toplevel::;
3417 3901
3418use CFPlus::OpenGL; 3902use DC::OpenGL;
3419 3903
3420sub new { 3904sub new {
3421 my $class = shift; 3905 my $class = shift;
3422 3906
3423 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3424 items => [], 3908 items => [],
3425 z => 100, 3909 z => 100,
3426 @_, 3910 @_,
3427 ); 3911 );
3428 3912
3429 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3913 $self->add ($self->{vbox} = new DC::UI::VBox);
3430 3914
3431 for my $item (@{ $self->{items} }) { 3915 for my $item (@{ $self->{items} }) {
3432 my ($widget, $cb, $tooltip) = @$item; 3916 my ($widget, $cb, $tooltip) = @$item;
3433 3917
3434 # handle various types of items, only text for now 3918 # handle various types of items, only text for now
3435 if (!ref $widget) { 3919 if (!ref $widget) {
3436 if ($widget =~ /\t/) { 3920 if ($widget =~ /\t/) {
3437 my ($left, $right) = split /\t/, $widget, 2; 3921 my ($left, $right) = split /\t/, $widget, 2;
3438 3922
3439 $widget = new CFPlus::UI::HBox 3923 $widget = new DC::UI::HBox
3440 can_hover => 1, 3924 can_hover => 1,
3441 can_events => 1, 3925 can_events => 1,
3442 tooltip => $tooltip, 3926 tooltip => $tooltip,
3443 children => [ 3927 children => [
3444 (new CFPlus::UI::Label markup => $left, expand => 1), 3928 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3445 (new CFPlus::UI::Label markup => $right, align => +1), 3929 (new DC::UI::Label markup => $right, align => 1),
3446 ], 3930 ],
3447 ; 3931 ;
3448 3932
3449 } else { 3933 } else {
3450 $widget = new CFPlus::UI::Label 3934 $widget = new DC::UI::Label
3451 can_hover => 1, 3935 can_hover => 1,
3452 can_events => 1, 3936 can_events => 1,
3937 align => 0,
3453 markup => $widget, 3938 markup => $widget,
3454 tooltip => $tooltip; 3939 tooltip => $tooltip;
3455 } 3940 }
3456 } 3941 }
3457 3942
3472 # maybe save $GRAB? must be careful about events... 3957 # maybe save $GRAB? must be careful about events...
3473 $GRAB = $self; 3958 $GRAB = $self;
3474 $self->{button} = $ev->{button}; 3959 $self->{button} = $ev->{button};
3475 3960
3476 $self->show; 3961 $self->show;
3477 $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
3478} 3971}
3479 3972
3480sub invoke_mouse_motion { 3973sub invoke_mouse_motion {
3481 my ($self, $ev, $x, $y) = @_; 3974 my ($self, $ev, $x, $y) = @_;
3482 3975
3503 1 3996 1
3504} 3997}
3505 3998
3506############################################################################# 3999#############################################################################
3507 4000
3508package CFPlus::UI::Multiplexer; 4001package DC::UI::Multiplexer;
3509 4002
3510our @ISA = CFPlus::UI::Container::; 4003our @ISA = DC::UI::Container::;
3511 4004
3512sub new { 4005sub new {
3513 my $class = shift; 4006 my $class = shift;
3514 4007
3515 my $self = $class->SUPER::new ( 4008 my $self = $class->SUPER::new (
3516 @_, 4009 @_,
3517 ); 4010 );
3518 4011
3519 $self->{current} = $self->{children}[0] 4012 $self->set_current_page (0);
3520 if @{ $self->{children} };
3521 4013
3522 $self 4014 $self
3523} 4015}
3524 4016
3525sub add { 4017sub add {
3526 my ($self, @widgets) = @_; 4018 my ($self, @widgets) = @_;
3527 4019
3528 $self->SUPER::add (@widgets); 4020 $self->SUPER::add (@widgets);
3529 4021
3530 $self->{current} = $self->{children}[0] 4022 $self->set_current_page (0)
3531 if @{ $self->{children} }; 4023 if @widgets == @{ $self->{children} };
3532} 4024}
3533 4025
3534sub get_current_page { 4026sub get_current_page {
3535 my ($self) = @_; 4027 my ($self) = @_;
3536 4028
3542 4034
3543 my $widget = ref $page_or_widget 4035 my $widget = ref $page_or_widget
3544 ? $page_or_widget 4036 ? $page_or_widget
3545 : $self->{children}[$page_or_widget]; 4037 : $self->{children}[$page_or_widget];
3546 4038
4039 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4040
3547 $self->{current} = $widget; 4041 if (($self->{current} = $widget)) {
4042 $self->{current}->set_visible if $self->{current} && $self->{visible};
3548 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4043 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3549 4044
3550 $self->emit (page_changed => $self->{current}); 4045 $self->emit (page_changed => $self->{current});
4046 }
3551 4047
3552 $self->realloc; 4048 $self->realloc;
3553} 4049}
3554 4050
3555sub visible_children { 4051sub visible_children {
3556 $_[0]{current} 4052 $_[0]{current} || ()
3557} 4053}
3558 4054
3559sub size_request { 4055sub size_request {
3560 my ($self) = @_; 4056 my ($self) = @_;
3561 4057
4058 $self->{current}
3562 $self->{current}->size_request 4059 ? $self->{current}->size_request
4060 : (0, 0)
3563} 4061}
3564 4062
3565sub invoke_size_allocate { 4063sub invoke_size_allocate {
3566 my ($self, $w, $h) = @_; 4064 my ($self, $w, $h) = @_;
3567 4065
3568 $self->{current}->configure (0, 0, $w, $h); 4066 $self->{current}->configure (0, 0, $w, $h)
4067 if $self->{current};
3569 4068
3570 1 4069 1
3571} 4070}
3572 4071
3573sub _draw { 4072sub _draw {
3574 my ($self) = @_; 4073 my ($self) = @_;
3575 4074
3576 $self->{current}->draw; 4075 $self->{current}->draw
4076 if $self->{current};
3577} 4077}
3578 4078
3579############################################################################# 4079#############################################################################
3580 4080
3581package CFPlus::UI::Notebook; 4081package DC::UI::Notebook;
3582 4082
4083use DC::OpenGL;
4084
3583our @ISA = CFPlus::UI::VBox::; 4085our @ISA = DC::UI::VBox::;
3584 4086
3585sub new { 4087sub new {
3586 my $class = shift; 4088 my $class = shift;
3587 4089
3588 my $self = $class->SUPER::new ( 4090 my $self = $class->SUPER::new (
3589 buttonbar => (new CFPlus::UI::Buttonbar), 4091 buttonbar => (new DC::UI::Buttonbar),
3590 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4092 multiplexer => (new DC::UI::Multiplexer expand => 1),
4093 active_outline => [.7, .7, 0.2],
3591 # filter => # will be put between multiplexer and $self 4094 # filter => # will be put between multiplexer and $self
3592 @_, 4095 @_,
3593 ); 4096 );
3594 4097
3595 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4098 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3596 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4099 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3597 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
3598 $self 4135 $self
3599} 4136}
3600 4137
3601sub 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 {
3602 my ($self, $title, $widget, $tooltip) = @_; 4168 my ($self, $title, $widget, $tooltip) = @_;
3603 4169
3604 CFPlus::weaken $self; 4170 $title = [$title, $tooltip] unless ref $title;
4171 $widget->{c_tab} = $title;
3605 4172
3606 $self->{buttonbar}->add (new CFPlus::UI::Button
3607 markup => $title,
3608 tooltip => $tooltip,
3609 on_activate => sub { $self->set_current_page ($widget) },
3610 );
3611
3612 $self->{multiplexer}->add ($widget); 4173 $self->add ($widget);
3613} 4174}
3614 4175
3615sub get_current_page { 4176sub get_current_page {
3616 my ($self) = @_; 4177 my ($self) = @_;
3617 4178
3623 4184
3624 $self->{multiplexer}->set_current_page ($page); 4185 $self->{multiplexer}->set_current_page ($page);
3625 $self->emit (page_changed => $self->{multiplexer}{current}); 4186 $self->emit (page_changed => $self->{multiplexer}{current});
3626} 4187}
3627 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
3628############################################################################# 4209#############################################################################
3629 4210
3630package CFPlus::UI::Selector; 4211package DC::UI::Selector;
3631 4212
3632use utf8; 4213use utf8;
3633 4214
3634our @ISA = CFPlus::UI::Button::; 4215our @ISA = DC::UI::Button::;
3635 4216
3636sub new { 4217sub new {
3637 my $class = shift; 4218 my $class = shift;
3638 4219
3639 my $self = $class->SUPER::new ( 4220 my $self = $class->SUPER::new (
3656 my ($value, $title, $tooltip) = @$_; 4237 my ($value, $title, $tooltip) = @$_;
3657 4238
3658 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4239 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3659 } 4240 }
3660 4241
3661 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4242 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3662} 4243}
3663 4244
3664sub _set_value { 4245sub _set_value {
3665 my ($self, $value) = @_; 4246 my ($self, $value) = @_;
3666 4247
3667 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4248 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4249 $item ||= $self->{options}[0]
3668 or return; 4250 or return;
3669 4251
3670 $self->{value} = $item->[0]; 4252 $self->{value} = $item->[0];
3671 $self->set_markup ("$item->[1] ⇓"); 4253 $self->set_markup ("$item->[1] ⇓");
3672 $self->set_tooltip ($item->[2]); 4254# $self->set_tooltip ($item->[2]);
3673} 4255}
3674 4256
3675sub set_value { 4257sub set_value {
3676 my ($self, $value) = @_; 4258 my ($self, $value) = @_;
3677 4259
3679 4261
3680 $self->_set_value ($value); 4262 $self->_set_value ($value);
3681 $self->emit (changed => $value); 4263 $self->emit (changed => $value);
3682} 4264}
3683 4265
4266sub set_options {
4267 my ($self, $options) = @_;
4268
4269 $self->{options} = $options;
4270 $self->_set_value ($self->{value});
4271}
4272
3684############################################################################# 4273#############################################################################
3685 4274
3686package CFPlus::UI::Statusbox; 4275package DC::UI::Statusbox;
3687 4276
3688our @ISA = CFPlus::UI::VBox::; 4277our @ISA = DC::UI::VBox::;
3689 4278
3690sub new { 4279sub new {
3691 my $class = shift; 4280 my $class = shift;
3692 4281
3693 my $self = $class->SUPER::new ( 4282 my $self = $class->SUPER::new (
3694 fontsize => 0.8, 4283 fontsize => 0.8,
3695 @_, 4284 @_,
3696 ); 4285 );
3697 4286
3698 CFPlus::weaken (my $this = $self); 4287 DC::weaken (my $this = $self);
3699 4288
3700 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4289 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3701 4290
3702 $self 4291 $self
3703} 4292}
3704 4293
3705sub reorder { 4294sub reorder {
3706 my ($self) = @_; 4295 my ($self) = @_;
3707 my $NOW = Time::HiRes::time; 4296 my $NOW = EV::time;
3708 4297
3709 # freeze display when hovering over any label 4298 # freeze display when hovering over any label
3710 return if $CFPlus::UI::TOOLTIP->{owner} 4299 return if $DC::UI::TOOLTIP->{owner}
3711 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4300 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3712 values %{ $self->{item} }; 4301 values %{ $self->{item} };
3713 4302
3714 while (my ($k, $v) = each %{ $self->{item} }) { 4303 while (my ($k, $v) = each %{ $self->{item} }) {
3715 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4304 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3716 } 4305 }
4306
4307 $self->{timer}->set (1, 1);
3717 4308
3718 my @widgets; 4309 my @widgets;
3719 4310
3720 my @items = sort { 4311 my @items = sort {
3721 $a->{pri} <=> $b->{pri} 4312 $a->{pri} <=> $b->{pri}
3722 or $b->{id} <=> $a->{id} 4313 or $b->{id} <=> $a->{id}
3723 } values %{ $self->{item} }; 4314 } values %{ $self->{item} };
3724
3725 $self->{timer}->interval (1);
3726 4315
3727 my $count = 10 + 1; 4316 my $count = 10 + 1;
3728 for my $item (@items) { 4317 for my $item (@items) {
3729 last unless --$count; 4318 last unless --$count;
3730 4319
3737 for ($short) { 4326 for ($short) {
3738 s/^\s+//; 4327 s/^\s+//;
3739 s/\s+/ /g; 4328 s/\s+/ /g;
3740 } 4329 }
3741 4330
3742 new CFPlus::UI::Label 4331 new DC::UI::Label
3743 markup => $short, 4332 markup => $short,
3744 tooltip => $item->{tooltip}, 4333 tooltip => $item->{tooltip},
3745 tooltip_font => $::FONT_PROP, 4334 tooltip_font => $::FONT_PROP,
3746 tooltip_width => 0.67, 4335 tooltip_width => 0.67,
3747 fontsize => $item->{fontsize} || $self->{fontsize}, 4336 fontsize => $item->{fontsize} || $self->{fontsize},
3748 max_w => $::WIDTH * 0.44, 4337 max_w => $::WIDTH * 0.44,
4338 align => 0,
3749 fg => [@{ $item->{fg} }], 4339 fg => [@{ $item->{fg} }],
3750 can_events => 1, 4340 can_events => 1,
3751 can_hover => 1 4341 can_hover => 1
3752 }; 4342 };
3753 4343
3754 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4344 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3755 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4345 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3756 $label->update; 4346 $label->update;
3757 $label->set_max_size (undef, $label->{req_h} * $diff) 4347 $label->set_max_size (undef, $label->{req_h} * $diff)
3758 if $diff < 1; 4348 if $diff < 1;
3759 $self->{timer}->interval (1/30); 4349 $self->{timer}->set (1/30, 1/30);
3760 } else { 4350 } else {
3761 $label->{fg}[3] = $item->{fg}[3] || 1; 4351 $label->{fg}[3] = $item->{fg}[3] || 1;
3762 } 4352 }
3763 4353
3764 push @widgets, $label; 4354 push @widgets, $label;
3765 } 4355 }
4356
4357 my $hash = join ",", @widgets;
4358 return if $hash eq $self->{last_widget_hash};
4359 $self->{last_widget_hash} = $hash;
3766 4360
3767 $self->clear; 4361 $self->clear;
3768 $self->SUPER::add (reverse @widgets); 4362 $self->SUPER::add (reverse @widgets);
3769} 4363}
3770 4364
3826 $self->SUPER::destroy; 4420 $self->SUPER::destroy;
3827} 4421}
3828 4422
3829############################################################################# 4423#############################################################################
3830 4424
3831package CFPlus::UI::Root; 4425package DC::UI::Root;
3832 4426
3833our @ISA = CFPlus::UI::Container::; 4427our @ISA = DC::UI::Container::;
3834 4428
3835use List::Util qw(min max); 4429use List::Util qw(min max);
3836 4430
3837use CFPlus::OpenGL; 4431use DC::OpenGL;
3838 4432
3839sub new { 4433sub new {
3840 my $class = shift; 4434 my $class = shift;
3841 4435
3842 my $self = $class->SUPER::new ( 4436 my $self = $class->SUPER::new (
3843 visible => 1, 4437 visible => 1,
3844 @_, 4438 @_,
3845 ); 4439 );
3846 4440
3847 CFPlus::weaken ($self->{root} = $self); 4441 DC::weaken ($self->{root} = $self);
3848 4442
3849 $self 4443 $self
3850} 4444}
3851 4445
3852sub size_request { 4446sub size_request {
3900} 4494}
3901 4495
3902sub update { 4496sub update {
3903 my ($self) = @_; 4497 my ($self) = @_;
3904 4498
3905 $::WANT_REFRESH++; 4499 $::WANT_REFRESH = 1;
3906} 4500}
3907 4501
3908sub add { 4502sub add {
3909 my ($self, @children) = @_; 4503 my ($self, @children) = @_;
3910 4504
3947 while ($self->{refresh_hook}) { 4541 while ($self->{refresh_hook}) {
3948 $_->() 4542 $_->()
3949 for values %{delete $self->{refresh_hook}}; 4543 for values %{delete $self->{refresh_hook}};
3950 } 4544 }
3951 4545
3952 if ($self->{realloc}) { 4546 while ($self->{realloc}) {
3953 my %queue; 4547 my %queue;
3954 my @queue; 4548 my @queue;
3955 my $widget; 4549 my $widget;
3956 4550
3957 outer: 4551 outer:
3977 4571
3978 delete $queue{$widget+0}; 4572 delete $queue{$widget+0};
3979 4573
3980 my ($w, $h) = $widget->size_request; 4574 my ($w, $h) = $widget->size_request;
3981 4575
3982 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4576 $w += $widget->{padding_x} * 2;
3983 $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;
3984 4581
3985 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4582 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3986 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4583 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3987 4584
3988 $w = $widget->{force_w} if exists $widget->{force_w}; 4585 $w = $widget->{force_w} if exists $widget->{force_w};
4004 } 4601 }
4005 } 4602 }
4006 4603
4007 delete $self->{realloc}{$widget+0}; 4604 delete $self->{realloc}{$widget+0};
4008 } 4605 }
4009 }
4010 4606
4011 while (my $size_alloc = delete $self->{size_alloc}) { 4607 while (my $size_alloc = delete $self->{size_alloc}) {
4012 my @queue = sort { $b->{visible} <=> $a->{visible} } 4608 my @queue = sort { $a->{visible} <=> $b->{visible} }
4013 values %$size_alloc; 4609 values %$size_alloc;
4014 4610
4015 while () { 4611 while () {
4016 my $widget = pop @queue || last; 4612 my $widget = pop @queue || last;
4017 4613
4018 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4614 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4019 4615
4020 $w = 0 if $w < 0;
4021 $h = 0 if $h < 0;
4022
4023 $w = max $widget->{min_w}, $w; 4616 $w = max $widget->{min_w}, $w;
4024 $h = max $widget->{min_h}, $h; 4617 $h = max $widget->{min_h}, $h;
4025 4618
4026# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4619# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4027# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4620# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4028 4621
4029 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4622 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4030 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4623 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4031 4624
4032 $w = int $w + 0.5; 4625 $w = int $w + 0.5;
4033 $h = int $h + 0.5; 4626 $h = int $h + 0.5;
4034 4627
4035 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}) {
4036 $widget->{old_w} = $widget->{w}; 4629 $widget->{old_w} = $widget->{w};
4037 $widget->{old_h} = $widget->{h}; 4630 $widget->{old_h} = $widget->{h};
4038 4631
4039 $widget->{w} = $w; 4632 $widget->{w} = $w;
4040 $widget->{h} = $h; 4633 $widget->{h} = $h;
4041 4634
4042 $widget->emit (size_allocate => $w, $h); 4635 $widget->emit (size_allocate => $w, $h);
4636 }
4043 } 4637 }
4044 } 4638 }
4045 } 4639 }
4046 4640
4047 while ($self->{post_alloc_hook}) { 4641 while ($self->{post_alloc_hook}) {
4048 $_->() 4642 $_->()
4049 for values %{delete $self->{post_alloc_hook}}; 4643 for values %{delete $self->{post_alloc_hook}};
4050 } 4644 }
4051
4052 4645
4053 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4646 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4054 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4647 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4055 glClear GL_COLOR_BUFFER_BIT; 4648 glClear GL_COLOR_BUFFER_BIT;
4056 4649
4059 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4652 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4060 glMatrixMode GL_MODELVIEW; 4653 glMatrixMode GL_MODELVIEW;
4061 glLoadIdentity; 4654 glLoadIdentity;
4062 4655
4063 { 4656 {
4064 package CFPlus::UI::Base; 4657 package DC::UI::Base;
4065 4658
4066 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4659 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4067 (0, 0, $self->{w}, $self->{h}); 4660 (0, 0, $self->{w}, $self->{h});
4068 4661
4069 $self->_draw; 4662 $self->_draw;
4070 } 4663 }
4071} 4664}
4072 4665
4073############################################################################# 4666#############################################################################
4074 4667
4075package CFPlus::UI; 4668package DC::UI;
4076 4669
4077$ROOT = new CFPlus::UI::Root; 4670$ROOT = new DC::UI::Root;
4078$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4671$TOOLTIP = new DC::UI::Tooltip z => 900;
4079 4672
40801 46731
4081

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines