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.82 by root, Wed Apr 12 02:00:06 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 $widget = $row->[$c]
642 if ($ws < $w) { $ws = $w } 747 or next;
748
749 my $col_w = $ws->[$c];
750
751 $widget->size_allocate ($x, $y, $col_w, $row_h);
752
753 $x += $col_w;
643 } 754 }
755
756 $y += $row_h;
644 } 757 }
645 return $ws; 758
646} 759}
760
761sub find_widget {
762 my ($self, $x, $y) = @_;
763
764 $x -= $self->{x};
765 $y -= $self->{y};
766
767 my $res;
768
769 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
770 $res = $_->find_widget ($x, $y)
771 and return $res;
772 }
773
774 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
775}
776
777sub _draw {
778 my ($self) = @_;
779
780 for (grep $_, @{$self->{children}}) {
781 $_->draw for grep $_, @$_;
782 }
783}
784
785#############################################################################
786
787package CFClient::UI::HBox;
788
789# TODO: wrap into common Box base class
790
791our @ISA = CFClient::UI::Container::;
647 792
648sub size_request { 793sub size_request {
649 my ($self) = @_; 794 my ($self) = @_;
650 795
796 my @alloc = map [$_->size_request], @{$self->{children}};
797
798 (
799 (List::Util::sum map $_->[0], @alloc),
800 (List::Util::max map $_->[1], @alloc),
801 )
802}
803
804sub size_allocate {
805 my ($self, $x, $y, $w, $h) = @_;
806
807 $self->_size_allocate ($x, $y, $w, $h) or return;
808
809 return unless $self->{w};
810
651 my ($hs, $ws) = (0, 0); 811 ($h, $w) = ($w, $h);
652 812
653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 813 my $children = $self->{children};
654 $hs += $self->max_row_height ($yi);
655 }
656 814
657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 815 my @h = map +($_->size_request)[0], @$children;
658 my $wm = 0; 816
659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 817 my $req_h = List::Util::sum @h;
660 $wm += $self->max_col_width ($xi) 818
819 if ($req_h > $h) {
820 # ah well, not enough space
821 $_ *= $h / $req_h for @h;
822 } else {
823 my $exp = List::Util::sum map $_->{expand}, @$children;
824 $exp ||= 1;
825
826 for (0 .. $#$children) {
827 my $child = $children->[$_];
828
829 my $alloc_h = $h[$_];
830 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
831 $h[$_] = $alloc_h;
661 } 832 }
662 if ($ws < $wm) { $ws = $wm }
663 } 833 }
664
665 return ($ws, $hs);
666}
667
668sub _draw {
669 my ($self) = @_;
670 834
671 my $y = 0; 835 my $y = 0;
672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 836 for (0 .. $#$children) {
837 my $child = $children->[$_];
673 my $x = 0; 838 my $h = $h[$_];
839 $child->size_allocate ($y, 0, $h, $w);
674 840
675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 841 $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 } 842 }
688} 843}
689 844
690############################################################################# 845#############################################################################
691 846
692package CFClient::UI::VBox; 847package CFClient::UI::VBox;
693 848
849# TODO: wrap into common Box base class
850
694our @ISA = CFClient::UI::Container::; 851our @ISA = CFClient::UI::Container::;
695
696use SDL::OpenGL;
697 852
698sub size_request { 853sub size_request {
699 my ($self) = @_; 854 my ($self) = @_;
700 855
701 my @alloc = map [$_->size_request], @{$self->{children}}; 856 my @alloc = map [$_->size_request], @{$self->{children}};
705 (List::Util::sum map $_->[1], @alloc), 860 (List::Util::sum map $_->[1], @alloc),
706 ) 861 )
707} 862}
708 863
709sub size_allocate { 864sub size_allocate {
710 my ($self, $w, $h) = @_; 865 my ($self, $x, $y, $w, $h) = @_;
711 866
712 return unless $self->{w} != $w || $self->{h} != $h; 867 $self->_size_allocate ($x, $y, $w, $h) or return;
713
714 $self->{w} = $w;
715 $self->{h} = $h;
716 868
717 return unless $self->{h}; 869 return unless $self->{h};
718 870
719 my $children = $self->{children}; 871 my $children = $self->{children};
720 872
722 874
723 my $req_h = List::Util::sum @h; 875 my $req_h = List::Util::sum @h;
724 876
725 if ($req_h > $h) { 877 if ($req_h > $h) {
726 # ah well, not enough space 878 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h; 879 $_ *= $h / $req_h for @h;
728 } else { 880 } else {
729 my @exp = grep $_->{expand}, @$children; 881 my $exp = List::Util::sum map $_->{expand}, @$children;
730 @exp = @$children unless @exp; 882 $exp ||= 1;
731 883
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) { 884 for (0 .. $#$children) {
735 my $child = $children->[$_]; 885 my $child = $children->[$_];
736 886
737 my $alloc_h = $h[$_]; 887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 } 888 }
741 } 889 }
742 890
743 my $y = 0; 891 my $y = 0;
744 for (0 .. $#$children) { 892 for (0 .. $#$children) {
745 my $child = $children->[$_]; 893 my $child = $children->[$_];
746 my $h = $h[$_]; 894 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h); 895 $child->size_allocate (0, $y, $w, $h);
749 896
750 $y += $h; 897 $y += $h;
751 } 898 }
752} 899}
753 900
761 908
762sub new { 909sub new {
763 my ($class, %arg) = @_; 910 my ($class, %arg) = @_;
764 911
765 my $self = $class->SUPER::new ( 912 my $self = $class->SUPER::new (
766 fg => [1, 1, 1], 913 fg => [1, 1, 1],
767 height => $::FONTSIZE, 914 height => $::FONTSIZE,
768 text => "", 915 text => "",
769 align => -1, 916 align => -1,
917 padding => 2,
770 layout => new CFClient::Layout, 918 layout => new CFClient::Layout,
771 %arg 919 %arg
772 ); 920 );
773 921
774 $self->set_text ($self->{text}); 922 $self->set_text ($self->{text});
775 923
791 939
792 $self->{text} = $text; 940 $self->{text} = $text;
793 $self->{layout}->set_markup ($text); 941 $self->{layout}->set_markup ($text);
794 942
795 delete $self->{texture}; 943 delete $self->{texture};
944# $self->{w} = $self->{h} = -1;
796 $self->update; 945 $self->update;
797} 946}
798 947
799sub get_text { 948sub get_text {
800 my ($self, $text) = @_; 949 my ($self, $text) = @_;
805sub size_request { 954sub size_request {
806 my ($self) = @_; 955 my ($self) = @_;
807 956
808 $self->{layout}->set_width; 957 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height}); 958 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size 959 my ($w, $h) = $self->{layout}->size;
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 960
812# ( 961 (
813# $self->{texture}{w}, 962 $w + $self->{padding} * 2,
814# $self->{texture}{h}, 963 $h + $self->{padding} * 2,
815# ) 964 )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821} 965}
822 966
823sub size_allocate { 967sub size_allocate {
824 my ($self, $w, $h) = @_; 968 my ($self, $x, $y, $w, $h) = @_;
825 969
826 return unless $self->{w} != $w || $self->{h} != $h; 970 $self->_size_allocate ($x, $y, $w, $h) or return;
827 971
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture}; 972 delete $self->{texture};
830} 973}
831 974
832sub update { 975sub update {
833 my ($self) = @_; 976 my ($self) = @_;
839sub _draw { 982sub _draw {
840 my ($self) = @_; 983 my ($self) = @_;
841 984
842 my $tex = $self->{texture} ||= do { 985 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w}); 986 $self->{layout}->set_width ($self->{w});
987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
844 new_from_layout CFClient::Texture $self->{layout} 988 new_from_layout CFClient::Texture $self->{layout}
845 }; 989 };
846 990
847 glEnable GL_BLEND; 991 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
851 995
852 glColor @{$self->{fg}}; 996 glColor @{$self->{fg}};
853 997
854 my $x = 998 my $x =
855 $self->{align} < 0 ? 0 999 $self->{align} < 0 ? $self->{padding}
856 : $self->{align} > 0 ? $self->{w} - $tex->{w} 1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
857 : ($self->{w} - $tex->{w}) * 0.5; 1001 : ($self->{w} - $tex->{w}) * 0.5;
858 1002
1003 glTranslate $x, $self->{padding}, 0;
859 $tex->draw_quad ($x, 0); 1004 $tex->draw_quad (0, 0);
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
889 1033
890 $self->{last_activity} = $::NOW; 1034 $self->{last_activity} = $::NOW;
891 1035
892 $self->{text} = $text; 1036 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w}); 1037 $self->{layout}->set_width ($self->{w});
1038 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
894 1039
895 $text =~ s/./*/g if $self->{hidden}; 1040 $text =~ s/./*/g if $self->{hidden};
896 1041
897
898 $self->{layout}->set_markup ($self->escape_text ($text)); 1042 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
899 1043
900 $text = substr $text, 0, $self->{cursor}; 1044 $text = substr $text, 0, $self->{cursor};
901 utf8::encode $text; 1045 utf8::encode $text;
902 1046
903 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1047 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
910 1054
911 ($w + 1, $h) # add 1 for cursor 1055 ($w + 1, $h) # add 1 for cursor
912} 1056}
913 1057
914sub size_allocate { 1058sub size_allocate {
915 my ($self, $w, $h) = @_; 1059 my ($self, $x, $y, $w, $h) = @_;
916 1060
917 return unless $self->{w} != $w || $self->{h} != $h;
918
919 $self->SUPER::size_allocate ($w, $h); 1061 $self->SUPER::size_allocate ($x, $y, $w, $h);
920 1062
921 $self->_set_text ($self->{text}); 1063 $self->_set_text ($self->{text});
922} 1064}
923 1065
924sub set_text { 1066sub set_text {
945 substr $text, $self->{cursor}, 1, ""; 1087 substr $text, $self->{cursor}, 1, "";
946 } elsif ($sym == SDLK_LEFT) { 1088 } elsif ($sym == SDLK_LEFT) {
947 --$self->{cursor} if $self->{cursor}; 1089 --$self->{cursor} if $self->{cursor};
948 } elsif ($sym == SDLK_RIGHT) { 1090 } elsif ($sym == SDLK_RIGHT) {
949 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1091 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1092 } elsif ($sym == SDLK_HOME) {
1093 $self->{cursor} = 0;
1094 } elsif ($sym == SDLK_END) {
1095 $self->{cursor} = length $text;
950 } elsif ($uni) { 1096 } elsif ($uni) {
951 substr $text, $self->{cursor}++, 0, chr $uni; 1097 substr $text, $self->{cursor}++, 0, chr $uni;
952 } 1098 }
953 1099
954 $self->_set_text ($text); 1100 $self->_set_text ($text);
969 $self->SUPER::button_down ($ev, $x, $y); 1115 $self->SUPER::button_down ($ev, $x, $y);
970 1116
971 my $idx = $self->{layout}->xy_to_index ($x, $y); 1117 my $idx = $self->{layout}->xy_to_index ($x, $y);
972 1118
973 # byte-index to char-index 1119 # byte-index to char-index
974 my $text = $self->{layout}; 1120 my $text = $self->{text};
975 utf8::encode $text; 1121 utf8::encode $text;
976 $self->{cursor} = length substr $text, 0, $idx; 1122 $self->{cursor} = length substr $text, 0, $idx;
977 1123
978 $self->_set_text ($self->{text}); 1124 $self->_set_text ($self->{text});
979 $self->update; 1125 $self->update;
994 $self->{fg} = $self->{active_fg}; 1140 $self->{fg} = $self->{active_fg};
995 } else { 1141 } else {
996 glColor @{$self->{bg}}; 1142 glColor @{$self->{bg}};
997 } 1143 }
998 1144
1145 glEnable GL_BLEND;
1146 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
999 glBegin GL_QUADS; 1147 glBegin GL_QUADS;
1000 glVertex 0 , 0; 1148 glVertex 0 , 0;
1001 glVertex 0 , $self->{h}; 1149 glVertex 0 , $self->{h};
1002 glVertex $self->{w}, $self->{h}; 1150 glVertex $self->{w}, $self->{h};
1003 glVertex $self->{w}, 0; 1151 glVertex $self->{w}, 0;
1004 glEnd; 1152 glEnd;
1153 glDisable GL_BLEND;
1005 1154
1006 $self->SUPER::_draw; 1155 $self->SUPER::_draw;
1007 1156
1008 #TODO: force update every cursor change :( 1157 #TODO: force update every cursor change :(
1009 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1158 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1015 } 1164 }
1016} 1165}
1017 1166
1018############################################################################# 1167#############################################################################
1019 1168
1020package CFClient::UI::Slider; 1169package CFClient::UI::Button;
1021 1170
1022use strict; 1171our @ISA = CFClient::UI::Label::;
1023 1172
1173use SDL;
1024use SDL::OpenGL; 1174use 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 1175
1037sub new { 1176sub new {
1038 my $class = shift; 1177 my $class = shift;
1039 1178
1179 $class->SUPER::new (
1180 padding => 4,
1181 fg => [1, 1, 1],
1182 bg => [1, 1, 1, 0.2],
1183 active_bg => [0, 0, 0, 0.5],
1184 active_fg => [1, 1, 0],
1185 border_fg => [1, 1, 0],
1186 @_
1187 )
1188}
1189
1190sub button_up {
1191 my ($self, $ev, $x, $y) = @_;
1192
1193 if ($x >= 0 && $x < $self->{w}
1194 && $y >= 0 && $y < $self->{h}) {
1195 $self->emit ("activate");
1196 }
1197}
1198
1199sub _draw {
1200 my ($self) = @_;
1201
1202 local $self->{fg} = $self->{fg};
1203
1204 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1206
1207 glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly
1208
1209 glBegin GL_LINE_LOOP;
1210 glColor @{$self->{border_fg}};
1211 glVertex 1 , 1;
1212 glVertex 1 , $self->{h} - 2;
1213 glVertex $self->{w} - 2, $self->{h} - 2;
1214 glVertex $self->{w} - 2, 1;
1215 glEnd;
1216
1217 if ($GRAB == $self) {
1218 glColor @{$self->{active_bg}};
1219 $self->{fg} = $self->{active_fg};
1220 } else {
1221 glColor @{$self->{bg}};
1222 }
1223
1224 glBegin GL_QUADS;
1225 glVertex 2 , 2;
1226 glVertex 2 , $self->{h} - 2;
1227 glVertex $self->{w} - 2, $self->{h} - 2;
1228 glVertex $self->{w} - 2, 2;
1229 glEnd;
1230 glDisable GL_BLEND;
1231
1232 $self->SUPER::_draw;
1233}
1234
1235#############################################################################
1236
1237package CFClient::UI::Slider;
1238
1239use strict;
1240
1241use SDL::OpenGL;
1242
1243our @ISA = CFClient::UI::DrawBG::;
1244
1245sub new {
1246 my $class = shift;
1247
1040 # range [value, low, high, page] 1248 # range [value, low, high, page]
1041 1249
1042 $class->SUPER::new ( 1250 my $self = $class->SUPER::new (
1043 fg => [1, 1, 1], 1251 fg => [1, 1, 1],
1044 active_fg => [0, 0, 0], 1252 active_fg => [0, 0, 0],
1045 range => [0, 0, 100, 10], 1253 range => [0, 0, 100, 10],
1254 req_w => 40,
1255 req_h => 10,
1046 vertical => 1, 1256 vertical => 0,
1047 @_ 1257 @_
1048 ) 1258 );
1259
1260 $self
1261}
1262
1263sub size_request {
1264 my ($self) = @_;
1265
1266 my $w = $self->{req_w};
1267 my $h = $self->{req_h};
1268
1269 $self->{vertical} ? ($h, $w) : ($w, $h)
1049} 1270}
1050 1271
1051sub button_down { 1272sub button_down {
1052 my ($self, $ev, $x, $y) = @_; 1273 my ($self, $ev, $x, $y) = @_;
1053 1274
1098 $value = int +($value - $lo) * $w / ($hi - $lo); 1319 $value = int +($value - $lo) * $w / ($hi - $lo);
1099 1320
1100 $w -= $page; 1321 $w -= $page;
1101 $page &= ~1; 1322 $page &= ~1;
1102 glTranslate $page * 0.5, 0, 0; 1323 glTranslate $page * 0.5, 0, 0;
1324 $page ||= 2;
1103 1325
1104 glColor @$fg; 1326 glColor @$fg;
1105 glBegin GL_LINES; 1327 glBegin GL_LINES;
1106 glVertex 0, 0; glVertex 0, $h; 1328 glVertex 0, 0; glVertex 0, $h;
1107 glVertex $w - 1, 0; glVertex $w - 1, $h; 1329 glVertex $w - 1, 0; glVertex $w - 1, $h;
1158sub key_up { 1380sub key_up {
1159} 1381}
1160 1382
1161sub size_request { 1383sub size_request {
1162 ( 1384 (
1163 1 + int $::WIDTH / 32, 1385 1 + 32 * int $::WIDTH / 32,
1164 1 + int $::HEIGHT / 32, 1386 1 + 32 * int $::HEIGHT / 32,
1165 ) 1387 )
1166} 1388}
1167 1389
1168sub update { 1390sub update {
1169 my ($self) = @_; 1391 my ($self) = @_;
1170 1392
1171 $self->{need_update} = 1; 1393 $self->{need_update} = 1;
1172 $self->SUPER::update; 1394 $self->SUPER::update;
1173} 1395}
1174 1396
1175sub _draw { 1397sub draw {
1176 my ($self) = @_; 1398 my ($self) = @_;
1177 1399
1178 if (delete $self->{need_update}) { 1400 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE; 1401 glNewList $self->{list}, GL_COMPILE;
1180 1402
1365sub size_request { 1587sub size_request {
1366 ($::WIDTH, $::HEIGHT) 1588 ($::WIDTH, $::HEIGHT)
1367} 1589}
1368 1590
1369sub size_allocate { 1591sub size_allocate {
1370 my ($self, $w, $h) = @_; 1592 my ($self, $x, $y, $w, $h) = @_;
1371 1593
1372 $self->SUPER::size_allocate ($w, $h); 1594 $self->_size_allocate ($x, $y, $w, $h);
1373 1595
1374 $_->size_allocate ($_->size_request) 1596 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1375 for @{$self->{children}}; 1597 for @{$self->{children}};
1376} 1598}
1377 1599
1378sub translate { 1600sub translate {
1379 my ($self, $x, $y) = @_; 1601 my ($self, $x, $y) = @_;
1382} 1604}
1383 1605
1384sub update { 1606sub update {
1385 my ($self) = @_; 1607 my ($self) = @_;
1386 1608
1387 $self->size_allocate ($self->size_request); 1609 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1388 ::refresh (); 1610 ::refresh ();
1389} 1611}
1390 1612
1391sub add { 1613sub add {
1392 my ($self, $widget) = @_; 1614 my ($self, $widget) = @_;
1393 1615
1394 $self->SUPER::add ($widget); 1616 $self->SUPER::add ($widget);
1395 1617
1396 $widget->size_allocate ($widget->size_request); 1618 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1397} 1619}
1398 1620
1399sub draw { 1621sub draw {
1400 my ($self) = @_; 1622 my ($self) = @_;
1401 1623

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines