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.73 by root, Tue Apr 11 18:06:53 2006 UTC vs.
Revision 1.77 by root, Tue Apr 11 22:14:13 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6use List::Util ();
6 7
7use CFClient; 8use CFClient;
8 9
9our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
10 11
42 43
43 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
44 45
45 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
46 47
47 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
48 49
49 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
50 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
51 $grab->update if $grab; 52 $grab->update if $grab;
52 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
82 83
83 bless { 84 bless {
84 x => 0, 85 x => 0,
85 y => 0, 86 y => 0,
86 z => 0, 87 z => 0,
88 w => -1,
89 h => -1,
87 @_ 90 @_
88 }, $class 91 }, $class
89} 92}
90 93
91sub move { 94sub move {
102sub size_request { 105sub size_request {
103 require Carp; 106 require Carp;
104 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
105} 108}
106 109
107sub size_allocate { 110sub _size_allocate {
108 my ($self, $w, $h) = @_; 111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
109 117
110 $self->{w} = $w; 118 $self->{w} = $w;
111 $self->{h} = $h; 119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
112} 128}
113 129
114# translate global koordinates to local coordinate system 130# translate global koordinates to local coordinate system
115sub translate { 131sub translate {
116 my ($self, $x, $y) = @_; 132 my ($self, $x, $y) = @_;
165 glPopMatrix; 181 glPopMatrix;
166 182
167 if ($self == $HOVER) { 183 if ($self == $HOVER) {
168 my ($x, $y) = @$self{qw(x y)}; 184 my ($x, $y) = @$self{qw(x y)};
169 185
170 glColor 1, 1, 1, 0.1; 186 glColor 0, 0, 1, 0.2;
171 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
172 glBegin GL_QUADS; 189 glBegin GL_QUADS;
173 glVertex $x , $y; 190 glVertex $x , $y;
174 glVertex $x + $self->{w}, $y; 191 glVertex $x + $self->{w}, $y;
175 glVertex $x + $self->{w}, $y + $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
176 glVertex $x , $y + $self->{h}; 193 glVertex $x , $y + $self->{h};
258 my $class = shift; 275 my $class = shift;
259 276
260 # range [value, low, high, page] 277 # range [value, low, high, page]
261 278
262 $class->SUPER::new ( 279 $class->SUPER::new (
263 bg => [0, 0, 0, 0.4], 280 bg => [0, 0, 0, 0.2],
264 active_bg => [1, 1, 1], 281 active_bg => [1, 1, 1],
265 @_ 282 @_
266 ) 283 )
267} 284}
268 285
269sub _draw { 286sub _draw {
270 my ($self) = @_; 287 my ($self) = @_;
271 288
272 my ($w, $h) = @$self{qw(w h)}; 289 my ($w, $h) = @$self{qw(w h)};
273 290
291 glEnable GL_BLEND;
292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
274 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 293 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
294
275 glBegin GL_QUADS; 295 glBegin GL_QUADS;
276 glVertex 0 , 0; 296 glVertex 0 , 0;
277 glVertex 0 , $h; 297 glVertex 0 , $h;
278 glVertex $w, $h; 298 glVertex $w, $h;
279 glVertex $w, 0; 299 glVertex $w, 0;
280 glEnd; 300 glEnd;
301
302 glDisable GL_BLEND;
281} 303}
282 304
283############################################################################# 305#############################################################################
284 306
285package CFClient::UI::Empty; 307package CFClient::UI::Empty;
308 330
309 $self 331 $self
310} 332}
311 333
312sub add { 334sub add {
313 my ($self, $chld, $expand) = @_; 335 my ($self, $chld) = @_;
314 336
315 $chld->{expand} = $expand;
316 $chld->set_parent ($self); 337 $chld->set_parent ($self);
317 338
318 $self->{children} = [ 339 $self->{children} = [
319 sort { $a->{z} <=> $b->{z} } 340 sort { $a->{z} <=> $b->{z} }
320 @{$self->{children}}, $chld 341 @{$self->{children}}, $chld
321 ]; 342 ];
322 343
323 $self->size_allocate ($self->{w}, $self->{h}) 344 $self->{w} = $self->{h} = -1;
324 if $self->{w}; #TODO: check for "realised state" 345 $self->update;
325} 346}
326 347
327sub remove { 348sub remove {
328 my ($self, $widget) = @_; 349 my ($self, $widget) = @_;
329 350
330 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 351 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
331 352
332 $self->size_allocate ($self->{w}, $self->{h}); 353 $self->size_allocate (0, 0, $self->{w}, $self->{h});
333} 354}
334 355
335sub find_widget { 356sub find_widget {
336 my ($self, $x, $y) = @_; 357 my ($self, $x, $y) = @_;
337 358
390sub size_request { 411sub size_request {
391 $_[0]{children}[0]->size_request 412 $_[0]{children}[0]->size_request
392} 413}
393 414
394sub size_allocate { 415sub size_allocate {
395 my ($self, $w, $h) = @_; 416 my ($self, $x, $y, $w, $h) = @_;
396 417
397 return unless $self->{w} != $w || $self->{h} != $h; 418 $self->_size_allocate ($x, $y, $w, $h) or return;
398 419
399 $self->SUPER::size_allocate ($w, $h);
400 $self->{children}[0]->size_allocate ($w, $h); 420 $self->{children}[0]->size_allocate (0, 0, $w, $h);
401} 421}
402 422
403############################################################################# 423#############################################################################
404 424
405package CFClient::UI::Window; 425package CFClient::UI::Window;
430 $self->{w}, $self->{h}, sub { $self->child->draw } 450 $self->{w}, $self->{h}, sub { $self->child->draw }
431 ); 451 );
432} 452}
433 453
434sub size_allocate { 454sub size_allocate {
435 my ($self, $w, $h) = @_; 455 my ($self, $x, $y, $w, $h) = @_;
436 456
437 return unless $self->{w} != $w || $self->{h} != $h; 457 $self->_size_allocate ($x, $y, $w, $h) or return;
438 458
439 $self->{w} = $w;
440 $self->{h} = $h;
441
442 $self->child->size_allocate ($w, $h); 459 $self->child->size_allocate (0, 0, $w, $h);
443 460
444 $self->render_chld; 461 $self->render_chld;
445} 462}
446 463
447sub _draw { 464sub _draw {
451 468
452 my $tex = $self->{texture} 469 my $tex = $self->{texture}
453 or return; 470 or return;
454 471
455 glEnable GL_BLEND; 472 glEnable GL_BLEND;
473 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
456 glEnable GL_TEXTURE_2D; 474 glEnable GL_TEXTURE_2D;
457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 475 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
458 476
459 $tex->draw_quad (0, 0, $w, $h); 477 $tex->draw_quad (0, 0, $w, $h);
460 478
479 497
480 map { $_ + 4 } $chld->size_request; 498 map { $_ + 4 } $chld->size_request;
481} 499}
482 500
483sub size_allocate { 501sub size_allocate {
484 my ($self, $w, $h) = @_; 502 my ($self, $x, $y, $w, $h) = @_;
485 503
486 return unless $self->{w} != $w || $self->{h} != $h; 504 $self->_size_allocate ($x, $y, $w, $h) or return;
487 505
488 $self->{w} = $w;
489 $self->{h} = $h;
490
491 $self->child->size_allocate ($w - 4, $h - 4); 506 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
492 $self->child->move (2, 2);
493} 507}
494 508
495sub _draw { 509sub _draw {
496 my ($self) = @_; 510 my ($self) = @_;
497 511
523 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 537 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
524 538
525sub size_request { 539sub size_request {
526 my ($self) = @_; 540 my ($self) = @_;
527 541
542 my ($w, $h) =
543 $self->{user_w} && $self->{user_h}
544 ? ($self->{user_w}, $self->{user_h})
528 my ($w, $h) = $self->SUPER::size_request; 545 : ($self->SUPER::size_request);
529 546
530 $h += $tex[1]->{h}; 547 $h += $tex[1]->{h};
531 $h += $tex[4]->{h}; 548 $h += $tex[4]->{h};
532 $w += $tex[2]->{w}; 549 $w += $tex[2]->{w};
533 $w += $tex[3]->{w}; 550 $w += $tex[3]->{w};
534 551
535 ($w, $h) 552 ($w, $h)
536} 553}
537 554
538sub size_allocate { 555sub size_allocate {
539 my ($self, $w, $h) = @_; 556 my ($self, $x, $y, $w, $h) = @_;
540 557
541 return unless $self->{w} != $w || $self->{h} != $h; 558 $self->_size_allocate ($x, $y, $w, $h) or return;
542 559
543 $self->SUPER::size_allocate ($w, $h);
544
545 $h -= $tex[1]->{h}; 560 $h -= $tex[1]{h};
546 $h -= $tex[4]->{h}; 561 $h -= $tex[4]{h};
547 $w -= $tex[2]->{w}; 562 $w -= $tex[2]{w};
548 $w -= $tex[3]->{w}; 563 $w -= $tex[3]{w};
549 564
550 $h = $h < 0 ? 0 : $h; 565 $h = $h < 0 ? 0 : $h;
551 $w = $w < 0 ? 0 : $w; 566 $w = $w < 0 ? 0 : $w;
552 567
553 my $child = $self->child; 568 my $child = $self->child;
554 569
555 $child->size_allocate ($w, $h);
556 $child->move ($tex[3]->{w}, $tex[1]->{h}); 570 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
571}
572
573sub button_down {
574 my ($self, $ev, $x, $y) = @_;
575
576 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
577 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
578
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
580 my ($bw, $bh) = ($self->{w}, $self->{h});
581
582 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_;
584
585 ($x, $y) = ($ev->motion_x, $ev->motion_y);
586
587 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy;
589 $self->update;
590 };
591
592 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $tex[1]{h}) {
594
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
596 my ($bx, $by) = ($self->{x}, $self->{y});
597
598 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_;
600
601 ($x, $y) = ($ev->motion_x, $ev->motion_y);
602
603 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update;
605 };
606 }
607}
608
609sub button_up {
610 my ($self, $ev, $x, $y) = @_;
611
612 delete $self->{motion};
613}
614
615sub mouse_motion {
616 my ($self, $ev, $x, $y) = @_;
617
618 $self->{motion}->($ev, $x, $y) if $self->{motion};
557} 619}
558 620
559sub _draw { 621sub _draw {
560 my ($self) = @_; 622 my ($self) = @_;
561 623
562 my ($w, $h) = ($self->{w}, $self->{h}); 624 my ($w, $h) = ($self->{w}, $self->{h});
563 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 625 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
564 626
565 glEnable GL_BLEND; 627 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
566 glEnable GL_TEXTURE_2D; 629 glEnable GL_TEXTURE_2D;
567 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
568 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
569 631
570 my $top = $tex[1]; 632 my $top = $tex[1];
571 $top->draw_quad (0, 0, $w, $top->{h}); 633 $top->draw_quad (0, 0, $w, $top->{h});
572 634
578 640
579 my $bottom = $tex[4]; 641 my $bottom = $tex[4];
580 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); 642 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
581 643
582 my $bg = $tex[0]; 644 my $bg = $tex[0];
645
583 glBindTexture GL_TEXTURE_2D, $bg->{name}; 646 glBindTexture GL_TEXTURE_2D, $bg->{name};
584 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
585 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 648 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
586 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 649 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
587 650
588 my $rep_x = $cw / $bg->{w}; 651 my $rep_x = $cw / $bg->{w};
589 my $rep_y = $ch / $bg->{h}; 652 my $rep_y = $ch / $bg->{h};
590 653
591 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 654 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
592 655
656 glDisable GL_TEXTURE_2D;
593 glDisable GL_BLEND; 657 glDisable GL_BLEND;
594 glDisable GL_TEXTURE_2D;
595 658
596 $self->child->draw; 659 $self->child->draw;
597 660
598} 661}
599 662
600############################################################################# 663#############################################################################
601 664
602package CFClient::UI::Table; 665package CFClient::UI::Table;
603 666
604our @ISA = CFClient::UI::Base::; 667our @ISA = CFClient::UI::Base::;
668
669use List::Util qw(max sum);
605 670
606use SDL::OpenGL; 671use SDL::OpenGL;
607 672
608sub add { 673sub add {
609 my ($self, $x, $y, $chld) = @_; 674 my ($self, $x, $y, $chld) = @_;
610 my $old_chld = $self->{children}[$y][$x];
611 675
612 $self->{children}[$y][$x] = $chld; 676 $self->{children}[$y][$x] = $chld;
613 $chld->set_parent ($self); 677 $chld->set_parent ($self);
678
679 $self->{w} = $self->{h} = -1;
614 $self->update; 680 $self->update;
615} 681}
616 682
617sub max_row_height { 683sub get_wh {
618 my ($self, $row) = @_; 684 my ($self) = @_;
619 685
620 my $hs = 0; 686 my (@w, @h);
621 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 687
688 for my $y (0 .. $#{$self->{children}}) {
622 my $c = $self->{children}->[$row]->[$xi]; 689 my $row = $self->{children}[$y]
623 if ($c) { 690 or next;
691
692 for my $x (0 .. $#$row) {
693 my $widget = $row->[$x]
694 or next;
624 my ($w, $h) = $c->size_request; 695 my ($w, $h) = $widget->size_request;
625 if ($hs < $h) { $hs = $h } 696
697 $w[$x] = max $w[$x], $w;
698 $h[$y] = max $h[$y], $h;
626 } 699 }
627 } 700 }
628 return $hs;
629}
630 701
631sub max_col_width { 702 (\@w, \@h)
703}
704
705sub size_request {
632 my ($self, $col) = @_; 706 my ($self) = @_;
633 707
708 my ($ws, $hs) = $self->get_wh;
709
710 (
711 (List::Util::sum @$ws),
712 (List::Util::sum @$hs),
713 )
714}
715
716sub size_allocate {
717 my ($self, $x, $y, $w, $h) = @_;
718
719 $self->_size_allocate ($x, $y, $w, $h) or return;
720
721 my ($ws, $hs) = $self->get_wh;
722
723 my $req_w = List::Util::sum @$ws;
724 my $req_h = List::Util::sum @$hs;
725
726 # linearly scale sizes
727 $_ *= $w / $req_w for @$ws;
728 $_ *= $h / $req_h for @$hs;
729
730 my $y;
731
732 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r]
734 or next;
735
634 my $ws = 0; 736 my $x = 0;
635 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 737 my $row_h = $hs->[$r];
636 my $c = ($self->{children}->[$yi] || [])->[$col]; 738
637 if ($c) { 739 for my $c (0 .. $#$row) {
638 my ($w, $h) = $c->size_request; 740 my $widget = $row->[$c]
639 if ($ws < $w) { $ws = $w } 741 or next;
742
743 my $col_w = $ws->[$c];
744
745 $widget->size_allocate ($x, $y, $col_w, $row_h);
746
747 $x += $col_w;
640 } 748 }
749
750 $y += $row_h;
641 } 751 }
642 return $ws; 752
643} 753}
754
755sub find_widget {
756 my ($self, $x, $y) = @_;
757
758 $x -= $self->{x};
759 $y -= $self->{y};
760
761 my $res;
762
763 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
764 $res = $_->find_widget ($x, $y)
765 and return $res;
766 }
767
768 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
769}
770
771sub _draw {
772 my ($self) = @_;
773
774 for (grep $_, @{$self->{children}}) {
775 $_->draw for grep $_, @$_;
776 }
777}
778
779#############################################################################
780
781package CFClient::UI::HBox;
782
783# TODO: wrap into common Box base class
784
785our @ISA = CFClient::UI::Container::;
644 786
645sub size_request { 787sub size_request {
646 my ($self) = @_; 788 my ($self) = @_;
647 789
790 my @alloc = map [$_->size_request], @{$self->{children}};
791
792 (
793 (List::Util::sum map $_->[0], @alloc),
794 (List::Util::max map $_->[1], @alloc),
795 )
796}
797
798sub size_allocate {
799 my ($self, $x, $y, $w, $h) = @_;
800
801 $self->_size_allocate ($x, $y, $w, $h) or return;
802
803 return unless $self->{w};
804
648 my ($hs, $ws) = (0, 0); 805 ($h, $w) = ($w, $h);
649 806
650 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 807 my $children = $self->{children};
651 $hs += $self->max_row_height ($yi);
652 }
653 808
654 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 809 my @h = map +($_->size_request)[0], @$children;
655 my $wm = 0; 810
656 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 811 my $req_h = List::Util::sum @h;
657 $wm += $self->max_col_width ($xi) 812
813 if ($req_h > $h) {
814 # ah well, not enough space
815 $_ = $h[$_] * $h / $req_h for @h;
816 } else {
817 my $exp = List::Util::sum map $_->{expand}, @$children;
818 $exp ||= 1;
819
820 for (0 .. $#$children) {
821 my $child = $children->[$_];
822
823 my $alloc_h = $h[$_];
824 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
825 $h[$_] = $alloc_h;
658 } 826 }
659 if ($ws < $wm) { $ws = $wm }
660 } 827 }
661
662 return ($ws, $hs);
663}
664
665sub _draw {
666 my ($self) = @_;
667 828
668 my $y = 0; 829 my $y = 0;
669 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 830 for (0 .. $#$children) {
831 my $child = $children->[$_];
670 my $x = 0; 832 my $h = $h[$_];
833 $child->size_allocate ($y, 0, $h, $w);
671 834
672 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 835 $y += $h;
673
674 my $c = $self->{children}->[$yi]->[$xi];
675 if ($c) {
676 $c->move ($x, $y, 0); #TODO: Move to size_request
677 $c->draw if $c;
678 }
679
680 $x += $self->max_col_width ($xi);
681 }
682
683 $y += $self->max_row_height ($yi);
684 } 836 }
685} 837}
686 838
687############################################################################# 839#############################################################################
688 840
689package CFClient::UI::VBox; 841package CFClient::UI::VBox;
690 842
843# TODO: wrap into common Box base class
844
691our @ISA = CFClient::UI::Container::; 845our @ISA = CFClient::UI::Container::;
692
693use SDL::OpenGL;
694 846
695sub size_request { 847sub size_request {
696 my ($self) = @_; 848 my ($self) = @_;
697 849
698 my @alloc = map [$_->size_request], @{$self->{children}}; 850 my @alloc = map [$_->size_request], @{$self->{children}};
702 (List::Util::sum map $_->[1], @alloc), 854 (List::Util::sum map $_->[1], @alloc),
703 ) 855 )
704} 856}
705 857
706sub size_allocate { 858sub size_allocate {
707 my ($self, $w, $h) = @_; 859 my ($self, $x, $y, $w, $h) = @_;
708 860
709 return unless $self->{w} != $w || $self->{h} != $h; 861 $self->_size_allocate ($x, $y, $w, $h) or return;
710
711 $self->{w} = $w;
712 $self->{h} = $h;
713 862
714 return unless $self->{h}; 863 return unless $self->{h};
715 864
716 my $children = $self->{children}; 865 my $children = $self->{children};
717 866
721 870
722 if ($req_h > $h) { 871 if ($req_h > $h) {
723 # ah well, not enough space 872 # ah well, not enough space
724 $_ = $h[$_] * $h / $req_h for @h; 873 $_ = $h[$_] * $h / $req_h for @h;
725 } else { 874 } else {
726 my @exp = grep $_->{expand}, @$children; 875 my $exp = List::Util::sum map $_->{expand}, @$children;
727 @exp = @$children unless @exp; 876 $exp ||= 1;
728 877
729 my %exp = map +($_ => 1), @exp;
730
731 for (0 .. $#$children) { 878 for (0 .. $#$children) {
732 my $child = $children->[$_]; 879 my $child = $children->[$_];
733 880
734 my $alloc_h = $h[$_]; 881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
735 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
736 $h[$_] = $alloc_h;
737 } 882 }
738 } 883 }
739 884
740 my $y = 0; 885 my $y = 0;
741 for (0 .. $#$children) { 886 for (0 .. $#$children) {
742 my $child = $children->[$_]; 887 my $child = $children->[$_];
743 my $h = $h[$_]; 888 my $h = $h[$_];
744 $child->move (0, $y);
745 $child->size_allocate ($w, $h); 889 $child->size_allocate (0, $y, $w, $h);
746 890
747 $y += $h; 891 $y += $h;
748 } 892 }
749} 893}
750 894
758 902
759sub new { 903sub new {
760 my ($class, %arg) = @_; 904 my ($class, %arg) = @_;
761 905
762 my $self = $class->SUPER::new ( 906 my $self = $class->SUPER::new (
763 fg => [1, 1, 1], 907 fg => [1, 1, 1],
764 height => $::FONTSIZE, 908 height => $::FONTSIZE,
765 text => "", 909 text => "",
766 align => -1, 910 align => -1,
911 padding => 2,
767 layout => new CFClient::Layout, 912 layout => new CFClient::Layout,
768 %arg 913 %arg
769 ); 914 );
770 915
771 $self->set_text ($self->{text}); 916 $self->set_text ($self->{text});
772 917
802sub size_request { 947sub size_request {
803 my ($self) = @_; 948 my ($self) = @_;
804 949
805 $self->{layout}->set_width; 950 $self->{layout}->set_width;
806 $self->{layout}->set_height ($self->{height}); 951 $self->{layout}->set_height ($self->{height});
807 $self->{layout}->size 952 my ($w, $h) = $self->{layout}->size;
808# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 953
809# ( 954 (
810# $self->{texture}{w}, 955 $w + $self->{padding} * 2,
811# $self->{texture}{h}, 956 $h + $self->{padding} * 2,
812# ) 957 )
813# } else {
814# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
815#
816# ($w, $h)
817# }
818} 958}
819 959
820sub size_allocate { 960sub size_allocate {
821 my ($self, $w, $h) = @_; 961 my ($self, $x, $y, $w, $h) = @_;
822 962
823 return unless $self->{w} != $w || $self->{h} != $h; 963 $self->_size_allocate ($x, $y, $w, $h) or return;
824 964
825 $self->SUPER::size_allocate ($w, $h);
826 delete $self->{texture}; 965 delete $self->{texture};
827} 966}
828 967
829sub update { 968sub update {
830 my ($self) = @_; 969 my ($self) = @_;
836sub _draw { 975sub _draw {
837 my ($self) = @_; 976 my ($self) = @_;
838 977
839 my $tex = $self->{texture} ||= do { 978 my $tex = $self->{texture} ||= do {
840 $self->{layout}->set_width ($self->{w}); 979 $self->{layout}->set_width ($self->{w});
841 new_from_layout CFClient::Texture $self->{layout}; 980 new_from_layout CFClient::Texture $self->{layout}
842 }; 981 };
843 982
844 glEnable GL_BLEND; 983 glEnable GL_BLEND;
984 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
845 glEnable GL_TEXTURE_2D; 985 glEnable GL_TEXTURE_2D;
846 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
847 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
848 987
849 glColor @{$self->{fg}}; 988 glColor @{$self->{fg}};
850 989
851 my $x = 990 my $x =
852 $self->{align} < 0 ? 0 991 $self->{align} < 0 ? $self->{padding}
853 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} 992 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
854 : ($self->{w} + $self->{texture}{w}) * 0.5; 993 : ($self->{w} - $tex->{w}) * 0.5;
855 994
856 $tex->draw_quad ($x, 0); 995 $tex->draw_quad ($x, $self->{padding});
857 996
997 glDisable GL_TEXTURE_2D;
858 glDisable GL_BLEND; 998 glDisable GL_BLEND;
859 glDisable GL_TEXTURE_2D;
860} 999}
861 1000
862############################################################################# 1001#############################################################################
863 1002
864package CFClient::UI::Entry; 1003package CFClient::UI::Entry;
871sub new { 1010sub new {
872 my $class = shift; 1011 my $class = shift;
873 1012
874 $class->SUPER::new ( 1013 $class->SUPER::new (
875 fg => [1, 1, 1], 1014 fg => [1, 1, 1],
876 bg => [0, 0, 0, 0.4], 1015 bg => [0, 0, 0, 0.2],
877 active_bg => [1, 1, 1], 1016 active_bg => [1, 1, 1],
878 active_fg => [0, 0, 0], 1017 active_fg => [0, 0, 0],
879 @_ 1018 @_
880 ) 1019 )
881} 1020}
888 $self->{text} = $text; 1027 $self->{text} = $text;
889 $self->{layout}->set_width ($self->{w}); 1028 $self->{layout}->set_width ($self->{w});
890 1029
891 $text =~ s/./*/g if $self->{hidden}; 1030 $text =~ s/./*/g if $self->{hidden};
892 1031
893
894 $self->{layout}->set_markup ($self->escape_text ($text)); 1032 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
895 1033
896 $text = substr $text, 0, $self->{cursor}; 1034 $text = substr $text, 0, $self->{cursor};
897 utf8::encode $text; 1035 utf8::encode $text;
898 1036
899 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1037 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
906 1044
907 ($w + 1, $h) # add 1 for cursor 1045 ($w + 1, $h) # add 1 for cursor
908} 1046}
909 1047
910sub size_allocate { 1048sub size_allocate {
911 my ($self, $w, $h) = @_; 1049 my ($self, $x, $y, $w, $h) = @_;
912 1050
913 return unless $self->{w} != $w || $self->{h} != $h;
914
915 $self->SUPER::size_allocate ($w, $h); 1051 $self->SUPER::size_allocate ($x, $y, $w, $h);
916 1052
917 $self->_set_text ($self->{text}); 1053 $self->_set_text ($self->{text});
918} 1054}
919 1055
920sub set_text { 1056sub set_text {
941 substr $text, $self->{cursor}, 1, ""; 1077 substr $text, $self->{cursor}, 1, "";
942 } elsif ($sym == SDLK_LEFT) { 1078 } elsif ($sym == SDLK_LEFT) {
943 --$self->{cursor} if $self->{cursor}; 1079 --$self->{cursor} if $self->{cursor};
944 } elsif ($sym == SDLK_RIGHT) { 1080 } elsif ($sym == SDLK_RIGHT) {
945 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1081 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1082 } elsif ($sym == SDLK_HOME) {
1083 $self->{cursor} = 0;
1084 } elsif ($sym == SDLK_END) {
1085 $self->{cursor} = length $text;
946 } elsif ($uni) { 1086 } elsif ($uni) {
947 substr $text, $self->{cursor}++, 0, chr $uni; 1087 substr $text, $self->{cursor}++, 0, chr $uni;
948 } 1088 }
949 1089
950 $self->_set_text ($text); 1090 $self->_set_text ($text);
965 $self->SUPER::button_down ($ev, $x, $y); 1105 $self->SUPER::button_down ($ev, $x, $y);
966 1106
967 my $idx = $self->{layout}->xy_to_index ($x, $y); 1107 my $idx = $self->{layout}->xy_to_index ($x, $y);
968 1108
969 # byte-index to char-index 1109 # byte-index to char-index
970 my $text = $self->{layout}; 1110 my $text = $self->{text};
971 utf8::encode $text; 1111 utf8::encode $text;
972 $self->{cursor} = length substr $text, 0, $idx; 1112 $self->{cursor} = length substr $text, 0, $idx;
973 1113
974 $self->_set_text ($self->{text}); 1114 $self->_set_text ($self->{text});
975 $self->update; 1115 $self->update;
990 $self->{fg} = $self->{active_fg}; 1130 $self->{fg} = $self->{active_fg};
991 } else { 1131 } else {
992 glColor @{$self->{bg}}; 1132 glColor @{$self->{bg}};
993 } 1133 }
994 1134
1135 glEnable GL_BLEND;
1136 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
995 glBegin GL_QUADS; 1137 glBegin GL_QUADS;
996 glVertex 0 , 0; 1138 glVertex 0 , 0;
997 glVertex 0 , $self->{h}; 1139 glVertex 0 , $self->{h};
998 glVertex $self->{w}, $self->{h}; 1140 glVertex $self->{w}, $self->{h};
999 glVertex $self->{w}, 0; 1141 glVertex $self->{w}, 0;
1000 glEnd; 1142 glEnd;
1143 glDisable GL_BLEND;
1001 1144
1002 $self->SUPER::_draw; 1145 $self->SUPER::_draw;
1003 1146
1004 #TODO: force update every cursor change :( 1147 #TODO: force update every cursor change :(
1005 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1148 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1016package CFClient::UI::Slider; 1159package CFClient::UI::Slider;
1017 1160
1018use strict; 1161use strict;
1019 1162
1020use SDL::OpenGL; 1163use SDL::OpenGL;
1021use SDL::OpenGL::Constants;
1022 1164
1023our @ISA = CFClient::UI::DrawBG::; 1165our @ISA = CFClient::UI::DrawBG::;
1024
1025sub size_request {
1026 my ($self) = @_;
1027
1028 my $w = 50;
1029 my $h = 10;
1030
1031 $self->{vertical} ? ($h, $w) : ($w, $h)
1032}
1033 1166
1034sub new { 1167sub new {
1035 my $class = shift; 1168 my $class = shift;
1036 1169
1037 # range [value, low, high, page] 1170 # range [value, low, high, page]
1038 1171
1039 $class->SUPER::new ( 1172 my $self = $class->SUPER::new (
1040 fg => [1, 1, 1], 1173 fg => [1, 1, 1],
1041 active_fg => [0, 0, 0], 1174 active_fg => [0, 0, 0],
1042 range => [0, 0, 100, 10], 1175 range => [0, 0, 100, 10],
1176 req_w => 40,
1177 req_h => 10,
1043 vertical => 1, 1178 vertical => 0,
1044 @_ 1179 @_
1045 ) 1180 );
1181
1182 $self
1183}
1184
1185sub size_request {
1186 my ($self) = @_;
1187
1188 my $w = $self->{req_w};
1189 my $h = $self->{req_h};
1190
1191 $self->{vertical} ? ($h, $w) : ($w, $h)
1046} 1192}
1047 1193
1048sub button_down { 1194sub button_down {
1049 my ($self, $ev, $x, $y) = @_; 1195 my ($self, $ev, $x, $y) = @_;
1050 1196
1133 1279
1134use List::Util qw(min max); 1280use List::Util qw(min max);
1135 1281
1136use SDL; 1282use SDL;
1137use SDL::OpenGL; 1283use SDL::OpenGL;
1138use SDL::OpenGL::Constants;
1139 1284
1140our @ISA = CFClient::UI::Base::; 1285our @ISA = CFClient::UI::Base::;
1141 1286
1142sub new { 1287sub new {
1143 my $class = shift; 1288 my $class = shift;
1156sub key_up { 1301sub key_up {
1157} 1302}
1158 1303
1159sub size_request { 1304sub size_request {
1160 ( 1305 (
1161 1 + int $::WIDTH / 32, 1306 1 + 32 * int $::WIDTH / 32,
1162 1 + int $::HEIGHT / 32, 1307 1 + 32 * int $::HEIGHT / 32,
1163 ) 1308 )
1164} 1309}
1165 1310
1166sub update { 1311sub update {
1167 my ($self) = @_; 1312 my ($self) = @_;
1168 1313
1169 $self->{need_update} = 1; 1314 $self->{need_update} = 1;
1315 $self->SUPER::update;
1170} 1316}
1171 1317
1172sub _draw { 1318sub draw {
1173 my ($self) = @_; 1319 my ($self) = @_;
1174 1320
1175 if (delete $self->{need_update}) { 1321 if (delete $self->{need_update}) {
1176 glNewList $self->{list}, GL_COMPILE; 1322 glNewList $self->{list}, GL_COMPILE;
1177 1323
1197 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1343 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1198 } 1344 }
1199 1345
1200 glEnable GL_TEXTURE_2D; 1346 glEnable GL_TEXTURE_2D;
1201 glEnable GL_BLEND; 1347 glEnable GL_BLEND;
1348 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1349 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1203 1350
1204 my $sw4 = ($sw + 3) & ~3; 1351 my $sw4 = ($sw + 3) & ~3;
1205 my $darkness = "\x00" x ($sw4 * $sh); 1352 my $darkness = "\x00" x ($sw4 * $sh);
1206 1353
1236# 1383#
1237# $lighting = $pb->get_pixels; 1384# $lighting = $pb->get_pixels;
1238# $lighting =~ s/(.)../$1/gs; 1385# $lighting =~ s/(.)../$1/gs;
1239# } 1386# }
1240 1387
1241 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1388 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1243 1389
1244 $darkness = new CFClient::Texture 1390 $darkness = new CFClient::Texture
1245 width => $sw4, 1391 w => $sw4,
1246 height => $sh, 1392 h => $sh,
1247 data => $darkness, 1393 data => $darkness,
1248 internalformat => GL_ALPHA, 1394 internalformat => GL_ALPHA,
1249 format => GL_ALPHA; 1395 format => GL_ALPHA;
1250 1396
1251 glColor 0.45, 0.45, 0.45, 1; 1397 glColor 0.45, 0.45, 0.45, 1;
1362sub size_request { 1508sub size_request {
1363 ($::WIDTH, $::HEIGHT) 1509 ($::WIDTH, $::HEIGHT)
1364} 1510}
1365 1511
1366sub size_allocate { 1512sub size_allocate {
1367 my ($self, $w, $h) = @_; 1513 my ($self, $x, $y, $w, $h) = @_;
1368 1514
1369 $self->SUPER::size_allocate ($w, $h); 1515 $self->_size_allocate ($x, $y, $w, $h);
1370 1516
1371 $_->size_allocate ($_->size_request) 1517 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1372 for @{$self->{children}}; 1518 for @{$self->{children}};
1373} 1519}
1374 1520
1375sub translate { 1521sub translate {
1376 my ($self, $x, $y) = @_; 1522 my ($self, $x, $y) = @_;
1379} 1525}
1380 1526
1381sub update { 1527sub update {
1382 my ($self) = @_; 1528 my ($self) = @_;
1383 1529
1384 $self->size_allocate ($self->size_request); 1530 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1385 ::refresh (); 1531 ::refresh ();
1386} 1532}
1387 1533
1388sub add { 1534sub add {
1389 my ($self, $widget) = @_; 1535 my ($self, $widget) = @_;
1390 1536
1391 $self->SUPER::add ($widget); 1537 $self->SUPER::add ($widget);
1392 1538
1393 $widget->size_allocate ($widget->size_request); 1539 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1394} 1540}
1395 1541
1396sub draw { 1542sub draw {
1397 my ($self) = @_; 1543 my ($self) = @_;
1398 1544

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines