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.88 by root, Wed Apr 12 21:01:46 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);
807
651 my ($hs, $ws) = (0, 0); 808 ($h, $w) = ($w, $h);
652 809
653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 810 my $children = $self->{children};
654 $hs += $self->max_row_height ($yi);
655 }
656 811
657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 812 my @h = map +($_->size_request)[0], @$children;
658 my $wm = 0; 813
659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 814 my $req_h = List::Util::sum @h;
660 $wm += $self->max_col_width ($xi) 815
816 if ($req_h > $h) {
817 # ah well, not enough space
818 $_ *= $h / $req_h for @h;
819 } else {
820 my $exp = List::Util::sum map $_->{expand}, @$children;
821 $exp ||= 1;
822
823 for (0 .. $#$children) {
824 my $child = $children->[$_];
825
826 my $alloc_h = $h[$_];
827 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
828 $h[$_] = $alloc_h;
661 } 829 }
662 if ($ws < $wm) { $ws = $wm }
663 } 830 }
664
665 return ($ws, $hs);
666}
667
668sub _draw {
669 my ($self) = @_;
670 831
671 my $y = 0; 832 my $y = 0;
672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 833 for (0 .. $#$children) {
834 my $child = $children->[$_];
673 my $x = 0; 835 my $h = $h[$_];
836 $child->size_allocate ($y, 0, $h, $w);
674 837
675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 838 $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 } 839 }
688} 840}
689 841
690############################################################################# 842#############################################################################
691 843
692package CFClient::UI::VBox; 844package CFClient::UI::VBox;
693 845
846# TODO: wrap into common Box base class
847
694our @ISA = CFClient::UI::Container::; 848our @ISA = CFClient::UI::Container::;
695
696use SDL::OpenGL;
697 849
698sub size_request { 850sub size_request {
699 my ($self) = @_; 851 my ($self) = @_;
700 852
701 my @alloc = map [$_->size_request], @{$self->{children}}; 853 my @alloc = map [$_->size_request], @{$self->{children}};
705 (List::Util::sum map $_->[1], @alloc), 857 (List::Util::sum map $_->[1], @alloc),
706 ) 858 )
707} 859}
708 860
709sub size_allocate { 861sub size_allocate {
710 my ($self, $w, $h) = @_; 862 my ($self, $x, $y, $w, $h) = @_;
711 863
712 return unless $self->{w} != $w || $self->{h} != $h; 864 $self->_size_allocate ($x, $y, $w, $h);
713
714 $self->{w} = $w;
715 $self->{h} = $h;
716
717 return unless $self->{h};
718 865
719 my $children = $self->{children}; 866 my $children = $self->{children};
720 867
721 my @h = map +($_->size_request)[1], @$children; 868 my @h = map +($_->size_request)[1], @$children;
722 869
723 my $req_h = List::Util::sum @h; 870 my $req_h = List::Util::sum @h;
724 871
725 if ($req_h > $h) { 872 if ($req_h > $h) {
726 # ah well, not enough space 873 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h; 874 $_ *= $h / $req_h for @h;
728 } else { 875 } else {
729 my @exp = grep $_->{expand}, @$children; 876 my $exp = List::Util::sum map $_->{expand}, @$children;
730 @exp = @$children unless @exp; 877 $exp ||= 1;
731 878
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) { 879 for (0 .. $#$children) {
735 my $child = $children->[$_]; 880 my $child = $children->[$_];
736 881
737 my $alloc_h = $h[$_]; 882 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 } 883 }
741 } 884 }
742 885
743 my $y = 0; 886 my $y = 0;
744 for (0 .. $#$children) { 887 for (0 .. $#$children) {
745 my $child = $children->[$_]; 888 my $child = $children->[$_];
746 my $h = $h[$_]; 889 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h); 890 $child->size_allocate (0, $y, $w, $h);
749 891
750 $y += $h; 892 $y += $h;
751 } 893 }
752} 894}
753 895
761 903
762sub new { 904sub new {
763 my ($class, %arg) = @_; 905 my ($class, %arg) = @_;
764 906
765 my $self = $class->SUPER::new ( 907 my $self = $class->SUPER::new (
766 fg => [1, 1, 1], 908 fg => [1, 1, 1],
767 height => $::FONTSIZE, 909 height => $::FONTSIZE,
768 text => "", 910 text => "",
769 align => -1, 911 align => -1,
912 padding => 2,
770 layout => new CFClient::Layout, 913 layout => new CFClient::Layout,
771 %arg 914 %arg
772 ); 915 );
773 916
774 $self->set_text ($self->{text}); 917 $self->set_text ($self->{text});
775 918
791 934
792 $self->{text} = $text; 935 $self->{text} = $text;
793 $self->{layout}->set_markup ($text); 936 $self->{layout}->set_markup ($text);
794 937
795 delete $self->{texture}; 938 delete $self->{texture};
939# $self->{w} = $self->{h} = -1;
796 $self->update; 940 $self->update;
797} 941}
798 942
799sub get_text { 943sub get_text {
800 my ($self, $text) = @_; 944 my ($self, $text) = @_;
805sub size_request { 949sub size_request {
806 my ($self) = @_; 950 my ($self) = @_;
807 951
808 $self->{layout}->set_width; 952 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height}); 953 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size 954 my ($w, $h) = $self->{layout}->size;
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 955
812# ( 956 (
813# $self->{texture}{w}, 957 $w + $self->{padding} * 2,
814# $self->{texture}{h}, 958 $h + $self->{padding} * 2,
815# ) 959 )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821} 960}
822 961
823sub size_allocate { 962sub size_allocate {
824 my ($self, $w, $h) = @_; 963 my ($self, $x, $y, $w, $h) = @_;
825 964
826 return unless $self->{w} != $w || $self->{h} != $h; 965 $self->_size_allocate ($x, $y, $w, $h) or return;
827 966
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture}; 967 delete $self->{texture};
830} 968}
831 969
832sub update { 970sub update {
833 my ($self) = @_; 971 my ($self) = @_;
839sub _draw { 977sub _draw {
840 my ($self) = @_; 978 my ($self) = @_;
841 979
842 my $tex = $self->{texture} ||= do { 980 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w}); 981 $self->{layout}->set_width ($self->{w});
982 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
844 new_from_layout CFClient::Texture $self->{layout} 983 new_from_layout CFClient::Texture $self->{layout}
845 }; 984 };
846 985
847 glEnable GL_BLEND; 986 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 987 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 989 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
851 990
852 glColor @{$self->{fg}}; 991 glColor @{$self->{fg}};
853 992
854 my $x = 993 my $x =
855 $self->{align} < 0 ? 0 994 $self->{align} < 0 ? $self->{padding}
856 : $self->{align} > 0 ? $self->{w} - $tex->{w} 995 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
857 : ($self->{w} - $tex->{w}) * 0.5; 996 : ($self->{w} - $tex->{w}) * 0.5;
858 997
998 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0;
859 $tex->draw_quad ($x, 0); 999 $tex->draw_quad (0, 0);
860 1000
861 glDisable GL_TEXTURE_2D; 1001 glDisable GL_TEXTURE_2D;
862 glDisable GL_BLEND; 1002 glDisable GL_BLEND;
863 glGetError and die;
864} 1003}
865 1004
866############################################################################# 1005#############################################################################
867 1006
868package CFClient::UI::Entry; 1007package CFClient::UI::Entry;
875sub new { 1014sub new {
876 my $class = shift; 1015 my $class = shift;
877 1016
878 $class->SUPER::new ( 1017 $class->SUPER::new (
879 fg => [1, 1, 1], 1018 fg => [1, 1, 1],
880 bg => [0, 0, 0, 0.4], 1019 bg => [0, 0, 0, 0.2],
881 active_bg => [1, 1, 1], 1020 active_bg => [1, 1, 1, 0.5],
882 active_fg => [0, 0, 0], 1021 active_fg => [0, 0, 0],
883 @_ 1022 @_
884 ) 1023 )
885} 1024}
886 1025
889 1028
890 $self->{last_activity} = $::NOW; 1029 $self->{last_activity} = $::NOW;
891 1030
892 $self->{text} = $text; 1031 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w}); 1032 $self->{layout}->set_width ($self->{w});
1033 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
894 1034
895 $text =~ s/./*/g if $self->{hidden}; 1035 $text =~ s/./*/g if $self->{hidden};
896 1036
897
898 $self->{layout}->set_markup ($self->escape_text ($text)); 1037 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
899 1038
900 $text = substr $text, 0, $self->{cursor}; 1039 $text = substr $text, 0, $self->{cursor};
901 utf8::encode $text; 1040 utf8::encode $text;
902 1041
903 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1042 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
910 1049
911 ($w + 1, $h) # add 1 for cursor 1050 ($w + 1, $h) # add 1 for cursor
912} 1051}
913 1052
914sub size_allocate { 1053sub size_allocate {
915 my ($self, $w, $h) = @_; 1054 my ($self, $x, $y, $w, $h) = @_;
916 1055
917 return unless $self->{w} != $w || $self->{h} != $h;
918
919 $self->SUPER::size_allocate ($w, $h); 1056 $self->SUPER::size_allocate ($x, $y, $w, $h);
920 1057
921 $self->_set_text ($self->{text}); 1058 $self->_set_text ($self->{text});
922} 1059}
923 1060
924sub set_text { 1061sub set_text {
945 substr $text, $self->{cursor}, 1, ""; 1082 substr $text, $self->{cursor}, 1, "";
946 } elsif ($sym == SDLK_LEFT) { 1083 } elsif ($sym == SDLK_LEFT) {
947 --$self->{cursor} if $self->{cursor}; 1084 --$self->{cursor} if $self->{cursor};
948 } elsif ($sym == SDLK_RIGHT) { 1085 } elsif ($sym == SDLK_RIGHT) {
949 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1086 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1087 } elsif ($sym == SDLK_HOME) {
1088 $self->{cursor} = 0;
1089 } elsif ($sym == SDLK_END) {
1090 $self->{cursor} = length $text;
950 } elsif ($uni) { 1091 } elsif ($uni) {
951 substr $text, $self->{cursor}++, 0, chr $uni; 1092 substr $text, $self->{cursor}++, 0, chr $uni;
952 } 1093 }
953 1094
954 $self->_set_text ($text); 1095 $self->_set_text ($text);
969 $self->SUPER::button_down ($ev, $x, $y); 1110 $self->SUPER::button_down ($ev, $x, $y);
970 1111
971 my $idx = $self->{layout}->xy_to_index ($x, $y); 1112 my $idx = $self->{layout}->xy_to_index ($x, $y);
972 1113
973 # byte-index to char-index 1114 # byte-index to char-index
974 my $text = $self->{layout}; 1115 my $text = $self->{text};
975 utf8::encode $text; 1116 utf8::encode $text;
976 $self->{cursor} = length substr $text, 0, $idx; 1117 $self->{cursor} = length substr $text, 0, $idx;
977 1118
978 $self->_set_text ($self->{text}); 1119 $self->_set_text ($self->{text});
979 $self->update; 1120 $self->update;
994 $self->{fg} = $self->{active_fg}; 1135 $self->{fg} = $self->{active_fg};
995 } else { 1136 } else {
996 glColor @{$self->{bg}}; 1137 glColor @{$self->{bg}};
997 } 1138 }
998 1139
1140 glEnable GL_BLEND;
1141 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
999 glBegin GL_QUADS; 1142 glBegin GL_QUADS;
1000 glVertex 0 , 0; 1143 glVertex 0 , 0;
1001 glVertex 0 , $self->{h}; 1144 glVertex 0 , $self->{h};
1002 glVertex $self->{w}, $self->{h}; 1145 glVertex $self->{w}, $self->{h};
1003 glVertex $self->{w}, 0; 1146 glVertex $self->{w}, 0;
1004 glEnd; 1147 glEnd;
1148 glDisable GL_BLEND;
1005 1149
1006 $self->SUPER::_draw; 1150 $self->SUPER::_draw;
1007 1151
1008 #TODO: force update every cursor change :( 1152 #TODO: force update every cursor change :(
1009 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1153 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1015 } 1159 }
1016} 1160}
1017 1161
1018############################################################################# 1162#############################################################################
1019 1163
1020package CFClient::UI::Slider; 1164package CFClient::UI::Button;
1021 1165
1022use strict; 1166our @ISA = CFClient::UI::Label::;
1023 1167
1168use SDL;
1024use SDL::OpenGL; 1169use SDL::OpenGL;
1025 1170
1026our @ISA = CFClient::UI::DrawBG::; 1171my @tex =
1027 1172 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1028sub size_request { 1173 qw(b1_button_active.png);
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_fg => [1, 1, 0],
1183# active_bg => [0, 0, 0, 0.5],
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 my $tex = $tex[0];
1203
1204 glEnable GL_BLEND;
1205 glEnable GL_TEXTURE_2D;
1206 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1207
1208 if ($GRAB == $self) {
1209 $self->{fg} = $self->{active_fg};
1210 }
1211
1212 glBindTexture GL_TEXTURE_2D, $tex->{name};
1213 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1214
1215 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
1216
1217 glDisable GL_TEXTURE_2D;
1218 glDisable GL_BLEND;
1219
1220 $self->SUPER::_draw;
1221}
1222
1223#############################################################################
1224
1225package CFClient::UI::CheckBox;
1226
1227our @ISA = CFClient::UI::DrawBG::;
1228
1229use SDL;
1230use SDL::OpenGL;
1231
1232sub new {
1233 my $class = shift;
1234
1235 $class->SUPER::new (
1236 padding => 2,
1237 fg => [1, 1, 1],
1238 active_fg => [1, 1, 0],
1239 state => 0,
1240 @_
1241 )
1242}
1243
1244sub size_request {
1245 my ($self) = @_;
1246
1247 ($self->{padding} * 2 + 6) x 2
1248}
1249
1250sub size_allocate {
1251 my ($self, $x, $y, $w, $h) = @_;
1252
1253 $self->_size_allocate ($x, $y, $w, $h);
1254}
1255
1256sub button_down {
1257 my ($self, $ev, $x, $y) = @_;
1258
1259 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1260 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1261 $self->{state} = !$self->{state};
1262 $self->emit (changed => $self->{state});
1263 }
1264}
1265
1266sub _draw {
1267 my ($self) = @_;
1268
1269 $self->SUPER::_draw;
1270
1271 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1272
1273 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1274
1275 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1276
1277 glBegin GL_LINE_LOOP;
1278 glVertex 0 , 0;
1279 glVertex 0 , $s;
1280 glVertex $s, $s;
1281 glVertex $s, 0;
1282 glEnd;
1283
1284 if ($self->{state}) {
1285 glBegin GL_LINES;
1286 glVertex 0 , 0;
1287 glVertex $s, $s;
1288 glVertex $s, 0;
1289 glVertex 0 , $s;
1290 glEnd;
1291 }
1292}
1293
1294#############################################################################
1295
1296package CFClient::UI::Slider;
1297
1298use strict;
1299
1300use SDL::OpenGL;
1301
1302our @ISA = CFClient::UI::DrawBG::;
1303
1304sub new {
1305 my $class = shift;
1306
1040 # range [value, low, high, page] 1307 # range [value, low, high, page]
1041 1308
1042 $class->SUPER::new ( 1309 my $self = $class->SUPER::new (
1043 fg => [1, 1, 1], 1310 fg => [1, 1, 1],
1044 active_fg => [0, 0, 0], 1311 active_fg => [0, 0, 0],
1045 range => [0, 0, 100, 10], 1312 range => [0, 0, 100, 10],
1313 req_w => 40,
1314 req_h => 10,
1046 vertical => 1, 1315 vertical => 0,
1047 @_ 1316 @_
1048 ) 1317 );
1318
1319 $self
1320}
1321
1322sub size_request {
1323 my ($self) = @_;
1324
1325 my $w = $self->{req_w};
1326 my $h = $self->{req_h};
1327
1328 $self->{vertical} ? ($h, $w) : ($w, $h)
1049} 1329}
1050 1330
1051sub button_down { 1331sub button_down {
1052 my ($self, $ev, $x, $y) = @_; 1332 my ($self, $ev, $x, $y) = @_;
1053 1333
1098 $value = int +($value - $lo) * $w / ($hi - $lo); 1378 $value = int +($value - $lo) * $w / ($hi - $lo);
1099 1379
1100 $w -= $page; 1380 $w -= $page;
1101 $page &= ~1; 1381 $page &= ~1;
1102 glTranslate $page * 0.5, 0, 0; 1382 glTranslate $page * 0.5, 0, 0;
1383 $page ||= 2;
1103 1384
1104 glColor @$fg; 1385 glColor @$fg;
1105 glBegin GL_LINES; 1386 glBegin GL_LINES;
1106 glVertex 0, 0; glVertex 0, $h; 1387 glVertex 0, 0; glVertex 0, $h;
1107 glVertex $w - 1, 0; glVertex $w - 1, $h; 1388 glVertex $w - 1, 0; glVertex $w - 1, $h;
1158sub key_up { 1439sub key_up {
1159} 1440}
1160 1441
1161sub size_request { 1442sub size_request {
1162 ( 1443 (
1163 1 + int $::WIDTH / 32, 1444 1 + 32 * int $::WIDTH / 32,
1164 1 + int $::HEIGHT / 32, 1445 1 + 32 * int $::HEIGHT / 32,
1165 ) 1446 )
1166} 1447}
1167 1448
1168sub update { 1449sub update {
1169 my ($self) = @_; 1450 my ($self) = @_;
1170 1451
1171 $self->{need_update} = 1; 1452 $self->{need_update} = 1;
1172 $self->SUPER::update; 1453 $self->SUPER::update;
1173} 1454}
1174 1455
1175sub _draw { 1456sub draw {
1176 my ($self) = @_; 1457 my ($self) = @_;
1177 1458
1178 if (delete $self->{need_update}) { 1459 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE; 1460 glNewList $self->{list}, GL_COMPILE;
1180 1461
1202 1483
1203 glEnable GL_TEXTURE_2D; 1484 glEnable GL_TEXTURE_2D;
1204 glEnable GL_BLEND; 1485 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1486 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1487 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1488 glColor 1, 1, 1, 1;
1207 1489
1208 my $sw4 = ($sw + 3) & ~3; 1490 my $sw4 = ($sw + 3) & ~3;
1209 my $darkness = "\x00" x ($sw4 * $sh); 1491 my $darkness = "\x00" x ($sw4 * $sh);
1210 1492
1211 for my $x (0 .. $sw - 1) { 1493 for my $x (0 .. $sw - 1) {
1259 1541
1260 glEndList; 1542 glEndList;
1261 } 1543 }
1262 1544
1263 glCallList $self->{list}; 1545 glCallList $self->{list};
1546
1547 if ($FOCUS != $self) {
1548 glEnable GL_BLEND;
1549 glColor 0, 0, 1, 0.4;
1550 glBegin GL_QUADS;
1551 glVertex 0, 0;
1552 glVertex 0, $::HEIGHT;
1553 glVertex $::WIDTH, $::HEIGHT;
1554 glVertex $::WIDTH, 0;
1555 glEnd;
1556 glDisable GL_BLEND;
1557 }
1264} 1558}
1265 1559
1266my %DIR = ( 1560my %DIR = (
1267 SDLK_KP8, [1, "north"], 1561 SDLK_KP8, [1, "north"],
1268 SDLK_KP9, [2, "northeast"], 1562 SDLK_KP9, [2, "northeast"],
1285 my $mod = $ev->key_mod; 1579 my $mod = $ev->key_mod;
1286 my $sym = $ev->key_sym; 1580 my $sym = $ev->key_sym;
1287 1581
1288 if ($sym == SDLK_KP5) { 1582 if ($sym == SDLK_KP5) {
1289 $::CONN->send ("command stay fire"); 1583 $::CONN->send ("command stay fire");
1584 } elsif ($sym == SDLK_a) {
1585 $::CONN->send ("command apply");
1290 } elsif (exists $DIR{$sym}) { 1586 } elsif (exists $DIR{$sym}) {
1291 if ($mod & KMOD_SHIFT) { 1587 if ($mod & KMOD_SHIFT) {
1292 $self->{shft}++; 1588 $self->{shft}++;
1293 $::CONN->send ("command fire $DIR{$sym}[0]"); 1589 $::CONN->send ("command fire $DIR{$sym}[0]");
1294 } elsif ($mod & KMOD_CTRL) { 1590 } elsif ($mod & KMOD_CTRL) {
1365sub size_request { 1661sub size_request {
1366 ($::WIDTH, $::HEIGHT) 1662 ($::WIDTH, $::HEIGHT)
1367} 1663}
1368 1664
1369sub size_allocate { 1665sub size_allocate {
1370 my ($self, $w, $h) = @_; 1666 my ($self, $x, $y, $w, $h) = @_;
1371 1667
1372 $self->SUPER::size_allocate ($w, $h); 1668 $self->_size_allocate ($x, $y, $w, $h);
1373 1669
1374 $_->size_allocate ($_->size_request) 1670 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1375 for @{$self->{children}}; 1671 for @{$self->{children}};
1376} 1672}
1377 1673
1378sub translate { 1674sub translate {
1379 my ($self, $x, $y) = @_; 1675 my ($self, $x, $y) = @_;
1382} 1678}
1383 1679
1384sub update { 1680sub update {
1385 my ($self) = @_; 1681 my ($self) = @_;
1386 1682
1387 $self->size_allocate ($self->size_request); 1683 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1388 ::refresh (); 1684 ::refresh ();
1389} 1685}
1390 1686
1391sub add { 1687sub add {
1392 my ($self, $widget) = @_; 1688 my ($self, $widget) = @_;
1393 1689
1394 $self->SUPER::add ($widget); 1690 $self->SUPER::add ($widget);
1395 1691
1396 $widget->size_allocate ($widget->size_request); 1692 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1397} 1693}
1398 1694
1399sub draw { 1695sub draw {
1400 my ($self) = @_; 1696 my ($self) = @_;
1401 1697

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines