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.381 by root, Thu Jul 19 13:45:18 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 {
998 $self->emit (changed => $x, $y); 978 $self->emit (changed => $x, $y);
999 $self->update; 979 $self->update;
1000 } 980 }
1001} 981}
1002 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 }
999}
1000
1003# 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
1004sub coord2local { 1002sub coord2local {
1005 my ($self, $x, $y) = @_; 1003 my ($self, $x, $y) = @_;
1006 1004
1007 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) 1005 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
1020 my ($self, $x, $y) = @_; 1018 my ($self, $x, $y) = @_;
1021 1019
1022 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1023 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1024 ) { 1022 ) {
1025 $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})
1026 } else { 1024 } else {
1027 $self->CFPlus::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
1028 } 1026 }
1029} 1027}
1030 1028
1031sub _render { 1029sub _render {
1032 my ($self) = @_; 1030 my ($self) = @_;
1033 1031
1034 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};
1035 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};
1036 1034
1037 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1038 1036
1039 $self->SUPER::_render; 1037 $self->SUPER::_render;
1040} 1038}
1041 1039
1042############################################################################# 1040#############################################################################
1043 1041
1044package CFPlus::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1045 1043
1046our @ISA = CFPlus::UI::Table::; 1044our @ISA = DC::UI::Table::;
1047 1045
1048sub new { 1046sub new {
1049 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1050 1048
1051 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1052 1050
1053 my $self; 1051 my $self;
1054 1052
1055 my $hslider = new CFPlus::UI::Slider 1053 my $hslider = new DC::UI::Slider
1054 c_col => 0,
1055 c_row => 1,
1056 vertical => 0, 1056 vertical => 0,
1057 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1058 on_changed => sub { 1058 on_changed => sub {
1059 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1061 }, 1061 },
1062 ; 1062 ;
1063 1063
1064 my $vslider = new CFPlus::UI::Slider 1064 my $vslider = new DC::UI::Slider
1065 c_col => 1,
1066 c_row => 0,
1065 vertical => 1, 1067 vertical => 1,
1066 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1067 on_changed => sub { 1069 on_changed => sub {
1068 $self->{vpos} = $_[1]; 1070 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1071 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1079 col_expand => [1, 0], 1081 col_expand => [1, 0],
1080 row_expand => [1, 0], 1082 row_expand => [1, 0],
1081 %arg, 1083 %arg,
1082 ); 1084 );
1083 1085
1084 $self->{vp} = new CFPlus::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1087 c_col => 0,
1088 c_row => 0,
1085 expand => 1, 1089 expand => 1,
1086 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1087 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1088 on_changed => sub { 1092 on_changed => sub {
1089 my ($vp, $x, $y) = @_; 1093 my ($vp, $x, $y) = @_;
1091 $vp->{parent}{hslider}->set_value ($x); 1095 $vp->{parent}{hslider}->set_value ($x);
1092 $vp->{parent}{vslider}->set_value ($y); 1096 $vp->{parent}{vslider}->set_value ($y);
1093 1097
1094 0 1098 0
1095 }, 1099 },
1100 on_size_allocate => sub {
1101 my ($vp, $w, $h) = @_;
1102 $vp->{parent}->update_slider;
1103 0
1104 },
1096 ; 1105 ;
1097 1106
1098 $self->SUPER::add (0, 0, $self->{vp}); 1107 $self->SUPER::add ($self->{vp});
1099 1108
1100 $self->add ($child) if $child; 1109 $self->add ($child) if $child;
1101 1110
1102 $self 1111 $self
1103} 1112}
1104
1105#TODO# update range on size_allocate depending on child
1106 1113
1107sub add { 1114sub add {
1108 my ($self, $widget) = @_; 1115 my ($self, $widget) = @_;
1109 1116
1110 $self->{vp}->add ($self->{child} = $widget); 1117 $self->{vp}->add ($self->{child} = $widget);
1111} 1118}
1112 1119
1120sub set_offset { shift->{vp}->set_offset (@_) }
1121sub set_center { shift->{vp}->set_center (@_) }
1122sub make_visible { shift->{vp}->make_visible (@_) }
1123
1113sub update_slider { 1124sub update_slider {
1114 my ($self) = @_; 1125 my ($self) = @_;
1115 1126
1116 my $child = ($self->{vp} or return)->child; 1127 my $child = ($self->{vp} or return)->child;
1117 1128
1129 if ($self->{scroll_x}) {
1118 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1130 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1119 $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]);
1120 1132
1121 my $visible = $w1 > $w2; 1133 my $visible = $w1 > $w2;
1122 if ($visible != $self->{hslider}{visible}) { 1134 if ($visible != $self->{hslider_visible}) {
1135 $self->{hslider_visible} = $visible;
1123 $visible ? $self->SUPER::add (0, 1, $self->{hslider}) 1136 $visible ? $self->SUPER::add ($self->{hslider})
1124 : $self->{hslider}->hide; 1137 : $self->SUPER::remove ($self->{hslider});
1138 }
1125 } 1139 }
1126 1140
1141 if ($self->{scroll_y}) {
1127 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1142 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1128 $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]);
1129 1144
1130 my $visible = $h1 > $h2; 1145 my $visible = $h1 > $h2;
1131 if ($visible != $self->{vslider}{visible}) { 1146 if ($visible != $self->{vslider_visible}) {
1147 $self->{vslider_visible} = $visible;
1132 $visible ? $self->SUPER::add (1, 0, $self->{vslider}) 1148 $visible ? $self->SUPER::add ($self->{vslider})
1133 : $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);
1134 } 1169 };
1135}
1136
1137sub update {
1138 my ($self) = @_;
1139
1140 $self->SUPER::update;
1141 $self->update_slider;
1142} 1170}
1143 1171
1144sub invoke_mouse_wheel { 1172sub invoke_mouse_wheel {
1145 my ($self, $ev) = @_; 1173 my ($self, $ev) = @_;
1146 1174
1147 return 0 unless $ev->{dy}; # only vertical movements for now
1148
1149 $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};
1150 1177
1151 1 1178 1
1152} 1179}
1153 1180
1154sub invoke_button_down { 1181sub invoke_button_down {
1155 my ($self, $ev, $x, $y) = @_; 1182 my ($self, $ev, $x, $y) = @_;
1156 1183
1157 if ($ev->{button} == 2) { 1184 if ($ev->{button} == 2) {
1158 $self->grab_focus; 1185 $self->start_dragging ($ev);
1159
1160 my $ox = $self->{vp}{view_x} + $ev->{x};
1161 my $oy = $self->{vp}{view_y} + $ev->{y};
1162
1163 $self->{motion} = sub {
1164 my ($ev, $x, $y) = @_;
1165
1166 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1167 $self->update;
1168 };
1169
1170 return 1; 1186 return 1;
1171 } 1187 }
1172 1188
1173 0 1189 0
1174} 1190}
1201 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1202} 1218}
1203 1219
1204############################################################################# 1220#############################################################################
1205 1221
1206package CFPlus::UI::Frame; 1222package DC::UI::Frame;
1207 1223
1208our @ISA = CFPlus::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1209 1225
1210use CFPlus::OpenGL; 1226use DC::OpenGL;
1211 1227
1212sub new { 1228sub new {
1213 my $class = shift; 1229 my $class = shift;
1214 1230
1215 $class->SUPER::new ( 1231 $class->SUPER::new (
1225 my ($w, $h) = @$self{qw(w h)}; 1241 my ($w, $h) = @$self{qw(w h)};
1226 1242
1227 glEnable GL_BLEND; 1243 glEnable GL_BLEND;
1228 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1244 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1229 glColor_premultiply @{ $self->{bg} }; 1245 glColor_premultiply @{ $self->{bg} };
1230
1231 glBegin GL_QUADS;
1232 glVertex 0 , 0;
1233 glVertex 0 , $h;
1234 glVertex $w, $h; 1246 glRect 0, 0, $w, $h;
1235 glVertex $w, 0;
1236 glEnd;
1237
1238 glDisable GL_BLEND; 1247 glDisable GL_BLEND;
1239 } 1248 }
1240 1249
1241 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1242} 1251}
1243 1252
1244############################################################################# 1253#############################################################################
1245 1254
1246package CFPlus::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1247 1256
1248our @ISA = CFPlus::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1249 1258
1250use CFPlus::OpenGL; 1259use DC::OpenGL;
1251 1260
1252sub new { 1261sub new {
1253 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1254 1263
1255 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1256 $arg{label} = new CFPlus::UI::Label 1265 $arg{label} = new DC::UI::Label
1257 align => 1, 1266 align => 1,
1258 valign => 0, 1267 valign => 0.5,
1259 text => $arg{label}, 1268 text => $arg{label},
1260 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1261 } 1270 }
1262 1271
1263 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1264 # label => "", 1273 # label => "",
1265 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1266 border => 0.8, 1275 border => 0.8,
1267 style => 'single', 1276 style => 'single',
1268 %arg, 1277 %arg,
1269 ); 1278 );
1270 1279
1273 1282
1274sub add { 1283sub add {
1275 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1276 1285
1277 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1278 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1279} 1288}
1280 1289
1281sub border { 1290sub border {
1282 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1283} 1292}
1323 my $border = $self->border; 1332 my $border = $self->border;
1324 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1325 1334
1326 $child->draw; 1335 $child->draw;
1327 1336
1328 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1329 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1330 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1331 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1332 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;
1333 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;
1341 } 1350 }
1342} 1351}
1343 1352
1344############################################################################# 1353#############################################################################
1345 1354
1346package CFPlus::UI::Toplevel; 1355package DC::UI::Toplevel;
1347 1356
1348our @ISA = CFPlus::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1349 1358
1350use CFPlus::OpenGL; 1359use DC::OpenGL;
1351 1360
1352my $bg = 1361my $bg =
1353 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1354 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1355 1364
1356my @border = 1365my @border =
1357 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1358 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);
1359 1372
1360sub new { 1373sub new {
1361 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1362 1375
1363 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1364 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1365 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1366 border => 0.6, 1379 border => 0.8,
1367 can_events => 1, 1380 can_events => 1,
1368 min_w => 64, 1381 min_w => 64,
1369 min_h => 32, 1382 min_h => 32,
1370 %arg, 1383 %arg,
1371 ); 1384 );
1372 1385
1373 $self->{title_widget} = new CFPlus::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1374 align => 0, 1387 align => 0.5,
1375 valign => 1, 1388 valign => 1,
1376 text => $self->{title}, 1389 text => $self->{title},
1377 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1378 if exists $self->{title}; 1391 if exists $self->{title};
1379 1392
1380 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1381 $self->{close_button} = 1394 $self->{close_button} =
1382 new CFPlus::UI::ImageButton 1395 new DC::UI::ImageButton
1383 path => 'x1_close.png', 1396 path => 'x1_close.png',
1384 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1385 1398
1386 $self->CFPlus::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1387 } 1400 }
1388 1401
1389 $self 1402 $self
1390} 1403}
1391 1404
1392sub add { 1405sub add {
1393 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1394 1407
1395 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1396 $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};
1397 $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};
1398} 1411}
1399 1412
1400sub border { 1413sub border {
1401 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1402} 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#}
1403 1425
1404sub size_request { 1426sub size_request {
1405 my ($self) = @_; 1427 my ($self) = @_;
1406 1428
1407 $self->{title_widget}->size_request 1429 $self->{title_widget}->size_request
1442 1464
1443sub invoke_delete { 1465sub invoke_delete {
1444 my ($self) = @_; 1466 my ($self) = @_;
1445 1467
1446 $self->hide; 1468 $self->hide;
1447 1469
1448 1 1470 1
1449} 1471}
1450 1472
1451sub invoke_button_down { 1473sub invoke_button_down {
1452 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1453 1475
1454 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1455 my $border = $self->border; 1477 my $border = $self->border;
1456 1478
1457 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
1458 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
1459 1481
1460 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1461 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1462 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1463 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1464 1486
1465 my $mx = $x < $border; 1487 my $mx = $x < $border;
1469 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1470 1492
1471 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1472 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1473 1495
1496 $self->set_size (
1474 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1475 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1476 1500
1477 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1478 $self->realloc; 1502 $self->realloc;
1479 }; 1503 };
1480 1504
1481 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1482 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1483 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1484 1508
1485 $self->{motion} = sub { 1509 $self->{motion} = sub {
1486 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1531 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1532 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1533 1557
1534 my $border = $self->border; 1558 my $border = $self->border;
1535 1559
1560 if ($border) {
1536 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1537 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1538 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1539 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1540 $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 }
1541 1581
1542 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1543 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1544 1584
1545 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
1564 if $self->{close_button}; 1604 if $self->{close_button};
1565} 1605}
1566 1606
1567############################################################################# 1607#############################################################################
1568 1608
1569package CFPlus::UI::Table; 1609package DC::UI::Table;
1570 1610
1571our @ISA = CFPlus::UI::Base::; 1611our @ISA = DC::UI::Container::;
1572 1612
1573use List::Util qw(max sum); 1613use List::Util qw(max sum);
1574 1614
1575use CFPlus::OpenGL; 1615use DC::OpenGL;
1576 1616
1577sub new { 1617sub new {
1578 my $class = shift; 1618 my $class = shift;
1579 1619
1580 $class->SUPER::new ( 1620 $class->SUPER::new (
1581 children => [],
1582 col_expand => [], 1621 col_expand => [],
1583 row_expand => [], 1622 row_expand => [],
1584 @_, 1623 @_,
1585 ) 1624 )
1586} 1625}
1587 1626
1588sub children {
1589 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1590}
1591
1592# TODO: store row/col info in child widget and use standard add/del
1593sub 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 {
1594 my ($self) = shift; 1639 my $self = shift;
1640
1641 my @widgets;
1595 1642
1596 while (@_) { 1643 while (@_) {
1597 my ($x, $y, $child) = splice @_, 0, 3, (); 1644 my ($col, $row, $child) = splice @_, 0, 3, ();
1598 $child->set_parent ($self);
1599 $self->{children}[$y][$x] = $child;
1600 }
1601 1645
1602 $self->{force_realloc} = 1; 1646 $child->{c_row} = $row;
1603 $self->{force_size_alloc} = 1; 1647 $child->{c_col} = $col;
1604 $self->realloc;
1605}
1606 1648
1607sub remove { 1649 push @widgets, $child;
1650 }
1651
1652 $self->add (@widgets);
1653}
1654
1655sub get_wh {
1608 my ($self, $child) = @_; 1656 my ($self) = @_;
1609 1657
1610 for (@{ $self->{children} }) { 1658 my (@w, @h);
1611 for (@{ $_ || [] }) { 1659
1612 $_ = 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];
1613 } 1670 }
1614 } 1671 }
1615}
1616 1672
1617# TODO: move to container class maybe? send children a signal on removal? 1673 # second pass, rows
1618sub clear { 1674 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1619 my ($self) = @_;
1620
1621 my @children = $self->children;
1622 delete $self->{children};
1623
1624 for (@children) {
1625 delete $_->{parent};
1626 $_->hide;
1627 }
1628
1629 $self->realloc;
1630}
1631
1632sub get_wh {
1633 my ($self) = @_;
1634
1635 my (@w, @h);
1636
1637 for my $y (0 .. $#{$self->{children}}) {
1638 my $row = $self->{children}[$y]
1639 or next;
1640
1641 for my $x (0 .. $#$row) {
1642 my $widget = $row->[$x]
1643 or next;
1644 my ($w, $h) = @$widget{qw(req_w req_h)}; 1675 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1645 1676
1646 $w[$x] = max $w[$x], $w; 1677 my $sh = sum @h[$r .. $r + $rs - 1];
1647 $h[$y] = max $h[$y], $h; 1678
1679 if ($h > $sh) {
1680 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1648 } 1681 }
1649 } 1682 }
1650 1683
1651 (\@w, \@h) 1684 (\@w, \@h)
1652} 1685}
1668 my ($ws, $hs) = $self->get_wh; 1701 my ($ws, $hs) = $self->get_wh;
1669 1702
1670 my $req_w = (sum @$ws) || 1; 1703 my $req_w = (sum @$ws) || 1;
1671 my $req_h = (sum @$hs) || 1; 1704 my $req_h = (sum @$hs) || 1;
1672 1705
1673 # TODO: nicer code 1706 # now linearly scale the rows/columns to the allocated size
1674 my @col_expand = @{$self->{col_expand}}; 1707 my @col_expand = @{$self->{col_expand}};
1675 @col_expand = (1) x @$ws unless @col_expand; 1708 @col_expand = (1) x @$ws unless @col_expand;
1676 my $col_expand = (sum @col_expand) || 1; 1709 my $col_expand = (sum @col_expand) || 1;
1677 1710
1678 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1711 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1679 1712
1680 CFPlus::UI::harmonize $ws; 1713 DC::UI::harmonize $ws;
1681 1714
1682 my @row_expand = @{$self->{row_expand}}; 1715 my @row_expand = @{$self->{row_expand}};
1683 @row_expand = (1) x @$ws unless @row_expand; 1716 @row_expand = (1) x @$ws unless @row_expand;
1684 my $row_expand = (sum @row_expand) || 1; 1717 my $row_expand = (sum @row_expand) || 1;
1685 1718
1686 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1719 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1687 1720
1688 CFPlus::UI::harmonize $hs; 1721 DC::UI::harmonize $hs;
1689 1722
1690 my $y; 1723 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1724 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1691 1725
1692 for my $r (0 .. $#{$self->{children}}) { 1726 for my $widget ($self->children) {
1693 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)};
1694 or next;
1695 1728
1696 my $x = 0; 1729 $widget->configure (
1697 my $row_h = $hs->[$r]; 1730 $x[$c], $y[$r],
1731 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1698 1732 );
1699 for my $c (0 .. $#$row) {
1700 my $col_w = $ws->[$c];
1701
1702 if (my $widget = $row->[$c]) {
1703 $widget->configure ($x, $y, $col_w, $row_h);
1704 }
1705
1706 $x += $col_w;
1707 }
1708
1709 $y += $row_h;
1710 } 1733 }
1711 1734
1712 1 1735 1
1713} 1736}
1714 1737
1715sub find_widget {
1716 my ($self, $x, $y) = @_;
1717
1718 $x -= $self->{x};
1719 $y -= $self->{y};
1720
1721 my $res;
1722
1723 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1724 $res = $_->find_widget ($x, $y)
1725 and return $res;
1726 }
1727
1728 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1729}
1730
1731sub _draw {
1732 my ($self) = @_;
1733
1734 for (grep $_, @{$self->{children}}) {
1735 $_->draw for grep $_, @$_;
1736 }
1737}
1738
1739############################################################################# 1738#############################################################################
1740 1739
1741package CFPlus::UI::Fixed; 1740package DC::UI::Fixed;
1742 1741
1743use List::Util qw(min max); 1742use List::Util qw(min max);
1744 1743
1745our @ISA = CFPlus::UI::Container::; 1744our @ISA = DC::UI::Container::;
1746
1747sub add {
1748 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1749
1750 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1751 $self->SUPER::add ($child);
1752}
1753 1745
1754sub _scale($$$) { 1746sub _scale($$$) {
1755 my ($mode, $val, $max) = @_; 1747 my ($rel, $val, $max) = @_;
1756 1748
1757 $mode eq "abs" ? $val 1749 $rel ? $val * $max : $val
1758 : $mode eq "rel" ? $val * $max
1759 : 0
1760} 1750}
1761 1751
1762sub size_request { 1752sub size_request {
1763 my ($self) = @_; 1753 my ($self) = @_;
1764 1754
1765 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1755 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1766 1756
1767 # determine overall size by querying abs widgets 1757 # determine overall size by querying abs widgets
1768 for my $child ($self->visible_children) { 1758 for my $child ($self->visible_children) {
1769 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};
1770 1762
1771 if ($pos eq "abs") {
1772 $w = _scale $size, $w, $child->{req_w};
1773 $h = _scale $size, $h, $child->{req_h};
1774
1775 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1763 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1776 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1764 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1777 } 1765 }
1778 } 1766 }
1779 1767
1780 my $W = $x2 - $x1; 1768 my $W = $x2 - $x1;
1781 my $H = $y2 - $y1; 1769 my $H = $y2 - $y1;
1782 1770
1783 # now layout remaining widgets 1771 # now layout remaining widgets
1784 for my $child ($self->visible_children) { 1772 for my $child ($self->visible_children) {
1785 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;
1786 1776
1787 if ($pos ne "abs") {
1788 $x = _scale $pos, $x, $W;
1789 $y = _scale $pos, $x, $H;
1790 $w = _scale $size, $w, $child->{req_w};
1791 $h = _scale $size, $h, $child->{req_h};
1792
1793 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1777 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1794 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1778 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1795 } 1779 }
1796 } 1780 }
1797 1781
1798 my $W = $x2 - $x1; 1782 my $W = $x2 - $x1;
1799 my $H = $y2 - $y1; 1783 my $H = $y2 - $y1;
1803 1787
1804sub invoke_size_allocate { 1788sub invoke_size_allocate {
1805 my ($self, $W, $H) = @_; 1789 my ($self, $W, $H) = @_;
1806 1790
1807 for my $child ($self->visible_children) { 1791 for my $child ($self->visible_children) {
1808 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;
1809 1794
1810 $x = _scale $pos, $x, $W; 1795 $x += $child->{c_halign} * $child->{req_w};
1811 $y = _scale $pos, $x, $H; 1796 $y += $child->{c_valign} * $child->{req_h};
1812 $w = _scale $size, $w, $child->{req_w};
1813 $h = _scale $size, $h, $child->{req_h};
1814 1797
1815 $child->configure ($x, $y, $w, $h); 1798 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1816 } 1799 }
1817 1800
1818 1 1801 1
1819} 1802}
1820 1803
1821############################################################################# 1804#############################################################################
1822 1805
1823package CFPlus::UI::Box; 1806package DC::UI::Box;
1824 1807
1825our @ISA = CFPlus::UI::Container::; 1808our @ISA = DC::UI::Container::;
1826 1809
1827sub size_request { 1810sub size_request {
1828 my ($self) = @_; 1811 my ($self) = @_;
1812
1813 my @children = $self->visible_children;
1829 1814
1830 $self->{vertical} 1815 $self->{vertical}
1831 ? ( 1816 ? (
1832 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1817 (List::Util::max map $_->{req_w}, @children),
1833 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1818 (List::Util::sum map $_->{req_h}, @children),
1834 ) 1819 )
1835 : ( 1820 : (
1836 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1821 (List::Util::sum map $_->{req_w}, @children),
1837 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1822 (List::Util::max map $_->{req_h}, @children),
1838 ) 1823 )
1839} 1824}
1840 1825
1841sub invoke_size_allocate { 1826sub invoke_size_allocate {
1842 my ($self, $w, $h) = @_; 1827 my ($self, $w, $h) = @_;
1855 if ($req > $space) { 1840 if ($req > $space) {
1856 # ah well, not enough space 1841 # ah well, not enough space
1857 $_ *= $space / $req for @req; 1842 $_ *= $space / $req for @req;
1858 } else { 1843 } else {
1859 my $expand = (List::Util::sum map $_->{expand}, @children) || 1; 1844 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1860 1845
1861 $space = ($space - $req) / $expand; # remaining space to give away 1846 $space = ($space - $req) / $expand; # remaining space to give away
1862 1847
1863 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1864 for 0 .. $#children; 1849 for 0 .. $#children;
1865 } 1850 }
1866 } 1851 }
1867 1852
1868 CFPlus::UI::harmonize \@req; 1853 DC::UI::harmonize \@req;
1869 1854
1870 my $pos = 0; 1855 my $pos = 0;
1871 for (0 .. $#children) { 1856 for (0 .. $#children) {
1872 my $alloc = $req[$_]; 1857 my $alloc = $req[$_];
1873 $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));
1878 1 1863 1
1879} 1864}
1880 1865
1881############################################################################# 1866#############################################################################
1882 1867
1883package CFPlus::UI::HBox; 1868package DC::UI::HBox;
1884 1869
1885our @ISA = CFPlus::UI::Box::; 1870our @ISA = DC::UI::Box::;
1886 1871
1887sub new { 1872sub new {
1888 my $class = shift; 1873 my $class = shift;
1889 1874
1890 $class->SUPER::new ( 1875 $class->SUPER::new (
1893 ) 1878 )
1894} 1879}
1895 1880
1896############################################################################# 1881#############################################################################
1897 1882
1898package CFPlus::UI::VBox; 1883package DC::UI::VBox;
1899 1884
1900our @ISA = CFPlus::UI::Box::; 1885our @ISA = DC::UI::Box::;
1901 1886
1902sub new { 1887sub new {
1903 my $class = shift; 1888 my $class = shift;
1904 1889
1905 $class->SUPER::new ( 1890 $class->SUPER::new (
1908 ) 1893 )
1909} 1894}
1910 1895
1911############################################################################# 1896#############################################################################
1912 1897
1913package CFPlus::UI::Label; 1898package DC::UI::Label;
1914 1899
1915our @ISA = CFPlus::UI::DrawBG::; 1900our @ISA = DC::UI::DrawBG::;
1916 1901
1917use CFPlus::OpenGL; 1902use DC::OpenGL;
1918 1903
1919sub new { 1904sub new {
1920 my ($class, %arg) = @_; 1905 my ($class, %arg) = @_;
1921 1906
1922 my $self = $class->SUPER::new ( 1907 my $self = $class->SUPER::new (
1927 #text => initial text 1912 #text => initial text
1928 #markup => initial narkup 1913 #markup => initial narkup
1929 #max_w => maximum pixel width 1914 #max_w => maximum pixel width
1930 #style => 0, # render flags 1915 #style => 0, # render flags
1931 ellipsise => 3, # end 1916 ellipsise => 3, # end
1932 layout => (new CFPlus::Layout), 1917 layout => (new DC::Layout),
1933 fontsize => 1, 1918 fontsize => 1,
1934 align => -1, 1919 align => 0.5,
1935 valign => -1, 1920 valign => 0.5,
1936 padding_x => 2, 1921 padding_x => 4,
1937 padding_y => 2, 1922 padding_y => 2,
1938 can_events => 0, 1923 can_events => 0,
1939 %arg 1924 %arg
1940 ); 1925 );
1941 1926
1942 if (exists $self->{template}) { 1927 if (exists $self->{template}) {
1943 my $layout = new CFPlus::Layout; 1928 my $layout = new DC::Layout;
1944 $layout->set_text (delete $self->{template}); 1929 $layout->set_text (delete $self->{template});
1945 $self->{template} = $layout; 1930 $self->{template} = $layout;
1946 } 1931 }
1947 1932
1948 if (exists $self->{markup}) { 1933 if (exists $self->{markup}) {
1966 1951
1967 delete $self->{ox}; 1952 delete $self->{ox};
1968 $self->SUPER::realloc; 1953 $self->SUPER::realloc;
1969} 1954}
1970 1955
1956sub clear {
1957 my ($self) = @_;
1958
1959 $self->set_text ("");
1960}
1961
1971sub set_text { 1962sub set_text {
1972 my ($self, $text) = @_; 1963 my ($self, $text) = @_;
1973 1964
1974 return if $self->{text} eq "T$text"; 1965 return if $self->{text} eq "T$text";
1975 $self->{text} = "T$text"; 1966 $self->{text} = "T$text";
1998 1989
1999sub size_request { 1990sub size_request {
2000 my ($self) = @_; 1991 my ($self) = @_;
2001 1992
2002 $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
2003 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
2004 $self->{layout}->set_width ($self->{max_w} || -1); 1997 $self->{layout}->set_width ($max_w);
2005 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
2006 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2007 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2000 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2008 2001
2009 my ($w, $h) = $self->{layout}->size; 2002 my ($w, $h) = $self->{layout}->size;
2010 2003
2011 if (exists $self->{template}) { 2004 if (exists $self->{template}) {
2012 $self->{template}->set_font ($self->{font}) if $self->{font}; 2005 $self->{template}->set_font ($self->{font}) if $self->{font};
2013 $self->{template}->set_width ($self->{max_w} || -1); 2006 $self->{template}->set_width ($max_w);
2014 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2007 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
2015 2008
2016 my ($w2, $h2) = $self->{template}->size; 2009 my ($w2, $h2) = $self->{template}->size;
2017 2010
2018 $w = List::Util::max $w, $w2; 2011 $w = List::Util::max $w, $w2;
2074 2067
2075 [$self->{layout}->size] 2068 [$self->{layout}->size]
2076 }; 2069 };
2077 2070
2078 unless (exists $self->{ox}) { 2071 unless (exists $self->{ox}) {
2079 $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);
2080 : $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);
2081 : ($self->{w} - $size->[0]) * 0.5);
2082 2074
2083 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2075 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2084 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2085 : ($self->{h} - $size->[1]) * 0.5);
2086 }; 2076 };
2087 2077
2088 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2078# unless ($self->{list}) {
2089 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2079# $self->{list} = DC::OpenGL::glGenList;
2090 2080# DC::OpenGL::glNewList $self->{list};
2091 $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;
2092} 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#}
2093 2097
2094############################################################################# 2098#############################################################################
2095 2099
2096package CFPlus::UI::EntryBase; 2100package DC::UI::EntryBase;
2097 2101
2098our @ISA = CFPlus::UI::Label::; 2102our @ISA = DC::UI::Label::;
2099 2103
2100use CFPlus::OpenGL; 2104use DC::OpenGL;
2101 2105
2102sub new { 2106sub new {
2103 my $class = shift; 2107 my $class = shift;
2104 2108
2105 $class->SUPER::new ( 2109 $class->SUPER::new (
2106 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2107 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2112 outline => undef,
2108 active_bg => [1, 1, 1, 0.5], 2113 active_bg => [0, 0, 1, .2],
2109 active_fg => [0, 0, 0], 2114 active_fg => [1, 1, 1],
2115 active_outline => [1, 1, 0],
2110 can_hover => 1, 2116 can_hover => 1,
2111 can_focus => 1, 2117 can_focus => 1,
2118 align => 0,
2112 valign => 0, 2119 valign => 0.5,
2113 can_events => 1, 2120 can_events => 1,
2114 ellipsise => 0, 2121 ellipsise => 0,
2122 padding_x => 4,
2123 padding_y => 2,
2115 #text => ... 2124 #text => ...
2116 #hidden => "*", 2125 #hidden => "*",
2117 @_ 2126 @_
2118 ) 2127 )
2119} 2128}
2166 2175
2167 my $text = $self->get_text; 2176 my $text = $self->get_text;
2168 2177
2169 $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;
2170 2179
2171 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2172 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2173 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2174 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2175 } elsif ($sym == CFPlus::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2176 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2177 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2178 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2179 } elsif ($sym == CFPlus::SDLK_HOME) { 2188 } elsif ($sym == DC::SDLK_HOME) {
2180 # what a hack 2189 # what a hack
2181 $self->{cursor} = 2190 $self->{cursor} =
2182 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2191 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2183 ? length $1 2192 ? length $1
2184 : 0; 2193 : 0;
2185 } elsif ($sym == CFPlus::SDLK_END) { 2194 } elsif ($sym == DC::SDLK_END) {
2186 # uh, again 2195 # uh, again
2187 $self->{cursor} = 2196 $self->{cursor} =
2188 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2197 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2189 ? $self->{cursor} + length $1 2198 ? $self->{cursor} + length $1
2190 : length $self->{text}; 2199 : length $self->{text};
2229 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; 2238 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2230 $self->{cursor} = length $text; 2239 $self->{cursor} = length $text;
2231 2240
2232 $self->_set_text ($self->{text}); 2241 $self->_set_text ($self->{text});
2233 $self->update; 2242 $self->update;
2234 2243
2235 1 2244 1
2236} 2245}
2237 2246
2238sub invoke_mouse_motion { 2247sub invoke_mouse_motion {
2239 my ($self, $ev, $x, $y) = @_; 2248 my ($self, $ev, $x, $y) = @_;
2254 glColor_premultiply @{$self->{bg}}; 2263 glColor_premultiply @{$self->{bg}};
2255 } 2264 }
2256 2265
2257 glEnable GL_BLEND; 2266 glEnable GL_BLEND;
2258 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2267 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2259 glBegin GL_QUADS;
2260 glVertex 0 , 0;
2261 glVertex 0 , $self->{h};
2262 glVertex $self->{w}, $self->{h}; 2268 glRect 0, 0, $self->{w}, $self->{h};
2263 glVertex $self->{w}, 0;
2264 glEnd;
2265 glDisable GL_BLEND; 2269 glDisable GL_BLEND;
2266 2270
2267 $self->SUPER::_draw; 2271 $self->SUPER::_draw;
2268 2272
2269 #TODO: force update every cursor change :( 2273 #TODO: force update every cursor change :(
2271 2275
2272 unless (exists $self->{cur_h}) { 2276 unless (exists $self->{cur_h}) {
2273 my $text = substr $self->{text}, 0, $self->{cursor}; 2277 my $text = substr $self->{text}, 0, $self->{cursor};
2274 utf8::encode $text; 2278 utf8::encode $text;
2275 2279
2276 @$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);
2277 } 2281 }
2278 2282
2283 glColor_premultiply @{$self->{active_fg}};
2279 glBegin GL_LINES; 2284 glBegin GL_LINES;
2280 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};
2281 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};
2282 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;
2283 } 2302 }
2284} 2303}
2285 2304
2286############################################################################# 2305#############################################################################
2287 2306
2288package CFPlus::UI::Entry; 2307package DC::UI::Entry;
2289 2308
2290our @ISA = CFPlus::UI::EntryBase::; 2309our @ISA = DC::UI::EntryBase::;
2291 2310
2292use 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
2293 2322
2294sub invoke_key_down { 2323sub invoke_key_down {
2295 my ($self, $ev) = @_; 2324 my ($self, $ev) = @_;
2296 2325
2297 my $sym = $ev->{sym}; 2326 my $sym = $ev->{sym};
2303 $self->{history_pointer} = -1; 2332 $self->{history_pointer} = -1;
2304 $self->{history_saveback} = ''; 2333 $self->{history_saveback} = '';
2305 $self->emit (activate => $txt); 2334 $self->emit (activate => $txt);
2306 $self->update; 2335 $self->update;
2307 2336
2308 } elsif ($sym == CFPlus::SDLK_UP) { 2337 } elsif ($sym == DC::SDLK_UP) {
2309 if ($self->{history_pointer} < 0) { 2338 if ($self->{history_pointer} < 0) {
2310 $self->{history_saveback} = $self->get_text; 2339 $self->{history_saveback} = $self->get_text;
2311 } 2340 }
2312 if (@{$self->{history} || []} > 0) { 2341 if (@{$self->{history} || []} > 0) {
2313 $self->{history_pointer}++; 2342 $self->{history_pointer}++;
2315 $self->{history_pointer} = @{$self->{history} || []} - 1; 2344 $self->{history_pointer} = @{$self->{history} || []} - 1;
2316 } 2345 }
2317 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2346 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2318 } 2347 }
2319 2348
2320 } elsif ($sym == CFPlus::SDLK_DOWN) { 2349 } elsif ($sym == DC::SDLK_DOWN) {
2321 $self->{history_pointer}--; 2350 $self->{history_pointer}--;
2322 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2351 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2323 2352
2324 if ($self->{history_pointer} >= 0) { 2353 if ($self->{history_pointer} >= 0) {
2325 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2354 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2326 } else { 2355 } else {
2356 if (defined $self->{history_saveback}) {
2327 $self->set_text ($self->{history_saveback}); 2357 $self->set_text ($self->{history_saveback});
2358 $self->{history_saveback} = undef;
2359 }
2328 } 2360 }
2329 2361
2330 } else { 2362 } else {
2331 return $self->SUPER::invoke_key_down ($ev) 2363 return $self->SUPER::invoke_key_down ($ev)
2332 } 2364 }
2334 1 2366 1
2335} 2367}
2336 2368
2337############################################################################# 2369#############################################################################
2338 2370
2339package CFPlus::UI::TextEdit; 2371package DC::UI::TextEdit;
2340 2372
2341our @ISA = CFPlus::UI::EntryBase::; 2373our @ISA = DC::UI::EntryBase::;
2342 2374
2343use 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}
2344 2386
2345sub move_cursor_ver { 2387sub move_cursor_ver {
2346 my ($self, $dy) = @_; 2388 my ($self, $dy) = @_;
2347 2389
2348 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2390 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2349 2391
2350 $y += $dy; 2392 $line += $dy;
2351 2393
2352 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))) {
2353 $self->{cursor} = $index; 2395 $self->{cursor} = $index;
2354 delete $self->{cur_h}; 2396 delete $self->{cur_h};
2355 $self->update; 2397 $self->update;
2356 return; 2398 return;
2357 } 2399 }
2360sub invoke_key_down { 2402sub invoke_key_down {
2361 my ($self, $ev) = @_; 2403 my ($self, $ev) = @_;
2362 2404
2363 my $sym = $ev->{sym}; 2405 my $sym = $ev->{sym};
2364 2406
2365 if ($sym == CFPlus::SDLK_UP) { 2407 if ($sym == DC::SDLK_UP) {
2366 $self->move_cursor_ver (-1); 2408 $self->move_cursor_ver (-1);
2367 } elsif ($sym == CFPlus::SDLK_DOWN) { 2409 } elsif ($sym == DC::SDLK_DOWN) {
2368 $self->move_cursor_ver (+1); 2410 $self->move_cursor_ver (+1);
2369 } else { 2411 } else {
2370 return $self->SUPER::invoke_key_down ($ev) 2412 return $self->SUPER::invoke_key_down ($ev)
2371 } 2413 }
2372 2414
2373 1 2415 1
2374} 2416}
2375 2417
2376############################################################################# 2418#############################################################################
2377 2419
2378package CFPlus::UI::Button; 2420package DC::UI::ButtonBin;
2379 2421
2380our @ISA = CFPlus::UI::Label::; 2422our @ISA = DC::UI::Bin::;
2381 2423
2382use CFPlus::OpenGL; 2424use DC::OpenGL;
2383 2425
2384my @tex = 2426my @tex =
2385 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2386 qw(b1_button_inactive.png b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2387 2429
2388sub new { 2430sub new {
2389 my $class = shift; 2431 my $class = shift;
2390 2432
2391 $class->SUPER::new ( 2433 $class->SUPER::new (
2434 can_hover => 1,
2435 align => 0.5,
2436 valign => 0.5,
2437 can_events => 1,
2438 @_
2439 )
2440}
2441
2442sub invoke_button_up {
2443 my ($self, $ev, $x, $y) = @_;
2444
2445 $self->emit ("activate")
2446 if $x >= 0 && $x < $self->{w}
2447 && $y >= 0 && $y < $self->{h};
2448
2449 1
2450}
2451
2452sub _draw {
2453 my ($self) = @_;
2454
2455 glEnable GL_TEXTURE_2D;
2456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2457 glColor 0, 0, 0, 1;
2458
2459 my $tex = $tex[$GRAB == $self];
2460 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2461
2462 glDisable GL_TEXTURE_2D;
2463
2464 $self->SUPER::_draw;
2465}
2466
2467#############################################################################
2468
2469package DC::UI::Button;
2470
2471our @ISA = DC::UI::Label::;
2472
2473use DC::OpenGL;
2474
2475my @tex =
2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2477 qw(b1_button_inactive.png b1_button_active.png);
2478
2479sub new {
2480 my $class = shift;
2481
2482 $class->SUPER::new (
2392 padding_x => 4, 2483 padding_x => 8,
2393 padding_y => 4, 2484 padding_y => 4,
2394 fg => [1.0, 1.0, 1.0], 2485 fg => [1.0, 1.0, 1.0],
2395 active_fg => [0.8, 0.8, 0.8], 2486 active_fg => [0.8, 0.8, 0.8],
2396 can_hover => 1, 2487 can_hover => 1,
2397 align => 0, 2488 align => 0.5,
2398 valign => 0, 2489 valign => 0.5,
2399 can_events => 1, 2490 can_events => 1,
2400 @_ 2491 @_
2401 ) 2492 )
2402} 2493}
2403 2494
2428 $self->SUPER::_draw; 2519 $self->SUPER::_draw;
2429} 2520}
2430 2521
2431############################################################################# 2522#############################################################################
2432 2523
2433package CFPlus::UI::CheckBox; 2524package DC::UI::CheckBox;
2434 2525
2435our @ISA = CFPlus::UI::DrawBG::; 2526our @ISA = DC::UI::DrawBG::;
2436 2527
2437my @tex = 2528my @tex =
2438 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2529 map { new_from_resource DC::Texture $_, mipmap => 1 }
2439 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2530 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2440 2531
2441use CFPlus::OpenGL; 2532use DC::OpenGL;
2442 2533
2443sub new { 2534sub new {
2444 my $class = shift; 2535 my $class = shift;
2445 2536
2446 $class->SUPER::new ( 2537 $class->SUPER::new (
2538 fontsize => 1,
2447 padding_x => 2, 2539 padding_x => 2,
2448 padding_y => 2, 2540 padding_y => 2,
2449 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2450 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2451 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2457} 2549}
2458 2550
2459sub size_request { 2551sub size_request {
2460 my ($self) = @_; 2552 my ($self) = @_;
2461 2553
2462 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2463} 2555}
2464 2556
2465sub toggle { 2557sub toggle {
2466 my ($self) = @_; 2558 my ($self) = @_;
2467 2559
2486sub _draw { 2578sub _draw {
2487 my ($self) = @_; 2579 my ($self) = @_;
2488 2580
2489 $self->SUPER::_draw; 2581 $self->SUPER::_draw;
2490 2582
2491 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2583 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2492 2584
2493 my ($w, $h) = @$self{qw(w h)}; 2585 my ($w, $h) = @$self{qw(w h)};
2494 2586
2495 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;
2496 2588
2503 glDisable GL_TEXTURE_2D; 2595 glDisable GL_TEXTURE_2D;
2504} 2596}
2505 2597
2506############################################################################# 2598#############################################################################
2507 2599
2508package CFPlus::UI::Image; 2600package DC::UI::Image;
2509 2601
2510our @ISA = CFPlus::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2511 2603
2512use CFPlus::OpenGL; 2604use DC::OpenGL;
2513 2605
2514our %texture_cache; 2606our %texture_cache;
2515 2607
2516sub new { 2608sub new {
2517 my $class = shift; 2609 my $class = shift;
2518 2610
2519 my $self = $class->SUPER::new ( 2611 my $self = $class->SUPER::new (
2520 can_events => 0, 2612 can_events => 0,
2613 scale => 1,
2521 @_, 2614 @_,
2522 ); 2615 );
2523 2616
2524 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2525 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2526 2619
2527 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2528 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2529 2622
2530 CFPlus::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2531 2624
2532 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2533 2626
2534 $self 2627 $self
2535} 2628}
2536 2629
2537sub STORABLE_freeze { 2630sub STORABLE_freeze {
2538 my ($self, $cloning) = @_; 2631 my ($self, $cloning) = @_;
2539 2632
2540 $self->{path} 2633 $self->{path}
2541 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";
2542 2635
2543 $self->{path} 2636 $self->{path}
2544} 2637}
2545 2638
2546sub STORABLE_attach { 2639sub STORABLE_attach {
2547 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2548 2641
2549 $self->new (path => $path) 2642 $self->new (path => $path)
2550} 2643}
2551 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2552sub size_request { 2652sub size_request {
2553 my ($self) = @_; 2653 my ($self) = @_;
2554 2654
2555 ($self->{tex}{w}, $self->{tex}{h}) 2655 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2556} 2656}
2557 2657
2558sub _draw { 2658sub _draw {
2559 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2560 2662
2561 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2562 2664
2563 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2564 2666
2570 } 2672 }
2571 2673
2572 glEnable GL_TEXTURE_2D; 2674 glEnable GL_TEXTURE_2D;
2573 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2574 2676
2575 $tex->draw_quad (0, 0, $w, $h); 2677 $tex->draw_quad_alpha (0, 0, $w, $h);
2576 2678
2577 glDisable GL_TEXTURE_2D; 2679 glDisable GL_TEXTURE_2D;
2578} 2680}
2579 2681
2580############################################################################# 2682#############################################################################
2581 2683
2582package CFPlus::UI::ImageButton; 2684package DC::UI::ImageButton;
2583 2685
2584our @ISA = CFPlus::UI::Image::; 2686our @ISA = DC::UI::Image::;
2585 2687
2586use CFPlus::OpenGL; 2688use DC::OpenGL;
2587
2588my %textures;
2589 2689
2590sub new { 2690sub new {
2591 my $class = shift; 2691 my $class = shift;
2592 2692
2593 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2594 padding_x => 4, 2694 padding_x => 4,
2595 padding_y => 4, 2695 padding_y => 4,
2596 fg => [1, 1, 1], 2696 fg => [1, 1, 1],
2597 active_fg => [0, 0, 1], 2697 active_fg => [0, 0, 1],
2598 can_hover => 1, 2698 can_hover => 1,
2599 align => 0, 2699 align => 0.5,
2600 valign => 0, 2700 valign => 0.5,
2601 can_events => 1, 2701 can_events => 1,
2602 @_ 2702 @_
2603 ); 2703 );
2604} 2704}
2605 2705
2706sub invoke_button_down {
2707 my ($self, $ev, $x, $y) = @_;
2708
2709 1
2710}
2711
2606sub invoke_button_up { 2712sub invoke_button_up {
2607 my ($self, $ev, $x, $y) = @_; 2713 my ($self, $ev, $x, $y) = @_;
2608 2714
2609 $self->emit ("activate") 2715 $self->emit ("activate")
2610 if $x >= 0 && $x < $self->{w} 2716 if $x >= 0 && $x < $self->{w}
2613 1 2719 1
2614} 2720}
2615 2721
2616############################################################################# 2722#############################################################################
2617 2723
2618package CFPlus::UI::VGauge; 2724package DC::UI::VGauge;
2619 2725
2620our @ISA = CFPlus::UI::Base::; 2726our @ISA = DC::UI::Base::;
2621 2727
2622use List::Util qw(min max); 2728use List::Util qw(min max);
2623 2729
2624use CFPlus::OpenGL; 2730use DC::OpenGL;
2625 2731
2626my %tex = ( 2732my %tex = (
2627 food => [ 2733 food => [
2628 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2629 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2630 ], 2736 ],
2631 grace => [ 2737 grace => [
2632 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2633 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/
2634 ], 2740 ],
2635 hp => [ 2741 hp => [
2636 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2637 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2638 ], 2744 ],
2639 mana => [ 2745 mana => [
2640 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2641 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/
2642 ], 2748 ],
2643); 2749);
2644 2750
2645# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
2748 glDisable GL_TEXTURE_2D; 2854 glDisable GL_TEXTURE_2D;
2749} 2855}
2750 2856
2751############################################################################# 2857#############################################################################
2752 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
2753package CFPlus::UI::Gauge; 3013package DC::UI::Gauge;
2754 3014
2755our @ISA = CFPlus::UI::VBox::; 3015our @ISA = DC::UI::VBox::;
2756 3016
2757sub new { 3017sub new {
2758 my ($class, %arg) = @_; 3018 my ($class, %arg) = @_;
2759 3019
2760 my $self = $class->SUPER::new ( 3020 my $self = $class->SUPER::new (
2762 can_hover => 1, 3022 can_hover => 1,
2763 can_events => 1, 3023 can_events => 1,
2764 %arg, 3024 %arg,
2765 ); 3025 );
2766 3026
2767 $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");
2768 $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);
2769 $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");
2770 3030
2771 $self 3031 $self
2772} 3032}
2773 3033
2774sub set_fontsize { 3034sub set_fontsize {
2795 $self->{value}->set_text ($val); 3055 $self->{value}->set_text ($val);
2796} 3056}
2797 3057
2798############################################################################# 3058#############################################################################
2799 3059
2800package CFPlus::UI::Slider; 3060package DC::UI::Slider;
2801 3061
2802use strict; 3062use common::sense;
2803 3063
2804use CFPlus::OpenGL; 3064use DC::OpenGL;
2805 3065
2806our @ISA = CFPlus::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
2807 3067
2808my @tex = 3068my @tex =
2809 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
2810 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
2811 3071
2812sub new { 3072sub new {
2813 my $class = shift; 3073 my $class = shift;
2814 3074
2851sub set_value { 3111sub set_value {
2852 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
2853 3113
2854 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2855 3115
2856 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
2857 3117
2858 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
2859
2860 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
2861 $value = $hi - $page if $value > $hi - $page;
2862 3120
2863 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2864 if $unit; 3122 if $unit;
2865 3123
2866 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2881 my ($self, $ev, $x, $y) = @_; 3139 my ($self, $ev, $x, $y) = @_;
2882 3140
2883 $self->SUPER::invoke_button_down ($ev, $x, $y); 3141 $self->SUPER::invoke_button_down ($ev, $x, $y);
2884 3142
2885 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3143 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2886 3144
2887 $self->invoke_mouse_motion ($ev, $x, $y) 3145 $self->invoke_mouse_motion ($ev, $x, $y);
3146
3147 1
2888} 3148}
2889 3149
2890sub invoke_mouse_motion { 3150sub invoke_mouse_motion {
2891 my ($self, $ev, $x, $y) = @_; 3151 my ($self, $ev, $x, $y) = @_;
2892 3152
2893 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
2894 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2895 3155
2896 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
2897 3157
2898 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
2899 3159
2900 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2901 } else { 3161 } else {
2902 return 0; 3162 return 0;
2903 } 3163 }
2908sub invoke_mouse_wheel { 3168sub invoke_mouse_wheel {
2909 my ($self, $ev) = @_; 3169 my ($self, $ev) = @_;
2910 3170
2911 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3171 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2912 3172
2913 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3173 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2914 3174
2915 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3175 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2916 3176
2917 ! ! $delta 3177 1
2918} 3178}
2919 3179
2920sub update { 3180sub update {
2921 my ($self) = @_; 3181 my ($self) = @_;
2922 3182
2928 my ($self) = @_; 3188 my ($self) = @_;
2929 3189
2930 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
2931 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
2932 3192
2933 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
2934 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
2935 3195
2936 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};
2937 3197
2938 $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;
2939 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2940 3200
2941 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
2949 3209
2950 glScale $self->{w}, $self->{h}; 3210 glScale $self->{w}, $self->{h};
2951 3211
2952 if ($self->{vertical}) { 3212 if ($self->{vertical}) {
2953 # draw a vertical slider like a rotated horizontal slider 3213 # draw a vertical slider like a rotated horizontal slider
2954 3214
2955 glTranslate 1, 0, 0; 3215 glTranslate 1, 0, 0;
2956 glRotate 90, 0, 0, 1; 3216 glRotate 90, 0, 0, 1;
2957 } 3217 }
2958 3218
2959 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3219 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2971 glDisable GL_TEXTURE_2D; 3231 glDisable GL_TEXTURE_2D;
2972} 3232}
2973 3233
2974############################################################################# 3234#############################################################################
2975 3235
2976package CFPlus::UI::ValSlider; 3236package DC::UI::ValSlider;
2977 3237
2978our @ISA = CFPlus::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
2979 3239
2980sub new { 3240sub new {
2981 my ($class, %arg) = @_; 3241 my ($class, %arg) = @_;
2982 3242
2983 my $range = delete $arg{range}; 3243 my $range = delete $arg{range};
2984 3244
2985 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
2986 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3246 slider => (new DC::UI::Slider expand => 1, range => $range),
2987 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3247 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2988 to_value => sub { shift }, 3248 to_value => sub { shift },
2989 from_value => sub { shift }, 3249 from_value => sub { shift },
2990 %arg, 3250 %arg,
2991 ); 3251 );
2992 3252
3012sub set_range { shift->{slider}->set_range (@_) } 3272sub set_range { shift->{slider}->set_range (@_) }
3013sub set_value { shift->{slider}->set_value (@_) } 3273sub set_value { shift->{slider}->set_value (@_) }
3014 3274
3015############################################################################# 3275#############################################################################
3016 3276
3017package CFPlus::UI::TextScroller; 3277package DC::UI::TextScroller;
3018 3278
3019our @ISA = CFPlus::UI::HBox::; 3279our @ISA = DC::UI::HBox::;
3020 3280
3021use CFPlus::OpenGL; 3281use DC::OpenGL;
3022 3282
3023sub new { 3283sub new {
3024 my $class = shift; 3284 my $class = shift;
3025 3285
3026 my $self = $class->SUPER::new ( 3286 my $self = $class->SUPER::new (
3027 fontsize => 1, 3287 fontsize => 1,
3028 can_events => 1, 3288 can_events => 1,
3029 indent => 0, 3289 indent => 0,
3030 #font => default_font 3290 #font => default_font
3031 @_, 3291 @_,
3032 3292
3033 layout => (new CFPlus::Layout), 3293 layout => (new DC::Layout),
3034 par => [],
3035 max_par => 0, 3294 max_par => 0,
3036 height => 0, 3295 height => 0,
3037 children => [ 3296 children => [
3038 (new CFPlus::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3039 (new CFPlus::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3040 ], 3299 ],
3041 ); 3300 );
3042 3301
3043 $self->{children}[1]->connect (changed => sub { $self->update }); 3302 $self->{children}[1]->connect (changed => sub { $self->update });
3044 3303
3304 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3305
3045 $self 3306 $self
3046} 3307}
3047 3308
3048sub set_fontsize { 3309sub set_fontsize {
3049 my ($self, $fontsize) = @_; 3310 my ($self, $fontsize) = @_;
3053} 3314}
3054 3315
3055sub size_request { 3316sub size_request {
3056 my ($self) = @_; 3317 my ($self) = @_;
3057 3318
3058 my ($empty, $slider) = @{ $self->{children} }; 3319 my ($empty, $slider) = $self->visible_children;
3059 3320
3060 local $self->{children} = [$empty, $slider]; 3321 local $self->{children} = [$empty, $slider];
3061 $self->SUPER::size_request 3322 $self->SUPER::size_request
3062} 3323}
3063 3324
3100 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3361 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3101 $layout->set_markup ($para->{markup}); 3362 $layout->set_markup ($para->{markup});
3102 3363
3103 $layout->set_shapes ( 3364 $layout->set_shapes (
3104 map 3365 map
3105 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3366 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3106 @{$para->{widget}} 3367 @{$para->{widget}}
3107 ); 3368 );
3108 3369
3109 $layout 3370 $layout
3110} 3371}
3227 $ROOT->on_post_alloc ($self => sub { 3488 $ROOT->on_post_alloc ($self => sub {
3228 $self->force_uptodate; 3489 $self->force_uptodate;
3229 3490
3230 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3491 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3231 3492
3232 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3493 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3233 glClearColor 0, 0, 0, 0; 3494 glClearColor 0, 0, 0, 0;
3234 glClear GL_COLOR_BUFFER_BIT; 3495 glClear GL_COLOR_BUFFER_BIT;
3235 3496
3236 package CFPlus::UI::Base; 3497 package DC::UI::Base;
3237 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3498 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3238 (0, 0, $self->{w}, $self->{h}); 3499 (0, 0, $self->{w}, $self->{h});
3239 3500
3240 my $top = int $self->{children}[1]{range}[0]; 3501 my $top = int $self->{children}[1]{range}[0];
3241 3502
3252 3513
3253 if ($y0 < $y + $h && $y < $y1) { 3514 if ($y0 < $y + $h && $y < $y1) {
3254 my $layout = $self->get_layout ($para); 3515 my $layout = $self->get_layout ($para);
3255 3516
3256 $layout->render ($para->{indent}, $y - $y0); 3517 $layout->render ($para->{indent}, $y - $y0);
3518 $layout->draw;
3257 3519
3258 if (my @w = @{ $para->{widget} }) { 3520 if (my @w = @{ $para->{widget} }) {
3259 my @s = $layout->get_shapes; 3521 my @s = $layout->get_shapes;
3260 3522
3261 for (@w) { 3523 for (@w) {
3299 $self->{children}[1]->draw; 3561 $self->{children}[1]->draw;
3300} 3562}
3301 3563
3302############################################################################# 3564#############################################################################
3303 3565
3304package CFPlus::UI::Animator; 3566package DC::UI::Animator;
3305 3567
3306use CFPlus::OpenGL; 3568use DC::OpenGL;
3307 3569
3308our @ISA = CFPlus::UI::Bin::; 3570our @ISA = DC::UI::Bin::;
3309 3571
3310sub moveto { 3572sub moveto {
3311 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3312 3574
3313 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3314 $self->{speed} = 0.001; 3576 $self->{speed} = 0.001;
3315 $self->{time} = 1; 3577 $self->{time} = 1;
3316 3578
3317 ::animation_start $self; 3579 ::animation_start $self;
3318} 3580}
3319 3581
3320sub animate { 3582sub animate {
3321 my ($self, $interval) = @_; 3583 my ($self, $interval) = @_;
3325 $self->{time} = 0; 3587 $self->{time} = 0;
3326 ::animation_stop $self; 3588 ::animation_stop $self;
3327 } 3589 }
3328 3590
3329 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; 3591 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3330 3592
3331 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); 3593 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3332 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); 3594 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3333} 3595}
3334 3596
3335sub _draw { 3597sub _draw {
3341 glPopMatrix; 3603 glPopMatrix;
3342} 3604}
3343 3605
3344############################################################################# 3606#############################################################################
3345 3607
3346package CFPlus::UI::Flopper; 3608package DC::UI::Flopper;
3347 3609
3348our @ISA = CFPlus::UI::Button::; 3610our @ISA = DC::UI::Button::;
3349 3611
3350sub new { 3612sub new {
3351 my $class = shift; 3613 my $class = shift;
3352 3614
3353 my $self = $class->SUPER::new ( 3615 my $self = $class->SUPER::new (
3365 $self->{other}->toggle_visibility; 3627 $self->{other}->toggle_visibility;
3366} 3628}
3367 3629
3368############################################################################# 3630#############################################################################
3369 3631
3370package CFPlus::UI::Tooltip; 3632package DC::UI::Tooltip;
3371 3633
3372our @ISA = CFPlus::UI::Bin::; 3634our @ISA = DC::UI::Bin::;
3373 3635
3374use CFPlus::OpenGL; 3636use DC::OpenGL;
3375 3637
3376sub new { 3638sub new {
3377 my $class = shift; 3639 my $class = shift;
3378 3640
3379 $class->SUPER::new ( 3641 $class->SUPER::new (
3380 @_, 3642 @_,
3381 can_events => 0, 3643 can_events => 0,
3382 ) 3644 )
3383} 3645}
3384 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
3385sub set_tooltip_from { 3699sub set_tooltip_from {
3386 my ($self, $widget) = @_; 3700 my ($self, $widget) = @_;
3387 3701
3388 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3389 if $widget->{tooltip} =~ /^#(.*)$/;
3390
3391 my $tooltip = $widget->{tooltip}; 3702 my $tip = $widget->{tooltip};
3703 $tip = $tip->($widget) if "CODE" eq ref $tip;
3392 3704
3393 if ($ENV{CFPLUS_DEBUG} & 2) {
3394 $tooltip .= "\n\n" . (ref $widget) . "\n"
3395 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3396 . "req $widget->{req_w} $widget->{req_h}\n"
3397 . "visible $widget->{visible}";
3398 }
3399
3400 $tooltip =~ s/^\n+//;
3401 $tooltip =~ s/\n+$//;
3402
3403 $self->add (new CFPlus::UI::Label 3705 $self->add (new DC::UI::Label
3404 markup => $tooltip, 3706 fg => $DC::THEME{tooltip_fg},
3405 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3707 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3708 align => 0,
3406 fontsize => 0.8, 3709 fontsize => 0.8,
3407 style => 1, # FLAG_INVERSE 3710 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3408 ellipsise => 0, 3711 ellipsise => 0,
3409 font => ($widget->{tooltip_font} || $::FONT_PROP), 3712 font => ($widget->{tooltip_font} || $::FONT_PROP),
3410 ); 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 };
3411} 3727}
3412 3728
3413sub size_request { 3729sub size_request {
3414 my ($self) = @_; 3730 my ($self) = @_;
3415 3731
3447} 3763}
3448 3764
3449sub _draw { 3765sub _draw {
3450 my ($self) = @_; 3766 my ($self) = @_;
3451 3767
3452 glTranslate 0.375, 0.375;
3453
3454 my ($w, $h) = @$self{qw(w h)}; 3768 my ($w, $h) = @$self{qw(w h)};
3455 3769
3456 glColor 1, 0.8, 0.4; 3770 glColor @{ $DC::THEME{tooltip_bg} };
3457 glBegin GL_QUADS; 3771 glRect 0, 0, $w, $h;
3458 glVertex 0 , 0; 3772
3459 glVertex 0 , $h; 3773 glColor @{ $DC::THEME{tooltip_border} };
3460 glVertex $w, $h; 3774 glRect_lineloop .5, .5, $w + .5, $h + .5;
3461 glVertex $w, 0; 3775
3462 glEnd; 3776 glTranslate 2, 2;
3463
3464 glColor 0, 0, 0;
3465 glBegin GL_LINE_LOOP;
3466 glVertex 0 , 0;
3467 glVertex 0 , $h;
3468 glVertex $w, $h;
3469 glVertex $w, 0;
3470 glEnd;
3471
3472 glTranslate 2 - 0.375, 2 - 0.375;
3473 3777
3474 $self->SUPER::_draw; 3778 $self->SUPER::_draw;
3475} 3779}
3476 3780
3477############################################################################# 3781#############################################################################
3478 3782
3479package CFPlus::UI::Face; 3783package DC::UI::Face;
3480 3784
3481our @ISA = CFPlus::UI::DrawBG::; 3785our @ISA = DC::UI::DrawBG::;
3482 3786
3483use CFPlus::OpenGL; 3787use DC::OpenGL;
3484 3788
3485sub new { 3789sub new {
3486 my $class = shift; 3790 my $class = shift;
3487 3791
3488 my $self = $class->SUPER::new ( 3792 my $self = $class->SUPER::new (
3491 aspect => 1, 3795 aspect => 1,
3492 can_events => 0, 3796 can_events => 0,
3493 @_, 3797 @_,
3494 ); 3798 );
3495 3799
3496 if ($self->{anim} && $self->{animspeed}) {
3497 CFPlus::weaken (my $widget = $self);
3498
3499 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3500 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3501 $self->{timer} = Event->timer (
3502 parked => 1,
3503 cb => sub {
3504 return unless $::CONN && $widget;
3505
3506 ++$widget->{frame};
3507 $widget->update_face;
3508 $widget->update;
3509
3510 $widget->update_timer;
3511 },
3512 );
3513
3514 $self->update_face;
3515 $self->update_timer; 3800 $self->update_anim;
3516 } 3801
3517
3518 $self 3802 $self
3519} 3803}
3520 3804
3521sub update_timer { 3805sub update_timer {
3522 my ($self) = @_; 3806 my ($self) = @_;
3523 3807
3524 return unless $self->{timer}; 3808 return unless $self->{timer};
3525 3809
3526 if ($self->{visible}) { 3810 if ($self->{visible}) {
3527 $self->{timer}->at (
3528 $self->{anim_start}
3529 + $self->{animspeed}
3530 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3531 );
3532 $self->{timer}->start; 3811 $self->{timer}->start;
3533 } else { 3812 } else {
3534 $self->{timer}->stop; 3813 $self->{timer}->stop;
3535 } 3814 }
3536} 3815}
3537 3816
3538sub update_face { 3817sub update_face {
3539 my ($self) = @_; 3818 my ($self) = @_;
3540 3819
3541 return unless $::CONN;
3542
3543 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3544 if ($anim && @$anim) {
3545 delete $self->{wait_face};
3546 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3547 }
3548 }
3549}
3550
3551sub size_request {
3552 my ($self) = @_;
3553
3554 if ($::CONN) { 3820 if ($::CONN) {
3555 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3821 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3556 if (my $tex = $::CONN->{texture}[$faceid]) { 3822 if ($anim && @$anim) {
3557 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3823 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3558 } else { 3824 delete $self->{face_change_cb};
3559 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3825
3560 $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 }
3561 }); 3830 }
3562 } 3831 }
3563 } 3832 }
3564 } 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 }
3565 3880
3566 ($self->{size_w} || 8, $self->{size_h} || 8) 3881 ($self->{size_w} || 8, $self->{size_h} || 8)
3567} 3882}
3568 3883
3569sub update { 3884sub update {
3572 return unless $self->{visible}; 3887 return unless $self->{visible};
3573 3888
3574 $self->SUPER::update; 3889 $self->SUPER::update;
3575} 3890}
3576 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
3577sub invoke_visibility_change { 3913sub invoke_visibility_change {
3578 my ($self) = @_; 3914 my ($self) = @_;
3579 3915
3580 $self->update_timer; 3916 $self->update_timer;
3581 3917
3583} 3919}
3584 3920
3585sub _draw { 3921sub _draw {
3586 my ($self) = @_; 3922 my ($self) = @_;
3587 3923
3588 return unless $::CONN;
3589
3590 $self->SUPER::_draw; 3924 $self->SUPER::_draw;
3591 3925
3592 my $faceid = $::CONN->{faceid}[$self->{face}] 3926 if (my $tex = $self->{tex}) {
3593 or return;
3594
3595 my $tex = $::CONN->{texture}[$faceid];
3596
3597 if ($tex) {
3598 glEnable GL_TEXTURE_2D; 3927 glEnable GL_TEXTURE_2D;
3599 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3928 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3600 glColor 0, 0, 0, 1; 3929 glColor 0, 0, 0, 1;
3601 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3930 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3602 glDisable GL_TEXTURE_2D; 3931 glDisable GL_TEXTURE_2D;
3612 $self->SUPER::destroy; 3941 $self->SUPER::destroy;
3613} 3942}
3614 3943
3615############################################################################# 3944#############################################################################
3616 3945
3617package CFPlus::UI::Buttonbar; 3946package DC::UI::Buttonbar;
3618 3947
3619our @ISA = CFPlus::UI::HBox::; 3948our @ISA = DC::UI::HBox::;
3620 3949
3621# TODO: should actually wrap buttons and other goodies. 3950# TODO: should actually wrap buttons and other goodies.
3622 3951
3623############################################################################# 3952#############################################################################
3624 3953
3625package CFPlus::UI::Menu; 3954package DC::UI::Menu;
3626 3955
3627our @ISA = CFPlus::UI::Toplevel::; 3956our @ISA = DC::UI::Toplevel::;
3628 3957
3629use CFPlus::OpenGL; 3958use DC::OpenGL;
3630 3959
3631sub new { 3960sub new {
3632 my $class = shift; 3961 my $class = shift;
3633 3962
3634 my $self = $class->SUPER::new ( 3963 my $self = $class->SUPER::new (
3635 items => [], 3964 items => [],
3636 z => 100, 3965 z => 100,
3637 @_, 3966 @_,
3638 ); 3967 );
3639 3968
3640 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3969 $self->add ($self->{vbox} = new DC::UI::VBox);
3641 3970
3642 for my $item (@{ $self->{items} }) { 3971 for my $item (@{ $self->{items} }) {
3643 my ($widget, $cb, $tooltip) = @$item; 3972 my ($widget, $cb, $tooltip) = @$item;
3644 3973
3645 # handle various types of items, only text for now 3974 # handle various types of items, only text for now
3646 if (!ref $widget) { 3975 if (!ref $widget) {
3647 if ($widget =~ /\t/) { 3976 if ($widget =~ /\t/) {
3648 my ($left, $right) = split /\t/, $widget, 2; 3977 my ($left, $right) = split /\t/, $widget, 2;
3649 3978
3650 $widget = new CFPlus::UI::HBox 3979 $widget = new DC::UI::HBox
3651 can_hover => 1, 3980 can_hover => 1,
3652 can_events => 1, 3981 can_events => 1,
3653 tooltip => $tooltip, 3982 tooltip => $tooltip,
3654 children => [ 3983 children => [
3655 (new CFPlus::UI::Label markup => $left, expand => 1), 3984 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3656 (new CFPlus::UI::Label markup => $right, align => +1), 3985 (new DC::UI::Label markup => $right, align => 1),
3657 ], 3986 ],
3658 ; 3987 ;
3659 3988
3660 } else { 3989 } else {
3661 $widget = new CFPlus::UI::Label 3990 $widget = new DC::UI::Label
3662 can_hover => 1, 3991 can_hover => 1,
3663 can_events => 1, 3992 can_events => 1,
3993 align => 0,
3664 markup => $widget, 3994 markup => $widget,
3665 tooltip => $tooltip; 3995 tooltip => $tooltip;
3666 } 3996 }
3667 } 3997 }
3668 3998
3683 # maybe save $GRAB? must be careful about events... 4013 # maybe save $GRAB? must be careful about events...
3684 $GRAB = $self; 4014 $GRAB = $self;
3685 $self->{button} = $ev->{button}; 4015 $self->{button} = $ev->{button};
3686 4016
3687 $self->show; 4017 $self->show;
3688 $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
3689} 4027}
3690 4028
3691sub invoke_mouse_motion { 4029sub invoke_mouse_motion {
3692 my ($self, $ev, $x, $y) = @_; 4030 my ($self, $ev, $x, $y) = @_;
3693 4031
3714 1 4052 1
3715} 4053}
3716 4054
3717############################################################################# 4055#############################################################################
3718 4056
3719package CFPlus::UI::Multiplexer; 4057package DC::UI::Multiplexer;
3720 4058
3721our @ISA = CFPlus::UI::Container::; 4059our @ISA = DC::UI::Container::;
3722 4060
3723sub new { 4061sub new {
3724 my $class = shift; 4062 my $class = shift;
3725 4063
3726 my $self = $class->SUPER::new ( 4064 my $self = $class->SUPER::new (
3727 @_, 4065 @_,
3728 ); 4066 );
3729 4067
3730 $self->{current} = $self->{children}[0] 4068 $self->set_current_page (0);
3731 if @{ $self->{children} };
3732 4069
3733 $self 4070 $self
3734} 4071}
3735 4072
3736sub add { 4073sub add {
3737 my ($self, @widgets) = @_; 4074 my ($self, @widgets) = @_;
3738 4075
3739 $self->SUPER::add (@widgets); 4076 $self->SUPER::add (@widgets);
3740 4077
3741 $self->{current} = $self->{children}[0] 4078 $self->set_current_page (0)
3742 if @{ $self->{children} }; 4079 if @widgets == @{ $self->{children} };
3743} 4080}
3744 4081
3745sub get_current_page { 4082sub get_current_page {
3746 my ($self) = @_; 4083 my ($self) = @_;
3747 4084
3753 4090
3754 my $widget = ref $page_or_widget 4091 my $widget = ref $page_or_widget
3755 ? $page_or_widget 4092 ? $page_or_widget
3756 : $self->{children}[$page_or_widget]; 4093 : $self->{children}[$page_or_widget];
3757 4094
4095 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4096
3758 $self->{current} = $widget; 4097 if (($self->{current} = $widget)) {
4098 $self->{current}->set_visible if $self->{current} && $self->{visible};
3759 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4099 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3760 4100
3761 $self->emit (page_changed => $self->{current}); 4101 $self->emit (page_changed => $self->{current});
4102 }
3762 4103
3763 $self->realloc; 4104 $self->realloc;
3764} 4105}
3765 4106
3766sub visible_children { 4107sub visible_children {
3767 $_[0]{current} 4108 $_[0]{current} || ()
3768} 4109}
3769 4110
3770sub size_request { 4111sub size_request {
3771 my ($self) = @_; 4112 my ($self) = @_;
3772 4113
4114 $self->{current}
3773 $self->{current}->size_request 4115 ? $self->{current}->size_request
4116 : (0, 0)
3774} 4117}
3775 4118
3776sub invoke_size_allocate { 4119sub invoke_size_allocate {
3777 my ($self, $w, $h) = @_; 4120 my ($self, $w, $h) = @_;
3778 4121
3779 $self->{current}->configure (0, 0, $w, $h); 4122 $self->{current}->configure (0, 0, $w, $h)
4123 if $self->{current};
3780 4124
3781 1 4125 1
3782} 4126}
3783 4127
3784sub _draw { 4128sub _draw {
3785 my ($self) = @_; 4129 my ($self) = @_;
3786 4130
3787 $self->{current}->draw; 4131 $self->{current}->draw
4132 if $self->{current};
3788} 4133}
3789 4134
3790############################################################################# 4135#############################################################################
3791 4136
3792package CFPlus::UI::Notebook; 4137package DC::UI::Notebook;
3793 4138
4139use DC::OpenGL;
4140
3794our @ISA = CFPlus::UI::VBox::; 4141our @ISA = DC::UI::VBox::;
3795 4142
3796sub new { 4143sub new {
3797 my $class = shift; 4144 my $class = shift;
3798 4145
3799 my $self = $class->SUPER::new ( 4146 my $self = $class->SUPER::new (
3800 buttonbar => (new CFPlus::UI::Buttonbar), 4147 buttonbar => (new DC::UI::Buttonbar),
3801 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4148 multiplexer => (new DC::UI::Multiplexer expand => 1),
4149 active_outline => [.7, .7, 0.2],
3802 # filter => # will be put between multiplexer and $self 4150 # filter => # will be put between multiplexer and $self
3803 @_, 4151 @_,
3804 ); 4152 );
3805 4153
3806 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4154 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3807 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4155 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3808 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
3809 $self 4191 $self
3810} 4192}
3811 4193
3812sub 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 {
3813 my ($self, $title, $widget, $tooltip) = @_; 4224 my ($self, $title, $widget, $tooltip) = @_;
3814 4225
3815 CFPlus::weaken $self; 4226 $title = [$title, $tooltip] unless ref $title;
4227 $widget->{c_tab} = $title;
3816 4228
3817 $self->{buttonbar}->add (new CFPlus::UI::Button
3818 markup => $title,
3819 tooltip => $tooltip,
3820 on_activate => sub { $self->set_current_page ($widget) },
3821 );
3822
3823 $self->{multiplexer}->add ($widget); 4229 $self->add ($widget);
3824} 4230}
3825 4231
3826sub get_current_page { 4232sub get_current_page {
3827 my ($self) = @_; 4233 my ($self) = @_;
3828 4234
3834 4240
3835 $self->{multiplexer}->set_current_page ($page); 4241 $self->{multiplexer}->set_current_page ($page);
3836 $self->emit (page_changed => $self->{multiplexer}{current}); 4242 $self->emit (page_changed => $self->{multiplexer}{current});
3837} 4243}
3838 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
3839############################################################################# 4265#############################################################################
3840 4266
3841package CFPlus::UI::Selector; 4267package DC::UI::Selector;
3842 4268
3843use utf8; 4269use utf8;
3844 4270
3845our @ISA = CFPlus::UI::Button::; 4271our @ISA = DC::UI::Button::;
3846 4272
3847sub new { 4273sub new {
3848 my $class = shift; 4274 my $class = shift;
3849 4275
3850 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
3867 my ($value, $title, $tooltip) = @$_; 4293 my ($value, $title, $tooltip) = @$_;
3868 4294
3869 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4295 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3870 } 4296 }
3871 4297
3872 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4298 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3873} 4299}
3874 4300
3875sub _set_value { 4301sub _set_value {
3876 my ($self, $value) = @_; 4302 my ($self, $value) = @_;
3877 4303
3878 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4304 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4305 $item ||= $self->{options}[0]
3879 or return; 4306 or return;
3880 4307
3881 $self->{value} = $item->[0]; 4308 $self->{value} = $item->[0];
3882 $self->set_markup ("$item->[1] ⇓"); 4309 $self->set_markup ("$item->[1] ⇓");
3883 $self->set_tooltip ($item->[2]); 4310# $self->set_tooltip ($item->[2]);
3884} 4311}
3885 4312
3886sub set_value { 4313sub set_value {
3887 my ($self, $value) = @_; 4314 my ($self, $value) = @_;
3888 4315
3890 4317
3891 $self->_set_value ($value); 4318 $self->_set_value ($value);
3892 $self->emit (changed => $value); 4319 $self->emit (changed => $value);
3893} 4320}
3894 4321
4322sub set_options {
4323 my ($self, $options) = @_;
4324
4325 $self->{options} = $options;
4326 $self->_set_value ($self->{value});
4327}
4328
3895############################################################################# 4329#############################################################################
3896 4330
3897package CFPlus::UI::Statusbox; 4331package DC::UI::Statusbox;
3898 4332
3899our @ISA = CFPlus::UI::VBox::; 4333our @ISA = DC::UI::VBox::;
3900 4334
3901sub new { 4335sub new {
3902 my $class = shift; 4336 my $class = shift;
3903 4337
3904 my $self = $class->SUPER::new ( 4338 my $self = $class->SUPER::new (
3905 fontsize => 0.8, 4339 fontsize => 0.8,
3906 @_, 4340 @_,
3907 ); 4341 );
3908 4342
3909 CFPlus::weaken (my $this = $self); 4343 DC::weaken (my $this = $self);
3910 4344
3911 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4345 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3912 4346
3913 $self 4347 $self
3914} 4348}
3915 4349
3916sub reorder { 4350sub reorder {
3917 my ($self) = @_; 4351 my ($self) = @_;
3918 my $NOW = Time::HiRes::time; 4352 my $NOW = AE::time;
3919 4353
3920 # freeze display when hovering over any label 4354 # freeze display when hovering over any label
3921 return if $CFPlus::UI::TOOLTIP->{owner} 4355 return if $DC::UI::TOOLTIP->{owner}
3922 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4356 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3923 values %{ $self->{item} }; 4357 values %{ $self->{item} };
3924 4358
3925 while (my ($k, $v) = each %{ $self->{item} }) { 4359 while (my ($k, $v) = each %{ $self->{item} }) {
3926 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4360 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3927 } 4361 }
4362
4363 $self->{timer}->set (1, 1);
3928 4364
3929 my @widgets; 4365 my @widgets;
3930 4366
3931 my @items = sort { 4367 my @items = sort {
3932 $a->{pri} <=> $b->{pri} 4368 $a->{pri} <=> $b->{pri}
3933 or $b->{id} <=> $a->{id} 4369 or $b->{id} <=> $a->{id}
3934 } values %{ $self->{item} }; 4370 } values %{ $self->{item} };
3935
3936 $self->{timer}->interval (1);
3937 4371
3938 my $count = 10 + 1; 4372 my $count = 10 + 1;
3939 for my $item (@items) { 4373 for my $item (@items) {
3940 last unless --$count; 4374 last unless --$count;
3941 4375
3948 for ($short) { 4382 for ($short) {
3949 s/^\s+//; 4383 s/^\s+//;
3950 s/\s+/ /g; 4384 s/\s+/ /g;
3951 } 4385 }
3952 4386
3953 new CFPlus::UI::Label 4387 new DC::UI::Label
3954 markup => $short, 4388 markup => $short,
3955 tooltip => $item->{tooltip}, 4389 tooltip => $item->{tooltip},
3956 tooltip_font => $::FONT_PROP, 4390 tooltip_font => $::FONT_PROP,
3957 tooltip_width => 0.67, 4391 tooltip_width => 0.67,
3958 fontsize => $item->{fontsize} || $self->{fontsize}, 4392 fontsize => $item->{fontsize} || $self->{fontsize},
3959 max_w => $::WIDTH * 0.44, 4393 max_w => $::WIDTH * 0.44,
4394 align => 0,
3960 fg => [@{ $item->{fg} }], 4395 fg => [@{ $item->{fg} }],
3961 can_events => 1, 4396 can_events => 1,
3962 can_hover => 1 4397 can_hover => 1
3963 }; 4398 };
3964 4399
3965 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4400 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3966 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4401 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3967 $label->update; 4402 $label->update;
3968 $label->set_max_size (undef, $label->{req_h} * $diff) 4403 $label->set_max_size (undef, $label->{req_h} * $diff)
3969 if $diff < 1; 4404 if $diff < 1;
3970 $self->{timer}->interval (1/30); 4405 $self->{timer}->set (1/30, 1/30);
3971 } else { 4406 } else {
3972 $label->{fg}[3] = $item->{fg}[3] || 1; 4407 $label->{fg}[3] = $item->{fg}[3] || 1;
3973 } 4408 }
3974 4409
3975 push @widgets, $label; 4410 push @widgets, $label;
3976 } 4411 }
4412
4413 my $hash = join ",", @widgets;
4414 return if $hash eq $self->{last_widget_hash};
4415 $self->{last_widget_hash} = $hash;
3977 4416
3978 $self->clear; 4417 $self->clear;
3979 $self->SUPER::add (reverse @widgets); 4418 $self->SUPER::add (reverse @widgets);
3980} 4419}
3981 4420
4017 $ROOT->on_refresh (reorder => sub { 4456 $ROOT->on_refresh (reorder => sub {
4018 $self->reorder; 4457 $self->reorder;
4019 }); 4458 });
4020} 4459}
4021 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
4022sub reconfigure { 4471sub reconfigure {
4023 my ($self) = @_; 4472 my ($self) = @_;
4024 4473
4025 delete $_->{label} 4474 delete $_->{label}
4026 for values %{ $self->{item} || {} }; 4475 for values %{ $self->{item} || {} };
4037 $self->SUPER::destroy; 4486 $self->SUPER::destroy;
4038} 4487}
4039 4488
4040############################################################################# 4489#############################################################################
4041 4490
4042package CFPlus::UI::Root; 4491package DC::UI::Root;
4043 4492
4044our @ISA = CFPlus::UI::Container::; 4493our @ISA = DC::UI::Container::;
4045 4494
4046use List::Util qw(min max); 4495use List::Util qw(min max);
4047 4496
4048use CFPlus::OpenGL; 4497use DC::OpenGL;
4049 4498
4050sub new { 4499sub new {
4051 my $class = shift; 4500 my $class = shift;
4052 4501
4053 my $self = $class->SUPER::new ( 4502 my $self = $class->SUPER::new (
4054 visible => 1, 4503 visible => 1,
4055 @_, 4504 @_,
4056 ); 4505 );
4057 4506
4058 CFPlus::weaken ($self->{root} = $self); 4507 DC::weaken ($self->{root} = $self);
4059 4508
4060 $self 4509 $self
4061} 4510}
4062 4511
4063sub size_request { 4512sub size_request {
4111} 4560}
4112 4561
4113sub update { 4562sub update {
4114 my ($self) = @_; 4563 my ($self) = @_;
4115 4564
4116 $::WANT_REFRESH++; 4565 $::WANT_REFRESH = 1;
4117} 4566}
4118 4567
4119sub add { 4568sub add {
4120 my ($self, @children) = @_; 4569 my ($self, @children) = @_;
4121 4570
4158 while ($self->{refresh_hook}) { 4607 while ($self->{refresh_hook}) {
4159 $_->() 4608 $_->()
4160 for values %{delete $self->{refresh_hook}}; 4609 for values %{delete $self->{refresh_hook}};
4161 } 4610 }
4162 4611
4163 if ($self->{realloc}) { 4612 while ($self->{realloc}) {
4164 my %queue; 4613 my %queue;
4165 my @queue; 4614 my @queue;
4166 my $widget; 4615 my $widget;
4167 4616
4168 outer: 4617 outer:
4180 while () { 4629 while () {
4181 @queue or last outer; 4630 @queue or last outer;
4182 4631
4183 $widget = pop @{ $queue[-1] || [] } 4632 $widget = pop @{ $queue[-1] || [] }
4184 and last; 4633 and last;
4185 4634
4186 pop @queue; 4635 pop @queue;
4187 } 4636 }
4188 4637
4189 delete $queue{$widget+0}; 4638 delete $queue{$widget+0};
4190 4639
4191 my ($w, $h) = $widget->size_request; 4640 my ($w, $h) = $widget->size_request;
4192 4641
4193 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4642 $w += $widget->{padding_x} * 2;
4194 $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;
4195 4647
4196 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4648 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4197 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4649 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4198 4650
4199 $w = $widget->{force_w} if exists $widget->{force_w}; 4651 $w = $widget->{force_w} if exists $widget->{force_w};
4215 } 4667 }
4216 } 4668 }
4217 4669
4218 delete $self->{realloc}{$widget+0}; 4670 delete $self->{realloc}{$widget+0};
4219 } 4671 }
4220 }
4221 4672
4222 while (my $size_alloc = delete $self->{size_alloc}) { 4673 while (my $size_alloc = delete $self->{size_alloc}) {
4223 my @queue = sort { $b->{visible} <=> $a->{visible} } 4674 my @queue = sort { $a->{visible} <=> $b->{visible} }
4224 values %$size_alloc; 4675 values %$size_alloc;
4225 4676
4226 while () { 4677 while () {
4227 my $widget = pop @queue || last; 4678 my $widget = pop @queue || last;
4228 4679
4229 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4680 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4230 4681
4231 $w = 0 if $w < 0;
4232 $h = 0 if $h < 0;
4233
4234 $w = max $widget->{min_w}, $w; 4682 $w = max $widget->{min_w}, $w;
4235 $h = max $widget->{min_h}, $h; 4683 $h = max $widget->{min_h}, $h;
4236 4684
4237# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4685# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4238# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4686# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4239 4687
4240 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4688 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4241 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4689 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4242 4690
4243 $w = int $w + 0.5; 4691 $w = int $w + 0.5;
4244 $h = int $h + 0.5; 4692 $h = int $h + 0.5;
4245 4693
4246 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}) {
4247 $widget->{old_w} = $widget->{w}; 4695 $widget->{old_w} = $widget->{w};
4248 $widget->{old_h} = $widget->{h}; 4696 $widget->{old_h} = $widget->{h};
4249 4697
4250 $widget->{w} = $w; 4698 $widget->{w} = $w;
4251 $widget->{h} = $h; 4699 $widget->{h} = $h;
4252 4700
4253 $widget->emit (size_allocate => $w, $h); 4701 $widget->emit (size_allocate => $w, $h);
4702 }
4254 } 4703 }
4255 } 4704 }
4256 } 4705 }
4257 4706
4258 while ($self->{post_alloc_hook}) { 4707 while ($self->{post_alloc_hook}) {
4269 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4718 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4270 glMatrixMode GL_MODELVIEW; 4719 glMatrixMode GL_MODELVIEW;
4271 glLoadIdentity; 4720 glLoadIdentity;
4272 4721
4273 { 4722 {
4274 package CFPlus::UI::Base; 4723 package DC::UI::Base;
4275 4724
4276 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4725 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4277 (0, 0, $self->{w}, $self->{h}); 4726 (0, 0, $self->{w}, $self->{h});
4278 4727
4279 $self->_draw; 4728 $self->_draw;
4280 } 4729 }
4281} 4730}
4282 4731
4283############################################################################# 4732#############################################################################
4284 4733
4285package CFPlus::UI; 4734package DC::UI;
4286 4735
4287$ROOT = new CFPlus::UI::Root; 4736$ROOT = new DC::UI::Root;
4288$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4737$TOOLTIP = new DC::UI::Tooltip z => 900;
4289 4738
42901 47391
4291

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines