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.361 by root, Sat Dec 9 02:21:25 2006 UTC vs.
Revision 1.484 by root, Sun Jan 8 07:58:39 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines