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.376 by root, Mon Jul 16 20:20:23 2007 UTC vs.
Revision 1.488 by root, Fri Nov 23 12:39:05 2012 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
6
7use Event; 7use AnyEvent ();
8use Guard ();
8 9
9use CFPlus; 10use DC;
10use CFPlus::Pod; 11use DC::Pod;
11use CFPlus::Texture; 12use DC::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
17our $TOOLTIP; 18our $TOOLTIP;
18our $BUTTON_STATE; 19our $BUTTON_STATE;
19 20
20our %WIDGET; # all widgets, weak-referenced 21our %WIDGET; # all widgets, weak-referenced
21 22
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 23our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
24 $_[0]->stop;
25
23 if (!$GRAB) { 26 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 27 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 28 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 29 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 30 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 33 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 34 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 35
33 return if $ENV{CFPLUS_DEBUG} & 8; 36 return if $ENV{CFPLUS_DEBUG} & 8;
34 37
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 38 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 39 $TOOLTIP->show;
41 } 40 }
42 41
43 return; 42 return;
46 } 45 }
47 46
48 $TOOLTIP->hide; 47 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 48 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 49 delete $TOOLTIP->{owner};
51}); 50};
52 51
53sub get_layout { 52sub get_layout {
54 my $layout; 53 my $layout;
55 54
56 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
57 my $win = $layout->{$_->{name}} = { }; 56 my $win = $layout->{$_->{name}} = { };
58 57
59 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/; 58 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
60 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/; 59 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
61 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w}; 60 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
62 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h}; 61 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
63 62
72 71
73 $LAYOUT = $layout; 72 $LAYOUT = $layout;
74} 73}
75 74
76# class methods for events 75# class methods for events
77sub feed_sdl_key_down_event { 76sub feed_sdl_key_down_event {
78 $FOCUS->emit (key_down => $_[0]) 77 $FOCUS->emit (key_down => $_[0])
79 if $FOCUS; 78 if $FOCUS;
80} 79}
81 80
82sub feed_sdl_key_up_event { 81sub feed_sdl_key_up_event {
91 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
92 91
93 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 94
96 $TOOLTIP_WATCHER->start; 95 $TOOLTIP_WATCHER->again;
97 } 96 }
98} 97}
99 98
100sub feed_sdl_button_down_event { 99sub feed_sdl_button_down_event {
101 my ($ev) = @_; 100 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 106 my $widget = $ROOT->find_widget ($x, $y);
108 107
109 $GRAB = $widget; 108 $GRAB = $widget;
110 $GRAB->update if $GRAB; 109 $GRAB->update if $GRAB;
111 110
112 $TOOLTIP_WATCHER->cb->(); 111 $TOOLTIP_WATCHER->invoke;
113 } 112 }
114 113
115 if ($GRAB) { 114 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 116 # mousewheel
118 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
119
120 $ev->{dx} = $shift ? $delta : 0;
121 $ev->{dy} = $shift ? 0 : $delta;
122
120 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
121 } else { 124 } else {
122 $GRAB->emit (button_down => $ev) 125 $GRAB->emit (button_down => $ev)
123 } 126 }
124 } 127 }
138 my $grab = $GRAB; undef $GRAB; 141 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 142 $grab->update if $grab;
140 $GRAB->update if $GRAB; 143 $GRAB->update if $GRAB;
141 144
142 check_hover $widget; 145 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 146 $TOOLTIP_WATCHER->invoke;
144 } 147 }
145} 148}
146 149
147sub feed_sdl_motion_event { 150sub feed_sdl_motion_event {
148 my ($ev) = @_; 151 my ($ev) = @_;
188# call when resolution changes etc. 191# call when resolution changes etc.
189sub rescale_widgets { 192sub rescale_widgets {
190 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
191 194
192 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 199
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 213 reconfigure_widgets;
211} 214}
212 215
213############################################################################# 216#############################################################################
214 217
215package CFPlus::UI::Event; 218package DC::UI::Event;
216 219
217sub xy { 220sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 222}
220 223
221############################################################################# 224#############################################################################
222 225
223package CFPlus::UI::Base; 226package DC::UI::Base;
224 227
225use strict; 228use common::sense;
226 229
227use CFPlus::OpenGL; 230use DC::OpenGL;
228 231
229sub new { 232sub new {
230 my $class = shift; 233 my $class = shift;
231 234
232 my $self = bless { 235 my $self = bless {
237 h => undef, 240 h => undef,
238 can_events => 1, 241 can_events => 1,
239 @_ 242 @_
240 }, $class; 243 }, $class;
241 244
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 246
244 for (keys %$self) { 247 for (keys %$self) {
245 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
247 } 250 }
248 } 251 }
249 252
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 258
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 261
259 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
269 $self->emit ("destroy"); 272 $self->emit ("destroy");
270 %$self = (); 273 %$self = ();
271} 274}
272 275
273sub TO_JSON { 276sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} } 277 { "\fw" => $_[0]{s_id} }
275} 278}
276 279
277sub show { 280sub show {
278 my ($self) = @_; 281 my ($self) = @_;
279 282
280 return if $self->{parent}; 283 return if $self->{parent};
281 284
282 $CFPlus::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
283} 286}
284 287
285sub set_visible { 288sub set_visible {
286 my ($self) = @_; 289 my ($self) = @_;
287 290
288 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 295
290 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
292 298
293 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
294 300
295 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
296 302
297 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
298} 304}
299 305
300sub set_invisible { 306sub set_invisible {
301 my ($self) = @_; 307 my ($self) = @_;
302 308
308 delete $self->{root}; 314 delete $self->{root};
309 315
310 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
312 318
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 319 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
315 321
316 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
318} 324}
337sub hide { 343sub hide {
338 my ($self) = @_; 344 my ($self) = @_;
339 345
340 $self->set_invisible; 346 $self->set_invisible;
341 347
348 # extra $parent copy for 5.8.8+ bug workaround
349 # (otherwise $_[0] in remove gets freed
350 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 351 $parent->remove ($self);
343 if $self->{parent}; 352 }
344} 353}
345 354
346sub move_abs { 355sub move_abs {
347 my ($self, $x, $y, $z) = @_; 356 my ($self, $x, $y, $z) = @_;
348 357
354} 363}
355 364
356sub set_size { 365sub set_size {
357 my ($self, $w, $h) = @_; 366 my ($self, $w, $h) = @_;
358 367
359 $self->{force_w} = $w; 368 $self->{force_w} = List::Util::min $w, ($self->{max_w} || $::WIDTH );
360 $self->{force_h} = $h; 369 $self->{force_h} = List::Util::min $h, ($self->{max_h} || $::HEIGHT);
361 370
362 $self->realloc; 371 $self->realloc;
363} 372}
373
374## traverse the widget chain up to find the maximum "physical" size constraints
375#sub get_max_wh {
376# my ($self) = @_;
377#
378# my ($w, $h) = @$self{qw(max_w max_h)};
379#
380# if ($w <= 0 || $h <= 0) {
381# my ($mw, $mh) = $self->{parent}
382# ? $self->{parent}->get_max_wh
383# : ($::WIDTH, $::HEIGHT);
384#
385# $w = $mw if $w <= 0;
386# $h = $mh if $h <= 0;
387# }
388#
389# ($w, $h)
390#}
364 391
365sub size_request { 392sub size_request {
366 require Carp; 393 require Carp;
367 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
368} 395}
375 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
376 403
377 if ($self->{aspect}) { 404 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
379 406
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 409
383 # use alignment to adjust x, y 410 # use alignment to adjust x, y
384 411
385 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
427 454
428 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
429 456
430 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
431 458
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 461 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 462 }
436} 463}
437 464
438# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
439sub coord2local { 466sub coord2local {
440 my ($self, $x, $y) = @_; 467 my ($self, $x, $y) = @_;
441 468
442 Carp::confess unless $self->{parent};#d# 469 return (undef, undef) unless $self->{parent};
443 470
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 471 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 472}
446 473
447# translate local coordinates to global coordinate system 474# translate local coordinates to global coordinate system
448sub coord2global { 475sub coord2global {
449 my ($self, $x, $y) = @_; 476 my ($self, $x, $y) = @_;
450 477
451 Carp::confess unless $self->{parent};#d# 478 return (undef, undef) unless $self->{parent};
452 479
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 480 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 481}
455 482
456sub invoke_focus_in { 483sub invoke_focus_in {
505sub connect { 532sub connect {
506 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
507 534
508 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
509 536
510 defined wantarray and CFPlus::guard { 537 defined wantarray and Guard::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
513 } 540 }
514} 541}
515 542
547 574
548 # parent 575 # parent
549 $self->{parent} && $self->{parent}->emit ($signal, @args) 576 $self->{parent} && $self->{parent}->emit ($signal, @args)
550} 577}
551 578
552sub find_widget { 579#sub find_widget {
553 my ($self, $x, $y) = @_; 580# in .xs
554
555 return () unless $self->{can_events};
556
557 return $self
558 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
559 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
560
561 ()
562}
563 581
564sub set_parent { 582sub set_parent {
565 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
566 584
567 CFPlus::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
568 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
569} 587}
570 588
571sub realloc { 589sub realloc {
572 my ($self) = @_; 590 my ($self) = @_;
598 616
599# using global variables seems a bit hacky, but passing through all drawing 617# using global variables seems a bit hacky, but passing through all drawing
600# functions seems pointless. 618# functions seems pointless.
601our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 619our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
602 620
603sub draw { 621#sub draw {
604 my ($self) = @_; 622#CFPlus.xs
605
606 return unless $self->{h} && $self->{w};
607
608 # update screen rectangle
609 local $draw_x = $draw_x + $self->{x};
610 local $draw_y = $draw_y + $self->{y};
611
612 # skip widgets that are entirely outside the drawing area
613 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
614 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
615
616 glPushMatrix;
617 glTranslate $self->{x}, $self->{y}, 0;
618
619 if ($self == $HOVER && $self->{can_hover}) {
620 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
621 glEnable GL_BLEND;
622 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
623 glBegin GL_QUADS;
624 glVertex 0 , 0;
625 glVertex $self->{w}, 0;
626 glVertex $self->{w}, $self->{h};
627 glVertex 0 , $self->{h};
628 glEnd;
629 glDisable GL_BLEND;
630 }
631
632 if ($ENV{CFPLUS_DEBUG} & 1) {
633 glPushMatrix;
634 glColor 1, 1, 0, 1;
635 glTranslate 0.375, 0.375;
636 glBegin GL_LINE_LOOP;
637 glVertex 0 , 0;
638 glVertex $self->{w} - 1, 0;
639 glVertex $self->{w} - 1, $self->{h} - 1;
640 glVertex 0 , $self->{h} - 1;
641 glEnd;
642 glPopMatrix;
643 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
644 }
645
646 $self->_draw;
647 glPopMatrix;
648}
649 623
650sub _draw { 624sub _draw {
651 my ($self) = @_; 625 my ($self) = @_;
652 626
653 warn "no draw defined for $self\n"; 627 warn "no draw defined for $self\n";
654} 628}
655 629
656my $cntx;#d#
657sub DESTROY { 630sub DESTROY {
658 my ($self) = @_; 631 my ($self) = @_;
659 632
660 return if CFPlus::in_destruct; 633 return if DC::in_destruct;
661 634
635 local $@;
662 eval { $self->destroy }; 636 eval { $self->destroy };
663 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
664 638
665 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
666} 640}
667 641
668############################################################################# 642#############################################################################
669 643
670package CFPlus::UI::DrawBG; 644package DC::UI::DrawBG;
671 645
672our @ISA = CFPlus::UI::Base::; 646our @ISA = DC::UI::Base::;
673 647
674use strict; 648use common::sense;
649
675use CFPlus::OpenGL; 650use DC::OpenGL;
676 651
677sub new { 652sub new {
678 my $class = shift; 653 my $class = shift;
679
680 # range [value, low, high, page]
681 654
682 $class->SUPER::new ( 655 $class->SUPER::new (
683 #bg => [0, 0, 0, 0.2], 656 #bg => [0, 0, 0, 0.2],
684 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
685 @_ 658 @_
686 ) 659 )
687} 660}
688 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
689sub _draw { 669sub _draw {
690 my ($self) = @_; 670 my ($self) = @_;
691 671
692 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
693 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
694 : $self->{bg}; 674 : $self->{bg};
695 675
696 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
697 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
698 678
699 glEnable GL_BLEND; 679 glEnable GL_BLEND;
700 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 680 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
701 glColor_premultiply @$color; 681 glColor_premultiply @$color;
702
703 glBegin GL_QUADS;
704 glVertex 0 , 0;
705 glVertex 0 , $h;
706 glVertex $w, $h; 682 glRect 0, 0, $w, $h;
707 glVertex $w, 0;
708 glEnd;
709
710 glDisable GL_BLEND; 683 glDisable GL_BLEND;
711 } 684 }
712} 685}
713 686
714############################################################################# 687#############################################################################
715 688
716package CFPlus::UI::Empty; 689package DC::UI::Empty;
717 690
718our @ISA = CFPlus::UI::Base::; 691our @ISA = DC::UI::Base::;
719 692
720sub new { 693sub new {
721 my ($class, %arg) = @_; 694 my ($class, %arg) = @_;
722 $class->SUPER::new (can_events => 0, %arg); 695 $class->SUPER::new (can_events => 0, %arg);
723} 696}
730 703
731sub draw { } 704sub draw { }
732 705
733############################################################################# 706#############################################################################
734 707
735package CFPlus::UI::Container; 708package DC::UI::Container;
736 709
737our @ISA = CFPlus::UI::Base::; 710our @ISA = DC::UI::Base::;
738 711
739sub new { 712sub new {
740 my ($class, %arg) = @_; 713 my ($class, %arg) = @_;
741 714
742 my $children = delete $arg{children}; 715 my $children = delete $arg{children};
746 can_events => 0, 719 can_events => 0,
747 %arg, 720 %arg,
748 ); 721 );
749 722
750 $self->add (@$children) 723 $self->add (@$children)
751 if $children; 724 if $children && @$children;
752 725
753 $self 726 $self
754} 727}
755 728
756sub realloc { 729sub realloc {
765 my ($self, @widgets) = @_; 738 my ($self, @widgets) = @_;
766 739
767 $_->set_parent ($self) 740 $_->set_parent ($self)
768 for @widgets; 741 for @widgets;
769 742
743 # TODO: only do this in widgets that need it, e.g. root, fixed
770 use sort 'stable'; 744 use sort 'stable';
771 745
772 $self->{children} = [ 746 $self->{children} = [
773 sort { $a->{z} <=> $b->{z} } 747 sort { $a->{z} <=> $b->{z} }
774 @{$self->{children}}, @widgets 748 @{$self->{children}}, @widgets
775 ]; 749 ];
776 750
777 $self->realloc; 751 $self->realloc;
752
753 $self->emit (c_add => \@widgets);
754
755 map $_+0, @widgets
778} 756}
779 757
780sub children { 758sub children {
781 @{ $_[0]{children} } 759 @{ $_[0]{children} }
782} 760}
783 761
784sub remove { 762sub remove {
785 my ($self, $child) = @_; 763 my ($self, @widgets) = @_;
786 764
765 $self->emit (c_remove => \@widgets);
766
767 for my $child (@widgets) {
787 delete $child->{parent}; 768 delete $child->{parent};
788 $child->hide; 769 $child->hide;
789
790 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 770 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
771 }
791 772
792 $self->realloc; 773 $self->realloc;
793} 774}
794 775
795sub clear { 776sub clear {
796 my ($self) = @_; 777 my ($self) = @_;
797 778
798 my $children = delete $self->{children}; 779 my $children = $self->{children};
799 $self->{children} = []; 780 $self->{children} = [];
800 781
801 for (@$children) { 782 for (@$children) {
802 delete $_->{parent}; 783 delete $_->{parent};
803 $_->hide; 784 $_->hide;
823} 804}
824 805
825sub _draw { 806sub _draw {
826 my ($self) = @_; 807 my ($self) = @_;
827 808
828 $_->draw for @{$self->{children}}; 809 $_->draw for $self->visible_children;
829} 810}
830 811
831############################################################################# 812#############################################################################
832 813
833package CFPlus::UI::Bin; 814package DC::UI::Bin;
834 815
835our @ISA = CFPlus::UI::Container::; 816our @ISA = DC::UI::Container::;
836 817
837sub new { 818sub new {
838 my ($class, %arg) = @_; 819 my ($class, %arg) = @_;
839 820
840 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 821 my $child = (delete $arg{child}) || new DC::UI::Empty::;
841 822
842 $class->SUPER::new (children => [$child], %arg) 823 $class->SUPER::new (children => [$child], %arg)
843} 824}
844 825
845sub add { 826sub add {
846 my ($self, $child) = @_; 827 my ($self, $child) = @_;
847 828
848 $self->SUPER::remove ($_) for @{ $self->{children} }; 829 $self->clear;
849 $self->SUPER::add ($child); 830 $self->SUPER::add ($child);
850} 831}
851 832
852sub remove { 833sub remove {
853 my ($self, $widget) = @_; 834 my ($self, $widget) = @_;
854 835
855 $self->SUPER::remove ($widget); 836 $self->SUPER::remove ($widget);
856 837
857 $self->{children} = [new CFPlus::UI::Empty] 838 $self->{children} = [new DC::UI::Empty]
858 unless @{$self->{children}}; 839 unless @{$self->{children}};
859} 840}
860 841
861sub child { $_[0]->{children}[0] } 842sub child { $_[0]->{children}[0] }
862 843
871 852
872 1 853 1
873} 854}
874 855
875############################################################################# 856#############################################################################
876
877# back-buffered drawing area 857# back-buffered drawing area
878 858
879package CFPlus::UI::Window; 859package DC::UI::Window;
880 860
881our @ISA = CFPlus::UI::Bin::; 861our @ISA = DC::UI::Bin::;
882 862
883use CFPlus::OpenGL; 863use DC::OpenGL;
884 864
885sub new { 865sub new {
886 my ($class, %arg) = @_; 866 my ($class, %arg) = @_;
887 867
888 my $self = $class->SUPER::new (%arg); 868 my $self = $class->SUPER::new (%arg);
910} 890}
911 891
912sub render_child { 892sub render_child {
913 my ($self) = @_; 893 my ($self) = @_;
914 894
915 $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 {
916 glClearColor 0, 0, 0, 0; 896 glClearColor 0, 0, 0, 0;
917 glClear GL_COLOR_BUFFER_BIT; 897 glClear GL_COLOR_BUFFER_BIT;
918 898
919 { 899 {
920 package CFPlus::UI::Base; 900 package DC::UI::Base;
921 901
922 local ($draw_x, $draw_y, $draw_w, $draw_h) = 902 local ($draw_x, $draw_y, $draw_w, $draw_h) =
923 (0, 0, $self->{w}, $self->{h}); 903 (0, 0, $self->{w}, $self->{h});
924 904
925 $self->_render; 905 $self->_render;
942 glDisable GL_TEXTURE_2D; 922 glDisable GL_TEXTURE_2D;
943} 923}
944 924
945############################################################################# 925#############################################################################
946 926
947package CFPlus::UI::ViewPort; 927package DC::UI::ViewPort;
948 928
949use List::Util qw(min max); 929use List::Util qw(min max);
950 930
951our @ISA = CFPlus::UI::Window::; 931our @ISA = DC::UI::Window::;
952 932
953sub new { 933sub new {
954 my $class = shift; 934 my $class = shift;
955 935
956 $class->SUPER::new ( 936 $class->SUPER::new (
963sub size_request { 943sub size_request {
964 my ($self) = @_; 944 my ($self) = @_;
965 945
966 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 946 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
967 947
968 $w = 10 if $self->{scroll_x}; 948 $w = 1 if $self->{scroll_x};
969 $h = 10 if $self->{scroll_y}; 949 $h = 1 if $self->{scroll_y};
970 950
971 ($w, $h) 951 ($w, $h)
972} 952}
973 953
974sub invoke_size_allocate { 954sub invoke_size_allocate {
986} 966}
987 967
988sub set_offset { 968sub set_offset {
989 my ($self, $x, $y) = @_; 969 my ($self, $x, $y) = @_;
990 970
991 $self->{view_x} = max 0, min $self->child->{w} - $self->{w}, int $x; 971 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
992 $self->{view_y} = max 0, min $self->child->{h} - $self->{h}, int $y; 972 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
993 973
994 $self->emit (changed => $self->{view_x}, $self->{view_y}); 974 if ($x != $self->{view_x} or $y != $self->{view_y}) {
975 $self->{view_x} = $x;
976 $self->{view_y} = $y;
977
978 $self->emit (changed => $x, $y);
995 $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 }
996} 999}
997 1000
998# 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
999sub coord2local { 1002sub coord2local {
1000 my ($self, $x, $y) = @_; 1003 my ($self, $x, $y) = @_;
1015 my ($self, $x, $y) = @_; 1018 my ($self, $x, $y) = @_;
1016 1019
1017 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1018 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1019 ) { 1022 ) {
1020 $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})
1021 } else { 1024 } else {
1022 $self->CFPlus::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
1023 } 1026 }
1024} 1027}
1025 1028
1026sub _render { 1029sub _render {
1027 my ($self) = @_; 1030 my ($self) = @_;
1028 1031
1029 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};
1030 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};
1031 1034
1032 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1033 1036
1034 $self->SUPER::_render; 1037 $self->SUPER::_render;
1035} 1038}
1036 1039
1037############################################################################# 1040#############################################################################
1038 1041
1039package CFPlus::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1040 1043
1041our @ISA = CFPlus::UI::Table::; 1044our @ISA = DC::UI::Table::;
1042 1045
1043sub new { 1046sub new {
1044 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1045 1048
1046 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1047 1050
1048 my $self; 1051 my $self;
1049 1052
1050 my $hslider = new CFPlus::UI::Slider 1053 my $hslider = new DC::UI::Slider
1054 c_col => 0,
1055 c_row => 1,
1051 vertical => 0, 1056 vertical => 0,
1052 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1053 on_changed => sub { 1058 on_changed => sub {
1054 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1055 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1056 }, 1061 },
1057 ; 1062 ;
1058 1063
1059 my $vslider = new CFPlus::UI::Slider 1064 my $vslider = new DC::UI::Slider
1065 c_col => 1,
1066 c_row => 0,
1060 vertical => 1, 1067 vertical => 1,
1061 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1062 on_changed => sub { 1069 on_changed => sub {
1063 $self->{vpos} = $_[1]; 1070 $self->{vpos} = $_[1];
1064 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1071 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1074 col_expand => [1, 0], 1081 col_expand => [1, 0],
1075 row_expand => [1, 0], 1082 row_expand => [1, 0],
1076 %arg, 1083 %arg,
1077 ); 1084 );
1078 1085
1079 $self->{vp} = new CFPlus::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1087 c_col => 0,
1088 c_row => 0,
1080 expand => 1, 1089 expand => 1,
1081 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1082 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1083 on_changed => sub { 1092 on_changed => sub {
1084 my ($vp, $x, $y) = @_; 1093 my ($vp, $x, $y) = @_;
1094
1085 $vp->{parent}{hslider}->set_value ($x); 1095 $vp->{parent}{hslider}->set_value ($x);
1086 $vp->{parent}{vslider}->set_value ($y); 1096 $vp->{parent}{vslider}->set_value ($y);
1087 1097
1098 0
1099 },
1100 on_size_allocate => sub {
1101 my ($vp, $w, $h) = @_;
1102 $vp->{parent}->update_slider;
1088 0 1103 0
1089 }, 1104 },
1090 ; 1105 ;
1091 1106
1092 $self->SUPER::add (0, 0, $self->{vp}); 1107 $self->SUPER::add ($self->{vp});
1093 1108
1094 $self->add ($child) if $child; 1109 $self->add ($child) if $child;
1095 1110
1096 $self 1111 $self
1097} 1112}
1098
1099#TODO# update range on size_allocate depending on child
1100 1113
1101sub add { 1114sub add {
1102 my ($self, $widget) = @_; 1115 my ($self, $widget) = @_;
1103 1116
1104 $self->{vp}->add ($self->{child} = $widget); 1117 $self->{vp}->add ($self->{child} = $widget);
1105} 1118}
1106 1119
1120sub set_offset { shift->{vp}->set_offset (@_) }
1121sub set_center { shift->{vp}->set_center (@_) }
1122sub make_visible { shift->{vp}->make_visible (@_) }
1123
1107sub update_slider { 1124sub update_slider {
1108 my ($self) = @_; 1125 my ($self) = @_;
1109 1126
1110 my $child = ($self->{vp} or return)->child; 1127 my $child = ($self->{vp} or return)->child;
1111 1128
1129 if ($self->{scroll_x}) {
1112 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1130 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1113 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1131 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1114 1132
1115 my $visible = $w1 > $w2; 1133 my $visible = $w1 > $w2;
1116 if ($visible != $self->{hslider}{visible}) { 1134 if ($visible != $self->{hslider_visible}) {
1135 $self->{hslider_visible} = $visible;
1117 $visible ? $self->SUPER::add (0, 1, $self->{hslider}) 1136 $visible ? $self->SUPER::add ($self->{hslider})
1118 : $self->{hslider}->hide; 1137 : $self->SUPER::remove ($self->{hslider});
1138 }
1119 } 1139 }
1120 1140
1141 if ($self->{scroll_y}) {
1121 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1142 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1122 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1143 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1123 1144
1124 my $visible = $h1 > $h2; 1145 my $visible = $h1 > $h2;
1125 if ($visible != $self->{vslider}{visible}) { 1146 if ($visible != $self->{vslider_visible}) {
1147 $self->{vslider_visible} = $visible;
1126 $visible ? $self->SUPER::add (1, 0, $self->{vslider}) 1148 $visible ? $self->SUPER::add ($self->{vslider})
1127 : $self->{vslider}->hide; 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);
1128 } 1169 };
1129}
1130
1131sub update {
1132 my ($self) = @_;
1133
1134 $self->SUPER::update;
1135 $self->update_slider;
1136} 1170}
1137 1171
1138sub invoke_mouse_wheel { 1172sub invoke_mouse_wheel {
1139 my ($self, $ev) = @_; 1173 my ($self, $ev) = @_;
1140 1174
1141 return 0 unless $ev->{dy}; # only vertical movements for now
1142
1143 $self->{vslider}->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};
1144 1177
1145 1 1178 1
1146} 1179}
1147 1180
1148sub invoke_button_down { 1181sub invoke_button_down {
1149 my ($self, $ev, $x, $y) = @_; 1182 my ($self, $ev, $x, $y) = @_;
1150 1183
1151 if ($ev->{button} == 2) { 1184 if ($ev->{button} == 2) {
1152 $self->grab_focus; 1185 $self->start_dragging ($ev);
1153
1154 my $ox = $self->{vp}{view_x} + $ev->{x};
1155 my $oy = $self->{vp}{view_y} + $ev->{y};
1156
1157 $self->{motion} = sub {
1158 my ($ev, $x, $y) = @_;
1159
1160 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1161 $self->update;
1162 };
1163
1164 return 1; 1186 return 1;
1165 } 1187 }
1166 1188
1167 0 1189 0
1168} 1190}
1195 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1196} 1218}
1197 1219
1198############################################################################# 1220#############################################################################
1199 1221
1200package CFPlus::UI::Frame; 1222package DC::UI::Frame;
1201 1223
1202our @ISA = CFPlus::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1203 1225
1204use CFPlus::OpenGL; 1226use DC::OpenGL;
1205 1227
1206sub new { 1228sub new {
1207 my $class = shift; 1229 my $class = shift;
1208 1230
1209 $class->SUPER::new ( 1231 $class->SUPER::new (
1219 my ($w, $h) = @$self{qw(w h)}; 1241 my ($w, $h) = @$self{qw(w h)};
1220 1242
1221 glEnable GL_BLEND; 1243 glEnable GL_BLEND;
1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1244 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1223 glColor_premultiply @{ $self->{bg} }; 1245 glColor_premultiply @{ $self->{bg} };
1224
1225 glBegin GL_QUADS;
1226 glVertex 0 , 0;
1227 glVertex 0 , $h;
1228 glVertex $w, $h; 1246 glRect 0, 0, $w, $h;
1229 glVertex $w, 0;
1230 glEnd;
1231
1232 glDisable GL_BLEND; 1247 glDisable GL_BLEND;
1233 } 1248 }
1234 1249
1235 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1236} 1251}
1237 1252
1238############################################################################# 1253#############################################################################
1239 1254
1240package CFPlus::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1241 1256
1242our @ISA = CFPlus::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1243 1258
1244use CFPlus::OpenGL; 1259use DC::OpenGL;
1245 1260
1246sub new { 1261sub new {
1247 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1248 1263
1249 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1250 $arg{label} = new CFPlus::UI::Label 1265 $arg{label} = new DC::UI::Label
1251 align => 1, 1266 align => 1,
1252 valign => 0, 1267 valign => 0.5,
1253 text => $arg{label}, 1268 text => $arg{label},
1254 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1255 } 1270 }
1256 1271
1257 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1258 # label => "", 1273 # label => "",
1259 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1260 border => 0.8, 1275 border => 0.8,
1261 style => 'single', 1276 style => 'single',
1262 %arg, 1277 %arg,
1263 ); 1278 );
1264 1279
1267 1282
1268sub add { 1283sub add {
1269 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1270 1285
1271 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1272 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1273} 1288}
1274 1289
1275sub border { 1290sub border {
1276 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1277} 1292}
1317 my $border = $self->border; 1332 my $border = $self->border;
1318 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1319 1334
1320 $child->draw; 1335 $child->draw;
1321 1336
1322 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1323 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1324 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1325 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1326 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;
1327 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;
1335 } 1350 }
1336} 1351}
1337 1352
1338############################################################################# 1353#############################################################################
1339 1354
1340package CFPlus::UI::Toplevel; 1355package DC::UI::Toplevel;
1341 1356
1342our @ISA = CFPlus::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1343 1358
1344use CFPlus::OpenGL; 1359use DC::OpenGL;
1345 1360
1346my $bg = 1361my $bg =
1347 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1348 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1349 1364
1350my @border = 1365my @border =
1351 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1352 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);
1353 1372
1354sub new { 1373sub new {
1355 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1356 1375
1357 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1358 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1359 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1360 border => 0.6, 1379 border => 0.8,
1361 can_events => 1, 1380 can_events => 1,
1362 min_w => 64, 1381 min_w => 64,
1363 min_h => 32, 1382 min_h => 32,
1364 %arg, 1383 %arg,
1365 ); 1384 );
1366 1385
1367 $self->{title_widget} = new CFPlus::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1368 align => 0, 1387 align => 0.5,
1369 valign => 1, 1388 valign => 1,
1370 text => $self->{title}, 1389 text => $self->{title},
1371 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1372 if exists $self->{title}; 1391 if exists $self->{title};
1373 1392
1374 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1375 $self->{close_button} = 1394 $self->{close_button} =
1376 new CFPlus::UI::ImageButton 1395 new DC::UI::ImageButton
1377 path => 'x1_close.png', 1396 path => 'x1_close.png',
1378 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1379 1398
1380 $self->CFPlus::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1381 } 1400 }
1382 1401
1383 $self 1402 $self
1384} 1403}
1385 1404
1386sub add { 1405sub add {
1387 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1388 1407
1389 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1390 $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};
1391 $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};
1392} 1411}
1393 1412
1394sub border { 1413sub border {
1395 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1396} 1415}
1416
1417#sub 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
1424#}
1397 1425
1398sub size_request { 1426sub size_request {
1399 my ($self) = @_; 1427 my ($self) = @_;
1400 1428
1401 $self->{title_widget}->size_request 1429 $self->{title_widget}->size_request
1436 1464
1437sub invoke_delete { 1465sub invoke_delete {
1438 my ($self) = @_; 1466 my ($self) = @_;
1439 1467
1440 $self->hide; 1468 $self->hide;
1441 1469
1442 1 1470 1
1443} 1471}
1444 1472
1445sub invoke_button_down { 1473sub invoke_button_down {
1446 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1447 1475
1448 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1449 my $border = $self->border; 1477 my $border = $self->border;
1450 1478
1451 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
1452 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
1453 1481
1454 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1455 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1456 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1457 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1458 1486
1459 my $mx = $x < $border; 1487 my $mx = $x < $border;
1463 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1464 1492
1465 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1466 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1467 1495
1496 $self->set_size (
1468 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1469 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1470 1500
1471 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1472 $self->realloc; 1502 $self->realloc;
1473 }; 1503 };
1474 1504
1475 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1476 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1477 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1478 1508
1479 $self->{motion} = sub { 1509 $self->{motion} = sub {
1480 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1525 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1526 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1527 1557
1528 my $border = $self->border; 1558 my $border = $self->border;
1529 1559
1560 if ($border) {
1530 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1531 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1532 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1533 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1534 $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 }
1535 1581
1536 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1537 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1538 1584
1539 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
1558 if $self->{close_button}; 1604 if $self->{close_button};
1559} 1605}
1560 1606
1561############################################################################# 1607#############################################################################
1562 1608
1563package CFPlus::UI::Table; 1609package DC::UI::Table;
1564 1610
1565our @ISA = CFPlus::UI::Base::; 1611our @ISA = DC::UI::Container::;
1566 1612
1567use List::Util qw(max sum); 1613use List::Util qw(max sum);
1568 1614
1569use CFPlus::OpenGL; 1615use DC::OpenGL;
1570 1616
1571sub new { 1617sub new {
1572 my $class = shift; 1618 my $class = shift;
1573 1619
1574 $class->SUPER::new ( 1620 $class->SUPER::new (
1575 children => [],
1576 col_expand => [], 1621 col_expand => [],
1577 row_expand => [], 1622 row_expand => [],
1578 @_, 1623 @_,
1579 ) 1624 )
1580} 1625}
1581 1626
1582sub children {
1583 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1584}
1585
1586# TODO: store row/col info in child widget and use standard add/del
1587sub 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 {
1588 my ($self) = shift; 1639 my $self = shift;
1640
1641 my @widgets;
1589 1642
1590 while (@_) { 1643 while (@_) {
1591 my ($x, $y, $child) = splice @_, 0, 3, (); 1644 my ($col, $row, $child) = splice @_, 0, 3, ();
1592 $child->set_parent ($self);
1593 $self->{children}[$y][$x] = $child;
1594 }
1595 1645
1596 $self->{force_realloc} = 1; 1646 $child->{c_row} = $row;
1597 $self->{force_size_alloc} = 1; 1647 $child->{c_col} = $col;
1598 $self->realloc;
1599}
1600 1648
1601sub remove { 1649 push @widgets, $child;
1650 }
1651
1652 $self->add (@widgets);
1653}
1654
1655sub get_wh {
1602 my ($self, $child) = @_; 1656 my ($self) = @_;
1603 1657
1604 for (@{ $self->{children} }) { 1658 my (@w, @h);
1605 for (@{ $_ || [] }) { 1659
1606 $_ = undef if $_ == $child; 1660 my @children = $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];
1607 } 1670 }
1608 } 1671 }
1609}
1610 1672
1611# TODO: move to container class maybe? send children a signal on removal? 1673 # second pass, rows
1612sub clear { 1674 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1613 my ($self) = @_;
1614
1615 my @children = $self->children;
1616 delete $self->{children};
1617
1618 for (@children) {
1619 delete $_->{parent};
1620 $_->hide;
1621 }
1622
1623 $self->realloc;
1624}
1625
1626sub get_wh {
1627 my ($self) = @_;
1628
1629 my (@w, @h);
1630
1631 for my $y (0 .. $#{$self->{children}}) {
1632 my $row = $self->{children}[$y]
1633 or next;
1634
1635 for my $x (0 .. $#$row) {
1636 my $widget = $row->[$x]
1637 or next;
1638 my ($w, $h) = @$widget{qw(req_w req_h)}; 1675 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1639 1676
1640 $w[$x] = max $w[$x], $w; 1677 my $sh = sum @h[$r .. $r + $rs - 1];
1641 $h[$y] = max $h[$y], $h; 1678
1679 if ($h > $sh) {
1680 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1642 } 1681 }
1643 } 1682 }
1644 1683
1645 (\@w, \@h) 1684 (\@w, \@h)
1646} 1685}
1662 my ($ws, $hs) = $self->get_wh; 1701 my ($ws, $hs) = $self->get_wh;
1663 1702
1664 my $req_w = (sum @$ws) || 1; 1703 my $req_w = (sum @$ws) || 1;
1665 my $req_h = (sum @$hs) || 1; 1704 my $req_h = (sum @$hs) || 1;
1666 1705
1667 # TODO: nicer code 1706 # now linearly scale the rows/columns to the allocated size
1668 my @col_expand = @{$self->{col_expand}}; 1707 my @col_expand = @{$self->{col_expand}};
1669 @col_expand = (1) x @$ws unless @col_expand; 1708 @col_expand = (1) x @$ws unless @col_expand;
1670 my $col_expand = (sum @col_expand) || 1; 1709 my $col_expand = (sum @col_expand) || 1;
1671 1710
1672 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1711 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1673 1712
1674 CFPlus::UI::harmonize $ws; 1713 DC::UI::harmonize $ws;
1675 1714
1676 my @row_expand = @{$self->{row_expand}}; 1715 my @row_expand = @{$self->{row_expand}};
1677 @row_expand = (1) x @$ws unless @row_expand; 1716 @row_expand = (1) x @$ws unless @row_expand;
1678 my $row_expand = (sum @row_expand) || 1; 1717 my $row_expand = (sum @row_expand) || 1;
1679 1718
1680 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1719 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1681 1720
1682 CFPlus::UI::harmonize $hs; 1721 DC::UI::harmonize $hs;
1683 1722
1684 my $y; 1723 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1724 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1685 1725
1686 for my $r (0 .. $#{$self->{children}}) { 1726 for my $widget ($self->children) {
1687 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)};
1688 or next;
1689 1728
1690 my $x = 0; 1729 $widget->configure (
1691 my $row_h = $hs->[$r]; 1730 $x[$c], $y[$r],
1731 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1692 1732 );
1693 for my $c (0 .. $#$row) {
1694 my $col_w = $ws->[$c];
1695
1696 if (my $widget = $row->[$c]) {
1697 $widget->configure ($x, $y, $col_w, $row_h);
1698 }
1699
1700 $x += $col_w;
1701 }
1702
1703 $y += $row_h;
1704 } 1733 }
1705 1734
1706 1 1735 1
1707} 1736}
1708 1737
1709sub find_widget {
1710 my ($self, $x, $y) = @_;
1711
1712 $x -= $self->{x};
1713 $y -= $self->{y};
1714
1715 my $res;
1716
1717 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1718 $res = $_->find_widget ($x, $y)
1719 and return $res;
1720 }
1721
1722 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1723}
1724
1725sub _draw {
1726 my ($self) = @_;
1727
1728 for (grep $_, @{$self->{children}}) {
1729 $_->draw for grep $_, @$_;
1730 }
1731}
1732
1733############################################################################# 1738#############################################################################
1734 1739
1735package CFPlus::UI::Fixed; 1740package DC::UI::Fixed;
1736 1741
1737use List::Util qw(min max); 1742use List::Util qw(min max);
1738 1743
1739our @ISA = CFPlus::UI::Container::; 1744our @ISA = DC::UI::Container::;
1740
1741sub add {
1742 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1743
1744 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1745 $self->SUPER::add ($child);
1746}
1747 1745
1748sub _scale($$$) { 1746sub _scale($$$) {
1749 my ($mode, $val, $max) = @_; 1747 my ($rel, $val, $max) = @_;
1750 1748
1751 $mode eq "abs" ? $val 1749 $rel ? $val * $max : $val
1752 : $mode eq "rel" ? $val * $max
1753 : 0
1754} 1750}
1755 1751
1756sub size_request { 1752sub size_request {
1757 my ($self) = @_; 1753 my ($self) = @_;
1758 1754
1759 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1755 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1760 1756
1761 # determine overall size by querying abs widgets 1757 # determine overall size by querying abs widgets
1762 for my $child ($self->visible_children) { 1758 for my $child ($self->visible_children) {
1763 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1759 unless ($child->{c_rel}) {
1760 my $x = $child->{c_x};
1761 my $y = $child->{c_y};
1764 1762
1765 if ($pos eq "abs") {
1766 $w = _scale $size, $w, $child->{req_w};
1767 $h = _scale $size, $h, $child->{req_h};
1768
1769 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1763 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1770 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1764 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1771 } 1765 }
1772 } 1766 }
1773 1767
1774 my $W = $x2 - $x1; 1768 my $W = $x2 - $x1;
1775 my $H = $y2 - $y1; 1769 my $H = $y2 - $y1;
1776 1770
1777 # now layout remaining widgets 1771 # now layout remaining widgets
1778 for my $child ($self->visible_children) { 1772 for my $child ($self->visible_children) {
1779 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 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;
1780 1776
1781 if ($pos ne "abs") {
1782 $x = _scale $pos, $x, $W;
1783 $y = _scale $pos, $x, $H;
1784 $w = _scale $size, $w, $child->{req_w};
1785 $h = _scale $size, $h, $child->{req_h};
1786
1787 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1777 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1788 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1778 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1789 } 1779 }
1790 } 1780 }
1791 1781
1792 my $W = $x2 - $x1; 1782 my $W = $x2 - $x1;
1793 my $H = $y2 - $y1; 1783 my $H = $y2 - $y1;
1797 1787
1798sub invoke_size_allocate { 1788sub invoke_size_allocate {
1799 my ($self, $W, $H) = @_; 1789 my ($self, $W, $H) = @_;
1800 1790
1801 for my $child ($self->visible_children) { 1791 for my $child ($self->visible_children) {
1802 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1792 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1793 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1803 1794
1804 $x = _scale $pos, $x, $W; 1795 $x += $child->{c_halign} * $child->{req_w};
1805 $y = _scale $pos, $x, $H; 1796 $y += $child->{c_valign} * $child->{req_h};
1806 $w = _scale $size, $w, $child->{req_w};
1807 $h = _scale $size, $h, $child->{req_h};
1808 1797
1809 $child->configure ($x, $y, $w, $h); 1798 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1810 } 1799 }
1811 1800
1812 1 1801 1
1813} 1802}
1814 1803
1815############################################################################# 1804#############################################################################
1816 1805
1817package CFPlus::UI::Box; 1806package DC::UI::Box;
1818 1807
1819our @ISA = CFPlus::UI::Container::; 1808our @ISA = DC::UI::Container::;
1820 1809
1821sub size_request { 1810sub size_request {
1822 my ($self) = @_; 1811 my ($self) = @_;
1812
1813 my @children = $self->visible_children;
1823 1814
1824 $self->{vertical} 1815 $self->{vertical}
1825 ? ( 1816 ? (
1826 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1817 (List::Util::max map $_->{req_w}, @children),
1827 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1818 (List::Util::sum map $_->{req_h}, @children),
1828 ) 1819 )
1829 : ( 1820 : (
1830 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1821 (List::Util::sum map $_->{req_w}, @children),
1831 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1822 (List::Util::max map $_->{req_h}, @children),
1832 ) 1823 )
1833} 1824}
1834 1825
1835sub invoke_size_allocate { 1826sub invoke_size_allocate {
1836 my ($self, $w, $h) = @_; 1827 my ($self, $w, $h) = @_;
1849 if ($req > $space) { 1840 if ($req > $space) {
1850 # ah well, not enough space 1841 # ah well, not enough space
1851 $_ *= $space / $req for @req; 1842 $_ *= $space / $req for @req;
1852 } else { 1843 } else {
1853 my $expand = (List::Util::sum map $_->{expand}, @children) || 1; 1844 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1854 1845
1855 $space = ($space - $req) / $expand; # remaining space to give away 1846 $space = ($space - $req) / $expand; # remaining space to give away
1856 1847
1857 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1858 for 0 .. $#children; 1849 for 0 .. $#children;
1859 } 1850 }
1860 } 1851 }
1861 1852
1862 CFPlus::UI::harmonize \@req; 1853 DC::UI::harmonize \@req;
1863 1854
1864 my $pos = 0; 1855 my $pos = 0;
1865 for (0 .. $#children) { 1856 for (0 .. $#children) {
1866 my $alloc = $req[$_]; 1857 my $alloc = $req[$_];
1867 $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));
1872 1 1863 1
1873} 1864}
1874 1865
1875############################################################################# 1866#############################################################################
1876 1867
1877package CFPlus::UI::HBox; 1868package DC::UI::HBox;
1878 1869
1879our @ISA = CFPlus::UI::Box::; 1870our @ISA = DC::UI::Box::;
1880 1871
1881sub new { 1872sub new {
1882 my $class = shift; 1873 my $class = shift;
1883 1874
1884 $class->SUPER::new ( 1875 $class->SUPER::new (
1887 ) 1878 )
1888} 1879}
1889 1880
1890############################################################################# 1881#############################################################################
1891 1882
1892package CFPlus::UI::VBox; 1883package DC::UI::VBox;
1893 1884
1894our @ISA = CFPlus::UI::Box::; 1885our @ISA = DC::UI::Box::;
1895 1886
1896sub new { 1887sub new {
1897 my $class = shift; 1888 my $class = shift;
1898 1889
1899 $class->SUPER::new ( 1890 $class->SUPER::new (
1902 ) 1893 )
1903} 1894}
1904 1895
1905############################################################################# 1896#############################################################################
1906 1897
1907package CFPlus::UI::Label; 1898package DC::UI::Label;
1908 1899
1909our @ISA = CFPlus::UI::DrawBG::; 1900our @ISA = DC::UI::DrawBG::;
1910 1901
1911use CFPlus::OpenGL; 1902use DC::OpenGL;
1912 1903
1913sub new { 1904sub new {
1914 my ($class, %arg) = @_; 1905 my ($class, %arg) = @_;
1915 1906
1916 my $self = $class->SUPER::new ( 1907 my $self = $class->SUPER::new (
1921 #text => initial text 1912 #text => initial text
1922 #markup => initial narkup 1913 #markup => initial narkup
1923 #max_w => maximum pixel width 1914 #max_w => maximum pixel width
1924 #style => 0, # render flags 1915 #style => 0, # render flags
1925 ellipsise => 3, # end 1916 ellipsise => 3, # end
1926 layout => (new CFPlus::Layout), 1917 layout => (new DC::Layout),
1927 fontsize => 1, 1918 fontsize => 1,
1928 align => -1, 1919 align => 0.5,
1929 valign => -1, 1920 valign => 0.5,
1930 padding_x => 2, 1921 padding_x => 4,
1931 padding_y => 2, 1922 padding_y => 2,
1932 can_events => 0, 1923 can_events => 0,
1933 %arg 1924 %arg
1934 ); 1925 );
1935 1926
1936 if (exists $self->{template}) { 1927 if (exists $self->{template}) {
1937 my $layout = new CFPlus::Layout; 1928 my $layout = new DC::Layout;
1938 $layout->set_text (delete $self->{template}); 1929 $layout->set_text (delete $self->{template});
1939 $self->{template} = $layout; 1930 $self->{template} = $layout;
1940 } 1931 }
1941 1932
1942 if (exists $self->{markup}) { 1933 if (exists $self->{markup}) {
1960 1951
1961 delete $self->{ox}; 1952 delete $self->{ox};
1962 $self->SUPER::realloc; 1953 $self->SUPER::realloc;
1963} 1954}
1964 1955
1956sub clear {
1957 my ($self) = @_;
1958
1959 $self->set_text ("");
1960}
1961
1965sub set_text { 1962sub set_text {
1966 my ($self, $text) = @_; 1963 my ($self, $text) = @_;
1967 1964
1968 return if $self->{text} eq "T$text"; 1965 return if $self->{text} eq "T$text";
1969 $self->{text} = "T$text"; 1966 $self->{text} = "T$text";
1992 1989
1993sub size_request { 1990sub size_request {
1994 my ($self) = @_; 1991 my ($self) = @_;
1995 1992
1996 $self->{size_req} ||= do { 1993 $self->{size_req} ||= do {
1994 my $max_w = $self->{w} || $self->{max_w} || 0x0fffffff; # actually 2**31-1 but allow for overflow inside pango
1995
1997 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
1998 $self->{layout}->set_width ($self->{max_w} || -1); 1997 $self->{layout}->set_width ($max_w);
1999 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
2000 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2001 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2000 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2002 2001
2003 my ($w, $h) = $self->{layout}->size; 2002 my ($w, $h) = $self->{layout}->size;
2004 2003
2005 if (exists $self->{template}) { 2004 if (exists $self->{template}) {
2006 $self->{template}->set_font ($self->{font}) if $self->{font}; 2005 $self->{template}->set_font ($self->{font}) if $self->{font};
2007 $self->{template}->set_width ($self->{max_w} || -1); 2006 $self->{template}->set_width ($max_w);
2008 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2007 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
2009 2008
2010 my ($w2, $h2) = $self->{template}->size; 2009 my ($w2, $h2) = $self->{template}->size;
2011 2010
2012 $w = List::Util::max $w, $w2; 2011 $w = List::Util::max $w, $w2;
2068 2067
2069 [$self->{layout}->size] 2068 [$self->{layout}->size]
2070 }; 2069 };
2071 2070
2072 unless (exists $self->{ox}) { 2071 unless (exists $self->{ox}) {
2073 $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);
2074 : $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);
2075 : ($self->{w} - $size->[0]) * 0.5);
2076 2074
2077 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2075 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2078 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2079 : ($self->{h} - $size->[1]) * 0.5);
2080 }; 2076 };
2081 2077
2082 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2078# unless ($self->{list}) {
2083 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2079# $self->{list} = DC::OpenGL::glGenList;
2084 2080# DC::OpenGL::glNewList $self->{list};
2085 $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;
2086} 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#}
2087 2097
2088############################################################################# 2098#############################################################################
2089 2099
2090package CFPlus::UI::EntryBase; 2100package DC::UI::EntryBase;
2091 2101
2092our @ISA = CFPlus::UI::Label::; 2102our @ISA = DC::UI::Label::;
2093 2103
2094use CFPlus::OpenGL; 2104use DC::OpenGL;
2095 2105
2096sub new { 2106sub new {
2097 my $class = shift; 2107 my $class = shift;
2098 2108
2099 $class->SUPER::new ( 2109 $class->SUPER::new (
2100 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2101 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2112 outline => undef,
2102 active_bg => [1, 1, 1, 0.5], 2113 active_bg => [0, 0, 1, .2],
2103 active_fg => [0, 0, 0], 2114 active_fg => [1, 1, 1],
2115 active_outline => [1, 1, 0],
2104 can_hover => 1, 2116 can_hover => 1,
2105 can_focus => 1, 2117 can_focus => 1,
2118 align => 0,
2106 valign => 0, 2119 valign => 0.5,
2107 can_events => 1, 2120 can_events => 1,
2108 ellipsise => 0, 2121 ellipsise => 0,
2122 padding_x => 4,
2123 padding_y => 2,
2109 #text => ... 2124 #text => ...
2110 #hidden => "*", 2125 #hidden => "*",
2111 @_ 2126 @_
2112 ) 2127 )
2113} 2128}
2160 2175
2161 my $text = $self->get_text; 2176 my $text = $self->get_text;
2162 2177
2163 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2178 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2164 2179
2165 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2166 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2167 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2168 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2169 } elsif ($sym == CFPlus::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2170 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2171 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2172 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2173 } elsif ($sym == CFPlus::SDLK_HOME) { 2188 } elsif ($sym == DC::SDLK_HOME) {
2174 # what a hack 2189 # what a hack
2175 $self->{cursor} = 2190 $self->{cursor} =
2176 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2191 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2177 ? length $1 2192 ? length $1
2178 : 0; 2193 : 0;
2179 } elsif ($sym == CFPlus::SDLK_END) { 2194 } elsif ($sym == DC::SDLK_END) {
2180 # uh, again 2195 # uh, again
2181 $self->{cursor} = 2196 $self->{cursor} =
2182 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2197 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2183 ? $self->{cursor} + length $1 2198 ? $self->{cursor} + length $1
2184 : length $self->{text}; 2199 : length $self->{text};
2223 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; 2238 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2224 $self->{cursor} = length $text; 2239 $self->{cursor} = length $text;
2225 2240
2226 $self->_set_text ($self->{text}); 2241 $self->_set_text ($self->{text});
2227 $self->update; 2242 $self->update;
2228 2243
2229 1 2244 1
2230} 2245}
2231 2246
2232sub invoke_mouse_motion { 2247sub invoke_mouse_motion {
2233 my ($self, $ev, $x, $y) = @_; 2248 my ($self, $ev, $x, $y) = @_;
2248 glColor_premultiply @{$self->{bg}}; 2263 glColor_premultiply @{$self->{bg}};
2249 } 2264 }
2250 2265
2251 glEnable GL_BLEND; 2266 glEnable GL_BLEND;
2252 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2267 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2253 glBegin GL_QUADS;
2254 glVertex 0 , 0;
2255 glVertex 0 , $self->{h};
2256 glVertex $self->{w}, $self->{h}; 2268 glRect 0, 0, $self->{w}, $self->{h};
2257 glVertex $self->{w}, 0;
2258 glEnd;
2259 glDisable GL_BLEND; 2269 glDisable GL_BLEND;
2260 2270
2261 $self->SUPER::_draw; 2271 $self->SUPER::_draw;
2262 2272
2263 #TODO: force update every cursor change :( 2273 #TODO: force update every cursor change :(
2265 2275
2266 unless (exists $self->{cur_h}) { 2276 unless (exists $self->{cur_h}) {
2267 my $text = substr $self->{text}, 0, $self->{cursor}; 2277 my $text = substr $self->{text}, 0, $self->{cursor};
2268 utf8::encode $text; 2278 utf8::encode $text;
2269 2279
2270 @$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);
2271 } 2281 }
2272 2282
2283 glColor_premultiply @{$self->{active_fg}};
2273 glBegin GL_LINES; 2284 glBegin GL_LINES;
2274 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};
2275 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};
2276 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;
2277 } 2302 }
2278} 2303}
2279 2304
2280############################################################################# 2305#############################################################################
2281 2306
2282package CFPlus::UI::Entry; 2307package DC::UI::Entry;
2283 2308
2284our @ISA = CFPlus::UI::EntryBase::; 2309our @ISA = DC::UI::EntryBase::;
2285 2310
2286use 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
2287 2322
2288sub invoke_key_down { 2323sub invoke_key_down {
2289 my ($self, $ev) = @_; 2324 my ($self, $ev) = @_;
2290 2325
2291 my $sym = $ev->{sym}; 2326 my $sym = $ev->{sym};
2297 $self->{history_pointer} = -1; 2332 $self->{history_pointer} = -1;
2298 $self->{history_saveback} = ''; 2333 $self->{history_saveback} = '';
2299 $self->emit (activate => $txt); 2334 $self->emit (activate => $txt);
2300 $self->update; 2335 $self->update;
2301 2336
2302 } elsif ($sym == CFPlus::SDLK_UP) { 2337 } elsif ($sym == DC::SDLK_UP) {
2303 if ($self->{history_pointer} < 0) { 2338 if ($self->{history_pointer} < 0) {
2304 $self->{history_saveback} = $self->get_text; 2339 $self->{history_saveback} = $self->get_text;
2305 } 2340 }
2306 if (@{$self->{history} || []} > 0) { 2341 if (@{$self->{history} || []} > 0) {
2307 $self->{history_pointer}++; 2342 $self->{history_pointer}++;
2309 $self->{history_pointer} = @{$self->{history} || []} - 1; 2344 $self->{history_pointer} = @{$self->{history} || []} - 1;
2310 } 2345 }
2311 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2346 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2312 } 2347 }
2313 2348
2314 } elsif ($sym == CFPlus::SDLK_DOWN) { 2349 } elsif ($sym == DC::SDLK_DOWN) {
2315 $self->{history_pointer}--; 2350 $self->{history_pointer}--;
2316 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2351 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2317 2352
2318 if ($self->{history_pointer} >= 0) { 2353 if ($self->{history_pointer} >= 0) {
2319 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2354 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2320 } else { 2355 } else {
2356 if (defined $self->{history_saveback}) {
2321 $self->set_text ($self->{history_saveback}); 2357 $self->set_text ($self->{history_saveback});
2358 $self->{history_saveback} = undef;
2359 }
2322 } 2360 }
2323 2361
2324 } else { 2362 } else {
2325 return $self->SUPER::invoke_key_down ($ev) 2363 return $self->SUPER::invoke_key_down ($ev)
2326 } 2364 }
2328 1 2366 1
2329} 2367}
2330 2368
2331############################################################################# 2369#############################################################################
2332 2370
2333package CFPlus::UI::TextEdit; 2371package DC::UI::TextEdit;
2334 2372
2335our @ISA = CFPlus::UI::EntryBase::; 2373our @ISA = DC::UI::EntryBase::;
2336 2374
2337use 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}
2338 2386
2339sub move_cursor_ver { 2387sub move_cursor_ver {
2340 my ($self, $dy) = @_; 2388 my ($self, $dy) = @_;
2341 2389
2342 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2390 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2343 2391
2344 $y += $dy; 2392 $line += $dy;
2345 2393
2346 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))) {
2347 $self->{cursor} = $index; 2395 $self->{cursor} = $index;
2348 delete $self->{cur_h}; 2396 delete $self->{cur_h};
2349 $self->update; 2397 $self->update;
2350 return; 2398 return;
2351 } 2399 }
2354sub invoke_key_down { 2402sub invoke_key_down {
2355 my ($self, $ev) = @_; 2403 my ($self, $ev) = @_;
2356 2404
2357 my $sym = $ev->{sym}; 2405 my $sym = $ev->{sym};
2358 2406
2359 if ($sym == CFPlus::SDLK_UP) { 2407 if ($sym == DC::SDLK_UP) {
2360 $self->move_cursor_ver (-1); 2408 $self->move_cursor_ver (-1);
2361 } elsif ($sym == CFPlus::SDLK_DOWN) { 2409 } elsif ($sym == DC::SDLK_DOWN) {
2362 $self->move_cursor_ver (+1); 2410 $self->move_cursor_ver (+1);
2363 } else { 2411 } else {
2364 return $self->SUPER::invoke_key_down ($ev) 2412 return $self->SUPER::invoke_key_down ($ev)
2365 } 2413 }
2366 2414
2367 1 2415 1
2368} 2416}
2369 2417
2370############################################################################# 2418#############################################################################
2371 2419
2372package CFPlus::UI::Button; 2420package DC::UI::ButtonBin;
2373 2421
2374our @ISA = CFPlus::UI::Label::; 2422our @ISA = DC::UI::Bin::;
2375 2423
2376use CFPlus::OpenGL; 2424use DC::OpenGL;
2377 2425
2378my @tex = 2426my @tex =
2379 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2380 qw(b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2381 2429
2382sub new { 2430sub new {
2383 my $class = shift; 2431 my $class = shift;
2384 2432
2385 $class->SUPER::new ( 2433 $class->SUPER::new (
2386 padding_x => 4,
2387 padding_y => 4,
2388 fg => [1, 1, 1],
2389 active_fg => [0, 0, 1],
2390 can_hover => 1, 2434 can_hover => 1,
2391 align => 0, 2435 align => 0.5,
2392 valign => 0, 2436 valign => 0.5,
2393 can_events => 1, 2437 can_events => 1,
2394 @_ 2438 @_
2395 ) 2439 )
2396} 2440}
2397 2441
2406} 2450}
2407 2451
2408sub _draw { 2452sub _draw {
2409 my ($self) = @_; 2453 my ($self) = @_;
2410 2454
2411 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2412
2413 glEnable GL_TEXTURE_2D; 2455 glEnable GL_TEXTURE_2D;
2414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2415 glColor 0, 0, 0, 1; 2457 glColor 0, 0, 0, 1;
2416 2458
2459 my $tex = $tex[$GRAB == $self];
2417 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2460 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2418 2461
2419 glDisable GL_TEXTURE_2D; 2462 glDisable GL_TEXTURE_2D;
2420 2463
2421 $self->SUPER::_draw; 2464 $self->SUPER::_draw;
2422} 2465}
2423 2466
2424############################################################################# 2467#############################################################################
2425 2468
2426package CFPlus::UI::CheckBox; 2469package DC::UI::Button;
2427 2470
2428our @ISA = CFPlus::UI::DrawBG::; 2471our @ISA = DC::UI::Label::;
2472
2473use DC::OpenGL;
2429 2474
2430my @tex = 2475my @tex =
2431 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2432 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2477 qw(b1_button_inactive.png b1_button_active.png);
2433
2434use CFPlus::OpenGL;
2435 2478
2436sub new { 2479sub new {
2437 my $class = shift; 2480 my $class = shift;
2438 2481
2439 $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,
2440 padding_x => 2, 2539 padding_x => 2,
2441 padding_y => 2, 2540 padding_y => 2,
2442 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2443 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2444 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2450} 2549}
2451 2550
2452sub size_request { 2551sub size_request {
2453 my ($self) = @_; 2552 my ($self) = @_;
2454 2553
2455 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2456} 2555}
2457 2556
2458sub toggle { 2557sub toggle {
2459 my ($self) = @_; 2558 my ($self) = @_;
2460 2559
2479sub _draw { 2578sub _draw {
2480 my ($self) = @_; 2579 my ($self) = @_;
2481 2580
2482 $self->SUPER::_draw; 2581 $self->SUPER::_draw;
2483 2582
2484 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2583 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2485 2584
2486 my ($w, $h) = @$self{qw(w h)}; 2585 my ($w, $h) = @$self{qw(w h)};
2487 2586
2488 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;
2489 2588
2496 glDisable GL_TEXTURE_2D; 2595 glDisable GL_TEXTURE_2D;
2497} 2596}
2498 2597
2499############################################################################# 2598#############################################################################
2500 2599
2501package CFPlus::UI::Image; 2600package DC::UI::Image;
2502 2601
2503our @ISA = CFPlus::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2504 2603
2505use CFPlus::OpenGL; 2604use DC::OpenGL;
2506 2605
2507our %texture_cache; 2606our %texture_cache;
2508 2607
2509sub new { 2608sub new {
2510 my $class = shift; 2609 my $class = shift;
2511 2610
2512 my $self = $class->SUPER::new ( 2611 my $self = $class->SUPER::new (
2513 can_events => 0, 2612 can_events => 0,
2613 scale => 1,
2514 @_, 2614 @_,
2515 ); 2615 );
2516 2616
2517 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2518 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2519 2619
2520 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2521 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2522 2622
2523 CFPlus::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2524 2624
2525 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2526 2626
2527 $self 2627 $self
2528} 2628}
2529 2629
2530sub STORABLE_freeze { 2630sub STORABLE_freeze {
2531 my ($self, $cloning) = @_; 2631 my ($self, $cloning) = @_;
2532 2632
2533 $self->{path} 2633 $self->{path}
2534 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";
2535 2635
2536 $self->{path} 2636 $self->{path}
2537} 2637}
2538 2638
2539sub STORABLE_attach { 2639sub STORABLE_attach {
2540 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2541 2641
2542 $self->new (path => $path) 2642 $self->new (path => $path)
2543} 2643}
2544 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2545sub size_request { 2652sub size_request {
2546 my ($self) = @_; 2653 my ($self) = @_;
2547 2654
2548 ($self->{tex}{w}, $self->{tex}{h}) 2655 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2549} 2656}
2550 2657
2551sub _draw { 2658sub _draw {
2552 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2553 2662
2554 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2555 2664
2556 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2557 2666
2563 } 2672 }
2564 2673
2565 glEnable GL_TEXTURE_2D; 2674 glEnable GL_TEXTURE_2D;
2566 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2567 2676
2568 $tex->draw_quad (0, 0, $w, $h); 2677 $tex->draw_quad_alpha (0, 0, $w, $h);
2569 2678
2570 glDisable GL_TEXTURE_2D; 2679 glDisable GL_TEXTURE_2D;
2571} 2680}
2572 2681
2573############################################################################# 2682#############################################################################
2574 2683
2575package CFPlus::UI::ImageButton; 2684package DC::UI::ImageButton;
2576 2685
2577our @ISA = CFPlus::UI::Image::; 2686our @ISA = DC::UI::Image::;
2578 2687
2579use CFPlus::OpenGL; 2688use DC::OpenGL;
2580
2581my %textures;
2582 2689
2583sub new { 2690sub new {
2584 my $class = shift; 2691 my $class = shift;
2585 2692
2586 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2587 padding_x => 4, 2694 padding_x => 4,
2588 padding_y => 4, 2695 padding_y => 4,
2589 fg => [1, 1, 1], 2696 fg => [1, 1, 1],
2590 active_fg => [0, 0, 1], 2697 active_fg => [0, 0, 1],
2591 can_hover => 1, 2698 can_hover => 1,
2592 align => 0, 2699 align => 0.5,
2593 valign => 0, 2700 valign => 0.5,
2594 can_events => 1, 2701 can_events => 1,
2595 @_ 2702 @_
2596 ); 2703 );
2597} 2704}
2598 2705
2706sub invoke_button_down {
2707 my ($self, $ev, $x, $y) = @_;
2708
2709 1
2710}
2711
2599sub invoke_button_up { 2712sub invoke_button_up {
2600 my ($self, $ev, $x, $y) = @_; 2713 my ($self, $ev, $x, $y) = @_;
2601 2714
2602 $self->emit ("activate") 2715 $self->emit ("activate")
2603 if $x >= 0 && $x < $self->{w} 2716 if $x >= 0 && $x < $self->{w}
2606 1 2719 1
2607} 2720}
2608 2721
2609############################################################################# 2722#############################################################################
2610 2723
2611package CFPlus::UI::VGauge; 2724package DC::UI::VGauge;
2612 2725
2613our @ISA = CFPlus::UI::Base::; 2726our @ISA = DC::UI::Base::;
2614 2727
2615use List::Util qw(min max); 2728use List::Util qw(min max);
2616 2729
2617use CFPlus::OpenGL; 2730use DC::OpenGL;
2618 2731
2619my %tex = ( 2732my %tex = (
2620 food => [ 2733 food => [
2621 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2622 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2623 ], 2736 ],
2624 grace => [ 2737 grace => [
2625 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2626 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/
2627 ], 2740 ],
2628 hp => [ 2741 hp => [
2629 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2630 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2631 ], 2744 ],
2632 mana => [ 2745 mana => [
2633 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2634 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/
2635 ], 2748 ],
2636); 2749);
2637 2750
2638# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
2741 glDisable GL_TEXTURE_2D; 2854 glDisable GL_TEXTURE_2D;
2742} 2855}
2743 2856
2744############################################################################# 2857#############################################################################
2745 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
2746package CFPlus::UI::Gauge; 3013package DC::UI::Gauge;
2747 3014
2748our @ISA = CFPlus::UI::VBox::; 3015our @ISA = DC::UI::VBox::;
2749 3016
2750sub new { 3017sub new {
2751 my ($class, %arg) = @_; 3018 my ($class, %arg) = @_;
2752 3019
2753 my $self = $class->SUPER::new ( 3020 my $self = $class->SUPER::new (
2755 can_hover => 1, 3022 can_hover => 1,
2756 can_events => 1, 3023 can_events => 1,
2757 %arg, 3024 %arg,
2758 ); 3025 );
2759 3026
2760 $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");
2761 $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);
2762 $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");
2763 3030
2764 $self 3031 $self
2765} 3032}
2766 3033
2767sub set_fontsize { 3034sub set_fontsize {
2788 $self->{value}->set_text ($val); 3055 $self->{value}->set_text ($val);
2789} 3056}
2790 3057
2791############################################################################# 3058#############################################################################
2792 3059
2793package CFPlus::UI::Slider; 3060package DC::UI::Slider;
2794 3061
2795use strict; 3062use common::sense;
2796 3063
2797use CFPlus::OpenGL; 3064use DC::OpenGL;
2798 3065
2799our @ISA = CFPlus::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
2800 3067
2801my @tex = 3068my @tex =
2802 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
2803 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
2804 3071
2805sub new { 3072sub new {
2806 my $class = shift; 3073 my $class = shift;
2807 3074
2844sub set_value { 3111sub set_value {
2845 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
2846 3113
2847 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2848 3115
2849 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
2850 3117
2851 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
2852
2853 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
2854 $value = $hi - $page if $value > $hi - $page;
2855 3120
2856 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2857 if $unit; 3122 if $unit;
2858 3123
2859 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2874 my ($self, $ev, $x, $y) = @_; 3139 my ($self, $ev, $x, $y) = @_;
2875 3140
2876 $self->SUPER::invoke_button_down ($ev, $x, $y); 3141 $self->SUPER::invoke_button_down ($ev, $x, $y);
2877 3142
2878 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3143 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2879 3144
2880 $self->invoke_mouse_motion ($ev, $x, $y) 3145 $self->invoke_mouse_motion ($ev, $x, $y);
3146
3147 1
2881} 3148}
2882 3149
2883sub invoke_mouse_motion { 3150sub invoke_mouse_motion {
2884 my ($self, $ev, $x, $y) = @_; 3151 my ($self, $ev, $x, $y) = @_;
2885 3152
2886 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
2887 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2888 3155
2889 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
2890 3157
2891 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
2892 3159
2893 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2894 } else { 3161 } else {
2895 return 0; 3162 return 0;
2896 } 3163 }
2901sub invoke_mouse_wheel { 3168sub invoke_mouse_wheel {
2902 my ($self, $ev) = @_; 3169 my ($self, $ev) = @_;
2903 3170
2904 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3171 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2905 3172
2906 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3173 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2907 3174
2908 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3175 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2909 3176
2910 ! ! $delta 3177 1
2911} 3178}
2912 3179
2913sub update { 3180sub update {
2914 my ($self) = @_; 3181 my ($self) = @_;
2915 3182
2921 my ($self) = @_; 3188 my ($self) = @_;
2922 3189
2923 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
2924 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
2925 3192
2926 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2927 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
2928 3195
2929 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};
2930 3197
2931 $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;
2932 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2933 3200
2934 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
2942 3209
2943 glScale $self->{w}, $self->{h}; 3210 glScale $self->{w}, $self->{h};
2944 3211
2945 if ($self->{vertical}) { 3212 if ($self->{vertical}) {
2946 # draw a vertical slider like a rotated horizontal slider 3213 # draw a vertical slider like a rotated horizontal slider
2947 3214
2948 glTranslate 1, 0, 0; 3215 glTranslate 1, 0, 0;
2949 glRotate 90, 0, 0, 1; 3216 glRotate 90, 0, 0, 1;
2950 } 3217 }
2951 3218
2952 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3219 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2964 glDisable GL_TEXTURE_2D; 3231 glDisable GL_TEXTURE_2D;
2965} 3232}
2966 3233
2967############################################################################# 3234#############################################################################
2968 3235
2969package CFPlus::UI::ValSlider; 3236package DC::UI::ValSlider;
2970 3237
2971our @ISA = CFPlus::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
2972 3239
2973sub new { 3240sub new {
2974 my ($class, %arg) = @_; 3241 my ($class, %arg) = @_;
2975 3242
2976 my $range = delete $arg{range}; 3243 my $range = delete $arg{range};
2977 3244
2978 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
2979 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3246 slider => (new DC::UI::Slider expand => 1, range => $range),
2980 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3247 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2981 to_value => sub { shift }, 3248 to_value => sub { shift },
2982 from_value => sub { shift }, 3249 from_value => sub { shift },
2983 %arg, 3250 %arg,
2984 ); 3251 );
2985 3252
3005sub set_range { shift->{slider}->set_range (@_) } 3272sub set_range { shift->{slider}->set_range (@_) }
3006sub set_value { shift->{slider}->set_value (@_) } 3273sub set_value { shift->{slider}->set_value (@_) }
3007 3274
3008############################################################################# 3275#############################################################################
3009 3276
3010package CFPlus::UI::TextScroller; 3277package DC::UI::TextScroller;
3011 3278
3012our @ISA = CFPlus::UI::HBox::; 3279our @ISA = DC::UI::HBox::;
3013 3280
3014use CFPlus::OpenGL; 3281use DC::OpenGL;
3015 3282
3016sub new { 3283sub new {
3017 my $class = shift; 3284 my $class = shift;
3018 3285
3019 my $self = $class->SUPER::new ( 3286 my $self = $class->SUPER::new (
3020 fontsize => 1, 3287 fontsize => 1,
3021 can_events => 1, 3288 can_events => 1,
3022 indent => 0, 3289 indent => 0,
3023 #font => default_font 3290 #font => default_font
3024 @_, 3291 @_,
3025 3292
3026 layout => (new CFPlus::Layout), 3293 layout => (new DC::Layout),
3027 par => [],
3028 max_par => 0, 3294 max_par => 0,
3029 height => 0, 3295 height => 0,
3030 children => [ 3296 children => [
3031 (new CFPlus::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3032 (new CFPlus::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3033 ], 3299 ],
3034 ); 3300 );
3035 3301
3036 $self->{children}[1]->connect (changed => sub { $self->update }); 3302 $self->{children}[1]->connect (changed => sub { $self->update });
3037 3303
3304 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3305
3038 $self 3306 $self
3039} 3307}
3040 3308
3041sub set_fontsize { 3309sub set_fontsize {
3042 my ($self, $fontsize) = @_; 3310 my ($self, $fontsize) = @_;
3046} 3314}
3047 3315
3048sub size_request { 3316sub size_request {
3049 my ($self) = @_; 3317 my ($self) = @_;
3050 3318
3051 my ($empty, $slider) = @{ $self->{children} }; 3319 my ($empty, $slider) = $self->visible_children;
3052 3320
3053 local $self->{children} = [$empty, $slider]; 3321 local $self->{children} = [$empty, $slider];
3054 $self->SUPER::size_request 3322 $self->SUPER::size_request
3055} 3323}
3056 3324
3093 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3361 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3094 $layout->set_markup ($para->{markup}); 3362 $layout->set_markup ($para->{markup});
3095 3363
3096 $layout->set_shapes ( 3364 $layout->set_shapes (
3097 map 3365 map
3098 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3366 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3099 @{$para->{widget}} 3367 @{$para->{widget}}
3100 ); 3368 );
3101 3369
3102 $layout 3370 $layout
3103} 3371}
3220 $ROOT->on_post_alloc ($self => sub { 3488 $ROOT->on_post_alloc ($self => sub {
3221 $self->force_uptodate; 3489 $self->force_uptodate;
3222 3490
3223 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3491 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3224 3492
3225 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3493 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3226 glClearColor 0, 0, 0, 0; 3494 glClearColor 0, 0, 0, 0;
3227 glClear GL_COLOR_BUFFER_BIT; 3495 glClear GL_COLOR_BUFFER_BIT;
3228 3496
3229 package CFPlus::UI::Base; 3497 package DC::UI::Base;
3230 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3498 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3231 (0, 0, $self->{w}, $self->{h}); 3499 (0, 0, $self->{w}, $self->{h});
3232 3500
3233 my $top = int $self->{children}[1]{range}[0]; 3501 my $top = int $self->{children}[1]{range}[0];
3234 3502
3245 3513
3246 if ($y0 < $y + $h && $y < $y1) { 3514 if ($y0 < $y + $h && $y < $y1) {
3247 my $layout = $self->get_layout ($para); 3515 my $layout = $self->get_layout ($para);
3248 3516
3249 $layout->render ($para->{indent}, $y - $y0); 3517 $layout->render ($para->{indent}, $y - $y0);
3518 $layout->draw;
3250 3519
3251 if (my @w = @{ $para->{widget} }) { 3520 if (my @w = @{ $para->{widget} }) {
3252 my @s = $layout->get_shapes; 3521 my @s = $layout->get_shapes;
3253 3522
3254 for (@w) { 3523 for (@w) {
3292 $self->{children}[1]->draw; 3561 $self->{children}[1]->draw;
3293} 3562}
3294 3563
3295############################################################################# 3564#############################################################################
3296 3565
3297package CFPlus::UI::Animator; 3566package DC::UI::Animator;
3298 3567
3299use CFPlus::OpenGL; 3568use DC::OpenGL;
3300 3569
3301our @ISA = CFPlus::UI::Bin::; 3570our @ISA = DC::UI::Bin::;
3302 3571
3303sub moveto { 3572sub moveto {
3304 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3305 3574
3306 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3307 $self->{speed} = 0.001; 3576 $self->{speed} = 0.001;
3308 $self->{time} = 1; 3577 $self->{time} = 1;
3309 3578
3310 ::animation_start $self; 3579 ::animation_start $self;
3311} 3580}
3312 3581
3313sub animate { 3582sub animate {
3314 my ($self, $interval) = @_; 3583 my ($self, $interval) = @_;
3318 $self->{time} = 0; 3587 $self->{time} = 0;
3319 ::animation_stop $self; 3588 ::animation_stop $self;
3320 } 3589 }
3321 3590
3322 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; 3591 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3323 3592
3324 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); 3593 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3325 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); 3594 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3326} 3595}
3327 3596
3328sub _draw { 3597sub _draw {
3334 glPopMatrix; 3603 glPopMatrix;
3335} 3604}
3336 3605
3337############################################################################# 3606#############################################################################
3338 3607
3339package CFPlus::UI::Flopper; 3608package DC::UI::Flopper;
3340 3609
3341our @ISA = CFPlus::UI::Button::; 3610our @ISA = DC::UI::Button::;
3342 3611
3343sub new { 3612sub new {
3344 my $class = shift; 3613 my $class = shift;
3345 3614
3346 my $self = $class->SUPER::new ( 3615 my $self = $class->SUPER::new (
3358 $self->{other}->toggle_visibility; 3627 $self->{other}->toggle_visibility;
3359} 3628}
3360 3629
3361############################################################################# 3630#############################################################################
3362 3631
3363package CFPlus::UI::Tooltip; 3632package DC::UI::Tooltip;
3364 3633
3365our @ISA = CFPlus::UI::Bin::; 3634our @ISA = DC::UI::Bin::;
3366 3635
3367use CFPlus::OpenGL; 3636use DC::OpenGL;
3368 3637
3369sub new { 3638sub new {
3370 my $class = shift; 3639 my $class = shift;
3371 3640
3372 $class->SUPER::new ( 3641 $class->SUPER::new (
3373 @_, 3642 @_,
3374 can_events => 0, 3643 can_events => 0,
3375 ) 3644 )
3376} 3645}
3377 3646
3647# expand, as good as possible
3648sub _expand_doclets {
3649 my ($tip) = @_;
3650
3651 $tip =~ s{#\(([^)]+)\)}{
3652 if ($::CONN) {
3653 exists $::CONN->{doclet}{$1}
3654 ? $::CONN->{doclet}{$1}
3655 : "(waiting for server to show full text)"
3656 } else {
3657 "(unable to show full text without server connection)"
3658 }
3659 }ge;
3660
3661 $tip =~ s/^\n+//;
3662 $tip =~ s/\n+$//;
3663
3664 $tip
3665}
3666
3667# expands a tooltip, potentially multiple times remotely
3668# and returns a guard. clals the clalback each time the text changes.
3669sub expand_tooltip {
3670 my ($tip, $cb) = @_;
3671
3672 # first expand #name tooltips from local pod
3673 $tip = DC::Pod::section_label tooltip => $1
3674 if $tip =~ /^#([^(].*)$/;
3675
3676 my $active; # true if any remote requests outstanding
3677
3678 if ($::CONN && $::CONN->{addme_success}) {
3679 # now find all doclet references
3680 for my $doclet ($tip =~ /#\(([^)]+)\)/g) {
3681 unless (exists $::CONN->{doclet}{$doclet}) {
3682 # need to ask the server
3683 # we don't try to avoid duplicate requests
3684
3685 $active = 1;
3686 $::CONN->send_exti_req (doclet => (split /\//, $doclet, 2), sub {
3687 $::CONN->{doclet}{$doclet} = DC::sanitise_cfxml $_[0];
3688 $cb->(_expand_doclets $tip) if $active;
3689 });
3690 }
3691 }
3692 }
3693
3694 $cb->(_expand_doclets $tip);
3695
3696 $active and Guard::guard { undef $active }
3697}
3698
3378sub set_tooltip_from { 3699sub set_tooltip_from {
3379 my ($self, $widget) = @_; 3700 my ($self, $widget) = @_;
3380 3701
3381 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3382 if $widget->{tooltip} =~ /^#(.*)$/;
3383
3384 my $tooltip = $widget->{tooltip}; 3702 my $tip = $widget->{tooltip};
3703 $tip = $tip->($widget) if "CODE" eq ref $tip;
3385 3704
3386 if ($ENV{CFPLUS_DEBUG} & 2) {
3387 $tooltip .= "\n\n" . (ref $widget) . "\n"
3388 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3389 . "req $widget->{req_w} $widget->{req_h}\n"
3390 . "visible $widget->{visible}";
3391 }
3392
3393 $tooltip =~ s/^\n+//;
3394 $tooltip =~ s/\n+$//;
3395
3396 $self->add (new CFPlus::UI::Label 3705 $self->add (new DC::UI::Label
3397 markup => $tooltip, 3706 fg => $DC::THEME{tooltip_fg},
3398 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3707 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3708 align => 0,
3399 fontsize => 0.8, 3709 fontsize => 0.8,
3400 style => 1, # FLAG_INVERSE 3710 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3401 ellipsise => 0, 3711 ellipsise => 0,
3402 font => ($widget->{tooltip_font} || $::FONT_PROP), 3712 font => ($widget->{tooltip_font} || $::FONT_PROP),
3403 ); 3713 );
3714
3715 $self->{tooltip_expand} = expand_tooltip $tip, sub {
3716 my ($tip) = @_;
3717
3718 if ($ENV{CFPLUS_DEBUG} & 2) {
3719 $tip .= "\n\n" . (ref $widget) . "\n"
3720 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3721 . "req $widget->{req_w} $widget->{req_h}\n"
3722 . "visible $widget->{visible}";
3723 }
3724
3725 $self->{children}[0]->set_markup ($tip);
3726 };
3404} 3727}
3405 3728
3406sub size_request { 3729sub size_request {
3407 my ($self) = @_; 3730 my ($self) = @_;
3408 3731
3440} 3763}
3441 3764
3442sub _draw { 3765sub _draw {
3443 my ($self) = @_; 3766 my ($self) = @_;
3444 3767
3445 glTranslate 0.375, 0.375;
3446
3447 my ($w, $h) = @$self{qw(w h)}; 3768 my ($w, $h) = @$self{qw(w h)};
3448 3769
3449 glColor 1, 0.8, 0.4; 3770 glColor @{ $DC::THEME{tooltip_bg} };
3450 glBegin GL_QUADS; 3771 glRect 0, 0, $w, $h;
3451 glVertex 0 , 0; 3772
3452 glVertex 0 , $h; 3773 glColor @{ $DC::THEME{tooltip_border} };
3453 glVertex $w, $h; 3774 glRect_lineloop .5, .5, $w + .5, $h + .5;
3454 glVertex $w, 0; 3775
3455 glEnd; 3776 glTranslate 2, 2;
3456
3457 glColor 0, 0, 0;
3458 glBegin GL_LINE_LOOP;
3459 glVertex 0 , 0;
3460 glVertex 0 , $h;
3461 glVertex $w, $h;
3462 glVertex $w, 0;
3463 glEnd;
3464
3465 glTranslate 2 - 0.375, 2 - 0.375;
3466 3777
3467 $self->SUPER::_draw; 3778 $self->SUPER::_draw;
3468} 3779}
3469 3780
3470############################################################################# 3781#############################################################################
3471 3782
3472package CFPlus::UI::Face; 3783package DC::UI::Face;
3473 3784
3474our @ISA = CFPlus::UI::DrawBG::; 3785our @ISA = DC::UI::DrawBG::;
3475 3786
3476use CFPlus::OpenGL; 3787use DC::OpenGL;
3477 3788
3478sub new { 3789sub new {
3479 my $class = shift; 3790 my $class = shift;
3480 3791
3481 my $self = $class->SUPER::new ( 3792 my $self = $class->SUPER::new (
3484 aspect => 1, 3795 aspect => 1,
3485 can_events => 0, 3796 can_events => 0,
3486 @_, 3797 @_,
3487 ); 3798 );
3488 3799
3489 if ($self->{anim} && $self->{animspeed}) {
3490 CFPlus::weaken (my $widget = $self);
3491
3492 $self->{timer} = Event->timer (
3493 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3494 hard => 1,
3495 interval => $self->{animspeed},
3496 cb => sub {
3497 return unless $::CONN;
3498
3499 ++$widget->{frame};
3500 $self->update_face;
3501 $self->update;
3502 },
3503 );
3504
3505 $self->update_face; 3800 $self->update_anim;
3506 } 3801
3507
3508 $self 3802 $self
3509} 3803}
3510 3804
3805sub update_timer {
3806 my ($self) = @_;
3807
3808 return unless $self->{timer};
3809
3810 if ($self->{visible}) {
3811 $self->{timer}->start;
3812 } else {
3813 $self->{timer}->stop;
3814 }
3815}
3816
3511sub update_face { 3817sub update_face {
3512 my ($self) = @_; 3818 my ($self) = @_;
3513 3819
3514 return unless $::CONN;
3515
3516 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3517 if ($anim && @$anim) {
3518 delete $self->{wait_face};
3519 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3520 }
3521 }
3522}
3523
3524sub size_request {
3525 my ($self) = @_;
3526
3527 if ($::CONN) { 3820 if ($::CONN) {
3528 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3821 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3529 if (my $tex = $::CONN->{texture}[$faceid]) { 3822 if ($anim && @$anim) {
3530 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3823 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3531 } else { 3824 delete $self->{face_change_cb};
3532 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3825
3533 $self->realloc; 3826 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3827 unless ($tex->{name} || $tex->{loading}) {
3828 $tex->upload (sub { $self->reconfigure });
3829 }
3534 }); 3830 }
3535 } 3831 }
3536 } 3832 }
3537 } 3833 }
3834}
3835
3836sub update_anim {
3837 my ($self) = @_;
3838
3839 if ($self->{anim} && $self->{animspeed}) {
3840 DC::weaken (my $widget = $self);
3841
3842 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3843 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3844 return unless $::CONN;
3845
3846 my $w = $widget
3847 or return;
3848
3849 ++$w->{frame};
3850 $w->update_face;
3851
3852 # somehow, $widget can go away
3853 $w->update;
3854 $w->update_timer;
3855 };
3856
3857 $self->update_face;
3858 $self->update_timer;
3859 } else {
3860 delete $self->{timer};
3861 }
3862}
3863
3864sub size_request {
3865 my ($self) = @_;
3866
3867 if ($::CONN) {
3868 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3869 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3870 if ($tex->{name}) {
3871 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3872 } elsif (!$tex->{loading}) {
3873 $tex->upload (sub { $self->reconfigure });
3874 }
3875 }
3876
3877 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3878 }
3879 }
3538 3880
3539 ($self->{size_w} || 8, $self->{size_h} || 8) 3881 ($self->{size_w} || 8, $self->{size_h} || 8)
3540} 3882}
3541 3883
3542sub update { 3884sub update {
3545 return unless $self->{visible}; 3887 return unless $self->{visible};
3546 3888
3547 $self->SUPER::update; 3889 $self->SUPER::update;
3548} 3890}
3549 3891
3892sub set_face {
3893 my ($self, $face) = @_;
3894
3895 $self->{face} = $face;
3896 $self->reconfigure;
3897}
3898
3899sub set_anim {
3900 my ($self, $anim) = @_;
3901
3902 $self->{anim} = $anim;
3903 $self->update_anim;
3904}
3905
3906sub set_animspeed {
3907 my ($self, $animspeed) = @_;
3908
3909 $self->{animspeed} = $animspeed;
3910 $self->update_anim;
3911}
3912
3913sub invoke_visibility_change {
3914 my ($self) = @_;
3915
3916 $self->update_timer;
3917
3918 0
3919}
3920
3550sub _draw { 3921sub _draw {
3551 my ($self) = @_; 3922 my ($self) = @_;
3552 3923
3553 return unless $::CONN;
3554
3555 $self->SUPER::_draw; 3924 $self->SUPER::_draw;
3556 3925
3557 my $faceid = $::CONN->{faceid}[$self->{face}] 3926 if (my $tex = $self->{tex}) {
3558 or return;
3559
3560 my $tex = $::CONN->{texture}[$faceid];
3561
3562 if ($tex) {
3563 glEnable GL_TEXTURE_2D; 3927 glEnable GL_TEXTURE_2D;
3564 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3928 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3565 glColor 0, 0, 0, 1; 3929 glColor 0, 0, 0, 1;
3566 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3930 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3567 glDisable GL_TEXTURE_2D; 3931 glDisable GL_TEXTURE_2D;
3569} 3933}
3570 3934
3571sub destroy { 3935sub destroy {
3572 my ($self) = @_; 3936 my ($self) = @_;
3573 3937
3574 $self->{timer}->cancel 3938 (delete $self->{timer})->cancel
3575 if $self->{timer}; 3939 if $self->{timer};
3576 3940
3577 $self->SUPER::destroy; 3941 $self->SUPER::destroy;
3578} 3942}
3579 3943
3580############################################################################# 3944#############################################################################
3581 3945
3582package CFPlus::UI::Buttonbar; 3946package DC::UI::Buttonbar;
3583 3947
3584our @ISA = CFPlus::UI::HBox::; 3948our @ISA = DC::UI::HBox::;
3585 3949
3586# TODO: should actually wrap buttons and other goodies. 3950# TODO: should actually wrap buttons and other goodies.
3587 3951
3588############################################################################# 3952#############################################################################
3589 3953
3590package CFPlus::UI::Menu; 3954package DC::UI::Menu;
3591 3955
3592our @ISA = CFPlus::UI::Toplevel::; 3956our @ISA = DC::UI::Toplevel::;
3593 3957
3594use CFPlus::OpenGL; 3958use DC::OpenGL;
3595 3959
3596sub new { 3960sub new {
3597 my $class = shift; 3961 my $class = shift;
3598 3962
3599 my $self = $class->SUPER::new ( 3963 my $self = $class->SUPER::new (
3600 items => [], 3964 items => [],
3601 z => 100, 3965 z => 100,
3602 @_, 3966 @_,
3603 ); 3967 );
3604 3968
3605 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3969 $self->add ($self->{vbox} = new DC::UI::VBox);
3606 3970
3607 for my $item (@{ $self->{items} }) { 3971 for my $item (@{ $self->{items} }) {
3608 my ($widget, $cb, $tooltip) = @$item; 3972 my ($widget, $cb, $tooltip) = @$item;
3609 3973
3610 # handle various types of items, only text for now 3974 # handle various types of items, only text for now
3611 if (!ref $widget) { 3975 if (!ref $widget) {
3612 if ($widget =~ /\t/) { 3976 if ($widget =~ /\t/) {
3613 my ($left, $right) = split /\t/, $widget, 2; 3977 my ($left, $right) = split /\t/, $widget, 2;
3614 3978
3615 $widget = new CFPlus::UI::HBox 3979 $widget = new DC::UI::HBox
3616 can_hover => 1, 3980 can_hover => 1,
3617 can_events => 1, 3981 can_events => 1,
3618 tooltip => $tooltip, 3982 tooltip => $tooltip,
3619 children => [ 3983 children => [
3620 (new CFPlus::UI::Label markup => $left, expand => 1), 3984 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3621 (new CFPlus::UI::Label markup => $right, align => +1), 3985 (new DC::UI::Label markup => $right, align => 1),
3622 ], 3986 ],
3623 ; 3987 ;
3624 3988
3625 } else { 3989 } else {
3626 $widget = new CFPlus::UI::Label 3990 $widget = new DC::UI::Label
3627 can_hover => 1, 3991 can_hover => 1,
3628 can_events => 1, 3992 can_events => 1,
3993 align => 0,
3629 markup => $widget, 3994 markup => $widget,
3630 tooltip => $tooltip; 3995 tooltip => $tooltip;
3631 } 3996 }
3632 } 3997 }
3633 3998
3648 # maybe save $GRAB? must be careful about events... 4013 # maybe save $GRAB? must be careful about events...
3649 $GRAB = $self; 4014 $GRAB = $self;
3650 $self->{button} = $ev->{button}; 4015 $self->{button} = $ev->{button};
3651 4016
3652 $self->show; 4017 $self->show;
3653 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 4018
4019 my $x = $ev->{x};
4020 my $y = $ev->{y};
4021
4022 $self->{root}->on_post_alloc ($self => sub {
4023 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
4024 });
4025
4026 1 # so it can be used inside event handlers
3654} 4027}
3655 4028
3656sub invoke_mouse_motion { 4029sub invoke_mouse_motion {
3657 my ($self, $ev, $x, $y) = @_; 4030 my ($self, $ev, $x, $y) = @_;
3658 4031
3679 1 4052 1
3680} 4053}
3681 4054
3682############################################################################# 4055#############################################################################
3683 4056
3684package CFPlus::UI::Multiplexer; 4057package DC::UI::Multiplexer;
3685 4058
3686our @ISA = CFPlus::UI::Container::; 4059our @ISA = DC::UI::Container::;
3687 4060
3688sub new { 4061sub new {
3689 my $class = shift; 4062 my $class = shift;
3690 4063
3691 my $self = $class->SUPER::new ( 4064 my $self = $class->SUPER::new (
3692 @_, 4065 @_,
3693 ); 4066 );
3694 4067
3695 $self->{current} = $self->{children}[0] 4068 $self->set_current_page (0);
3696 if @{ $self->{children} };
3697 4069
3698 $self 4070 $self
3699} 4071}
3700 4072
3701sub add { 4073sub add {
3702 my ($self, @widgets) = @_; 4074 my ($self, @widgets) = @_;
3703 4075
3704 $self->SUPER::add (@widgets); 4076 $self->SUPER::add (@widgets);
3705 4077
3706 $self->{current} = $self->{children}[0] 4078 $self->set_current_page (0)
3707 if @{ $self->{children} }; 4079 if @widgets == @{ $self->{children} };
3708} 4080}
3709 4081
3710sub get_current_page { 4082sub get_current_page {
3711 my ($self) = @_; 4083 my ($self) = @_;
3712 4084
3718 4090
3719 my $widget = ref $page_or_widget 4091 my $widget = ref $page_or_widget
3720 ? $page_or_widget 4092 ? $page_or_widget
3721 : $self->{children}[$page_or_widget]; 4093 : $self->{children}[$page_or_widget];
3722 4094
4095 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4096
3723 $self->{current} = $widget; 4097 if (($self->{current} = $widget)) {
4098 $self->{current}->set_visible if $self->{current} && $self->{visible};
3724 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4099 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3725 4100
3726 $self->emit (page_changed => $self->{current}); 4101 $self->emit (page_changed => $self->{current});
4102 }
3727 4103
3728 $self->realloc; 4104 $self->realloc;
3729} 4105}
3730 4106
3731sub visible_children { 4107sub visible_children {
3732 $_[0]{current} 4108 $_[0]{current} || ()
3733} 4109}
3734 4110
3735sub size_request { 4111sub size_request {
3736 my ($self) = @_; 4112 my ($self) = @_;
3737 4113
4114 $self->{current}
3738 $self->{current}->size_request 4115 ? $self->{current}->size_request
4116 : (0, 0)
3739} 4117}
3740 4118
3741sub invoke_size_allocate { 4119sub invoke_size_allocate {
3742 my ($self, $w, $h) = @_; 4120 my ($self, $w, $h) = @_;
3743 4121
3744 $self->{current}->configure (0, 0, $w, $h); 4122 $self->{current}->configure (0, 0, $w, $h)
4123 if $self->{current};
3745 4124
3746 1 4125 1
3747} 4126}
3748 4127
3749sub _draw { 4128sub _draw {
3750 my ($self) = @_; 4129 my ($self) = @_;
3751 4130
3752 $self->{current}->draw; 4131 $self->{current}->draw
4132 if $self->{current};
3753} 4133}
3754 4134
3755############################################################################# 4135#############################################################################
3756 4136
3757package CFPlus::UI::Notebook; 4137package DC::UI::Notebook;
3758 4138
4139use DC::OpenGL;
4140
3759our @ISA = CFPlus::UI::VBox::; 4141our @ISA = DC::UI::VBox::;
3760 4142
3761sub new { 4143sub new {
3762 my $class = shift; 4144 my $class = shift;
3763 4145
3764 my $self = $class->SUPER::new ( 4146 my $self = $class->SUPER::new (
3765 buttonbar => (new CFPlus::UI::Buttonbar), 4147 buttonbar => (new DC::UI::Buttonbar),
3766 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4148 multiplexer => (new DC::UI::Multiplexer expand => 1),
4149 active_outline => [.7, .7, 0.2],
3767 # filter => # will be put between multiplexer and $self 4150 # filter => # will be put between multiplexer and $self
3768 @_, 4151 @_,
3769 ); 4152 );
3770 4153
3771 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4154 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3772 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4155 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3773 4156
4157 {
4158 Scalar::Util::weaken (my $wself = $self);
4159
4160 $self->{multiplexer}->connect (c_add => sub {
4161 my ($mplex, $widgets) = @_;
4162
4163 for my $child (@$widgets) {
4164 Scalar::Util::weaken $child;
4165 $child->{c_tab_} ||= do {
4166 my $tab =
4167 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4168 ? $child->{c_tab}
4169 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4170
4171 $tab->connect (activate => sub {
4172 $wself->set_current_page ($child);
4173 });
4174
4175 $tab
4176 };
4177
4178 $self->{buttonbar}->add ($child->{c_tab_});
4179 }
4180 });
4181
4182 $self->{multiplexer}->connect (c_remove => sub {
4183 my ($mplex, $widgets) = @_;
4184
4185 for my $child (@$widgets) {
4186 $wself->{buttonbar}->remove ($child->{c_tab_});
4187 }
4188 });
4189 }
4190
3774 $self 4191 $self
3775} 4192}
3776 4193
3777sub add { 4194sub add {
4195 my ($self, @widgets) = @_;
4196
4197 $self->{multiplexer}->add (@widgets)
4198}
4199
4200sub remove {
4201 my ($self, @widgets) = @_;
4202
4203 $self->{multiplexer}->remove (@widgets)
4204}
4205
4206sub pages {
4207 my ($self) = @_;
4208 $self->{multiplexer}->children
4209}
4210
4211sub page_index {
4212 my ($self, $widget) = @_;
4213
4214 my $i = 0;
4215 for ($self->pages) {
4216 if ($_ eq $widget) { return $i };
4217 $i++;
4218 }
4219
4220 undef
4221}
4222
4223sub add_tab {
3778 my ($self, $title, $widget, $tooltip) = @_; 4224 my ($self, $title, $widget, $tooltip) = @_;
3779 4225
3780 CFPlus::weaken $self; 4226 $title = [$title, $tooltip] unless ref $title;
4227 $widget->{c_tab} = $title;
3781 4228
3782 $self->{buttonbar}->add (new CFPlus::UI::Button
3783 markup => $title,
3784 tooltip => $tooltip,
3785 on_activate => sub { $self->set_current_page ($widget) },
3786 );
3787
3788 $self->{multiplexer}->add ($widget); 4229 $self->add ($widget);
3789} 4230}
3790 4231
3791sub get_current_page { 4232sub get_current_page {
3792 my ($self) = @_; 4233 my ($self) = @_;
3793 4234
3799 4240
3800 $self->{multiplexer}->set_current_page ($page); 4241 $self->{multiplexer}->set_current_page ($page);
3801 $self->emit (page_changed => $self->{multiplexer}{current}); 4242 $self->emit (page_changed => $self->{multiplexer}{current});
3802} 4243}
3803 4244
4245sub _draw {
4246 my ($self) = @_;
4247
4248 $self->SUPER::_draw ();
4249
4250 if (my $cur = $self->{multiplexer}{current}) {
4251 if ($cur = $cur->{c_tab_}) {
4252 glTranslate $self->{buttonbar}{x} + $cur->{x},
4253 $self->{buttonbar}{y} + $cur->{y};
4254 glLineWidth 3;
4255 #glEnable GL_BLEND;
4256 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4257 glColor @{$self->{active_outline}};
4258 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4259 glLineWidth 1;
4260 #glDisable GL_BLEND;
4261 }
4262 }
4263}
4264
3804############################################################################# 4265#############################################################################
3805 4266
3806package CFPlus::UI::Selector; 4267package DC::UI::Selector;
3807 4268
3808use utf8; 4269use utf8;
3809 4270
3810our @ISA = CFPlus::UI::Button::; 4271our @ISA = DC::UI::Button::;
3811 4272
3812sub new { 4273sub new {
3813 my $class = shift; 4274 my $class = shift;
3814 4275
3815 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
3832 my ($value, $title, $tooltip) = @$_; 4293 my ($value, $title, $tooltip) = @$_;
3833 4294
3834 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4295 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3835 } 4296 }
3836 4297
3837 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4298 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3838} 4299}
3839 4300
3840sub _set_value { 4301sub _set_value {
3841 my ($self, $value) = @_; 4302 my ($self, $value) = @_;
3842 4303
3843 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4304 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4305 $item ||= $self->{options}[0]
3844 or return; 4306 or return;
3845 4307
3846 $self->{value} = $item->[0]; 4308 $self->{value} = $item->[0];
3847 $self->set_markup ("$item->[1] ⇓"); 4309 $self->set_markup ("$item->[1] ⇓");
3848 $self->set_tooltip ($item->[2]); 4310# $self->set_tooltip ($item->[2]);
3849} 4311}
3850 4312
3851sub set_value { 4313sub set_value {
3852 my ($self, $value) = @_; 4314 my ($self, $value) = @_;
3853 4315
3855 4317
3856 $self->_set_value ($value); 4318 $self->_set_value ($value);
3857 $self->emit (changed => $value); 4319 $self->emit (changed => $value);
3858} 4320}
3859 4321
4322sub set_options {
4323 my ($self, $options) = @_;
4324
4325 $self->{options} = $options;
4326 $self->_set_value ($self->{value});
4327}
4328
3860############################################################################# 4329#############################################################################
3861 4330
3862package CFPlus::UI::Statusbox; 4331package DC::UI::Statusbox;
3863 4332
3864our @ISA = CFPlus::UI::VBox::; 4333our @ISA = DC::UI::VBox::;
3865 4334
3866sub new { 4335sub new {
3867 my $class = shift; 4336 my $class = shift;
3868 4337
3869 my $self = $class->SUPER::new ( 4338 my $self = $class->SUPER::new (
3870 fontsize => 0.8, 4339 fontsize => 0.8,
3871 @_, 4340 @_,
3872 ); 4341 );
3873 4342
3874 CFPlus::weaken (my $this = $self); 4343 DC::weaken (my $this = $self);
3875 4344
3876 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4345 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3877 4346
3878 $self 4347 $self
3879} 4348}
3880 4349
3881sub reorder { 4350sub reorder {
3882 my ($self) = @_; 4351 my ($self) = @_;
3883 my $NOW = Time::HiRes::time; 4352 my $NOW = AE::time;
3884 4353
3885 # freeze display when hovering over any label 4354 # freeze display when hovering over any label
3886 return if $CFPlus::UI::TOOLTIP->{owner} 4355 return if $DC::UI::TOOLTIP->{owner}
3887 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4356 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3888 values %{ $self->{item} }; 4357 values %{ $self->{item} };
3889 4358
3890 while (my ($k, $v) = each %{ $self->{item} }) { 4359 while (my ($k, $v) = each %{ $self->{item} }) {
3891 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4360 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3892 } 4361 }
4362
4363 $self->{timer}->set (1, 1);
3893 4364
3894 my @widgets; 4365 my @widgets;
3895 4366
3896 my @items = sort { 4367 my @items = sort {
3897 $a->{pri} <=> $b->{pri} 4368 $a->{pri} <=> $b->{pri}
3898 or $b->{id} <=> $a->{id} 4369 or $b->{id} <=> $a->{id}
3899 } values %{ $self->{item} }; 4370 } values %{ $self->{item} };
3900
3901 $self->{timer}->interval (1);
3902 4371
3903 my $count = 10 + 1; 4372 my $count = 10 + 1;
3904 for my $item (@items) { 4373 for my $item (@items) {
3905 last unless --$count; 4374 last unless --$count;
3906 4375
3913 for ($short) { 4382 for ($short) {
3914 s/^\s+//; 4383 s/^\s+//;
3915 s/\s+/ /g; 4384 s/\s+/ /g;
3916 } 4385 }
3917 4386
3918 new CFPlus::UI::Label 4387 new DC::UI::Label
3919 markup => $short, 4388 markup => $short,
3920 tooltip => $item->{tooltip}, 4389 tooltip => $item->{tooltip},
3921 tooltip_font => $::FONT_PROP, 4390 tooltip_font => $::FONT_PROP,
3922 tooltip_width => 0.67, 4391 tooltip_width => 0.67,
3923 fontsize => $item->{fontsize} || $self->{fontsize}, 4392 fontsize => $item->{fontsize} || $self->{fontsize},
3924 max_w => $::WIDTH * 0.44, 4393 max_w => $::WIDTH * 0.44,
4394 align => 0,
3925 fg => [@{ $item->{fg} }], 4395 fg => [@{ $item->{fg} }],
3926 can_events => 1, 4396 can_events => 1,
3927 can_hover => 1 4397 can_hover => 1
3928 }; 4398 };
3929 4399
3930 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4400 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3931 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4401 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3932 $label->update; 4402 $label->update;
3933 $label->set_max_size (undef, $label->{req_h} * $diff) 4403 $label->set_max_size (undef, $label->{req_h} * $diff)
3934 if $diff < 1; 4404 if $diff < 1;
3935 $self->{timer}->interval (1/30); 4405 $self->{timer}->set (1/30, 1/30);
3936 } else { 4406 } else {
3937 $label->{fg}[3] = $item->{fg}[3] || 1; 4407 $label->{fg}[3] = $item->{fg}[3] || 1;
3938 } 4408 }
3939 4409
3940 push @widgets, $label; 4410 push @widgets, $label;
3941 } 4411 }
4412
4413 my $hash = join ",", @widgets;
4414 return if $hash eq $self->{last_widget_hash};
4415 $self->{last_widget_hash} = $hash;
3942 4416
3943 $self->clear; 4417 $self->clear;
3944 $self->SUPER::add (reverse @widgets); 4418 $self->SUPER::add (reverse @widgets);
3945} 4419}
3946 4420
3982 $ROOT->on_refresh (reorder => sub { 4456 $ROOT->on_refresh (reorder => sub {
3983 $self->reorder; 4457 $self->reorder;
3984 }); 4458 });
3985} 4459}
3986 4460
4461sub clr_group {
4462 my ($self, $group) = @_;
4463
4464 if (delete $self->{item}{$group}) {
4465 $ROOT->on_refresh (reorder => sub {
4466 $self->reorder;
4467 });
4468 }
4469}
4470
3987sub reconfigure { 4471sub reconfigure {
3988 my ($self) = @_; 4472 my ($self) = @_;
3989 4473
3990 delete $_->{label} 4474 delete $_->{label}
3991 for values %{ $self->{item} || {} }; 4475 for values %{ $self->{item} || {} };
4002 $self->SUPER::destroy; 4486 $self->SUPER::destroy;
4003} 4487}
4004 4488
4005############################################################################# 4489#############################################################################
4006 4490
4007package CFPlus::UI::Root; 4491package DC::UI::Root;
4008 4492
4009our @ISA = CFPlus::UI::Container::; 4493our @ISA = DC::UI::Container::;
4010 4494
4011use List::Util qw(min max); 4495use List::Util qw(min max);
4012 4496
4013use CFPlus::OpenGL; 4497use DC::OpenGL;
4014 4498
4015sub new { 4499sub new {
4016 my $class = shift; 4500 my $class = shift;
4017 4501
4018 my $self = $class->SUPER::new ( 4502 my $self = $class->SUPER::new (
4019 visible => 1, 4503 visible => 1,
4020 @_, 4504 @_,
4021 ); 4505 );
4022 4506
4023 CFPlus::weaken ($self->{root} = $self); 4507 DC::weaken ($self->{root} = $self);
4024 4508
4025 $self 4509 $self
4026} 4510}
4027 4511
4028sub size_request { 4512sub size_request {
4076} 4560}
4077 4561
4078sub update { 4562sub update {
4079 my ($self) = @_; 4563 my ($self) = @_;
4080 4564
4081 $::WANT_REFRESH++; 4565 $::WANT_REFRESH = 1;
4082} 4566}
4083 4567
4084sub add { 4568sub add {
4085 my ($self, @children) = @_; 4569 my ($self, @children) = @_;
4086 4570
4123 while ($self->{refresh_hook}) { 4607 while ($self->{refresh_hook}) {
4124 $_->() 4608 $_->()
4125 for values %{delete $self->{refresh_hook}}; 4609 for values %{delete $self->{refresh_hook}};
4126 } 4610 }
4127 4611
4128 if ($self->{realloc}) { 4612 while ($self->{realloc}) {
4129 my %queue; 4613 my %queue;
4130 my @queue; 4614 my @queue;
4131 my $widget; 4615 my $widget;
4132 4616
4133 outer: 4617 outer:
4145 while () { 4629 while () {
4146 @queue or last outer; 4630 @queue or last outer;
4147 4631
4148 $widget = pop @{ $queue[-1] || [] } 4632 $widget = pop @{ $queue[-1] || [] }
4149 and last; 4633 and last;
4150 4634
4151 pop @queue; 4635 pop @queue;
4152 } 4636 }
4153 4637
4154 delete $queue{$widget+0}; 4638 delete $queue{$widget+0};
4155 4639
4156 my ($w, $h) = $widget->size_request; 4640 my ($w, $h) = $widget->size_request;
4157 4641
4158 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4642 $w += $widget->{padding_x} * 2;
4159 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4643 $h += $widget->{padding_y} * 2;
4644
4645 $w = max $widget->{min_w}, $w;
4646 $h = max $widget->{min_h}, $h;
4160 4647
4161 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4648 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4162 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4649 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4163 4650
4164 $w = $widget->{force_w} if exists $widget->{force_w}; 4651 $w = $widget->{force_w} if exists $widget->{force_w};
4180 } 4667 }
4181 } 4668 }
4182 4669
4183 delete $self->{realloc}{$widget+0}; 4670 delete $self->{realloc}{$widget+0};
4184 } 4671 }
4185 }
4186 4672
4187 while (my $size_alloc = delete $self->{size_alloc}) { 4673 while (my $size_alloc = delete $self->{size_alloc}) {
4188 my @queue = sort { $b->{visible} <=> $a->{visible} } 4674 my @queue = sort { $a->{visible} <=> $b->{visible} }
4189 values %$size_alloc; 4675 values %$size_alloc;
4190 4676
4191 while () { 4677 while () {
4192 my $widget = pop @queue || last; 4678 my $widget = pop @queue || last;
4193 4679
4194 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4680 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4195 4681
4196 $w = 0 if $w < 0;
4197 $h = 0 if $h < 0;
4198
4199 $w = max $widget->{min_w}, $w; 4682 $w = max $widget->{min_w}, $w;
4200 $h = max $widget->{min_h}, $h; 4683 $h = max $widget->{min_h}, $h;
4201 4684
4202# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4685# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4203# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4686# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4204 4687
4205 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4688 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4206 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4689 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4207 4690
4208 $w = int $w + 0.5; 4691 $w = int $w + 0.5;
4209 $h = int $h + 0.5; 4692 $h = int $h + 0.5;
4210 4693
4211 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4694 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4212 $widget->{old_w} = $widget->{w}; 4695 $widget->{old_w} = $widget->{w};
4213 $widget->{old_h} = $widget->{h}; 4696 $widget->{old_h} = $widget->{h};
4214 4697
4215 $widget->{w} = $w; 4698 $widget->{w} = $w;
4216 $widget->{h} = $h; 4699 $widget->{h} = $h;
4217 4700
4218 $widget->emit (size_allocate => $w, $h); 4701 $widget->emit (size_allocate => $w, $h);
4702 }
4219 } 4703 }
4220 } 4704 }
4221 } 4705 }
4222 4706
4223 while ($self->{post_alloc_hook}) { 4707 while ($self->{post_alloc_hook}) {
4224 $_->() 4708 $_->()
4225 for values %{delete $self->{post_alloc_hook}}; 4709 for values %{delete $self->{post_alloc_hook}};
4226 } 4710 }
4227
4228 4711
4229 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4712 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4230 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4713 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4231 glClear GL_COLOR_BUFFER_BIT; 4714 glClear GL_COLOR_BUFFER_BIT;
4232 4715
4235 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4718 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4236 glMatrixMode GL_MODELVIEW; 4719 glMatrixMode GL_MODELVIEW;
4237 glLoadIdentity; 4720 glLoadIdentity;
4238 4721
4239 { 4722 {
4240 package CFPlus::UI::Base; 4723 package DC::UI::Base;
4241 4724
4242 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4725 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4243 (0, 0, $self->{w}, $self->{h}); 4726 (0, 0, $self->{w}, $self->{h});
4244 4727
4245 $self->_draw; 4728 $self->_draw;
4246 } 4729 }
4247} 4730}
4248 4731
4249############################################################################# 4732#############################################################################
4250 4733
4251package CFPlus::UI; 4734package DC::UI;
4252 4735
4253$ROOT = new CFPlus::UI::Root; 4736$ROOT = new DC::UI::Root;
4254$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4737$TOOLTIP = new DC::UI::Tooltip z => 900;
4255 4738
42561 47391
4257

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines