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.84 by root, Wed Apr 12 12:20:00 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;
113}
114 128
129 1
130}
131
132sub size_allocate {
133 my ($self, $x, $y, $w, $h) = @_;
134
135 $self->_size_allocate ($x, $y, $w, $h);
136}
137
115# translate global koordinates to local coordinate system 138# translate global coordinates to local coordinate system
116sub translate { 139sub translate {
117 my ($self, $x, $y) = @_; 140 my ($self, $x, $y) = @_;
118 141
119 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
120} 143}
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;
589 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
590 642
591 my $rep_x = $cw / $bg->{w}; 643 my $rep_x = $cw / $bg->{w};
592 my $rep_y = $ch / $bg->{h}; 644 my $rep_y = $ch / $bg->{h};
593 645
594 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
595 647
648 glDisable GL_TEXTURE_2D;
596 glDisable GL_BLEND; 649 glDisable GL_BLEND;
597 glDisable GL_TEXTURE_2D;
598 650
599 $self->child->draw; 651 $self->child->draw;
600 652
601} 653}
602 654
604 656
605package CFClient::UI::Table; 657package CFClient::UI::Table;
606 658
607our @ISA = CFClient::UI::Base::; 659our @ISA = CFClient::UI::Base::;
608 660
661use List::Util qw(max sum);
662
609use SDL::OpenGL; 663use SDL::OpenGL;
664
665sub new {
666 my $class = shift;
667
668 $class->SUPER::new (
669 col_expand => [],
670 @_
671 )
672}
610 673
611sub add { 674sub add {
612 my ($self, $x, $y, $chld) = @_; 675 my ($self, $x, $y, $chld) = @_;
613 my $old_chld = $self->{children}[$y][$x];
614 676
615 $self->{children}[$y][$x] = $chld; 677 $self->{children}[$y][$x] = $chld;
616 $chld->set_parent ($self); 678 $chld->set_parent ($self);
679
680 $self->{w} = $self->{h} = -1;
617 $self->update; 681 $self->update;
618} 682}
619 683
620sub max_row_height { 684sub get_wh {
621 my ($self, $row) = @_; 685 my ($self) = @_;
622 686
623 my $hs = 0; 687 my (@w, @h);
624 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 688
689 for my $y (0 .. $#{$self->{children}}) {
625 my $c = $self->{children}->[$row]->[$xi]; 690 my $row = $self->{children}[$y]
626 if ($c) { 691 or next;
692
693 for my $x (0 .. $#$row) {
694 my $widget = $row->[$x]
695 or next;
627 my ($w, $h) = $c->size_request; 696 my ($w, $h) = $widget->size_request;
628 if ($hs < $h) { $hs = $h } 697
698 $w[$x] = max $w[$x], $w;
699 $h[$y] = max $h[$y], $h;
629 } 700 }
630 } 701 }
631 return $hs;
632}
633 702
634sub max_col_width { 703 (\@w, \@h)
704}
705
706sub size_request {
635 my ($self, $col) = @_; 707 my ($self) = @_;
636 708
709 my ($ws, $hs) = $self->get_wh;
710
711 (
712 (sum @$ws),
713 (sum @$hs),
714 )
715}
716
717sub size_allocate {
718 my ($self, $x, $y, $w, $h) = @_;
719
720 $self->_size_allocate ($x, $y, $w, $h) or return;
721
722 my ($ws, $hs) = $self->get_wh;
723
724 my $req_w = sum @$ws;
725 my $req_h = sum @$hs;
726
727 # TODO: nicer code && do row_expand
728 my @col_expand = @{$self->{col_expand}};
729 @col_expand = (1) x @$ws unless @col_expand;
730 my $col_expand = (sum @col_expand) || 1;
731
732 # linearly scale sizes
733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
734 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
735
736 my $y;
737
738 for my $r (0 .. $#{$self->{children}}) {
739 my $row = $self->{children}[$r]
740 or next;
741
637 my $ws = 0; 742 my $x = 0;
638 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 743 my $row_h = $hs->[$r];
639 my $c = ($self->{children}->[$yi] || [])->[$col]; 744
640 if ($c) { 745 for my $c (0 .. $#$row) {
641 my ($w, $h) = $c->size_request; 746 my $col_w = $ws->[$c];
642 if ($ws < $w) { $ws = $w } 747
748 if (my $widget = $row->[$c]) {
749 $widget->size_allocate ($x, $y, $col_w, $row_h);
750 }
751
752 $x += $col_w;
643 } 753 }
754
755 $y += $row_h;
644 } 756 }
645 return $ws; 757
646} 758}
759
760sub find_widget {
761 my ($self, $x, $y) = @_;
762
763 $x -= $self->{x};
764 $y -= $self->{y};
765
766 my $res;
767
768 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
769 $res = $_->find_widget ($x, $y)
770 and return $res;
771 }
772
773 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
774}
775
776sub _draw {
777 my ($self) = @_;
778
779 for (grep $_, @{$self->{children}}) {
780 $_->draw for grep $_, @$_;
781 }
782}
783
784#############################################################################
785
786package CFClient::UI::HBox;
787
788# TODO: wrap into common Box base class
789
790our @ISA = CFClient::UI::Container::;
647 791
648sub size_request { 792sub size_request {
649 my ($self) = @_; 793 my ($self) = @_;
650 794
795 my @alloc = map [$_->size_request], @{$self->{children}};
796
797 (
798 (List::Util::sum map $_->[0], @alloc),
799 (List::Util::max map $_->[1], @alloc),
800 )
801}
802
803sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807
808 return unless $self->{w};
809
651 my ($hs, $ws) = (0, 0); 810 ($h, $w) = ($w, $h);
652 811
653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 812 my $children = $self->{children};
654 $hs += $self->max_row_height ($yi);
655 }
656 813
657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 814 my @h = map +($_->size_request)[0], @$children;
658 my $wm = 0; 815
659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 816 my $req_h = List::Util::sum @h;
660 $wm += $self->max_col_width ($xi) 817
818 if ($req_h > $h) {
819 # ah well, not enough space
820 $_ *= $h / $req_h for @h;
821 } else {
822 my $exp = List::Util::sum map $_->{expand}, @$children;
823 $exp ||= 1;
824
825 for (0 .. $#$children) {
826 my $child = $children->[$_];
827
828 my $alloc_h = $h[$_];
829 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
830 $h[$_] = $alloc_h;
661 } 831 }
662 if ($ws < $wm) { $ws = $wm }
663 } 832 }
664
665 return ($ws, $hs);
666}
667
668sub _draw {
669 my ($self) = @_;
670 833
671 my $y = 0; 834 my $y = 0;
672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 835 for (0 .. $#$children) {
836 my $child = $children->[$_];
673 my $x = 0; 837 my $h = $h[$_];
838 $child->size_allocate ($y, 0, $h, $w);
674 839
675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 840 $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 } 841 }
688} 842}
689 843
690############################################################################# 844#############################################################################
691 845
692package CFClient::UI::VBox; 846package CFClient::UI::VBox;
693 847
848# TODO: wrap into common Box base class
849
694our @ISA = CFClient::UI::Container::; 850our @ISA = CFClient::UI::Container::;
695
696use SDL::OpenGL;
697 851
698sub size_request { 852sub size_request {
699 my ($self) = @_; 853 my ($self) = @_;
700 854
701 my @alloc = map [$_->size_request], @{$self->{children}}; 855 my @alloc = map [$_->size_request], @{$self->{children}};
705 (List::Util::sum map $_->[1], @alloc), 859 (List::Util::sum map $_->[1], @alloc),
706 ) 860 )
707} 861}
708 862
709sub size_allocate { 863sub size_allocate {
710 my ($self, $w, $h) = @_; 864 my ($self, $x, $y, $w, $h) = @_;
711 865
712 return unless $self->{w} != $w || $self->{h} != $h; 866 $self->_size_allocate ($x, $y, $w, $h) or return;
713
714 $self->{w} = $w;
715 $self->{h} = $h;
716 867
717 return unless $self->{h}; 868 return unless $self->{h};
718 869
719 my $children = $self->{children}; 870 my $children = $self->{children};
720 871
722 873
723 my $req_h = List::Util::sum @h; 874 my $req_h = List::Util::sum @h;
724 875
725 if ($req_h > $h) { 876 if ($req_h > $h) {
726 # ah well, not enough space 877 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h; 878 $_ *= $h / $req_h for @h;
728 } else { 879 } else {
729 my @exp = grep $_->{expand}, @$children; 880 my $exp = List::Util::sum map $_->{expand}, @$children;
730 @exp = @$children unless @exp; 881 $exp ||= 1;
731 882
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) { 883 for (0 .. $#$children) {
735 my $child = $children->[$_]; 884 my $child = $children->[$_];
736 885
737 my $alloc_h = $h[$_]; 886 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 } 887 }
741 } 888 }
742 889
743 my $y = 0; 890 my $y = 0;
744 for (0 .. $#$children) { 891 for (0 .. $#$children) {
745 my $child = $children->[$_]; 892 my $child = $children->[$_];
746 my $h = $h[$_]; 893 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h); 894 $child->size_allocate (0, $y, $w, $h);
749 895
750 $y += $h; 896 $y += $h;
751 } 897 }
752} 898}
753 899
761 907
762sub new { 908sub new {
763 my ($class, %arg) = @_; 909 my ($class, %arg) = @_;
764 910
765 my $self = $class->SUPER::new ( 911 my $self = $class->SUPER::new (
766 fg => [1, 1, 1], 912 fg => [1, 1, 1],
767 height => $::FONTSIZE, 913 height => $::FONTSIZE,
768 text => "", 914 text => "",
769 align => -1, 915 align => -1,
916 padding => 2,
770 layout => new CFClient::Layout, 917 layout => new CFClient::Layout,
771 %arg 918 %arg
772 ); 919 );
773 920
774 $self->set_text ($self->{text}); 921 $self->set_text ($self->{text});
775 922
791 938
792 $self->{text} = $text; 939 $self->{text} = $text;
793 $self->{layout}->set_markup ($text); 940 $self->{layout}->set_markup ($text);
794 941
795 delete $self->{texture}; 942 delete $self->{texture};
943# $self->{w} = $self->{h} = -1;
796 $self->update; 944 $self->update;
797} 945}
798 946
799sub get_text { 947sub get_text {
800 my ($self, $text) = @_; 948 my ($self, $text) = @_;
805sub size_request { 953sub size_request {
806 my ($self) = @_; 954 my ($self) = @_;
807 955
808 $self->{layout}->set_width; 956 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height}); 957 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size 958 my ($w, $h) = $self->{layout}->size;
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 959
812# ( 960 (
813# $self->{texture}{w}, 961 $w + $self->{padding} * 2,
814# $self->{texture}{h}, 962 $h + $self->{padding} * 2,
815# ) 963 )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821} 964}
822 965
823sub size_allocate { 966sub size_allocate {
824 my ($self, $w, $h) = @_; 967 my ($self, $x, $y, $w, $h) = @_;
825 968
826 return unless $self->{w} != $w || $self->{h} != $h; 969 $self->_size_allocate ($x, $y, $w, $h) or return;
827 970
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture}; 971 delete $self->{texture};
830} 972}
831 973
832sub update { 974sub update {
833 my ($self) = @_; 975 my ($self) = @_;
839sub _draw { 981sub _draw {
840 my ($self) = @_; 982 my ($self) = @_;
841 983
842 my $tex = $self->{texture} ||= do { 984 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w}); 985 $self->{layout}->set_width ($self->{w});
986 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
844 new_from_layout CFClient::Texture $self->{layout} 987 new_from_layout CFClient::Texture $self->{layout}
845 }; 988 };
846 989
847 glEnable GL_BLEND; 990 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 991 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 993 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
851 994
852 glColor @{$self->{fg}}; 995 glColor @{$self->{fg}};
853 996
854 my $x = 997 my $x =
855 $self->{align} < 0 ? 0 998 $self->{align} < 0 ? $self->{padding}
856 : $self->{align} > 0 ? $self->{w} - $tex->{w} 999 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
857 : ($self->{w} - $tex->{w}) * 0.5; 1000 : ($self->{w} - $tex->{w}) * 0.5;
858 1001
1002 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0;
859 $tex->draw_quad ($x, 0); 1003 $tex->draw_quad (0, 0);
860 1004
861 glDisable GL_TEXTURE_2D; 1005 glDisable GL_TEXTURE_2D;
862 glDisable GL_BLEND; 1006 glDisable GL_BLEND;
863 glGetError and die;
864} 1007}
865 1008
866############################################################################# 1009#############################################################################
867 1010
868package CFClient::UI::Entry; 1011package CFClient::UI::Entry;
875sub new { 1018sub new {
876 my $class = shift; 1019 my $class = shift;
877 1020
878 $class->SUPER::new ( 1021 $class->SUPER::new (
879 fg => [1, 1, 1], 1022 fg => [1, 1, 1],
880 bg => [0, 0, 0, 0.4], 1023 bg => [0, 0, 0, 0.2],
881 active_bg => [1, 1, 1], 1024 active_bg => [1, 1, 1, 0.5],
882 active_fg => [0, 0, 0], 1025 active_fg => [0, 0, 0],
883 @_ 1026 @_
884 ) 1027 )
885} 1028}
886 1029
889 1032
890 $self->{last_activity} = $::NOW; 1033 $self->{last_activity} = $::NOW;
891 1034
892 $self->{text} = $text; 1035 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w}); 1036 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
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
1098 $value = int +($value - $lo) * $w / ($hi - $lo); 1318 $value = int +($value - $lo) * $w / ($hi - $lo);
1099 1319
1100 $w -= $page; 1320 $w -= $page;
1101 $page &= ~1; 1321 $page &= ~1;
1102 glTranslate $page * 0.5, 0, 0; 1322 glTranslate $page * 0.5, 0, 0;
1323 $page ||= 2;
1103 1324
1104 glColor @$fg; 1325 glColor @$fg;
1105 glBegin GL_LINES; 1326 glBegin GL_LINES;
1106 glVertex 0, 0; glVertex 0, $h; 1327 glVertex 0, 0; glVertex 0, $h;
1107 glVertex $w - 1, 0; glVertex $w - 1, $h; 1328 glVertex $w - 1, 0; glVertex $w - 1, $h;
1158sub key_up { 1379sub key_up {
1159} 1380}
1160 1381
1161sub size_request { 1382sub size_request {
1162 ( 1383 (
1163 1 + int $::WIDTH / 32, 1384 1 + 32 * int $::WIDTH / 32,
1164 1 + int $::HEIGHT / 32, 1385 1 + 32 * int $::HEIGHT / 32,
1165 ) 1386 )
1166} 1387}
1167 1388
1168sub update { 1389sub update {
1169 my ($self) = @_; 1390 my ($self) = @_;
1170 1391
1171 $self->{need_update} = 1; 1392 $self->{need_update} = 1;
1172 $self->SUPER::update; 1393 $self->SUPER::update;
1173} 1394}
1174 1395
1175sub _draw { 1396sub draw {
1176 my ($self) = @_; 1397 my ($self) = @_;
1177 1398
1178 if (delete $self->{need_update}) { 1399 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE; 1400 glNewList $self->{list}, GL_COMPILE;
1180 1401
1365sub size_request { 1586sub size_request {
1366 ($::WIDTH, $::HEIGHT) 1587 ($::WIDTH, $::HEIGHT)
1367} 1588}
1368 1589
1369sub size_allocate { 1590sub size_allocate {
1370 my ($self, $w, $h) = @_; 1591 my ($self, $x, $y, $w, $h) = @_;
1371 1592
1372 $self->SUPER::size_allocate ($w, $h); 1593 $self->_size_allocate ($x, $y, $w, $h);
1373 1594
1374 $_->size_allocate ($_->size_request) 1595 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1375 for @{$self->{children}}; 1596 for @{$self->{children}};
1376} 1597}
1377 1598
1378sub translate { 1599sub translate {
1379 my ($self, $x, $y) = @_; 1600 my ($self, $x, $y) = @_;
1382} 1603}
1383 1604
1384sub update { 1605sub update {
1385 my ($self) = @_; 1606 my ($self) = @_;
1386 1607
1387 $self->size_allocate ($self->size_request); 1608 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1388 ::refresh (); 1609 ::refresh ();
1389} 1610}
1390 1611
1391sub add { 1612sub add {
1392 my ($self, $widget) = @_; 1613 my ($self, $widget) = @_;
1393 1614
1394 $self->SUPER::add ($widget); 1615 $self->SUPER::add ($widget);
1395 1616
1396 $widget->size_allocate ($widget->size_request); 1617 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1397} 1618}
1398 1619
1399sub draw { 1620sub draw {
1400 my ($self) = @_; 1621 my ($self) = @_;
1401 1622

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines