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.74 by root, Tue Apr 11 19:31:18 2006 UTC vs.
Revision 1.79 by root, Wed Apr 12 00:26:50 2006 UTC

43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47 47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
79use SDL::OpenGL; 79use SDL::OpenGL;
80 80
81sub new { 81sub new {
82 my $class = shift; 82 my $class = shift;
83 83
84 bless { 84 my $self = bless {
85 x => 0, 85 x => 0,
86 y => 0, 86 y => 0,
87 z => 0, 87 z => 0,
88 w => -1,
89 h => -1,
88 @_ 90 @_
89 }, $class 91 }, $class;
92
93 for (keys %$self) {
94 if (/^connect_(.*)$/) {
95 $self->connect ($1 => delete $self->{$_});
96 }
97 }
98
99 $self
90} 100}
91 101
92sub move { 102sub move {
93 my ($self, $x, $y, $z) = @_; 103 my ($self, $x, $y, $z) = @_;
94 $self->{x} = $x; 104 $self->{x} = $x;
103sub size_request { 113sub size_request {
104 require Carp; 114 require Carp;
105 Carp::confess "size_request is abtract"; 115 Carp::confess "size_request is abtract";
106} 116}
107 117
108sub size_allocate { 118sub _size_allocate {
109 my ($self, $w, $h) = @_; 119 my ($self, $x, $y, $w, $h) = @_;
120
121 $self->{x} = $x;
122 $self->{y} = $y;
123
124 return unless $self->{w} != $w || $self->{h} != $h;
110 125
111 $self->{w} = $w; 126 $self->{w} = $w;
112 $self->{h} = $h; 127 $self->{h} = $h;
128
129 1
130}
131
132sub size_allocate {
133 my ($self, $x, $y, $w, $h) = @_;
134
135 $self->_size_allocate ($x, $y, $w, $h);
113} 136}
114 137
115# translate global koordinates to local coordinate system 138# translate global koordinates to local coordinate system
116sub translate { 139sub translate {
117 my ($self, $x, $y) = @_; 140 my ($self, $x, $y) = @_;
166 glPopMatrix; 189 glPopMatrix;
167 190
168 if ($self == $HOVER) { 191 if ($self == $HOVER) {
169 my ($x, $y) = @$self{qw(x y)}; 192 my ($x, $y) = @$self{qw(x y)};
170 193
171 glColor 1, 1, 1, 0.1; 194 glColor 0, 0, 1, 0.2;
172 glEnable GL_BLEND; 195 glEnable GL_BLEND;
173 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
174 glBegin GL_QUADS; 197 glBegin GL_QUADS;
175 glVertex $x , $y; 198 glVertex $x , $y;
176 glVertex $x + $self->{w}, $y; 199 glVertex $x + $self->{w}, $y;
185 my ($self) = @_; 208 my ($self) = @_;
186 209
187 warn "no draw defined for $self\n"; 210 warn "no draw defined for $self\n";
188} 211}
189 212
190sub bbox {
191 my ($self) = @_;
192 my ($w, $h) = $self->size_request;
193 (
194 $self->{x},
195 $self->{y},
196 $self->{x} = $w,
197 $self->{y} = $h
198 )
199}
200
201sub find_widget { 213sub find_widget {
202 my ($self, $x, $y) = @_; 214 my ($self, $x, $y) = @_;
203 215
204 return $self 216 return $self
205 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 217 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
206 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 218 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
207 219
208 () 220 ()
209} 221}
210 222
211sub del_parent { $_[0]->{parent} = undef }
212
213sub set_parent { 223sub set_parent {
214 my ($self, $par) = @_; 224 my ($self, $par) = @_;
215 225
216 $self->{parent} = $par; 226 $self->{parent} = $par;
217 Scalar::Util::weaken $self->{parent}; 227 Scalar::Util::weaken $self->{parent};
260 my $class = shift; 270 my $class = shift;
261 271
262 # range [value, low, high, page] 272 # range [value, low, high, page]
263 273
264 $class->SUPER::new ( 274 $class->SUPER::new (
265 bg => [0, 0, 0, 0.4], 275 bg => [0, 0, 0, 0.2],
266 active_bg => [1, 1, 1], 276 active_bg => [1, 1, 1, 0.5],
267 @_ 277 @_
268 ) 278 )
269} 279}
270 280
271sub _draw { 281sub _draw {
272 my ($self) = @_; 282 my ($self) = @_;
273 283
274 my ($w, $h) = @$self{qw(w h)}; 284 my ($w, $h) = @$self{qw(w h)};
275 285
286 glEnable GL_BLEND;
287 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
276 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 288 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
289
277 glBegin GL_QUADS; 290 glBegin GL_QUADS;
278 glVertex 0 , 0; 291 glVertex 0 , 0;
279 glVertex 0 , $h; 292 glVertex 0 , $h;
280 glVertex $w, $h; 293 glVertex $w, $h;
281 glVertex $w, 0; 294 glVertex $w, 0;
282 glEnd; 295 glEnd;
296
297 glDisable GL_BLEND;
283} 298}
284 299
285############################################################################# 300#############################################################################
286 301
287package CFClient::UI::Empty; 302package CFClient::UI::Empty;
310 325
311 $self 326 $self
312} 327}
313 328
314sub add { 329sub add {
315 my ($self, $chld, $expand) = @_; 330 my ($self, $chld) = @_;
316 331
317 $chld->{expand} = $expand;
318 $chld->set_parent ($self); 332 $chld->set_parent ($self);
319 333
320 $self->{children} = [ 334 $self->{children} = [
321 sort { $a->{z} <=> $b->{z} } 335 sort { $a->{z} <=> $b->{z} }
322 @{$self->{children}}, $chld 336 @{$self->{children}}, $chld
323 ]; 337 ];
324 338
325 $self->size_allocate ($self->{w}, $self->{h}) 339 $self->{w} = $self->{h} = -1;
326 if $self->{w}; #TODO: check for "realised state" 340 $self->update;
327} 341}
328 342
329sub remove { 343sub remove {
330 my ($self, $widget) = @_; 344 my ($self, $widget) = @_;
331 345
332 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 346 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
333 347
334 $self->size_allocate ($self->{w}, $self->{h}); 348 $self->size_allocate (0, 0, $self->{w}, $self->{h});
335} 349}
336 350
337sub find_widget { 351sub find_widget {
338 my ($self, $x, $y) = @_; 352 my ($self, $x, $y) = @_;
339 353
392sub size_request { 406sub size_request {
393 $_[0]{children}[0]->size_request 407 $_[0]{children}[0]->size_request
394} 408}
395 409
396sub size_allocate { 410sub size_allocate {
397 my ($self, $w, $h) = @_; 411 my ($self, $x, $y, $w, $h) = @_;
398 412
399 return unless $self->{w} != $w || $self->{h} != $h; 413 $self->_size_allocate ($x, $y, $w, $h) or return;
400 414
401 $self->SUPER::size_allocate ($w, $h);
402 $self->{children}[0]->size_allocate ($w, $h); 415 $self->{children}[0]->size_allocate (0, 0, $w, $h);
403} 416}
404 417
405############################################################################# 418#############################################################################
406 419
407package CFClient::UI::Window; 420package CFClient::UI::Window;
432 $self->{w}, $self->{h}, sub { $self->child->draw } 445 $self->{w}, $self->{h}, sub { $self->child->draw }
433 ); 446 );
434} 447}
435 448
436sub size_allocate { 449sub size_allocate {
437 my ($self, $w, $h) = @_; 450 my ($self, $x, $y, $w, $h) = @_;
438 451
439 return unless $self->{w} != $w || $self->{h} != $h; 452 $self->_size_allocate ($x, $y, $w, $h) or return;
440 453
441 $self->{w} = $w;
442 $self->{h} = $h;
443
444 $self->child->size_allocate ($w, $h); 454 $self->child->size_allocate (0, 0, $w, $h);
445 455
446 $self->render_chld; 456 $self->render_chld;
447} 457}
448 458
449sub _draw { 459sub _draw {
482 492
483 map { $_ + 4 } $chld->size_request; 493 map { $_ + 4 } $chld->size_request;
484} 494}
485 495
486sub size_allocate { 496sub size_allocate {
487 my ($self, $w, $h) = @_; 497 my ($self, $x, $y, $w, $h) = @_;
488 498
489 return unless $self->{w} != $w || $self->{h} != $h; 499 $self->_size_allocate ($x, $y, $w, $h) or return;
490 500
491 $self->{w} = $w;
492 $self->{h} = $h;
493
494 $self->child->size_allocate ($w - 4, $h - 4); 501 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
495 $self->child->move (2, 2);
496} 502}
497 503
498sub _draw { 504sub _draw {
499 my ($self) = @_; 505 my ($self) = @_;
500 506
526 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
527 533
528sub size_request { 534sub size_request {
529 my ($self) = @_; 535 my ($self) = @_;
530 536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
531 my ($w, $h) = $self->SUPER::size_request; 539 my ($w, $h) = $self->SUPER::size_request;
532 540
533 $h += $tex[1]->{h}; 541 $h += $tex[1]->{h};
534 $h += $tex[4]->{h}; 542 $h += $tex[4]->{h};
535 $w += $tex[2]->{w}; 543 $w += $tex[2]->{w};
537 545
538 ($w, $h) 546 ($w, $h)
539} 547}
540 548
541sub size_allocate { 549sub size_allocate {
542 my ($self, $w, $h) = @_; 550 my ($self, $x, $y, $w, $h) = @_;
543 551
544 return unless $self->{w} != $w || $self->{h} != $h; 552 $self->_size_allocate ($x, $y, $w, $h) or return;
545 553
546 $self->SUPER::size_allocate ($w, $h);
547
548 $h -= $tex[1]->{h}; 554 $h -= $tex[1]{h};
549 $h -= $tex[4]->{h}; 555 $h -= $tex[4]{h};
550 $w -= $tex[2]->{w}; 556 $w -= $tex[2]{w};
551 $w -= $tex[3]->{w}; 557 $w -= $tex[3]{w};
552 558
553 $h = $h < 0 ? 0 : $h; 559 $h = $h < 0 ? 0 : $h;
554 $w = $w < 0 ? 0 : $w; 560 $w = $w < 0 ? 0 : $w;
555 561
556 my $child = $self->child; 562 my $child = $self->child;
557 563
558 $child->size_allocate ($w, $h);
559 $child->move ($tex[3]->{w}, $tex[1]->{h}); 564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
560} 613}
561 614
562sub _draw { 615sub _draw {
563 my ($self) = @_; 616 my ($self) = @_;
564 617
581 634
582 my $bottom = $tex[4]; 635 my $bottom = $tex[4];
583 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); 636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
584 637
585 my $bg = $tex[0]; 638 my $bg = $tex[0];
639
586 glBindTexture GL_TEXTURE_2D, $bg->{name}; 640 glBindTexture GL_TEXTURE_2D, $bg->{name};
587 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
588 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 642 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
589 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 643 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
590 644
591 my $rep_x = $cw / $bg->{w}; 645 my $rep_x = $cw / $bg->{w};
592 my $rep_y = $ch / $bg->{h}; 646 my $rep_y = $ch / $bg->{h};
593 647
594 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 648 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
595 649
650 glDisable GL_TEXTURE_2D;
596 glDisable GL_BLEND; 651 glDisable GL_BLEND;
597 glDisable GL_TEXTURE_2D;
598 652
599 $self->child->draw; 653 $self->child->draw;
600 654
601} 655}
602 656
604 658
605package CFClient::UI::Table; 659package CFClient::UI::Table;
606 660
607our @ISA = CFClient::UI::Base::; 661our @ISA = CFClient::UI::Base::;
608 662
663use List::Util qw(max sum);
664
609use SDL::OpenGL; 665use SDL::OpenGL;
666
667sub new {
668 my $class = shift;
669
670 $class->SUPER::new (
671 col_expand => [],
672 @_
673 )
674}
610 675
611sub add { 676sub add {
612 my ($self, $x, $y, $chld) = @_; 677 my ($self, $x, $y, $chld) = @_;
613 my $old_chld = $self->{children}[$y][$x];
614 678
615 $self->{children}[$y][$x] = $chld; 679 $self->{children}[$y][$x] = $chld;
616 $chld->set_parent ($self); 680 $chld->set_parent ($self);
681
682 $self->{w} = $self->{h} = -1;
617 $self->update; 683 $self->update;
618} 684}
619 685
620sub max_row_height { 686sub get_wh {
621 my ($self, $row) = @_; 687 my ($self) = @_;
622 688
623 my $hs = 0; 689 my (@w, @h);
624 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 690
691 for my $y (0 .. $#{$self->{children}}) {
625 my $c = $self->{children}->[$row]->[$xi]; 692 my $row = $self->{children}[$y]
626 if ($c) { 693 or next;
694
695 for my $x (0 .. $#$row) {
696 my $widget = $row->[$x]
697 or next;
627 my ($w, $h) = $c->size_request; 698 my ($w, $h) = $widget->size_request;
628 if ($hs < $h) { $hs = $h } 699
700 $w[$x] = max $w[$x], $w;
701 $h[$y] = max $h[$y], $h;
629 } 702 }
630 } 703 }
631 return $hs;
632}
633 704
634sub max_col_width { 705 (\@w, \@h)
706}
707
708sub size_request {
635 my ($self, $col) = @_; 709 my ($self) = @_;
636 710
711 my ($ws, $hs) = $self->get_wh;
712
713 (
714 (sum @$ws),
715 (sum @$hs),
716 )
717}
718
719sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723
724 my ($ws, $hs) = $self->get_wh;
725
726 my $req_w = sum @$ws;
727 my $req_h = sum @$hs;
728
729 # TODO: nicer code && do row_expand
730 my @col_expand = @{$self->{col_expand}};
731 @col_expand = (1) x @$ws unless @col_expand;
732 my $col_expand = (sum @col_expand) || 1;
733
734 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737
738 my $y;
739
740 for my $r (0 .. $#{$self->{children}}) {
741 my $row = $self->{children}[$r]
742 or next;
743
637 my $ws = 0; 744 my $x = 0;
638 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 745 my $row_h = $hs->[$r];
639 my $c = ($self->{children}->[$yi] || [])->[$col]; 746
640 if ($c) { 747 for my $c (0 .. $#$row) {
641 my ($w, $h) = $c->size_request; 748 my $widget = $row->[$c]
642 if ($ws < $w) { $ws = $w } 749 or next;
750
751 my $col_w = $ws->[$c];
752
753 $widget->size_allocate ($x, $y, $col_w, $row_h);
754
755 $x += $col_w;
643 } 756 }
757
758 $y += $row_h;
644 } 759 }
645 return $ws; 760
646} 761}
762
763sub find_widget {
764 my ($self, $x, $y) = @_;
765
766 $x -= $self->{x};
767 $y -= $self->{y};
768
769 my $res;
770
771 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
772 $res = $_->find_widget ($x, $y)
773 and return $res;
774 }
775
776 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
777}
778
779sub _draw {
780 my ($self) = @_;
781
782 for (grep $_, @{$self->{children}}) {
783 $_->draw for grep $_, @$_;
784 }
785}
786
787#############################################################################
788
789package CFClient::UI::HBox;
790
791# TODO: wrap into common Box base class
792
793our @ISA = CFClient::UI::Container::;
647 794
648sub size_request { 795sub size_request {
649 my ($self) = @_; 796 my ($self) = @_;
650 797
798 my @alloc = map [$_->size_request], @{$self->{children}};
799
800 (
801 (List::Util::sum map $_->[0], @alloc),
802 (List::Util::max map $_->[1], @alloc),
803 )
804}
805
806sub size_allocate {
807 my ($self, $x, $y, $w, $h) = @_;
808
809 $self->_size_allocate ($x, $y, $w, $h) or return;
810
811 return unless $self->{w};
812
651 my ($hs, $ws) = (0, 0); 813 ($h, $w) = ($w, $h);
652 814
653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 815 my $children = $self->{children};
654 $hs += $self->max_row_height ($yi);
655 }
656 816
657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 817 my @h = map +($_->size_request)[0], @$children;
658 my $wm = 0; 818
659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 819 my $req_h = List::Util::sum @h;
660 $wm += $self->max_col_width ($xi) 820
821 if ($req_h > $h) {
822 # ah well, not enough space
823 $_ *= $h / $req_h for @h;
824 } else {
825 my $exp = List::Util::sum map $_->{expand}, @$children;
826 $exp ||= 1;
827
828 for (0 .. $#$children) {
829 my $child = $children->[$_];
830
831 my $alloc_h = $h[$_];
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h;
661 } 834 }
662 if ($ws < $wm) { $ws = $wm }
663 } 835 }
664
665 return ($ws, $hs);
666}
667
668sub _draw {
669 my ($self) = @_;
670 836
671 my $y = 0; 837 my $y = 0;
672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 838 for (0 .. $#$children) {
839 my $child = $children->[$_];
673 my $x = 0; 840 my $h = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w);
674 842
675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 843 $y += $h;
676
677 my $c = $self->{children}->[$yi]->[$xi];
678 if ($c) {
679 $c->move ($x, $y, 0); #TODO: Move to size_request
680 $c->draw if $c;
681 }
682
683 $x += $self->max_col_width ($xi);
684 }
685
686 $y += $self->max_row_height ($yi);
687 } 844 }
688} 845}
689 846
690############################################################################# 847#############################################################################
691 848
692package CFClient::UI::VBox; 849package CFClient::UI::VBox;
693 850
851# TODO: wrap into common Box base class
852
694our @ISA = CFClient::UI::Container::; 853our @ISA = CFClient::UI::Container::;
695
696use SDL::OpenGL;
697 854
698sub size_request { 855sub size_request {
699 my ($self) = @_; 856 my ($self) = @_;
700 857
701 my @alloc = map [$_->size_request], @{$self->{children}}; 858 my @alloc = map [$_->size_request], @{$self->{children}};
705 (List::Util::sum map $_->[1], @alloc), 862 (List::Util::sum map $_->[1], @alloc),
706 ) 863 )
707} 864}
708 865
709sub size_allocate { 866sub size_allocate {
710 my ($self, $w, $h) = @_; 867 my ($self, $x, $y, $w, $h) = @_;
711 868
712 return unless $self->{w} != $w || $self->{h} != $h; 869 $self->_size_allocate ($x, $y, $w, $h) or return;
713
714 $self->{w} = $w;
715 $self->{h} = $h;
716 870
717 return unless $self->{h}; 871 return unless $self->{h};
718 872
719 my $children = $self->{children}; 873 my $children = $self->{children};
720 874
722 876
723 my $req_h = List::Util::sum @h; 877 my $req_h = List::Util::sum @h;
724 878
725 if ($req_h > $h) { 879 if ($req_h > $h) {
726 # ah well, not enough space 880 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h; 881 $_ *= $h / $req_h for @h;
728 } else { 882 } else {
729 my @exp = grep $_->{expand}, @$children; 883 my $exp = List::Util::sum map $_->{expand}, @$children;
730 @exp = @$children unless @exp; 884 $exp ||= 1;
731 885
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) { 886 for (0 .. $#$children) {
735 my $child = $children->[$_]; 887 my $child = $children->[$_];
736 888
737 my $alloc_h = $h[$_]; 889 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 } 890 }
741 } 891 }
742 892
743 my $y = 0; 893 my $y = 0;
744 for (0 .. $#$children) { 894 for (0 .. $#$children) {
745 my $child = $children->[$_]; 895 my $child = $children->[$_];
746 my $h = $h[$_]; 896 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h); 897 $child->size_allocate (0, $y, $w, $h);
749 898
750 $y += $h; 899 $y += $h;
751 } 900 }
752} 901}
753 902
761 910
762sub new { 911sub new {
763 my ($class, %arg) = @_; 912 my ($class, %arg) = @_;
764 913
765 my $self = $class->SUPER::new ( 914 my $self = $class->SUPER::new (
766 fg => [1, 1, 1], 915 fg => [1, 1, 1],
767 height => $::FONTSIZE, 916 height => $::FONTSIZE,
768 text => "", 917 text => "",
769 align => -1, 918 align => -1,
919 padding => 2,
770 layout => new CFClient::Layout, 920 layout => new CFClient::Layout,
771 %arg 921 %arg
772 ); 922 );
773 923
774 $self->set_text ($self->{text}); 924 $self->set_text ($self->{text});
775 925
805sub size_request { 955sub size_request {
806 my ($self) = @_; 956 my ($self) = @_;
807 957
808 $self->{layout}->set_width; 958 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height}); 959 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size 960 my ($w, $h) = $self->{layout}->size;
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 961
812# ( 962 (
813# $self->{texture}{w}, 963 $w + $self->{padding} * 2,
814# $self->{texture}{h}, 964 $h + $self->{padding} * 2,
815# ) 965 )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821} 966}
822 967
823sub size_allocate { 968sub size_allocate {
824 my ($self, $w, $h) = @_; 969 my ($self, $x, $y, $w, $h) = @_;
825 970
826 return unless $self->{w} != $w || $self->{h} != $h; 971 $self->_size_allocate ($x, $y, $w, $h) or return;
827 972
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture}; 973 delete $self->{texture};
830} 974}
831 975
832sub update { 976sub update {
833 my ($self) = @_; 977 my ($self) = @_;
839sub _draw { 983sub _draw {
840 my ($self) = @_; 984 my ($self) = @_;
841 985
842 my $tex = $self->{texture} ||= do { 986 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w}); 987 $self->{layout}->set_width ($self->{w});
988 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{height});
844 new_from_layout CFClient::Texture $self->{layout} 989 new_from_layout CFClient::Texture $self->{layout}
845 }; 990 };
846 991
847 glEnable GL_BLEND; 992 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 993 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 995 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
851 996
852 glColor @{$self->{fg}}; 997 glColor @{$self->{fg}};
853 998
854 my $x = 999 my $x =
855 $self->{align} < 0 ? 0 1000 $self->{align} < 0 ? $self->{padding}
856 : $self->{align} > 0 ? $self->{w} - $tex->{w} 1001 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
857 : ($self->{w} - $tex->{w}) * 0.5; 1002 : ($self->{w} - $tex->{w}) * 0.5;
858 1003
859 $tex->draw_quad ($x, 0); 1004 $tex->draw_quad ($x, $self->{padding});
860 1005
861 glDisable GL_TEXTURE_2D; 1006 glDisable GL_TEXTURE_2D;
862 glDisable GL_BLEND; 1007 glDisable GL_BLEND;
863 glGetError and die;
864} 1008}
865 1009
866############################################################################# 1010#############################################################################
867 1011
868package CFClient::UI::Entry; 1012package CFClient::UI::Entry;
875sub new { 1019sub new {
876 my $class = shift; 1020 my $class = shift;
877 1021
878 $class->SUPER::new ( 1022 $class->SUPER::new (
879 fg => [1, 1, 1], 1023 fg => [1, 1, 1],
880 bg => [0, 0, 0, 0.4], 1024 bg => [0, 0, 0, 0.2],
881 active_bg => [1, 1, 1], 1025 active_bg => [1, 1, 1, 0.5],
882 active_fg => [0, 0, 0], 1026 active_fg => [0, 0, 0],
883 @_ 1027 @_
884 ) 1028 )
885} 1029}
886 1030
892 $self->{text} = $text; 1036 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w}); 1037 $self->{layout}->set_width ($self->{w});
894 1038
895 $text =~ s/./*/g if $self->{hidden}; 1039 $text =~ s/./*/g if $self->{hidden};
896 1040
897
898 $self->{layout}->set_markup ($self->escape_text ($text)); 1041 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
899 1042
900 $text = substr $text, 0, $self->{cursor}; 1043 $text = substr $text, 0, $self->{cursor};
901 utf8::encode $text; 1044 utf8::encode $text;
902 1045
903 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1046 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
910 1053
911 ($w + 1, $h) # add 1 for cursor 1054 ($w + 1, $h) # add 1 for cursor
912} 1055}
913 1056
914sub size_allocate { 1057sub size_allocate {
915 my ($self, $w, $h) = @_; 1058 my ($self, $x, $y, $w, $h) = @_;
916 1059
917 return unless $self->{w} != $w || $self->{h} != $h;
918
919 $self->SUPER::size_allocate ($w, $h); 1060 $self->SUPER::size_allocate ($x, $y, $w, $h);
920 1061
921 $self->_set_text ($self->{text}); 1062 $self->_set_text ($self->{text});
922} 1063}
923 1064
924sub set_text { 1065sub set_text {
945 substr $text, $self->{cursor}, 1, ""; 1086 substr $text, $self->{cursor}, 1, "";
946 } elsif ($sym == SDLK_LEFT) { 1087 } elsif ($sym == SDLK_LEFT) {
947 --$self->{cursor} if $self->{cursor}; 1088 --$self->{cursor} if $self->{cursor};
948 } elsif ($sym == SDLK_RIGHT) { 1089 } elsif ($sym == SDLK_RIGHT) {
949 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1090 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1091 } elsif ($sym == SDLK_HOME) {
1092 $self->{cursor} = 0;
1093 } elsif ($sym == SDLK_END) {
1094 $self->{cursor} = length $text;
950 } elsif ($uni) { 1095 } elsif ($uni) {
951 substr $text, $self->{cursor}++, 0, chr $uni; 1096 substr $text, $self->{cursor}++, 0, chr $uni;
952 } 1097 }
953 1098
954 $self->_set_text ($text); 1099 $self->_set_text ($text);
969 $self->SUPER::button_down ($ev, $x, $y); 1114 $self->SUPER::button_down ($ev, $x, $y);
970 1115
971 my $idx = $self->{layout}->xy_to_index ($x, $y); 1116 my $idx = $self->{layout}->xy_to_index ($x, $y);
972 1117
973 # byte-index to char-index 1118 # byte-index to char-index
974 my $text = $self->{layout}; 1119 my $text = $self->{text};
975 utf8::encode $text; 1120 utf8::encode $text;
976 $self->{cursor} = length substr $text, 0, $idx; 1121 $self->{cursor} = length substr $text, 0, $idx;
977 1122
978 $self->_set_text ($self->{text}); 1123 $self->_set_text ($self->{text});
979 $self->update; 1124 $self->update;
994 $self->{fg} = $self->{active_fg}; 1139 $self->{fg} = $self->{active_fg};
995 } else { 1140 } else {
996 glColor @{$self->{bg}}; 1141 glColor @{$self->{bg}};
997 } 1142 }
998 1143
1144 glEnable GL_BLEND;
1145 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
999 glBegin GL_QUADS; 1146 glBegin GL_QUADS;
1000 glVertex 0 , 0; 1147 glVertex 0 , 0;
1001 glVertex 0 , $self->{h}; 1148 glVertex 0 , $self->{h};
1002 glVertex $self->{w}, $self->{h}; 1149 glVertex $self->{w}, $self->{h};
1003 glVertex $self->{w}, 0; 1150 glVertex $self->{w}, 0;
1004 glEnd; 1151 glEnd;
1152 glDisable GL_BLEND;
1005 1153
1006 $self->SUPER::_draw; 1154 $self->SUPER::_draw;
1007 1155
1008 #TODO: force update every cursor change :( 1156 #TODO: force update every cursor change :(
1009 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1157 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1015 } 1163 }
1016} 1164}
1017 1165
1018############################################################################# 1166#############################################################################
1019 1167
1020package CFClient::UI::Slider; 1168package CFClient::UI::Button;
1021 1169
1022use strict; 1170our @ISA = CFClient::UI::Label::;
1023 1171
1172use SDL;
1024use SDL::OpenGL; 1173use SDL::OpenGL;
1025
1026our @ISA = CFClient::UI::DrawBG::;
1027
1028sub size_request {
1029 my ($self) = @_;
1030
1031 my $w = 50;
1032 my $h = 10;
1033
1034 $self->{vertical} ? ($h, $w) : ($w, $h)
1035}
1036 1174
1037sub new { 1175sub new {
1038 my $class = shift; 1176 my $class = shift;
1039 1177
1178 $class->SUPER::new (
1179 padding => 4,
1180 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2],
1182 active_bg => [0, 0, 0, 0.5],
1183 active_fg => [1, 1, 0],
1184 border_fg => [1, 1, 0],
1185 @_
1186 )
1187}
1188
1189sub button_up {
1190 my ($self, $ev, $x, $y) = @_;
1191
1192 if ($x >= 0 && $x < $self->{w}
1193 && $y >= 0 && $y < $self->{h}) {
1194 $self->emit ("activate");
1195 }
1196}
1197
1198sub _draw {
1199 my ($self) = @_;
1200
1201 local $self->{fg} = $self->{fg};
1202
1203 glEnable GL_BLEND;
1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1205
1206 glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly
1207
1208 glBegin GL_LINE_LOOP;
1209 glColor @{$self->{border_fg}};
1210 glVertex 1 , 1;
1211 glVertex 1 , $self->{h} - 2;
1212 glVertex $self->{w} - 2, $self->{h} - 2;
1213 glVertex $self->{w} - 2, 1;
1214 glEnd;
1215
1216 if ($GRAB == $self) {
1217 glColor @{$self->{active_bg}};
1218 $self->{fg} = $self->{active_fg};
1219 } else {
1220 glColor @{$self->{bg}};
1221 }
1222
1223 glBegin GL_QUADS;
1224 glVertex 2 , 2;
1225 glVertex 2 , $self->{h} - 2;
1226 glVertex $self->{w} - 2, $self->{h} - 2;
1227 glVertex $self->{w} - 2, 2;
1228 glEnd;
1229 glDisable GL_BLEND;
1230
1231 $self->SUPER::_draw;
1232}
1233
1234#############################################################################
1235
1236package CFClient::UI::Slider;
1237
1238use strict;
1239
1240use SDL::OpenGL;
1241
1242our @ISA = CFClient::UI::DrawBG::;
1243
1244sub new {
1245 my $class = shift;
1246
1040 # range [value, low, high, page] 1247 # range [value, low, high, page]
1041 1248
1042 $class->SUPER::new ( 1249 my $self = $class->SUPER::new (
1043 fg => [1, 1, 1], 1250 fg => [1, 1, 1],
1044 active_fg => [0, 0, 0], 1251 active_fg => [0, 0, 0],
1045 range => [0, 0, 100, 10], 1252 range => [0, 0, 100, 10],
1253 req_w => 40,
1254 req_h => 10,
1046 vertical => 1, 1255 vertical => 0,
1047 @_ 1256 @_
1048 ) 1257 );
1258
1259 $self
1260}
1261
1262sub size_request {
1263 my ($self) = @_;
1264
1265 my $w = $self->{req_w};
1266 my $h = $self->{req_h};
1267
1268 $self->{vertical} ? ($h, $w) : ($w, $h)
1049} 1269}
1050 1270
1051sub button_down { 1271sub button_down {
1052 my ($self, $ev, $x, $y) = @_; 1272 my ($self, $ev, $x, $y) = @_;
1053 1273
1158sub key_up { 1378sub key_up {
1159} 1379}
1160 1380
1161sub size_request { 1381sub size_request {
1162 ( 1382 (
1163 1 + int $::WIDTH / 32, 1383 1 + 32 * int $::WIDTH / 32,
1164 1 + int $::HEIGHT / 32, 1384 1 + 32 * int $::HEIGHT / 32,
1165 ) 1385 )
1166} 1386}
1167 1387
1168sub update { 1388sub update {
1169 my ($self) = @_; 1389 my ($self) = @_;
1170 1390
1171 $self->{need_update} = 1; 1391 $self->{need_update} = 1;
1172 $self->SUPER::update; 1392 $self->SUPER::update;
1173} 1393}
1174 1394
1175sub _draw { 1395sub draw {
1176 my ($self) = @_; 1396 my ($self) = @_;
1177 1397
1178 if (delete $self->{need_update}) { 1398 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE; 1399 glNewList $self->{list}, GL_COMPILE;
1180 1400
1365sub size_request { 1585sub size_request {
1366 ($::WIDTH, $::HEIGHT) 1586 ($::WIDTH, $::HEIGHT)
1367} 1587}
1368 1588
1369sub size_allocate { 1589sub size_allocate {
1370 my ($self, $w, $h) = @_; 1590 my ($self, $x, $y, $w, $h) = @_;
1371 1591
1372 $self->SUPER::size_allocate ($w, $h); 1592 $self->_size_allocate ($x, $y, $w, $h);
1373 1593
1374 $_->size_allocate ($_->size_request) 1594 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1375 for @{$self->{children}}; 1595 for @{$self->{children}};
1376} 1596}
1377 1597
1378sub translate { 1598sub translate {
1379 my ($self, $x, $y) = @_; 1599 my ($self, $x, $y) = @_;
1382} 1602}
1383 1603
1384sub update { 1604sub update {
1385 my ($self) = @_; 1605 my ($self) = @_;
1386 1606
1387 $self->size_allocate ($self->size_request); 1607 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1388 ::refresh (); 1608 ::refresh ();
1389} 1609}
1390 1610
1391sub add { 1611sub add {
1392 my ($self, $widget) = @_; 1612 my ($self, $widget) = @_;
1393 1613
1394 $self->SUPER::add ($widget); 1614 $self->SUPER::add ($widget);
1395 1615
1396 $widget->size_allocate ($widget->size_request); 1616 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1397} 1617}
1398 1618
1399sub draw { 1619sub draw {
1400 my ($self) = @_; 1620 my ($self) = @_;
1401 1621

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines