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.366 by root, Mon Apr 23 19:17:41 2007 UTC vs.
Revision 1.480 by root, Sat Apr 3 02:58:25 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines